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.

Kindconstraint
Example LanguageSQL

📝 Syntax

`... DEFERRABLE INITIALLY DEFERRED`
`... DEFERRABLE INITIALLY IMMEDIATE`
Switch inside a transaction: `SET CONSTRAINTS ALL DEFERRED` / `SET CONSTRAINTS ALL IMMEDIATE`

⚙️ Parameters

INITIALLY DEFERRED / IMMEDIATEWhether the constraint is checked at transaction start or immediately. DEFERRED waits until commit; IMMEDIATE checks after each statement.
SET CONSTRAINTSSwitches 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