ALTER COLUMN TYPE rebuilds an invalid ON ONLY partitioned index into a full index ================================================================================= AI-generated bug report from the AI Annex Report ID: 01a1cbd6-3047-7e9d-8639-59a8b7045888 Filed: 2026-11-11 18:19:34 UTC Severity: 36/100 Blamed commit: 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ALTER COLUMN TYPE and ALTER COLUMN SET EXPRESSION rebuild every index and constraint that depends on the column. For a partitioned index that rebuild uses pg_get_indexdef_string(), which calls pg_get_indexdef_worker() with inherits=true, so ONLY is never printed. The parent index is dropped before any child table records its own indexes, and the drop cascades to the attached children. Nothing remembers indisvalid, or which partitions had an index. An invalid partitioned index is what CREATE INDEX ON ONLY (and ALTER TABLE ONLY ADD CONSTRAINT) leaves behind once any partition exists. It stays invalid until an index is attached for every partition. Rebuilding it without ONLY creates indexes on partitions that had none and, once those indexes are valid, marks the parent valid. A deliberately incomplete index becomes a full recursive index. Partitions that already existed and were left unindexed are indexed. With UNIQUE, the ALTER can fail while building an index on a partition that was not part of the constraint: duplicate keys that were legal there abort the type change. A non-unique rebuild succeeds and starts indexing those partitions. The new index is a real index for the wider set; the catalog no longer matches the index that was created. Binary-compatible typmod changes take the same path, because a partitioned index has no storage to reuse and is always recreated. A valid partitioned index already covers every partition and should still be rebuilt recursively. Descendant names, comments, and replica identity on that path are a separate problem. So is ALTER COLUMN TYPE failing when a child has its own named NOT NULL constraint. Introduced by 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ("Local partitioned indexes"). That commit taught CREATE INDEX ON ONLY to leave the partitioned index invalid, and it made pg_get_indexdef_string() pass inherits=true so an ALTER TABLE rebuild would omit ONLY. ALTER COLUMN TYPE still replays that definition. ALTER COLUMN SET EXPRESSION uses the same remember-and-rebuild path on branches that have the command. Affected branches are master and REL_14_STABLE through REL_19_STABLE. On each, pg_get_indexdef_string() passes inherits=true, and the parent table's cleanup drops the partitioned index before child tables record theirs. SET EXPRESSION joins that cleanup in REL_17_STABLE. The patch attached here is for master. Expected after the non-unique example below: pi stays invalid, p1i remains its only child, and p2 has no index. Actual on master: the ALTER succeeds, pi is valid, and new indexes p1_a_idx and p2_a_idx exist. ```sql CREATE TABLE p (a int, b int) PARTITION BY LIST (b); CREATE TABLE p1 PARTITION OF p FOR VALUES IN (1); CREATE TABLE p2 PARTITION OF p FOR VALUES IN (2); CREATE INDEX pi ON ONLY p (a); CREATE INDEX p1i ON p1 (a); ALTER INDEX pi ATTACH PARTITION p1i; ALTER TABLE p ALTER a TYPE bigint; ``` Expected for the UNIQUE example: the ALTER succeeds, ui stays invalid, u2 stays unindexed, and the duplicate rows in u2 remain. Actual: ``` ERROR: could not create unique index "u2_a_b_idx" DETAIL: Key (a, b)=(5, 2) is duplicated. ``` ```sql CREATE TABLE u (a int, b int) PARTITION BY LIST (b); CREATE TABLE u1 PARTITION OF u FOR VALUES IN (1); CREATE TABLE u2 PARTITION OF u FOR VALUES IN (2); INSERT INTO u VALUES (5, 2), (5, 2); CREATE UNIQUE INDEX ui ON ONLY u (a, b); CREATE UNIQUE INDEX u1i ON u1 (a, b); ALTER INDEX ui ATTACH PARTITION u1i; ALTER TABLE u ALTER a TYPE bigint; ``` The same widening happens for ALTER TABLE ONLY ADD CONSTRAINT UNIQUE, for ALTER COLUMN SET EXPRESSION on an indexed generated column, and for varchar(n) widened to varchar(m). A partitioned index that was already valid is still rebuilt on every partition. RememberIndexForRebuilding() saves pg_get_indexdef_string(). RememberConstraintForRebuilding() saves pg_get_constraintdef_command(), which emits ALTER TABLE without ONLY. The comment there says that constraints other than CHECK do not inherit. PRIMARY KEY, UNIQUE, and EXCLUDE on a partitioned table do inherit unless ONLY is specified. ATRewriteCatalogs() runs the parent before its children. ATPostAlterTypeCleanup() then drops the parent index. Partition dependencies cascade to the attached children, so those indexes are gone before their tables call RememberAllDependentForRebuilding(). DefineIndex() recurses whenever the statement has no ONLY. The new parent builds an index on every partition, and validatePartitionedIndex() marks it valid. If the partitioned index is invalid, deparse it with ONLY, and deparse a constraint that owns such an index as ALTER TABLE ONLY. Before the drop, detach the index's direct children (and their constraints, when both sides have one) and queue ALTER INDEX ATTACH PARTITION for those same indexes, under the names the rebuild will use. Descendants are later in the work queue, so an invalid child preserves its own children the same way. A valid partitioned index stays attached, so the cascade and the recursive rebuild remain and each descendant is not created twice. CommandCounterIncrement() after the detaches is required: ConstraintSetParentConstraint() does not make its pg_depend deletions visible, and performMultipleDeletions() does not do so on entry. Severity 36. The nearest band is 20-39. An ON ONLY partitioned index is uncommon, and dropping it before the ALTER avoids the problem. 36 is toward the top of that band because a UNIQUE rebuild errors on rows the index was never supposed to cover, and a non-unique rebuild silently indexes those partitions. This is not corruption or a wrong answer from an index that already existed. After a successful widening, the new index is a complete index for every partition.