pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedIMMEDIATE constraint enforcement does not comply with the SQL standard
6+ messages / 4 participants
[nested] [flat]
* IMMEDIATE constraint enforcement does not comply with the SQL standard
@ 2018-10-08 21:35 Finnerty, Jim <jfinnert@amazon.com>
2018-10-08 21:49 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 6+ messages in thread
From: Finnerty, Jim @ 2018-10-08 21:35 UTC (permalink / raw)
To: pgsql-bugs
Problem Description:
The current implementation of immediate-mode foreign key constraints in PostgreSQL does not comply with the SQL standard. According to the standard, an immediate-mode foreign key constraint must be enforced immediately upon statement completion. This implies that an immediate-mode constraint must be enforced before any ‘after statement’ triggers are fired.
Foreign Key Constraints in PostgreSQL are implemented as triggers. When a user creates a foreign key constraint, internally the system creates 4 triggers:
· An after trigger on update of an entry in the foreign key table to check that the constraint is met by the update.
· An after trigger on insert into the foreign key table to check that the constraint is met by the insert.
· An after trigger on delete of an entry in the primary key table to check that the constraint is not broken by the delete.
· An after trigger on update of an entry in the primary key table to check that the constraint is not broken by the update.
· Each of these triggers is given a name that starts with "RI_ConstraintTrigger".
PostgreSQL executes AFTER triggers (and constraints) in alphabetical order according to the name assigned to the trigger at creation time; therefore, PostgreSQL permits IMMEDIATE foreign key constraints to be deferred until after AFTER triggers are fired if their name sorts lower than “RI_ConstraintTrigger”. This is a violation of the standard.
If an AFTER trigger or user-mode constraint has a name (such as “A_trig”) that causes it to fire before an IMMEDIATE foreign key constraint, then arbitrary code can be executed while the constraint is violated, including code that causes the foreign key condition to be satisfied again. In that case the statement should fail according to the standard but it will succeed in PostgreSQL. A user that wants to have code fire before the constraint is enforced can (and should) do so in a standard-compliant way by making the constraint DEFERRABLE INITIALLY DEFERRED.
We believe that the scenario of having a trigger intentionally fixing up an immediate-mode foreign key constraint probably doesn’t happen very often. This is fortunate, because it makes it possible to conform with the SQL standard without affecting a large number of applications. What is desired is a solution that preserves the name ordering of triggers, except for the IMMEDIATE constraints, which must fire first. User-defined IMMEDIATE constraints are not covered by the standard, but we believe that the interpretation most consistent with both the standard and existing PostgreSQL behavior is:
· Fire the IMMEDIATE system constraint triggers in ascending name order,
· Then fire the IMMEDIATE user-defined constraint triggers in ascending name order,
· Then fire all other triggers in ascending name order
A patch implementing this proposed fix is attached (see after_trigger.diff).
Example:
<see fk_bug.sql, attached>
postgres=# \i ~/fk_bug.sql
CREATE TABLE
CREATE TABLE
psql:/home/jfinnert/fk_bug.sql:7: ERROR: insert or update on table "t2" violates foreign key constraint "t2_t1id_fkey"
DETAIL: Key (t1id)=(1) is not present in table "t1".
CREATE FUNCTION
CREATE TRIGGER
INSERT 0 1
id | data
----+----------------------------------------------------------
1 | the presence of this row in t1 violates the SQL standard
(1 row)
id | t1id | data
----+------+----------------------------------------------------------
1 | 1 | the presence of this row in t2 violates the SQL standard
(1 row)
Platform Information:
postgres=# select version();
version
--------------------------------------------------------------------------------
PostgreSQL 10beta3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.9.3, 64-bit
(1 row)
Thank you,
Jim Finnerty, Principal Engineer, Amazon Aurora PostgreSQL
Attachments:
fk_bug.sql (application/octet-stream, 1.1K) download
after_trigger.diff (application/octet-stream, 22.2K) download
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index a599205..d41fa32 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -100,6 +105,10 @@
List *recheckIndexes, Bitmapset *modifiedCols,
TransitionCaptureState *transition_capture);
static void AfterTriggerEnlargeQueryState(void);
+static Trigger *PrioritizeConstraintTriggers(Trigger* triggers,
+ int numtrigs,
+ int numInternalConstraints,
+ int numCustomConstraints);
/*
@@ -1642,6 +1647,59 @@
CacheInvalidateRelcache(rel);
}
+/*
+ * Given an array of triggers sorted by name, returns a new array of triggers
+ * that prioritizes first internal constraint triggers, then user constraint
+ * triggers.
+ *
+ * Note: Constraints will just take precedence over normal triggers, and
+ * internal constraints will take the highest precedence. Each partition
+ * of the array will preserve the existing order of triggers as passed in.
+ * This will return a new array that is a modified version of the array
+ * passed in.
+ */
+static Trigger *PrioritizeConstraintTriggers(Trigger* triggers,
+ int numtrigs,
+ int numInternalConstraints,
+ int numCustomConstraints)
+{
+ Trigger *result;
+ int i;
+ int internalConstStart = 0;
+ int customConstStart = numInternalConstraints;
+ int normalTriggerStart = numInternalConstraints + numCustomConstraints;
+ int internalConstOffset = 0;
+ int customConstOffset = 0;
+ int normalTriggerOffset = 0;
+
+ result = palloc(numtrigs * sizeof(Trigger));
+
+ for (i = 0; i < numtrigs; i++)
+ {
+ int sortIndex;
+
+ if (triggers[i].tgconstraint != 0)
+ {
+ if (triggers[i].tgisinternal)
+ {
+ sortIndex = internalConstStart + internalConstOffset;
+ internalConstOffset++;
+ }
+ else
+ {
+ sortIndex = customConstStart + customConstOffset;
+ customConstOffset++;
+ }
+ }
+ else
+ {
+ sortIndex = normalTriggerStart + normalTriggerOffset;
+ normalTriggerOffset++;
+ }
+ result[sortIndex] = triggers[i];
+ }
+ return result;
+}
/*
* Build trigger data to attach to the given relcache entry.
@@ -1658,8 +1665,11 @@
{
TriggerDesc *trigdesc;
int numtrigs;
+ int numInternalConstraints;
+ int numCustomConstraints;
int maxtrigs;
Trigger *triggers;
+ Trigger *prioritized_trigs;
Relation tgrel;
ScanKeyData skey;
SysScanDesc tgscan;
@@ -1674,6 +1679,8 @@
maxtrigs = 16;
triggers = (Trigger *) palloc(maxtrigs * sizeof(Trigger));
numtrigs = 0;
+ numInternalConstraints = 0;
+ numCustomConstraints = 0;
/*
* Note: since we scan the triggers using TriggerRelidNameIndexId, we will
@@ -1703,6 +1708,14 @@
triggers = (Trigger *) repalloc(triggers, maxtrigs * sizeof(Trigger));
}
build = &(triggers[numtrigs]);
+
+ if (pg_trigger->tgconstraint != 0)
+ {
+ if (pg_trigger->tgisinternal)
+ numInternalConstraints++;
+ else
+ numCustomConstraints++;
+ }
build->tgoid = HeapTupleGetOid(htup);
build->tgname = DatumGetCString(DirectFunctionCall1(nameout,
@@ -1785,12 +1796,16 @@
return;
}
+ prioritized_trigs = PrioritizeConstraintTriggers(triggers, numtrigs,
+ numInternalConstraints, numCustomConstraints);
+ pfree(triggers);
+
/* Build trigdesc */
trigdesc = (TriggerDesc *) palloc0(sizeof(TriggerDesc));
- trigdesc->triggers = triggers;
+ trigdesc->triggers = prioritized_trigs;
trigdesc->numtriggers = numtrigs;
for (i = 0; i < numtrigs; i++)
- SetTriggerFlags(trigdesc, &(triggers[i]));
+ SetTriggerFlags(trigdesc, &(prioritized_trigs[i]));
/* Copy completed trigdesc into cache storage */
oldContext = MemoryContextSwitchTo(CacheMemoryContext);
diff --git a/src/test/regress/expected/man-5377.out b/src/test/regress/expected/man-5377.out
new file mode 100644
index 0000000..d2e7a81
--- /dev/null
+++ b/src/test/regress/expected/man-5377.out
@@ -0,0 +0,244 @@
+create schema ts;
+create table ts.t1 (id int primary key);
+create table ts.t2 (id int primary key);
+create table ts.t3 (id int primary key, t1_id int, t2_id int,
+ foreign key (t1_id) references ts.t1(id),
+ foreign key (t2_id) references ts.t2(id));
+insert into ts.t1 select generate_series(1,10) as id;
+insert into ts.t2 values(0);
+insert into ts.t3 select generate_series(11,20) as id,
+ generate_series(1,10) as t1_id;
+create or replace function "A_trig" () returns trigger
+as $$
+begin
+ raise notice 'A_trig fired!';
+ return null;
+end;
+$$ language plpgsql;
+create or replace function "D_trig" () returns trigger
+as $$
+begin
+ raise notice 'D_trig fired!';
+ return null;
+end;
+$$ language plpgsql;
+create or replace function "B_constr" () returns trigger
+as $$
+begin
+ raise notice 'B_constr fired!';
+ return null;
+end;
+$$ language plpgsql;
+create or replace function "C_constr" () returns trigger
+as $$
+begin
+ raise notice 'C_constr fired!';
+ return null;
+end;
+$$ language plpgsql;
+-- Test 001: TestWhenUserTriggerDefinedThenImmediateInternalConstrFiresFirst
+begin;
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- A_trig should not fire because Fkey constraint will fire first
+-- and error out
+insert into ts.t3 values(21, 11, 0);
+ERROR: insert or update on table "t3" violates foreign key constraint "t3_t1_id_fkey"
+DETAIL: Key (t1_id)=(11) is not present in table "t1".
+rollback;
+-- Test 002: TestWhenUserTriggerDefinedThenImmediateCustomConstrFiresFirst
+begin;
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+create constraint trigger "B_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "B_constr"();
+-- expect B_constr->A_trig
+insert into ts.t1 values(11);
+NOTICE: B_constr fired!
+NOTICE: A_trig fired!
+rollback;
+-- Test 003: TestWhenCustomConstrDefinedThenImmediateInternalCosntrFiresFirst
+begin;
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+-- B_constr should not fire because Fkey constraint will fire first
+-- and error out
+insert into ts.t3 values(21, 11, 0);
+ERROR: insert or update on table "t3" violates foreign key constraint "t3_t1_id_fkey"
+DETAIL: Key (t1_id)=(11) is not present in table "t1".
+rollback;
+-- Test 004: TestWhenAllDefinedThenFireInProperOrder
+begin;
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- B_constr and A_trig should not fire because Fkey constraint will fire first
+-- and error out. Use this to test internal is firing first when all defined
+insert into ts.t3 values(21, 11, 0);
+ERROR: insert or update on table "t3" violates foreign key constraint "t3_t1_id_fkey"
+DETAIL: Key (t1_id)=(11) is not present in table "t1".
+rollback;
+begin;
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- Now Fkey constraint will pass, then B_constr should fire, followed by A_trig
+insert into ts.t3 values(21, 10, 0);
+NOTICE: B_constr fired!
+NOTICE: A_trig fired!
+rollback;
+-- Test 005: TestWhenInternalConstrDeferredThenTriggersFireFirst
+begin;
+alter table ts.t3 alter constraint t3_t1_id_fkey deferrable;
+set constraints ts.t3_t1_id_fkey deferred;
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- A_trig should fire
+insert into ts.t3 values(100, 100, 0);
+NOTICE: A_trig fired!
+-- fix constraints
+insert into ts.t1 values(100);
+-- fkey constraint should pass on commit
+commit;
+drop trigger "A_trig" on ts.t3;
+-- Test 006: TestWhenCustomConstrDeferredThenTriggersFireFirst
+begin;
+create constraint trigger "B_constr" after insert on ts.t1
+ deferrable initially deferred for each row execute procedure "B_constr"();
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+-- A_trig should fire
+insert into ts.t1 values(101);
+NOTICE: A_trig fired!
+-- B_constr should fire on transaction commit
+commit;
+NOTICE: B_constr fired!
+drop trigger "B_constr" on ts.t1;
+drop trigger "A_trig" on ts.t1;
+-- Test 007: TestWhenInternalAndCustomConstrDeferredThenTriggersFireFirst
+begin;
+alter table ts.t3 alter constraint t3_t1_id_fkey deferrable;
+set constraints ts.t3_t1_id_fkey deferred;
+create constraint trigger "B_constr" after insert on ts.t3
+ deferrable initially deferred for each row execute procedure "B_constr"();
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- A_trig should fire first
+insert into ts.t3 values(102, 102, 0);
+NOTICE: A_trig fired!
+-- fix fkey constraint
+insert into ts.t1 values(102);
+-- fkey constraint should fire and pass, followed by B_constr
+commit;
+NOTICE: B_constr fired!
+drop trigger "B_constr" on ts.t3;
+drop trigger "A_trig" on ts.t3;
+-- Test 008: TestWhenMultipleCustomConstrThenConstrFireInAlphabeticalOrder
+begin;
+create constraint trigger "C_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "C_constr"();
+create constraint trigger "B_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "B_constr"();
+-- B_constr -> C_constr expected
+insert into ts.t1 values(11);
+NOTICE: B_constr fired!
+NOTICE: C_constr fired!
+rollback;
+-- Test 009: TestWhenMultipleCustomTriggersThenTriggersFireInAlphabeticalOrder
+begin;
+create trigger "D_trig" after insert on ts.t1
+ for each row execute procedure "D_trig"();
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+-- A_trig -> D_trig expected
+insert into ts.t1 values(11);
+NOTICE: A_trig fired!
+NOTICE: D_trig fired!
+rollback;
+-- Test 010: TestWhenMultipleOfEachThenFireInProperOrderEachAlphabetically
+begin;
+create constraint trigger "C_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "C_constr"();
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+create trigger "D_trig" after insert on ts.t3
+ for each row execute procedure "D_trig"();
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- Check that FKey fires first, this should fail because insert violates
+-- the constraint
+insert into ts.t3 values(21, 11, 0);
+ERROR: insert or update on table "t3" violates foreign key constraint "t3_t1_id_fkey"
+DETAIL: Key (t1_id)=(11) is not present in table "t1".
+rollback;
+begin;
+create constraint trigger "C_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "C_constr"();
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+create trigger "D_trig" after insert on ts.t3
+ for each row execute procedure "D_trig"();
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- Now Fkey constraint will pass, then B_constr->C_constr->A_trig->D_trig
+insert into ts.t3 values(21, 10);
+NOTICE: B_constr fired!
+NOTICE: C_constr fired!
+NOTICE: A_trig fired!
+NOTICE: D_trig fired!
+rollback;
+-- Test 011: TestWhenOneConstrDeferredThenOtherConstrsFireFirst
+begin;
+create constraint trigger "C_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "C_constr"();
+create constraint trigger "B_constr" after insert on ts.t1
+ deferrable initially deferred for each row execute procedure "B_constr"();
+create trigger "D_trig" after insert on ts.t1
+ for each row execute procedure "D_trig"();
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+-- This should run non def constr C_constr. Then A_trig->D_trig.
+insert into ts.t1 values(103);
+NOTICE: C_constr fired!
+NOTICE: A_trig fired!
+NOTICE: D_trig fired!
+-- On transaction commmit run B_constr
+commit;
+NOTICE: B_constr fired!
+drop trigger "B_constr" on ts.t1;
+drop trigger "C_constr" on ts.t1;
+drop trigger "A_trig" on ts.t1;
+drop trigger "D_trig" on ts.t1;
+-- Test 012: TestWhenInternalConstrDeferredThenCheckedAtTransactionCommit
+begin;
+-- defers the fkey from t2 to t3
+alter table ts.t3 alter constraint t3_t2_id_fkey deferrable;
+set constraints ts.t3_t2_id_fkey deferred;
+create constraint trigger "C_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "C_constr"();
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+create trigger "D_trig" after insert on ts.t3
+ for each row execute procedure "D_trig"();
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+-- Should run all of the other triggers, including the fkey to t1
+-- which it will check first and should pass.
+insert into ts.t3 values(21, 10, 1);
+NOTICE: B_constr fired!
+NOTICE: C_constr fired!
+NOTICE: A_trig fired!
+NOTICE: D_trig fired!
+-- transaction commit should fail because of fkey constraint to t3
+-- transaction should abort now
+commit;
+ERROR: insert or update on table "t3" violates foreign key constraint "t3_t2_id_fkey"
+DETAIL: Key (t2_id)=(1) is not present in table "t2".
+-- Clean up
+drop schema ts cascade;
+NOTICE: drop cascades to 3 other objects
+DETAIL: drop cascades to table ts.t1
+drop cascades to table ts.t2
+drop cascades to table ts.t3
diff --git a/src/test/regress/serial_schedule b/src/test/regress/serial_schedule
index cfb20a8..85e148c 100644
--- a/src/test/regress/serial_schedule
+++ b/src/test/regress/serial_schedule
@@ -10,6 +15,7 @@
test: man-2279
test: man-1941
test: man-784
+test: man-5377
test: tablespace
test: boolean
test: char
diff --git a/src/test/regress/sql/man-5377.sql b/src/test/regress/sql/man-5377.sql
new file mode 100644
index 0000000..05a4117
--- /dev/null
+++ b/src/test/regress/sql/man-5377.sql
@@ -0,0 +0,280 @@
+create schema ts;
+
+create table ts.t1 (id int primary key);
+create table ts.t2 (id int primary key);
+create table ts.t3 (id int primary key, t1_id int, t2_id int,
+ foreign key (t1_id) references ts.t1(id),
+ foreign key (t2_id) references ts.t2(id));
+
+insert into ts.t1 select generate_series(1,10) as id;
+insert into ts.t2 values(0);
+insert into ts.t3 select generate_series(11,20) as id,
+ generate_series(1,10) as t1_id;
+
+create or replace function "A_trig" () returns trigger
+as $$
+begin
+ raise notice 'A_trig fired!';
+ return null;
+end;
+$$ language plpgsql;
+
+create or replace function "D_trig" () returns trigger
+as $$
+begin
+ raise notice 'D_trig fired!';
+ return null;
+end;
+$$ language plpgsql;
+
+create or replace function "B_constr" () returns trigger
+as $$
+begin
+ raise notice 'B_constr fired!';
+ return null;
+end;
+$$ language plpgsql;
+
+create or replace function "C_constr" () returns trigger
+as $$
+begin
+ raise notice 'C_constr fired!';
+ return null;
+end;
+$$ language plpgsql;
+
+-- Test 001: TestWhenUserTriggerDefinedThenImmediateInternalConstrFiresFirst
+begin;
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- A_trig should not fire because Fkey constraint will fire first
+-- and error out
+insert into ts.t3 values(21, 11, 0);
+rollback;
+
+-- Test 002: TestWhenUserTriggerDefinedThenImmediateCustomConstrFiresFirst
+begin;
+
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+
+create constraint trigger "B_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "B_constr"();
+
+-- expect B_constr->A_trig
+insert into ts.t1 values(11);
+rollback;
+
+-- Test 003: TestWhenCustomConstrDefinedThenImmediateInternalCosntrFiresFirst
+begin;
+
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+
+-- B_constr should not fire because Fkey constraint will fire first
+-- and error out
+insert into ts.t3 values(21, 11, 0);
+rollback;
+
+-- Test 004: TestWhenAllDefinedThenFireInProperOrder
+begin;
+
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- B_constr and A_trig should not fire because Fkey constraint will fire first
+-- and error out. Use this to test internal is firing first when all defined
+insert into ts.t3 values(21, 11, 0);
+rollback;
+
+begin;
+
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- Now Fkey constraint will pass, then B_constr should fire, followed by A_trig
+insert into ts.t3 values(21, 10, 0);
+rollback;
+
+-- Test 005: TestWhenInternalConstrDeferredThenTriggersFireFirst
+begin;
+
+alter table ts.t3 alter constraint t3_t1_id_fkey deferrable;
+set constraints ts.t3_t1_id_fkey deferred;
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- A_trig should fire
+insert into ts.t3 values(100, 100, 0);
+-- fix constraints
+insert into ts.t1 values(100);
+-- fkey constraint should pass on commit
+commit;
+
+drop trigger "A_trig" on ts.t3;
+
+-- Test 006: TestWhenCustomConstrDeferredThenTriggersFireFirst
+begin;
+
+create constraint trigger "B_constr" after insert on ts.t1
+ deferrable initially deferred for each row execute procedure "B_constr"();
+
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+
+-- A_trig should fire
+insert into ts.t1 values(101);
+-- B_constr should fire on transaction commit
+commit;
+
+drop trigger "B_constr" on ts.t1;
+drop trigger "A_trig" on ts.t1;
+
+-- Test 007: TestWhenInternalAndCustomConstrDeferredThenTriggersFireFirst
+begin;
+
+alter table ts.t3 alter constraint t3_t1_id_fkey deferrable;
+set constraints ts.t3_t1_id_fkey deferred;
+
+create constraint trigger "B_constr" after insert on ts.t3
+ deferrable initially deferred for each row execute procedure "B_constr"();
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- A_trig should fire first
+insert into ts.t3 values(102, 102, 0);
+-- fix fkey constraint
+insert into ts.t1 values(102);
+-- fkey constraint should fire and pass, followed by B_constr
+commit;
+
+drop trigger "B_constr" on ts.t3;
+drop trigger "A_trig" on ts.t3;
+
+-- Test 008: TestWhenMultipleCustomConstrThenConstrFireInAlphabeticalOrder
+begin;
+
+create constraint trigger "C_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "C_constr"();
+
+create constraint trigger "B_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "B_constr"();
+
+-- B_constr -> C_constr expected
+insert into ts.t1 values(11);
+rollback;
+
+-- Test 009: TestWhenMultipleCustomTriggersThenTriggersFireInAlphabeticalOrder
+begin;
+
+create trigger "D_trig" after insert on ts.t1
+ for each row execute procedure "D_trig"();
+
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+
+-- A_trig -> D_trig expected
+insert into ts.t1 values(11);
+rollback;
+
+-- Test 010: TestWhenMultipleOfEachThenFireInProperOrderEachAlphabetically
+begin;
+
+create constraint trigger "C_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "C_constr"();
+
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+
+create trigger "D_trig" after insert on ts.t3
+ for each row execute procedure "D_trig"();
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- Check that FKey fires first, this should fail because insert violates
+-- the constraint
+insert into ts.t3 values(21, 11, 0);
+rollback;
+
+begin;
+
+create constraint trigger "C_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "C_constr"();
+
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+
+create trigger "D_trig" after insert on ts.t3
+ for each row execute procedure "D_trig"();
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- Now Fkey constraint will pass, then B_constr->C_constr->A_trig->D_trig
+insert into ts.t3 values(21, 10);
+rollback;
+
+-- Test 011: TestWhenOneConstrDeferredThenOtherConstrsFireFirst
+begin;
+
+create constraint trigger "C_constr" after insert on ts.t1
+ not deferrable for each row execute procedure "C_constr"();
+
+create constraint trigger "B_constr" after insert on ts.t1
+ deferrable initially deferred for each row execute procedure "B_constr"();
+
+create trigger "D_trig" after insert on ts.t1
+ for each row execute procedure "D_trig"();
+
+create trigger "A_trig" after insert on ts.t1
+ for each row execute procedure "A_trig"();
+
+-- This should run non def constr C_constr. Then A_trig->D_trig.
+insert into ts.t1 values(103);
+-- On transaction commmit run B_constr
+commit;
+
+drop trigger "B_constr" on ts.t1;
+drop trigger "C_constr" on ts.t1;
+drop trigger "A_trig" on ts.t1;
+drop trigger "D_trig" on ts.t1;
+
+-- Test 012: TestWhenInternalConstrDeferredThenCheckedAtTransactionCommit
+begin;
+
+-- defers the fkey from t2 to t3
+alter table ts.t3 alter constraint t3_t2_id_fkey deferrable;
+set constraints ts.t3_t2_id_fkey deferred;
+
+create constraint trigger "C_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "C_constr"();
+
+create constraint trigger "B_constr" after insert on ts.t3
+ not deferrable for each row execute procedure "B_constr"();
+
+create trigger "D_trig" after insert on ts.t3
+ for each row execute procedure "D_trig"();
+
+create trigger "A_trig" after insert on ts.t3
+ for each row execute procedure "A_trig"();
+
+-- Should run all of the other triggers, including the fkey to t1
+-- which it will check first and should pass.
+insert into ts.t3 values(21, 10, 1);
+-- transaction commit should fail because of fkey constraint to t3
+-- transaction should abort now
+commit;
+
+-- Clean up
+drop schema ts cascade;
\ No newline at end of file
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: IMMEDIATE constraint enforcement does not comply with the SQL standard
2018-10-08 21:35 IMMEDIATE constraint enforcement does not comply with the SQL standard Finnerty, Jim <jfinnert@amazon.com>
@ 2018-10-08 21:49 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-10-08 22:05 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Jim Finnerty <jfinnert@amazon.com>
0 siblings, 1 reply; 6+ messages in thread
From: Tom Lane @ 2018-10-08 21:49 UTC (permalink / raw)
To: Finnerty, Jim <jfinnert@amazon.com>; +Cc: pgsql-bugs
"Finnerty, Jim" <jfinnert@amazon.com> writes:
> PostgreSQL executes AFTER triggers (and constraints) in alphabetical order according to the name assigned to the trigger at creation time; therefore, PostgreSQL permits IMMEDIATE foreign key constraints to be deferred until after AFTER triggers are fired if their name sorts lower than “RI_ConstraintTrigger”. This is a violation of the standard.
We consider that a feature, not a bug. If you want standard-compliant
behavior, don't name your triggers that way. On the other hand, if
you'd like to get something done before FK constraints fire, you have
the option to do so.
(I think this is documented somewhere. If not, there's an opportunity
for documentation improvement.)
The fact that the sort is plain ASCII means that "RI_ConstraintTrigger"
sorts before any lower-case trigger name, so the issue is not as large
as it might otherwise seem.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: IMMEDIATE constraint enforcement does not comply with the SQL standard
2018-10-08 21:35 IMMEDIATE constraint enforcement does not comply with the SQL standard Finnerty, Jim <jfinnert@amazon.com>
2018-10-08 21:49 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-10-08 22:05 ` Jim Finnerty <jfinnert@amazon.com>
2018-10-09 05:54 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 1 reply; 6+ messages in thread
From: Jim Finnerty @ 2018-10-08 22:05 UTC (permalink / raw)
To: pgsql-bugs
We do want standard-compliant behavior, and standard-compliance a strength of
PostgreSQL, and as mentioned we already do have a way (DEFERRABLE INITIALLY
DEFERRED) if this is the user's intent.
The consequences of non-compliance is much more insidious than it at first
appears. At stake here is whether PostgreSQL supports non-deferrable
constraints at all. The purpose of having non-deferrable IMMEDIATE
constraints in the standard is to make query optimizations possible.
Also, as far as I can tell, we do not document the specific name that we
generate for constraint triggers; therefore, any application that depends on
undocumented naming conventions is skating on precariously thin ice, in
addition to writing non-portable code.
The equivalent logic was implemented on Oracle and MySQL. Both of those
systems implement IMMEDIATE mode constraints consistently with the standard,
and produce a different result than PostgreSQL.
best regards,
/Jim F
-----
Jim Finnerty, AWS, Amazon Aurora PostgreSQL
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-bugs-f2117394.html
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: IMMEDIATE constraint enforcement does not comply with the SQL standard
2018-10-08 21:35 IMMEDIATE constraint enforcement does not comply with the SQL standard Finnerty, Jim <jfinnert@amazon.com>
2018-10-08 21:49 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Tom Lane <tgl@sss.pgh.pa.us>
2018-10-08 22:05 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Jim Finnerty <jfinnert@amazon.com>
@ 2018-10-09 05:54 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-10-09 11:55 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Jim Finnerty <jfinnert@amazon.com>
0 siblings, 1 reply; 6+ messages in thread
From: Andrew Gierth @ 2018-10-09 05:54 UTC (permalink / raw)
To: Jim Finnerty <jfinnert@amazon.com>; +Cc: pgsql-bugs
>>>>> "Jim" == Jim Finnerty <jfinnert@amazon.com> writes:
Jim> The consequences of non-compliance is much more insidious than it
Jim> at first appears. At stake here is whether PostgreSQL supports
Jim> non-deferrable constraints at all. The purpose of having
Jim> non-deferrable IMMEDIATE constraints in the standard is to make
Jim> query optimizations possible.
Unfortunately the possibility of query optimizations for FKs is already
defeated by the issue of referential actions, where we also violate the
spec.
https://wiki.postgresql.org/wiki/PostgreSQL_vs_SQL_Standard#Trigger_firing_relative_to_referential_c...
(I just added your issue to that entry, which was already there to
discuss the referential actions vs BEFORE trigger timing.)
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: IMMEDIATE constraint enforcement does not comply with the SQL standard
2018-10-08 21:35 IMMEDIATE constraint enforcement does not comply with the SQL standard Finnerty, Jim <jfinnert@amazon.com>
2018-10-08 21:49 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Tom Lane <tgl@sss.pgh.pa.us>
2018-10-08 22:05 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Jim Finnerty <jfinnert@amazon.com>
2018-10-09 05:54 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Andrew Gierth <andrew@tao11.riddles.org.uk>
@ 2018-10-09 11:55 ` Jim Finnerty <jfinnert@amazon.com>
2018-10-09 17:31 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Jim Finnerty <jfinnert@amazon.com>
0 siblings, 1 reply; 6+ messages in thread
From: Jim Finnerty @ 2018-10-09 11:55 UTC (permalink / raw)
To: pgsql-bugs
ON [ DELETE | UPDATE ] [ CASCADE | RESTRICT ] pertains to inter-table
relationships. My understanding is that the constraint trigger names have
been carefully chosen in order to get the referential actions to work
correctly; however, this patch does not change the names or the relative
firing order of constraint triggers relative to each other.
The SET NULL action is not problematic with respect to standard compliance,
but the SET DEFAULT action potentially would be. According to the
documentation page that you referenced, though:
"... if an action specifies SET DEFAULT but the default value would not
satisfy the foreign key constraint, the operation will fail."
The patch passes all tests in installcheck serial, as well as the tests we
have added (included with the patch), so if there is a problem we don't have
test coverage for it. Can anyone devise a test that would cause the patch
to change the behavior of a BEFORE trigger or a referential action?
thank you,
/Jim F
-----
Jim Finnerty, AWS, Amazon Aurora PostgreSQL
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-bugs-f2117394.html
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: IMMEDIATE constraint enforcement does not comply with the SQL standard
2018-10-08 21:35 IMMEDIATE constraint enforcement does not comply with the SQL standard Finnerty, Jim <jfinnert@amazon.com>
2018-10-08 21:49 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Tom Lane <tgl@sss.pgh.pa.us>
2018-10-08 22:05 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Jim Finnerty <jfinnert@amazon.com>
2018-10-09 05:54 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-10-09 11:55 ` Re: IMMEDIATE constraint enforcement does not comply with the SQL standard Jim Finnerty <jfinnert@amazon.com>
@ 2018-10-09 17:31 ` Jim Finnerty <jfinnert@amazon.com>
0 siblings, 0 replies; 6+ messages in thread
From: Jim Finnerty @ 2018-10-09 17:31 UTC (permalink / raw)
To: pgsql-bugs
We created a repro for the ON UPDATE CASCADE ... BEFORE trigger problem,
which as Andrew said is a second area where the SQL standard is violated.
-----
Jim Finnerty, AWS, Amazon Aurora PostgreSQL
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-bugs-f2117394.html
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2018-10-09 17:31 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-10-08 21:35 IMMEDIATE constraint enforcement does not comply with the SQL standard Finnerty, Jim <jfinnert@amazon.com>
2018-10-08 21:49 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-10-08 22:05 ` Jim Finnerty <jfinnert@amazon.com>
2018-10-09 05:54 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-10-09 11:55 ` Jim Finnerty <jfinnert@amazon.com>
2018-10-09 17:31 ` Jim Finnerty <jfinnert@amazon.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