← Back to PostgreSQL, page 1

postgresql-0015 · Easy fresher

Write a CHECK constraint that involves two columns, and show how existing bad data blocks adding it.

A CHECK constraint can compare columns in the same row, such as end_date >= start_date. Adding it to a table with violating rows fails. Fix the data first, or add the constraint NOT VALID so new rows are checked immediately, then clean up and run VALIDATE CONSTRAINT.

Full answer

Sample schema and data:

sql
CREATE TABLE bookings (id int, start_date date, end_date date, guests int);
INSERT INTO bookings VALUES
  (1, '2026-12-10', '2026-12-12', 2),
  (2, '2026-12-15', '2026-12-14', 1);
sql
ALTER TABLE bookings ADD CONSTRAINT dates_in_order CHECK (end_date >= start_date);

Result on PostgreSQL 17:

text
ERROR:  check constraint "dates_in_order" of relation "bookings" is violated by some row
sql
ALTER TABLE bookings ADD CONSTRAINT dates_in_order CHECK (end_date >= start_date) NOT VALID;

Result:

text
ALTER TABLE
sql
INSERT INTO bookings VALUES (3, '2026-12-20', '2026-12-01', 2);

Output:

text
ERROR:  new row for relation "bookings" violates check constraint "dates_in_order"
sql
UPDATE bookings SET end_date = '2026-12-16' WHERE id = 2; ALTER TABLE bookings VALIDATE CONSTRAINT dates_in_order; SELECT conname, convalidated FROM pg_constraint WHERE conname = 'dates_in_order';

PostgreSQL returns:

text
    conname     | convalidated
----------------+--------------
 dates_in_order | t
(1 row)

Adding the constraint normally scans the table and fails on booking 2, whose end date is before its start. NOT VALID skips the scan of existing rows but still rejects the new bad booking 3. After the data is fixed, VALIDATE CONSTRAINT checks the old rows and marks the constraint validated. On large tables this two-step approach also shortens the strong lock, because validation takes a weaker lock than adding a constraint with a full scan. Note that CHECK passes when the expression is NULL, so add NOT NULL where dates are required.

sql
CREATE TABLE bookings (id int, start_date date, end_date date, guests int);
INSERT INTO bookings VALUES
  (1, '2026-12-10', '2026-12-12', 2),
  (2, '2026-12-15', '2026-12-14', 1);

ALTER TABLE bookings ADD CONSTRAINT dates_in_order CHECK (end_date >= start_date);

ALTER TABLE bookings ADD CONSTRAINT dates_in_order CHECK (end_date >= start_date) NOT VALID;

INSERT INTO bookings VALUES (3, '2026-12-20', '2026-12-01', 2);

UPDATE bookings SET end_date = '2026-12-16' WHERE id = 2; ALTER TABLE bookings VALIDATE CONSTRAINT dates_in_order; SELECT conname, convalidated FROM pg_constraint WHERE conname = 'dates_in_order';

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • CHECK can compare columns in a row
  • Existing bad rows block a normal ADD
  • NOT VALID checks new rows only
  • VALIDATE CONSTRAINT checks old rows later
  • CHECK passes on NULL

Common mistakes

  • Adding constraints to big tables in one step at peak time
  • Forgetting NOT NULL alongside CHECK

Follow-ups

  • Which lock does VALIDATE CONSTRAINT take?
  • When would a trigger be needed instead of CHECK?

Tips

  • Mention the NULL pass