pgsql: Fix FK triggers losing DEFERRABLE/INITIALLY DEFERRED when marked

From: Fujii Masao <fujii(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix FK triggers losing DEFERRABLE/INITIALLY DEFERRED when marked
Date: 2026-03-30 05:39:43
Message-ID: E1w75Li-0026XN-2i@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(dot)honda(at)gmail(dot)com>
Reviewed-by: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAKmOUTms2nkxEZDdcrsjq5P3b2L_PR266Hv8kW5pANwmVaRJJQ@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5db5e339692ed98adb5ae6ff625a92b49f770a6f

Modified Files
--------------
src/backend/commands/tablecmds.c | 2 ++
src/test/regress/expected/foreign_key.out | 53 +++++++++++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql | 37 +++++++++++++++++++++
3 files changed, 92 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2026-03-30 07:24:40 pgsql: Make cast function from circle to polygon error safe
Previous Message Fujii Masao 2026-03-30 05:39:28 pgsql: Fix FK triggers losing DEFERRABLE/INITIALLY DEFERRED when marked