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.
| Kind | constraint |
|---|---|
| Example Language | SQL |
📝 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 VALID | Applies to newly written rows only; existing rows are left unchecked (supported for FOREIGN KEY and CHECK only). |
|---|---|
| VALIDATE CONSTRAINT | Rescans 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
- NOT VALID applies to FOREIGN KEY and CHECK only; PRIMARY KEY, UNIQUE and EXCLUDE do not support it.
- While a constraint is NOT VALID, existing rows can still be written and updated; only newly written rows are constrained.
- A failed VALIDATE does not drop the constraint: it stays convalidated = false and can be retried after the data is fixed.
- VALIDATE CONSTRAINT takes a SHARE UPDATE EXCLUSIVE lock: it does not block ordinary reads and writes, but it does block DDL.
💡 Related Cases
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.