ALTER TABLE combining ALTER COLUMN SET EXPRESSION with ALTER COLUMN TYPE fails with "cache lookup failed for constraint/index" when the type-changed column has a dependent constraint or index =============================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a234f4-9ed7-7af2-b511-e141f3820ffa Filed: 2026-12-02 04:12:56 UTC Severity: 30/100 Blamed commit: 5d06e99a3cfc23bbc217b4d78b8c070ad52f720e ## Summary A single ALTER TABLE that contains both ALTER COLUMN ... SET EXPRESSION and ALTER COLUMN ... TYPE fails with an internal "cache lookup failed for constraint " (or "for index ") error. This happens whenever the column whose type changes has a dependent CHECK constraint, NOT NULL constraint or index. The generated column and the type-changed column don't need to be related. Running the two subcommands as separate statements works. ## Reproduction CREATE TABLE c1 (a int, b int, v int GENERATED ALWAYS AS (b + 1) STORED); ALTER TABLE c1 ADD CONSTRAINT apos CHECK (a > 0); ALTER TABLE c1 ALTER COLUMN v SET EXPRESSION AS (a * 2), ALTER COLUMN a TYPE bigint; -- ERROR: XX000: cache lookup failed for constraint 17412 -- LOCATION: ATPostAlterTypeCleanup, tablecmds.c CREATE TABLE c4 (a int, b int, v int GENERATED ALWAYS AS (b + 1) VIRTUAL); CREATE INDEX c4_a ON c4 (a); ALTER TABLE c4 ALTER COLUMN v SET EXPRESSION AS (a * 2), ALTER COLUMN a TYPE bigint; -- ERROR: XX000: cache lookup failed for index 17430 -- LOCATION: IndexGetRelation, index.c The same happens with `a int NOT NULL` and no other constraints. Expected: both subcommands succeed, as they do when run as two separate ALTER TABLE statements. Tested on master at dc168a83874fbf5cee4afb76a623e1349a539724. ## Analysis ATRewriteCatalogs() calls ATPostAlterTypeCleanup() after the AT_PASS_ALTER_TYPE pass and again after the AT_PASS_SET_EXPRESSION pass: if (pass == AT_PASS_ALTER_TYPE || pass == AT_PASS_SET_EXPRESSION) ATPostAlterTypeCleanup(wqueue, tab, lockmode); ATPostAlterTypeCleanup() walks tab->changedConstraintOids, changedIndexOids and changedStatisticsOids, queues the re-add commands and drops the old objects with performMultipleDeletions(). It never resets those lists. On the second call, made after the SET EXPRESSION pass, it walks the same OIDs again. They were already dropped, so the syscache lookup fails. The replicaIdentityIndex and clusterOnIndex markers are also not reset, so their re-mark subcommands would be queued twice. 5d06e99a3cf (ALTER TABLE command to change generation expression) added AT_PASS_SET_EXPRESSION and the second cleanup call. That function had assumed it ran only once per relation, as its own comment says. A possible fix is to reset the changed* lists and the replica identity and cluster markers at the end of ATPostAlterTypeCleanup(). The second call would then handle only the objects that the SET EXPRESSION pass remembered. ## Notes - Workaround: run SET EXPRESSION and ALTER COLUMN TYPE as separate ALTER TABLE statements. - The error is raised before anything is committed, so no catalog damage remains. - A pgsql-hackers thread, "bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation", discusses this with patches. ## Branches Blame 5d06e99a3cf is in REL_17_STABLE, REL_18_STABLE, REL_19_STABLE and master, and the double cleanup call is present on each. Reproduced on master only; back-branch reproductions welcome. ## Related Different bugs in the same area: - 01a223bd-3661-7135-8846-bd770589835d (SET EXPRESSION on a child drops inherited CHECKs; same blame) - 01a1e671-119f-7383-bdfd-640b989ae9cb