承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

带你深入了解PostgreSQL中Oid和Relfilenode的映照问题

管理员 2023-06-24 07:48:42 互联网圈 6 ℃ 0 评论 7153字 收藏

作者李传成
中国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

到此这篇关于PostgreSQL中Oid和Relfilenode的映照的文章就介绍到这了,更多相关PostgreSQL中Oid和Relfilenode的映照内容请搜索之前的文章或继续浏览下面的相关文章希望大家以后多多支持!

文章来源:丸子建站

文章标题:带你深入了解PostgreSQL中Oid和Relfilenode的映照问题

https://www.wanzijz.com/view/58851.html

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信