Constraint Validation (NOT VALID / VALIDATE)

Last updated: 2026-11-04

Constraint Validation (NOT VALID / VALIDATE)

Adding a CHECK or foreign key to a large existing table means a full scan and a lock, which can block a production database for a long time. NOT VALID makes the new constraint cover only subsequent writes and skip existing rows; running VALIDATE CONSTRAINT afterwards performs the backfill check, splitting one heavy operation into two controllable steps.

Kindconstraint
Example LanguageSQL

📝 Syntax

Constrain new rows only: `ALTER TABLE t ADD CONSTRAINT c CHECK (...) NOT VALID`
Validate existing rows later: `ALTER TABLE t VALIDATE CONSTRAINT c`
Check the status: `pg_constraint.convalidated`

⚙️ Parameters

NOT VALIDApplies to newly written rows only; existing rows are left unchecked (supported for FOREIGN KEY and CHECK only).
VALIDATE CONSTRAINTRescans the whole table to validate existing rows. On failure it reports `... is violated by some row` and leaves the constraint unvalidated.

Returns:Neither statement returns a value. The status lives in pg_constraint.convalidated: false means existing rows are still unvalidated, true means everything passed.

▶ 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 step, result FROM (
  SELECT 1 AS ord, step, result FROM validation_log
  UNION ALL
  SELECT 2, 'convalidated now',
         (SELECT convalidated::TEXT FROM pg_constraint
           WHERE conrelid='legacy_orders'::regclass AND contype='f')
) t ORDER BY ord
 step                               | result
------------------------------------+------------------------------------------------------------------------------------------------------------
 VALIDATE while orphan row exists   | insert or update on table "legacy_orders" violates foreign key constraint "legacy_orders_customer_id_fkey"
 VALIDATE after removing orphan row | OK
 convalidated now                   | true
(3 rows)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhy add a constraint as NOT VALID first?
ABecause ADD CONSTRAINT scans all existing rows immediately, so its runtime grows with the table and it may hold locks for a long time. Adding it as NOT VALID rolls out quickly (new data only), and a later VALIDATE during a quiet period backfills the existing rows, keeping both the risk and the downtime under control.
QWhat do I do when VALIDATE fails?
ALocate the offending rows using the error (for example `SELECT * FROM t WHERE v <= 0`), fix or delete them, and run VALIDATE again. The constraint still exists after the failure and still applies to new writes, so data written while you are fixing the problem stays valid.
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%

🙏 帮我们做得更好

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

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