diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index afbe068cd0a..63d214782da 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -5570,22 +5570,6 @@ XLOGShmemInit(void *arg) char *allocptr; int i; -#ifdef WAL_DEBUG - - /* - * Create a memory context for WAL debugging that's exempt from the normal - * "no pallocs in critical section" rule. Yes, that can lead to a PANIC if - * an allocation fails, but wal_debug is not for production use anyway. - */ - if (walDebugCxt == NULL) - { - walDebugCxt = AllocSetContextCreate(TopMemoryContext, - "WAL Debug", - ALLOCSET_DEFAULT_SIZES); - MemoryContextAllowInCriticalSection(walDebugCxt, true); - } -#endif - memset(XLogCtl, 0, sizeof(XLogCtlData)); /* @@ -5669,6 +5653,30 @@ XLOGShmemAttach(void *arg) WALInsertLocks = XLogCtl->Insert.WALInsertLocks; } +/* + * Initialize process-local state needed by wal_debug. + * + * This must be called before XLogInsertRecord(). + */ +#ifdef WAL_DEBUG +void +InitWalDebug(void) +{ + /* + * Create a memory context for WAL debugging that's exempt from the normal + * "no pallocs in critical section" rule. Yes, that can lead to a PANIC if + * an allocation fails, but wal_debug is not for production use anyway. + */ + if (walDebugCxt == NULL) + { + walDebugCxt = AllocSetContextCreate(TopMemoryContext, + "WAL Debug", + ALLOCSET_DEFAULT_SIZES); + MemoryContextAllowInCriticalSection(walDebugCxt, true); + } +} +#endif + /* * This func must be called ONCE on system install. It creates pg_control * and the initial XLOG segment. diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index c9aff944a2e..2195df85b10 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -1437,4 +1437,9 @@ InitXLogInsert(void) if (hdr_scratch == NULL) hdr_scratch = MemoryContextAllocZero(xloginsert_cxt, HEADER_SCRATCH_SIZE); + + /* Extra initialization for wal_debug */ +#ifdef WAL_DEBUG + InitWalDebug(); +#endif } diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index 7a590b7e1ea..2bf5714b51e 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -234,6 +234,9 @@ extern bool XLogBackgroundFlush(void); extern bool XLogNeedsFlush(XLogRecPtr record); extern int XLogFileInit(XLogSegNo logsegno, TimeLineID logtli); extern int XLogFileOpen(XLogSegNo segno, TimeLineID tli); +#ifdef WAL_DEBUG +extern void InitWalDebug(void); +#endif extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli); extern XLogSegNo XLogGetLastRemovedSegno(void);