文章目录

  • 前言
  • 📣 1.表空间概述
  • 📣 2.表空间优点
  • 📣 3.默认表空间
  • 📣 4.表空间管理
  • ✨ 4.1 创建表空间
  • ✨ 4.2 删除表空间
  • ✨ 4.3 修改表空间名称
  • 📣 5.表空间运维


前言


本篇介绍了openGauss的表空间的日常管理及运维


📣 1.表空间概述

表空间是数据库的逻辑划分,所有的数据库对象都存放在指定的表空间中。
但主要存放的是表, 所以称作表空间。

对于openGauss数据库,表空间是一个目录,可以存在多个表空间,每个表空间所对应的目录存储的是其所包含的数据库的各种物理文件。openGauss的表空间这一特性和Oracle不一样,Oracle的表空间是一逻辑概念,Oracle表空间不是一个目录,其数据是存放于数据文件之上的,一个表空间可以对应着一个或多个物理的数据文件。
openGauss的表空间是一个目录,其仅起到物理隔离的作用,对于其管理功能更依赖于表空间所在的文件系统。

【openGauss实战12】表空间管理_sql

📣 2.表空间优点

1)如果初始化数据库所在的分区或者卷空间已满,又不能逻辑上扩展更多空间,可以在不同的分 区上创建和使用表空间,直到系统重新配置空间。
2)表空间允许管理员根据数据库对象的使用模式安排数据位置,从而提高性能。
3)如频繁使用的索引可以放在性能稳定且运算速度较快的磁盘上。
4)如存储归档的数据,很少使用的或者对性能要求不高的表可以存储在运算速度较慢的磁盘上。
5)表空间可以控制数据库数据占用的磁盘空间,当表空间所在磁盘的使用率达到90%时,数据库 将被设置为只读模式,当磁盘使用率降到90%以下时,数据库将恢复到读写模式。
6)表空间对应于一个文件系统目录,且用户需要拥有读写权限的空目录。

📣 3.默认表空间

在安装部署openGauss后,会创建两个默认的表空间,分别是pg_default和pg_global。

1)默认表空间pg_default:用来存储非共享系统表、用户表、用户表index、临时表、临时表index、内部临时表的默认表空间。对应存储目录为实例数据目录下的base目录。
2)共享表空间pg_global:用来存放共享系统表的表空间及系统字典表,对应存储目录为实例数据目录下的global目录。

[omm@opengauss ~]$ gsql -d postgres -p 15400
openGauss=# \db
      List of tablespaces
    Name    | Owner | Location
------------+-------+----------
 pg_default | omm   |
 pg_global  | omm   |
(2 rows)
openGauss=# select oid,* from pg_tablespace;
 oid  |  spcname   | spcowner | spcacl | spcoptions | spcmaxsize | relative
------+------------+----------+--------+------------+------------+----------
 1663 | pg_default |       10 |        |            |            | f
 1664 | pg_global  |       10 |        |            |            | f
(2 rows)

##默认表空间存放位置
[root@opengauss base]# pwd
/app/openGauss/install/data/dn/base
[root@opengauss base]# ls
1  15646  15651  16385  16394  16410  16411  16412  pgsql_tmp

[root@opengauss global]# ls
15055      15292      15300_fsm  15309      15358  15368_fsm  15376      15434      15539      15566      config_exec_params      pg_internal.init.92848
15055_fsm  15294      15300_vm   15310      15359  15368_vm   15376_fsm  15438      15540      15567      pg_control              pg_remain_segs
15055_vm   15295      15302      15311      15360  15370      15376_vm   15440      15559      15569      pg_control.backup
15057      15296      15303      15311_fsm  15361  15371      15378      15441      15561      15570      pg_dw_0
15058      15296_fsm  15305      15311_vm   15363  15372      15379      15449      15562      15647      pg_dw_ext_chunk
15135      15296_vm   15306      15313      15365  15372_fsm  15400      15468      15563      15647_fsm  pg_dw_meta
15137      15298      15307      15315      15366  15372_vm   15402      15468_fsm  15563_fsm  15647_vm   pg_dw_single
15138      15299      15307_fsm  15316      15367  15374      15431      15468_vm   15563_vm   15649      pg_filenode.map
15290      15300      15307_vm   15356      15368  15375      15433      15537      15565      15650      pg_filenode.map.backup

【openGauss实战12】表空间管理_表空间_02

📣 4.表空间管理

✨ 4.1 创建表空间

