From c46a40fa773b996c1a292f91b1360ca4d28179ae Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-38-230.ec2.internal>
Date: Fri, 15 Aug 2025 16:48:10 +0000
Subject: [PATCH v10 1/2] Implement a DSA for LWLock tranche names

Previously, only LWLock tranches registered during postmaster startup were
available to all backends via a tranche name array inherited through fork.
Other backends that wanted to know about a tranche name had to register it
using LWLockRegisterTranche. While this design worked as intended, it was
not ideal because if a backend did not call LWLockRegisterTranche, it would
show a generic "extension" wait_event name in pg_stat_activity instead of
the actual tranche name. This caused inconsistencies with other backends
that registered the tranche name.

Commit fe07100e82b0 made dynamic creation of named DSA areas (and dshash)
much simpler for a backend, so it is anticipated that fewer tranche names
will be registered during postmaster startup in the future, with more
registered dynamically. This will make the above issue more common.

To address this, this change introduces a DSA to store LWLock tranche names
after postmaster startup. When a backend looks up a tranche name by tranche ID,
it first checks its local cache; if the name is not found locally, it fetches
it from the DSA and caches it for future lookups.

This also simplifies the API for registering tranche names: all registration
work is now done via LWLockNewTrancheId, which accepts a tranche name. The
LWLockRegisterTranche call is no longer required.

Previously users could pass arbitrary tranche IDs (that is, IDs not created
via LWLockNewTrancheId) to LWLockInitialize. This now results in an error
message.

Discussion: https://www.postgresql.org/message-id/flat/CAA5RZ0vvED3naph8My8Szv6DL4AxOVK3eTPS0qXsaKi%3DbVdW2A%40mail.gmail.com
---
 contrib/pg_prewarm/autoprewarm.c              |   4 +-
 doc/src/sgml/xfunc.sgml                       |  18 +-
 src/backend/storage/ipc/dsm_registry.c        |  16 +-
 src/backend/storage/ipc/ipci.c                |   4 +
 src/backend/storage/lmgr/lwlock.c             | 452 +++++++++++++++---
 .../utils/activity/wait_event_names.txt       |   2 +
 src/backend/utils/init/postinit.c             |   2 +
 src/include/storage/lwlock.h                  |  22 +-
 src/include/storage/lwlocklist.h              |   2 +
 src/test/modules/test_dsa/test_dsa.c          |   6 +-
 .../test_dsm_registry/test_dsm_registry.c     |   4 +-
 .../modules/test_radixtree/test_radixtree.c   |   9 +-
 src/test/modules/test_slru/test_slru.c        |   6 +-
 .../modules/test_tidstore/test_tidstore.c     |   3 +-
 src/tools/pgindent/typedefs.list              |   2 +
 15 files changed, 435 insertions(+), 117 deletions(-)

diff --git a/contrib/pg_prewarm/autoprewarm.c b/contrib/pg_prewarm/autoprewarm.c
index c01b9c7e6a4..b623589b430 100644
--- a/contrib/pg_prewarm/autoprewarm.c
+++ b/contrib/pg_prewarm/autoprewarm.c
@@ -864,7 +864,7 @@ apw_init_state(void *ptr)
 {
 	AutoPrewarmSharedState *state = (AutoPrewarmSharedState *) ptr;
 
-	LWLockInitialize(&state->lock, LWLockNewTrancheId());
+	LWLockInitialize(&state->lock, LWLockNewTrancheId("autoprewarm"));
 	state->bgworker_pid = InvalidPid;
 	state->pid_using_dumpfile = InvalidPid;
 }
@@ -883,8 +883,6 @@ apw_init_shmem(void)
 								   sizeof(AutoPrewarmSharedState),
 								   apw_init_state,
 								   &found);
-	LWLockRegisterTranche(apw_state->lock.tranche, "autoprewarm");
-
 	return found;
 }
 
diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml
index f116d0648e5..20b0767f204 100644
--- a/doc/src/sgml/xfunc.sgml
+++ b/doc/src/sgml/xfunc.sgml
@@ -3757,9 +3757,10 @@ LWLockPadded *GetNamedLWLockTranche(const char *tranche_name)
       There is another, more flexible method of obtaining LWLocks that can be
       done after server startup and outside a
       <literal>shmem_request_hook</literal>.  To do so, first allocate a
-      <literal>tranche_id</literal> by calling:
+      <literal>tranche_id</literal> with an associated <literal>tranche_name</literal>
+      by calling:
 <programlisting>
-int LWLockNewTrancheId(void)
+int LWLockNewTrancheId(const char *tranche_name)
 </programlisting>
       Next, initialize each LWLock, passing the new
       <literal>tranche_id</literal> as an argument:
@@ -3777,17 +3778,8 @@ void LWLockInitialize(LWLock *lock, int tranche_id)
      </para>
 
      <para>
