From af4c2b7b438585240f134aa50ec92c0b838e4573 Mon Sep 17 00:00:00 2001 From: Mohamed Ali Date: Fri, 27 Mar 2026 19:56:28 -0700 Subject: [PATCH] fix: Always validate parent index in ALTER INDEX ATTACH PARTITION When ALTER INDEX ... ATTACH PARTITION is executed on a partition index that is already attached to the parent, the entire if-block is skipped, including the call to validatePartitionedIndex(). This means that if a previously invalid partition index has been repaired via REINDEX, there is no way to trigger re-validation of the parent partitioned index. Move the validatePartitionedIndex() call outside the if-block so it runs unconditionally. This allows users to re-run ALTER INDEX ATTACH PARTITION on an already-attached index to trigger parent validation after a child has been fixed. When the partition is already attached, emit a NOTICE informing the user that the index is already attached and that only parent validation is being performed. This follows the existing PostgreSQL convention of using NOTICE for informational messages about no-op or reduced-scope operations (e.g., DROP IF EXISTS, CREATE INDEX IF NOT EXISTS). NOTICE: partition index "child_idx" is already attached to "parent_idx", validating parent index validatePartitionedIndex() is idempotent and cheap (it scans pg_inherits and counts valid children), so calling it when not strictly needed has negligible performance impact. --- src/backend/commands/tablecmds.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index c69c12d..0b29180 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -21917,7 +21917,7 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name) ObjectAddressSet(address, RelationRelationId, RelationGetRelid(partIdx)); - /* Silently do nothing if already in the right state */ + /* Check if already attached to this parent */ currParent = partIdx->rd_rel->relispartition ? get_partition_parent(partIdxId, false) : InvalidOid; if (currParent != RelationGetRelid(parentIdx)) @@ -22023,9 +22023,22 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name) RelationGetRelid(partTbl)); free_attrmap(attmap); - - validatePartitionedIndex(parentIdx, parentTbl); } + else + { + ereport(NOTICE, + (errmsg("partition index \"%s\" is already attached to \"%s\", validating parent index", + RelationGetRelationName(partIdx), + RelationGetRelationName(parentIdx)))); + } + + /* + * Always validate the parent partitioned index, even if the partition + * was already attached. This handles the case where a previously + * invalid partition index has been repaired (e.g., via REINDEX) and + * the parent can now be marked valid. + */ + validatePartitionedIndex(parentIdx, parentTbl); relation_close(parentTbl, AccessShareLock); /* keep these locks till commit */ -- 2.50.1 (Apple Git-155)