v7-0004-refactor-Only-call-CreateLWLocks-in-postmaster.patch

text/x-patch

Filename: v7-0004-refactor-Only-call-CreateLWLocks-in-postmaster.patch
Type: text/x-patch
Part: 3
Message: Re: Better shared data structure management and resizable shared data structures

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v7-0004
Subject: refactor: Only call CreateLWLocks() in postmaster
File+
src/backend/storage/ipc/ipci.c 6 5
src/backend/storage/lmgr/lwlock.c 39 40
From 2465e05a86bbb5be3b7b05f68c81f635c4d3cb24 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Fri, 20 Mar 2026 17:09:06 +0200
Subject: [PATCH v7 04/18] refactor: Only call CreateLWLocks() in postmaster

---
 src/backend/storage/ipc/ipci.c    | 11 +++--
 src/backend/storage/lmgr/lwlock.c | 79 +++++++++++++++----------------
 2 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 3d3f153809b..e6d9daeb04a 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -174,6 +174,7 @@ AttachSharedMemoryStructs(void)
 	 */
 	InitializeFastPathLocks();
 
+	/* Establish pointers to all shared memory areas in this backend */
 	CreateOrAttachShmemStructs();
 
 	/*
@@ -218,6 +219,11 @@ CreateSharedMemoryAndSemaphores(void)
 	 */
 	InitShmemAllocator(seghdr);
 
+	/*
+	 * Now initialize LWLocks, which do shared memory allocation.
+	 */
+	CreateLWLocks();
+
 	/* Initialize subsystems */
 	CreateOrAttachShmemStructs();
 
@@ -249,11 +255,6 @@ CreateSharedMemoryAndSemaphores(void)
 static void
 CreateOrAttachShmemStructs(void)
 {
-	/*
-	 * Now initialize LWLocks, which do shared memory allocation.
-	 */
-	CreateLWLocks();
-
 	dsm_shmem_init();
 	DSMRegistryShmemInit();
 
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index 49382de88fc..1fa12068e2c 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -441,55 +441,54 @@ LWLockShmemSize(void)
 void
 CreateLWLocks(void)
 {
-	if (!IsUnderPostmaster)
-	{
-		Size		spaceLocks = LWLockShmemSize();
-		char	   *ptr;
+	Size		spaceLocks = LWLockShmemSize();
+	char	   *ptr;
 
-		/* Allocate space */
-		ptr = (char *) ShmemAlloc(spaceLocks);
+	Assert(!IsUnderPostmaster);
 
-		/* Initialize the dynamic-allocation counter for tranches */
-		LWLockCounter = (int *) ptr;
-		*LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
-		ptr += MAXALIGN(sizeof(int));
+	/* Allocate space */
+	ptr = (char *) ShmemAlloc(spaceLocks);
 
-		/* Initialize tranche names */
-		LWLockTrancheNames = (char **) ptr;
-		ptr += MAX_NAMED_TRANCHES * sizeof(char *);
-		for (int i = 0; i < MAX_NAMED_TRANCHES; i++)
-		{
-			LWLockTrancheNames[i] = ptr;
-			ptr += NAMEDATALEN;
-		}
+	/* Initialize the dynamic-allocation counter for tranches */
+	LWLockCounter = (int *) ptr;
+	*LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
+	ptr += MAXALIGN(sizeof(int));
+
+	/* Initialize tranche names */
+	LWLockTrancheNames = (char **) ptr;
+	ptr += MAX_NAMED_TRANCHES * sizeof(char *);
+	for (int i = 0; i < MAX_NAMED_TRANCHES; i++)
+	{
+		LWLockTrancheNames[i] = ptr;
+		ptr += NAMEDATALEN;
+	}
 
+	/*
+	 * Move named tranche requests to shared memory.  This is done for the
+	 * benefit of EXEC_BACKEND builds, which otherwise wouldn't be able to
+	 * call GetNamedLWLockTranche() outside postmaster.
+	 */
+	if (NamedLWLockTrancheRequests > 0)
+	{
 		/*
-		 * Move named tranche requests to shared memory.  This is done for the
-		 * benefit of EXEC_BACKEND builds, which otherwise wouldn't be able to
-		 * call GetNamedLWLockTranche() outside postmaster.
+		 * 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.
 		 */
-		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;
+		LocalNamedLWLockTrancheRequestArray = NamedLWLockTrancheRequestArray;
 
-			memcpy(ptr, NamedLWLockTrancheRequestArray,
-				   NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest));
-			NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *) ptr;
-			ptr += NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest);
-		}
+		memcpy(ptr, NamedLWLockTrancheRequestArray,
+			   NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest));
+		NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *) ptr;
+		ptr += NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest);
+	}
 
-		/* Ensure desired alignment of LWLock array */
-		ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
-		MainLWLockArray = (LWLockPadded *) ptr;
+	/* Ensure desired alignment of LWLock array */
+	ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
+	MainLWLockArray = (LWLockPadded *) ptr;
 
-		/* Initialize all LWLocks */
-		InitializeLWLocks();
-	}
+	/* Initialize all LWLocks */
+	InitializeLWLocks();
 }
 
 /*
-- 
2.47.3