當(dāng)前位置:首頁 >  站長 >  數(shù)據(jù)庫 >  正文

解析PostgreSQL中Oid和Relfilenode的映射問題

 2021-05-25 17:08  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

PostgreSQL中的表會有一個RelFileNode值指定這個表在磁盤上的文件名(外部表、分區(qū)表除外)。一般情況下在pg_class表的relfilenode字段可以查出這個值,但是有一些特定表在relfilenode字段的查詢結(jié)果是0,這個博客中將會探究這些特殊表relfilenode的內(nèi)核處理。

正常表的Relfilenode

當(dāng)我們創(chuàng)建一張普通表時,在pg_class系統(tǒng)表里可以查詢出其relfilenode,可以看出在表剛剛創(chuàng)建時其oid和relfilenode都是16808,在磁盤上也可以查詢到16808這個文件。事實上,這個文件存儲了我們向表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$

 

在我們對一張表執(zhí)行truncate,vacuum full等操作后,會重寫這個表的數(shù)據(jù),會引發(fā)這個表relfilenode值的變更。如下測試可以看出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: 無法訪問'../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系統(tǒng)表中查詢出的這些表的relfilenode為0。其中pg_type、pg_proc、pg_attribute、pg_class是非共享表,在內(nèi)核中稱他們?yōu)镹ail表。剩余的表是在pg_global表空間里的共享表。

pg_class表中relfilenode字段的意義是為了告訴程序,某一張表在磁盤上存儲的文件名。比如我們查詢t2表時,一定會先到pg_class系統(tǒng)表中獲取其relfilenode,然后到磁盤找到這個文件,然后打開并掃描??墒侨绻覀兿氩樵僷g_class系統(tǒng)表在磁盤上的文件名時,應(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表中不存儲oid和relfilenode的對應(yīng)關(guān)系,那么PostgreSQL是怎么樣保存這個映射關(guān)系的呢?

Nail表Relfilenode的存儲機(jī)制

經(jīng)過研究發(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文件里存儲了shared表的oid和relfilenode的映射關(guān)系,12835目錄下存儲了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;

 

結(jié)語

這個博客主要闡述了在PostgreSQL中表的oid和relfilenode映射的兩種不同表現(xiàn)形式,你只要記住使用pg_relation_filenode()永遠(yuǎn)會得到正確的結(jié)果,從pg_class系統(tǒng)表中查詢則可能會得到錯誤的結(jié)果。

了解更多PostgreSQL技術(shù)干貨、熱點(diǎn)文集、行業(yè)動態(tài)、新聞資訊、精彩活動,請訪問中國PostgreSQL社區(qū)網(wǎng)站:www.postgresqlchina.com

文章來源:腳本之家

來源地址:https://www.jb51.net/article/204873.htm

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)文章

熱門排行

信息推薦