pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: alpkeles99@gmail.com
Subject: BUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key
Date: Thu, 22 Oct 2026 14:07:10 +0000
Message-ID: <19791-ebe77f384f92b31b@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19791
Logged by:          Alperen Keles
Email address:      alpkeles99@gmail.com
PostgreSQL version: 18.6
Operating system:   macOS 26.7 arm64
Description:        

Hello,

Changing the type of a non-key (INCLUDE) column of a unique index
fails when a foreign key references the key columns of that index:

  CREATE TABLE p (a int4, b int4);
  CREATE UNIQUE INDEX p_u ON p (a) INCLUDE (b);
  CREATE TABLE c (a int4 REFERENCES p (a));
  ALTER TABLE p ALTER COLUMN b TYPE int8;
  ERROR:  cannot drop index p_u because other objects depend on it
  DETAIL:  constraint c_a_fkey on table c depends on index p_u
  HINT:  Use DROP ... CASCADE to drop the dependent objects too.

The same happens with UNIQUE (a) INCLUDE (b) and PRIMARY KEY (a)
INCLUDE (b) constraints ("cannot drop constraint ..."), with a
partitioned referenced table, with a self-referencing foreign key,
with ALTER COLUMN b SET EXPRESSION on a stored generated column, and
with changes that need no rewrite, such as varchar(5) -> varchar(10)
or only a new COLLATE on b.

It works without the foreign key (the index is rebuilt), when the
key column a is altered instead (index and foreign key are rebuilt),
and when both are altered in one statement:

  ALTER TABLE p ALTER COLUMN a TYPE int8, ALTER COLUMN b TYPE int8;
  ALTER TABLE

The ALTER TABLE documentation says indexes and simple table
constraints involving the column are converted automatically.

As far as I can tell, RememberAllDependentForRebuilding() collects
only the direct pg_depend dependents of the altered column. The
index depends on a and b, but the foreign key depends only on p.a
and on the index, so it is not collected when b is altered.
ATPostAlterTypeCleanup() then drops the index with DROP_RESTRICT
("nothing else should be depending on these objects"), and the
foreign key's dependency on the index makes that fail. Before
INCLUDE, a foreign key could only depend on an index whose columns
were all referenced by it.

Reproduced on 18.6 (REL_18_STABLE a85b5523e67), master 158b6d5109f
and 11.22, macOS arm64.

Found with a query fuzzer.

Regards,
Alperen Keles








view thread (2+ messages)  latest in thread

Message-ID: <19791-ebe77f384f92b31b@postgresql.org>
Permalink:  ../19791-ebe77f384f92b31b@postgresql.org/
Also on:    postgresql.org/message-id/19791-ebe77f384f92b31b@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, alpkeles99@gmail.com
  Subject: Re: BUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key
  In-Reply-To: <19791-ebe77f384f92b31b@postgresql.org>
  References: <19791-ebe77f384f92b31b@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox