Patch
Don't expand invalid partitioned indexes during ALTER TYPE
- Branch
- master
- Risk
- –
- Base commit
- b5608391087c6baf1a9890bcfeedc126eef5c40f
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1cbd6-3048-716a-b7cc-eb5eef23c820 | jq -r .body | git am
From 0b2308e9268b01c446d06c03476b04afd3b3c006 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Wed, 11 Nov 2026 10:17:21 -0800
Subject: [PATCH] Don't expand invalid partitioned indexes during ALTER TYPE
pg_get_indexdef_string() rebuilt every partitioned index with
inherits=true, and dropping the parent cascaded to its children.
ALTER COLUMN TYPE and SET EXPRESSION therefore turned an invalid
ON ONLY partitioned index into a full recursive index. Partitions
that had no index gained one, and a UNIQUE rebuild could fail on
duplicates in those partitions.
When the partitioned index is invalid, deparse it (and a constraint
that owns it) with ONLY. Detach its direct children before the drop
and reattach those indexes after every affected table has rebuilt
its own. Valid partitioned indexes stay recursive.
---
src/backend/commands/tablecmds.c | 177 ++++++++++++++++++++
src/backend/utils/adt/ruleutils.c | 36 +++-
src/test/regress/expected/indexing.out | 221 +++++++++++++++++++++++++
src/test/regress/sql/indexing.sql | 169 +++++++++++++++++++
4 files changed, 596 insertions(+), 7 deletions(-)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index aa89c9a4637..706e0fe3af9 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -684,6 +684,13 @@ static void RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab);
static void RememberStatisticsForRebuilding(Oid stxoid, AlteredTableInfo *tab);
static void ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab,
LOCKMODE lockmode);
+static void PreserveInvalidPartitionedIndexes(AlteredTableInfo *tab);
+static bool PreserveOneInvalidPartIndex(AlteredTableInfo *tab, Oid indexoid,
+ List **done);
+static void QueuePartitionedIndexReattach(AlteredTableInfo *tab,
+ Oid parentidx, Oid childidx);
+static char *IndexNameForRebuild(Oid indexoid);
+static RangeVar *IndexRebuildRangeVar(Oid indexoid);
static void ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, Oid ownerId,
const char *cmdstring, List **wqueue,
LOCKMODE lockmode, bool rewrite);
@@ -16772,6 +16779,169 @@ RememberStatisticsForRebuilding(Oid stxoid, AlteredTableInfo *tab)
}
}
+/*
+ * Name the rebuilt index will have. Constraint indexes are recreated under
+ * the constraint name, which can differ from the current index name.
+ */
+static char *
+IndexNameForRebuild(Oid indexoid)
+{
+ Oid conoid = get_index_constraint(indexoid);
+ char *name;
+
+ if (OidIsValid(conoid))
+ name = get_constraint_name(conoid);
+ else
+ name = get_rel_name(indexoid);
+
+ if (name == NULL)
+ elog(ERROR, "cache lookup failed for index %u", indexoid);
+
+ return name;
+}
+
+/*
+ * Schema-qualified RangeVar for an index that will be rebuilt under
+ * IndexNameForRebuild. Partitions need not share the parent's schema.
+ */
+static RangeVar *
+IndexRebuildRangeVar(Oid indexoid)
+{
+ char *nspname;
+
+ nspname = get_namespace_name(get_rel_namespace(indexoid));
+ if (nspname == NULL)
+ elog(ERROR, "cache lookup failed for index %u", indexoid);
+
+ return makeRangeVar(nspname, IndexNameForRebuild(indexoid), -1);
+}
+
+/*
+ * Queue ALTER INDEX parent ATTACH PARTITION child, to run after every
+ * affected table has recreated its indexes. Names are copied now because
+ * the indexes are dropped later in this cleanup.
+ */
+static void
+QueuePartitionedIndexReattach(AlteredTableInfo *tab, Oid parentidx,
+ Oid childidx)
+{
+ AlterTableStmt *stmt = makeNode(AlterTableStmt);
+ AlterTableCmd *cmd = makeNode(AlterTableCmd);
+ PartitionCmd *partcmd = makeNode(PartitionCmd);
+
+ partcmd->name = IndexRebuildRangeVar(childidx);
+ partcmd->bound = NULL;
+ partcmd->concurrent = false;
+
+ cmd->subtype = AT_AttachPartition;
+ cmd->def = (Node *) partcmd;
+
+ stmt->relation = IndexRebuildRangeVar(parentidx);
+ stmt->cmds = list_make1(cmd);
+ stmt->objtype = OBJECT_INDEX;
+ stmt->missing_ok = false;
+
+ tab->afterStmts = lappend(tab->afterStmts, stmt);
+}
+
+/*
+ * Detach one level of children of an invalid partitioned index and remember
+ * how to attach those same indexes again after the rebuild.
+ *
+ * Only direct children are detached. Descendants are later in the work
+ * queue than their ancestors, so an invalid child does this again for its
+ * own children. A valid partitioned index is left attached: the ancestor
+ * drop still cascades, and the recursive rebuild creates the descendants.
+ * Detaching those too would create each descendant twice.
+ */
+static bool
+PreserveOneInvalidPartIndex(AlteredTableInfo *tab, Oid indexoid, List **done)
+{
+ List *children;
+ ListCell *lc;
+ bool detached = false;
+
+ if (!OidIsValid(indexoid) || list_member_oid(*done, indexoid))
+ return false;
+
+ *done = lappend_oid(*done, indexoid);
+
+ if (get_rel_relkind(indexoid) != RELKIND_PARTITIONED_INDEX)
+ return false;
+ if (get_index_isvalid(indexoid))
+ return false;
+
+ children = find_inheritance_children(indexoid, AccessExclusiveLock);
+ foreach(lc, children)
+ {
+ Oid childidx = lfirst_oid(lc);
+ Relation idx;
+ Oid parentcon;
+ Oid childcon;
+
+ QueuePartitionedIndexReattach(tab, indexoid, childidx);
+
+ idx = index_open(childidx, AccessExclusiveLock);
+ IndexSetParentIndex(idx, InvalidOid);
+
+ /*
+ * A constraint index in a partition can be the child of a
+ * non-constraint index, so detach the constraint only when both sides
+ * have one. ConstraintSetParentConstraint does not CCI; the caller
+ * does, after every child, so the partition dependencies are gone
+ * before the parent is dropped.
+ */
+ parentcon = get_index_constraint(indexoid);
+ childcon = get_index_constraint(childidx);
+ if (OidIsValid(parentcon) && OidIsValid(childcon))
+ ConstraintSetParentConstraint(childcon, InvalidOid, InvalidOid);
+
+ index_close(idx, NoLock);
+ detached = true;
+ }
+ list_free(children);
+
+ return detached;
+}
+
+/*
+ * Preserve every invalid partitioned index this table is about to drop.
+ * Plain indexes and constraint indexes are recorded separately; a constraint
+ * index is not also present in changedIndexOids.
+ */
+static void
+PreserveInvalidPartitionedIndexes(AlteredTableInfo *tab)
+{
+ List *done = NIL;
+ ListCell *lc;
+ bool need_cci = false;
+
+ foreach(lc, tab->changedIndexOids)
+ {
+ if (PreserveOneInvalidPartIndex(tab, lfirst_oid(lc), &done))
+ need_cci = true;
+ }
+
+ foreach(lc, tab->changedConstraintOids)
+ {
+ Oid indoid = get_constraint_index(lfirst_oid(lc));
+
+ if (OidIsValid(indoid) &&
+ PreserveOneInvalidPartIndex(tab, indoid, &done))
+ need_cci = true;
+ }
+
+ /*
+ * IndexSetParentIndex makes its own catalog updates visible, but
+ * detaching a constraint does not. performMultipleDeletions will not see
+ * those dependency deletions without a CCI here.
+ */
+ if (need_cci)
+ CommandCounterIncrement();
+
+ list_free(done);
+}
+
/*
* Cleanup after we've finished all the ALTER TYPE or SET EXPRESSION
* operations for a particular relation. We have to drop and recreate all the
@@ -16795,6 +16965,13 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
*/
objects = new_object_addresses();
+ /*
+ * Detach children of invalid partitioned indexes before the drops below.
+ * Otherwise the cascade removes the only record of which partitions were
+ * indexed, and the rebuilt parent is created without ONLY.
+ */
+ PreserveInvalidPartitionedIndexes(tab);
+
/*
* Re-parse the index and constraint definitions, and attach them to the
* appropriate work queue entries. We do this before dropping because in
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 8f6a46fa614..6b6390c8b91 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -1223,13 +1223,24 @@ pg_get_indexdef_ext(PG_FUNCTION_ARGS)
* Internal version for use by ALTER TABLE.
* Includes a tablespace clause in the result.
* Returns a palloc'd C string; no pretty-printing.
+ *
+ * An invalid partitioned index is incomplete: it was created with ONLY, or a
+ * partition never received an index. Emit ONLY so the rebuild does not
+ * recurse into partitions that were not indexed. A valid partitioned index
+ * covers every partition and keeps the recursive definition.
*/
char *
pg_get_indexdef_string(Oid indexrelid)
{
+ bool inherits = true;
+
+ if (get_rel_relkind(indexrelid) == RELKIND_PARTITIONED_INDEX &&
+ !get_index_isvalid(indexrelid))
+ inherits = false;
+
return pg_get_indexdef_worker(indexrelid, 0, NULL,
false, false,
- true, true,
+ true, inherits,
0, false);
}
@@ -2242,14 +2253,25 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
{
if (OidIsValid(conForm->conrelid))
{
+ bool only = false;
+
/*
- * Currently, callers want ALTER TABLE (without ONLY) for CHECK
- * constraints, and other types of constraints don't inherit
- * anyway so it doesn't matter whether we say ONLY or not. Someday
- * we might need to let callers specify whether to put ONLY in the
- * command.
+ * CHECK constraints are rebuilt without ONLY so they recurse.
+ * Primary key, unique, and exclusion constraints do inherit. When
+ * the supporting index is an invalid partitioned index, the
+ * constraint is incomplete and must be rebuilt with ONLY;
+ * otherwise partitions that were never constrained gain one.
*/
- appendStringInfo(&buf, "ALTER TABLE %s ADD CONSTRAINT %s ",
+ if ((conForm->contype == CONSTRAINT_PRIMARY ||
+ conForm->contype == CONSTRAINT_UNIQUE ||
+ conForm->contype == CONSTRAINT_EXCLUSION) &&
+ OidIsValid(conForm->conindid) &&
+ get_rel_relkind(conForm->conindid) == RELKIND_PARTITIONED_INDEX &&
+ !get_index_isvalid(conForm->conindid))
+ only = true;
+
+ appendStringInfo(&buf, "ALTER TABLE%s %s ADD CONSTRAINT %s ",
+ only ? " ONLY" : "",
generate_qualified_relation_name(conForm->conrelid),
quote_identifier(NameStr(conForm->conname)));
}
diff --git a/src/test/regress/expected/indexing.out b/src/test/regress/expected/indexing.out
index 4a0a652e9f6..15f4960d685 100644
--- a/src/test/regress/expected/indexing.out
+++ b/src/test/regress/expected/indexing.out
@@ -1800,3 +1800,224 @@ alter table idx_excl_part_1 drop constraint idxpart_1_id_data_excl;
alter table idx_excl_part_1 add constraint idxpart_1_id_data_excl exclude using gist (a with =, b with &&);
alter index idxpart_id_data_excl attach partition idxpart_1_id_data_excl;
-- leave these tables around, for pg_upgrade testing
+--
+-- ALTER COLUMN TYPE / SET EXPRESSION must not complete an invalid
+-- partitioned index. Partitions that were not indexed stay unindexed,
+-- and a UNIQUE rebuild must not fail on duplicates that live there.
+--
+create table idxpart_only (a int, b int) partition by list (b);
+create table idxpart_only_1 partition of idxpart_only for values in (1);
+create table idxpart_only_2 partition of idxpart_only for values in (2);
+create index idxpart_only_idx on only idxpart_only (a);
+create index idxpart_only_1_idx on idxpart_only_1 (a);
+alter index idxpart_only_idx attach partition idxpart_only_1_idx;
+insert into idxpart_only values (1, 1), (1, 2), (1, 2);
+alter table idxpart_only alter a type bigint;
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_only%'
+ order by c.relname collate "C";
+ relname | indisvalid | children
+--------------------+------------+--------------------
+ idxpart_only_1_idx | t |
+ idxpart_only_idx | f | idxpart_only_1_idx
+(2 rows)
+
+drop table idxpart_only;
+-- No attached children: the parent stays invalid and nothing is built.
+create table idxpart_none (a int, b int) partition by list (b);
+create table idxpart_none_1 partition of idxpart_none for values in (1);
+create table idxpart_none_2 partition of idxpart_none for values in (2);
+create index idxpart_none_idx on only idxpart_none (a);
+alter table idxpart_none alter a type bigint;
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_none%'
+ order by c.relname collate "C";
+ relname | indisvalid
+------------------+------------
+ idxpart_none_idx | f
+(1 row)
+
+drop table idxpart_none;
+-- Duplicates in an unindexed partition must not fail the ALTER, and the
+-- indexed partition must keep rejecting duplicates.
+create table idxpart_only_u (a int, b int) partition by list (b);
+create table idxpart_only_u1 partition of idxpart_only_u for values in (1);
+create table idxpart_only_u2 partition of idxpart_only_u for values in (2);
+insert into idxpart_only_u values (5, 2), (5, 2), (7, 1);
+create unique index idxpart_only_ui on only idxpart_only_u (a, b);
+create unique index idxpart_only_u1i on idxpart_only_u1 (a, b);
+alter index idxpart_only_ui attach partition idxpart_only_u1i;
+alter table idxpart_only_u alter a type bigint;
+insert into idxpart_only_u values (5, 2);
+insert into idxpart_only_u values (7, 1);
+ERROR: duplicate key value violates unique constraint "idxpart_only_u1i"
+DETAIL: Key (a, b)=(7, 1) already exists.
+select c.relname, i.indisvalid, i.indisunique
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_only_u%'
+ order by c.relname collate "C";
+ relname | indisvalid | indisunique
+------------------+------------+-------------
+ idxpart_only_u1i | t | t
+ idxpart_only_ui | f | t
+(2 rows)
+
+drop table idxpart_only_u;
+-- Same preservation for ALTER TABLE ONLY ADD CONSTRAINT.
+create table idxpart_only_c (a int, b int) partition by list (b);
+create table idxpart_only_c1 partition of idxpart_only_c for values in (1);
+create table idxpart_only_c2 partition of idxpart_only_c for values in (2);
+insert into idxpart_only_c values (5, 2), (5, 2), (7, 1);
+alter table only idxpart_only_c add constraint idxpart_only_ck unique (a, b);
+alter table idxpart_only_c1 add constraint idxpart_only_c1k unique (a, b);
+alter index idxpart_only_ck attach partition idxpart_only_c1k;
+alter table idxpart_only_c alter a type bigint;
+insert into idxpart_only_c values (5, 2);
+insert into idxpart_only_c values (7, 1);
+ERROR: duplicate key value violates unique constraint "idxpart_only_c1k"
+DETAIL: Key (a, b)=(7, 1) already exists.
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_only_c%'
+ order by c.relname collate "C";
+ relname | indisvalid
+------------------+------------
+ idxpart_only_c1k | t
+ idxpart_only_ck | f
+(2 rows)
+
+drop table idxpart_only_c;
+-- A complete partitioned index is still rebuilt on every partition.
+create table idxpart_full (a int, b int) partition by list (b);
+create table idxpart_full_1 partition of idxpart_full for values in (1);
+create table idxpart_full_2 partition of idxpart_full for values in (2);
+create index idxpart_full_idx on idxpart_full (a);
+alter table idxpart_full alter a type bigint;
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_full%'
+ order by c.relname collate "C";
+ relname | indisvalid
+----------------------+------------
+ idxpart_full_1_a_idx | t
+ idxpart_full_2_a_idx | t
+ idxpart_full_idx | t
+(3 rows)
+
+drop table idxpart_full;
+-- Binary-compatible typmod changes rebuild partitioned indexes too.
+create table idxpart_tm (a varchar(4), b int) partition by list (b);
+create table idxpart_tm_1 partition of idxpart_tm for values in (1);
+create table idxpart_tm_2 partition of idxpart_tm for values in (2);
+create index idxpart_tm_idx on only idxpart_tm (a);
+create index idxpart_tm_1_idx on idxpart_tm_1 (a);
+alter index idxpart_tm_idx attach partition idxpart_tm_1_idx;
+alter table idxpart_tm alter a type varchar(8);
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_tm%'
+ order by c.relname collate "C";
+ relname | indisvalid
+------------------+------------
+ idxpart_tm_1_idx | t
+ idxpart_tm_idx | f
+(2 rows)
+
+drop table idxpart_tm;
+-- SET EXPRESSION uses the same rebuild path.
+create table idxpart_gen (a int, b int generated always as (a + 1) stored)
+ partition by list (a);
+create table idxpart_gen_1 partition of idxpart_gen for values in (1);
+create table idxpart_gen_2 partition of idxpart_gen for values in (2);
+create index idxpart_gen_idx on only idxpart_gen (b);
+create index idxpart_gen_1_idx on idxpart_gen_1 (b);
+alter index idxpart_gen_idx attach partition idxpart_gen_1_idx;
+alter table idxpart_gen alter column b set expression as (a + 2);
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_gen%'
+ order by c.relname collate "C";
+ relname | indisvalid | children
+-------------------+------------+-------------------
+ idxpart_gen_1_idx | t |
+ idxpart_gen_idx | f | idxpart_gen_1_idx
+(2 rows)
+
+drop table idxpart_gen;
+-- Multi-level: each invalid level keeps only the children it had.
+create table idxpart_ml (a int, b int, c int) partition by list (b);
+create table idxpart_ml_1 partition of idxpart_ml for values in (1)
+ partition by list (c);
+create table idxpart_ml_1_1 partition of idxpart_ml_1 for values in (1);
+create table idxpart_ml_1_2 partition of idxpart_ml_1 for values in (2);
+create table idxpart_ml_2 partition of idxpart_ml for values in (2);
+create index idxpart_ml_idx on only idxpart_ml (a);
+create index idxpart_ml_1_idx on only idxpart_ml_1 (a);
+create index idxpart_ml_1_1_idx on idxpart_ml_1_1 (a);
+alter index idxpart_ml_1_idx attach partition idxpart_ml_1_1_idx;
+alter index idxpart_ml_idx attach partition idxpart_ml_1_idx;
+alter table idxpart_ml alter a type bigint;
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_ml%'
+ order by c.relname collate "C";
+ relname | indisvalid | children
+--------------------+------------+--------------------
+ idxpart_ml_1_1_idx | t |
+ idxpart_ml_1_idx | f | idxpart_ml_1_1_idx
+ idxpart_ml_idx | f | idxpart_ml_1_idx
+(3 rows)
+
+drop table idxpart_ml;
+-- Valid intermediate index: its descendants are rebuilt with it, and the
+-- invalid top index does not gain an index on the uncovered sibling.
+create table idxpart_mlv (a int, b int, c int) partition by list (b);
+create table idxpart_mlv_1 partition of idxpart_mlv for values in (1)
+ partition by list (c);
+create table idxpart_mlv_1_1 partition of idxpart_mlv_1 for values in (1);
+create table idxpart_mlv_1_2 partition of idxpart_mlv_1 for values in (2);
+create table idxpart_mlv_2 partition of idxpart_mlv for values in (2);
+create index idxpart_mlv_1_idx on idxpart_mlv_1 (a);
+create index idxpart_mlv_idx on only idxpart_mlv (a);
+alter index idxpart_mlv_idx attach partition idxpart_mlv_1_idx;
+alter table idxpart_mlv alter a type bigint;
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_mlv%'
+ order by c.relname collate "C";
+ relname | indisvalid | children
+-----------------------+------------+---------------------------------------------
+ idxpart_mlv_1_1_a_idx | t |
+ idxpart_mlv_1_2_a_idx | t |
+ idxpart_mlv_1_idx | t | idxpart_mlv_1_1_a_idx,idxpart_mlv_1_2_a_idx
+ idxpart_mlv_idx | f | idxpart_mlv_1_idx
+(4 rows)
+
+drop table idxpart_mlv;
diff --git a/src/test/regress/sql/indexing.sql b/src/test/regress/sql/indexing.sql
index bbcfb365281..e3482cee942 100644
--- a/src/test/regress/sql/indexing.sql
+++ b/src/test/regress/sql/indexing.sql
@@ -1019,3 +1019,172 @@ alter table idx_excl_part_1 drop constraint idxpart_1_id_data_excl;
alter table idx_excl_part_1 add constraint idxpart_1_id_data_excl exclude using gist (a with =, b with &&);
alter index idxpart_id_data_excl attach partition idxpart_1_id_data_excl;
-- leave these tables around, for pg_upgrade testing
+
+--
+-- ALTER COLUMN TYPE / SET EXPRESSION must not complete an invalid
+-- partitioned index. Partitions that were not indexed stay unindexed,
+-- and a UNIQUE rebuild must not fail on duplicates that live there.
+--
+create table idxpart_only (a int, b int) partition by list (b);
+create table idxpart_only_1 partition of idxpart_only for values in (1);
+create table idxpart_only_2 partition of idxpart_only for values in (2);
+create index idxpart_only_idx on only idxpart_only (a);
+create index idxpart_only_1_idx on idxpart_only_1 (a);
+alter index idxpart_only_idx attach partition idxpart_only_1_idx;
+insert into idxpart_only values (1, 1), (1, 2), (1, 2);
+alter table idxpart_only alter a type bigint;
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_only%'
+ order by c.relname collate "C";
+drop table idxpart_only;
+
+-- No attached children: the parent stays invalid and nothing is built.
+create table idxpart_none (a int, b int) partition by list (b);
+create table idxpart_none_1 partition of idxpart_none for values in (1);
+create table idxpart_none_2 partition of idxpart_none for values in (2);
+create index idxpart_none_idx on only idxpart_none (a);
+alter table idxpart_none alter a type bigint;
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_none%'
+ order by c.relname collate "C";
+drop table idxpart_none;
+
+-- Duplicates in an unindexed partition must not fail the ALTER, and the
+-- indexed partition must keep rejecting duplicates.
+create table idxpart_only_u (a int, b int) partition by list (b);
+create table idxpart_only_u1 partition of idxpart_only_u for values in (1);
+create table idxpart_only_u2 partition of idxpart_only_u for values in (2);
+insert into idxpart_only_u values (5, 2), (5, 2), (7, 1);
+create unique index idxpart_only_ui on only idxpart_only_u (a, b);
+create unique index idxpart_only_u1i on idxpart_only_u1 (a, b);
+alter index idxpart_only_ui attach partition idxpart_only_u1i;
+alter table idxpart_only_u alter a type bigint;
+insert into idxpart_only_u values (5, 2);
+insert into idxpart_only_u values (7, 1);
+select c.relname, i.indisvalid, i.indisunique
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_only_u%'
+ order by c.relname collate "C";
+drop table idxpart_only_u;
+
+-- Same preservation for ALTER TABLE ONLY ADD CONSTRAINT.
+create table idxpart_only_c (a int, b int) partition by list (b);
+create table idxpart_only_c1 partition of idxpart_only_c for values in (1);
+create table idxpart_only_c2 partition of idxpart_only_c for values in (2);
+insert into idxpart_only_c values (5, 2), (5, 2), (7, 1);
+alter table only idxpart_only_c add constraint idxpart_only_ck unique (a, b);
+alter table idxpart_only_c1 add constraint idxpart_only_c1k unique (a, b);
+alter index idxpart_only_ck attach partition idxpart_only_c1k;
+alter table idxpart_only_c alter a type bigint;
+insert into idxpart_only_c values (5, 2);
+insert into idxpart_only_c values (7, 1);
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_only_c%'
+ order by c.relname collate "C";
+drop table idxpart_only_c;
+
+-- A complete partitioned index is still rebuilt on every partition.
+create table idxpart_full (a int, b int) partition by list (b);
+create table idxpart_full_1 partition of idxpart_full for values in (1);
+create table idxpart_full_2 partition of idxpart_full for values in (2);
+create index idxpart_full_idx on idxpart_full (a);
+alter table idxpart_full alter a type bigint;
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_full%'
+ order by c.relname collate "C";
+drop table idxpart_full;
+
+-- Binary-compatible typmod changes rebuild partitioned indexes too.
+create table idxpart_tm (a varchar(4), b int) partition by list (b);
+create table idxpart_tm_1 partition of idxpart_tm for values in (1);
+create table idxpart_tm_2 partition of idxpart_tm for values in (2);
+create index idxpart_tm_idx on only idxpart_tm (a);
+create index idxpart_tm_1_idx on idxpart_tm_1 (a);
+alter index idxpart_tm_idx attach partition idxpart_tm_1_idx;
+alter table idxpart_tm alter a type varchar(8);
+select c.relname, i.indisvalid
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_tm%'
+ order by c.relname collate "C";
+drop table idxpart_tm;
+
+-- SET EXPRESSION uses the same rebuild path.
+create table idxpart_gen (a int, b int generated always as (a + 1) stored)
+ partition by list (a);
+create table idxpart_gen_1 partition of idxpart_gen for values in (1);
+create table idxpart_gen_2 partition of idxpart_gen for values in (2);
+create index idxpart_gen_idx on only idxpart_gen (b);
+create index idxpart_gen_1_idx on idxpart_gen_1 (b);
+alter index idxpart_gen_idx attach partition idxpart_gen_1_idx;
+alter table idxpart_gen alter column b set expression as (a + 2);
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_gen%'
+ order by c.relname collate "C";
+drop table idxpart_gen;
+
+-- Multi-level: each invalid level keeps only the children it had.
+create table idxpart_ml (a int, b int, c int) partition by list (b);
+create table idxpart_ml_1 partition of idxpart_ml for values in (1)
+ partition by list (c);
+create table idxpart_ml_1_1 partition of idxpart_ml_1 for values in (1);
+create table idxpart_ml_1_2 partition of idxpart_ml_1 for values in (2);
+create table idxpart_ml_2 partition of idxpart_ml for values in (2);
+create index idxpart_ml_idx on only idxpart_ml (a);
+create index idxpart_ml_1_idx on only idxpart_ml_1 (a);
+create index idxpart_ml_1_1_idx on idxpart_ml_1_1 (a);
+alter index idxpart_ml_1_idx attach partition idxpart_ml_1_1_idx;
+alter index idxpart_ml_idx attach partition idxpart_ml_1_idx;
+alter table idxpart_ml alter a type bigint;
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_ml%'
+ order by c.relname collate "C";
+drop table idxpart_ml;
+
+-- Valid intermediate index: its descendants are rebuilt with it, and the
+-- invalid top index does not gain an index on the uncovered sibling.
+create table idxpart_mlv (a int, b int, c int) partition by list (b);
+create table idxpart_mlv_1 partition of idxpart_mlv for values in (1)
+ partition by list (c);
+create table idxpart_mlv_1_1 partition of idxpart_mlv_1 for values in (1);
+create table idxpart_mlv_1_2 partition of idxpart_mlv_1 for values in (2);
+create table idxpart_mlv_2 partition of idxpart_mlv for values in (2);
+create index idxpart_mlv_1_idx on idxpart_mlv_1 (a);
+create index idxpart_mlv_idx on only idxpart_mlv (a);
+alter index idxpart_mlv_idx attach partition idxpart_mlv_1_idx;
+alter table idxpart_mlv alter a type bigint;
+select c.relname, i.indisvalid,
+ (select string_agg(cc.relname, ',' order by cc.relname collate "C")
+ from pg_inherits inh
+ join pg_class cc on cc.oid = inh.inhrelid
+ where inh.inhparent = c.oid) as children
+ from pg_index i
+ join pg_class c on c.oid = i.indexrelid
+ where c.relname like 'idxpart_mlv%'
+ order by c.relname collate "C";
+drop table idxpart_mlv;
base-commit: b5608391087c6baf1a9890bcfeedc126eef5c40f
--
2.53.0