-
学习目标
表空间是数据的容器。掌握表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况。 -
课程学习
通过使用表空间,管理员可以控制一个数据库安装的磁盘布局。
表空间对应于一个文件系统目录,假定数据库节点数据目录/opengauss/install/data/dn01是用户拥有读写权限的空目录。
openGauss自带了两个表空间:pg_default和pg_global。
-
默认表空间pg_default:用来存储非共享系统表、用户表、用户表index、临时表、临时表index、内部临时表的默认表空间。对应存储目录为实例数据目录下的base目录。
-
共享表空间pg_global:用来存放共享系统表的表空间。对应存储目录为实例数据目录下的global目录。
连接数据库 #第一次进入等待15秒 #数据库启动中... su - omm gsql -r
复制
执行如下命令创建用户jack
omm=# CREATE USER joh2 IDENTIFIED BY 'johnny@1234'; CREATE ROLE
复制
创建表空间、表
--执行下面的SQL语句,创建表空间j_tbspace: omm=# CREATE TABLESPACE j_tbspace RELATIVE LOCATION 'tablespace/j_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 16385 | joh_tbs | 10 | | | | t 16429 | ds_location1 | 10 | | | | t 16441 | j_tbspace | 10 | | | | t (5 rows) omm=# \db List of tablespaces Name | Owner | Location --------------+-------+------------------------- ds_location1 | omm | tablespace/tablespace_1 j_tbspace | omm | tablespace/j_tbspace1 joh_tbs | omm | tablespace/joh_ts pg_default | omm | pg_global | omm | (5 rows) --数据库系统管理员执行如下命令将“j_tbspace”表空间的访问权限赋予数据用户joh2。 omm=# GRANT CREATE ON TABLESPACE j_tbspace TO joh2; GRANT --执行如下命令,使用jack用户在指定表空间t_tbspace创建表。 omm=# \c omm joh2 Password for user joh2: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "omm" as user "joh2". omm=> CREATE TABLE foo(i int) TABLESPACE j_tbspace; CREATE TABLE
复制
查看表空间t_tbspace的大小
[omm@dbtestenv dn01]$ gsql -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# SELECT PG_TABLESPACE_SIZE('j_tbspace'); pg_tablespace_size -------------------- 25 (1 row)
复制
查看数据库在默认表空间下有哪些对象
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; 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_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_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_stream | r | 0 | 0 bytes | 0 | 10 snapshot | r | 0 | 0 bytes | 0 | 10 streaming_reaper_status | r | 0 | 0 bytes | 0 | 10 streaming_cont_query | r | 0 | 0 bytes | 0 | 10 (21 rows)
复制
查看数据库在非默认表空间下有哪些对象
--执行下面的SQL语句,查询数据库omm的非默认表空间j_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='j_tbspace' order by a.relpages desc; relname | relkind | relpages | pg_size_pretty | reltablespace | relowner ---------+---------+----------+----------------+---------------+---------- foo | r | 0 | 0 bytes | 16441 | 16437 (1 row)
复制
重命名表空间
--重命名表空间j_tbspace为app_tbs omm=# ALTER TABLESPACE j_tbspace RENAME TO app_tbs; ALTER TABLESPACE --执行下面的gsql命令,查看数据库当前的表空间信息: omm=# \db List of tablespaces Name | Owner | Location --------------+-------+------------------------- app_tbs | omm | tablespace/j_tbspace1 ds_location1 | omm | tablespace/tablespace_1 joh_tbs | omm | tablespace/joh_ts pg_default | omm | pg_global | omm | (5 rows)
复制
删除表空间
--用户必须是表空间的owner或者系统管理员才能删除表空间。需要先删除表空间的对象,再删除表空间app_ts: omm=# DROP TABLESPACE app_tbs; ERROR: tablespace "app_tbs" is not empty omm=# drop table joh2.foo; DROP TABLE omm=# DROP TABLESPACE app_tbs; DROP TABLESPACE
复制
- 总结
本节课学习了表空间相关维护知识以及表空间与表对象的关系验证。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
506次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
305次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
201次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
164次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
156次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
139次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
94次阅读
2025-04-18 10:49:53
Postgresql数据库单个Page最多存储多少行数据
maozicb
85次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
59次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
48次阅读
2025-04-16 10:33:59