openGauss每日一练第8天
今日目标:学习表空间与数据库对象的关系。
系统视图
- pg_tables

系统视图
-
PG_CLASS系统表存储数据库对象信息及其之间的关系
-
PG_TABLESPACE
PG_TABLESPACE系统表存储表空间信息。

1.实验准备
- 创建表空间music_tbs ,并在该表空间下创建数据库musicdb
创建user1,并授予sysadmin权限
omm=# create tablespace music_tbs relative location 'tablespace/test_ts1';
CREATE TABLESPACE
omm=# create database musicdb with tablespace music_tbs ;
CREATE DATABASE
omm=# create user user1 sysadmin identified by 'gauss@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# \db
List of tablespaces
Name | Owner | Location
------------+-------+---------------------
music_tbs | omm | tablespace/test_ts1
pg_default | omm |
pg_global | omm |
(3 rows)
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
musicdb | omm | UTF8 | C | C |
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
(5 rows)
omm=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------------------------------------------------------------+-----------
gaussdb | Sysadmin | {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin | {}
omm=#
- openGauss的默认表空间查看
musicdb=> select datname,dattablespace,spcname from pg_database d, pg_tablespace t where d.dattablespace=t.oid;
datname | dattablespace | spcname
-----------+---------------+------------
template1 | 1663 | pg_default
omm | 1663 | pg_default
musicdb | 16389 | music_tbs
template0 | 1663 | pg_default
newdb1 | 16396 | newtbs1
postgres | 1663 | pg_default
(6 rows)
-
查询数据库的默认表空间上的对象
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;
2.课后作业
2.1.创建表空间newtbs1、 ds_location1,查看表空间
\db // 查看表表空间
omm=# create tablespace ds_location1 relative location 'tablespace/tablespace_1';
CREATE TABLESPACE
omm=# \db
List of tablespaces
Name | Owner | Location
--------------+-------+-------------------------
ds_location1 | omm | tablespace/tablespace_1
music_tbs | omm | tablespace/test_ts1
pg_default | omm |
pg_global | omm |
(4 rows)
omm=# create tablespace newtbs1 relative location 'tablespace/newtbs_1';
CREATE TABLESPACE
omm=# \db
List of tablespaces
Name | Owner | Location
--------------+-------+-------------------------
ds_location1 | omm | tablespace/tablespace_1
music_tbs | omm | tablespace/test_ts1
newtbs1 | omm | tablespace/newtbs_1
pg_default | omm |
pg_global | omm |
(5 rows)
omm=#
2.2 创建一个数据库newdb1,默认表空间为newtbs1
omm=# create database newdb1 with tablespace = newtbs1 ;
CREATE DATABASE
2.3 创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)
musicdb=> create user user5 sysadmin identified by 'gauss@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
musicdb=> \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=> \db
List of tablespaces
Name | Owner | Location
--------------+-------+-------------------------
ds_location1 | omm | tablespace/tablespace_1
music_tbs | omm | tablespace/test_ts1
newtbs1 | omm | tablespace/newtbs_1
pg_default | omm |
pg_global | omm |
(5 rows)
newdb1=> create table newt1(col1 char(50)) tablespace ds_location1 ;
CREATE TABLE
newdb1=>
2.4.查看表所在的表空间
newdb1=> create table newt1(col1 char(50)) tablespace ds_location1 ;
CREATE TABLE
newdb1=> \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | newt1 | table | user5 | {orientation=row,compression=no}
(1 row)
newdb1=> select * from pg_tables where tablename = 'newt1';
public | newt1 | user5 | ds_location1 | f | f | f | user5 | 2022-12-01 23:58:31.702802+08 | 2022-1
2-01 23:58:31.702802+08
(1 row)
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator | created |
last_ddl_time
------------+-----------+------------+--------------+------------+----------+-------------+--------------+-------------------------------+-------
------------------------
2.5 查看表空间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='ds_location1'
newdb1-> ;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
newt1 | r | 0 | 0 bytes | 16395 | 16404
(1 row)
newdb1=>
newdb1=>
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' ;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
(0 rows)
newdb1=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
musicdb | omm | UTF8 | C | C |
newdb1 | omm | UTF8 | C | C |
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
(6 rows)
newdb1=>
最后修改时间:2022-12-17 15:28:02
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




