溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

PostgreSQL WAL解析:構(gòu)建WAL記錄準(zhǔn)備

發(fā)布時(shí)間:2020-05-07 16:22:36 來(lái)源:網(wǎng)絡(luò) 閱讀:2877 作者:yzs的專(zhuān)欄 欄目:數(shù)據(jù)庫(kù)

以heap_insert為例,簡(jiǎn)述WAL的插入過(guò)程。


在構(gòu)建WAL日志記錄的過(guò)程中主要涉及2個(gè)數(shù)據(jù)變量:static XLogRecData *rdatas數(shù)組和static registered_buffer *registered_buffers數(shù)組。這兩個(gè)數(shù)組分別用來(lái)保存WAL數(shù)據(jù)和管理rdatas鏈表。


主要涉及3個(gè)重要的函數(shù):XLogRegisterData、XLogRegisterBuffer和XLogRegisterBufData。這三個(gè)函數(shù)的作用分別是將WAL記錄的特殊結(jié)構(gòu)體數(shù)據(jù)注冊(cè)到WAL,比如heap_insert中的xl_heap_insert結(jié)構(gòu)體;將涉及到的buf注冊(cè)到wal記錄,比如heap_insert中page頁(yè)賦予regbuf->page;將元組內(nèi)容注冊(cè)到WAL記錄,比如insert語(yǔ)句的元組數(shù)據(jù)等。


下面首先介紹相關(guān)數(shù)據(jù)結(jié)構(gòu)。


1、數(shù)據(jù)結(jié)構(gòu)

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的流程

?PostgreSQL WAL解析:構(gòu)建WAL記錄準(zhǔn)備

第一步中,得到如下結(jié)果,mainrdata_last保存rdata[0],存儲(chǔ)的是xl_heap_insert結(jié)構(gòu):

PostgreSQL WAL解析:構(gòu)建WAL記錄準(zhǔn)備

第二步,得到如下結(jié)果,取registered_buffer[0],其rdata_head->next指向rdata[1],存儲(chǔ)tuple記錄的頭信息:

PostgreSQL WAL解析:構(gòu)建WAL記錄準(zhǔn)備

接著進(jìn)入第三步,取rdata[2],將其放到rdata[1]->next中,即加入registered_buffers[0]的rdata_head鏈表中,存儲(chǔ)TUPLE值:

PostgreSQL WAL解析:構(gòu)建WAL記錄準(zhǔn)備

以上是構(gòu)建WAL記錄的準(zhǔn)備階段,下一節(jié)介紹WAL的構(gòu)建及其通用結(jié)構(gòu)。



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guā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)容。

AI