← Back to PostgreSQL, page 4

postgresql-0067 · Hard senior

Prevent double bookings at the database level with an exclusion constraint.

EXCLUDE USING gist (room WITH =, period WITH &&) rejects any two rows with the same room and overlapping periods, enforced atomically even under concurrency. Equality on a scalar column inside a GiST constraint needs the btree_gist extension. Application-level checks cannot do this safely because two transactions can both check and then both insert.

Full answer

Tables and rows used here:

sql
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)');
sql
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:

text
ERROR:  conflicting key value violates exclusion constraint "room_bookings_room_period_excl"
sql
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:

text
 room | guest
------+-------
 101  | Asha
 101  | Chen
 102  | Ben
(3 rows)
sql
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:

text
 clashes
---------
 1
(1 row)

In a second session:

sql
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:

text
COMMIT
sql
ROLLBACK;

Result on PostgreSQL 17:

text
ROLLBACK

Ben'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.

sql
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