写点什么

PostgreSQL 中 Oid 和 Relfilenode 的映射

发布于: 2021 年 01 月 25 日

作者李传成

中国 PG 分会认证专家,瀚高软件资深内核研发工程师

https://zhuanlan.zhihu.com/p/342466054


PostgreSQL 中的表会有一个 RelFileNode 值指定这个表在磁盘上的文件名(外部表、分区表除外)。一般情况下在 pg_class 表的 relfilenode 字段可以查出这个值,但是有一些特定表在 relfilenode 字段的查询结果是 0,这个博客中将会探究这些特殊表 relfilenode 的内核处理。


正常表的 Relfilenode

当我们创建一张普通表时,在 pg_class 系统表里可以查询出其 relfilenode,可以看出在表刚刚创建时其 oid 和 relfilenode 都是 16808,在磁盘上也可以查询到 16808 这个文件。事实上,这个文件存储了我们向表 t2 插入的数据。


postgres=# create table t2(i int);

CREATE TABLE

postgres=# select oid,relname,relfilenode from pg_class where relname = 't2';

  oid  | relname | relfilenode

-------+---------+-------------

 16808 | t2      |       16808

(1 row)

 

postgres=# \q

movead@movead-PC:/h2/pgpgpg/bin$ ll ../data/base/12835/16808 

-rw-------+ 1 movead movead 0 12月31 17:11 ../data/base/12835/16808

movead@movead-PC:/h2/pgpgpg/bin$


在我们对一张表执行 truncate,vacuum full 等操作后,会重写这个表的数据,会引发这个表 relfilenode 值的变更。如下测试可以看出 truncate 之后,t2 表的 relfilenode 从 16808 变为了 16811.


postgres=# truncate t2;

TRUNCATE TABLE

postgres=# select oid,relname,relfilenode from pg_class where relname = 't2';

  oid  | relname | relfilenode

-------+---------+-------------

 16808 | t2      |       16811

(1 row)

 

postgres=# checkpoint;

CHECKPOINT

postgres=# \q

movead@movead-PC:/h2/pgpgpg/bin$ ll ../data/base/12835/16808

ls: 无法访问'../data/base/12835/16808': 没有那个文件或目录

movead@movead-PC:/h2/pgpgpg/bin$ ll ../data/base/12835/16811

-rw-------+ 1 movead movead 0 12月31 17:16 ../data/base/12835/16811

movead@movead-PC:/h2/pgpgpg/bin$


Nail 表的 Relfilenode

postgres=# select oid, relname, relfilenode,reltablespace

from pg_class

where relfilenode = 0 and relkind = 'r'

order by reltablespace;

 oid  |        relname        | relfilenode | reltablespace

------+-----------------------+-------------+---------------

 1247 | pg_type               |           0 |             0

 1255 | pg_proc               |           0 |             0

 1249 | pg_attribute          |           0 |             0

 1259 | pg_class              |           0 |             0

 3592 | pg_shseclabel         |           0 |          1664

 1262 | pg_database           |           0 |          1664

 2964 | pg_db_role_setting    |           0 |          1664

 1213 | pg_tablespace         |           0 |          1664

 1261 | pg_auth_members       |           0 |          1664

 1214 | pg_shdepend           |           0 |          1664

 2396 | pg_shdescription      |           0 |          1664

 1260 | pg_authid             |           0 |          1664

 6000 | pg_replication_origin |           0 |          1664

 6100 | pg_subscription       |           0 |          1664

(14 rows)

 

postgres=#


上述查询可以看出,从 pg_class 系统表中查询出的这些表的 relfilenode 为 0。其中 pg_type、pg_proc、pg_attribute、pg_class 是非共享表,在内核中称他们为 Nail 表。剩余的表是在 pg_global 表空间里的共享表。


pg_class 表中 relfilenode 字段的意义是为了告诉程序,某一张表在磁盘上存储的文件名。比如我们查询 t2 表时,一定会先到 pg_class 系统表中获取其 relfilenode,然后到磁盘找到这个文件,然后打开并扫描。可是如果我们想查询 pg_class 系统表在磁盘上的文件名时,应该去哪找到它的 relfilenode?在 PostgreSQL 中提供了一组函数接口进行 oid 和 relfilenode 的转化。


postgres=# select pg_relation_filenode(1259);

 pg_relation_filenode

----------------------

                16475

(1 row)

 

postgres=# select pg_filenode_relation(0,16475);

 pg_filenode_relation

----------------------

 pg_class

(1 row)

 

postgres=# select pg_filenode_relation(0,16475)::oid;

 pg_filenode_relation

----------------------

                 1259

(1 row)

 

postgres=#


通过 pg_relation_filenode()可以将 oid 转化为 relfilenode,

通过 pg_filenode_relation 可以将 relfilenode 转化为 oid.

既然 pg_class 表中不存储 oid 和 relfilenode 的对应关系,那么 PostgreSQL 是怎么样保存这个映射关系的呢?



Nail 表 Relfilenode 的存储机制

经过研究发现,在数据目录里存在着 pg_filenode.map 文件,如下所示。


movead@movead-PC:/h2/pgpgpg/data/base/12835$ ll pg_filenode.map

-rw-------+ 1 movead movead 512 12月31 15:10 pg_filenode.map

movead@movead-PC:/h2/pgpgpg/data/base/12835$

movead@movead-PC:/h2/pgpgpg/data/global$ ll pg_filenode.map

-rw-------+ 1 movead movead 512 12月31 15:10 pg_filenode.map

movead@movead-PC:/h2/pgpgpg/data/global$


在 global 目录下的 pg_filenode.map 文件里存储了 shared 表的 oid 和 relfilenode 的映射关系,12835 目录下存储了 OID 为 12835 的数据库里 nail 表的 oid 和 relfilenode 的映射关系。

pg_filenode.map 文件的结构为:


typedef struct RelMapping

{

    Oid         mapoid;         /* OID of a catalog */

    Oid         mapfilenode;    /* its filenode number */

} RelMapping;

 

typedef struct RelMapFile

{

    int32       magic;          /* always RELMAPPER_FILEMAGIC */

    int32       num_mappings;   /* number of valid RelMapping entries */

    RelMapping  mappings[MAX_MAPPINGS];

    pg_crc32c   crc;            /* CRC of all above */

    int32       pad;            /* to make the struct size be 512 exactly */

} RelMapFile;


结语

这个博客主要阐述了在 PostgreSQL 中表的 oid 和 relfilenode 映射的两种不同表现形式,你只要记住使用 pg_relation_filenode()永远会得到正确的结果,从 pg_class 系统表中查询则可能会得到错误的结果。


了解更多 PostgreSQL 技术干货、热点文集、行业动态、新闻资讯、精彩活动,请访问中国 PostgreSQL 社区网站:www.postgresqlchina.com

发布于: 2021 年 01 月 25 日阅读数: 17
用户头像

开源是一种商业模式适合于中国 2020.10.31 加入

官方公众号:开源软件联盟PostgreSQL分会 官方网站:postgresqlchina.com 官方交流社区:pgfans.cn 官方资源社区:postgreshub.cn

评论

发布
暂无评论
PostgreSQL中Oid和Relfilenode的映射