From 87de1f9bf75c470382abffb88e930f367b9b4c83 Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-83-116.ec2.internal>
Date: Thu, 28 Aug 2025 14:43:35 +0000
Subject: [PATCH v15 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      |  3 ---
 src/include/storage/lwlock.h           |  3 +++
 3 files changed, 9 insertions(+), 13 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 6633c5ec37d..a9a61ffac38 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -107,9 +107,6 @@
 
 /* 128 named tranches limit: arbitrary but sufficient */
 #define MAX_NAMED_TRANCHES			128
-/* Maximum length of a named tranche */
-#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.43.0

