表空间是数据的容器。掌握表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况。
通过使用表空间,管理员可以控制一个数据库安装的磁盘布局。
表空间对应于一个文件系统目录,假定数据库节点数据目录/pg_location/mount1/path1是用户拥有读写权限的空目录。
openGauss自带了两个表空间:pg_default和pg_global。
1.执行如下命令创建用户jack
CREATE USER jack IDENTIFIED BY ‘kunpeng@1234’;
2.创建表空间、表
--执行下面的SQL语句,创建表空间t_tbspace:
CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
--查看系统有哪些表空间
select oid,* from pg_tablespace ;
或
\db
--数据库系统管理员执行如下命令将“t_tbspace”表空间的访问权限赋予数据用户jack。
GRANT CREATE ON TABLESPACE t_tbspace TO jack;
--执行如下命令,使用jack用户在指定表空间t_tbspace创建表。
\c omm jack
CREATE TABLE foo(i int) TABLESPACE t_tbspace;
\q
root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.omm=# CREATE USER jack IDENTIFIED BY 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
CREATE TABLESPACE
omm=# select oid,* from pg_tablespace;oid | spcname | spcowner | spcacl | spcoptions | spcmaxsize | relative
-------+------------+----------+--------+------------+------------+----------1663 | pg_default | 10 | | | | f1664 | pg_global | 10 | | | | f16389 | music_tbs | 10 | | | | t16398 | newtbs1 | 10 | | | | t16406 | t_tbspace | 10 | | | | t
omm=# (5 rows)
omm=# \dbList of tablespacesName | Owner | Location
------------+-------+-----------------------music_tbs | omm | tablespace/test_ts1newtbs1 | omm | tablespace/test_ts2pg_default | omm | pg_global | omm | t_tbspace | omm | tablespace/t_tbspace1
(5 rows) ^
omm=# GRANT CREATE ON TABLESPACE t_tbspace TO jack;
GRANT
omm=# \c omm jack
Password for user jack:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "omm" as user "jack".
omm=> CREATE TABLE foo(i int) TABLESPACE t_tbspace;
CREATE TABLE
omm=> \q
omm@modb:~$
3.查看表空间t_tbspace的大小
gsql -r
SELECT PG_TABLESPACE_SIZE(‘t_tbspace’);
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.omm=# SELECT PG_TABLESPACE_SIZE('t_tbspace');pg_tablespace_size
--------------------8192
(1 row)omm=#
4.查看数据库在默认表空间下有哪些对象
with objectInDefaultTS as( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relownerfrom pg_class awhere a.relkind in ('r', 'i') and reltablespace='0')select * from objectInDefaultTS where relname not like 'pg_%' and relname not like 'gs_%' and relname not like 'sql_%'order by relpages desc;
omm=# with objectInDefaultTS as
omm-# ( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm(# reltablespace,relowner
omm(# from pg_class a
omm(# where a.relkind in ('r', 'i') and reltablespace='0'
omm(# omm-# )select *
omm-# from objectInDefaultTS
omm-# omm-# where relname not like 'pg_%' and relname not like 'gs_%' and relnameike 'sql_%'order by relpages desc;relname | relkind | relpages | pg_size_pretty | re
ltablespace | relowner
------------------------------------------+---------+----------+----------------+---
------------+----------streaming_gather_agg_index | i | 2 | 16 kB | 0 | 10snapshot_id_key | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_lookupidxid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_schema_change_index | i | 1 | 8192 bytes | 0 | 10streaming_reaper_status_id_index | i | 1 | 8192 bytes | 0 | 10streaming_reaper_status_oid_index | i | 1 | 8192 bytes | 0 | 10snapshot_pkey | i | 1 | 8192 bytes | 0 | 10statement_history_time_idx | i | 1 | 8192 bytes | 0 | 10streaming_stream_oid_index | i | 1 | 8192 bytes | 0 | 10streaming_stream_relid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_relid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_defrelid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_id_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_oid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_matrelid_index | i | 1 | 8192 bytes | 0 | 10plan_table_data | r | 0 | 0 bytes | 0 | 10statement_history | r | 0 | 0 bytes | 0 | 10streaming_stream | r | 0 | 0 bytes | 0 | 10snapshot | r | 0 | 0 bytes | 0 | 10streaming_reaper_status | r | 0 | 0 bytes | 0 | 10streaming_cont_query | r | 0 | 0 bytes | 0 | 10
(21 rows)omm=#
5.查看数据库在非默认表空间下有哪些对象
--执行下面的SQL语句,查询数据库studentdb的非默认表空间t_tbspace下有哪些对象:select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relownerfrom pg_class a, pg_tablespace tbwhere a.relkind in ('r', 'i')and a.reltablespace=tb.oidand tb.spcname='t_tbspace'order by a.relpages desc;
omm=# select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm-# reltablespace,relowner
omm-# omm-# from pg_class a, pg_tablespace tbwhere a.relkind in ('r', 'i')
omm-# and a.reltablespace=tb.oid
omm-# and tb.spcname='t_tbspace'
omm-# order by a.relpages desc;relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------foo | r | 0 | 0 bytes | 16406 | 16402
(1 row)omm=#
6.重命名表空间
–命名表空间t_tbspace为app_tbs
ALTER TABLESPACE t_tbspace RENAME TO app_tbs;
–执行下面的gsql命令,查看数据库当前的表空间信息:
\db
omm=# ALTER TABLESPACE t_tbspace RENAME TO app_tbs;
ALTER TABLESPACE
omm=# \dbList of tablespacesName | Owner | Location
------------+-------+-----------------------app_tbs | omm | tablespace/t_tbspace1music_tbs | omm | tablespace/test_ts1newtbs1 | omm | tablespace/test_ts2pg_default | omm | pg_global | omm |
(5 rows)omm=#
7.删除表空间
--用户必须是表空间的owner或者系统管理员才能删除表空间。需要先删除表空间的对象,再删除表空间app_ts:
drop table jack.foo ;
DROP TABLESPACE app_tbs;
\db
omm=# drop table jack.foo ;
DROP TABLE
omm=# DROP TABLESPACE app_tbs;
DROP TABLESPACE
omm=# \dbList of tablespacesName | Owner | Location
------------+-------+---------------------music_tbs | omm | tablespace/test_ts1newtbs1 | omm | tablespace/test_ts2pg_default | omm | pg_global | omm |
(4 rows)omm=#
--创建用户test
CREATE USER test IDENTIFIED BY 'hello@1234';
\du
--创建表空间t_tbspace
CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
\db
--数据库系统管理员执行如下命令将“t_tbspace”表空间的访问权限赋予数据用户test。
GRANT CREATE ON TABLESPACE t_tbspace TO test;
\c omm test
CREATE TABLE t1(id int primary key,c1 char(9)) TABLESPACE t_tbspace;
\q
root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.omm=# CREATE USER test IDENTIFIED BY 'hello@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# \duList of rolesRole name | Attributes | Member of
-----------+-------------------------------------------------------------------------
-----------------------------------------+-----------gaussdb | Sysadmin | {}jack | | {}| {}user1 | Sysadmin omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitor
admin, Operatoradmin, Policyadmin, UseFT | {}test | | {}omm=# CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
CREATE TABLESPACE
omm=# \dbmusic_tbs | omm | tablespace/test_ts1newtbs1 | omm | tablespace/test_ts2pg_default | omm | List of tablespacesName | Owner | Location
------------+-------+-----------------------
omm=# pg_global | omm | t_tbspace | omm | tablespace/t_tbspace1
(5 rows)omm=# GRANT CREATE ON TABLESPACE t_tbspace TO test;
GRANT
omm=# \c omm test
Password for user test:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "omm" as user "test".
omm=> CREATE TABLE t1(id int primary key,c1 char(9)) TABLESPACE t_tbspace;
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
CREATE TABLE
omm=>
select oid,* from pg_tablespace;
SELECT PG_TABLESPACE_SIZE(‘t_tbspace’);
omm=> select oid,* from pg_tablespace;oid | spcname | spcowner | spcacl | spcoptions | spcmaxsize | r
elative
-------+------------+----------+------------------------+------------+------------+--
--------1663 | pg_default | 10 | | | | f1664 | pg_global | 10 | | | | f16389 | music_tbs | 10 | | | | t16398 | newtbs1 | 10 | | | | t16424 | t_tbspace | 10 | {omm=C/omm,test=C/omm} | | | t
(5 rows)omm=> SELECT PG_TABLESPACE_SIZE('t_tbspace');pg_tablespace_size
--------------------8192
(1 row)omm=>
with objectInDefaultTS as( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relownerfrom pg_class awhere a.relkind in ('r', 'i') and reltablespace='0')select * from objectInDefaultTS where relname not like 'pg_%' and relname not like 'gs_%' and relname not like 'sql_%'order by relpages desc;
omm=# with objectInDefaultTS as
omm-# ( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm(# reltablespace,relowner
omm(# from pg_class a
omm(# where a.relkind in ('r', 'i') and reltablespace='0'
omm(# omm-# select * )
omm-# from objectInDefaultTS
omm-# where relname not like 'pg_%' and relname not like 'gs_%' and relname not like 'sql_%'
omm-# order by relpages desc;relname | relkind | relpages | pg_size_pretty | rel
tablespace | relowner
------------------------------------------+---------+----------+----------------+----
-----------+---------- streaming_reaper_status_id_index | i | 1 | 8192 bytes | 0 | 10streaming_reaper_status_oid_index | i | 1 | 8192 bytes | streaming_gather_agg_index | i | 2 | 16 kB | 0 | 10statement_history_time_idx | i | 1 | 8192 bytes | 0 | 10snapshot_id_key | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_lookupidxid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_schema_change_index | i | 1 | 8192 bytes | 0 | 100 | 10snapshot_pkey | i | 1 | 8192 bytes | streaming_stream_oid_index | i | 1 | 8192 bytes |
--More-- 0 | 10t1_pkey | i | 1 | 8192 bytes | 0 | 164200 | 10streaming_stream_relid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_relid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_defrelid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_id_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_oid_index | i | 1 | 8192 bytes | 0 | 10streaming_cont_query_matrelid_index | i | 1 | 8192 bytes | 0 | 10plan_table_data | r | 0 | 0 bytes | 0 | 10statement_history | r | 0 | 0 bytes | 0 | 10streaming_stream | r | 0 | 0 bytes | 0 | 10snapshot | r | 0 | 0 bytes | 0 | 10streaming_reaper_status | r | 0 | 0 bytes | 0 | 10streaming_cont_query | r | 0 | 0 bytes | 0 | 10
(22 rows)
--执行下面的SQL语句,查询数据库studentdb的非默认表空间t_tbspace下有哪些对象:select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relownerfrom pg_class a, pg_tablespace tbwhere a.relkind in ('r', 'i')and a.reltablespace=tb.oidand tb.spcname='t_tbspace'order by a.relpages desc;
omm=# omm-# select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner
omm-# omm-# from pg_class a, pg_tablespace tb
omm-# where a.relkind in ('r', 'i')and a.reltablespace=tb.oid
omm-# and tb.spcname='t_tbspace'
omm-# order by a.relpages desc;relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------t1 | r | 0 | 0 bytes | 16424 | 16420
(1 row)omm=#
–命名表空间t_tbspace为new_tbspace
ALTER TABLESPACE t_tbspace RENAME TO new_tbspace;
–执行下面的gsql命令,查看数据库当前的表空间信息:
\db
omm=# ALTER TABLESPACE t_tbspace RENAME TO new_tbspace;
ALTER TABLESPACE
omm=# \dbList of tablespacesName | Owner | Location
-------------+-------+-----------------------music_tbs | omm | tablespace/test_ts1new_tbspace | omm | tablespace/t_tbspace1newtbs1 | omm | tablespace/test_ts2pg_default | omm | pg_global | omm |
(5 rows)omm=#
omm=# drop table test.t1;
DROP TABLE
omm=# drop tablespace new_tbspace;
DROP TABLESPACE
omm=# \dbList of tablespacesName | Owner | Location
------------+-------+---------------------music_tbs | omm | tablespace/test_ts1newtbs1 | omm | tablespace/test_ts2pg_default | omm | pg_global | omm |
(4 rows)omm=#