怎么在PostgreSQL中對Oid和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:/h3/pgpgpg/bin$ ll ../data/base/12835/16808 -rw-------+ 1 movead movead 0 12月 31 17:11 ../data/base/12835/16808 movead@movead-PC:/h3/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:/h3/pgpgpg/bin$ ll ../data/base/12835/16808 ls: 無法訪問'../data/base/12835/16808': 沒有那個文件或目錄 movead@movead-PC:/h3/pgpgpg/bin$ ll ../data/base/12835/16811 -rw-------+ 1 movead movead 0 12月 31 17:16 ../data/base/12835/16811 movead@movead-PC:/h3/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,然后到磁盤找到這個文件,然后打開并掃描??墒侨绻覀兿氩樵僷g_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是怎么樣保存這個映射關系的呢?

經過研究發現,在數據目錄里存在著pg_filenode.map文件,如下所示。
movead@movead-PC:/h3/pgpgpg/data/base/12835$ ll pg_filenode.map -rw-------+ 1 movead movead 512 12月 31 15:10 pg_filenode.map movead@movead-PC:/h3/pgpgpg/data/base/12835$ movead@movead-PC:/h3/pgpgpg/data/global$ ll pg_filenode.map -rw-------+ 1 movead movead 512 12月 31 15:10 pg_filenode.map movead@movead-PC:/h3/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進行映射的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。