標籤:
資料表空間:字面上理解就是表格儲存體的物理空間,其實包括資料庫的表、索引、序列等。
可以將資料表空間建立在伺服器的不同分區,這樣做的好處有:
一、如果初始化叢集所在分區已經用光,可以方便的其他分區上建立資料表空間已達到擴容的目的。
二、對於頻繁訪問的資料可以儲存在效能較高、較快的磁碟分割上,而不常用的資料存放區在便宜的較慢的磁碟分割上。
文法:
postgres=# \h create tablespace
Command: CREATE TABLESPACE
Description: define a new tablespace
Syntax:
CREATE TABLESPACE tablespace_name
[ OWNER user_name ]
LOCATION ‘directory‘
[ WITH ( tablespace_option = value [, ... ] ) ]
使用者必須有資料表空間所在目錄存取權限,所以在建立資料表空間之前需要在對應分區下建立相應的目錄,並為其分配許可權。
[[email protected] ~]# mkdir /usr/local/pgdata
[[email protected] ~]# chown postgres:postgres /usr/local/pgdata/
建立資料表空間樣本:
postgres=# create tablespace tbs_test owner postgres location ‘/usr/local/pgdata‘;CREATE TABLESPACE
建立資料表空間成功後,可在資料庫叢集目錄下看到一個新增的目錄pg_tblspc下有一個串連檔案51276,指向到/usr/local/pgdata下
[[email protected] ~]# ll /mnt/syncdata/pgsql941/data/pg_tblspc/total 0lrwxrwxrwx. 1 postgres postgres 17 Aug 30 02:06 51276 -> /usr/local/pgdata
[[email protected] ~]# ll /usr/local/pgdata/total 4drwx------. 2 postgres postgres 4096 Aug 30 02:06 PG_9.4_201409291
在此資料表空間內建立表:
postgres=# create table test(a int) tablespace tbs_test;CREATE TABLE
現在在資料表空間目錄下就會新增一個test表對應的檔案:
[[email protected] ~]# ll /usr/local/pgdata/PG_9.4_201409291/13003/51277
-rw-------. 1 postgres postgres 0 Aug 30 02:15 /usr/local/pgdata/PG_9.4_201409291/13003/51277
其中51277對應的是test表的relfilenode,13003是資料庫postgres的oid。
postgres=# select oid,datname from pg_database where datname = ‘postgres‘; oid | datname -------+---------- 13003 | postgres(1 row)postgres=# select relname,relfilenode from pg_class where relname=‘test‘; relname | relfilenode ---------+------------- test | 51277(1 row)
刪除資料表空間:
postgres=# \h drop tablespace
Command: DROP TABLESPACE
Description: remove a tablespace
Syntax:
DROP TABLESPACE [ IF EXISTS ] name
刪除資料表空間前必須要刪除該資料表空間下的所有資料庫物件,否則無法刪除。
如:
postgres=# drop tablespace if exists tbs_test;ERROR: tablespace "tbs_test" is not empty
刪除剛才在此資料表空間建立的表test,然後再刪除資料表空間。
postgres=# drop table if exists test;DROP TABLEpostgres=# drop tablespace if exists tbs_test;DROP TABLESPACE
postgresql 資料表空間建立、刪除