openGauss的逻辑存储结构主要是指数据库中的各种数据库对象,包括:数据库集群、数据库、表、索引、视图等等。所有数据库对象都有各自的对象标识符oid(object identifiers),它是一个无符号的四字节整数,相关对象的oid都存放在相关的系统目录表中,比如数据库的oid和表的oid分别存放在pg_database,pg_class表中。
| 点击这里查看视频讲解:【赵渝强老师】高斯数据库(openGauss)的逻辑存储结构 |
下面对openGauss数据库中的各种逻辑存储结构进行说明。
一、 数据库集群-Database Cluster
它也叫数据库集簇,是指由单个OpenGauss数据库服务器实例管理的所有数据库集合。组成数据库集群的这些数据库使用相同的全局配置文件和监听端口、共用数据库的后台线程和内存结构。一个数据库集群可以包括:多个数据库、多个用户以及数据库中的所有对象。
在文件系统术语中,一个数据库集群是一个单一目录,该目录称之为数据目录或数据区域,所有数据都将被存储在该目录中。它没有默认的位置,其文件系统位置可以由-D选项或者环境变量PGDATA指定,例如:
powershell
[postgres@opengauss gaussdb]$ pwd
/home/postgres/training/gaussdb
[postgres@opengauss gaussdb]$ bin/gs_ctl -D data/single_node/ -l logfile start
二、 数据库-Database
在OpenGauss中,数据库本身也是数据库对象。不同的数据库在逻辑上彼此分离,除数据库之外的其他数据库对象(例如:表、索引等等)都属于它们各自的数据库。通过下面的语句可以查看OpenGauss数据库服务器中已存在的数据库。
(1)登录OpenGauss。
powershell
bin/gsql -d postgres
(2)查看OpenGauss中已有的数据库。
sql
openGauss=# \l
# 输出的信息如下:
List of databases
Name | Owner | Encoding |......
-----------+----------+----------+------
finance | postgres | UTF8 |......
postgres | postgres | UTF8 |......
school | postgres | UTF8 |......
scott | postgres | UTF8 |......
template0 | postgres | UTF8 |......
template1 | postgres | UTF8 |......
| | |......
(6 rows)
三、 表空间-Tablespace
表空间是一个目录,在一个数据库集群中可以存在多个表空间。它里面存储的是数据库的各种物理文件。每个表空间可以对应多个数据库。表空间用作把逻辑上相关的数据结构放在一起。在数据库集群初始化的时候,会自动创建pg_default和pg_global两个表空间。其中:
- pg_global:该表空间用于存放系统表。
- pg_default:创建表时的默认表空间,该表空间的物理文件存储在数据目录中的base目录中,如:/home/postgres/training/gaussdb/data/single_node/base。
下面通过具体的操作来演示如何查看OpenGauss中已有的表空间和如何创建自己的表空间。
(1)登录OpenGauss。
powershell
bin/gsql -d postgres
(2)查看PostgreSQL中已有的表空间。
sql
openGauss=# \db
# 输出的信息如下:
List of tablespaces
Name | Owner | Location
------------+----------+----------
pg_default | postgres |
pg_global | postgres |
(2 rows)
(3)创建自己的表空间。
sql
openGauss=# create tablespace mydemotbs location '/home/postgres/training/mydemotbs';
(4)在mydemotbs 表空间上创建表。
sql
openGauss=# create table testtable1(tid int primary key,tname text) tablespace mydemotbs;
(5)再次查看openGauss中已有的表空间。
sql
openGauss=# \db
# 输出的信息如下:
List of tablespaces
Name | Owner | Location
------------+----------+-----------------------------------
mydemotbs | postgres | /home/postgres/training/mydemotbs
pg_default | postgres |
pg_global | postgres |
(3 rows)
(6)将该表空间设置为默认的表空间。
sql
openGauss=# set default_tablespace = mydemotbs;
(7)查询表空间信息。
sql
openGauss=# select * from pg_tablespace;
# 输出的信息如下:
spcname | spcowner | spcacl | spcoptions | spcmaxsize | relative
------------+----------+--------+------------+------------+-------
pg_default | 10 | | | | f
pg_global | 10 | | | | f
mydemotbs | 10 | | | | f
(3 rows)
(8)使用\db+命令查看表空间的详细信息,输出的信息如下:
sql
# 命令中的加号表示显示详细信息。
openGauss=# \x
Expanded display is on.
openGauss=# \db+
# 输出的信息如下:
List of tablespaces
-[ RECORD 1 ]-----+----------------------------------
Name | mydemotbs
Owner | postgres
Location | /home/postgres/training/mydemotbs
Access privileges |
Description |
-[ RECORD 2 ]-----+----------------------------------
Name | pg_default
Owner | postgres
Location |
Access privileges |
Description |
-[ RECORD 3 ]-----+----------------------------------
Name | pg_global
Owner | postgres
Location |
Access privileges |
Description |
四、 模式-Schema
模式Schema是数据库中的命名空间,在数据库中创建的所有对象都是在Schema中创建。一个用户可以从同一个客户端连接中访问不同的Schema。而不同的Schema中可以有多个同名的表、索引、视图、序列、函数等等各种不同的数据库对象。可以通过下面的方式来查看当前数据库的Schema。
sql
openGauss=# \dn
# 输出的信息如下:
List of schemas
Name | Owner
-----------------+--------
blockchain | postgres
coverage | postgres
cstore | postgres
db4ai | postgres
dbe_perf | postgres
dbe_pldebugger | postgres
dbe_pldeveloper | postgres
dbe_sql_util | postgres
myuser | myuser
pkg_service | postgres
public | postgres
snapshot | postgres
sqladvisor | postgres
(13 rows)
在默认情况下,OpenGauss会自动创建12个模式。下表说明了其中主要模式的功能和作用。
下面的步骤将创建一个新的模式,并在该模式下创建一张表。
(1)创建一个新的模式。
sql
openGauss=# create schema myschema;
(2)在该模式下创建一张表。
sql
openGauss=# create table myschema.test1(tid int);
(3)查看指定模式下的表。
sql
openGauss=# \dt myschema.*
List of relations
Schema | Name | Type | Owner | Storage
----------+-------+-------+---------+----------------------------------
myschema | test1 | table |postgres | {orientation=row,compression=no}
(1 row)
五、 段-Segment
一个段是分配给一个逻辑结构,如:一个表、一个索引或其他对象的一组区,它是数据库对象使用空间的集合。段可以有表段、索引段、回滚段、临时段和高速缓存段等,而最常用的段就是表段和索引段。
六、 区-Extent
区是数据库存储空间分配的一个逻辑单位,它由连续数据块所组成。一个段是由一个或多个磁盘盘区组成。当一段中间所有空间已完全使用,OpenGauss会自动为该段分配一个新的磁盘盘区范围。
七、 块-Block(Page)
数据块是openGauss管理数据文件中存储空间的单位,为数据库使用的I/O的最小单位。数据库是最小的逻辑存储单位,其默认值8K。通过参数block_size可以查看当前数据库的数据块大小。
sql
openGauss=# show block_size;
block_size
------------
8192
(1 row)
在OpenGauss中,数据的读写是以数据块为最小单位。在编译openGauss时通过指定BLCKSZ参数大小将决定数据块的大小。每个表文件由都由BLCKSZ字节大小的数据块组成。在分析型数据库中,适当增加BLCKSZ大小可以小幅度提升数据库的性能。
八、 数据库对象-Database Object
openGauss提供了各种数据库对象,如表、视图、索引、序列、函数等等。在openGauss中的所有数据库对象都由各自的对象标识符(oid)进行内部的管理。数据库的oid存储在pg_database系统表中,可以通过下面的语句进行查询。
sql
openGauss=# select oid,datname from pg_database;
# 输出的信息如下:
oid | datname
-------+-----------
1 | template1
16384 | school
16420 | finance
14809 | template0
16470 | scott
14814 | postgres
(6 rows)
而数据库中的表、索引、序列等数据库对象的oid则存在了pg_class系统表中,例如可以通过下面的语句查询前面创建的testtable1表的OID。
sql
openGauss=# select oid,relname,relkind,relfilenode from pg_class where relname ='testtable1';
# 输出的信息如下:
oid | relname | relkind | relfilenode
-------+------------+---------+-------------
16517 | testtable1 | r | 16517
(1 row)