From 3d0b5f4320d5bc1b61ad4067f5f047303634df99 Mon Sep 17 00:00:00 2001
From: shihao zhong <zhong950419@gmail.com>
Date: Tue, 3 Nov 2026 21:22:14 -0500
Subject: [PATCH v2 1/3] Count partitions pending detach when validating a
 partitioned index

Otherwise the parent index could be marked valid while another
partition had no index, letting duplicate keys in.

Backpatch-through: 14
---
 src/backend/commands/tablecmds.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 47f55121574..d0484d40dfd 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -22599,8 +22599,11 @@ validatePartitionedIndex(Relation partedIdx, Relation partedTbl)
 	/*
 	 * If we found as many inherited indexes as the partitioned table has
 	 * partitions, we're good; update pg_index to set indisvalid.
+	 *
+	 * Partitions pending detach must be counted too, because their indexes
+	 * remain attached to this index until the detach is finalized.
 	 */
-	if (tuples == RelationGetPartitionDesc(partedTbl, true)->nparts)
+	if (tuples == RelationGetPartitionDesc(partedTbl, false)->nparts)
 	{
 		Relation	idxRel;
 		HeapTuple	indTup;
-- 
2.37.1 (Apple Git-137.1)

