Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
An exclusion constraint (EXCLUDE USING gist (during WITH &&)) forbids two rows whose ranges overlap. Inserting or updating a row that overlaps an existing one gives 23P01.
The DETAIL line names both values: Key (during)=(new) conflicts with existing key (during)=(old). Compare those two ranges first. The runs below use a tsrange booking table on PostgreSQL 17.
01New booking overlaps an existing one
Booking 1 was 10:00–12:00. Inserting booking 2 as 11:00–13:00 failed with 23P01, and DETAIL showed it conflicting with the 10:00–12:00 key.
A query with during && tsrange('2024-05-01 11:00', '2024-05-01 13:00') found booking 1. Starting booking 2 at 12:00 instead ([12:00, 13:00)) was accepted, and both rows were stored.
Setup
CREATE TABLE bookings (id int PRIMARY KEY, during tsrange NOT NULL,
EXCLUDE USING gist (during WITH &&));
INSERT INTO bookings VALUES (1, '[2024-05-01 10:00, 2024-05-01 12:00)');CREATE TABLE
INSERT 0 1INSERT INTO bookings VALUES (2, '[2024-05-01 11:00, 2024-05-01 13:00)');ERROR: 23P01: conflicting key value violates exclusion constraint "bookings_during_excl"
DETAIL: Key (during)=(["2024-05-01 11:00:00","2024-05-01 13:00:00")) conflicts with existing key (during)=(["2024-05-01 10:00:00","2024-05-01 12:00:00")).
SCHEMA NAME: lab_a
TABLE NAME: bookings
CONSTRAINT NAME: bookings_during_exclSELECT id, during FROM bookings WHERE during && tsrange('2024-05-01 11:00', '2024-05-01 13:00'); id | during
----+-----------------------------------------------
1 | ["2024-05-01 10:00:00","2024-05-01 12:00:00")
(1 row)INSERT INTO bookings VALUES (2, '[2024-05-01 12:00, 2024-05-01 13:00)');
SELECT id, during FROM bookings ORDER BY id;INSERT 0 1
id | during
----+-----------------------------------------------
1 | ["2024-05-01 10:00:00","2024-05-01 12:00:00")
2 | ["2024-05-01 12:00:00","2024-05-01 13:00:00")
(2 rows)02Inclusive upper bound '[]' makes back-to-back ranges overlap
Booking 1 was stored as [10:00, 12:00] with ], so it includes 12:00. A booking [12:00, 13:00] that starts at 12:00 shares that instant, and the insert failed with 23P01.
Changing booking 1 to a half-open range with tsrange(lower(during), upper(during), '[)') and inserting booking 2 as tsrange('2024-05-01 12:00', '2024-05-01 13:00', '[)') worked. Both rows then showed [...).
Setup
CREATE TABLE bookings (id int PRIMARY KEY, during tsrange NOT NULL,
EXCLUDE USING gist (during WITH &&));
INSERT INTO bookings VALUES (1, '[2024-05-01 10:00, 2024-05-01 12:00]');CREATE TABLE
INSERT 0 1INSERT INTO bookings VALUES (2, '[2024-05-01 12:00, 2024-05-01 13:00]');ERROR: 23P01: conflicting key value violates exclusion constraint "bookings_during_excl"
DETAIL: Key (during)=(["2024-05-01 12:00:00","2024-05-01 13:00:00"]) conflicts with existing key (during)=(["2024-05-01 10:00:00","2024-05-01 12:00:00"]).
SCHEMA NAME: lab_a
TABLE NAME: bookings
CONSTRAINT NAME: bookings_during_exclUPDATE bookings SET during = tsrange(lower(during), upper(during), '[)') WHERE id = 1;
SELECT id, during FROM bookings;UPDATE 1
id | during
----+-----------------------------------------------
1 | ["2024-05-01 10:00:00","2024-05-01 12:00:00")
(1 row)INSERT INTO bookings VALUES (2, tsrange('2024-05-01 12:00', '2024-05-01 13:00', '[)'));
SELECT id, during FROM bookings ORDER BY id;INSERT 0 1
id | during
----+-----------------------------------------------
1 | ["2024-05-01 10:00:00","2024-05-01 12:00:00")
2 | ["2024-05-01 12:00:00","2024-05-01 13:00:00")
(2 rows)03UPDATE extends a range into the next row
Booking 1 was 10:00–12:00 and booking 2 13:00–15:00. Extending booking 1 by 2 hours (to 14:00) failed with 23P01, and DETAIL showed the new range 10:00–14:00 against 13:00–15:00.
A query with lead(lower(during)) showed the next start at 13:00. Extending by 1 hour (to 13:00) succeeded, because [10:00, 13:00) does not include 13:00.
Setup
CREATE TABLE bookings (id int PRIMARY KEY, during tsrange NOT NULL,
EXCLUDE USING gist (during WITH &&));
INSERT INTO bookings VALUES (1, '[2024-05-01 10:00, 2024-05-01 12:00)'), (2, '[2024-05-01 13:00, 2024-05-01 15:00)');CREATE TABLE
INSERT 0 2UPDATE bookings SET during = tsrange(lower(during), upper(during) + interval '2 hours') WHERE id = 1;ERROR: 23P01: conflicting key value violates exclusion constraint "bookings_during_excl"
DETAIL: Key (during)=(["2024-05-01 10:00:00","2024-05-01 14:00:00")) conflicts with existing key (during)=(["2024-05-01 13:00:00","2024-05-01 15:00:00")).
SCHEMA NAME: lab_a
TABLE NAME: bookings
CONSTRAINT NAME: bookings_during_exclSELECT id, during, lead(lower(during)) OVER (ORDER BY lower(during)) AS next_start FROM bookings ORDER BY lower(during); id | during | next_start
----+-----------------------------------------------+---------------------
1 | ["2024-05-01 10:00:00","2024-05-01 12:00:00") | 2024-05-01 13:00:00
2 | ["2024-05-01 13:00:00","2024-05-01 15:00:00") |
(2 rows)UPDATE bookings SET during = tsrange(lower(during), upper(during) + interval '1 hour') WHERE id = 1;
SELECT id, during FROM bookings ORDER BY id;UPDATE 1
id | during
----+-----------------------------------------------
1 | ["2024-05-01 10:00:00","2024-05-01 13:00:00")
2 | ["2024-05-01 13:00:00","2024-05-01 15:00:00")
(2 rows)FAQ
How do I find which row conflicts?
Read the DETAIL line; it shows the existing key. You can also query with the && operator and the new range, as in the first run above.
Why do back-to-back bookings conflict?
If the ranges use an inclusive upper bound ']', the end of one equals the start of the next. With '[)' ranges they no longer overlapped in our run.
What is the difference between 23P01 and 23505?
23505 is a unique constraint (equal values). 23P01 is an exclusion constraint, here overlapping ranges with the && operator.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12