-      Finally, each backend using the <literal>tranche_id</literal> should
-      associate it with a <literal>tranche_name</literal> by calling:
-<programlisting>
-void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
-</programlisting>
-     </para>
-
-     <para>
-      A complete usage example of <function>LWLockNewTrancheId</function>,
-      <function>LWLockInitialize</function>, and
-      <function>LWLockRegisterTranche</function> can be found in
+      A complete usage example of <function>LWLockNewTrancheId</function> and
+      <function>LWLockInitialize</function> can be found in
       <filename>contrib/pg_prewarm/autoprewarm.c</filename> in the
       <productname>PostgreSQL</productname> source tree.
      </para>
diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c
index 1682cc6d34c..cc0adc19971 100644
--- a/src/backend/storage/ipc/dsm_registry.c
+++ b/src/backend/storage/ipc/dsm_registry.c
@@ -307,9 +307,8 @@ GetNamedDSA(const char *name, bool *found)
 		entry->type = DSMR_ENTRY_TYPE_DSA;
 
 		/* Initialize the LWLock tranche for the DSA. */
-		state->tranche = LWLockNewTrancheId();
 		strcpy(state->tranche_name, name);
-		LWLockRegisterTranche(state->tranche, state->tranche_name);
+		state->tranche = LWLockNewTrancheId(state->tranche_name);
 
 		/* Initialize the DSA. */
 		ret = dsa_create(state->tranche);
@@ -330,9 +329,6 @@ GetNamedDSA(const char *name, bool *found)
 			ereport(ERROR,
 					(errmsg("requested DSA already attached to current process")));
 
-		/* Initialize existing LWLock tranche for the DSA. */
-		LWLockRegisterTranche(state->tranche, state->tranche_name);
-
 		/* Attach to existing DSA. */
 		ret = dsa_attach(state->handle);
 		dsa_pin_mapping(ret);
@@ -389,14 +385,12 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
 		entry->type = DSMR_ENTRY_TYPE_DSH;
 
 		/* Initialize the LWLock tranche for the DSA. */
-		dsa_state->tranche = LWLockNewTrancheId();
 		sprintf(dsa_state->tranche_name, "%s%s", name, DSMR_DSA_TRANCHE_SUFFIX);
-		LWLockRegisterTranche(dsa_state->tranche, dsa_state->tranche_name);
+		dsa_state->tranche = LWLockNewTrancheId(dsa_state->tranche_name);
 
 		/* Initialize the LWLock tranche for the dshash table. */
-		dsh_state->tranche = LWLockNewTrancheId();
 		strcpy(dsh_state->tranche_name, name);
-		LWLockRegisterTranche(dsh_state->tranche, dsh_state->tranche_name);
+		dsh_state->tranche = LWLockNewTrancheId(dsh_state->tranche_name);
 
 		/* Initialize the DSA for the hash table. */
 		dsa = dsa_create(dsa_state->tranche);
@@ -427,10 +421,6 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
 			ereport(ERROR,
 					(errmsg("requested DSHash already attached to current process")));
 
-		/* Initialize existing LWLock tranches for the DSA and dshash table. */
-		LWLockRegisterTranche(dsa_state->tranche, dsa_state->tranche_name);
-		LWLockRegisterTranche(dsh_state->tranche, dsh_state->tranche_name);
-
 		/* Attach to existing DSA for the hash table. */
 		dsa = dsa_attach(dsa_state->handle);
 		dsa_pin_mapping(dsa);
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 2fa045e6b0f..2f0df67bf7a 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -184,6 +184,8 @@ AttachSharedMemoryStructs(void)
 
 	CreateOrAttachShmemStructs();
 
+	LWLockTrancheNamesBEInit();
+
 	/*
 	 * Now give loadable modules a chance to set up their shmem allocations
 	 */
@@ -343,6 +345,8 @@ CreateOrAttachShmemStructs(void)
 	WaitEventCustomShmemInit();
 	InjectionPointShmemInit();
 	AioShmemInit();
+
+	LWLockTrancheNamesShmemInit();
 }
 
 /*
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index ec9c345ffdf..941b1ce321b 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -80,6 +80,7 @@
 #include "pg_trace.h"
 #include "pgstat.h"
 #include "port/pg_bitutils.h"
+#include "storage/ipc.h"
 #include "storage/proc.h"
 #include "storage/proclist.h"
 #include "storage/procnumber.h"
@@ -125,9 +126,11 @@ StaticAssertDecl((LW_VAL_EXCLUSIVE & LW_FLAG_MASK) == 0,
  * 2. There are some predefined tranches for built-in groups of locks defined
  * in lwlocklist.h.  We absorb the names of these tranches, too.
  *
- * 3. Extensions can create new tranches, via either RequestNamedLWLockTranche
- * or LWLockRegisterTranche.  The names of these that are known in the current
- * process appear in LWLockTrancheNames[].
+ * 3. Extensions can create new tranches using either RequestNamedLWLockTranche
+ * or LWLockNewTrancheId. Tranche names are registered in LWLockTrancheNames in
+ * two ways: if registration happens during postmaster startup, the names are
+ * stored directly in the local array; if registration happens afterward, the
+ * names are first stored in DSA and later cached into the local array during lookup.
  *
  * All these names are user-visible as wait event names, so choose with care
  * ... and do not forget to update the documentation's list of wait events.
@@ -144,14 +147,6 @@ StaticAssertDecl(lengthof(BuiltinTrancheNames) ==
 				 LWTRANCHE_FIRST_USER_DEFINED,
 				 "missing entries in BuiltinTrancheNames[]");
 
-/*
- * This is indexed by tranche ID minus LWTRANCHE_FIRST_USER_DEFINED, and
- * stores the names of all dynamically-created tranches known to the current
- * process.  Any unused entries in the array will contain NULL.
- */
-static const char **LWLockTrancheNames = NULL;
-static int	LWLockTrancheNamesAllocated = 0;
-
 /*
  * This points to the main array of LWLocks in shared memory.  Backends inherit
  * the pointer by fork from the postmaster (except in the EXEC_BACKEND case,
@@ -187,6 +182,53 @@ typedef struct NamedLWLockTrancheRequest
 static NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray = NULL;
 static int	NamedLWLockTrancheRequestsAllocated = 0;
 
+/* struct representing the LWLock tranche names stored in DSA */
+typedef struct LWLockTrancheNamesShmem
+{
+	/* pointer to the DSA shared memory, created in-place */
+	void	   *raw_dsa_area;
+	/* DSA Pointer to a list of DSA pointers, each storing a tranche name */
+	dsa_pointer list_ptr;
+
+	/*
+	 * The number of slots allocated for tranche names. The allocated size
+	 * will grow geometrically to reduce resizing of the list_ptr.
+	 */
+	int			allocated;
+
+	/* lock to protect access to the list */
+	LWLock		lock;
+} LWLockTrancheNamesShmem;
+
+/*
+ * Struct representing the names of LWLock tranches, containing pointers
+ * to both local and shared tranche names.
+ */
+typedef struct LWLockTrancheNamesStruct
+{
+	/*
+	 * Tranche names allocated in shared memory after postmaster startup, when
+	 * dynamic allocation is allowed.
+	 */
+	LWLockTrancheNamesShmem *shmem;
+	dsa_area   *dsa;
+
+	/*
+	 * Tranche names stored in local backend memory, either during postmaster
+	 * startup or as a local cache afterward.
+	 */
+	const char **local;
+	int			allocated;		/* number of pre-allocated slots for tranche
+								 * names */
+} LWLockTrancheNamesStruct;
+
+static LWLockTrancheNamesStruct LWLockTrancheNames =
+{
+	NULL, NULL, NULL, 0
+};
+
+#define LWLOCK_TRANCHE_NAMES_INIT_SIZE 16
+
 /*
  * NamedLWLockTrancheRequests is both the valid length of the request array,
  * and the length of the shared-memory NamedLWLockTrancheArray later on.
@@ -201,7 +243,11 @@ NamedLWLockTranche *NamedLWLockTrancheArray = NULL;
 static void InitializeLWLocks(void);
 static inline void LWLockReportWaitStart(LWLock *lock);
 static inline void LWLockReportWaitEnd(void);
+static void SetLocalTrancheName(int tranche_index, const char *tranche_name);
+static void SetSharedTrancheName(int tranche_index, const char *tranche_name);
 static const char *GetLWTrancheName(uint16 trancheId);
+static void LWLockTrancheNamesAttach(void);
+static void LWLockTrancheNamesDetach(void);
 
 #define T_NAME(lock) \
 	GetLWTrancheName((lock)->tranche)
@@ -448,11 +494,6 @@ CreateLWLocks(void)
 		/* Initialize all LWLocks */
 		InitializeLWLocks();
 	}
-
-	/* Register named extension LWLock tranches in the current process. */
-	for (int i = 0; i < NamedLWLockTrancheRequests; i++)
-		LWLockRegisterTranche(NamedLWLockTrancheArray[i].trancheId,
-							  NamedLWLockTrancheArray[i].trancheName);
 }
 
 /*
@@ -513,7 +554,7 @@ InitializeLWLocks(void)
 			name = trancheNames;
 			trancheNames += strlen(request->tranche_name) + 1;
 			strcpy(name, request->tranche_name);
-			tranche->trancheId = LWLockNewTrancheId();
+			tranche->trancheId = LWLockNewTrancheId(name);
 			tranche->trancheName = name;
 
 			for (j = 0; j < request->num_lwlocks; j++, lock++)
@@ -569,61 +610,149 @@ GetNamedLWLockTranche(const char *tranche_name)
 }
 
 /*
- * Allocate a new tranche ID.
+ * Allocate a new tranche ID and register it with the given tranche
+ * name.  Tranche IDs created by the postmaster will not be used
+ * for a tranche created in shared memory, since the index is generated
+ * from the shared LWLockCounter.
+ *
+ * The tranche name will be user-visible as a wait event name, so use
+ * a name that matches the style of existing wait events.
  */
 int
