日志头格式
代码语言:javascript复制typedef struct XLogPageHeaderData
{
【校验用magic数字】
uint16 xlp_magic; /* magic value for correctness checks */
【标志位信息】
uint16 xlp_info; /* flag bits, see below */
【时间线信息】
TimeLineID xlp_tli; /* TimeLineID of first record on page */
【当前页面在日志文件中的位置】
XLogRecPtr xlp_pageaddr; /* XLOG address of this page */
【日志跨页面保存,当前页面需要存的剩下的长度】
uint32 xlp_rem_len; /* total len of remaining data for record */
} XLogPageHeaderData;
文件结构
XLOG RECORD
日志之间有链接关系,xl_prev指向上一条日志的起始位置,下一条日志的位置用xl_tot_len可以找到,日志之间形成“双向链表”。
代码语言:javascript复制typedef struct XLogRecord
{
【日记长度】
uint32 xl_tot_len; /* total len of entire record */
【事务ID】
TransactionId xl_xid; /* xact id */
【上一条日志的LSN】
XLogRecPtr xl_prev; /* ptr to previous record in log */
【产生这个记录的动作】
uint8 xl_info; /* flag bits, see below */
【日志记录对应的资源管理器】
RmgrId xl_rmid; /* resource manager for this record */
/* 2 bytes of padding here, initialize to zero */
pg_crc32c xl_crc; /* CRC for this record */
/* XLogRecordBlockHeaders and XLogRecordDataHeader follow, no padding */
【日志的数据信息】
} XLogRecord;
xl_info低4位保存flag信息,高4位保存日志动作信息。
第四位:
代码语言:javascript复制/*
* If a WAL record modifies any relation files, in ways not covered by the
* usual block references, this flag is set. This is not used for anything
* by PostgreSQL itself, but it allows external tools that read WAL and keep
* track of modified blocks to recognize such special record types.
*/
#define XLR_SPECIAL_REL_UPDATE 0x01
/*
* Enforces consistency checks of replayed WAL at recovery. If enabled,
* each record will log a full-page write for each block modified by the
* record and will reuse it afterwards for consistency checks. The caller
* of XLogInsert can use this value if necessary, but if
* wal_consistency_checking is enabled for a rmgr this is set unconditionally.
*/
#define XLR_CHECK_CONSISTENCY 0x02
高四位:比如HEAP操作,对应8种动作信息
代码语言:javascript复制#define XLOG_HEAP_INSERT 0x00
#define XLOG_HEAP_DELETE 0x10
#define XLOG_HEAP_UPDATE 0x20
#define XLOG_HEAP_TRUNCATE 0x30
#define XLOG_HEAP_HOT_UPDATE 0x40
#define XLOG_HEAP_CONFIRM 0x50
#define XLOG_HEAP_LOCK 0x60
#define XLOG_HEAP_INPLACE 0x70
#define XLOG_HEAP_OPMASK 0x70
/*
* When we insert 1st item on new page in INSERT, UPDATE, HOT_UPDATE,
* or MULTI_INSERT, we can (and we do) restore entire page in redo
*/
#define XLOG_HEAP_INIT_PAGE 0x80
XLOG RECORD的数据格式
数据主要分两部分:
- 页面信息:没有实际数据,只有页面相关的信息
- 数据信息:紧跟着XlogRecordDataHeader后面存储,
- 实际数据<255字节,使用XLogRecordDataHeaderShort,用1字节保存数据长度
- 否则使用XLogRecordDataHeaderLong,用4字节保存数据长度
typedef struct XLogRecordDataHeaderShort
{
uint8 id; /* XLR_BLOCK_ID_DATA_SHORT */
uint8 data_length; /* number of payload bytes */
} XLogRecordDataHeaderShort;
#define SizeOfXLogRecordDataHeaderShort (sizeof(uint8) * 2)
typedef struct XLogRecordDataHeaderLong
{
uint8 id; /* XLR_BLOCK_ID_DATA_LONG */
/* followed by uint32 data_length, unaligned */
} XLogRecordDataHeaderLong;
#define SizeOfXLogRecordDataHeaderLong (sizeof(uint8) sizeof(uint32))