pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathan@postgresql.org>
Subject: [PATCH v3 1/1] Fix re-initialization of LWLock-related shared memory.
Date: Wed, 17 Sep 2025 09:24:45 -0500

When shared memory is re-initialized after a crash, the named
LWLock tranche request array that was copied to shared memory will
no longer be accessible.  To fix, save the pointer to the original
array in postmaster's local memory, and switch to it when
re-initializing the LWLock-related shared memory.

Oversight in commit ed1aad15e0.  Per buildfarm member batta.

Reported-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/E1uwodL-000EuJ-2j%40gemulon.postgresql.org
---
 src/backend/storage/lmgr/lwlock.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

inline patch 1: src/backend/storage/lmgr/lwlock.c (+19/-1 lines) download inline-1.patchdiff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c index 46c82c63ca5..527bd85c1f7 100644 --- a/src/backend/storage/lmgr/lwlock.c +++ b/src/backend/storage/lmgr/lwlock.c @@ -192,6 +192,9 @@ typedef struct NamedLWLockTrancheRequest int NamedLWLockTrancheRequests = 0; NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray = NULL; +/* postmaster's local copy of the request array */ +static NamedLWLockTrancheRequest *LocalNamedLWLockTrancheRequestArray = NULL; + /* shared memory counter of registered tranches */ int *LWLockCounter = NULL; @@ -396,6 +399,15 @@ LWLockShmemSize(void) Size size; int numLocks = NUM_FIXED_LWLOCKS; + /* + * If re-initializing shared memory, the request array will no longer be + * accessible, so switch to the copy that was stowed away in postmaster's + * local memory. We'll copy it back into shared memory later when + * CreateLWLocks() is called again. + */ + if (LocalNamedLWLockTrancheRequestArray) + NamedLWLockTrancheRequestArray = LocalNamedLWLockTrancheRequestArray; + /* Calculate total number of locks needed in the main array. */ numLocks += NumLWLocksForNamedTranches(); @@ -457,9 +469,15 @@ CreateLWLocks(void) */ if (NamedLWLockTrancheRequests > 0) { + /* + * Save the pointer to the request array in postmaster's local + * memory. We'll need it if we ever need to re-initialize shared + * memory after a crash. + */ + LocalNamedLWLockTrancheRequestArray = NamedLWLockTrancheRequestArray; + memcpy(ptr, NamedLWLockTrancheRequestArray, NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest)); - pfree(NamedLWLockTrancheRequestArray); NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *) ptr; ptr += NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest); } -- 2.39.5 (Apple Git-154)
--Zv6Yd4rOwBfBuepf--

Message-ID: <no-message-id-19401@localhost>
Permalink:  ../no-message-id-19401@localhost/
Also on:    postgresql.org/message-id/no-message-id-19401@localhost

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: nathan@postgresql.org
  Subject: Re: [PATCH v3 1/1] Fix re-initialization of LWLock-related shared memory.
  In-Reply-To: <no-message-id-19401@localhost>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox