第 8 天学习打卡,本节课程的学习目标是“学习表空间与数据库对象的关系”,需理解一个数据库中的对象,可以位于不同的表空间中。
本节课是继续对表空间内容的扩展学习,上一次关于表空间的的课程是第 3 课“openGauss 中一个数据库集簇对应多个数据库”。
具体关于表空间的说明,请参考官方文档-“创建和管理表空间”。以下是作业操作记录。
作业
- 创建表空间 newtbs1、 ds_location1,查看表空间
omm=# CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/newtbs1_ts1'; CREATE TABLESPACE omm=# CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/ds_location1_ts1'; CREATE TABLESPACE omm=#
- 创建一个数据库 newdb1,默认表空间为 newtbs1
omm=# CREATE DATABASE newdb1 WITH TABLESPACE = newtbs1; CREATE DATABASE
- 创建用户 user5,并授予 SYSADMIN 权限,访问数据库 newdb1,在表空间 ds_location1 上,创建一个表 newt1(表结构自定义)
omm=# CREATE USER user5 IDENTIFIED BY 'Tank@Modb'; NOTICE: The encrypted password contains MD5 ciphertext, which is not secure. CREATE ROLE omm=# ALTER USER user5 SYSADMIN; ALTER ROLE omm=# omm=# \c newdb1 user5 Password for user user5: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "newdb1" as user "user5". newdb1=> newdb1=> create table newt1 (col1 char(10)) tablespace ds_location1; CREATE TABLE newdb1=>
- 查看表所在的表空间
newdb1=> select * from pg_tables where tablename = 'newt1'; schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator | created | last_ddl_time ------------+-----------+------------+--------------+------------+----------+-------------+--------------+----------------------------- --+------------------------------- public | newt1 | user5 | ds_location1 | f | f | f | user5 | 2022-12-03 14:01:56.085382+0 8 | 2022-12-03 14:01:56.085382+08 (1 row) newdb1=>
- 查看表空间 newtbs1、 ds_location1 上的对象
newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner
newdb1-> from pg_class a, pg_tablespace tb
newdb1-> where a.relkind in ('r', 'i')
newdb1-> and a.reltablespace=tb.oid
newdb1-> and tb.spcname='newtbs1'
newdb1-> order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
(0 rows)
newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner
newdb1-> from pg_class a, pg_tablespace tb
newdb1-> where a.relkind in ('r', 'i')
newdb1-> and a.reltablespace=tb.oid
newdb1-> and tb.spcname='ds_location1'
newdb1-> order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
newt1 | r | 0 | 0 bytes | 16390 | 16392
(1 row)
newdb1=>
命令汇总
- 查询表空间
/db
- 查询默认表空间
select datname,dattablespace,spcname from pg_database d, pg_tablespace t where d.dattablespace=t.oid;
- 查询数据库默认表空间中的对象
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'
order by a.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='tablespace_name'
order by a.relpages desc;
- 查询指定表对应的表空间
select * from pg_tables where tablename = 'table_name';
总结
本节课学习了表空间的操作,需要掌握上一部分命令汇总的相关语句和使用场景。创建对象对象时可灵活指定表空间。
历史打卡记录:
openGauss 每日一练第 1 天|openGauss 数据库状态查看
openGauss 每日一练第 2 天|学习 openGauss 客户端工具 gsql 的使用
openGauss 每日一练第 3 天|openGauss 中一个数据库集簇对应多个数据库
openGauss 每日一练第 4 天|openGauss 中一个数据库可以被多个用户访问
openGauss 每日一练第 5 天|openGauss 中一个用户可以访问多个数据库
最后修改时间:2022-12-03 14:16:16
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




