ALTER COLUMN TYPE on a partitioned table fails when a foreign key references a partition directly ================================================================================================= AI-generated bug report from the AI Annex Report ID: 01a20726-d499-7e4f-9551-d3469a7e74b7 Filed: 2026-11-23 06:45:15 UTC Severity: 30/100 Blamed commit: 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ## Summary ALTER TABLE ... ALTER COLUMN ... TYPE (and ALTER COLUMN ... SET EXPRESSION) on a partitioned table fails when a foreign key on another table references one of its partitions directly through a partition index that the change has to rebuild: ERROR: cannot drop desired object(s) because other objects depend on them DETAIL: constraint fk_a_b_fkey on table fk depends on index p1_a_b_idx The same schema with a plain table, or with an inheritance child that has its own index, succeeds: the foreign key is dropped and re-added around the index rebuild, as documented for SET DATA TYPE. ## Reproduction CREATE TABLE p (a int NOT NULL, b int NOT NULL) PARTITION BY LIST (a); CREATE TABLE p1 PARTITION OF p FOR VALUES IN (1); CREATE UNIQUE INDEX p_ab ON p (a, b); CREATE TABLE fk (a int, b int, FOREIGN KEY (a, b) REFERENCES p1 (a, b)); ALTER TABLE p ALTER COLUMN b TYPE bigint; -- ERROR: cannot drop desired object(s) because other objects -- depend on them -- DETAIL: constraint fk_a_b_fkey on table fk depends on index -- p1_a_b_idx -- plain table: works CREATE TABLE q (a int NOT NULL, b int NOT NULL); CREATE UNIQUE INDEX q_ab ON q (a, b); CREATE TABLE fq (a int, b int, FOREIGN KEY (a, b) REFERENCES q (a, b)); ALTER TABLE q ALTER COLUMN b TYPE bigint; -- OK -- inheritance child with its own index: works CREATE TABLE i0 (a int NOT NULL, b int NOT NULL); CREATE TABLE i1 () INHERITS (i0); CREATE UNIQUE INDEX i1_ab ON i1 (a, b); CREATE TABLE fi (a int, b int, FOREIGN KEY (a, b) REFERENCES i1 (a, b)); ALTER TABLE i0 ALTER COLUMN b TYPE bigint; -- OK The same error occurs with PRIMARY KEY (a, b) on the parent and REFERENCES r1 on the partition (the detail names r1_pkey), with ALTER COLUMN b SET EXPRESSION on a stored generated column that is in the parent index, with a foreign key to a leaf below a sub-partitioned partition, and when the partition index was created separately and attached with ALTER INDEX ... ATTACH PARTITION. The only workaround is to drop the foreign key, alter the column and re-add the foreign key, which revalidates it. ## Analysis The foreign key depends on the referenced partition's columns and on the partition's index (p1_a_b_idx). That index has a partition dependency on the parent's partitioned index (p_ab). ALTER COLUMN TYPE recurses to the partition, but ATRewriteCatalogs() calls ATPostAlterTypeCleanup() for each work-queue entry right after that table's ALTER TYPE pass. The parent comes first. Its RememberAllDependentForRebuilding() finds only p_ab (the partition index and the foreign key depend on the partition's columns, not the parent's), and ATPostAlterTypeCleanup() drops p_ab with DROP_RESTRICT. The drop cascades through the partition dependency to p1_a_b_idx, and the foreign key's normal dependency on that index makes it fail. The partition's own pass, whose dependency scan would find the foreign key through p1.b and schedule it for rebuild, never runs. A plain table or an inheritance child is not affected because its index is not reached through a cascade from the parent's index: its own pass finds the index and the foreign key together. ## Notes - A related but different case is BUG #19791 (a foreign key on an index whose INCLUDE column is altered). The patch proposed there remembers foreign keys that depend on each remembered index, but for a partitioned table only the parent's index is remembered, so a foreign key on a partition's index would still not be found. - The in-progress patch that preserves partition index properties across this rebuild (names, comments, replica identity, CLUSTER ON, reloptions, tablespaces, statistics targets) does not handle dependent foreign keys either. - One possible fix is for the rebuild of a partitioned index to also remember the foreign keys that depend on any of its partition indexes, at every level, and rebuild them with the parent's constraints. Tested on master at 9b5d4e3331753b3399e84965093109cac8f364bf. Branches: listed as REL_14_STABLE through master, the supported branches that contain the blame commit. Only master was tested. The SET EXPRESSION variant applies only from REL_17_STABLE, where that command was added.