1. 表空間的概念
PostgreSQL中的表空間允許在文件系統中定義用來存放表示數據庫對象的文件的位置。在PostgreSQL中表空間實際上就是給表指定一個存儲目錄。
2. 表空間的作用
官方解釋
通過使用表空間,管理員可以控制一個PostgreSQL安裝的磁盤布局。這么做至少有兩個用處。
- 如果初始化集簇所在的分區或者卷用光了空間,而又不能在邏輯上擴展或者做別的什么操作,那么表空間可以被創建在一個不同的分區上,直到系統可以被重新配置。
- 表空間允許管理員根據數據庫對象的使用模式來優化性能。例如,一個很頻繁使用的索引可以被放在非常快並且非常可靠的磁盤上,如一種非常貴的固態設備。同時,一個很少使用的或者對性能要求不高的存儲歸檔數據的表可以存儲在一個便宜但比較慢的磁盤系統上。
用一句話來講:能合理利用磁盤性能和空間,制定最優的物理存儲方式來管理數據庫表和索引。
3. 表空間跟數據庫關系
- 在Oracle數據庫中;一個表空間只屬於一個數據庫使用;而一個數據庫可以擁有多個表空間。屬於"一對多"的關系
- 在PostgreSQL集群中;一個表空間可以讓多個數據庫使用;而一個數據庫可以使用多個表空間。屬於"多對多"的關系。
4. 系統自帶表空間
- 表空間pg_default是用來存儲系統目錄對象、用戶表、用戶表index、和臨時表、臨時表index、內部臨時表的默認空間。對應存儲目錄$PADATA/base/
- 表空間pg_global用來存放系統字典表;對應存儲目錄$PADATA/global/
5. 查看表空間
列出現有的表空間
postgres=# \db List of tablespaces Name | Owner | Location ------------+----------+--------------------- pg_default | postgres | pg_global | postgres | tp_lottu | lottu | /data/pg_data/lottu (3 rows) postgres=# select oid,* from pg_tablespace; oid | spcname | spcowner | spcacl | spcoptions -------+------------+----------+--------+------------ 1663 | pg_default | 10 | | 1664 | pg_global | 10 | | 16385 | tp_lottu | 16384 | | (3 rows)
6. 創建表空間
Syntax:
CREATE TABLESPACE tablespace_name [ OWNER { new_owner | CURRENT_USER | SESSION_USER } ] LOCATION 'directory'
示例如下:
postgres=# \c lottu postgres You are now connected to database "lottu" as user "postgres". lottu=# CREATE TABLESPACE tsp01 OWNER lottu LOCATION '/data/pg_data/tsp'; CREATE TABLESPACE
目錄"/data/pg_data/tsp"必須是一個已有的空目錄,並且屬於PostgreSQL操作系統用戶
$ mkdir -p /data/pg_data/tsp $ chown -R postgres:postgres /data/pg_data/tsp
7. 權限分配
表空間的創建本身必須作為一個數據庫超級用戶完成,但在創建完之后之后你可以允許普通數據庫用戶來使用它.要這樣做,給數據庫普通用戶授予表空間上的CREATE權限。表、索引和整個數據庫都可以被分配到特定的表空間.
示例用戶"rax":為普通用戶。
lottu=# \c lottu01 rax You are now connected to database "lottu01" as user "rax". lottu01=> create table test_tsp(id int) tablespace tsp01; ERROR: permission denied for tablespace tsp01 lottu01=> \c lottu01 postgres You are now connected to database "lottu01" as user "postgres". lottu01=# GRANT CREATE ON TABLESPACE tsp01 TO rax; GRANT lottu01=# \c lottu01 rax You are now connected to database "lottu01" as user "rax". lottu01=> create table test_tsp(id int) tablespace tsp01; CREATE TABLE
8. 為數據庫指定默認表空間
Syntax:
ALTER DATABASE name SET TABLESPACE new_tablespace
以數據庫lottu01為例:
ALTER DATABASE lottu01 SET TABLESPACE tsp01; lottu01=> \c lottu01 lottu You are now connected to database "lottu01" as user "lottu".
注意1:執行該操作;不能連着對應數據庫操作
lottu01=# ALTER DATABASE lottu01 SET TABLESPACE tsp01; ERROR: cannot change the tablespace of the currently open database lottu01=# \c postgres postgres You are now connected to database "postgres" as user "postgres".
注意2:執行該操作;對應的數據庫不能存在表或者索引已經指定默認的表空間
postgres=# ALTER DATABASE lottu01 SET TABLESPACE tsp01; ERROR: some relations of database "lottu01" are already in tablespace "tsp01" HINT: You must move them back to the database's default tablespace before using this command. postgres=# \c lottu01 You are now connected to database "lottu01" as user "postgres". lottu01=# drop table test_tsp ; DROP TABLE lottu01=# create table test_tsp(id int); CREATE TABLE lottu01=# \c postgres postgres You are now connected to database "postgres" as user "postgres".
注意3:執行該操作;必須是沒有人連着對應的數據庫
postgres=# ALTER DATABASE lottu01 SET TABLESPACE tsp01; ERROR: database "lottu01" is being accessed by other users DETAIL: There is 1 other session using the database. postgres=# ALTER DATABASE lottu01 SET TABLESPACE tsp01; ALTER DATABASE
查看數據庫默認表空間
lottu01=# select d.datname,p.spcname from pg_database d, pg_tablespace p where d.datname='lottu01' and p.oid = d.dattablespace; datname | spcname ---------+--------- lottu01 | tsp01 (1 row)
9. 如何將表從一個表空間移到另一個表空間。
我們知道表空間pg_default是用來存儲系統目錄對象、用戶表、用戶表index、和臨時表、臨時表index、內部臨時表的默認空間。若沒指定默認表空間;表就所屬的表空間就是pg_default。"當然也可以通過參數設置"。而不是數據庫默認的表空間。這個時候我們可以將表移到默認的表空間
Syntax:
ALTER TABLE name SET TABLESPACE new_tablespace
將表從一個表空間移到另一個表空間
lottu01=# create table test_tsp03(id int) tablespace tp_lottu; CREATE TABLE lottu01=# alter table test_tsp03 set tablespace tsp01; ALTER TABLE
注意:該操作時會鎖表。
10. 臨時表空間
PostgreSQL的臨時表空間,通過參數temp_tablespaces進行配置,PostgreSQL允許用戶配置多個臨時表空間。配置多個臨時表空間時,使用逗號隔開。如果沒有配置temp_tablespaces 參數,臨時表空間對應的是默認的表空間pg_default。PostgreSQL的臨時表空間用來存儲臨時表或臨時表的索引,以及執行SQL時可能產生的臨時文件例如排序,聚合,哈希等。為了提高性能,一般建議將臨時表空間放在SSD或者IOPS,以及吞吐量較高的分區中。
$ mkdir -p /data/pg_data/temp_tsp $ chown -R postgres:postgres /data/pg_data/temp_tsp postgres=# CREATE TABLESPACE temp01 LOCATION '/data/pg_data/temp_tsp'; CREATE TABLESPACE postgres=# show temp_tablespaces ; temp_tablespaces ------------------ (1 row)
設置臨時表空間
- 會話級生效
postgres=# set temp_tablespaces = 'temp01'; SET
- 永久生效
- 修改參數文件postgresql.conf
- 執行pg_ctl reload
[postgres@Postgres201 data]$ grep "temp_tablespace" postgresql.conf temp_tablespaces = 'temp01' # a list of tablespace names, '' uses
查看臨時表空間
postgres=# show temp_tablespaces ; temp_tablespaces ------------------ temp01 (1 row)