From 8e81efa74f05783b663dabf466b1880c517f2ad4 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Fri, 18 Jul 2025 11:19:19 +0000
Subject: [PATCH v2 2/3] Cross-check lists of predefined tranches.

Both builtin_lwlocktranchelist.h and wait_event_names.txt contain a list of all
the predefined tranches. It is easy to miss one or the other (see a493e741d32
,08b9b9e043b and c3623703f36).

This commit adds a cross-check of these lists to the script that already does
the same cross-check for lwlocklist.h.

If the lists do not match exactly, building will fail (same as in 5b1b9bce844).
---
 src/backend/Makefile                          |  2 +-
 src/backend/storage/lmgr/Makefile             |  2 +-
 .../storage/lmgr/generate-lwlocknames.pl      | 77 +++++++++++++++++--
 .../utils/activity/wait_event_names.txt       |  4 +-
 src/include/storage/meson.build               |  1 +
 5 files changed, 77 insertions(+), 9 deletions(-)

diff --git a/src/backend/Makefile b/src/backend/Makefile
index 7344c8c7f5c..cda1bbe2b35 100644
--- a/src/backend/Makefile
+++ b/src/backend/Makefile
@@ -111,7 +111,7 @@ $(top_builddir)/src/port/libpgport_srv.a: | submake-libpgport
 parser/gram.h: parser/gram.y
 	$(MAKE) -C parser gram.h
 
-storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl ../include/storage/lwlocklist.h utils/activity/wait_event_names.txt
+storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl ../include/storage/lwlocklist.h ../include/storage/lwlocktranchelist.h utils/activity/wait_event_names.txt
 	$(MAKE) -C storage/lmgr lwlocknames.h
 
 utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt
diff --git a/src/backend/storage/lmgr/Makefile b/src/backend/storage/lmgr/Makefile
index 6cbaf23b855..523804889ae 100644
--- a/src/backend/storage/lmgr/Makefile
+++ b/src/backend/storage/lmgr/Makefile
@@ -33,7 +33,7 @@ s_lock_test: s_lock.c $(top_builddir)/src/common/libpgcommon.a $(top_builddir)/s
 		$(TASPATH) -L $(top_builddir)/src/common -lpgcommon \
 		-L $(top_builddir)/src/port -lpgport -lm -o s_lock_test
 
-lwlocknames.h: ../../../include/storage/lwlocklist.h ../../utils/activity/wait_event_names.txt generate-lwlocknames.pl
+lwlocknames.h: ../../../include/storage/lwlocklist.h ../../../include/storage/lwlocktranchelist.h ../../utils/activity/wait_event_names.txt generate-lwlocknames.pl
 	$(PERL) $(srcdir)/generate-lwlocknames.pl $^
 
 check: s_lock_test
diff --git a/src/backend/storage/lmgr/generate-lwlocknames.pl b/src/backend/storage/lmgr/generate-lwlocknames.pl
index c7a6720440d..f546d080d7a 100644
--- a/src/backend/storage/lmgr/generate-lwlocknames.pl
+++ b/src/backend/storage/lmgr/generate-lwlocknames.pl
@@ -14,7 +14,8 @@ my $lastlockidx = -1;
 GetOptions('outdir:s' => \$output_path);
 
 open my $lwlocklist, '<', $ARGV[0] or die;
-open my $wait_event_names, '<', $ARGV[1] or die;
+open my $builtin_lwtranche_list, '<', $ARGV[1] or die;
+open my $wait_event_names, '<', $ARGV[2] or die;
 
 # Include PID in suffix in case parallel make runs this multiple times.
 my $htmp = "$output_path/lwlocknames.h.tmp$$";
@@ -27,18 +28,25 @@ print $h "/* there is deliberately not an #ifndef LWLOCKNAMES_H here */\n\n";
 
 
 #
-# First, record the predefined LWLocks listed in wait_event_names.txt.  We'll
-# cross-check those with the ones in lwlocklist.h.
+# First, record the predefined LWLocks and built-in tranches listed in
+# wait_event_names.txt. We'll cross-check those with the ones in lwlocklist.h
+# and in lwlocktranchelist.h.
 #
+my @wait_event_builtin_tranches;
 my @wait_event_lwlocks;
 my $record_lwlocks = 0;
