Full answer
Tables and rows used here:
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE TABLE room_bookings (
room int,
guest text,
period tstzrange,
EXCLUDE USING gist (room WITH =, period WITH &&)
);
INSERT INTO room_bookings VALUES (101, 'Asha', '[2026-12-10 14:00+05:30, 2026-12-12 11:00+05:30)');INSERT INTO room_bookings VALUES (101, 'Ben', '[2026-12-11 14:00+05:30, 2026-12-13 11:00+05:30)');Result on PostgreSQL 17:
ERROR: conflicting key value violates exclusion constraint "room_bookings_room_period_excl"INSERT INTO room_bookings VALUES (101, 'Chen', '[2026-12-12 11:00+05:30, 2026-12-14 11:00+05:30)'), (102, 'Ben', '[2026-12-11 14:00+05:30, 2026-12-13 11:00+05:30)'); SELECT room, guest FROM room_bookings ORDER BY room, lower(period);Result:
room | guest
------+-------
101 | Asha
101 | Chen
102 | Ben
(3 rows)BEGIN; SELECT count(*) AS clashes FROM room_bookings WHERE room = 101 AND period && '[2026-12-13 00:00+05:30, 2026-12-13 12:00+05:30)';Output:
clashes
---------
1
(1 row)In a second session:
BEGIN; SELECT count(*) AS clashes FROM room_bookings WHERE room = 101 AND period && '[2026-12-15 00:00+05:30, 2026-12-15 12:00+05:30)'; INSERT INTO room_bookings VALUES (101, 'Dev', '[2026-12-15 10:00+05:30, 2026-12-16 10:00+05:30)'); COMMIT;PostgreSQL returns:
COMMITROLLBACK;Result on PostgreSQL 17:
ROLLBACKBen's booking overlaps Asha's stay in room 101 and is rejected with 'conflicting key value violates exclusion constraint'. Chen's booking starts exactly when Asha's ends, which is allowed because the range upper bound is exclusive, and Ben gets room 102 instead. In the last runs, session A's pre-check finds one clash on 13 December (Chen's stay), so A rolls back, while session B checks 15 December, finds it free and books it. Pre-checks like these are useful for friendly messages, but only the constraint is safe when two requests for the same slot race: the second insert waits for the first transaction and then fails. The constraint's GiST index also speeds up availability searches.
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE TABLE room_bookings (
room int,
guest text,
period tstzrange,
EXCLUDE USING gist (room WITH =, period WITH &&)
);
INSERT INTO room_bookings VALUES (101, 'Asha', '[2026-12-10 14:00+05:30, 2026-12-12 11:00+05:30)');
INSERT INTO room_bookings VALUES (101, 'Ben', '[2026-12-11 14:00+05:30, 2026-12-13 11:00+05:30)');
INSERT INTO room_bookings VALUES (101, 'Chen', '[2026-12-12 11:00+05:30, 2026-12-14 11:00+05:30)'), (102, 'Ben', '[2026-12-11 14:00+05:30, 2026-12-13 11:00+05:30)'); SELECT room, guest FROM room_bookings ORDER BY room, lower(period);
BEGIN; SELECT count(*) AS clashes FROM room_bookings WHERE room = 101 AND period && '[2026-12-13 00:00+05:30, 2026-12-13 12:00+05:30)';
-- session B
BEGIN; SELECT count(*) AS clashes FROM room_bookings WHERE room = 101 AND period && '[2026-12-15 00:00+05:30, 2026-12-15 12:00+05:30)'; INSERT INTO room_bookings VALUES (101, 'Dev', '[2026-12-15 10:00+05:30, 2026-12-16 10:00+05:30)'); COMMIT;
ROLLBACK;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- EXCLUDE enforces no-overlap rules
- btree_gist enables = in GiST
- Exclusive upper bounds allow back-to-back stays
- Constraints beat check-then-insert
- The index also serves availability queries
Common mistakes
- Checking overlaps only in application code
- Using inclusive end times
Follow-ups
- How would you allow overlaps for cancelled bookings?
- How does this behave under heavy write load?
Tips
- Show the back-to-back case