mirror of https://github.com/postgres/postgres
Fix failure to create FKs correctly in partitions
On a multi-level partioned table, when adding a partition not directly
connected to the root table, foreign key constraints referencing the
root were not cloned to the new partition, leading to the FK being
possibly inadvertently violated later on.
This was caused by fuzzy thinking in CloneFkReferenced (commit
f56f8f8da6
): it was skipping constraints marked as having parents on
the theory that cloning those would create duplicates; but that's only
correct for the top level of the partitioning hierarchy. For levels
below that one, such constraints must still be considered and only
skipped if later on we see that we'd create duplicates. Apparently, I
(Álvaro) wrote the comments right but the code implemented something
slightly different.
Author: Jehan-Guillaume de Rorthais
Discussion: https://postgr.es/m/20200206004948.238352db@firost
This commit is contained in:
parent
9710d3d4a8
commit
55173d2e66
|
@ -8962,13 +8962,13 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
|
||||||
List *clone = NIL;
|
List *clone = NIL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Search for any constraints where this partition is in the referenced
|
* Search for any constraints where this partition's parent is in the
|
||||||
* side. However, we must ignore any constraint whose parent constraint
|
* referenced side. However, we must not clone any constraint whose
|
||||||
* is also going to be cloned, to avoid duplicates. So do it in two
|
* parent constraint is also going to be cloned, to avoid duplicates. So
|
||||||
* steps: first construct the list of constraints to clone, then go over
|
* do it in two steps: first construct the list of constraints to clone,
|
||||||
* that list cloning those whose parents are not in the list. (We must
|
* then go over that list cloning those whose parents are not in the list.
|
||||||
* not rely on the parent being seen first, since the catalog scan could
|
* (We must not rely on the parent being seen first, since the catalog
|
||||||
* return children first.)
|
* scan could return children first.)
|
||||||
*/
|
*/
|
||||||
pg_constraint = table_open(ConstraintRelationId, RowShareLock);
|
pg_constraint = table_open(ConstraintRelationId, RowShareLock);
|
||||||
ScanKeyInit(&key[0],
|
ScanKeyInit(&key[0],
|
||||||
|
@ -8984,10 +8984,6 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
|
||||||
{
|
{
|
||||||
Form_pg_constraint constrForm = (Form_pg_constraint) GETSTRUCT(tuple);
|
Form_pg_constraint constrForm = (Form_pg_constraint) GETSTRUCT(tuple);
|
||||||
|
|
||||||
/* Only try to clone the top-level constraint; skip child ones. */
|
|
||||||
if (constrForm->conparentid != InvalidOid)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
clone = lappend_oid(clone, constrForm->oid);
|
clone = lappend_oid(clone, constrForm->oid);
|
||||||
}
|
}
|
||||||
systable_endscan(scan);
|
systable_endscan(scan);
|
||||||
|
@ -9016,6 +9012,16 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
|
||||||
elog(ERROR, "cache lookup failed for constraint %u", constrOid);
|
elog(ERROR, "cache lookup failed for constraint %u", constrOid);
|
||||||
constrForm = (Form_pg_constraint) GETSTRUCT(tuple);
|
constrForm = (Form_pg_constraint) GETSTRUCT(tuple);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* As explained above: don't try to clone a constraint for which we're
|
||||||
|
* going to clone the parent.
|
||||||
|
*/
|
||||||
|
if (list_member_oid(clone, constrForm->conparentid))
|
||||||
|
{
|
||||||
|
ReleaseSysCache(tuple);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Because we're only expanding the key space at the referenced side,
|
* Because we're only expanding the key space at the referenced side,
|
||||||
* we don't need to prevent any operation in the referencing table, so
|
* we don't need to prevent any operation in the referencing table, so
|
||||||
|
|
|
@ -2444,3 +2444,27 @@ DROP SCHEMA fkpart8 CASCADE;
|
||||||
NOTICE: drop cascades to 2 other objects
|
NOTICE: drop cascades to 2 other objects
|
||||||
DETAIL: drop cascades to table fkpart8.tbl1
|
DETAIL: drop cascades to table fkpart8.tbl1
|
||||||
drop cascades to table fkpart8.tbl2
|
drop cascades to table fkpart8.tbl2
|
||||||
|
-- ensure FK referencing a multi-level partitioned table are
|
||||||
|
-- enforce reference to sub-children.
|
||||||
|
CREATE SCHEMA fkpart9
|
||||||
|
CREATE TABLE pk (a INT PRIMARY KEY) PARTITION BY RANGE (a)
|
||||||
|
CREATE TABLE fk (
|
||||||
|
fk_a INT REFERENCES pk(a) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
CREATE TABLE pk1 PARTITION OF pk FOR VALUES FROM (30) TO (50) PARTITION BY RANGE (a)
|
||||||
|
CREATE TABLE pk11 PARTITION OF pk1 FOR VALUES FROM (30) TO (40);
|
||||||
|
INSERT INTO fkpart9.pk VALUES (35);
|
||||||
|
INSERT INTO fkpart9.fk VALUES (35);
|
||||||
|
DELETE FROM fkpart9.pk WHERE a=35;
|
||||||
|
SELECT fk.fk_a, pk.a
|
||||||
|
FROM fkpart9.fk
|
||||||
|
LEFT JOIN fkpart9.pk ON fk.fk_a = pk.a
|
||||||
|
WHERE fk.fk_a=35;
|
||||||
|
fk_a | a
|
||||||
|
------+---
|
||||||
|
(0 rows)
|
||||||
|
|
||||||
|
DROP SCHEMA fkpart9 CASCADE;
|
||||||
|
NOTICE: drop cascades to 2 other objects
|
||||||
|
DETAIL: drop cascades to table fkpart9.pk
|
||||||
|
drop cascades to table fkpart9.fk
|
||||||
|
|
|
@ -1722,3 +1722,21 @@ INSERT INTO fkpart8.tbl2 VALUES(1);
|
||||||
ALTER TABLE fkpart8.tbl2 DROP CONSTRAINT tbl2_f1_fkey;
|
ALTER TABLE fkpart8.tbl2 DROP CONSTRAINT tbl2_f1_fkey;
|
||||||
COMMIT;
|
COMMIT;
|
||||||
DROP SCHEMA fkpart8 CASCADE;
|
DROP SCHEMA fkpart8 CASCADE;
|
||||||
|
|
||||||
|
-- ensure FK referencing a multi-level partitioned table are
|
||||||
|
-- enforce reference to sub-children.
|
||||||
|
CREATE SCHEMA fkpart9
|
||||||
|
CREATE TABLE pk (a INT PRIMARY KEY) PARTITION BY RANGE (a)
|
||||||
|
CREATE TABLE fk (
|
||||||
|
fk_a INT REFERENCES pk(a) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
CREATE TABLE pk1 PARTITION OF pk FOR VALUES FROM (30) TO (50) PARTITION BY RANGE (a)
|
||||||
|
CREATE TABLE pk11 PARTITION OF pk1 FOR VALUES FROM (30) TO (40);
|
||||||
|
INSERT INTO fkpart9.pk VALUES (35);
|
||||||
|
INSERT INTO fkpart9.fk VALUES (35);
|
||||||
|
DELETE FROM fkpart9.pk WHERE a=35;
|
||||||
|
SELECT fk.fk_a, pk.a
|
||||||
|
FROM fkpart9.fk
|
||||||
|
LEFT JOIN fkpart9.pk ON fk.fk_a = pk.a
|
||||||
|
WHERE fk.fk_a=35;
|
||||||
|
DROP SCHEMA fkpart9 CASCADE;
|
||||||
|
|
Loading…
Reference in New Issue