PostgreSQL的表空间

PostgreSQL的表空间

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

在 PostgreSQL 中,表空间(Tablespace)是一个可以用于存储数据库对象(如表、索引)的文件系统位置。PostgreSQL的表空间让你可以将数据库对象分散存储在不同的系统目录中,以优化磁盘I/O、管理数据存放、更好地进行性能调优,以及灵活地管理磁盘空间。(PostgreSQL的表空间更像是ORACLE当中的directory

创建表空间

使用 CREATE TABLESPACE 命令来创建一个新的表空间。你需要指定表空间的名称以及其对应的操作系统目录。目录必须由数据库超级用户所有,并且 PostgreSQL 服务器进程必须对该目录具有写权限。

示例1

创建表空间test1,路径为/pgdir/test1

[pg16@test ~]$ psql -p 5777
psql (16.2)
Type "help" for help.

postgres=# create tablespace test1 location '/pgdir/test1';
CREATE TABLESPACE
postgres=# SELECT spcname AS tablespace_name, pg_catalog.pg_tablespace_location(oid) AS location
postgres-# FROM pg_catalog.pg_tablespace
postgres-# WHERE spcname = 'test1';
 tablespace_name |   location   
-----------------+--------------
 test1           | /pgdir/test1
(1 row)

postgres=# 

使用表空间

创建表空间之后,可以在创建表或索引时指定表空间,或者将现有的表或索引迁移到新的表空间。

示例2

创建表t1,并指定表空间test1

postgres=# create table t1 (id int) tablespace test1;
CREATE TABLE
postgres=# select tablename,tablespace from pg_catalog.pg_tables where tablename='t1';
 tablename | tablespace 
-----------+------------
 t1        | test1
(1 row)

示例3

创建索引idx_t1,并指定表空间test2

postgres=# create index idx_t1 on t1 (id) tablespace test2;
CREATE INDEX
postgres=# select tablename,indexname,tablespace from pg_catalog.pg_indexes where tablename='t1';
 tablename | indexname | tablespace 
-----------+-----------+------------
 t1        | idx_t1    | test2
(1 row)

示例4

移动现有表t1到新表空间test3

要将现有的表移动到一个新的表空间,需要使用 ALTER TABLE 命令:

postgres=# select tablename,tablespace from pg_catalog.pg_tables where tablename='t1';
 tablename | tablespace 
-----------+------------
 t1        | test1
(1 row)

postgres=# alter table t1 set tablespace test3;
ALTER TABLE
postgres=# select tablename,tablespace from pg_catalog.pg_tables where tablename='t1';
 tablename | tablespace 
-----------+------------
 t1        | test3
(1 row)

示例5

移动现有索引idx_t1到新表空间test3

同样地,可以使用 ALTER INDEX 命令来移动索引:

postgres=# select tablename,indexname,tablespace from pg_catalog.pg_indexes where tablename='t1';
 tablename | indexname | tablespace 
-----------+-----------+------------
 t1        | idx_t1    | test2
(1 row)

postgres=# alter index idx_t1 set tablespace test3;
ALTER INDEX
postgres=# select tablename,indexname,tablespace from pg_catalog.pg_indexes where tablename='t1';
 tablename | indexname | tablespace 
-----------+-----------+------------
 t1        | idx_t1    | test3
(1 row)

删除表空间

删除表空间时,必须确保表空间为空,即表空间不能包含任何数据库对象(表、索引等)。删除表空间使用 DROP TABLESPACE 命令:

postgres=# select spcname as tablespace_name, pg_catalog.pg_tablespace_location(oid) as location from pg_catalog.pg_tablespace where spcname like 'test%';
 tablespace_name |   location   
-----------------+--------------
 test1           | /pgdir/test1
 test2           | /pgdir/test2
 test3           | /pgdir/test3
(3 rows)

postgres=# drop tablespace test1;
DROP TABLESPACE
postgres=# select spcname as tablespace_name, pg_catalog.pg_tablespace_location(oid) as location from pg_catalog.pg_tablespace where spcname like 'test%';
 tablespace_name |   location   
-----------------+--------------
 test2           | /pgdir/test2
 test3           | /pgdir/test3
(2 rows)

尝试删除表空间test3,报表空间非空的错误。

postgres=# drop tablespace test3;
ERROR:  tablespace "test3" is not empty

查看表空间信息

可以查询系统表来查看现有表空间以及其相关信息

postgres=# select spcname as tablespace_name, pg_catalog.pg_tablespace_location(oid) as location from pg_catalog.pg_tablespace;
 tablespace_name |   location   
-----------------+--------------
 pg_default      | 
 pg_global       | 
 test2           | /pgdir/test2
 test3           | /pgdir/test3
(4 rows)

这将列出所有的表空间。在 PostgreSQL 中,pg_default 是默认的表空间,pg_global 是用于全局对象的表空间。

管理建议

  1. 磁盘I/O优化:将高访问量的表或索引放置在不同的磁盘或SSD上,以分散I/O负载。
  2. 管理存储:使用不同的表空间来管理不同类型的数据,尤其是当数据库规模较大时,方便进行独立的备份与恢复操作。
  3. 数据分层:可以根据数据访问频率将不同的数据放在不同的表空间上,例如将历史数据放在相对慢速的大容量磁盘上,将热点数据放在高速SSD上。

注意事项

  1. 权限:创建表空间是一个需要特权的操作,通常需要超级用户来完成。
  2. 恢复:确保表空间目录被正确设置和备份,因为表空间存储的位置直接影响数据库的恢复过程。
  3. 安全性:确保表空间目录其他非数据库进程不能随意访问或修改,确保数据的完整性和安全性。

通过使用表空间,数据库管理员可以更灵活地管理存储和I/O,使得数据库性能和管理更加高效。

谨记:心存敬畏,行有所止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值