您好,登錄后才能下訂單哦!
怎么在PostgreSQL中對(duì)Oid和Relfilenode進(jìn)行映射?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
正常表的Relfilenode
當(dāng)我們創(chuàng)建一張普通表時(shí),在pg_class系統(tǒng)表里可以查詢出其relfilenode,可以看出在表剛剛創(chuàng)建時(shí)其oid和relfilenode都是16808,在磁盤上也可以查詢到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:/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$
在我們對(duì)一張表執(zhí)行truncate,vacuum full等操作后,會(huì)重寫這個(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:/h3/pgpgpg/bin$ ll ../data/base/12835/16808 ls: 無法訪問'../data/base/12835/16808': 沒有那個(gè)文件或目錄 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系統(tǒng)表中查詢出的這些表的relfilenode為0。其中pg_type、pg_proc、pg_attribute、pg_class是非共享表,在內(nèi)核中稱他們?yōu)镹ail表。剩余的表是在pg_global表空間里的共享表。
pg_class表中relfilenode字段的意義是為了告訴程序,某一張表在磁盤上存儲(chǔ)的文件名。比如我們查詢t2表時(shí),一定會(huì)先到pg_class系統(tǒng)表中獲取其relfilenode,然后到磁盤找到這個(gè)文件,然后打開并掃描??墒侨绻覀兿氩樵僷g_class系統(tǒng)表在磁盤上的文件名時(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=#
通過pg_relation_filenode()可以將oid轉(zhuǎn)化為relfilenode,
通過pg_filenode_relation可以將relfilenode轉(zhuǎn)化為oid.
既然pg_class表中不存儲(chǔ)oid和relfilenode的對(duì)應(yīng)關(guān)系,那么PostgreSQL是怎么樣保存這個(gè)映射關(guān)系的呢?
經(jīng)過研究發(fā)現(xiàn),在數(shù)據(jù)目錄里存在著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文件里存儲(chǔ)了shared表的oid和relfilenode的映射關(guān)系,12835目錄下存儲(chǔ)了OID為12835的數(shù)據(jù)庫里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;
看完上述內(nèi)容,你們掌握怎么在PostgreSQL中對(duì)Oid和Relfilenode進(jìn)行映射的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。