Re: Fix a wal_debug crash with the new shmem allocation API

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Subject: Re: Fix a wal_debug crash with the new shmem allocation API
Date: 2026-10-08 10:06:55
Message-ID: fb1bd612-4a58-4235-976f-2b2ef036bfff@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 21/09/2026 21:56, Bharath Rupireddy wrote:
> I found a crash with WAL_DEBUG related code on HEAD. After commit
> 9b5acad3f40f converted XLOGShmemInit() to the new shared memory
> allocation API, the memory context used by wal_debug is created in the
> initialization callback, which runs only in the postmaster or in a
> standalone backend. In EXEC_BACKEND builds, child processes run the
> attach callback instead, and that one was not taught to create the
> context. Previously, XLOGShmemInit() itself ran in every child and
> created the context before returning early.
>
> As a result, with WAL_DEBUG compiled in and wal_debug turned on, a
> child process has nowhere to allocate the description of the record it
> is about to insert, and crashes on the first WAL insertion.
> Reproduction steps at [1].

Good catch, thanks.

> I propose to fix this by creating the context from both callbacks, so
> that every process ends up with one, as before. Please find the
> attached patch. I think this fix needs to be back-patched through
> PG19, where the above commit went in.

Hmm, it's a bit ugly to initialize what is a purely backend-private
thing in the ShmemInit/Attach() functions. It was expedient in the past,
because the ShmemInit() functions happened to run at the right times,
but initializing the memory context was never related to shared memory
in any way. I propose the attached, which calls the InitWalDebug()
function from InitXLogInsert() instead.

Searching for similar cases where we do backend-private initialization
that is not related to shared memory in the shmem callbacks, I found
BufferManagerShmemAttach():

> static void
> BufferManagerShmemAttach(void *arg)
> {
> /* Initialize per-backend file flush context */
> WritebackContextInit(&BackendWritebackContext,
> &backend_flush_after);
> }

There's no bug here, but I propose that we also move that to
InitBufferManagerAccess(), per the second attached patch.

- Heikki

Attachment Content-Type Size
v2-0001-Fix-crash-with-wal_debug-in-EXEC_BACKEND-child-pr.patch text/x-patch 4.0 KB
v2-0002-refactor-Move-BackendWritebackContext-initializat.patch text/x-patch 3.3 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nitin Jadhav 2026-10-08 10:35:17 Reporting WAL replay progress during pre-consistency standby reovery
Previous Message Nazir Bilal Yavuz 2026-10-08 09:54:30 Re: Adding init-po and update-po targets to the meson build system