Postgresql 底层存储 页面布局

来源:互联网 发布:怪物之子知乎 编辑:程序博客网 时间:2024/06/07 10:24

    Postgresql 底层存储管理方式:

    Postgresql的每个数据库均存放在一个目录中,以db_oid命名,该目录中存放每个表对应的文件,文件名以该数据表对应的relfilenode_oid命名。当表中的数据量足够大,导致表文件的大小大于1GB的时候,postgresql会自动创建新的文件用于存放新插入的数据。新文件的名称为: relfilenode_iod.1, relfilenode_iod.2 等。使用该策略是为了防止在某些文件系统中,最大支持文件尺寸不能大于1GB的情形。

    db_oid, relfilenode_oid可以从pg_class系统表中查询得出。

    每个table对应的文件内部又按照Page的方式组织。每个Page的大小默认为8KB。所以每个数据库对应文件的Disk 分布由下图所示:



  每个Page中包含Page Header以及Data段,Page Header中,pg_lower指向Free Space的起始地址,pg_upper指向Free Space的结束地址。

  Data 段中,包含有: ItemIdData 段,Free Space段, Items段 以及Special space段。

  ItemIdData 段: Array of (offset,length) pairs pointing to the actual items. 4 bytes per item.

  Free Space 段:The unallocated space. New item pointers are allocated from the start of this area, new items from the end.

  Items 段: The actual items themselves.

  Special space 段:  Index access method specific data. Different methods store different data. Empty in ordinary tables.

  Item的存储是从pg_upper向pg_lower(类似堆)方向增长。ItenIdData的存储是从pg_lower向pg_upper方向增长(类似栈)。