XLogBeginInsert初始化主要工作:通过设置begininsert_called标志防止递归调用日志生成函数;通过XLogInsertAllowed函数和一些Assert做代码检查工作。
XLogBeginInsert函数必须在XLogRegister*和XLogInsert之前调用

/* Begin constructing a WAL record. This must be called before the XLogRegister* functions and XLogInsert(). */
void XLogBeginInsert(void) {
Assert(max_registered_block_id == 0);
Assert(mainrdata_last == (XLogRecData *) &mainrdata_head);
Assert(mainrdata_len == 0);

/* cross-check on whether we should be here or not */
if (!XLogInsertAllowed()) elog(ERROR, "cannot make new WAL entries during recovery");

if (begininsert_called) elog(ERROR, "XLogBeginInsert was already called");
begininsert_called = true;
}

在编译PostgreSQL时,打开WAL_DEBUG编译开关,开发者可借助wal_debug参数来显示每个写操作所产生的事务日志。

PostgreSQL数据库WAL——XLogBeginInsert初始化_database