如果数据库集群所在的初始磁盘分区或磁盘卷的空间不足,又无法进行扩展,可以在其他分区上创建一个新的表空间以供使用。
管理员可以根据数据库对象的使用统计优化系统的性能。例如,可以将访问频繁的索引存放到一个非常快速且可靠的磁盘上,比如昂贵的固态硬盘。与此同时,将很少使用或者对性能要求不高的归档数据表存储到廉价的低速磁盘上。
pg_default, template1 和 template0 默认的表空间,也是创建其他数据库时的默认表空间;对应的目录为 PGDATA/base。
pg_global,用于存储一些集群级别的共享系统表(system catalogs),例如 pg_database、pg_control;对应的目录为 PGDATA/global。
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+------------------------
pg_default | postgres |
pg_global | postgres |
(2 rows)
-bash-4.2$ ls -l /var/lib/pgsql/14/data/
total 60
drwx------. 8 postgres postgres 80 Jan 8 05:53 base
-rw-------. 1 postgres postgres 30 Jan 13 00:00 current_logfiles
drwx------. 2 postgres postgres 4096 Jan 12 16:28 global
drwx------. 2 postgres postgres 188 Jan 10 00:00 log
drwx------. 2 postgres postgres 6 Jan 4 16:56 pg_commit_ts
drwx------. 2 postgres postgres 6 Jan 4 16:56 pg_dynshmem
-rw-------. 1 postgres postgres 4305 Jan 4 17:58 pg_hba.conf
-rw-------. 1 postgres postgres 1636 Jan 4 16:56 pg_ident.conf
drwx------. 4 postgres postgres 68 Jan 13 13:55 pg_logical
drwx------. 4 postgres postgres 36 Jan 4 16:56 pg_multixact
drwx------. 2 postgres postgres 18 Jan 4 17:58 pg_notify
drwx------. 2 postgres postgres 6 Jan 4 16:56 pg_replslot
drwx------. 2 postgres postgres 6 Jan 4 16:56 pg_serial
drwx------. 2 postgres postgres 6 Jan 4 16:56 pg_snapshots
drwx------. 2 postgres postgres 6 Jan 4 17:58 pg_stat
drwx------. 2 postgres postgres 105 Jan 13 16:57 pg_stat_tmp
drwx------. 2 postgres postgres 18 Jan 4 16:56 pg_subtrans
drwx------. 2 postgres postgres 19 Jan 12 15:42 pg_tblspc
drwx------. 2 postgres postgres 6 Jan 4 16:56 pg_twophase
-rw-------. 1 postgres postgres 3 Jan 4 16:56 PG_VERSION
drwx------. 3 postgres postgres 60 Jan 4 16:56 pg_wal
drwx------. 2 postgres postgres 18 Jan 4 16:56 pg_xact
-rw-------. 1 postgres postgres 88 Jan 4 16:56 postgresql.auto.conf
-rw-------. 1 postgres postgres 23800 Jan 4 17:50 postgresql.conf
-rw-------. 1 postgres postgres 58 Jan 4 17:58 postmaster.opts
-rw-------. 1 postgres postgres 109 Jan 4 17:58 postmaster.pid
CREATE TABLESPACE tablespace_name
OWNER user_name
LOCATION 'directory';
[root@centos7 ~]# su - postgres
Last login: Wed Jan 9 09:29:19 EST 2019 on pts/0
-bash-4.2$ mkdir /var/lib/pgsql/app_tbs
-bash-4.2$ ll
total 0
drwx------. 4 postgres postgres 51 Jan 4 16:56 11
drwxr-xr-x. 2 postgres postgres 6 Jan 12 15:39 app_tbs
postgres=# CREATE TABLESPACE app_tbs LOCATION '/var/lib/pgsql/app_tbs';
CREATE TABLESPACE
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+------------------------
app_tbs | postgres | /var/lib/pgsql/app_tbs
pg_default | postgres |
pg_global | postgres |
(3 rows)
postgres=# select oid,* from pg_tablespace;
oid | spcname | spcowner | spcacl | spcoptions
-------+------------+----------+--------+------------
1663 | pg_default | 10 | |
1664 | pg_global | 10 | |
16470 | app_tbs | 10 | |
(3 rows)
-bash-4.2$ ls -l /var/lib/pgsql/app_tbs/
total 0
drwx------. 2 postgres postgres 6 Jan 12 15:42 PG_11_201809051
-bash-4.2$ ls -l /var/lib/pgsql/14/data/pg_tblspc/
total 0
lrwxrwxrwx. 1 postgres postgres 22 Jan 12 15:42 16470 -> /var/lib/pgsql/app_tbs
postgres=# GRANT CREATE ON TABLESPACE app_tbs TO tony;
GRANT
postgres=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size | Description
------------+----------+------------------------+---------------------+---------+---------+-------------
app_tbs | postgres | /var/lib/pgsql/app_tbs | postgres=C/postgres+| | 0 bytes |
| | | tony=C/postgres | | |
pg_default | postgres | | | | 46 MB |
pg_global | postgres | | | | 574 kB |
(3 rows)
testdb=> CREATE TABLE t(id int) tablespace app_tbs;
CREATE TABLE
testdb=> SELECT * FROM pg_tables WHERE tablename='t';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+-----------+------------+------------+------------+----------+-------------+-------------
public | t | tony | app_tbs | f | f | f | f
(1 row)
testdb=> SET default_tablespace = app_tbs;
SET
testdb=> CREATE TABLE t1(id int);
CREATE TABLE
testdb=> SELECT * FROM pg_tables WHERE tablename='t1';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+-----------+------------+------------+------------+----------+-------------+-------------
public | t1 | tony | app_tbs | f | f | f | f
(1 row)
ALTER TABLESPACE name RENAME TO new_name;
ALTER TABLESPACE name OWNER TO { new_owner | CURRENT_USER | SESSION_USER };
ALTER TABLESPACE name SET ( tablespace_option = value [, ... ] );
ALTER TABLESPACE name RESET ( tablespace_option [, ... ] );
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+------------------------
app_tbs | postgres | /var/lib/pgsql/app_tbs
pg_default | postgres |
pg_global | postgres |
(3 rows)
postgres=# ALTER TABLESPACE app_tbs RENAME TO tony_tbs;
ALTER TABLESPACE
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+------------------------
pg_default | postgres |
pg_global | postgres |
tony_tbs | postgres | /var/lib/pgsql/app_tbs
(3 rows)
postgres=# ALTER TABLESPACE tony_tbs OWNER TO tony;
ALTER TABLESPACE
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+------------------------
pg_default | postgres |
pg_global | postgres |
tony_tbs | tony | /var/lib/pgsql/app_tbs
(3 rows)
停止 PostgreSQL 服务器进程;
移动文件系统中的数据文件位置;
修改 PGDATA/pg_tblspc 目录中的符号链接文件(需要提前获取文件名),指向新的目录;
启动 PostgreSQL 服务器进程。
-bash-4.2$ whoami
postgres
-bash-4.2$ /usr/pgsql-14/bin/pg_ctl stop
waiting for server to shut down.... done
server stopped
-bash-4.2$ mv /var/lib/pgsql/app_tbs/ /var/lib/pgsql/tony_tbs
-bash-4.2$ ln -snf /var/lib/pgsql/tony_tbs /var/lib/pgsql/14/data/pg_tblspc/16470
-bash-4.2$ ls /var/lib/pgsql/14/data/pg_tblspc/16470 -l
lrwxrwxrwx. 1 postgres postgres 23 Jan 14 20:21 /var/lib/pgsql/14/data/pg_tblspc/16470 -> /var/lib/pgsql/tony_tbs
-bash-4.2$ /usr/pgsql-14/bin/pg_ctl start
waiting for server to start....2019-01-14 20:23:43.628 EST [20994] LOG: listening on IPv4 address "192.168.56.103", port 5432
2019-01-14 20:23:43.632 EST [20994] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-01-14 20:23:43.639 EST [20994] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-01-14 20:23:43.661 EST [20994] LOG: redirecting log output to logging collector process
2019-01-14 20:23:43.661 EST [20994] HINT: Future log output will appear in directory "log".
done
server started
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+-------------------------
pg_default | postgres |
pg_global | postgres |
tony_tbs | tony | /var/lib/pgsql/tony_tbs
(3 rows)
DROP TABLESPACE [ IF EXISTS ] name;
testdb=> DROP TABLESPACE tony_tbs;
ERROR: tablespace "tony_tbs" is not empty
testdb=> SELECT ts.spcname,
testdb-> cl.relname
testdb-> FROM pg_class cl
testdb-> JOIN pg_tablespace ts ON cl.reltablespace = ts.oid
testdb-> WHERE ts.spcname = 'tony_tbs';
spcname | relname
----------+---------
tony_tbs | t
tony_tbs | t1
(2 rows)
testdb=> DROP TABLE t, t1;
DROP TABLE
testdb=> DROP TABLESPACE tony_tbs;
DROP TABLESPACE
testdb=> \db
List of tablespaces
Name | Owner | Location
------------+----------+----------
pg_default | postgres |
pg_global | postgres |
(2 rows)
原文作者:SQL编程思想;如有侵权,请联系管理员删除。