]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix FK triggers losing DEFERRABLE/INITIALLY DEFERRED when marked ENFORCED again
authorFujii Masao <fujii@postgresql.org>
Mon, 30 Mar 2026 05:37:33 +0000 (14:37 +0900)
committerFujii Masao <fujii@postgresql.org>
Mon, 30 Mar 2026 05:38:58 +0000 (14:38 +0900)
Previously, a foreign key defined as DEFERRABLE INITIALLY DEFERRED could
behave as NOT DEFERRABLE after being set to NOT ENFORCED and then back
to ENFORCED.

This happened because recreating the FK triggers on re-enabling the constraint
forgot to restore the tgdeferrable and tginitdeferred fields in pg_trigger.

Fix this bug by properly setting those fields when the foreign key constraint
is marked ENFORCED again and its triggers are recreated, so the original
DEFERRABLE and INITIALLY DEFERRED properties are preserved.

Backpatch to v18, where NOT ENFORCED foreign keys were introduced.

Author: Yasuo Honda <yasuo.honda@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAKmOUTms2nkxEZDdcrsjq5P3b2L_PR266Hv8kW5pANwmVaRJJQ@mail.gmail.com
Backpatch-through: 18

src/backend/commands/tablecmds.c
src/test/regress/expected/foreign_key.out
src/test/regress/sql/foreign_key.sql

