加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL(四)存储结构

发布时间:2020-12-13 16:45:38 所属栏目:百科 来源:网络整理
导读:先上一张图: 这是PostgreSQL 的DMS 管理方式,与 oracle 管理一样。 DMS(database management space)数据库管理空间,数据库中管理,容器是预分配的文件。 SMS(system management space)系统管理空间,操作系统管理,容器是操作系统文件空间中的目录。

先上一张图:


这是PostgreSQL 的DMS 管理方式,与 oracle 管理一样。

DMS(database management space)数据库管理空间,数据库中管理,容器是预分配的文件。

SMS(system management space)系统管理空间,操作系统管理,容器是操作系统文件空间中的目录。


查看当前数据库,可以把oid查询出来,oid标识每个对象的id。

testdb=# select oid,datname from pg_database order by oid;
  oid  |  datname  
-------+-----------
     1 | template1
 13211 | template0
 13212 | postgres
 16384 | demodb
 16387 | testdb
(5 rows)

在数据库中,默认存储的位置为:/usr/local/pgsql/data/base
[root@hzc base]# pwd
/usr/local/pgsql/data/base
[root@hzc base]# ll
总用量 60
drwx------. 2 postgres postgres 8192 11月  7 11:23 1
drwx------. 2 postgres postgres 8192 11月  6 15:43 13211
drwx------. 2 postgres postgres 8192 11月  6 16:22 13212
drwx------. 2 postgres postgres 8192 11月  6 16:22 16384
drwx------. 2 postgres postgres 8192 11月  8 10:02 16387
[root@hzc base]# 

可以看到,每个数据库对应的oid在操作系统中都一样,即数据库所在的存储位置。

同样,可以查询表的oid。

testdb=# select oid,relfilenode from pg_class where relname='weather';
  oid  | relfilenode 
-------+-------------
 16388 |       16388
(1 row)

对应的具体物理文件:
testdb=# select pg_relation_filepath('weather');
 pg_relation_filepath 
----------------------
 base/16387/16388
(1 row)

当表数据存储大于 1GB 时,它将被划分到1个或多个GB大小的段(segment),如 filenode,filenode.1,filenode.2等。


其他:系统表,可查看系统相关信息

dt pg_catalog.*


数据默认存储在8KB的数据页中,如果一行数据存储超过了8KB,PG会使用 TOAST(The Oversized-Attribute Storage Technique) 技术处理。但 TOAST 现在字段大小为1GB,如果太大,建议使用 ARRAY 或JSON 类型。TOAST 参考:https://www.postgresql.org/docs/current/static/storage-toast.html

ctid 显示记录所在的数据页及位置:

testdb=# select ctid,* from weather;
 ctid  |     city      | temp_lo | temp_hi | prcp |    date    
-------+---------------+---------+---------+------+------------
 (0,1) | San Francisco |      46 |      50 | 0.25 | 1994-11-27
 (0,2) | Piter         |       0 |      50 | 0.88 | 2016-02-22
 (0,3) | San Abama     |      50 |      22 | 0.02 | 2010-08-11
(3 rows)

【Free Space map】

每张表都有 free space map,它记录文件存储的可用空间情况。FSM 不是很准确,因为它以8KB来计算,并且不是实时更新的,它是由 VACUUM 更新的。在 MVCC 情况下,update 和 delete 后,旧的数据快照不会立即删除,VACUUM会释放空间以重用或还给操作系统。VACUUM 参考:https://www.postgresql.org/docs/current/static/sql-vacuum.html

查看表的可用空间:

SELECT * FROM pg_freespace('weather');

【Visibility map】

每张表都有一个 vm 用于跟踪哪个数据页可用于活动的事务,物理文件与表的存储位置相同,后缀为 "_vm"。vm 每个页存储 bit ,用于标识页中是否存在将被用于 VACUUM 的元祖。



Database Physical Storage:https://www.postgresql.org/docs/current/static/storage.html

存储结构:http://rachbelaid.com/introduction-to-postgres-physical-storage/

PostgreSQL数据库存储体系结构简介:https://searchdatabase.techtarget.com.cn/7-18253/

PostgreSQL 存储体系结构pdf :http://download.csdn.net/download/kk185800961/10109912

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读