Deferred constraints: validate data rules at transaction end
Learn when postponing a constraint check helps, how PostgreSQL controls the timing, and which rules and databases do not support deferral.
What is a deferred constraint?
A deferred constraint postpones validation until the current transaction ends instead of checking after each data-changing statement. The transaction may temporarily violate a rule, but its final state must satisfy the rule at COMMIT. PostgreSQL supports deferral for foreign keys, unique constraints, primary keys, and exclusion constraints; support is not universal across database systems.
Why delay the check?
Some changes have a valid final result but temporarily violate a rule while several rows are being updated. Suppose two items in a list occupy positions 1 and 2 and you want to swap them. With an immediately checked UNIQUE (list_id, position), changing the first row to position 2 can conflict with the other row that still has position 2. Deferring the check lets the database validate the final pair of positions after both changes.
Using it in PostgreSQL
PostgreSQL constraints are NOT DEFERRABLE by default. A deferrable constraint can start each transaction as INITIALLY IMMEDIATE (checked after each statement) or INITIALLY DEFERRED (checked at transaction end).
CREATE TABLE list_items (
list_id bigint NOT NULL,
item_id bigint PRIMARY KEY,
position integer NOT NULL,
CONSTRAINT item_position_unique
UNIQUE (list_id, position)
DEFERRABLE INITIALLY DEFERRED
);You can then swap two positions in one transaction:
BEGIN;
UPDATE list_items
SET position = CASE position
WHEN 1 THEN 2
WHEN 2 THEN 1
END
WHERE list_id = 42 AND position IN (1, 2);
COMMIT;This SQL is illustrative and was not executed. Before using it, confirm that exactly the intended two rows match and that no other ordering rule is affected. If the constraint is INITIALLY IMMEDIATE, run SET CONSTRAINTS item_position_unique DEFERRED in that transaction before the changes. Afterward, SET CONSTRAINTS item_position_unique IMMEDIATE can request validation at that point; outstanding changes are checked retroactively. COMMIT fails if a deferred constraint is still violated.
What can and cannot be deferred?
In PostgreSQL, deferral applies to UNIQUE, PRIMARY KEY, EXCLUDE, and REFERENCES (foreign-key) constraints. NOT NULL and CHECK are always checked immediately. A NOT DEFERRABLE constraint cannot be changed with SET CONSTRAINTS, so do not assume every kind of constraint can wait until commit.
A deferrable UNIQUE constraint cannot be used as the conflict arbiter of PostgreSQL INSERT ... ON CONFLICT. Deferred checking validates the transaction’s final state; it does not publish an invalid intermediate state to other sessions or permit an invalid state to commit.
When to use it
Consider a deferrable constraint when a sequence of row changes has a valid final state but an intermediate step would conflict.
If each statement can satisfy the rule, immediate checking is simpler; defer only the constraints that need it.
In PostgreSQL, `SET CONSTRAINTS` applies only to the current transaction. Outside a transaction block it has no effect.
The application must handle a commit-time failure. Roll back, correct the cause, and retry as appropriate.
MySQL 8.4 does not support deferred constraint checks. InnoDB checks foreign keys immediately and treats `NO ACTION` like `RESTRICT`. Check both syntax and timing when moving to another DBMS or version.
Sources and scope
[PostgreSQL 18: SET CONSTRAINTS](https://www.postgresql.org/docs/18/sql-set-constraints.html)
[PostgreSQL 18: CREATE TABLE — DEFERRABLE and INITIALLY](https://www.postgresql.org/docs/18/sql-createtable.html)
[MySQL 8.4: FOREIGN KEY constraint differences](https://dev.mysql.com/doc/refman/8.4/en/constraint-foreign-key.html)
Documentation checked November 23, 2026. The SQL is illustrative and was not run. Deferred constraint types and support vary by DBMS and version.