暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第10天 |openGauss逻辑结构:表空间管理

原创 zwtian 2022-12-03
348

不知不觉,今天已经是openGauss每日一练的第10天了,上一篇我们着重介绍和了解了表空间与数据库对象的关系,了解oid的含义,并做了相关的实验。今天我们重点来学习openGauss的逻辑结构-表空间管理相关的知识。还是以前的惯例,我们通过下面的实验来学习表空间管理相关的内容

实验

1、创建表空间t_tbspace、用户test,并使用test,在这个表空间上创建表t1

--执行如下命令创建用户test

omm=# CREATE USER test IDENTIFIED BY 'kunpeng@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE

--创建表空间t_tbspace、表t1

--执行下面的SQL语句,创建表空间t_tbspace:
omm=# CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
CREATE TABLESPACE
--查看系统有哪些表空间
omm=# select oid,* from pg_tablespace ;
  oid  |  spcname   | spcowner | spcacl | spcoptions | spcmaxsize | relative 
-------+------------+----------+--------+------------+------------+----------
  1663 | pg_default |       10 |        |            |            | f
  1664 | pg_global  |       10 |        |            |            | f
 16401 | t_tbspace  |       10 |        |            |            | t
或者:
omm=# \db
            List of tablespaces
    Name    | Owner |       Location        
------------+-------+-----------------------
 pg_default | omm   | 
 pg_global  | omm   | 
 t_tbspace  | omm   | tablespace/t_tbspace1
--数据库系统管理员执行如下命令将“t_tbspace”表空间的访问权限赋予数据用户test。
omm=# GRANT CREATE ON TABLESPACE t_tbspace TO test;
GRANT
--执行如下命令,使用test用户在指定表空间t_tbspace创建表。
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 t1(i int) TABLESPACE t_tbspace;
CREATE TABLE

2、查看表空间t_tbspace的oid和大小

--查看大小omm=# SELECT PG_TABLESPACE_SIZE('t_tbspace');
 pg_tablespace_size 
--------------------
               8192
--查看oid omm=# select oid,* from pg_tablespace ; oid | spcname | spcowner | spcacl | spcoptions | spcmaxsize | relative -------+------------+----------+------------------------+------------+------------+---------- 1663 | pg_default | 10 | | | | f 1664 | pg_global | 10 | | | | f 16401 | t_tbspace | 10 | {omm=C/omm,test=C/omm} | | | t (3 rows)

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;

4、查看数据库在非默认表空间下有哪些对象

--执行下面的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;

5、重命名表空间

--命名表空间t_tbspace为app_tbs
omm=# ALTER TABLESPACE t_tbspace RENAME TO app_tbs;
ALTER TABLESPACE
--执行下面的gsql命令,查看数据库当前的表空间信息:
omm=# \db
            List of tablespaces
    Name    | Owner |       Location        
------------+-------+-----------------------
 app_tbs    | omm   | tablespace/t_tbspace1
 pg_default | omm   | 
 pg_global  | omm   | 
(3 rows)

6、删除表空间

omm=# drop table test.t1 ;
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)

总结

通过使用表空间,管理员可以控制一个数据库安装的磁盘布局。表空间对应于一个文件系统目录,假定数据库节点数据目录/pg_location/mount1/path1是用户拥有读写权限的空目录。openGauss自带了两个表空间:pg_default和pg_global。

默认表空间pg_default:用来存储非共享系统表、用户表、用户表index、临时表、临时表index、内部临时表的默认表空间。对应存储目录为实例数据目录下的base目录。

共享表空间pg_global:用来存放共享系统表的表空间。对应存储目录为实例数据目录下的global目录。

通过以上的实验,我们得出结论,在openGauss中表空间是数据的容器。同时也掌握了表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况。希望通过这一节的学习,能够对您学习openGauss有所帮助。


最后修改时间:2022-12-03 10:35:50
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论