-LWLockNewTrancheId(void)
+LWLockNewTrancheId(const char *tranche_name)
 {
-	int			result;
+	int			tranche_id;
+	int			tranche_index;
 	int		   *LWLockCounter;
 
 	LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
 	/* We use the ShmemLock spinlock to protect LWLockCounter */
 	SpinLockAcquire(ShmemLock);
-	result = (*LWLockCounter)++;
+	tranche_id = (*LWLockCounter)++;
 	SpinLockRelease(ShmemLock);
 
-	return result;
+	tranche_index = tranche_id - LWTRANCHE_FIRST_USER_DEFINED;
+
+	if (IsUnderPostmaster || !IsPostmasterEnvironment)
+		SetSharedTrancheName(tranche_index, tranche_name);
+	else
+		SetLocalTrancheName(tranche_index, tranche_name);
+
+	return tranche_id;
 }
 
 /*
- * Register a dynamic tranche name in the lookup table of the current process.
- *
- * This routine will save a pointer to the tranche name passed as an argument,
- * so the name should be allocated in a backend-lifetime context
- * (shared memory, TopMemoryContext, static constant, or similar).
- *
- * The tranche name will be user-visible as a wait event name, so try to
- * use a name that fits the style for those.
+ * Adds a tranche name to shared memory. This routine should only be
+ * called from LWLockNewTrancheId.
  */
-void
-LWLockRegisterTranche(int tranche_id, const char *tranche_name)
+static void
+SetSharedTrancheName(int tranche_index, const char *tranche_name)
 {
-	/* This should only be called for user-defined tranches. */
-	if (tranche_id < LWTRANCHE_FIRST_USER_DEFINED)
-		return;
+	dsa_pointer *name_ptrs;
+	dsa_pointer str_ptr;
+	char	   *str_addr;
+	int			len;
+	int			current_allocated;
+	int			new_alloc = 0;
 
-	/* Convert to array index. */
-	tranche_id -= LWTRANCHE_FIRST_USER_DEFINED;
+	Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
 
-	/* If necessary, create or enlarge array. */
-	if (tranche_id >= LWLockTrancheNamesAllocated)
+	LWLockAcquire(&LWLockTrancheNames.shmem->lock, LW_EXCLUSIVE);
+
+	current_allocated = LWLockTrancheNames.shmem->allocated;
+
+	/* Tranche array does not exist, initialize it */
+	if (!DsaPointerIsValid(LWLockTrancheNames.shmem->list_ptr))
+	{
+		new_alloc = LWLOCK_TRANCHE_NAMES_INIT_SIZE;
+
+		LWLockTrancheNames.shmem->allocated = new_alloc;
+		LWLockTrancheNames.shmem->list_ptr =
+			dsa_allocate0(LWLockTrancheNames.dsa, new_alloc * sizeof(dsa_pointer));
+
+		name_ptrs = dsa_get_address(LWLockTrancheNames.dsa, LWLockTrancheNames.shmem->list_ptr);
+		memset(name_ptrs, InvalidDsaPointer, new_alloc);
+	}
+
+	/*
+	 * Need to enlarge the tranche array. Allocate a new list, copy the
+	 * current tranche name pointers into it, and update shared memory to
+	 * point to the new list.
+	 */
+	else if (tranche_index >= current_allocated)
+	{
+		dsa_pointer new_list;
+		dsa_pointer *old_ptrs;
+
+		new_alloc = pg_nextpower2_32(Max(LWLOCK_TRANCHE_NAMES_INIT_SIZE,
+										 tranche_index + 1));
+
+		new_list = dsa_allocate(LWLockTrancheNames.dsa,
+								new_alloc * sizeof(dsa_pointer));
+
+		old_ptrs = dsa_get_address(LWLockTrancheNames.dsa,
+								   LWLockTrancheNames.shmem->list_ptr);
+
+		name_ptrs = dsa_get_address(LWLockTrancheNames.dsa, new_list);
+
+		memset(name_ptrs, InvalidDsaPointer, new_alloc);
+		memcpy(name_ptrs, old_ptrs, sizeof(dsa_pointer) * current_allocated);
+
+		dsa_free(LWLockTrancheNames.dsa, LWLockTrancheNames.shmem->list_ptr);
+
+		LWLockTrancheNames.shmem->list_ptr = new_list;
+		LWLockTrancheNames.shmem->allocated = new_alloc;
+	}
+	/* Use the current list */
+	else
 	{
-		int			newalloc;
+		name_ptrs = dsa_get_address(LWLockTrancheNames.dsa,
+									LWLockTrancheNames.shmem->list_ptr);
+	}
+
+	/* Allocate and copy new string */
+	len = strlen(tranche_name) + 1;
+	str_ptr = dsa_allocate(LWLockTrancheNames.dsa, len);
+	str_addr = dsa_get_address(LWLockTrancheNames.dsa, str_ptr);
+	memcpy(str_addr, tranche_name, len);
+
+	name_ptrs[tranche_index] = str_ptr;
+
+	LWLockRelease(&LWLockTrancheNames.shmem->lock);
+}
+
+/*
+ * Adds a tranche name to local memory. This routine should only be
+ * called from LWLockNewTrancheId and SyncLWLockTrancheNames.
+ */
+static void
+SetLocalTrancheName(int tranche_index, const char *tranche_name)
+{
+	int			newalloc;
 
-		newalloc = pg_nextpower2_32(Max(8, tranche_id + 1));
+	Assert(tranche_name);
 
-		if (LWLockTrancheNames == NULL)
-			LWLockTrancheNames = (const char **)
+	/* If necessary, create or enlarge array. */
+	if (tranche_index >= LWLockTrancheNames.allocated)
+	{
+		newalloc = pg_nextpower2_32(Max(8, tranche_index + 1));
+
+		if (LWLockTrancheNames.local == NULL)
+		{
+			LWLockTrancheNames.local = (const char **)
 				MemoryContextAllocZero(TopMemoryContext,
 									   newalloc * sizeof(char *));
+		}
 		else
-			LWLockTrancheNames =
-				repalloc0_array(LWLockTrancheNames, const char *, LWLockTrancheNamesAllocated, newalloc);
-		LWLockTrancheNamesAllocated = newalloc;
+		{
+			LWLockTrancheNames.local =
+				repalloc0_array(LWLockTrancheNames.local, const char *, LWLockTrancheNames.allocated, newalloc);
+		}
+
+		LWLockTrancheNames.allocated = newalloc;
 	}
 
-	LWLockTrancheNames[tranche_id] = tranche_name;
+	LWLockTrancheNames.local[tranche_index] = tranche_name;
 }
 
 /*
@@ -674,10 +803,15 @@ RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
 
 /*
  * LWLockInitialize - initialize a new lwlock; it's initially unlocked
+ *
+ * We check if the tranche_id exists. GetLWTrancheName will raise an
+ * error if it does not.
  */
 void
 LWLockInitialize(LWLock *lock, int tranche_id)
 {
+	GetLWTrancheName(tranche_id);
+
 	pg_atomic_init_u32(&lock->state, LW_FLAG_RELEASE_OK);
 #ifdef LOCK_DEBUG
 	pg_atomic_init_u32(&lock->nwaiters, 0);
@@ -709,27 +843,101 @@ LWLockReportWaitEnd(void)
 }
 
 /*
- * Return the name of an LWLock tranche.
+ * Update the local cache of LWLock tranche names using the list from shared
+ * memory and the tranches requested suring postmaster startup. When syncing
+ * from shared memory, we stop updating the cache upon encountering an invalid
+ * DSA pointer, which indicates that no valid tranche names can exist beyond
+ * that point.
+ */
+static void
+SyncLWLockTrancheNames(void)
+{
+	dsa_pointer *pointers = dsa_get_address(LWLockTrancheNames.dsa,
+											LWLockTrancheNames.shmem->list_ptr);
+
+	int			i = 0;
+
+	/*
+	 * Sync tranches requested during postmaster startup (via
+	 * RequestNamedLWLockTranche). Although this list is static, it is not
+	 * worth doing something to avoid syncing each time, especially since
+	 * these syncs are infrequent and the list is likely very small.
+	 */
+	while (i < NamedLWLockTrancheRequests)
+	{
+		NamedLWLockTranche *tranche;
+
+		tranche = &NamedLWLockTrancheArray[i];
+
+		SetLocalTrancheName(i, tranche->trancheName);
+
+		i++;
+	}
+
+	/*
+	 * Acquire shared lock on tranche names shared memory, and sync the shared
+	 * tranches.
+	 */
+	LWLockAcquire(&LWLockTrancheNames.shmem->lock, LW_SHARED);
+
+	while (i < LWLockTrancheNames.shmem->allocated)
+	{
+		const char *tranche_name;
+
+		/* Stop iteration if the pointer is invalid */
+		if (!DsaPointerIsValid(pointers[i]))
+			break;
+
+		/* Get tranche name from shared memory */
+		tranche_name = (const char *) dsa_get_address(LWLockTrancheNames.dsa, pointers[i]);
+
+		/* Update local tranche name */
+		SetLocalTrancheName(i, tranche_name);
+
+		i++;
+	}
+
+	/* Release shared lock */
+	LWLockRelease(&LWLockTrancheNames.shmem->lock);
+}
+
+/*
+ * Return the name of an LWLock tranche; or raise an error
+ * if it not found.
  */
 static const char *
 GetLWTrancheName(uint16 trancheId)
 {
+	const char *tranche_name = NULL;
+	uint16		tranche_id_saved = trancheId;
+	bool		needs_sync = false;
+
 	/* Built-in tranche or individual LWLock? */
 	if (trancheId < LWTRANCHE_FIRST_USER_DEFINED)
 		return BuiltinTrancheNames[trancheId];
 
+	trancheId -= LWTRANCHE_FIRST_USER_DEFINED;
+
 	/*
-	 * It's an extension tranche, so look in LWLockTrancheNames[].  However,
-	 * it's possible that the tranche has never been registered in the current
-	 * process, in which case give up and return "extension".
+	 * This is an extension tranche, so check LWLockTrancheNames[]. First,
+	 * sync the local list with shared memory (if not under postmaster or
+	 * single-user) if needed, then perform the lookup. If the tranche ID is
+	 * not found, error out.
 	 */
-	trancheId -= LWTRANCHE_FIRST_USER_DEFINED;
+	needs_sync = ((trancheId >= LWLockTrancheNames.allocated ||
+				   LWLockTrancheNames.local[trancheId] == NULL)
+				  && (IsUnderPostmaster || !IsPostmasterEnvironment));
+
+	if (needs_sync)
+		SyncLWLockTrancheNames();
+
+	if (trancheId < LWLockTrancheNames.allocated)
+		tranche_name = LWLockTrancheNames.local[trancheId];
 
-	if (trancheId >= LWLockTrancheNamesAllocated ||
-		LWLockTrancheNames[trancheId] == NULL)
-		return "extension";
+	if (!tranche_name)
+		elog(ERROR, "tranche ID %d is not registered", tranche_id_saved);
 
-	return LWLockTrancheNames[trancheId];
+	return tranche_name;
 }
 
 /*
@@ -1995,3 +2203,135 @@ LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
 	}
 	return false;
 }
+
+/*
+ * Initial Size of the shared memory to create in place the tranche DSA.
+ * This should just be the minimum size of a DSA segment.
+ */
+static Size
+LWLockTrancheNamesInitSize()
+{
+	Size		sz;
+
+	sz = DSA_MIN_SEGMENT_SIZE;
+
+	Assert(dsa_minimum_size() <= sz);
+
+	return MAXALIGN(sz);
+}
+
+Size
+LWLockTrancheNamesShmemSize(void)
+{
+	Size		sz;
+
+	sz = MAXALIGN(sizeof(LWLockTrancheNamesStruct));
+	sz = add_size(sz, LWLockTrancheNamesInitSize());
+
+	return sz;
+}
+
+/*
+ * Allocate the DSA to store tranche names in dynamic shared memory. This
+ * allocation happens during postmaster startup, so create_in_place is used.
+ * At this stage, the postmaster should only create the DSA and avoid any other
+ * operations, as allocating additional DSM segments will not yet be possible.
+ */
+void
+LWLockTrancheNamesShmemInit(void)
+{
+	bool		found;
+
+	/* Initialize or attach to shared memory structure */
+	LWLockTrancheNames.shmem = (LWLockTrancheNamesShmem *)
+		ShmemInitStruct("Dynamic LWLock tranche names",
+						LWLockTrancheNamesShmemSize(),
+						&found);
+
+	if (!IsUnderPostmaster)
+	{
+		dsa_area   *dsa;
+		LWLockTrancheNamesShmem *ctl = LWLockTrancheNames.shmem;
+		char	   *p = (char *) ctl;
+
+		/* Calculate layout within the shared memory region */
+		p += MAXALIGN(sizeof(LWLockTrancheNamesShmem));
+		ctl->raw_dsa_area = p;
+
+		LWLockInitialize(&ctl->lock, LWTRANCHE_LWLOCK_TRANCHE_NAMES_LIST);
+
+		/* Create a dynamic shared memory area in-place */
+		dsa = dsa_create_in_place(ctl->raw_dsa_area,
+								  LWLockTrancheNamesInitSize(),
+								  LWTRANCHE_LWLOCK_TRANCHE_NAMES_DSA,
+								  NULL);
+
+		/*
+		 * Postmaster will never access these again, thus free the local
+		 * dsa/dshash references.
+		 */
+		dsa_pin(dsa);
+		dsa_detach(dsa);
+
+		ctl->allocated = 0;
+		ctl->list_ptr = InvalidDsaPointer;
+	}
+	else
+	{
+		Assert(found);
+	}
+}
+
+static void
+LWLockTrancheNamesAttach(void)
+{
+	MemoryContext oldcontext;
+
+	Assert(LWLockTrancheNames.dsa == NULL);
+
+	/* stats shared memory persists for the backend lifetime */
+	oldcontext = MemoryContextSwitchTo(TopMemoryContext);
+
+	LWLockTrancheNames.dsa = dsa_attach_in_place(LWLockTrancheNames.shmem->raw_dsa_area,
+												 NULL);
+	dsa_pin_mapping(LWLockTrancheNames.dsa);
+
+	Assert(LWLockTrancheNames.dsa);
+
+	MemoryContextSwitchTo(oldcontext);
+}
+
+static void
+LWLockTrancheNamesDetach(void)
+{
+	Assert(LWLockTrancheNames.dsa);
+
+	dsa_detach(LWLockTrancheNames.dsa);
+
+	dsa_release_in_place(LWLockTrancheNames.shmem->raw_dsa_area);
+
+	LWLockTrancheNames.dsa = NULL;
+}
+
+/* Detach the DSA from a backend */
+static void
+LWLockTrancheNamesShutdownHook(int code, Datum arg)
+{
+	Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
+
+	LWLockTrancheNamesDetach();
+}
+
+/* Attach the DSA to a backend */
+void
+LWLockTrancheNamesBEInit(void)
+{
+	/* Already attached, nothing to do */
+	if (LWLockTrancheNames.dsa)
+		return;
+
+	LWLockTrancheNamesAttach();
+
+	/* Set up a process-exit hook to clean up */
+	before_shmem_exit(LWLockTrancheNamesShutdownHook, 0);
+}
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 0be307d2ca0..f60ed927892 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -406,6 +406,8 @@ SubtransSLRU	"Waiting to access the sub-transaction SLRU cache."
 XactSLRU	"Waiting to access the transaction status SLRU cache."
 ParallelVacuumDSA	"Waiting for parallel vacuum dynamic shared memory allocation."
 AioUringCompletion	"Waiting for another process to complete IO via io_uring."
