From c6b491e192048df9f350047040229c42f2409f61 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Fri, 18 Jul 2025 10:03:18 +0000
Subject: [PATCH v2 1/3] Create lwlocktranchelist.h

This commit extracts the predefined LWLocks tranches from lwlock.c and puts them
in a new file (namely lwlocktranchelist.h).

This way, we can include this file in lwlock.c using the same macro pattern
as we do for lwlocklist.h.

This gives us the chance to cross-check with wait_event_names.txt in
generate-lwlocknames.pl in a following commit.
---
 src/backend/storage/lmgr/lwlock.c       | 47 ++--------------
 src/include/storage/lwlocktranchelist.h | 72 +++++++++++++++++++++++++
 src/tools/pginclude/headerscheck        |  1 +
 3 files changed, 78 insertions(+), 42 deletions(-)
 create mode 100644 src/include/storage/lwlocktranchelist.h

diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index 2d43bf2cc13..d20750c9f2f 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -124,7 +124,7 @@ StaticAssertDecl((LW_VAL_EXCLUSIVE & LW_FLAG_MASK) == 0,
  *
  * 2. There are some predefined tranches for built-in groups of locks.
  * These are listed in enum BuiltinTrancheIds in lwlock.h, and their names
- * appear in BuiltinTrancheNames[] below.
+ * appear in lwlocktranchelist.h.
  *
  * 3. Extensions can create new tranches, via either RequestNamedLWLockTranche
  * or LWLockRegisterTranche.  The names of these that are known in the current
@@ -137,47 +137,10 @@ static const char *const BuiltinTrancheNames[] = {
 #define PG_LWLOCK(id, lockname) [id] = CppAsString(lockname),
 #include "storage/lwlocklist.h"
 #undef PG_LWLOCK
-	[LWTRANCHE_XACT_BUFFER] = "XactBuffer",
-	[LWTRANCHE_COMMITTS_BUFFER] = "CommitTsBuffer",
-	[LWTRANCHE_SUBTRANS_BUFFER] = "SubtransBuffer",
-	[LWTRANCHE_MULTIXACTOFFSET_BUFFER] = "MultiXactOffsetBuffer",
-	[LWTRANCHE_MULTIXACTMEMBER_BUFFER] = "MultiXactMemberBuffer",
-	[LWTRANCHE_NOTIFY_BUFFER] = "NotifyBuffer",
-	[LWTRANCHE_SERIAL_BUFFER] = "SerialBuffer",
-	[LWTRANCHE_WAL_INSERT] = "WALInsert",
-	[LWTRANCHE_BUFFER_CONTENT] = "BufferContent",
-	[LWTRANCHE_REPLICATION_ORIGIN_STATE] = "ReplicationOriginState",
-	[LWTRANCHE_REPLICATION_SLOT_IO] = "ReplicationSlotIO",
-	[LWTRANCHE_LOCK_FASTPATH] = "LockFastPath",
-	[LWTRANCHE_BUFFER_MAPPING] = "BufferMapping",
-	[LWTRANCHE_LOCK_MANAGER] = "LockManager",
-	[LWTRANCHE_PREDICATE_LOCK_MANAGER] = "PredicateLockManager",
-	[LWTRANCHE_PARALLEL_HASH_JOIN] = "ParallelHashJoin",
-	[LWTRANCHE_PARALLEL_BTREE_SCAN] = "ParallelBtreeScan",
-	[LWTRANCHE_PARALLEL_QUERY_DSA] = "ParallelQueryDSA",
-	[LWTRANCHE_PER_SESSION_DSA] = "PerSessionDSA",
-	[LWTRANCHE_PER_SESSION_RECORD_TYPE] = "PerSessionRecordType",
-	[LWTRANCHE_PER_SESSION_RECORD_TYPMOD] = "PerSessionRecordTypmod",
-	[LWTRANCHE_SHARED_TUPLESTORE] = "SharedTupleStore",
-	[LWTRANCHE_SHARED_TIDBITMAP] = "SharedTidBitmap",
-	[LWTRANCHE_PARALLEL_APPEND] = "ParallelAppend",
-	[LWTRANCHE_PER_XACT_PREDICATE_LIST] = "PerXactPredicateList",
-	[LWTRANCHE_PGSTATS_DSA] = "PgStatsDSA",
-	[LWTRANCHE_PGSTATS_HASH] = "PgStatsHash",
-	[LWTRANCHE_PGSTATS_DATA] = "PgStatsData",
-	[LWTRANCHE_LAUNCHER_DSA] = "LogicalRepLauncherDSA",
-	[LWTRANCHE_LAUNCHER_HASH] = "LogicalRepLauncherHash",
-	[LWTRANCHE_DSM_REGISTRY_DSA] = "DSMRegistryDSA",
-	[LWTRANCHE_DSM_REGISTRY_HASH] = "DSMRegistryHash",
-	[LWTRANCHE_COMMITTS_SLRU] = "CommitTsSLRU",
-	[LWTRANCHE_MULTIXACTOFFSET_SLRU] = "MultiXactOffsetSLRU",
-	[LWTRANCHE_MULTIXACTMEMBER_SLRU] = "MultiXactMemberSLRU",
-	[LWTRANCHE_NOTIFY_SLRU] = "NotifySLRU",
-	[LWTRANCHE_SERIAL_SLRU] = "SerialSLRU",
-	[LWTRANCHE_SUBTRANS_SLRU] = "SubtransSLRU",
-	[LWTRANCHE_XACT_SLRU] = "XactSLRU",
-	[LWTRANCHE_PARALLEL_VACUUM_DSA] = "ParallelVacuumDSA",
-	[LWTRANCHE_AIO_URING_COMPLETION] = "AioUringCompletion",
+
+#define PG_LWLOCK(id, name) [id] = name,
+#include "storage/lwlocktranchelist.h"
+#undef PG_LWLOCK
 };
 
 StaticAssertDecl(lengthof(BuiltinTrancheNames) ==
diff --git a/src/include/storage/lwlocktranchelist.h b/src/include/storage/lwlocktranchelist.h
new file mode 100644
index 00000000000..973be80369f
--- /dev/null
+++ b/src/include/storage/lwlocktranchelist.h
@@ -0,0 +1,72 @@
+/*-------------------------------------------------------------------------
+ *
+ * lwlocktranchelist.h
+ *
+ * The predefined built-in LWLock tranche list is kept in its own source
+ * file for use by automatic tools. The exact representation of a tranche
+ * is determined by the PG_LWLOCK macro, which is not defined in
+ * this file; it can be defined by the caller for special purposes.
+ *
+ * Also, generate-lwlocknames.pl processes this file for validation against
+ * wait_event_names.txt.
+ *
+ * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *    src/include/storage/lwlocktranchelist.h
+ *
+ *-------------------------------------------------------------------------
+ */
+
+/*
+ * Predefined tranches for built-in groups of locks.
+ *
+ * If you add a tranche, do not forget to update the section WaitEventLWLock of
+ * wait_event_names.txt.
+ *
+ * Note that the tranche names here should match exactly what appears
+ * in wait_event_names.txt.
+ */
+
+PG_LWLOCK(LWTRANCHE_XACT_BUFFER, "XactBuffer")
+PG_LWLOCK(LWTRANCHE_COMMITTS_BUFFER, "CommitTsBuffer")
+PG_LWLOCK(LWTRANCHE_SUBTRANS_BUFFER, "SubtransBuffer")
+PG_LWLOCK(LWTRANCHE_MULTIXACTOFFSET_BUFFER, "MultiXactOffsetBuffer")
+PG_LWLOCK(LWTRANCHE_MULTIXACTMEMBER_BUFFER, "MultiXactMemberBuffer")
+PG_LWLOCK(LWTRANCHE_NOTIFY_BUFFER, "NotifyBuffer")
+PG_LWLOCK(LWTRANCHE_SERIAL_BUFFER, "SerialBuffer")
+PG_LWLOCK(LWTRANCHE_WAL_INSERT, "WALInsert")
+PG_LWLOCK(LWTRANCHE_BUFFER_CONTENT, "BufferContent")
+PG_LWLOCK(LWTRANCHE_REPLICATION_ORIGIN_STATE, "ReplicationOriginState")
+PG_LWLOCK(LWTRANCHE_REPLICATION_SLOT_IO, "ReplicationSlotIO")
+PG_LWLOCK(LWTRANCHE_LOCK_FASTPATH, "LockFastPath")
+PG_LWLOCK(LWTRANCHE_BUFFER_MAPPING, "BufferMapping")
+PG_LWLOCK(LWTRANCHE_LOCK_MANAGER, "LockManager")
+PG_LWLOCK(LWTRANCHE_PREDICATE_LOCK_MANAGER, "PredicateLockManager")
+PG_LWLOCK(LWTRANCHE_PARALLEL_HASH_JOIN, "ParallelHashJoin")
+PG_LWLOCK(LWTRANCHE_PARALLEL_BTREE_SCAN, "ParallelBtreeScan")
+PG_LWLOCK(LWTRANCHE_PARALLEL_QUERY_DSA, "ParallelQueryDSA")
+PG_LWLOCK(LWTRANCHE_PER_SESSION_DSA, "PerSessionDSA")
+PG_LWLOCK(LWTRANCHE_PER_SESSION_RECORD_TYPE, "PerSessionRecordType")
+PG_LWLOCK(LWTRANCHE_PER_SESSION_RECORD_TYPMOD, "PerSessionRecordTypmod")
+PG_LWLOCK(LWTRANCHE_SHARED_TUPLESTORE, "SharedTupleStore")
+PG_LWLOCK(LWTRANCHE_SHARED_TIDBITMAP, "SharedTidBitmap")
+PG_LWLOCK(LWTRANCHE_PARALLEL_APPEND, "ParallelAppend")
+PG_LWLOCK(LWTRANCHE_PER_XACT_PREDICATE_LIST, "PerXactPredicateList")
+PG_LWLOCK(LWTRANCHE_PGSTATS_DSA, "PgStatsDSA")
+PG_LWLOCK(LWTRANCHE_PGSTATS_HASH, "PgStatsHash")
+PG_LWLOCK(LWTRANCHE_PGSTATS_DATA, "PgStatsData")
+PG_LWLOCK(LWTRANCHE_LAUNCHER_DSA, "LogicalRepLauncherDSA")
+PG_LWLOCK(LWTRANCHE_LAUNCHER_HASH, "LogicalRepLauncherHash")
+PG_LWLOCK(LWTRANCHE_DSM_REGISTRY_DSA, "DSMRegistryDSA")
+PG_LWLOCK(LWTRANCHE_DSM_REGISTRY_HASH, "DSMRegistryHash")
+PG_LWLOCK(LWTRANCHE_COMMITTS_SLRU, "CommitTsSLRU")
+PG_LWLOCK(LWTRANCHE_MULTIXACTOFFSET_SLRU, "MultiXactOffsetSLRU")
+PG_LWLOCK(LWTRANCHE_MULTIXACTMEMBER_SLRU, "MultiXactMemberSLRU")
+PG_LWLOCK(LWTRANCHE_NOTIFY_SLRU, "NotifySLRU")
+PG_LWLOCK(LWTRANCHE_SERIAL_SLRU, "SerialSLRU")
+PG_LWLOCK(LWTRANCHE_SUBTRANS_SLRU, "SubtransSLRU")
+PG_LWLOCK(LWTRANCHE_XACT_SLRU, "XactSLRU")
+PG_LWLOCK(LWTRANCHE_PARALLEL_VACUUM_DSA, "ParallelVacuumDSA")
+PG_LWLOCK(LWTRANCHE_AIO_URING_COMPLETION, "AioUringCompletion")
diff --git a/src/tools/pginclude/headerscheck b/src/tools/pginclude/headerscheck
index 9e86d049362..4dc5bfab995 100755
--- a/src/tools/pginclude/headerscheck
+++ b/src/tools/pginclude/headerscheck
@@ -132,6 +132,7 @@ do
 	test "$f" = src/interfaces/ecpg/preproc/ecpg_kwlist.h && continue
 	test "$f" = src/include/regex/regerrs.h && continue
 	test "$f" = src/include/storage/lwlocklist.h && continue
+	test "$f" = src/include/storage/lwlocktranchelist.h && continue
 	test "$f" = src/include/tcop/cmdtaglist.h && continue
 	test "$f" = src/pl/plpgsql/src/plerrcodes.h && continue
 	test "$f" = src/pl/plpython/spiexceptions.h && continue
-- 
2.34.1

