AntDB上使用表空间
2019獨角獸企業重金招聘Python工程師標準>>>
查看表空間的語法幫助
postgres=# \h create tablespace Command: CREATE TABLESPACE Description: define a new tablespace Syntax: CREATE TABLESPACE tablespace_name[ OWNER { new_owner | CURRENT_USER | SESSION_USER } ]LOCATION 'directory'[ WITH ( tablespace_option = value [, ... ] ) ]創建表空間
連接coordinator:
create tablespace tst_tbs location '/home/shboss/antdb/data/tablespace';需要在集群涉及到的主機上都存在location指定的目錄,否則報錯:
postgres=# create tablespace tst_tbs location '/home/shboss/antdb/data/tablespace'; ERROR: Fail to process utility query on remote node. DETAIL: ERROR: directory "/home/shboss/antdb/data/tablespace" does not exist在各個主機上創建目錄:
remote_cmd "mkdir -p /home/shboss/antdb/data/tablespace"再次連接coordinator創建表空間:
postgres=# create tablespace tst_tbs location '/home/shboss/antdb/data/tablespace'; CREATE TABLESPACE查看表空間信息
數據庫級別:
postgres=# \dbList of tablespacesName | Owner | Location ------------+--------+------------------------------------pg_default | shboss | pg_global | shboss | tst_tbs | shboss | /home/shboss/antdb/data/tablespace (3 rows)postgres=# select * from pg_tablespace ;spcname | spcowner | spcacl | spcoptions ------------+----------+--------+------------pg_default | 10 | | pg_global | 10 | | tst_tbs | 10 | | (3 rows)文件系統:
coord節點:
[shboss@localhost1 pg_tblspc]$ pwd /home/shboss/antdb/data/coord/pg_tblspcdatanode 節點:
lrwxrwxrwx 1 shboss shboss 34 Jul 23 15:25 164514 -> /home/shboss/antdb/data/tablespace [shboss@localhost1 pg_tblspc]$ pwd /home/shboss/antdb/data/db1/pg_tblspc表空間路徑:
[shboss@localhost1 pg_tblspc]$ cd /home/shboss/antdb/data/tablespace [shboss@localhost1 tablespace]$ ll total 12 drwx------ 2 shboss shboss 4096 Jul 23 15:25 PG_9.6_201608131_coord1 drwx------ 2 shboss shboss 4096 Jul 23 15:25 PG_9.6_201608131_coord5 drwx------ 2 shboss shboss 4096 Jul 23 15:25 PG_9.6_201608131_db1_2 [shboss@localhost1 tablespace]$ tree . . ├── PG_9.6_201608131_coord1 ├── PG_9.6_201608131_coord5 └── PG_9.6_201608131_db1_2在表空間內建表
create table test_tbs (id int) tablespace tst_tbs;查看表空間中的數據文件:
[shboss@localhost1 tablespace]$ tree . . ├── PG_9.6_201608131_coord1 │?? └── 13603 │?? └── 476507 ├── PG_9.6_201608131_coord5 │?? └── 13603 │?? └── 18025 └── PG_9.6_201608131_db1_2└── 13597└── 164515使用其他普通用戶在tst_tbs 表空間下創建表:
bmsql5=> create table test_tbs (id int) tablespace tst_tbs; ERROR: permission denied for tablespace tst_tbs bmsql5=>解決辦法1:給普通用戶賦權
postgres=# grant create on tablespace tst_tbs to bmsql5_ora_fdw; GRANT再次建表成功:
bmsql5=> create table test_tbs (id int) tablespace tst_tbs; CREATE TABLE bmsql5=> \d+ test_tbsTable "bmsql5_ora_fdw.test_tbs"Column | Type | Modifiers | Storage | Stats target | Description --------+---------+-----------+---------+--------------+-------------id | integer | | plain | | Tablespace: "tst_tbs" Distribute By: HASH(id) Location Nodes: ALL DATANODESbmsql5=>在創建之前,可以查詢當前用戶表空間tst_tbs 是否有權限:
bmsql5=> select has_tablespace_privilege('tst_tbs','create');has_tablespace_privilege --------------------------t (1 row)revoke權限后再次查詢:
-- superuser postgres=# revoke create on tablespace tst_tbs from bmsql5_ora_fdw; REVOKE -- 普通用戶 bmsql5=> select has_tablespace_privilege('tst_tbs','create');has_tablespace_privilege --------------------------f (1 row)解決辦法2:修改表空間的屬主為表的創建用戶
alter tablespace tst_tbs owner to bmsql5_ora_fdw;再次建表成功:
bmsql5=> create table test_tbs (id int) tablespace tst_tbs; CREATE TABLE bmsql5=>解決辦法3:修改數據庫的默認表空間
alter database bmsql5 set tablespace tst_tbs;postgres=# alter database bmsql5 set tablespace tst_tbs; ALTER DATABASE Time: 323772.143 ms postgres=# postgres=# \l+ List of databasesName | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description -----------+--------+----------+---------+-------+-------------------+---------+------------+--------------------------------------------bmsql5 | shboss | UTF8 | C | C | | 26 GB | tst_tbs | db1 | shboss | UTF8 | C | C | | 1529 MB | pg_default | postgres | shboss | UTF8 | C | C | | 43 MB | pg_default | default administrative connection databasetemplate0 | shboss | UTF8 | C | C | =c/shboss +| 30 MB | pg_default | unmodifiable empty database| | | | | shboss=CTc/shboss | | | template1 | shboss | UTF8 | C | C | =c/shboss +| 30 MB | pg_default | default template for new databases| | | | | shboss=CTc/shboss | | | testdb | shboss | UTF8 | C | C | | 108 MB | pg_default | (6 rows)這個操作的耗時取決于數據庫的大小和磁盤的性能。且在數據文件移動過程中,該數據庫無法連接。
[shboss@localhost1 tablespace]$ psql -d bmsql5 psql: FATAL: database "bmsql5" does not exist DETAIL: It seems to have just been dropped or renamed.再次建表成功:
bmsql5=> create table test_tbs (id int) tablespace tst_tbs; CREATE TABLE bmsql5=> drop table test_tbs; DROP TABLE bmsql5=> create table test_tbs (id int); CREATE TABLE碰到的問題:
更改的數據庫上有連接的時候,沒法修改:
postgres=# alter database bmsql5 set tablespace tst_tbs; ERROR: database "bmsql5" is being accessed by other users DETAIL: There is 1 other session using the database.連接到修改的數據庫上也無法執行修改操作:
bmsql5=# alter database bmsql5 set tablespace tst_tbs; ERROR: cannot change the tablespace of the currently open database如果修改的數據庫中已經有對象在目標表空間中,也無法執行操作:
postgres=# alter database bmsql5 set tablespace tst_tbs; ERROR: some relations of database "bmsql5" are already in tablespace "tst_tbs" HINT: You must move them back to the database's default tablespace before using this command.查找指定表空間中的對象:
select relname,reltype,spcname from pg_class c, pg_tablespace tbs where c.reltablespace=tbs.oid and tbs.spcname='tst_tbs';查看表的表空間
postgres=# \d test_tbsTable "public.test_tbs"Column | Type | Modifiers --------+---------+-----------id | integer | Tablespace: "tst_tbs"如果表沒有使用所在數據庫的默認表空間,則會在\d 的時候顯示。
修改表的表空間
bmsql5=# select pg_relation_filepath('bmsql_stock');pg_relation_filepath ----------------------base/27636/461935 (1 row)bmsql5=# alter table bmsql_stock set tablespace tst_tbs; ALTER TABLE bmsql5=# select pg_relation_filepath('bmsql_stock');pg_relation_filepath -------------------------------------------------------pg_tblspc/476505/PG_9.6_201608131_coord1/27636/476515 (1 row)bmsql5=#表很大的話,會比較耗時,是個挪動文件的過程,同時發現filenode是變了的。
查看表空間大小
postgres=# select pg_tablespace_size('tst_tbs');pg_tablespace_size --------------------36757504 (1 row)postgres=# select pg_size_pretty(pg_tablespace_size('tst_tbs'));pg_size_pretty ----------------35 MB (1 row)刪除表空間
postgres=# drop tablespace tst_tbs; ERROR: tablespace "tst_tbs" is not empty如果表空間中有對象刪除,則無法刪除。
根據上面提供的查找指定表空間中的對象 語句找出對象移走或刪除后,再進行刪除:
postgres=# drop tablespace tst_tbs; DROP TABLESPACE查看主機上的文件:
[shboss@localhost1 tablespace]$ tree . .0 directories, 0 files [shboss@localhost1 tablespace]$ pwd /home/shboss/antdb/data/tablespace目錄已經為空。
轉載于:https://my.oschina.net/yafeishi/blog/1861567
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的AntDB上使用表空间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于提高代码复用性的几个知识点的回顾
- 下一篇: 调用远程service aidl接口定义