Quick revision
The short answer
We add CHECK (ends_at > starts_at) and CHECK (status IN ('held', 'confirmed', 'cancelled')), and prevent overlaps with an exclusion constraint: EXCLUDE USING gist (room_id WITH =, tstzrange(starts_at, ends_at) WITH &&) WHERE (status <> 'cancelled'). The equality on room_id inside a GiST constraint needs the btree_gist extension. Application checks alone race under concurrency, so the database must be the arbiter; MySQL has no exclusion constraint, so there we serialize on the room row with SELECT ... FOR UPDATE.
What to remember
In this answer, we added CHECK constraints for the time order and the allowed statuses, saw that an application-level overlap check races under concurrency, and replaced it with a PostgreSQL exclusion constraint over room_id and a half-open tstzrange, limited to non-cancelled bookings and enabled for room_id by the btree_gist extension. We chose '[)' bounds so back-to-back bookings are allowed, and in MySQL serialized requests per room with SELECT ... FOR UPDATE.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We add CHECK (ends_at > starts_at) and CHECK (status IN ('held', 'confirmed', 'cancelled')), and prevent overlaps with an exclusion constraint: EXCLUDE USING gist (room_id WITH =, tstzrange(starts_at, ends_at) WITH &&) WHERE (status <> 'cancelled'). The equality on room_id inside a GiST constraint needs the btree_gist extension. Application checks alone race under concurrency, so the database must be the arbiter; MySQL has no exclusion constraint, so there we serialize on the room row with SELECT ... FOR UPDATE.
Why this question is asked
Double bookings are the classic bug of reservation systems: two people book the same room for overlapping times, and both get a confirmation. Interviewers ask for these constraints because the first two are easy and the third separates candidates who know PostgreSQL's exclusion constraints, and why an application check is not enough, from those who do not. In this answer, we will add the simple checks, show why checking for overlaps in code fails, build the exclusion constraint step by step, choose the range bounds, and see what to do in MySQL.
1The Two Simple Checks
A CHECK constraint is a condition every row must satisfy; the database rejects any insert or update that makes it false.
For status we could also use a PostgreSQL enum type or a lookup table with a foreign key. A CHECK is the simplest and easy to change with a migration; an enum is compact but adding values needs ALTER TYPE; a lookup table is best when statuses carry extra data or are managed by users. Note that a CHECK with IN passes for NULL, which is why status is also NOT NULL.
2Why Checking for Overlaps in Code Is Not Enough
The natural first attempt is: select any overlapping booking for the room, and insert only if none is found. Under concurrency, two requests can both run the select before either inserts.
7 messages in order. Play them, or step through them one at a time.
Each request saw no conflict in its own snapshot, so both inserted.
Default READ COMMITTED isolation does nothing to stop this, because neither transaction wrote a row the other one read. We could run at SERIALIZABLE or lock the room, but the cleanest answer in PostgreSQL is to state the rule as a constraint, so the database itself rejects the second insert.
3Building the Exclusion Constraint
An exclusion constraint generalizes a unique constraint. A unique constraint says no two rows may be equal on these columns. An exclusion constraint says no two rows may satisfy all of these operator comparisons at once. Here we want: no two rows with the same room_id (operator =) whose time ranges overlap (operator &&).
Let us go through the pieces. tstzrange builds a range of timestamps with time zone from the two columns, and && is the range overlap operator. Exclusion constraints need an index type that supports the operators, and GiST (a generalized search tree index) supports && on ranges. But plain bigint equality is normally a B-tree operator, so we install btree_gist, an extension that ships with PostgreSQL and teaches GiST to handle = on scalar types. The WHERE clause makes it a partial constraint: cancelled bookings are not checked, so a cancelled slot can be booked again.
Concurrency is now handled by the database. If two transactions insert overlapping bookings at the same time, the second one waits for the first to commit and then fails with the exclusion error (SQLSTATE 23P01), which the application can turn into a friendly 'this slot was just taken'.
4Choosing Half-Open Range Bounds
The third argument '[)' means the range includes its start and excludes its end. A booking from 10:00 to 11:00 covers 10:00 up to, but not including, 11:00. That is why the 11:00 to 12:00 booking above is accepted: the two ranges touch but do not overlap.
With closed bounds '[]' both bookings would include 11:00 exactly, so back-to-back bookings would conflict, which is almost never what people want. Half-open ranges also compose neatly: a day is [00:00, next 00:00), and consecutive slots tile without gaps or overlaps. '[)' is the default for tstzrange, but writing it explicitly documents the intent.
5What to Do in MySQL
MySQL 8.4 enforces CHECK constraints, so the first two rules carry over. It has no exclusion constraint and no range types, so overlap prevention must serialize the booking requests for each room. The usual way is to lock the room's row first, then check and insert in the same transaction.
Notice the overlap test: two half-open ranges overlap when each starts before the other ends. Because every booking request for room 1 must first lock the room row, they run one at a time. The weakness is that the rule lives in code, so every path that inserts bookings, including admin tools and scripts, must follow the same locking protocol.
6Follow-up questions
What half-open range bounds would you use and why?
I would use '[)', inclusive start and exclusive end. Then a booking ending at 11:00 and another starting at 11:00 do not overlap, which matches how people think about back-to-back meetings, and adjacent slots tile without gaps. With inclusive ends, consecutive bookings would conflict at the boundary instant and we would have to fake end times like 10:59:59.
Does the exclusion constraint slow down inserts?
Each insert does a GiST index search for overlapping rows of the same room, which is a logarithmic lookup, so it is cheap for normal booking volumes. The GiST index is larger and a bit slower to update than a B-tree, but that cost is tiny compared with the bugs it prevents.
How should the Spring application handle the constraint violation?
Spring translates the PostgreSQL error into a DataIntegrityViolationException, and the cause carries SQLSTATE 23P01 and the constraint name. The service catches it around the insert and returns a conflict response such as HTTP 409 with a message that the slot was just taken, rather than a generic server error.
7Conclusion
In this answer, we added CHECK constraints for the time order and the allowed statuses, saw that an application-level overlap check races under concurrency, and replaced it with a PostgreSQL exclusion constraint over room_id and a half-open tstzrange, limited to non-cancelled bookings and enabled for room_id by the btree_gist extension. We chose '[)' bounds so back-to-back bookings are allowed, and in MySQL serialized requests per room with SELECT ... FOR UPDATE.
Open with
The first two rules are plain check constraints: ends_at greater than starts_at, and status in held, confirmed or cancelled, with status also not null since a check passes on null.
Then, if they want more
An enum or a lookup table would work for status too. The interesting one is the overlap rule. Checking in the application with a select before the insert does not work, because two requests can both see no conflict and both insert.
So in PostgreSQL I would use an exclusion constraint: exclude using gist, room_id with equals, and tstzrange of starts_at and ends_at with the overlap operator, with a where clause status not equal cancelled, so cancelled bookings free the slot. GiST supports the range overlap operator, but equality on a plain bigint needs the btree_gist extension, so I create that first.
I would build the range with half-open bounds, inclusive start and exclusive end, so a booking ending at eleven and another starting at eleven do not conflict. With this in place, concurrent overlapping inserts are serialized by the database and the loser gets an exclusion violation, SQLSTATE 23P01, which I map to a 409 conflict.
In MySQL there is no exclusion constraint, so I would lock the room row with select for update, check for overlaps with starts before the other ends, and insert in the same transaction.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Add constraints to a PostgreSQL table bookings(id, room_id, starts_at, ends_at, status)...": https://scrivio-interview-library.netlify.app/databases/data-modeling/c9ab601381/