From d6df83ed6fe5f7966bbfa08862569fe8cd5ce60e Mon Sep 17 00:00:00 2001
From: shihao zhong <zhong950419@gmail.com>
Date: Tue, 3 Nov 2026 21:22:14 -0500
Subject: [PATCH 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: 15
---
 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 aa89c9a4637..45171356ad1 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -23259,8 +23259,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)

