Created
March 23, 2026 14:43
-
-
Save yahonda/8d0b62b591055072dab78adf39ea0023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #3 NOT ENFORCED -> ENFORCED resets pg_trigger tgdeferrable and tginitdeferred value to f | |
| ``` | |
| postgres=# select version(); | |
| version | |
| ----------------------------------------------------------------------------------------------------------------------------- | |
| PostgreSQL 18.3 (Homebrew) on aarch64-apple-darwin25.2.0, compiled by Apple clang version 17.0.0 (clang-1700.6.3.2), 64-bit | |
| (1 row) | |
| postgres=# CREATE TABLE parent ( | |
| id INT PRIMARY KEY | |
| ); | |
| CREATE TABLE | |
| postgres=# CREATE TABLE child ( | |
| id INT PRIMARY KEY, | |
| parent_id INT, | |
| CONSTRAINT child_parent_fk | |
| FOREIGN KEY (parent_id) | |
| REFERENCES parent(id) | |
| DEFERRABLE INITIALLY DEFERRED | |
| ); | |
| CREATE TABLE | |
| postgres=# SELECT | |
| conname, | |
| condeferrable, | |
| condeferred | |
| FROM pg_constraint | |
| WHERE conname = 'child_parent_fk'; | |
| conname | condeferrable | condeferred | |
| -----------------+---------------+------------- | |
| child_parent_fk | t | t | |
| (1 row) | |
| postgres=# SELECT | |
| tgname, | |
| tgdeferrable, | |
| tginitdeferred | |
| FROM pg_trigger | |
| WHERE tgname LIKE 'RI_ConstraintTrigger%'; | |
| tgname | tgdeferrable | tginitdeferred | |
| ------------------------------+--------------+---------------- | |
| RI_ConstraintTrigger_a_25888 | t | t | |
| RI_ConstraintTrigger_a_25889 | t | t | |
| RI_ConstraintTrigger_c_25890 | t | t | |
| RI_ConstraintTrigger_c_25891 | t | t | |
| (4 rows) | |
| postgres=# ALTER TABLE child ALTER CONSTRAINT child_parent_fk NOT ENFORCED; | |
| ALTER TABLE | |
| postgres=# ALTER TABLE child ALTER CONSTRAINT child_parent_fk ENFORCED; | |
| ALTER TABLE | |
| postgres=# SELECT | |
| conname, | |
| condeferrable, | |
| condeferred | |
| FROM pg_constraint | |
| WHERE conname = 'child_parent_fk'; | |
| conname | condeferrable | condeferred | |
| -----------------+---------------+------------- | |
| child_parent_fk | t | t | |
| (1 row) | |
| postgres=# SELECT | |
| tgname, | |
| tgdeferrable, | |
| tginitdeferred | |
| FROM pg_trigger | |
| WHERE tgname LIKE 'RI_ConstraintTrigger%'; | |
| tgname | tgdeferrable | tginitdeferred | |
| ------------------------------+--------------+---------------- | |
| RI_ConstraintTrigger_a_25892 | f | f | |
| RI_ConstraintTrigger_a_25893 | f | f | |
| RI_ConstraintTrigger_c_25894 | f | f | |
| RI_ConstraintTrigger_c_25895 | f | f | |
| (4 rows) | |
| postgres=# BEGIN; | |
| BEGIN | |
| postgres=*# SET CONSTRAINTS child_parent_fk DEFERRED; | |
| SET CONSTRAINTS | |
| postgres=*# INSERT INTO child (id, parent_id) VALUES (1, 999); | |
| ERROR: insert or update on table "child" violates foreign key constraint "child_parent_fk" | |
| DETAIL: Key (parent_id)=(999) is not present in table "parent". | |
| postgres=!# | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment