Skip to content

Commit 20bef2c

Browse files
committed
Fix ALTER/TYPE on columns referenced by FKs in partitioned tables
When ALTER TABLE ... SET DATA TYPE affects a column referenced by constraints and indexes, it drop those constraints and indexes and recreates them afterwards, so that the definitions match the new data type. The original code did this by dropping one object at a time (commit 077db40 of May 2004), which worked fine because the dependencies between the objects were pretty straightforward, and ordering the objects in a specific way was enough to make this work. However, when there are foreign key constraints in partitioned tables, the dependencies are no longer so straightforward, and we were getting errors when attempted: ERROR: cache lookup failed for constraint 16398 This can be fixed by doing all the drops in one pass instead, using performMultipleDeletions (introduced by df18c51 of Aug 2006). With this change we can also remove the code to carefully order the list of objects to be deleted. Reported-by: Rajkumar Raghuwanshi <rajkumar.raghuwanshi@enterprisedb.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAKcux6nWS_m+s=1Udk_U9B+QY7pA-Ac58qR5BdUfOyrwnWHDew@mail.gmail.com
1 parent 728202b commit 20bef2c

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

src/backend/commands/tablecmds.c

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9527,33 +9527,12 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
95279527
{
95289528
char *defstring = pg_get_constraintdef_command(foundObject.objectId);
95299529

9530-
/*
9531-
* Put NORMAL dependencies at the front of the list and
9532-
* AUTO dependencies at the back. This makes sure that
9533-
* foreign-key constraints depending on this column will
9534-
* be dropped before unique or primary-key constraints of
9535-
* the column; which we must have because the FK
9536-
* constraints depend on the indexes belonging to the
9537-
* unique constraints.
9538-
*/
9539-
if (foundDep->deptype == DEPENDENCY_NORMAL)
9540-
{
9541-
tab->changedConstraintOids =
9542-
lcons_oid(foundObject.objectId,
9543-
tab->changedConstraintOids);
9544-
tab->changedConstraintDefs =
9545-
lcons(defstring,
9546-
tab->changedConstraintDefs);
9547-
}
9548-
else
9549-
{
9550-
tab->changedConstraintOids =
9551-
lappend_oid(tab->changedConstraintOids,
9552-
foundObject.objectId);
9553-
tab->changedConstraintDefs =
9554-
lappend(tab->changedConstraintDefs,
9555-
defstring);
9556-
}
9530+
tab->changedConstraintOids =
9531+
lappend_oid(tab->changedConstraintOids,
9532+
foundObject.objectId);
9533+
tab->changedConstraintDefs =
9534+
lappend(tab->changedConstraintDefs,
9535+
defstring);
95579536
}
95589537
break;
95599538

@@ -9893,9 +9872,17 @@ static void
98939872
ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
98949873
{
98959874
ObjectAddress obj;
9875+
ObjectAddresses *objects;
98969876
ListCell *def_item;
98979877
ListCell *oid_item;
98989878

9879+
/*
9880+
* Collect all the constraints and indexes to drop so we can process them
9881+
* in a single call. That way we don't have to worry about dependencies
9882+
* among them.
9883+
*/
9884+
objects = new_object_addresses();
9885+
98999886
/*
99009887
* Re-parse the index and constraint definitions, and attach them to the
99019888
* appropriate work queue entries. We do this before dropping because in
@@ -9937,6 +9924,9 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
99379924
conislocal = con->conislocal;
99389925
ReleaseSysCache(tup);
99399926

9927+
ObjectAddressSet(obj, ConstraintRelationId, lfirst_oid(oid_item));
9928+
add_exact_object_address(&obj, objects);
9929+
99409930
/*
99419931
* If the constraint is inherited (only), we don't want to inject a
99429932
* new definition here; it'll get recreated when ATAddCheckConstraint
@@ -9960,31 +9950,18 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
99609950
ATPostAlterTypeParse(oldId, relid, InvalidOid,
99619951
(char *) lfirst(def_item),
99629952
wqueue, lockmode, tab->rewrite);
9953+
9954+
ObjectAddressSet(obj, RelationRelationId, lfirst_oid(oid_item));
9955+
add_exact_object_address(&obj, objects);
99639956
}
99649957

99659958
/*
9966-
* Now we can drop the existing constraints and indexes --- constraints
9967-
* first, since some of them might depend on the indexes. In fact, we
9968-
* have to delete FOREIGN KEY constraints before UNIQUE constraints, but
9969-
* we already ordered the constraint list to ensure that would happen. It
9970-
* should be okay to use DROP_RESTRICT here, since nothing else should be
9971-
* depending on these objects.
9959+
* It should be okay to use DROP_RESTRICT here, since nothing else should
9960+
* be depending on these objects.
99729961
*/
9973-
foreach(oid_item, tab->changedConstraintOids)
9974-
{
9975-
obj.classId = ConstraintRelationId;
9976-
obj.objectId = lfirst_oid(oid_item);
9977-
obj.objectSubId = 0;
9978-
performDeletion(&obj, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
9979-
}
9962+
performMultipleDeletions(objects, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
99809963

9981-
foreach(oid_item, tab->changedIndexOids)
9982-
{
9983-
obj.classId = RelationRelationId;
9984-
obj.objectId = lfirst_oid(oid_item);
9985-
obj.objectSubId = 0;
9986-
performDeletion(&obj, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
9987-
}
9964+
free_object_addresses(objects);
99889965

99899966
/*
99909967
* The objects will get recreated during subsequent passes over the work

src/test/regress/expected/foreign_key.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,18 @@ DETAIL: Key (a, b)=(2500, 2502) is still referenced from table "fk_partitioned_
15181518
ALTER TABLE fk_partitioned_fk DROP CONSTRAINT fk_partitioned_fk_a_fkey;
15191519
-- done.
15201520
DROP TABLE fk_notpartitioned_pk, fk_partitioned_fk;
1521+
-- Altering a type referenced by a foreign key needs to drop/recreate the FK.
1522+
-- Ensure that works.
1523+
CREATE TABLE fk_notpartitioned_pk (a INT, PRIMARY KEY(a), CHECK (a > 0));
1524+
CREATE TABLE fk_partitioned_fk (a INT REFERENCES fk_notpartitioned_pk(a) PRIMARY KEY) PARTITION BY RANGE(a);
1525+
CREATE TABLE fk_partitioned_fk_1 PARTITION OF fk_partitioned_fk FOR VALUES FROM (MINVALUE) TO (MAXVALUE);
1526+
INSERT INTO fk_notpartitioned_pk VALUES (1);
1527+
INSERT INTO fk_partitioned_fk VALUES (1);
1528+
ALTER TABLE fk_notpartitioned_pk ALTER COLUMN a TYPE bigint;
1529+
DELETE FROM fk_notpartitioned_pk WHERE a = 1;
1530+
ERROR: update or delete on table "fk_notpartitioned_pk" violates foreign key constraint "fk_partitioned_fk_a_fkey" on table "fk_partitioned_fk"
1531+
DETAIL: Key (a)=(1) is still referenced from table "fk_partitioned_fk".
1532+
DROP TABLE fk_notpartitioned_pk, fk_partitioned_fk;
15211533
-- Test some other exotic foreign key features: MATCH SIMPLE, ON UPDATE/DELETE
15221534
-- actions
15231535
CREATE TABLE fk_notpartitioned_pk (a int, b int, primary key (a, b));

src/test/regress/sql/foreign_key.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,17 @@ ALTER TABLE fk_partitioned_fk DROP CONSTRAINT fk_partitioned_fk_a_fkey;
11391139
-- done.
11401140
DROP TABLE fk_notpartitioned_pk, fk_partitioned_fk;
11411141

1142+
-- Altering a type referenced by a foreign key needs to drop/recreate the FK.
1143+
-- Ensure that works.
1144+
CREATE TABLE fk_notpartitioned_pk (a INT, PRIMARY KEY(a), CHECK (a > 0));
1145+
CREATE TABLE fk_partitioned_fk (a INT REFERENCES fk_notpartitioned_pk(a) PRIMARY KEY) PARTITION BY RANGE(a);
1146+
CREATE TABLE fk_partitioned_fk_1 PARTITION OF fk_partitioned_fk FOR VALUES FROM (MINVALUE) TO (MAXVALUE);
1147+
INSERT INTO fk_notpartitioned_pk VALUES (1);
1148+
INSERT INTO fk_partitioned_fk VALUES (1);
1149+
ALTER TABLE fk_notpartitioned_pk ALTER COLUMN a TYPE bigint;
1150+
DELETE FROM fk_notpartitioned_pk WHERE a = 1;
1151+
DROP TABLE fk_notpartitioned_pk, fk_partitioned_fk;
1152+
11421153
-- Test some other exotic foreign key features: MATCH SIMPLE, ON UPDATE/DELETE
11431154
-- actions
11441155
CREATE TABLE fk_notpartitioned_pk (a int, b int, primary key (a, b));

0 commit comments

Comments
 (0)