SQLSTATE 23P01Severity lowLab verified

Incident brief

New row violates exclusion constraint

A table has an EXCLUDE constraint, and a new row conflicts with an existing row under the constraint's comparison — most commonly, two overlapping time ranges for the same resource.

Reproduced on PostgreSQL 16.14Verified 2026-07-15 (Docker lab, PostgreSQL 16.14)Reviewed by Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What
New row violates exclusion constraint
What triggers it
Create a table with EXCLUDE USING gist to prevent overlapping ranges for the same key.
The fix
Pick a range that does not overlap an existing one for the same key.
Proof
Reproduced on PostgreSQL 16.14 → The second reservation for room 1, overlapping the first by 30 minutes, was rejected with SQLSTATE 23P01 and only the original reservation remained.

The fix

What to do right now

The immediate, application-level response to this error.

  • Pick a range that does not overlap an existing one for the same key.
  • Query for conflicts (using the && operator) before inserting, if the application needs a friendlier error than a constraint violation.
  • Use EXCLUDE, not a plain UNIQUE constraint, whenever the rule is about overlap rather than exact duplicates.
Fix — SQL
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE TABLE booking.reservations (
  room_id integer,
  during tsrange,
  EXCLUDE USING gist (room_id WITH =, during WITH &&)
);

-- a non-overlapping range for the same room is accepted
INSERT INTO booking.reservations (room_id, during)
  VALUES (1, '[2026-01-01 11:00, 2026-01-01 12:00)');

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-15 (Docker lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed