From 6acc6bf5d416a4454d2eb07b586e224a66890957 Mon Sep 17 00:00:00 2001
From: Manu <manuelreyesbravo@gmail.com>
Date: Sun, 6 Dec 2026 01:35:45 -0300
Subject: [PATCH v4 3/3] Test a cache reset while releasing partition
 descriptor records

Add an injection point just before PreCommit_PartitionDescRecords()
looks up the table of a flagged record, and an isolation test that
waits there while another session overflows the shared invalidation
queue.  The lookup then processes a cache reset, which flags the
records again while the last one is being released.  A version of
0001 that left the pending flag set after freeing the table of
records crashed at the backend's next commit; this test catches it.
---
 src/backend/partitioning/partdesc.c           |  2 +
 src/test/modules/injection_points/Makefile    |  1 +
 .../expected/partition-desc-records.out       | 34 ++++++++++
 src/test/modules/injection_points/meson.build |  1 +
 .../specs/partition-desc-records.spec         | 62 +++++++++++++++++++
 5 files changed, 100 insertions(+)
 create mode 100644 src/test/modules/injection_points/expected/partition-desc-records.out
 create mode 100644 src/test/modules/injection_points/specs/partition-desc-records.spec

diff --git a/src/backend/partitioning/partdesc.c b/src/backend/partitioning/partdesc.c
index 5be4cf956bf..9b658ae1c44 100644
--- a/src/backend/partitioning/partdesc.c
+++ b/src/backend/partitioning/partdesc.c
@@ -24,6 +24,7 @@
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
 #include "utils/hsearch.h"
+#include "utils/injection_point.h"
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
@@ -212,6 +213,7 @@ PreCommit_PartitionDescRecords(void)
 
 		/* an invalidation that arrives during the lookup flags it again */
 		rec->recheck = false;
+		INJECTION_POINT("partition-desc-records-lookup", NULL);
 		if (rec->valid &&
 			SearchSysCacheExists1(RELOID, ObjectIdGetDatum(parents[i])))
 			continue;
diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile
index e30740ab124..7dee2ecf280 100644
--- a/src/test/modules/injection_points/Makefile
+++ b/src/test/modules/injection_points/Makefile
@@ -38,6 +38,7 @@ ISOLATION = basic \
 	    $(WAIT_CLEANUP_TST) \
 	    heap_lock_update \
 	    on_conflict_probe_window \
+	    partition-desc-records \
 	    multixact-aborted-updater
 
 # some isolation tests require wal_level=replica
diff --git a/src/test/modules/injection_points/expected/partition-desc-records.out b/src/test/modules/injection_points/expected/partition-desc-records.out
new file mode 100644
index 00000000000..0c1913d47b6
--- /dev/null
+++ b/src/test/modules/injection_points/expected/partition-desc-records.out
@@ -0,0 +1,34 @@
+Parsed test spec with 2 sessions
+
+starting permutation: s1build s2drop s1commit s2flood s1next
+step s1build: SELECT count(*) FROM pdr_parent;
+count
+-----
+    0
+(1 row)
+
+step s2drop: DROP TABLE pdr_parent;
+step s1commit: SELECT 1 AS released; <waiting ...>
+step s2flood: 
+	DO $$
+	BEGIN
+		FOR i IN 1..1000 LOOP
+			EXECUTE format('CREATE TABLE pdr_flood.t%s (a int, b text)', i);
+		END LOOP;
+	END
+	$$;
+	SELECT FROM injection_points_detach('partition-desc-records-lookup');
+	SELECT FROM injection_points_wakeup('partition-desc-records-lookup');
+
+step s1commit: <... completed>
+released
+--------
+       1
+(1 row)
+
+step s1next: SELECT 2 AS next;
+next
+----
+   2
+(1 row)
+
diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build
index a6a296030f4..f411f932f12 100644
--- a/src/test/modules/injection_points/meson.build
+++ b/src/test/modules/injection_points/meson.build
@@ -45,6 +45,7 @@ injection_points_isolation = [
   'syscache-update-pruned',
   'heap_lock_update',
   'on_conflict_probe_window',
+  'partition-desc-records',
   'multixact-aborted-updater',
 ]
 
diff --git a/src/test/modules/injection_points/specs/partition-desc-records.spec b/src/test/modules/injection_points/specs/partition-desc-records.spec
new file mode 100644
index 00000000000..9b6a9f764ca
--- /dev/null
+++ b/src/test/modules/injection_points/specs/partition-desc-records.spec
@@ -0,0 +1,62 @@
+# A global invalidation arriving while PreCommit_PartitionDescRecords()
+# looks up the table of a partition descriptor record.
+#
+# s1 builds the partition descriptor of pdr_parent, which leaves a record of
+# it, and s2 drops the table.  s1's next commit looks the table up to release
+# the record, and waits just before the lookup.  Meanwhile s2 sends enough
+# invalidation messages to overflow the shared queue, so the lookup processes
+# a cache reset: a global invalidation that flags the records again while the
+# last one is being released.  s1's next transaction must still commit.
+
+setup
+{
+	CREATE EXTENSION injection_points;
+	CREATE SCHEMA pdr_flood;
+	CREATE TABLE pdr_parent (a int) PARTITION BY LIST (a);
+	CREATE TABLE pdr_part1 PARTITION OF pdr_parent FOR VALUES IN (1);
+}
+
+teardown
+{
+	DROP TABLE IF EXISTS pdr_parent;
+	DROP EXTENSION injection_points;
+}
+
+# Disable debug_discard_caches: a descriptor built while invalidations arrive
+# is not recorded, and then there would be nothing to release.
+session s1
+setup
+{
+	SET debug_discard_caches = 0;
+	SELECT FROM injection_points_set_local();
+	SELECT FROM injection_points_attach('partition-desc-records-lookup', 'wait');
+}
+step s1build	{ SELECT count(*) FROM pdr_parent; }
+step s1commit	{ SELECT 1 AS released; }
+step s1next		{ SELECT 2 AS next; }
+
+# Creating 1000 tables sends several times more messages than the shared
+# invalidation queue holds, and s1 reads none of them while it waits.
+session s2
+setup			{ SET debug_discard_caches = 0; SET client_min_messages = warning; }
+step s2drop		{ DROP TABLE pdr_parent; }
+step s2flood
+{
+	DO $$
+	BEGIN
+		FOR i IN 1..1000 LOOP
+			EXECUTE format('CREATE TABLE pdr_flood.t%s (a int, b text)', i);
+		END LOOP;
+	END
+	$$;
+	SELECT FROM injection_points_detach('partition-desc-records-lookup');
+	SELECT FROM injection_points_wakeup('partition-desc-records-lookup');
+}
+teardown		{ DROP SCHEMA pdr_flood CASCADE; }
+
+permutation
+	s1build
+	s2drop
+	s1commit(s2flood)	# waits before looking up pdr_parent
+	s2flood				# overflows the invalidation queue, wakes s1
+	s1next
-- 
2.56.1

