微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL四存储结构

先上一张图:


这是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

原文地址:https://www.jb51.cc/postgresql/193426.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