SET CONSTRAINTS ... DEFERRED failed on partitioned tables, because of a
sanity check that ensures that the affected constraints have triggers.
On partitioned tables, the triggers are in the leaf partitions, not in
the partitioned relations themselves, so the sanity check fails.
Removing the sanity check solves the problem, because the code needed to
support the case is already there.
Backpatch to 11.
Note: deferred unique constraints are not affected by this bug, because
they do have triggers in the parent partitioned table. I did not add a
test for this scenario.
Discussion: https://postgr.es/m/
20191105212915[email protected]
foreach(lc, conoidlist)
{
Oid conoid = lfirst_oid(lc);
- bool found;
ScanKeyData skey;
SysScanDesc tgscan;
HeapTuple htup;
- found = false;
-
ScanKeyInit(&skey,
Anum_pg_trigger_tgconstraint,
BTEqualStrategyNumber, F_OIDEQ,
if (pg_trigger->tgdeferrable)
tgoidlist = lappend_oid(tgoidlist,
HeapTupleGetOid(htup));
-
- found = true;
}
systable_endscan(tgscan);
-
- /* Safety check: a deferrable constraint should have triggers */
- if (!found)
- elog(ERROR, "no triggers found for constraint with OID %u",
- conoid);
}
heap_close(tgrel, AccessShareLock);
alter table fkpart2.fk_part_1 drop constraint fkey; -- ok
alter table fkpart2.fk_part_1_1 drop constraint my_fkey; -- doesn't exist
ERROR: constraint "my_fkey" of relation "fk_part_1_1" does not exist
+-- verify constraint deferrability
+create schema fkpart3
+ create table pkey (a int primary key)
+ create table fk_part (a int, constraint fkey foreign key (a) references fkpart3.pkey deferrable initially immediate) partition by list (a)
+ create table fk_part_1 partition of fkpart3.fk_part for values in (1) partition by list (a)
+ create table fk_part_1_1 partition of fkpart3.fk_part_1 for values in (1)
+ create table fk_part_2 partition of fkpart3.fk_part for values in (2);
+begin;
+set constraints fkpart3.fkey deferred;
+insert into fkpart3.fk_part values (1);
+insert into fkpart3.pkey values (1);
+commit;
+begin;
+set constraints fkpart3.fkey deferred;
+delete from fkpart3.pkey;
+delete from fkpart3.fk_part;
+commit;
-- ensure we check partitions are "not used" when dropping constraints
CREATE SCHEMA fkpart8
CREATE TABLE tbl1(f1 int PRIMARY KEY)
ERROR: cannot ALTER TABLE "tbl2_p1" because it has pending trigger events
COMMIT;
\set VERBOSITY terse \\ -- suppress cascade details
-drop schema fkpart0, fkpart1, fkpart2, fkpart8 cascade;
-NOTICE: drop cascades to 10 other objects
+drop schema fkpart0, fkpart1, fkpart2, fkpart3, fkpart8 cascade;
+NOTICE: drop cascades to 12 other objects
\set VERBOSITY default
alter table fkpart2.fk_part_1 drop constraint fkey; -- ok
alter table fkpart2.fk_part_1_1 drop constraint my_fkey; -- doesn't exist
+-- verify constraint deferrability
+create schema fkpart3
+ create table pkey (a int primary key)
+ create table fk_part (a int, constraint fkey foreign key (a) references fkpart3.pkey deferrable initially immediate) partition by list (a)
+ create table fk_part_1 partition of fkpart3.fk_part for values in (1) partition by list (a)
+ create table fk_part_1_1 partition of fkpart3.fk_part_1 for values in (1)
+ create table fk_part_2 partition of fkpart3.fk_part for values in (2);
+begin;
+set constraints fkpart3.fkey deferred;
+insert into fkpart3.fk_part values (1);
+insert into fkpart3.pkey values (1);
+commit;
+begin;
+set constraints fkpart3.fkey deferred;
+delete from fkpart3.pkey;
+delete from fkpart3.fk_part;
+commit;
+
-- ensure we check partitions are "not used" when dropping constraints
CREATE SCHEMA fkpart8
CREATE TABLE tbl1(f1 int PRIMARY KEY)
COMMIT;
\set VERBOSITY terse \\ -- suppress cascade details
-drop schema fkpart0, fkpart1, fkpart2, fkpart8 cascade;
+drop schema fkpart0, fkpart1, fkpart2, fkpart3, fkpart8 cascade;
\set VERBOSITY default