[PATCH] Fix unexpected loss of DEFERRABLE property after toggling NOT ENFORCED / ENFORCED

Started by Yasuo Honda9 months ago6 messageshackers
Jump to latest
#1Yasuo Honda
yasuo.honda@gmail.com

Hi,

I have encountered an unexpected behavior where the DEFERRABLE and
INITIALLY DEFERRED properties of foreign keys are lost after toggling
them from NOT ENFORCED to ENFORCED.

Background

In the Ruby on Rails framework, there is a built-in mechanism to
temporarily bypass foreign key checks while loading test data.
Currently, this is implemented using: ALTER TABLE ... DISABLE TRIGGER
ALL; ALTER TABLE ... ENABLE TRIGGER ALL;

However, this requires superuser privileges. With the newly introduced
support for "NOT ENFORCED" foreign keys in PostgreSQL 18, I am
interested in switching to: ALTER TABLE ... ALTER CONSTRAINT ... NOT
ENFORCED; ALTER TABLE ... ALTER CONSTRAINT ... ENFORCED;

This would allow the operation to be performed by the table owner
without superuser rights. However, I discovered that switching the
state back to ENFORCED unexpectedly strips away the DEFERRABLE
property.

Problem

When re-enforcing a constraint, there is a discrepancy between the
constraint definition and its underlying triggers.
While the flags in pg_constraint remain correct, the corresponding
triggers in pg_trigger (tgdeferrable and tginitdeferred) are reset to
defaults ('f') when they are reconstructed during the ENFORCED
operation.

I have attached a reproduction SQL script that demonstrates this by
comparing the values in pg_constraint and pg_trigger. In the current
PostgreSQL 18.3, you can see that the triggers lose their
deferrability even though the constraint itself is still defined as
DEFERRABLE. This causes "SET CONSTRAINTS ... DEFERRED" to fail.

After Patch

* The tgdeferrable and tginitdeferred flags in pg_trigger are
correctly preserved to match pg_constraint after the toggle, and
deferred execution works as expected.

I've attached the reproduction SQL script and a patch to fix this in
src/backend/commands/tablecmds.c. The patch and reproduction SQL
script were developed with the assistance of Claude Code. I have
reviewed and verified the code myself.

Any feedback is appreciated.
Regards,
--
Yasuo Honda

Attachments:

