On 11th of March 2026, Andrew Dunstan committed patch:
Add support for altering CHECK constraint enforceability This commit adds support for ALTER TABLE ALTER CONSTRAINT ... [NOT] ENFORCED for CHECK constraints. Previously, only foreign key constraints could have their enforceability altered. When changing from NOT ENFORCED to ENFORCED, the operation not only updates catalog information but also performs a full table scan in Phase 3 to validate that existing data satisfies the constraint. For partitioned tables and inheritance hierarchies, the operation recurses to all child tables. When changing to NOT ENFORCED, we must recurse even if the parent is already NOT ENFORCED, since child constraints may still be ENFORCED. Author: Jian He <jian.universality@gmail.com> Reviewed-by: Robert Treat <rob@xzilla.net> Reviewed-by: Kirill Reshke <reshkekirill@gmail.com> Reviewed-by: Amul Sul <sulamul@gmail.com> Reviewed-by: Zsolt Parragi <zsolt.parragi@cybertec.at> Reviewed-by: Andrew Dunstan <andrew@dunslane.net> Discussion: https://postgr.es/m/CACJufxHCh_FU-FsEwsCvg9mN6-5tzR6H9ntn+0KUgTCaerDOmg@mail.gmail.com
PostgreSQL 18 added NOT ENFORCED constraints. You could create CHECK (or foreign key) constraint that is not enforced, but later on, you could change enforceability only for foreign keys. For CHECK constraints the only option was to drop the constraint and create it again.
Now, we can just flip it. Let's make a test table:
=$ create table products ( id int8 generated always as identity primary key, name text not null, price numeric not null, constraint positive_price check (price > 0) ); CREATE TABLE =$ insert into products (name, price) values ('widget', 10), ('gadget', 25); INSERT 0 2 =$ insert into products (name, price) values ('freebie', 0); ERROR: new row for relation "products" violates check constraint "positive_price" DETAIL: Failing row contains (3, freebie, 0).
All as expected. Now, let's say that we have to load some data that we know is not perfect, and fix it later. We can temporarily turn off the constraint:
=$ alter table products alter constraint positive_price not enforced; ALTER TABLE =$ select conname, contype, conenforced, convalidated, pg_get_constraintdef(oid) from pg_constraint where conrelid = 'products'::regclass and contype = 'c'; conname │ contype │ conenforced │ convalidated │ pg_get_constraintdef ────────────────┼─────────┼─────────────┼──────────────┼───────────────────────────────────────────── positive_price │ c │ f │ f │ CHECK ((price > (0)::numeric)) NOT ENFORCED (1 row) =$ \d products Table "public.products" Column │ Type │ Collation │ Nullable │ Default ────────┼─────────┼───────────┼──────────┼────────────────────────────── id │ bigint │ │ not null │ generated always as identity name │ text │ │ not null │ price │ numeric │ │ not null │ Indexes: "products_pkey" PRIMARY KEY, btree (id) Check constraints: "positive_price" CHECK (price > 0::numeric) NOT ENFORCED
Please note that not enforced constraint is also automatically marked as not validated. Which makes sense – PostgreSQL can't guarantee anything about the data anymore.
And now bad row goes in:
=$ insert into products (name, price) values ('freebie', 0); INSERT 0 1 =$ select * from products; id │ name │ price ────┼─────────┼─────── 1 │ widget │ 10 2 │ gadget │ 25 4 │ freebie │ 0 (3 rows)
What happens if I try to enforce it again, while bad data is still there?
=$ alter table products alter constraint positive_price enforced; ERROR: check constraint "positive_price" of relation "products" is violated by some row
Good. Switching to enforced scans the whole table, and refuses if anything is wrong. Let's fix the data and retry:
=$ update products set price = 0.01 where price <= 0; UPDATE 1 =$ alter table products alter constraint positive_price enforced; ALTER TABLE =$ select conname, conenforced, convalidated from pg_constraint where conrelid = 'products'::regclass and contype = 'c'; conname │ conenforced │ convalidated ────────────────┼─────────────┼────────────── positive_price │ t │ t (1 row) =$ insert into products (name, price) values ('freebie', 0); ERROR: new row for relation "products" violates check constraint "positive_price" DETAIL: Failing row contains (5, freebie, 0).
Constraint is back, and it's both enforced and validated.
One thing to keep in mind: going back to enforced does full table scan, and it does it while holding pretty strong lock. To check it, I switched the constraint to not enforced again, and then:
=$ begin; BEGIN =$ alter table products alter constraint positive_price enforced; ALTER TABLE =$ select mode, granted from pg_locks where relation = 'products'::regclass; mode │ granted ─────────────────────┼───────── AccessExclusiveLock │ t (1 row) =$ rollback; ROLLBACK
So, for large tables, that means that nothing can read or write the table until the scan is done. If that's a problem, then the old approach of dropping constraint, adding it as NOT VALID, and then running VALIDATE CONSTRAINT separately (which needs only ShareUpdateExclusiveLock) is still the way to go.
But for many cases, simple flip is much nicer than having to remember full constraint definition to recreate it.
That definitely sounds useful, thanks to everyone involved.