On Wed, Sep 17, 2025 at 11:35:56AM +0900, Michael Paquier wrote:
> Since this commit has been merged, batta has kept failing.  Here is
> the first failure:
> https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=batta&dt=2025-09-12%2002%3A05%3A01

Thanks for bringing this to my attention.

> And here is the backtrace:
> #0  0x000055fcdf6bc97a in NumLWLocksForNamedTranches () at lwlock.c:385
> 385 numLocks += NamedLWLockTrancheRequestArray[i].num_lwlocks;
> (gdb) bt
> #0  0x000055fcdf6bc97a in NumLWLocksForNamedTranches () at lwlock.c:385 
> #1  0x000055fcdf6bc9b3 in LWLockShmemSize () at lwlock.c:400 
> #2  0x000055fcdf65bda5 in CalculateShmemSize (num_semaphores=0x7ffcaf7a78e4) 
> at ipci.c:130 
> #3  0x000055fcdf65c0b1 in CreateSharedMemoryAndSemaphores () at ipci.c:210 
> #4  0x000055fcdf42830c in PostmasterStateMachine () at postmaster.c:3223 
> #5  0x000055fcdf42703f in process_pm_child_exit () at postmaster.c:2558 
> #6  0x000055fcdf425729 in ServerLoop () at postmaster.c:1696 
> #7  0x000055fcdf424be1 in PostmasterMain (argc=4, argv=0x55fd0a8faa10) at 
> postmaster.c:1403 
> #8  0x000055fcdef80a19 in main (argc=4, argv=0x55fd0a8faa10) at main.c:231
> (gdb) p i
> $3 = 0
> (gdb) p NamedLWLockTrancheRequestArray[0]
> Cannot access memory at address 0x7f15ee4ccc08

It looks like the postmaster is trying to access the request array after
re-initializing shared memory, which of course fails.  So, we need to keep
the request array in postmaster's local memory, too.  Attached is a quick
attempt at a fix.

-- 
nathan
>From ddc39754763edc82a9eb76164559addbcbced3c4 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <nat...@postgresql.org>
Date: Tue, 16 Sep 2025 22:11:47 -0500
Subject: [PATCH v1 1/1] fix LWLock shmem reinitialization

---
 src/backend/storage/lmgr/lwlock.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/backend/storage/lmgr/lwlock.c 
b/src/backend/storage/lmgr/lwlock.c
index 46c82c63ca5..694e9e33510 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;
+
 /* shared memory counter of registered tranches */
 int               *LWLockCounter = NULL;
 
@@ -382,7 +385,7 @@ NumLWLocksForNamedTranches(void)
        int                     i;
 
        for (i = 0; i < NamedLWLockTrancheRequests; i++)
-               numLocks += NamedLWLockTrancheRequestArray[i].num_lwlocks;
+               numLocks += LocalNamedLWLockTrancheRequestArray[i].num_lwlocks;
 
        return numLocks;
 }
@@ -457,9 +460,8 @@ CreateLWLocks(void)
                 */
                if (NamedLWLockTrancheRequests > 0)
                {
-                       memcpy(ptr, NamedLWLockTrancheRequestArray,
+                       memcpy(ptr, LocalNamedLWLockTrancheRequestArray,
                                   NamedLWLockTrancheRequests * 
sizeof(NamedLWLockTrancheRequest));
-                       pfree(NamedLWLockTrancheRequestArray);
                        NamedLWLockTrancheRequestArray = 
(NamedLWLockTrancheRequest *) ptr;
                        ptr += NamedLWLockTrancheRequests * 
sizeof(NamedLWLockTrancheRequest);
                }
@@ -648,10 +650,10 @@ RequestNamedLWLockTranche(const char *tranche_name, int 
num_lwlocks)
                                 errdetail("LWLock tranche names must be no 
longer than %d bytes.",
                                                   NAMEDATALEN - 1)));
 
-       if (NamedLWLockTrancheRequestArray == NULL)
+       if (LocalNamedLWLockTrancheRequestArray == NULL)
        {
                NamedLWLockTrancheRequestsAllocated = 16;
-               NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *)
+               LocalNamedLWLockTrancheRequestArray = 
(NamedLWLockTrancheRequest *)
                        MemoryContextAlloc(TopMemoryContext,
                                                           
NamedLWLockTrancheRequestsAllocated
                                                           * 
sizeof(NamedLWLockTrancheRequest));
@@ -661,13 +663,13 @@ RequestNamedLWLockTranche(const char *tranche_name, int 
num_lwlocks)
        {
                int                     i = 
pg_nextpower2_32(NamedLWLockTrancheRequests + 1);
 
-               NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *)
-                       repalloc(NamedLWLockTrancheRequestArray,
+               LocalNamedLWLockTrancheRequestArray = 
(NamedLWLockTrancheRequest *)
+                       repalloc(LocalNamedLWLockTrancheRequestArray,
                                         i * sizeof(NamedLWLockTrancheRequest));
                NamedLWLockTrancheRequestsAllocated = i;
        }
 
-       request = &NamedLWLockTrancheRequestArray[NamedLWLockTrancheRequests];
+       request = 
&LocalNamedLWLockTrancheRequestArray[NamedLWLockTrancheRequests];
        strlcpy(request->tranche_name, tranche_name, NAMEDATALEN);
        request->num_lwlocks = num_lwlocks;
        NamedLWLockTrancheRequests++;
-- 
2.39.5 (Apple Git-154)

Reply via email to