ALTER COLUMN TYPE fails with "relation ... already exists" when another table's partitioned expression index selects a field of the altered rowtype =================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a1e671-1383-7ba0-8b80-039e7b839397 Filed: 2026-11-16 22:18:52 UTC Severity: 35/100 Blamed commit: 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ## Summary If a partitioned table has an expression index that selects a field of another table's rowtype, both the partitioned index and each partition's index record a direct dependency on the referenced column. ALTER COLUMN TYPE on that column remembers and recreates both independently, so the rebuild of the partitioned index creates the partition index and the separate rebuild of the partition index then collides with it. The ALTER fails. The outcome depends on the order of the remembered indexes. ## Reproduction create table attbl(a numeric); create function mk(text) returns attbl immutable language sql as $$ select row(length($1))::public.attbl $$; create table t2(b text) partition by list (b); create table t2a partition of t2 for values in ('abc'); create index i1 on t2(((mk(b)).a)); alter table attbl alter column a type bigint; -- ERROR: 42P07: relation "t2a_mk_b_idx" already exists If the partition's index is created first and then adopted by the parent index (create index ci on t2a(...); create index i1 on t2(...)), the same ALTER succeeds. The equivalent same-table case (partitioned index on (b+1), ALTER COLUMN b TYPE bigint) also succeeds. Expected: the ALTER succeeds, rebuilding the partitioned index once. ## Analysis RememberAllDependentForRebuilding() in src/backend/commands/tablecmds.c finds both indexes in the altered column's pg_depend entries and calls RememberIndexForRebuilding() for each, putting both in the altered table's work entry. ATPostAlterTypeCleanup() drops both and queues a CREATE INDEX for each on its own table. When the partitioned index is recreated first, DefineIndex() recurses and builds a new partition index under the old name; the queued re-creation of the partition index then fails. Nothing skips indexes that are partitions of an index being rebuilt. Partitioned indexes were introduced by commit 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ("Local partitioned indexes"). ## Notes Tested on master 7fc2f0423320e02beb8c44b863f1827604c47c75. Found while checking variants of pgsql-bugs BUG #19903; see the [ALTER COLUMN TYPE "unexpected object depending on column" report](https://ai-annex.j-davis.com/report/01a1e670-4a7d-7400-96fb-397ca26b0ac0), which reports a different internal error from the same column-dependency scan.