From 5c7206b27760dad6cc9d032db4e9e92bedf45f73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Herrera?= <alvherre@kurilemu.de>
Date: Thu, 26 Nov 2026 19:25:16 +0100
Subject: [PATCH v3] Recheck partition index attachment after locking it

When creating an index on a partitioned table, DefineIndex() may reuse
an existing index on a partition that is not yet attached to a parent.
It checked that with has_superclass() before locking the child index.
If ALTER INDEX ... ATTACH PARTITION committed while CREATE INDEX waited
for that lock, CREATE INDEX then tried to attach the index to a second
parent and failed with "bogus pg_inherit row".

We cannot simply lock the one index before the check, because that risks
deadlock with concurrent DML.  But we can at first lock *all* indexes,
find the ones we need, and release the lock on the ones we didn't touch.

Add an isolation test covering both cases.

Author: Rafia Sabih, Ayush Tiwari
Reported-by: Alexander Lakhin <exclusion@gmail.com>
Bug: #19723
Backpatch-through: 14
Discussion: https://postgr.es/m/19723-8a47b625c2c64f75@postgresql.org
---
 src/backend/commands/indexcmds.c              | 14 ++++--
 .../expected/partition-index-attach.out       | 46 +++++++++++++++++++
 src/test/isolation/isolation_schedule         |  1 +
 .../specs/partition-index-attach.spec         | 46 +++++++++++++++++++
 4 files changed, 103 insertions(+), 4 deletions(-)
 create mode 100644 src/test/isolation/expected/partition-index-attach.out
 create mode 100644 src/test/isolation/specs/partition-index-attach.spec

diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index c8b88ba4f2a..993cd80a963 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -1423,7 +1423,6 @@ DefineIndex(ParseState *pstate,
 				int			child_save_sec_context;
 				int			child_save_nestlevel;
 				List	   *childidxs;
-				ListCell   *cell;
 				AttrMap    *attmap;
 				bool		found = false;
 
@@ -1458,23 +1457,30 @@ DefineIndex(ParseState *pstate,
 					continue;
 				}
 
+				/* Get index list; lock them all now to avoid deadlock */
 				childidxs = RelationGetIndexList(childrel);
+				foreach_oid(cldidxid, childidxs)
+					LockRelationOid(cldidxid, lockmode);
+
 				attmap =
 					build_attrmap_by_name(RelationGetDescr(childrel),
 										  parentDesc,
 										  false);
 
-				foreach(cell, childidxs)
+				foreach_oid(cldidxid, childidxs)
 				{
-					Oid			cldidxid = lfirst_oid(cell);
 					Relation	cldidx;
 					IndexInfo  *cldIdxInfo;
 
 					/* this index is already partition of another one */
 					if (has_superclass(cldidxid))
+					{
+						UnlockRelationOid(cldidxid, lockmode);
 						continue;
+					}
+
+					cldidx = index_open(cldidxid, NoLock);
 
-					cldidx = index_open(cldidxid, lockmode);
 					cldIdxInfo = BuildIndexInfo(cldidx);
 					if (CompareIndexInfo(cldIdxInfo, indexInfo,
 										 cldidx->rd_indcollation,
diff --git a/src/test/isolation/expected/partition-index-attach.out b/src/test/isolation/expected/partition-index-attach.out
new file mode 100644
index 00000000000..ce21389fc89
--- /dev/null
+++ b/src/test/isolation/expected/partition-index-attach.out
@@ -0,0 +1,46 @@
+Parsed test spec with 2 sessions
+
+starting permutation: s1b s1attach s2create s1c s2check
+step s1b: BEGIN;
+step s1attach: ALTER INDEX pia_old ATTACH PARTITION pia_1_a;
+step s2create: CREATE INDEX pia_new ON pia (a); <waiting ...>
+step s1c: COMMIT;
+step s2create: <... completed>
+step s2check: 
+  SELECT c.relname AS index, p.relname AS parent
+  FROM pg_inherits i
+    JOIN pg_class c ON c.oid = i.inhrelid
+    JOIN pg_class p ON p.oid = i.inhparent
+  WHERE p.relname IN ('pia_old', 'pia_new')
+  ORDER BY 1;
+
+index      |parent 
+-----------+-------
+pia_1_a    |pia_old
+pia_1_a_idx|pia_new
+pia_2_a    |pia_old
+pia_2_a_idx|pia_new
+(4 rows)
+
+
+starting permutation: s1b s1rename s2create s1c s2check
+step s1b: BEGIN;
+step s1rename: ALTER INDEX pia_2_a RENAME TO pia_2_renamed;
+step s2create: CREATE INDEX pia_new ON pia (a); <waiting ...>
+step s1c: COMMIT;
+step s2create: <... completed>
+step s2check: 
+  SELECT c.relname AS index, p.relname AS parent
+  FROM pg_inherits i
+    JOIN pg_class c ON c.oid = i.inhrelid
+    JOIN pg_class p ON p.oid = i.inhparent
+  WHERE p.relname IN ('pia_old', 'pia_new')
+  ORDER BY 1;
+
+index        |parent 
+-------------+-------
+pia_1_a      |pia_new
+pia_2_a_idx  |pia_new
+pia_2_renamed|pia_old
+(3 rows)
+
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index ae8e579b199..2de19ea634f 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -121,6 +121,7 @@ test: predicate-gist
 test: predicate-gin
 test: partition-concurrent-attach
 test: partition-drop-index-locking
+test: partition-index-attach
 test: partition-key-update-1
 test: partition-key-update-2
 test: partition-key-update-3
diff --git a/src/test/isolation/specs/partition-index-attach.spec b/src/test/isolation/specs/partition-index-attach.spec
new file mode 100644
index 00000000000..093226b736b
--- /dev/null
+++ b/src/test/isolation/specs/partition-index-attach.spec
@@ -0,0 +1,46 @@
+# Test CREATE INDEX on a partitioned table concurrently with
+# ALTER INDEX ... ATTACH PARTITION on one of its partitions' indexes.
+#
+# CREATE INDEX must notice that the partition's index was attached to
+# another parent while it waited for the lock on it, and must not wait for
+# locks on partition indexes that were already attached.
+
+setup
+{
+  CREATE TABLE pia (a int) PARTITION BY RANGE (a);
+  CREATE TABLE pia_1 PARTITION OF pia FOR VALUES FROM (0) TO (10);
+  CREATE TABLE pia_2 PARTITION OF pia FOR VALUES FROM (10) TO (20);
+  CREATE INDEX pia_old ON ONLY pia (a);
+  CREATE INDEX pia_1_a ON pia_1 (a);
+  CREATE INDEX pia_2_a ON pia_2 (a);
+  ALTER INDEX pia_old ATTACH PARTITION pia_2_a;
+}
+
+teardown
+{
+  DROP TABLE pia;
+}
+
+session s1
+step s1b		{ BEGIN; }
+step s1attach	{ ALTER INDEX pia_old ATTACH PARTITION pia_1_a; }
+step s1rename	{ ALTER INDEX pia_2_a RENAME TO pia_2_renamed; }
+step s1c		{ COMMIT; }
+
+session s2
+step s2create	{ CREATE INDEX pia_new ON pia (a); }
+step s2check	{
+  SELECT c.relname AS index, p.relname AS parent
+  FROM pg_inherits i
+    JOIN pg_class c ON c.oid = i.inhrelid
+    JOIN pg_class p ON p.oid = i.inhparent
+  WHERE p.relname IN ('pia_old', 'pia_new')
+  ORDER BY 1;
+}
+
+# CREATE INDEX waits for the ATTACH, then must build a new index on pia_1
+# rather than trying to reuse pia_1_a.
+permutation s1b s1attach s2create s1c s2check
+
+# CREATE INDEX must not wait for the lock on an already-attached index.
+permutation s1b s1rename s2create s1c s2check
-- 
2.47.3

