pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key
2+ messages / 2 participants
[nested] [flat]
* BUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key
@ 2026-10-22 14:07 PG Bug reporting form <noreply@postgresql.org>
2026-10-29 14:23 ` Re: BUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key Dilip Kumar <dilipbalaut@gmail.com>
0 siblings, 1 reply; 2+ messages in thread
From: PG Bug reporting form @ 2026-10-22 14:07 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: alpkeles99@gmail.com
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
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: BUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key
2026-10-22 14:07 BUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key PG Bug reporting form <noreply@postgresql.org>
@ 2026-10-29 14:23 ` Dilip Kumar <dilipbalaut@gmail.com>
0 siblings, 0 replies; 2+ messages in thread
From: Dilip Kumar @ 2026-10-29 14:23 UTC (permalink / raw)
To: alpkeles99@gmail.com; pgsql-bugs@lists.postgresql.org
On Fri, Oct 23, 2026 at 1:41 PM PG Bug reporting form
<noreply@postgresql.org> wrote:
>
> 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.
>
Thanks for reporting, yes your analysis looks correct to me. And
IMHO, To fix, whenever an index is remembered for rebuilding,either
directly or as the index of a constraint being rebuilt, also remember
any foreign key constraints that depend on it. They are then dropped
and re-added together with the index, the same way as when a key
column is altered.
I have attached a patch to fix it. If we think this is the right
approach, I will also add a test case.
--
Regards,
Dilip Kumar
Google
Attachments:
v1-0001-Fix-ALTER-COLUMN-TYPE-on-INCLUDE-column-of-index-.patch (application/octet-stream, 4.3K) download
From 4631ce7d3fb0d14710e85c79c88223116701c843 Mon Sep 17 00:00:00 2001
From: Dilip Kumar <dilipkumarb@google.com>
Date: Thu, 29 Oct 2026 12:15:53 +0000
Subject: [PATCH v1] Fix ALTER COLUMN TYPE on INCLUDE column of index used by a
foreign key
ALTER COLUMN TYPE collects the indexes and constraints that depend
directly on the altered column, drops them in ATPostAlterTypeCleanup()
using DROP_RESTRICT, and recreates them afterwards.
A foreign key constraint depends both on its referenced columns and
on the unique index supporting them, so altering a key column of that
index rebuilds the foreign key along with the index. However, a
foreign key does not depend on the INCLUDE columns of its supporting
index. Altering such a column therefore queued the index for
rebuilding, but not the foreign key, and the drop failed:
To fix, whenever an index is remembered for rebuilding,
either directly or as the index of a constraint being rebuilt, also
remember any foreign key constraints that depend on it. They are then
dropped and re-added together with the index, the same way as when a key
column is altered.
---
src/backend/commands/tablecmds.c | 50 ++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 93ad49e59fd..e07daff73fe 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -667,6 +667,7 @@ static void RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableT
static void RememberWholeRowDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, Relation rel);
static void RememberConstraintForRebuilding(Oid conoid, AlteredTableInfo *tab);
static void RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab);
+static void RememberFKsOnIndexForRebuilding(Oid indoid, AlteredTableInfo *tab);
static void RememberStatisticsForRebuilding(Oid stxoid, AlteredTableInfo *tab);
static void ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab,
LOCKMODE lockmode);
@@ -16002,6 +16003,7 @@ RememberConstraintForRebuilding(Oid conoid, AlteredTableInfo *tab)
{
RememberReplicaIdentityForRebuilding(indoid, tab);
RememberClusterOnForRebuilding(indoid, tab);
+ RememberFKsOnIndexForRebuilding(indoid, tab);
}
}
}
@@ -16053,10 +16055,58 @@ RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab)
*/
RememberReplicaIdentityForRebuilding(indoid, tab);
RememberClusterOnForRebuilding(indoid, tab);
+ RememberFKsOnIndexForRebuilding(indoid, tab);
}
}
}
+/*
+ * Subroutine for ATExecAlterColumnType: remember any foreign key constraints
+ * that depend on the given index, which we are about to rebuild.
+ *
+ * A foreign key constraint has a dependency on the unique index supporting
+ * its referenced columns. Usually such a constraint gets remembered anyway,
+ * because it also depends on the referenced columns themselves. But if the
+ * column being altered is only an INCLUDE column of that index, the foreign
+ * key does not depend on it, and dropping the index in
+ * ATPostAlterTypeCleanup() would fail. So we must rebuild the foreign key
+ * along with the index.
+ */
+static void
+RememberFKsOnIndexForRebuilding(Oid indoid, AlteredTableInfo *tab)
+{
+ Relation depRel;
+ ScanKeyData key[2];
+ SysScanDesc scan;
+ HeapTuple depTup;
+
+ depRel = table_open(DependRelationId, AccessShareLock);
+
+ ScanKeyInit(&key[0],
+ Anum_pg_depend_refclassid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(RelationRelationId));
+ ScanKeyInit(&key[1],
+ Anum_pg_depend_refobjid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(indoid));
+
+ scan = systable_beginscan(depRel, DependReferenceIndexId, true,
+ NULL, 2, key);
+
+ while (HeapTupleIsValid(depTup = systable_getnext(scan)))
+ {
+ Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(depTup);
+
+ if (foundDep->classid == ConstraintRelationId &&
+ get_constraint_type(foundDep->objid) == CONSTRAINT_FOREIGN)
+ RememberConstraintForRebuilding(foundDep->objid, tab);
+ }
+
+ systable_endscan(scan);
+ table_close(depRel, AccessShareLock);
+}
+
/*
* Subroutine for ATExecAlterColumnType: remember that a statistics object
* needs to be rebuilt (which we might already know).
--
2.56.0.385.gac671c819a-goog
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2026-10-29 14:23 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-22 14:07 BUG #19791: ALTER COLUMN TYPE fails on an INCLUDE column of an index used by a foreign key PG Bug reporting form <noreply@postgresql.org>
2026-10-29 14:23 ` Dilip Kumar <dilipbalaut@gmail.com>
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