+LWLockDynamicTranchesDSA	"Waiting to access the LWLock tranche names DSA area."
+LWLockDynamicTranchesList	"Waiting to access the list of LWLock tranche names in DSA."
 
 # No "ABI_compatibility" region here as WaitEventLWLock has its own C code.
 
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 641e535a73c..1ef04c6b5ec 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -662,6 +662,8 @@ BaseInit(void)
 	 * drop ephemeral slots, which in turn triggers stats reporting.
 	 */
 	ReplicationSlotInitialize();
+
+	LWLockTrancheNamesBEInit();
 }
 
 
diff --git a/src/include/storage/lwlock.h b/src/include/storage/lwlock.h
index 5e717765764..8a008fc67dd 100644
--- a/src/include/storage/lwlock.h
+++ b/src/include/storage/lwlock.h
@@ -156,22 +156,18 @@ extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
 extern LWLockPadded *GetNamedLWLockTranche(const char *tranche_name);
 
 /*
- * There is another, more flexible method of obtaining lwlocks. First, call
- * LWLockNewTrancheId just once to obtain a tranche ID; this allocates from
- * a shared counter.  Next, each individual process using the tranche should
- * call LWLockRegisterTranche() to associate that tranche ID with a name.
- * Finally, LWLockInitialize should be called just once per lwlock, passing
- * the tranche ID as an argument.
- *
- * It may seem strange that each process using the tranche must register it
- * separately, but dynamic shared memory segments aren't guaranteed to be
- * mapped at the same address in all coordinating backends, so storing the
- * registration in the main shared memory segment wouldn't work for that case.
+ * An alternative, more flexible method of obtaining lwlocks is available.
+ * First, call LWLockNewTrancheId once, providing an associated tranche name.
+ * This function allocates a tranche ID from a shared counter. Then, for each
+ * lwlock, call LWLockInitialize once, passing the tranche ID as an argument.
  */
