第 10 天学习打卡,第 3 课和第 8 课已经对表空间的概念做了部分介绍,第 10 天的内容正式开始了对标空间的学习。本课学习目标是“掌握表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况”。
在线手册入口:创建和管理表空间。
作业
- 创建表空间 t_tbspace、用户 test,并使用 test,在这个表空间上创建表 t1
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 TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
CREATE TABLESPACE
omm=# CREATE USER test IDENTIFIED BY 'Tank@Modb';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
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 foo(i int) TABLESPACE t_tbspace;
CREATE TABLE
omm=>
- 查看表空间 t_tbspace 的 oid 和大小
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 | {omm=C/omm,test=C/omm} | | | t
(3 rows)
- 查看数据库在默认表空间下有哪些对象
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=# select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm-# omm-# reltablespace,relowner
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
---------+---------+----------+----------------+---------------+----------
foo | r | 0 | 0 bytes | 16389 | 16390
(1 row)
- 重命名表空间
omm=# ALTER TABLESPACE t_tbspace RENAME TO app_tbs;
ALTER TABLESPACE
omm=# \db
(3 rows)
omm=# List of tablespaces
Name | Owner | Location
------------+-------+-----------------------
app_tbs | omm | tablespace/t_tbspace1
pg_default | omm |
pg_global | omm |
omm=#
- 删除表空间
-- 用户必须是表空间的 owner 或者系统管理员才能删除表空间。需要先删除表空间的对象,再删除表空间app_ts:
omm=# drop table test.foo;
DROP TABLE
omm=# DROP TABLESPACE app_tbs;
DROP TABLESPACE
omm=# \db
List of tablespaces
Name | Owner | Location
------------+-------+----------
pg_default | omm |
pg_global | omm |
(2 rows)
命令总结
- 创建表空间
CREATE USER jack IDENTIFIED BY 'xxxxxxxxx';
- 查看表空间
SELECT spcname FROM pg_tablespace;
\db
- 查询表空间使用情况
SELECT PG_TABLESPACE_SIZE('example');
- 重命名表空间
ALTER TABLESPACE fastspace RENAME TO fspace;
- 删除表空间
DROP TABLESPACE fspace;
- 查看数据库在默认表空间下有哪些对象
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;
- 查看数据库在非默认表空间下有哪些对象
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;
课程总结
今天完成了 3 个课程,终于追赶上进度了!本节课掌握了表空间的相关操作,如创建、查看(OID 和使用情况)、修改和删除等。具体操作整理到了“命令总结”中。
周末愉快!
历史打卡记录:
openGauss 每日一练第 1 天|openGauss 数据库状态查看
openGauss 每日一练第 2 天|学习 openGauss 客户端工具 gsql 的使用
openGauss 每日一练第 3 天|openGauss 中一个数据库集簇对应多个数据库
openGauss 每日一练第 4 天|openGauss 中一个数据库可以被多个用户访问
openGauss 每日一练第 5 天|openGauss 中一个用户可以访问多个数据库
openGauss 每日一练第 6 天|openGauss 中用户一次只能连接到一个数据库
openGauss 每日一练第 7 天|openGauss 中一个数据库中可以创建多个模式
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