+my $in_builtin_tranches = 0;
 
 while (<$wait_event_names>)
 {
 	chomp;
 
-	# Check for end marker.
-	last if /^# END OF PREDEFINED LWLOCKS/;
+	# Check for predefined end marker.
+	if (/^# END OF PREDEFINED LWLOCKS/)
+	{
+		$in_builtin_tranches = 1;
+		next;
+	}
 
 	# Skip comments and empty lines.
 	next if /^#/;
@@ -54,9 +62,20 @@ while (<$wait_event_names>)
 	# Go to the next line if we are not yet recording LWLocks.
 	next if not $record_lwlocks;
 
+	# Stop recording if we reach another section.
+	last if /^Section:/;
+
 	# Record the LWLock.
 	(my $waiteventname, my $waitevendocsentence) = split(/\t/, $_);
-	push(@wait_event_lwlocks, $waiteventname);
+
+	if ($in_builtin_tranches)
+	{
+		push(@wait_event_builtin_tranches, $waiteventname);
+	}
+	else
+	{
+		push(@wait_event_lwlocks, $waiteventname);
+	}
 }
 
 my $in_comment = 0;
@@ -114,6 +133,52 @@ die
   . "lwlocklist.h"
   if $i < scalar @wait_event_lwlocks;
 
+$in_comment = 0;
+$i = 0;
+
+# Cross-check the built-in tranches in lwlocktranchelist.h with
+# wait_event_names.txt.
+while (<$builtin_lwtranche_list>)
+{
+	chomp;
+
+	# Skip single-line C comments and empty lines
+	next if m{^\s*/\*.*\*/$};
+	next if /^\s*$/;
+
+	# skip multiline C comments
+	if ($in_comment == 1)
+	{
+		$in_comment = 0 if m{\*/};
+		next;
+	}
+	elsif (m{^\s*/\*})
+	{
+		$in_comment = 1;
+		next;
+	}
+
+	die "unable to parse lwlocktranchelist.h line \"$_\""
+	  unless /^PG_LWLOCK\((\w+),\s*"([^"]+)"\)$/;
+
+	my ($tranche_id, $tranche_name) = ($1, $2);
+
+	die "$tranche_name defined in lwlocktranchelist.h but missing from "
+	  . "wait_event_names.txt"
+	  if $i >= scalar @wait_event_builtin_tranches;
+	die "lists of built-in tranches do not match (first mismatch at "
+	  . "$wait_event_builtin_tranches[$i] in wait_event_names.txt and $tranche_name in "
+	  . "lwlocktranchelist.h)"
+	  if $wait_event_builtin_tranches[$i] ne $tranche_name;
+
+	$i++;
+}
+
+die
+  "$wait_event_builtin_tranches[$i] defined in wait_event_names.txt but missing from "
+  . "lwlocktranchelist.h"
+  if $i < scalar @wait_event_builtin_tranches;
+
 print $h "\n";
 printf $h "#define NUM_INDIVIDUAL_LWLOCKS		%s\n", $lastlockidx + 1;
 
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 4da68312b5f..050f94fc56e 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -358,7 +358,9 @@ AioWorkerSubmissionQueue	"Waiting to access AIO worker submission queue."
 #
 # Predefined LWLocks (i.e., those declared in lwlocknames.h) must be listed
 # in the section above and must be listed in the same order as in
-# lwlocknames.h.  Other LWLocks must be listed in the section below.
+# lwlocknames.h.  Other LWLocks (i.e., those declared in lwlocktranchelist.h)
+# must be listed in the section below and must be listed in the same order as in
+# lwlocktranchelist.h.
 #
 
 XactBuffer	"Waiting for I/O on a transaction status SLRU buffer."
diff --git a/src/include/storage/meson.build b/src/include/storage/meson.build
index 1e0f5080727..c2d95befd1b 100644
--- a/src/include/storage/meson.build
+++ b/src/include/storage/meson.build
@@ -3,6 +3,7 @@
 lwlocknames_h = custom_target('lwlocknames_h',
   input: files(
     '../../include/storage/lwlocklist.h',
+    '../../include/storage/lwlocktranchelist.h',
     '../../backend/utils/activity/wait_event_names.txt'),
   output: ['lwlocknames.h'],
   command: [
-- 
2.34.1