-extern int	LWLockNewTrancheId(void);
-extern void LWLockRegisterTranche(int tranche_id, const char *tranche_name);
+extern int	LWLockNewTrancheId(const char *tranche_name);
 extern void LWLockInitialize(LWLock *lock, int tranche_id);
 
+extern void LWLockTrancheNamesShmemInit(void);
+extern Size LWLockTrancheNamesShmemSize(void);
+extern void LWLockTrancheNamesBEInit(void);
+
 /*
  * Every tranche ID less than NUM_INDIVIDUAL_LWLOCKS is reserved; also,
  * we reserve additional tranche IDs for builtin tranches not included in
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index 208d2e3a8ed..5978648c09d 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -135,3 +135,5 @@ PG_LWLOCKTRANCHE(SUBTRANS_SLRU, SubtransSLRU)
 PG_LWLOCKTRANCHE(XACT_SLRU, XactSLRU)
 PG_LWLOCKTRANCHE(PARALLEL_VACUUM_DSA, ParallelVacuumDSA)
 PG_LWLOCKTRANCHE(AIO_URING_COMPLETION, AioUringCompletion)
+PG_LWLOCKTRANCHE(LWLOCK_TRANCHE_NAMES_DSA, LWLockDynamicTranchesDSA)
+PG_LWLOCKTRANCHE(LWLOCK_TRANCHE_NAMES_LIST, LWLockDynamicTranchesList)
diff --git a/src/test/modules/test_dsa/test_dsa.c b/src/test/modules/test_dsa/test_dsa.c
index cd24d0f4873..01d5c6fa67f 100644
--- a/src/test/modules/test_dsa/test_dsa.c
+++ b/src/test/modules/test_dsa/test_dsa.c
@@ -29,8 +29,7 @@ test_dsa_basic(PG_FUNCTION_ARGS)
 	dsa_pointer p[100];
 
 	/* XXX: this tranche is leaked */
-	tranche_id = LWLockNewTrancheId();
-	LWLockRegisterTranche(tranche_id, "test_dsa");
+	tranche_id = LWLockNewTrancheId("test_dsa");
 
 	a = dsa_create(tranche_id);
 	for (int i = 0; i < 100; i++)
@@ -70,8 +69,7 @@ test_dsa_resowners(PG_FUNCTION_ARGS)
 	ResourceOwner childowner;
 
 	/* XXX: this tranche is leaked */
-	tranche_id = LWLockNewTrancheId();
-	LWLockRegisterTranche(tranche_id, "test_dsa");
+	tranche_id = LWLockNewTrancheId("test_dsa");
 
 	/* Create DSA in parent resource owner */
 	a = dsa_create(tranche_id);
