From e55750f146db201494002545fda1b9bfec2a98db Mon Sep 17 00:00:00 2001
From: Sami Imseih <simseih@amazon.com>
Date: Tue, 26 Aug 2025 16:58:29 -0500
Subject: [PATCH v13 3/3] Remove the DSA suffix for tranches created with
 GetNamedDSHash

The previous commit fe07100 increased the tranche name length to 128 bytes,
but there is no reason to exceed the tranche name length defined in lwlock.h.
This change restores the limit to NAMEDATALEN for tranches created
with GetNamedDSHash. This also removes the separate DSA tranche name.
DSA and DSH will now share the same tranche name, reducing complexity.

Discussion: https://www.postgresql.org/message-id/aKzIg1JryN1qhNuy@nathan
---
 src/backend/storage/ipc/dsm_registry.c | 16 ++++++----------
 src/backend/storage/lmgr/lwlock.c      |  2 --
 src/include/storage/lwlock.h           |  3 +++
 3 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c
index cc0adc19971..9b50e212270 100644
--- a/src/backend/storage/ipc/dsm_registry.c
+++ b/src/backend/storage/ipc/dsm_registry.c
@@ -48,11 +48,7 @@
 #include "utils/builtins.h"
 #include "utils/memutils.h"
 
-#define DSMR_NAME_LEN				128
-
-#define DSMR_DSA_TRANCHE_SUFFIX		" DSA"
-#define DSMR_DSA_TRANCHE_SUFFIX_LEN (sizeof(DSMR_DSA_TRANCHE_SUFFIX) - 1)
-#define DSMR_DSA_TRANCHE_NAME_LEN	(DSMR_NAME_LEN + DSMR_DSA_TRANCHE_SUFFIX_LEN)
+#define DSMR_NAME_LEN	MAX_NAMED_TRANCHES_NAME_LEN
 
 typedef struct DSMRegistryCtxStruct
 {
@@ -72,7 +68,7 @@ typedef struct NamedDSAState
 {
 	dsa_handle	handle;
 	int			tranche;
-	char		tranche_name[DSMR_DSA_TRANCHE_NAME_LEN];
+	char		tranche_name[DSMR_NAME_LEN];
 } NamedDSAState;
 
 typedef struct NamedDSHState
@@ -384,14 +380,14 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
 
 		entry->type = DSMR_ENTRY_TYPE_DSH;
 
-		/* Initialize the LWLock tranche for the DSA. */
-		sprintf(dsa_state->tranche_name, "%s%s", name, DSMR_DSA_TRANCHE_SUFFIX);
-		dsa_state->tranche = LWLockNewTrancheId(dsa_state->tranche_name);
-
 		/* Initialize the LWLock tranche for the dshash table. */
 		strcpy(dsh_state->tranche_name, name);
 		dsh_state->tranche = LWLockNewTrancheId(dsh_state->tranche_name);
 
+		/* DSA uses the same tranche as DSH */
+		strcpy(dsa_state->tranche_name, dsh_state->tranche_name);
+		dsa_state->tranche = dsh_state->tranche;
+
 		/* Initialize the DSA for the hash table. */
 		dsa = dsa_create(dsa_state->tranche);
 		dsa_pin(dsa);
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index 81f9914bf08..041aebf101e 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -106,8 +106,6 @@
 #define LW_LOCK_MASK				(MAX_BACKENDS | LW_VAL_EXCLUSIVE)
 
 #define MAX_NAMED_TRANCHES			1024
-#define MAX_NAMED_TRANCHES_NAME_LEN			NAMEDATALEN
-
 
 StaticAssertDecl(((MAX_BACKENDS + 1) & MAX_BACKENDS) == 0,
 				 "MAX_BACKENDS + 1 needs to be a power of 2");
diff --git a/src/include/storage/lwlock.h b/src/include/storage/lwlock.h
index 1ba77cb3658..a514960fe29 100644
--- a/src/include/storage/lwlock.h
+++ b/src/include/storage/lwlock.h
@@ -83,6 +83,9 @@ typedef struct NamedLWLockTranche
 extern PGDLLIMPORT const char *NamedLWLockTrancheArray;
 extern PGDLLIMPORT int NamedLWLockTrancheRequests;
 
+/* Maximum length of a named tranche */
+#define MAX_NAMED_TRANCHES_NAME_LEN			NAMEDATALEN
+
 /*
  * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
  * here, but we need them to figure out offsets within MainLWLockArray, and
-- 
2.39.5 (Apple Git-154)

