Skip to content

PosgreSQL三种表空间使用方式

第一种:懒汉模式 直接使用系统默认表空间pg_default,不需要知道表空间的概念

第二种:悲观模式

用户预先创建好文件系统目录,创建自定义表空间,再创建数据库,建库使用自定义表空间,以后使用过程中直接创建表无需关注表空间,无感知。这也是推荐比较良好的使用习惯

第三种:乐观模式 不够用或者出问题再扩容 不管是使用默认表空间或者自定义表空间也有可能会出现空间满的情况,这时可以在现有库对新表指定新的表空间来满足使用。

代码演示

sql
-- pg_default是初始化后的默认表空间,oid为1663
-- my_tablespace是新建的表空间,oid为24692
postgres=# select oid,spcname from pg_tablespace ;
  oid  |    spcname    
-------+---------------
  1663 | pg_default
  1664 | pg_global
 24692 | my_tablespace
(3 rows)

-- 初始化后默认有三个数据库,template0和template1,一般不能直接使用
-- 另一个postgres数据库oid为13593
postgres=# select oid,datname from pg_database ;
  oid  |  datname  
-------+-----------
 13593 | postgres
     1 | template1
 13592 | template0
(4 rows)

-- 第一种:postgres数据库创建数据表t
postgres=# create table t(id int,info text) ;
postgres=# select to_regclass('t')::oid;
 to_regclass 
-------------
       24674
(1 row)

--查看文件存储位置
postgres=# select pg_relation_filepath('t');
 pg_relation_filepath 
----------------------
 base/13593/24674
(1 row)
-- base为$PGDATA下的目录,默认表空间pg_default放在此目录下
-- 13593代表数据库postgres
-- 24674是上面t表的oid

-- 新建数据库test,并指定表空间
postgres=# create user test password'123456';
postgres=# create database test owner=test tablespace=my_tablespace;

-- 查看数据库,可以看到test数据库的oid为24693
postgres=# select oid,datname from pg_database ;
  oid  |  datname  
-------+-----------
 13593 | postgres
 24693 | test
     1 | template1
 13592 | template0
(4 rows)

-- 第二种:test数据库使用test用户创建数据表t
test=>  create table t(id int,info text) ;
test=>  select to_regclass('t')::oid;
 to_regclass 
-------------
       24694
(1 row)

--查看文件存储位置
test=> select pg_relation_filepath('t');
            pg_relation_filepath             
---------------------------------------------
 pg_tblspc/24692/PG_12_201909212/24693/24694
(1 row)
-- pg_tblspc下为符号链接
-- 24692是自定义表空间my_tablespace的oid
-- PG_12_201909212是一个特定命名,12是主版本号,201909212是目录版本号
-- 24693是test数据库的oid
-- 24694是t表的oid

$ ls -l pg_tblspc/
lrwxrwxrwx. 1 postgres dba 17 Jan  7 14:07 24692 -> /opt/mytablespace

-- 第三种:postgres数据库创建数据表t2,同时指定表空间
postgres=# create table t2 (id int,info text) tablespace my_tablespace ;
postgres=# select pg_relation_filepath('t2');
            pg_relation_filepath             
---------------------------------------------
 pg_tblspc/24692/PG_12_201909212/13593/24700
(1 row)
-- 24692是自定义表空间my_tablespace的oid
-- 13593是postgres数据库的oid
-- 24700是t2表的oid
-- pg_default是初始化后的默认表空间,oid为1663
-- my_tablespace是新建的表空间,oid为24692
postgres=# select oid,spcname from pg_tablespace ;
  oid  |    spcname    
-------+---------------
  1663 | pg_default
  1664 | pg_global
 24692 | my_tablespace
(3 rows)

-- 初始化后默认有三个数据库,template0和template1,一般不能直接使用
-- 另一个postgres数据库oid为13593
postgres=# select oid,datname from pg_database ;
  oid  |  datname  
-------+-----------
 13593 | postgres
     1 | template1
 13592 | template0
(4 rows)

-- 第一种:postgres数据库创建数据表t
postgres=# create table t(id int,info text) ;
postgres=# select to_regclass('t')::oid;
 to_regclass 
-------------
       24674
(1 row)

--查看文件存储位置
postgres=# select pg_relation_filepath('t');
 pg_relation_filepath 
----------------------
 base/13593/24674
(1 row)
-- base为$PGDATA下的目录,默认表空间pg_default放在此目录下
-- 13593代表数据库postgres
-- 24674是上面t表的oid

-- 新建数据库test,并指定表空间
postgres=# create user test password'123456';
postgres=# create database test owner=test tablespace=my_tablespace;

-- 查看数据库,可以看到test数据库的oid为24693
postgres=# select oid,datname from pg_database ;
  oid  |  datname  
-------+-----------
 13593 | postgres
 24693 | test
     1 | template1
 13592 | template0
(4 rows)

-- 第二种:test数据库使用test用户创建数据表t
test=>  create table t(id int,info text) ;
test=>  select to_regclass('t')::oid;
 to_regclass 
-------------
       24694
(1 row)

--查看文件存储位置
test=> select pg_relation_filepath('t');
            pg_relation_filepath             
---------------------------------------------
 pg_tblspc/24692/PG_12_201909212/24693/24694
(1 row)
-- pg_tblspc下为符号链接
-- 24692是自定义表空间my_tablespace的oid
-- PG_12_201909212是一个特定命名,12是主版本号,201909212是目录版本号
-- 24693是test数据库的oid
-- 24694是t表的oid

$ ls -l pg_tblspc/
lrwxrwxrwx. 1 postgres dba 17 Jan  7 14:07 24692 -> /opt/mytablespace

-- 第三种:postgres数据库创建数据表t2,同时指定表空间
postgres=# create table t2 (id int,info text) tablespace my_tablespace ;
postgres=# select pg_relation_filepath('t2');
            pg_relation_filepath             
---------------------------------------------
 pg_tblspc/24692/PG_12_201909212/13593/24700
(1 row)
-- 24692是自定义表空间my_tablespace的oid
-- 13593是postgres数据库的oid
-- 24700是t2表的oid