DEFERRABLE Constraint
Last updated: 2026-11-04
DEFERRABLE Constraint
PostgreSQL can postpone checking foreign keys, unique and exclusion constraints until commit time, which solves the circular dependency of writing a child row before its parent. With DEFERRABLE INITIALLY DEFERRED the constraint is deferred by default, and SET CONSTRAINTS switches the timing at any point inside a transaction.
| Kind | constraint |
|---|---|
| Example Language | SQL |
📝 Syntax
`... DEFERRABLE INITIALLY DEFERRED`
`... DEFERRABLE INITIALLY IMMEDIATE`
Switch inside a transaction: `SET CONSTRAINTS ALL DEFERRED` / `SET CONSTRAINTS ALL IMMEDIATE`
⚙️ Parameters
| INITIALLY DEFERRED / IMMEDIATE | Whether the constraint is checked at transaction start or immediately. DEFERRED waits until commit; IMMEDIATE checks after each statement. |
|---|---|
| SET CONSTRAINTS | Switches constraints between deferred and immediate mode inside a transaction block. ALL affects every deferrable constraint. |
Returns:DEFERRABLE has no return value. If a deferred check finds a conflict at commit time, the whole transaction rolls back with the corresponding constraint error.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT 'deferred FK resolved at COMMIT' AS step, (SELECT count(*)::TEXT FROM shipments) AS result
UNION ALL SELECT 'warehouse 777 now exists', (SELECT count(*)::TEXT FROM warehouses WHERE warehouse_id = 777)
UNION ALL SELECT 'immediate mode still rejects orphan', (SELECT pg_try('SET CONSTRAINTS ALL IMMEDIATE; INSERT INTO shipments VALUES (2, 888)'))
step | result
-------------------------------------+-----------------------------------------------------------------------------------------------------
deferred FK resolved at COMMIT | 1
warehouse 777 now exists | 1
immediate mode still rejects orphan | insert or update on table "shipments" violates foreign key constraint "shipments_warehouse_id_fkey"
(3 rows)
📌 Key Points
- SET CONSTRAINTS must run inside a transaction block, otherwise PostgreSQL reports `SET CONSTRAINTS can only be used in transaction blocks`.
- Only constraints declared DEFERRABLE can be deferred; ordinary constraints are always checked immediately.
- Deferred checks happen at commit, so a conflict rolls back the entire transaction rather than a single row.
- NOT NULL, CHECK and DEFAULT can never be deferred; they always take effect immediately.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhen is a deferred constraint genuinely needed?
ATypical cases: parent and child tables referencing each other, bulk imports whose order you do not control, or filling a foreign key later in the same transaction. If you can always write parents before children, immediate checking is simpler and reports the offending row at once.
QAre deferred constraints the same thing as NOT VALID?
ANo. DEFERRABLE changes when the check happens (postponed to commit), while NOT VALID changes which rows are checked (existing rows are skipped, only new writes are constrained). They are independent and can be combined.