In heap_insert example, summarizes the WAL insertion process.
In constructing WAL logging data mainly involves two variables: static XLogRecData * rdatas array and static registered_buffer * registered_buffers array. The two arrays are used to store data and manage rdatas WAL list.
Mainly related to three important functions: XLogRegisterData, XLogRegisterBuffer and XLogRegisterBufData. Effect of these three functions are registered to the special structure of the data recorded to WAL WAL, such xl_heap_insert structure in heap_insert; buf related to the registered record to the wal, such as the page page heap_insert impart regbuf-> page; the tuple content registered to WAL records, such as insert statements and other data tuples.
The following first introduced associated data structures.
1, a data structure
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 process involving WAL
In a first step, the following results, mainrdata_last stored rdata [0], is stored xl_heap_insert structure:
Second step, following results were obtained, taking registered_buffer [0], which rdata_head-> next point rdata [1], the information stored tuple header record:
Then proceeds to the third step, taking rdata [2], which was put rdata [1] -> next, i.e. added registered_buffers [0] rdata_head the list of stored values TUPLE:
These are the building WAL records preparation stage, the next section describes the building and the general structure of WAL.