0001-Restore-tgdeferrable-and-tginitdeferred-after-NOT-EN.patchapplication/octet-stream; name=0001-Restore-tgdeferrable-and-tginitdeferred-after-NOT-EN.patchDownload+30-1
tgdeferrable-tginitdeferred-not-restored.sqlapplication/octet-stream; name=tgdeferrable-tginitdeferred-not-restored.sqlDownload
#2Fujii Masao
masao.fujii@gmail.com
In reply to: Yasuo Honda (#1)
Re: [PATCH] Fix unexpected loss of DEFERRABLE property after toggling NOT ENFORCED / ENFORCED

On Fri, Feb 27, 2026 at 6:51 PM Yasuo Honda <yasuo.honda@gmail.com> wrote:

Hi,

I have encountered an unexpected behavior where the DEFERRABLE and
INITIALLY DEFERRED properties of foreign keys are lost after toggling
them from NOT ENFORCED to ENFORCED.

Background

In the Ruby on Rails framework, there is a built-in mechanism to
temporarily bypass foreign key checks while loading test data.
Currently, this is implemented using: ALTER TABLE ... DISABLE TRIGGER
ALL; ALTER TABLE ... ENABLE TRIGGER ALL;

However, this requires superuser privileges. With the newly introduced
support for "NOT ENFORCED" foreign keys in PostgreSQL 18, I am
interested in switching to: ALTER TABLE ... ALTER CONSTRAINT ... NOT
ENFORCED; ALTER TABLE ... ALTER CONSTRAINT ... ENFORCED;

This would allow the operation to be performed by the table owner
without superuser rights. However, I discovered that switching the
state back to ENFORCED unexpectedly strips away the DEFERRABLE
property.

Problem

When re-enforcing a constraint, there is a discrepancy between the
constraint definition and its underlying triggers.
While the flags in pg_constraint remain correct, the corresponding
triggers in pg_trigger (tgdeferrable and tginitdeferred) are reset to
defaults ('f') when they are reconstructed during the ENFORCED
operation.

I have attached a reproduction SQL script that demonstrates this by
comparing the values in pg_constraint and pg_trigger. In the current
PostgreSQL 18.3, you can see that the triggers lose their
deferrability even though the constraint itself is still defined as
DEFERRABLE. This causes "SET CONSTRAINTS ... DEFERRED" to fail.

After Patch

* The tgdeferrable and tginitdeferred flags in pg_trigger are
correctly preserved to match pg_constraint after the toggle, and
deferred execution works as expected.

I've attached the reproduction SQL script and a patch to fix this in
src/backend/commands/tablecmds.c. The patch and reproduction SQL
script were developed with the assistance of Claude Code. I have
reviewed and verified the code myself.

Any feedback is appreciated.

Thanks for reporting the issue and providing a patch!

I was able to reproduce the issue on the master.

The patch looks good overall, but since I'm not very familiar with this area,
I'd like to spend a bit more time reviewing the changes in detail.

Regarding the regression test, would it be better to verify not only catalog
state (e.g., pg_trigger) but also the actual behavior? For example, we could
check that a foreign key violation is not raised immediately on INSERT,
but instead at COMMIT even after ALTER CONSTRAINT ENFORCED:

ALTER TABLE ... ALTER CONSTRAINT ... NOT ENFORCED;
ALTER TABLE ... ALTER CONSTRAINT ... ENFORCED;
BEGIN;
INSERT INTO t VALUES (1);
COMMIT;

In this case, the foreign key violation should be reported at COMMIT,
even after ALTER CONSTRAINT ... ENFORCED.

Regards,

--
Fujii Masao

#3Yasuo Honda
yasuo.honda@gmail.com
In reply to: Fujii Masao (#2)
Re: [PATCH] Fix unexpected loss of DEFERRABLE property after toggling NOT ENFORCED / ENFORCED

(Resending to the list, as my previous reply went to the sender only.)

Thanks for the comment.

Regarding the regression test, would it be better to verify not only catalog

state (e.g., pg_trigger) but also the actual behavior?

Agreed. In addition to inspecting pg_trigger contents, I've added test
cases that verify the actual runtime behavior of deferred constraints.
I've also covered INITIALLY IMMEDIATE alongside INITIALLY DEFERRED.

--
Yasuo Honda

Show quoted text

On Tue, Mar 24, 2026 at 3:30 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Feb 27, 2026 at 6:51 PM Yasuo Honda <yasuo.honda@gmail.com> wrote:

Hi,

I have encountered an unexpected behavior where the DEFERRABLE and
INITIALLY DEFERRED properties of foreign keys are lost after toggling
them from NOT ENFORCED to ENFORCED.

Background

In the Ruby on Rails framework, there is a built-in mechanism to
temporarily bypass foreign key checks while loading test data.
Currently, this is implemented using: ALTER TABLE ... DISABLE TRIGGER
ALL; ALTER TABLE ... ENABLE TRIGGER ALL;

However, this requires superuser privileges. With the newly introduced
support for "NOT ENFORCED" foreign keys in PostgreSQL 18, I am
interested in switching to: ALTER TABLE ... ALTER CONSTRAINT ... NOT
ENFORCED; ALTER TABLE ... ALTER CONSTRAINT ... ENFORCED;

This would allow the operation to be performed by the table owner
without superuser rights. However, I discovered that switching the
state back to ENFORCED unexpectedly strips away the DEFERRABLE
property.

Problem

When re-enforcing a constraint, there is a discrepancy between the
constraint definition and its underlying triggers.
While the flags in pg_constraint remain correct, the corresponding
triggers in pg_trigger (tgdeferrable and tginitdeferred) are reset to
defaults ('f') when they are reconstructed during the ENFORCED
operation.

I have attached a reproduction SQL script that demonstrates this by
comparing the values in pg_constraint and pg_trigger. In the current
PostgreSQL 18.3, you can see that the triggers lose their
deferrability even though the constraint itself is still defined as
DEFERRABLE. This causes "SET CONSTRAINTS ... DEFERRED" to fail.

After Patch

* The tgdeferrable and tginitdeferred flags in pg_trigger are
correctly preserved to match pg_constraint after the toggle, and
deferred execution works as expected.

I've attached the reproduction SQL script and a patch to fix this in
src/backend/commands/tablecmds.c. The patch and reproduction SQL
script were developed with the assistance of Claude Code. I have
reviewed and verified the code myself.

Any feedback is appreciated.

Thanks for reporting the issue and providing a patch!

I was able to reproduce the issue on the master.

The patch looks good overall, but since I'm not very familiar with this area,
I'd like to spend a bit more time reviewing the changes in detail.

Regarding the regression test, would it be better to verify not only catalog
state (e.g., pg_trigger) but also the actual behavior? For example, we could
check that a foreign key violation is not raised immediately on INSERT,
but instead at COMMIT even after ALTER CONSTRAINT ENFORCED:

ALTER TABLE ... ALTER CONSTRAINT ... NOT ENFORCED;
ALTER TABLE ... ALTER CONSTRAINT ... ENFORCED;
BEGIN;
INSERT INTO t VALUES (1);
COMMIT;

In this case, the foreign key violation should be reported at COMMIT,
even after ALTER CONSTRAINT ... ENFORCED.

Regards,

--
Fujii Masao

Attachments:

v2-Restore-tgdeferrable-and-tginitdeferred-after-NOT.patchapplication/octet-stream; name=v2-Restore-tgdeferrable-and-tginitdeferred-after-NOT.patchDownload+96-1
#4Fujii Masao
masao.fujii@gmail.com
In reply to: Yasuo Honda (#3)
Re: [PATCH] Fix unexpected loss of DEFERRABLE property after toggling NOT ENFORCED / ENFORCED

On Fri, Mar 27, 2026 at 4:41 PM Yasuo Honda <yasuo.honda@gmail.com> wrote:

(Resending to the list, as my previous reply went to the sender only.)

Thanks for the comment.

Regarding the regression test, would it be better to verify not only catalog

state (e.g., pg_trigger) but also the actual behavior?

Agreed. In addition to inspecting pg_trigger contents, I've added test
cases that verify the actual runtime behavior of deferred constraints.

Thanks for updating the patch!

I've also covered INITIALLY IMMEDIATE alongside INITIALLY DEFERRED.

I'm not sure this test adds much value. Testing DEFERRABLE INITIALLY DEFERRED
seems sufficient, so I removed the DEFERRABLE INITIALLY IMMEDIATE case.
The updated patch is attached.

+-- verify that tgdeferrable/tginitdeferred are preserved after NOT
ENFORCED -> ENFORCED
+ALTER TABLE FKTABLE ALTER CONSTRAINT fktable_fk_fkey DEFERRABLE
INITIALLY DEFERRED;
+ALTER TABLE FKTABLE ALTER CONSTRAINT fktable_fk_fkey NOT ENFORCED;

It looks like this assumes the constraint is initially ENFORCED,
but in this part of the test it is already NOT ENFORCED. So I moved
this test to a section where the constraint is ENFORCED, which
seems more appropriate.

I also updated the commit log message.

Barring any objections, I will commit this and backpatch it to v18, where
NOT ENFORCED foreign keys were introduced.

Regards,

--
Fujii Masao

Attachments:

v3-0001-Fix-FK-triggers-losing-DEFERRABLE-INITIALLY-DEFER.patchapplication/octet-stream; name=v3-0001-Fix-FK-triggers-losing-DEFERRABLE-INITIALLY-DEFER.patchDownload+92-1
#5Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#4)
Re: [PATCH] Fix unexpected loss of DEFERRABLE property after toggling NOT ENFORCED / ENFORCED

On Sat, Mar 28, 2026 at 12:01 AM Fujii Masao <masao.fujii@gmail.com> wrote:

Barring any objections, I will commit this and backpatch it to v18, where
NOT ENFORCED foreign keys were introduced.

I've pushed the patch. Thanks!

Regards,

--
Fujii Masao

#6Yasuo Honda
yasuo.honda@gmail.com
In reply to: Fujii Masao (#5)
Re: [PATCH] Fix unexpected loss of DEFERRABLE property after toggling NOT ENFORCED / ENFORCED

I've pushed the patch. Thanks!

Thanks for reviewing and committing this, and for the fixups.
Once this fix is released, I can complete NOT ENFORCED foreign key
support in Ruby on Rails.

--
Yasuo Honda

Show quoted text

On Mon, Mar 30, 2026 at 2:41 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Sat, Mar 28, 2026 at 12:01 AM Fujii Masao <masao.fujii@gmail.com> wrote:

Barring any objections, I will commit this and backpatch it to v18, where
NOT ENFORCED foreign keys were introduced.

I've pushed the patch. Thanks!

Regards,

--
Fujii Masao