1)执行如下命令创建用户jeames
 openGauss=# drop user IF EXISTS jeames;
 openGauss=# CREATE USER jeames IDENTIFIED BY ‘abcd@123456’;
 如果要授予user1数据库系统的SYSADMIN权限,请执行以下命令
 ALTER USER jeames SYSADMIN;2)执行如下命令创建表空间
 openGauss=# CREATE TABLESPACE users RELATIVE LOCATION ‘tablespace/users’;
 其中users为新创建的表空间,tablespace/users是用户拥有读写权限的空目录。
 #查看系统有哪些表空间
 select oid,* from pg_tablespace 或 \db
 #通过 -f 后跟脚本名称创建表空间
 gsql -d postgres -p 15400 -f /home/omm/tb01.sql
 #绝对路径创建
 openGauss=# CREATE TABLESPACE tb01 location ‘/app/openGauss/install/data/tbs_tb01’;


注意:tablespace cannot be created under data directory

【openGauss实战12】表空间管理_oracle_03

3)数据库系统管理员执行如下命令将users表空间的访问权限赋予数据用户jeames

openGauss=# GRANT CREATE ON TABLESPACE users TO jeames;


说明:如果用户拥有表空间的CREATE权限,就可以在表空间上创建数据库对象,比如:表和索引等。

4)表空间中创建对象 #新建库默认表空间
CREATE DATABASE tb1 WITH TABLESPACE = tb01; CREATE DATABASE tb2 WITH TABLESPACE = tb01;
查看数据库所在的表空间,可以看到一个表空间可以有多个数据库
openGauss=# select oid,datname from pg_database; openGauss=# select datname,dattablespace,spcname from pg_database d, pg_tablespace t where d.dattablespace=t.oid;

【openGauss实战12】表空间管理_数据库_04

#指定表空间创建表

 openGauss=# CREATE TABLE foo(i int) TABLESPACE tb01;

 openGauss=# select schemaname,tablename,tablespace from pg_catalog.pg_tables;

 使用gsql程序的元命令查询表空间

 openGauss=# \db
#先使用set default_tablespace设置默认表空间,再创建表
 openGauss=# CREATE TABLESPACE tb02 location ‘/app/openGauss/install/data/tbs_tb02’;
 openGauss=# SET default_tablespace = ‘tb02’;
 openGauss=# CREATE TABLE foo2(i int);
 说明:表空间所对应的目录名称并不一定是create tablespace tablespace_name中tablespace_name中的名称,
 而是创建表空间语句location之后最后面的文件名称,比如
 CREATE TABLESPACE tb01 location ‘/opt/gaussdb/tablespace/tbs_tb01’; 该表空间对应的目录名称是tbs_tb01而非tb01

✨ 4.2 删除表空间

删除表空间,需要先删除表空间的对应的表等数据信息,否则无法删除表空间。
执行drop tablespace tablespace_name后,仅仅是将表空间下的文件给清空了,但表空间目录还存在。

openGauss=# drop tablespace tb02;

✨ 4.3 修改表空间名称

openGauss=# alter tablespace tb01 rename to tb02;
说明:修改表空间名称后,仍可正常查询原表空间上的表数据信息,修改表空间名称,其对对应的目录名称并未随之改变

📣 5.表空间运维

-- 通过\db 元命令查询表空间
openGauss=# \db

-- 查询单个表空间大小
openGauss=# SELECT PG_TABLESPACE_SIZE('tb02');
-- 查询所有表空间大小
openGauss=# select spcname, pg_size_pretty(pg_tablespace_size(spcname)) as size from pg_tablespace;
openGauss=# select spcname, pg_size_pretty(pg_tablespace_size(oid)) as size from pg_tablespace;

-- 查询单个表大小
select pg_size_pretty(pg_relation_size('mytab')) as size; 
-- 查询所有表大小
select relname, pg_size_pretty(pg_relation_size(relid)) as size from pg_stat_user_tables;
-- 查询单个表的总大小,包括该表的索引大小
select pg_size_pretty(pg_total_relation_size('tab')) as size;
-- 查询所有表的总大小,包括其索引大小
select relname, pg_size_pretty(pg_total_relation_size(relid)) as size from pg_stat_user_tables;

--查询数据库大小
openGauss=# select pg_size_pretty(pg_database_size('postgres')) as size;
openGauss=# select datname, pg_size_pretty (pg_database_size(datname)) AS size from pg_database;

-- 询表空间oid信息
openGauss=# select oid,* from pg_tablespace;

- 查询表空间物理位置信息
openGauss=# select * from pg_tablespace_location((select oid from pg_tablespace where spcname='tb01'));

--建表帮助命令
openGauss=# \h create tablespace
MAXSIZE ‘space_size’:指定表空间在单个数据库节点上的最大值。

--设置表空间限额的语法
ALTER TABLESPACE tablespace_name RESIZE MAXSIZE { UNLIMITED | 'space_size'};

--创建用户lvzz2,并更改ds_location1的所有者为lvzz2
create role lvzz2 identified by 'lvzz#123456';
alter tablespace ds_location1 owner to lvzz2;

【openGauss实战12】表空间管理_sql_05