PostgreSQL WAL解析:構建WAL記錄準備
以heap_insert為例,簡述WAL的插入過程。
在構建WAL日誌記錄的過程中主要涉及2個資料變數:static XLogRecData
rdatas陣列和static registered_buffer registered_buffers陣列。這兩個陣列分別用來儲存WAL資料和管理rdatas連結串列。
主要涉及3個重要的函式:XLogRegisterData、XLogRegisterBuffer和XLogRegisterBufData。這三個函式的作用分別是將WAL記錄的特殊結構體資料註冊到WAL,比如heap_insert中的xl_heap_insert結構體;將涉及到的buf註冊到wal記錄,比如heap_insert中page頁賦予regbuf->page;將元組內容註冊到WAL記錄,比如insert語句的元組資料等。
下面首先介紹相關資料結構。
1、資料結構
HeapTupleData
typedef struct HeapTupleData
{
uint32 t_len; /* length of *t_data */
ItemPointerData t_self; /* SelfItemPointer */
Oid t_tableOid; /* table the tuple came from */
HeapTupleHeader t_data; /* -> tuple header and data */
} HeapTupleData;
xl_heap_header
`/*
- We don’t store the whole fixed part (HeapTupleHeaderData) of an inserted
- or updated tuple in WAL; we can save a few bytes by reconstructing the
- fields that are available elsewhere in the WAL record, or perhaps just
- plain needn’t be reconstructed. These are the fields we must store.
- NOTE: t_hoff could be recomputed, but we may as well store it because
- it will come for free due to alignment considerations.
/
typedef struct xl_heap_header
{
uint16 t_infomask2;
uint16 t_infomask;
uint8 t_hoff;
} xl_heap_header;xl_heap_insert
/ This is what we need to know about insert /
typedef struct xl_heap_insert
{
OffsetNumber offnum; / inserted tuple’s offset /
uint8 flags;
/ xl_heap_header & TUPLE DATA in backup block 0 /
} xl_heap_insert;XLogRecData
/ - The functions in xloginsert.c construct a chain of XLogRecData structs
- to represent the final WAL record.
/
typedef struct XLogRecData
{
struct XLogRecData next; / next struct in chain, or NULL /
char data; / start of rmgr data to include /
uint32 len; / length of rmgr data to include /
} XLogRecData;registered_buffer
/ - For each block reference registered with XLogRegisterBuffer, we fill in
- a registered
buffer struct.
/
typedef struct
{
bool in_use; / is this slot in use? /
uint8 flags; / REGBUF flags /
RelFileNode rnode; / identifies the relation and block /
ForkNumber forkno;
BlockNumber block;
Page page; / page content /
uint32 rdata_len; / total length of data in rdata chain /
XLogRecData rdata_head; / head of the chain of data registered with this block /
XLogRecData rdata_tail; / last entry in the chain, or &rdata_head if empty /
XLogRecData bkp_rdatas[2]; /* temporary rdatas used to hold references to- backup block data in XLogRecordAssemble() /
/ buffer to store a compressed version of backup block image */
char compressed_page[PGLZ_MAX_BLCKSZ];
} registered_buffer;`
2、heap_insert涉及WAL的流程
- backup block data in XLogRecordAssemble() /
第一步中,得到如下結果,mainrdata_last儲存rdata[0],儲存的是xl_heap_insert結構:
第二步,得到如下結果,取registered_buffer[0],其rdata_head->next指向rdata[1],儲存tuple記錄的頭資訊:
接著進入第三步,取rdata[2],將其放到rdata[1]->next中,即加入registered_buffers[0]的rdata_head連結串列中,儲存TUPLE值:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31493717/viewspace-2656406/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PostgreSQL DBA(15) - WAL檔案結構SQL
- PostgreSQL的wal_buffersSQL
- 非易失性WAL BUFFER解析:flush WAL改造
- PostgreSQL email list:nvm wal bufferSQLAI
- PostgreSQL DBA(16) - WAL segment file內部結構SQL
- PostgreSQL-PG體系結構之WAL(五)SQL
- PostgreSQL 原始碼解讀(104)- WAL#1(Insert & WAL-heap_i...SQL原始碼
- PostgreSQL 原始碼解讀(105)- WAL#2(Insert & WAL-heap_i...SQL原始碼
- PostgreSQL 原始碼解讀(106)- WAL#3(Insert & WAL-heap_i...SQL原始碼
- PostgreSQL 原始碼解讀(107)- WAL#4(Insert & WAL-heap_i...SQL原始碼
- PostgreSQL 原始碼解讀(110)- WAL#6(Insert&WAL - XLogRe...SQL原始碼
- PostgreSQL 原始碼解讀(111)- WAL#7(Insert&WAL - XLogRe...SQL原始碼
- PostgreSQL 原始碼解讀(113)- WAL#9(Insert&WAL - CopyXL...SQL原始碼
- PostgreSQL DBA(14) - WAL基本術語SQL
- LightDB不記錄WAL日誌的表
- 非易失性WAL BUFFER解析:WAL日誌讀寫改造
- PostgreSQL DBA(172) - PG 13(WAL activity in EXPLAIN)SQLAI
- 【PG】PostgreSQL 預寫日誌(WAL)、checkpoint、LSNSQL
- PostgreSQL mail list patch:Non-volatile WAL BUFFERSQLAI
- PostgreSQL DBA(38) - PG 12 Connection slots and WAL sendersSQL
- 非易失性WAL BUFFER實現機制解析:預分配WAL檔案改造
- PostgreSQL 原始碼解讀(112)- WAL#8(XLogCtrl資料結構)SQL原始碼GC資料結構
- PostgreSQL DBA(20) - WAL full-page-write淺析SQL
- PostgreSQL的xlog/Wal歸檔及日誌清理SQL
- PostgreSQL 原始碼解讀(109)- WAL#5(相關資料結構)SQL原始碼資料結構
- openGauss備庫wal-replay與query衝突
- WAL檔案回收
- PostgreSQL啟動恢復透過checkpoint open wal檔案SQL
- PostgreSQL pg_rewind例項--could not find previous WAL record at %X/%XSQL
- MogDB備機處於standby need-repair(WAL)狀態AI
- 非易失性WAL BUFFER實現機制解析:checkpoint改造
- 2面準備記錄
- cmu15545筆記-WAL和資料庫恢復筆記資料庫
- PG崩潰恢復讀取WAL
- 非易失性WAL BUFFER實現機制解析:日誌源切換
- PG wal 日誌的物理儲存分析
- PG wal日誌LSN相關函式函式
- 非易失性WAL BUFFER實現機制解析:啟動恢復流程改造