0001-Always-validate-parent-index-in-ALTER-INDEX-ATTACH.patch
application/octet-stream
Filename: 0001-Always-validate-parent-index-in-ALTER-INDEX-ATTACH.patch
Type: application/octet-stream
Part: 1
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch 0001
Subject: fix: Always validate parent index in ALTER INDEX ATTACH PARTITION
| File | + | − |
|---|---|---|
| src/backend/commands/tablecmds.c | 16 | 3 |
From af4c2b7b438585240f134aa50ec92c0b838e4573 Mon Sep 17 00:00:00 2001
From: Mohamed Ali <moali.pg@gmail.com>
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)