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

openGauss每日一练第10天 - 学习心得体会

原创 陆巴巴无 2022-12-09
146

表空间的管理

表空间是数据的容器。掌握表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况。

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

-- 创建表空间
omm=# create tablespace t_tbspace relative location 'tablespace/t_tbspace_c1';
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

--使用test用户连接数据库
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".

--创建表t1
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 
--------------------
              16384
(1 row)

--查询表空间的oid
omm=> select oid ,spcname from pg_tablespace ; 
  oid  |  spcname   
-------+------------
  1663 | pg_default
  1664 | pg_global
 16397 | t_tbspace
(3 rows)


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

omm=> select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner 
omm-> from pg_class a where a.relkind in('r','i') and reltablespace ='0' and relname not like 'pg_%' and relname not like 'gs_%' and relname not like 'sql_%' 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_cont_query_oid_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
 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_defrelid_index      | i       |        1 | 8192 bytes     |             0 |       10
 streaming_cont_query_id_index            | i       |        1 | 8192 bytes     |             0 |       10
 streaming_stream                         | r       |        0 | 0 bytes        |             0 |       10

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

omm=> select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
omm-> omm->           reltablespace,relowner
   from pg_class a, pg_tablespace tb
omm->    where a.relkind in ('r', 'i')
omm->    and a.reltablespace=tb.oid
omm-> omm->    and tb.spcname='t_tbspace'
   order by a.relpages desc;
 relname | relkind | relpages | pg_size_pretty | reltablespace | relowner 
---------+---------+----------+----------------+---------------+----------
 foo     | r       |        0 | 0 bytes        |         16397 |    16398
 t1      | r       |        0 | 8192 bytes     |         16397 |    16398
(2 rows)


5、重命名表空间

omm=# alter tablespace t_tbspace rename to t_tbstest;
ALTER TABLESPACE

omm=# \db
             List of tablespaces
    Name    | Owner |        Location         
------------+-------+-------------------------
 pg_default | omm   | 
 pg_global  | omm   | 
 t_tbstest  | omm   | tablespace/t_tbspace_c1
(3 rows)


6、删除表空间

--先删除表空间里面的表
omm=# drop table test.t1;
DROP TABLE
omm=# drop table test.foo;
DROP TABLE

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

评论