diff --git a/src/test/modules/test_dsm_registry/test_dsm_registry.c b/src/test/modules/test_dsm_registry/test_dsm_registry.c
index 141c8ed1b34..7f3e6849401 100644
--- a/src/test/modules/test_dsm_registry/test_dsm_registry.c
+++ b/src/test/modules/test_dsm_registry/test_dsm_registry.c
@@ -48,7 +48,7 @@ init_tdr_dsm(void *ptr)
 {
 	TestDSMRegistryStruct *dsm = (TestDSMRegistryStruct *) ptr;
 
-	LWLockInitialize(&dsm->lck, LWLockNewTrancheId());
+	LWLockInitialize(&dsm->lck, LWLockNewTrancheId("test_dsm_registry"));
 	dsm->val = 0;
 }
 
@@ -61,8 +61,6 @@ tdr_attach_shmem(void)
 								 sizeof(TestDSMRegistryStruct),
 								 init_tdr_dsm,
 								 &found);
-	LWLockRegisterTranche(tdr_dsm->lck.tranche, "test_dsm_registry");
-
 	if (tdr_dsa == NULL)
 		tdr_dsa = GetNamedDSA("test_dsm_registry_dsa", &found);
 
diff --git a/src/test/modules/test_radixtree/test_radixtree.c b/src/test/modules/test_radixtree/test_radixtree.c
index 80ad0296164..787162c8793 100644
--- a/src/test/modules/test_radixtree/test_radixtree.c
+++ b/src/test/modules/test_radixtree/test_radixtree.c
@@ -124,10 +124,9 @@ test_empty(void)
 	rt_iter    *iter;
 	uint64		key;
 #ifdef TEST_SHARED_RT
-	int			tranche_id = LWLockNewTrancheId();
+	int			tranche_id = LWLockNewTrancheId("test_radix_tree");
 	dsa_area   *dsa;
 
-	LWLockRegisterTranche(tranche_id, "test_radix_tree");
 	dsa = dsa_create(tranche_id);
 	radixtree = rt_create(dsa, tranche_id);
 #else
@@ -167,10 +166,9 @@ test_basic(rt_node_class_test_elem *test_info, int shift, bool asc)
 	uint64	   *keys;
 	int			children = test_info->nkeys;
 #ifdef TEST_SHARED_RT
-	int			tranche_id = LWLockNewTrancheId();
+	int			tranche_id = LWLockNewTrancheId("test_radix_tree");
 	dsa_area   *dsa;
 
-	LWLockRegisterTranche(tranche_id, "test_radix_tree");
 	dsa = dsa_create(tranche_id);
 	radixtree = rt_create(dsa, tranche_id);
 #else
@@ -304,10 +302,9 @@ test_random(void)
 	int			num_keys = 100000;
 	uint64	   *keys;
 #ifdef TEST_SHARED_RT
-	int			tranche_id = LWLockNewTrancheId();
+	int			tranche_id = LWLockNewTrancheId("test_radix_tree");
 	dsa_area   *dsa;
 
-	LWLockRegisterTranche(tranche_id, "test_radix_tree");
 	dsa = dsa_create(tranche_id);
 	radixtree = rt_create(dsa, tranche_id);
 #else
diff --git a/src/test/modules/test_slru/test_slru.c b/src/test/modules/test_slru/test_slru.c
index 32750930e43..8c0367eeee4 100644
--- a/src/test/modules/test_slru/test_slru.c
+++ b/src/test/modules/test_slru/test_slru.c
@@ -232,11 +232,9 @@ test_slru_shmem_startup(void)
 	(void) MakePGDirectory(slru_dir_name);
 
 	/* initialize the SLRU facility */
-	test_tranche_id = LWLockNewTrancheId();
-	LWLockRegisterTranche(test_tranche_id, "test_slru_tranche");
+	test_tranche_id = LWLockNewTrancheId("test_slru_tranche");
 
-	test_buffer_tranche_id = LWLockNewTrancheId();
-	LWLockRegisterTranche(test_tranche_id, "test_buffer_tranche");
+	test_buffer_tranche_id = LWLockNewTrancheId("test_buffer_tranche");
 
 	TestSlruCtl->PagePrecedes = test_slru_page_precedes_logically;
 	SimpleLruInit(TestSlruCtl, "TestSLRU",
diff --git a/src/test/modules/test_tidstore/test_tidstore.c b/src/test/modules/test_tidstore/test_tidstore.c
index eb16e0fbfa6..0c8f43867e5 100644
--- a/src/test/modules/test_tidstore/test_tidstore.c
+++ b/src/test/modules/test_tidstore/test_tidstore.c
@@ -103,8 +103,7 @@ test_create(PG_FUNCTION_ARGS)
 	{
 		int			tranche_id;
 
-		tranche_id = LWLockNewTrancheId();
-		LWLockRegisterTranche(tranche_id, "test_tidstore");
+		tranche_id = LWLockNewTrancheId("test_tidstore");
 
 		tidstore = TidStoreCreateShared(tidstore_max_size, tranche_id);
 
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index e6f2e93b2d6..80cdbfa6d0d 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -1531,6 +1531,8 @@ LWLock
 LWLockHandle
 LWLockMode
 LWLockPadded
+LWLockTrancheNamesShmem
+LWLockTrancheNamesStruct
 LZ4F_compressionContext_t
 LZ4F_decompressOptions_t
 LZ4F_decompressionContext_t
-- 
2.43.0

