Fix FK triggers losing DEFERRABLE/INITIALLY DEFERRED when marked ENFORCED again

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
This commit is contained in:
Fujii Masao 2026-03-30 14:37:33 +09:00
parent 49315de0c0
commit 5db5e33969
3 changed files with 92 additions and 0 deletions

View 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)

View 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*,

View 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