接着昨天继续学习openGauss,今天是第10天了。今天学习内容是openGauss逻辑结构:表空间管理。
老规矩,先登陆墨天轮为我准备的实训实验室
root@modb:~# su - omm
omm@modb:~$ gsql -r
omm=# CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/tablespace_1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb1 WITH TABLESPACE = t_tbspace ;
CREATE DATABASE
omm=# CREATE USER test IDENTIFIED BY 'test@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER test SYSADMIN;
omm=# ALTER ROLE
omm=# create table t1 (col varchar(100)) TABLESPACE t_tbspace ;
CREATE TABLE
omm=# \db
List of tablespaces
Name | Owner | Location
------------+-------+-------------------------
pg_default | omm |
pg_global | omm |
t_tbspace | omm | tablespace/tablespace_1
–查看系统有哪些表空间
omm=# select oid,* from pg_tablespace ;
oid | spcname | spcowner | spcacl | spcoptions | spcmaxsize | relative
-------+------------+----------+--------+------------+------------+----------
1663 | pg_default | 10 | | | | f
1664 | pg_global | 10 | | | | f
16389 | t_tbspace | 10 | | | | t
(3 rows)
–查看表空间大小
omm=# SELECT PG_TABLESPACE_SIZE('t_tbspace');
pg_tablespace_size
--------------------
12754948
(1 row)
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-# omm-# select *
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 | reltablespace | relowner
------------------------------------------+---------+----------+----------------+---------------+----------
streaming_gather_agg_index | i | 2 | 16 kB | 0 | 10
snapshot_id_key | i | 1 | 8192 bytes | 0 | 10
snapshot_pkey | i | 1 | 8192 bytes | 0 | 10
statement_history_time_idx | i | 1 | 8192 bytes | 0 | 10
streaming_cont_query_lookupidxid_index | i | 1 | 8192 bytes | 0 | 10
streaming_cont_query_schema_change_index | i | 1 | 8192 bytes | 0 | 10
streaming_reaper_status_id_index | i | 1 | 8192 bytes | 0 | 10
streaming_reaper_status_oid_index | i | 1 | 8192 bytes | 0 | 10
streaming_cont_query_id_index | i | 1 | 8192 bytes | 0 | 10
streaming_cont_query_oid_index | i | 1 | 8192 bytes | 0 | 10
streaming_stream_oid_index | i | 1 | 8192 bytes | 0 | 10
streaming_stream_relid_index | i | 1 | 8192 bytes | 0 | 10
streaming_cont_query_relid_index | i | 1 | 8192 bytes | 0 | 10
streaming_cont_query_defrelid_index | i | 1 | 8192 bytes | 0 | 10
streaming_cont_query_matrelid_index | i | 1 | 8192 bytes | 0 | 10
plan_table_data | r | 0 | 0 bytes | 0 | 10
streaming_stream | r | 0 | 0 bytes | 0 | 10
snapshot | r | 0 | 0 bytes | 0 | 10
streaming_reaper_status | r | 0 | 0 bytes | 0 | 10
streaming_cont_query | r | 0 | 0 bytes | 0 | 10
--More-- statement_history | r | 0 | 0 bytes | 0 | 10
–执行下面的SQL语句,查询数据库studentdb的非默认表空间t_tbspace下有哪些对象:
omm=# select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm-# reltablespace,relowner
omm-# from pg_class a, pg_tablespace tb
omm-# 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 | 16389 | 10
(1 row)
–命名表空间t_tbspace为app_tbs
omm=# ALTER TABLESPACE t_tbspace RENAME TO app_tbs;
ALTER TABLESPACE
查看变更后表空间名称
omm=# \db
pg_default | omm |
pg_global | omm |
(3 rows)
omm=# List of tablespaces
Name | Owner | Location
------------+-------+-------------------------
app_tbs | omm | tablespace/tablespace_1
–用户必须是表空间的owner或者系统管理员才能删除表空间。需要先删除表空间的对象,再删除表空间app_ts:
omm=# drop table t1;
DROP TABLE
omm=# DROP TABLESPACE app_tbs;
ERROR: tablespace "app_tbs" is not empty
omm=# drop database musicdb1 ;
DROP DATABASE
omm=# DROP TABLESPACE app_tbs;
DROP TABLESPACE