index 9b0aad95a0a36e42a910332ddd943375ce89d663..7f62e89342e1332122494845face867f93d15c88 100644 (file)
@@ -12474,6 +12474,8 @@ ATExecAlterConstrEnforceability(List **wqueue, ATAlterConstraint *cmdcon,
                fkconstraint->fk_matchtype = currcon->confmatchtype;
                fkconstraint->fk_upd_action = currcon->confupdtype;
                fkconstraint->fk_del_action = currcon->confdeltype;
+               fkconstraint->deferrable = currcon->condeferrable;
+               fkconstraint->initdeferred = currcon->condeferred;
 
                /* Create referenced triggers */
                if (currcon->conrelid == fkrelid)
index 7f9e0ebb82d654d66085e8b5bf6938c5e44ef935..be07bf26a33e721daa3d03035afb890496f15940 100644 (file)
@@ -1157,6 +1157,59 @@ INSERT INTO fktable VALUES (500, 1000);
 ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
 DETAIL:  Key (fk)=(1000) is not present in table "pktable".
 COMMIT;
+-- Check that the existing FK trigger is both deferrable and initially deferred
+SELECT conname, tgrelid::regclass as tgrel,
+       regexp_replace(tgname, '[0-9]+', 'N') as tgname, tgtype,
+       tgdeferrable, tginitdeferred
+FROM pg_trigger t JOIN pg_constraint c ON (t.tgconstraint = c.oid)
+WHERE conrelid = 'fktable'::regclass AND conname = 'fktable_fk_fkey'
+ORDER BY tgrelid, tgtype;
+     conname     |  tgrel  |          tgname          | tgtype | tgdeferrable | tginitdeferred 
+-----------------+---------+--------------------------+--------+--------------+----------------
+ fktable_fk_fkey | pktable | RI_ConstraintTrigger_a_N |      9 | t            | t
+ fktable_fk_fkey | pktable | RI_ConstraintTrigger_a_N |     17 | t            | t
+ fktable_fk_fkey | fktable | RI_ConstraintTrigger_c_N |      5 | t            | t
+ fktable_fk_fkey | fktable | RI_ConstraintTrigger_c_N |     17 | t            | t
+(4 rows)
+
+-- Changing the constraint to NOT ENFORCED drops the associated FK triggers
+ALTER TABLE FKTABLE ALTER CONSTRAINT fktable_fk_fkey NOT ENFORCED;
+SELECT conname, tgrelid::regclass as tgrel,
+       regexp_replace(tgname, '[0-9]+', 'N') as tgname, tgtype,
+       tgdeferrable, tginitdeferred
+FROM pg_trigger t JOIN pg_constraint c ON (t.tgconstraint = c.oid)
+WHERE conrelid = 'fktable'::regclass AND conname = 'fktable_fk_fkey'
+ORDER BY tgrelid, tgtype;
+ conname | tgrel | tgname | tgtype | tgdeferrable | tginitdeferred 
+---------+-------+--------+--------+--------------+----------------
+(0 rows)
+
+-- Changing it back to ENFORCED will recreate the necessary FK triggers
+-- that are deferrable and initially deferred
+ALTER TABLE FKTABLE ALTER CONSTRAINT fktable_fk_fkey ENFORCED;
+SELECT conname, tgrelid::regclass as tgrel,
+       regexp_replace(tgname, '[0-9]+', 'N') as tgname, tgtype,
+       tgdeferrable, tginitdeferred
+FROM pg_trigger t JOIN pg_constraint c ON (t.tgconstraint = c.oid)
+WHERE conrelid = 'fktable'::regclass AND conname = 'fktable_fk_fkey'
+ORDER BY tgrelid, tgtype;
+     conname     |  tgrel  |          tgname          | tgtype | tgdeferrable | tginitdeferred 
+-----------------+---------+--------------------------+--------+--------------+----------------
+ fktable_fk_fkey | pktable | RI_ConstraintTrigger_a_N |      9 | t            | t
+ fktable_fk_fkey | pktable | RI_ConstraintTrigger_a_N |     17 | t            | t
+ fktable_fk_fkey | fktable | RI_ConstraintTrigger_c_N |      5 | t            | t
+ fktable_fk_fkey | fktable | RI_ConstraintTrigger_c_N |     17 | t            | t
+(4 rows)
+
+-- Verify that a deferrable, initially deferred foreign key still works
+-- as expected after being set to NOT ENFORCED and then re-enabled
+BEGIN;
+-- doesn't match PK, but no error yet
+INSERT INTO fktable VALUES (2, 20);
+-- should catch error from INSERT at commit
+COMMIT;
+ERROR:  insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
+DETAIL:  Key (fk)=(20) is not present in table "pktable".
 DROP TABLE fktable, pktable;
 -- tricky behavior: according to SQL99, if a deferred constraint is set
 -- to 'immediate' mode, it should be checked for validity *immediately*,
index 4a6172b8e56d85e33075add367ab632ecfe99316..cfe6d9f9cc5365631ddd0f41b79a17438ffdeb0d 100644 (file)
@@ -784,6 +784,43 @@ INSERT INTO fktable VALUES (500, 1000);
 
 COMMIT;
 
+-- Check that the existing FK trigger is both deferrable and initially deferred
+SELECT conname, tgrelid::regclass as tgrel,
+       regexp_replace(tgname, '[0-9]+', 'N') as tgname, tgtype,
+       tgdeferrable, tginitdeferred
+FROM pg_trigger t JOIN pg_constraint c ON (t.tgconstraint = c.oid)
+WHERE conrelid = 'fktable'::regclass AND conname = 'fktable_fk_fkey'
+ORDER BY tgrelid, tgtype;
+
+-- Changing the constraint to NOT ENFORCED drops the associated FK triggers
+ALTER TABLE FKTABLE ALTER CONSTRAINT fktable_fk_fkey NOT ENFORCED;
+SELECT conname, tgrelid::regclass as tgrel,
+       regexp_replace(tgname, '[0-9]+', 'N') as tgname, tgtype,
+       tgdeferrable, tginitdeferred
+FROM pg_trigger t JOIN pg_constraint c ON (t.tgconstraint = c.oid)
+WHERE conrelid = 'fktable'::regclass AND conname = 'fktable_fk_fkey'
+ORDER BY tgrelid, tgtype;
+
+-- Changing it back to ENFORCED will recreate the necessary FK triggers
+-- that are deferrable and initially deferred
+ALTER TABLE FKTABLE ALTER CONSTRAINT fktable_fk_fkey ENFORCED;
+SELECT conname, tgrelid::regclass as tgrel,
+       regexp_replace(tgname, '[0-9]+', 'N') as tgname, tgtype,
+       tgdeferrable, tginitdeferred
+FROM pg_trigger t JOIN pg_constraint c ON (t.tgconstraint = c.oid)
+WHERE conrelid = 'fktable'::regclass AND conname = 'fktable_fk_fkey'
+ORDER BY tgrelid, tgtype;
+
+-- Verify that a deferrable, initially deferred foreign key still works
+-- as expected after being set to NOT ENFORCED and then re-enabled
+BEGIN;
+
+-- doesn't match PK, but no error yet
+INSERT INTO fktable VALUES (2, 20);
+
+-- should catch error from INSERT at commit
+COMMIT;
+
 DROP TABLE fktable, pktable;
 
 -- tricky behavior: according to SQL99, if a deferred constraint is set