学习目标
表空间是数据的容器。掌握表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况。
前面每日一练链接
openGauss 每日一练第 1 天 | openGauss 数据库状态查看
openGauss 每日一练第 2 天 | 学习 gsql 命令行的使用
openGauss 每日一练第 3 天 | openGauss 数据库状态查看
openGauss 每日一练第 4 天 | openGauss 中一个数据库可以被多个用户访问
openGauss 每日一练第 5 天 | openGauss 中一个用户可以访问多个数据库
openGauss 每日一练第 6 天 | openGauss 中用户一次只能连接到一个数据库
openGauss 每日一练第 7 天 | openGauss 中一个数据库中可以创建多个模式
openGauss 每日一练第 8 天 | openGauss 中一个数据库可以存储在多个表空间中
openGauss 每日一练第 9 天 | openGauss 中一个表空间可以存储多个数据库
课程学习
通过使用表空间,管理员可以控制一个数据库安装的磁盘布局。
表空间对应于一个文件系统目录,假定数据库节点数据目录 /pg_location/mount1/path1 是用户拥有读写权限的空目录。
openGauss 自带了两个表空间:pg_default 和 pg_global。
默认表空间 pg_default:用来存储非共享系统表、用户表、用户表 index、临时表、临时表 index、内部临时表的默认表空间。对应存储目录为实例数据目录下的 base 目录。
共享表空间 pg_global:用来存放共享系统表的表空间。对应存储目录为实例数据目录下的 global 目录。
1.执行如下命令创建用户 jieke
CREATE USER jieke IDENTIFIED BY 'JiekeXu_1234';
2.创建表空间、表
--执行下面的 SQL 语句,创建表空间 t_tbspace:
CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
--查看系统有哪些表空间
select oid,* from pg_tablespace ;
或
\db
--数据库系统管理员执行如下命令将 “t_tbspace” 表空间的访问权限赋予数据用户jieke。
GRANT CREATE ON TABLESPACE t_tbspace TO jieke;
--执行如下命令,使用 jieke 用户在指定表空间 jieke 创建表。
\c omm jieke
CREATE TABLE foo(i int) TABLESPACE t_tbspace;
\q


3.查看表空间 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)
4.查看数据库在默认表空间下有哪些对象
with objectInDefaultTS as
( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),
reltablespace,relowner
from pg_class a
where 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;

5.查看数据库在非默认表空间下有哪些对象
--执行下面的SQL语句,查询数据库studentdb的非默认表空间t_tbspace下有哪些对象:
select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
reltablespace,relowner
from pg_class a, pg_tablespace tb
where a.relkind in ('r', 'i')
and a.reltablespace=tb.oid
and tb.spcname='t_tbspace'
order by a.relpages desc;

6.重命名表空间
--命名表空间 t_tbspace 为 app_tbs
ALTER TABLESPACE t_tbspace RENAME TO app_tbs;
--执行下面的 gsql 命令,查看数据库当前的表空间信息:
\db

7.删除表空间
--用户必须是表空间的 owner 或者系统管理员才能删除表空间。需要先删除表空间的对象,再删除表空间 app_ts,不然会报 ERROR: tablespace "app_tbs" is not empty
drop table jieke.foo;
DROP TABLESPACE app_tbs;
学习目标
1、创建表空间 t_tbspace、用户 jieke,并使用 jieke,在这个表空间上创建表 t1
create tablespace t_tbspace relative location 'tablespace/t_tbspace';
CREATE USER jieke IDENTIFIED BY 'JiekeXu_1234';
alter user jieke sysadmin;
-- 或 GRANT CREATE ON TABLESPACE t_tbspace TO jieke;
\c omm jieke
create table t1(id int) tablespace t_tbspace;

2、查看表空间 t_tbspace 的 oid 和大小
SELECT PG_TABLESPACE_SIZE('t_tbspace');
select oid,spcname from pg_tablespace where spcname='t_tbspace';

3、查看数据库在默认表空间下有哪些对象
with objectInDefaultTS as
( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),
reltablespace,relowner
from pg_class a
where 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
e(a.oid)), ( select relname, relkind, relpages,pg_size_pretty(pg_relation_size
omm(> reltablespace,relowner
omm(> from pg_class a
omm(> where a.relkind in ('r', 'i') and reltablespace='0'
omm(> )
omm-> select *
omm-> from objectInDefaultTS
like 'sql_%'e relname not like 'pg_%' and relname not like 'gs_%' and relname not
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
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
snapshot_pkey | i | 1 | 8192 bytes |
0 | 10
statement_history_time_idx | 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_id_index | i | 1 | 8192 bytes |
0 | 10
streaming_cont_query_oid_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
statement_history | 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
(21 rows)
4、查看数据库在非默认表空间下有哪些对象
--执行下面的SQL语句,查询数据库 omm 的非默认表空间 t_tbspace 下有哪些对象:
select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
reltablespace,relowner
from pg_class a, pg_tablespace tb
where a.relkind in ('r', 'i')
and a.reltablespace=tb.oid
and tb.spcname='t_tbspace'
order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
t1 | r | 0 | 0 bytes | 16403 | 16404
(1 row)
5、重命名表空间
--命名表空间 t_tbspace 为 jieke_tbs
ALTER TABLESPACE t_tbspace RENAME TO jieke_tbs;
--执行下面的 gsql 命令,查看数据库当前的表空间信息:
\db

6、删除表空间
--用户必须是表空间的 owner 或者系统管理员才能删除表空间。需要先删除表空间的对象,再删除表空间 jieke_tbs,不然会报 ERROR: tablespace "jieke_tbs" is not empty
根据上面第四节查看表空间下都有哪些对象,然后删除表对象。
select schemaname,tablename,tableowner,tablespace from pg_tables where tablespace ='jieke_tbs';
omm=# schemaname | tablename | tableowner | tablespace
------------+-----------+------------+------------
jieke | t1 | jieke | jieke_tbs
(1 row)
drop table jieke.t1;
DROP TABLESPACE jieke_tbs;

欢迎关注我的公众号【JiekeXu DBA之路】,第一时间一起学习新知识!
————————————————————————————
公众号:JiekeXu DBA之路
CSDN :https://blog.csdn.net/JiekeXu
墨天轮:https://www.modb.pro/u/4347
腾讯云:https://cloud.tencent.com/developer/user/5645107
————————————————————————————





