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

openGauss每日一练第10天|学习心得

原创 star 2022-12-09
235

表空间是数据的容器,第10天的学习内容是表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况。

一、练习环境

实训环境

二、练习内容及课程作业

本章学习了创建表空间、删除表空间、重命名表空间、查看表空间的情况的操作,接下来就是通过这些学习内容完成课程作业

在开始作业之前,我们要准备测试环境。

su - omm
gsql -r

结果如下:

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=# 


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

要完成以上操作,只需要输入以下sql命令即可

create tablespace t_tbspace relative location 'tablespace/tbs1';
CREATE USER test IDENTIFIED BY 'kunpeng@1234';
GRANT CREATE ON TABLESPACE t_tbspace TO test;
create table t1(id int) tablespace t_tbspace;

结果如下:


omm=# create tablespace t_tbspace relative location 'tablespace/tbs1';
CREATE TABLESPACE
omm=# CREATE USER test IDENTIFIED BY 'kunpeng@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# GRANT CREATE ON TABLESPACE t_tbspace TO test;
GRANT
omm=# 
omm=# create table t1(id int) tablespace t_tbspace; CREATE TABLE


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

要完成以上操作,只需要输入以下sql命令即可,可以看到oid是16389

select oid,* from pg_tablespace ;SELECT PG_TABLESPACE_SIZE('t_tbspace');


结果如下:


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)

omm=# 
omm=> SELECT PG_TABLESPACE_SIZE('t_tbspace'); pg_tablespace_size -------------------- 8192 (1 row) omm=>


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

要完成以上操作,只需要输入以下sql命令即可

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
omm->           ( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm(>                   reltablespace,relowner
omm(> omm(>             from pg_class a
omm(>             where a.relkind in ('r', 'i')  and reltablespace='0'
         )
omm->  select * 
omm->  from objectInDefaultTS 
omm->    where relname not like 'pg_%' and relname not like 'gs_%' and relname not like 'sql_%'
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_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_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_stream                         | r       |        0 | 0 bytes        |             0 |       10
 snapshot                                 | r       |        0 | 0 bytes        |             0 |       10
--More--


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

要完成以上操作,只需要输入以下sql命令即可

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


结果如下:

omm=#  select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm-#           reltablespace,relowner
omm-#    from pg_class a, pg_tablespace tb
omm-#    where a.relkind in ('r', 'i')
omm-#    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 
---------+---------+----------+----------------+---------------+----------
 t1      | r       |        0 | 0 bytes        |         16389 |       10
(1 row)

omm=# 


5、重命名表空间

要完成以上操作,只需要输入以下sql命令即可

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


结果如下:

omm=# ALTER TABLESPACE t_tbspace RENAME TO app_tbs;
omm=# ALTER TABLESPACE


6、删除表空间

要完成以上操作,只需要输入以下sql命令即可

--用户必须是表空间的owner或者系统管理员才能删除表空间。需要先删除表空间的对象,再删除表空间app_ts:
drop table test.t1 ;
DROP TABLESPACE app_tbs;

结果如下:

omm=# drop table test.t1;
DROP TABLE
omm=# drop tablespace app_tbs;
DROP TABLESPACE


三、总结

通过使用表空间,管理员可以控制一个数据库安装的磁盘布局。

表空间对应于一个文件系统目录,假定数据库节点数据目录/pg_location/mount1/path1是用户拥有读写权限的空目录。

openGauss自带了两个表空间:pg_default和pg_global。

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

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

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

评论