Skip to content

Commit e97121d

Browse files
committed
Refuse ATTACH of a table referenced by a foreign key
Trying to attach a table as a partition which is already on the referenced side of a foreign key on the partitioned table that it is being attached to, leads to strange behavior: we try to clone the foreign key from the parent to the partition, but this new FK points to the partition itself, and the mix of pg_constraint rows and triggers doesn't behave well. Rather than trying to untangle the mess (which might be possible given sufficient time), I opted to forbid the ATTACH. This doesn't seem a problematic restriction, given that we already fail to create the foreign key if you do it the other way around, that is, having the partition first and the FK second. Backpatch to all supported branches. Reported-by: Alexander Lakhin <exclusion@gmail.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/18541-628a61bc267cd2d3@postgresql.org
1 parent bb5592c commit e97121d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10033,6 +10033,23 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
1003310033
{
1003410034
ForeignKeyCacheInfo *fk = lfirst(cell);
1003510035

10036+
/*
10037+
* Refuse to attach a table as partition that this partitioned table
10038+
* already has a foreign key to. This isn't useful schema, which is
10039+
* proven by the fact that there have been no user complaints that
10040+
* it's already impossible to achieve this in the opposite direction,
10041+
* i.e., creating a foreign key that references a partition. This
10042+
* restriction allows us to dodge some complexities around
10043+
* pg_constraint and pg_trigger row creations that would be needed
10044+
* during ATTACH/DETACH for this kind of relationship.
10045+
*/
10046+
if (fk->confrelid == RelationGetRelid(partRel))
10047+
ereport(ERROR,
10048+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10049+
errmsg("can't attach table \"%s\" as a partition which is referenced by foreign key \"%s\"",
10050+
RelationGetRelationName(partRel),
10051+
get_constraint_name(fk->conoid))));
10052+
1003610053
clone = lappend_oid(clone, fk->conoid);
1003710054
}
1003810055

src/test/regress/expected/foreign_key.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,23 @@ INSERT INTO fk_notpartitioned_pk VALUES (1600, 601), (1600, 1601);
18961896
ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2
18971897
FOR VALUES IN (1600);
18981898
-- leave these tables around intentionally
1899+
-- Verify that attaching a table that's referenced by an existing FK
1900+
-- in the parent throws an error
1901+
CREATE TABLE fk_partitioned_pk_6 (a int PRIMARY KEY);
1902+
CREATE TABLE fk_partitioned_fk_6 (a int REFERENCES fk_partitioned_pk_6) PARTITION BY LIST (a);
1903+
ALTER TABLE fk_partitioned_fk_6 ATTACH PARTITION fk_partitioned_pk_6 FOR VALUES IN (1);
1904+
ERROR: can't attach table "fk_partitioned_pk_6" as a partition which is referenced by foreign key "fk_partitioned_fk_6_a_fkey"
1905+
DROP TABLE fk_partitioned_pk_6, fk_partitioned_fk_6;
1906+
-- This case is similar to above, but the referenced relation is one level
1907+
-- lower in the hierarchy. This one fails in a different way as the above,
1908+
-- because we don't bother to protect against this case explicitly. If the
1909+
-- current error stops happening, we'll need to add a better protection.
1910+
CREATE TABLE fk_partitioned_pk_6 (a int PRIMARY KEY) PARTITION BY list (a);
1911+
CREATE TABLE fk_partitioned_pk_61 PARTITION OF fk_partitioned_pk_6 FOR VALUES IN (1);
1912+
CREATE TABLE fk_partitioned_fk_6 (a int REFERENCES fk_partitioned_pk_61) PARTITION BY LIST (a);
1913+
ALTER TABLE fk_partitioned_fk_6 ATTACH PARTITION fk_partitioned_pk_6 FOR VALUES IN (1);
1914+
ERROR: cannot ALTER TABLE "fk_partitioned_pk_61" because it is being used by active queries in this session
1915+
DROP TABLE fk_partitioned_pk_6, fk_partitioned_fk_6;
18991916
-- test the case when the referenced table is owned by a different user
19001917
create role regress_other_partitioned_fk_owner;
19011918
grant references on fk_notpartitioned_pk to regress_other_partitioned_fk_owner;

src/test/regress/sql/foreign_key.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,23 @@ ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2
13661366

13671367
-- leave these tables around intentionally
13681368

1369+
-- Verify that attaching a table that's referenced by an existing FK
1370+
-- in the parent throws an error
1371+
CREATE TABLE fk_partitioned_pk_6 (a int PRIMARY KEY);
1372+
CREATE TABLE fk_partitioned_fk_6 (a int REFERENCES fk_partitioned_pk_6) PARTITION BY LIST (a);
1373+
ALTER TABLE fk_partitioned_fk_6 ATTACH PARTITION fk_partitioned_pk_6 FOR VALUES IN (1);
1374+
DROP TABLE fk_partitioned_pk_6, fk_partitioned_fk_6;
1375+
1376+
-- This case is similar to above, but the referenced relation is one level
1377+
-- lower in the hierarchy. This one fails in a different way as the above,
1378+
-- because we don't bother to protect against this case explicitly. If the
1379+
-- current error stops happening, we'll need to add a better protection.
1380+
CREATE TABLE fk_partitioned_pk_6 (a int PRIMARY KEY) PARTITION BY list (a);
1381+
CREATE TABLE fk_partitioned_pk_61 PARTITION OF fk_partitioned_pk_6 FOR VALUES IN (1);
1382+
CREATE TABLE fk_partitioned_fk_6 (a int REFERENCES fk_partitioned_pk_61) PARTITION BY LIST (a);
1383+
ALTER TABLE fk_partitioned_fk_6 ATTACH PARTITION fk_partitioned_pk_6 FOR VALUES IN (1);
1384+
DROP TABLE fk_partitioned_pk_6, fk_partitioned_fk_6;
1385+
13691386
-- test the case when the referenced table is owned by a different user
13701387
create role regress_other_partitioned_fk_owner;
13711388
grant references on fk_notpartitioned_pk to regress_other_partitioned_fk_owner;

0 commit comments

Comments
 (0)