ALTER COLUMN TYPE on a partitioned table fails with "relation ... already exists" when a partition has a local index with the name the rebuilt partition index picks ==================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a22716-075c-72cb-a849-6cc198ec66dd Filed: 2026-11-29 11:34:44 UTC Severity: 40/100 Blamed commit: 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ## Summary ALTER TABLE ... ALTER COLUMN TYPE on a partitioned table fails when a partition has its own (not attached) index on the column whose default name is the one the rebuilt partition index would choose. All names here are generated by default, so the setup is common: a partial or otherwise different index is created on a partition first, then an index on the parent. The ALTER then fails even for a no-op change such as int4 to int4. The same happens with UNIQUE and EXCLUDE constraints. ## Reproduction ```sql CREATE TABLE p (k int, a int) PARTITION BY LIST (k); CREATE TABLE p1 PARTITION OF p FOR VALUES IN (1); CREATE INDEX ON p1 (a) WHERE a > 0; -- p1_a_idx (local) CREATE INDEX ON p (a); -- p_a_idx; on p1: p1_a_idx1 ALTER TABLE p ALTER COLUMN a TYPE bigint; -- ERROR: relation "p1_a_idx" already exists ALTER TABLE p ALTER COLUMN a TYPE int4; -- same error ``` Constraint variants fail the same way: ```sql CREATE TABLE q (k int, a int, b int) PARTITION BY LIST (k); CREATE TABLE q1 (k int, a int, b int, CONSTRAINT q1_k_a_key UNIQUE (k, a, b)); ALTER TABLE q ATTACH PARTITION q1 FOR VALUES IN (1); ALTER TABLE q ADD UNIQUE (k, a); -- on q1: q1_k_a_key1 ALTER TABLE q ALTER COLUMN a TYPE bigint; -- ERROR: relation "q1_k_a_key" already exists CREATE EXTENSION btree_gist; CREATE TABLE x (k int, a int) PARTITION BY LIST (k); CREATE TABLE x1 (k int, a int, CONSTRAINT x1_k_a_excl EXCLUDE USING gist (k WITH =, a WITH <>)); ALTER TABLE x ATTACH PARTITION x1 FOR VALUES IN (1); ALTER TABLE x ADD EXCLUDE USING gist (k WITH =, a WITH =); ALTER TABLE x ALTER COLUMN a TYPE bigint; -- ERROR: relation "x1_k_a_excl" already exists ``` Expected: the ALTER succeeds and each index keeps its name. With the indexes created in the other order (parent index first), the ALTER succeeds. A non-partitioned table with the same two indexes also succeeds. The only workaround is to rename the local index first. The failure persists after a plain dump and restore, since the index names are kept. ## Analysis ATRewriteCatalogs() runs ATPostAlterTypeCleanup() for the parent first. Dropping p_a_idx also drops its partition p1_a_idx1, so the partition's own pass only remembers p1_a_idx, and queues "CREATE INDEX p1_a_idx ..." for it. In the later AT_PASS_OLD_INDEX pass the parent's CREATE INDEX runs first. DefineIndex() recurses, finds no matching index on p1 (the local one is not recreated yet, and would not match anyway), and creates a new partition index with ChooseIndexName(), which now returns the free name p1_a_idx. The queued re-creation of the local index then fails in index_create(). The attached partition index is not rebuilt under its own name; it gets a new generated name from the parent's recursion, so it can take the name of an unrelated index that is about to be recreated. ## Notes Partitioned indexes came from 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ("Local partitioned indexes"). The pgsql-hackers thread "Unexpected reindex when altering column types for partitioned tables" proposes remembering child indexes and recreating them before the parent, which would likely avoid this collision, but it is not committed and the thread does not mention this failure. Tested on master at f836f25b6f095f3b23762026831d2fab6117087c. Branches: master (reproduced at f836f25b); REL_14 through REL_19 likely by code inspection (the blame commit predates all supported branches, and the same ATPostAlterTypeCleanup / AT_PASS_OLD_INDEX flow is present there). Related: https://ai-annex.j-davis.com/report/01a1e671-1383-7ba0-8b80-039e7b839397 (same "already exists" failure in ALTER COLUMN TYPE, different trigger: another table's partitioned expression index); https://ai-annex.j-davis.com/report/01a21a5c-3f21-78de-b430-6fa561161087 (same root: rebuilt partition indexes are not recreated under their own identity, losing REPLICA IDENTITY, CLUSTER ON and comments)