解析PostgreSQL中Oid和Relfilenode的映射問(wèn)題
作者李傳成
中國(guó)PG分會(huì)認(rèn)證專家,瀚高軟件資深內(nèi)核研發(fā)工程師
https://zhuanlan.zhihu.com/p/342466054
PostgreSQL中的表會(huì)有一個(gè)RelFileNode值指定這個(gè)表在磁盤(pán)上的文件名(外部表、分區(qū)表除外)。一般情況下在pg_class表的relfilenode字段可以查出這個(gè)值,但是有一些特定表在relfilenode字段的查詢結(jié)果是0,這個(gè)博客中將會(huì)探究這些特殊表relfilenode的內(nèi)核處理。
正常表的Relfilenode
當(dāng)我們創(chuàng)建一張普通表時(shí),在pg_class系統(tǒng)表里可以查詢出其relfilenode,可以看出在表剛剛創(chuàng)建時(shí)其oid和relfilenode都是16808,在磁盤(pán)上也可以查詢到16808這個(gè)文件。事實(shí)上,這個(gè)文件存儲(chǔ)了我們向表t2插入的數(shù)據(jù)。
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$
在我們對(duì)一張表執(zhí)行truncate,vacuum full等操作后,會(huì)重寫(xiě)這個(gè)表的數(shù)據(jù),會(huì)引發(fā)這個(gè)表relfilenode值的變更。如下測(cè)試可以看出truncate之后,t2表的relfilenode從16808變?yōu)榱?6811.
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: 無(wú)法訪問(wèn)'../data/base/12835/16808': 沒(méi)有那個(gè)文件或目錄 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系統(tǒng)表中查詢出的這些表的relfilenode為0。其中pg_type、pg_proc、pg_attribute、pg_class是非共享表,在內(nèi)核中稱他們?yōu)镹ail表。剩余的表是在pg_global表空間里的共享表。
pg_class表中relfilenode字段的意義是為了告訴程序,某一張表在磁盤(pán)上存儲(chǔ)的文件名。比如我們查詢t2表時(shí),一定會(huì)先到pg_class系統(tǒng)表中獲取其relfilenode,然后到磁盤(pán)找到這個(gè)文件,然后打開(kāi)并掃描??墒侨绻覀兿氩樵僷g_class系統(tǒng)表在磁盤(pán)上的文件名時(shí),應(yīng)該去哪找到它的relfilenode?在PostgreSQL中提供了一組函數(shù)接口進(jìn)行oid和relfilenode的轉(zhuǎn)化。
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=#
通過(guò)pg_relation_filenode()可以將oid轉(zhuǎn)化為relfilenode,
通過(guò)pg_filenode_relation可以將relfilenode轉(zhuǎn)化為oid.
既然pg_class表中不存儲(chǔ)oid和relfilenode的對(duì)應(yīng)關(guān)系,那么PostgreSQL是怎么樣保存這個(gè)映射關(guān)系的呢?
Nail表Relfilenode的存儲(chǔ)機(jī)制
經(jīng)過(guò)研究發(fā)現(xiàn),在數(shù)據(jù)目錄里存在著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文件里存儲(chǔ)了shared表的oid和relfilenode的映射關(guān)系,12835目錄下存儲(chǔ)了OID為12835的數(shù)據(jù)庫(kù)里nail表的oid和relfilenode的映射關(guān)系。
pg_filenode.map文件的結(jié)構(gòu)為:
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;
結(jié)語(yǔ)
這個(gè)博客主要闡述了在PostgreSQL中表的oid和relfilenode映射的兩種不同表現(xiàn)形式,你只要記住使用pg_relation_filenode()永遠(yuǎn)會(huì)得到正確的結(jié)果,從pg_class系統(tǒng)表中查詢則可能會(huì)得到錯(cuò)誤的結(jié)果。
了解更多PostgreSQL技術(shù)干貨、熱點(diǎn)文集、行業(yè)動(dòng)態(tài)、新聞資訊、精彩活動(dòng),請(qǐng)?jiān)L問(wèn)中國(guó)PostgreSQL社區(qū)網(wǎng)站:www.postgresqlchina.com
到此這篇關(guān)于PostgreSQL中Oid和Relfilenode的映射的文章就介紹到這了,更多相關(guān)PostgreSQL中Oid和Relfilenode的映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
postgresql pg_hba.conf 簡(jiǎn)介及配置詳解
配置文件之pg_hba.conf該文件用于控制訪問(wèn)安全性,管理客戶端對(duì)于PostgreSQL服務(wù)器的訪問(wèn)權(quán)限,本文給大家介紹postgresql pg_hba.conf 簡(jiǎn)介及配置,感興趣的朋友跟隨小編一起看看吧2024-03-03pgsql之pg_stat_replication的使用詳解
這篇文章主要介紹了pgsql之pg_stat_replication的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01詳解PostgreSql數(shù)據(jù)庫(kù)對(duì)象信息及應(yīng)用
這篇文章主要介紹了PostgreSql數(shù)據(jù)庫(kù)對(duì)象信息及應(yīng)用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12postgresql實(shí)現(xiàn)對(duì)已有數(shù)據(jù)表分區(qū)處理的操作詳解
這篇文章主要為大家詳細(xì)介紹了postgresql實(shí)現(xiàn)對(duì)已有數(shù)據(jù)表分區(qū)處理的操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12postgresql查詢今天、昨天、本周、本月、上月、今年、去年的時(shí)間以及計(jì)算時(shí)間之差
PostgreSQL提供了許多返回當(dāng)前日期和時(shí)間的函數(shù),下面這篇文章主要給大家介紹了關(guān)于postgresql查詢今天、昨天、本周、本月、上月、今年、去年的時(shí)間以及計(jì)算時(shí)間之差的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08PostgreSQL用戶登錄失敗自動(dòng)鎖定的處理方案
這篇文章主要介紹了PostgreSQL用戶登錄失敗自動(dòng)鎖定的解決辦法,本文給大家分享解決方案,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03對(duì)Postgresql中的json和array使用介紹
這篇文章主要介紹了對(duì)Postgresql中的json和array使用介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01PostgreSQL+GeoHash地圖點(diǎn)位聚合實(shí)現(xiàn)代碼
這篇文章主要介紹了PostgreSQL+GeoHash地圖點(diǎn)位聚合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07