My Tennis Pal
Preventing overlapping court bookings
A player booking a match and a coach scheduling a class can claim the same available court seconds apart. My Tennis Pal resolves that conflict when the reservations reach PostgreSQL.
One court, one schedule
My Tennis Pal manages bookings and classes for a tennis club. A player can reserve a court directly, while a recurring class generates a booking for each session. Both use the same court schedule.
The gap between a read and a write
The availability grid helps a player choose a slot. Two players can still see the same opening before either reservation has been saved. Checking the schedule again in application code leaves a gap between reading availability and writing the booking.
Enforcing the rule on every write
The booking table has an exclusion constraint. For active reservations on the same court, PostgreSQL rejects overlapping time ranges. This also covers partial overlaps, such as a class that starts halfway through a player’s reservation.
EXCLUDE USING gist (
court_id WITH =,
tstzrange(start_time, end_time) WITH &&
)
WHERE (status != 'cancelled')The constraint compares both the court identifier and the time range, so simultaneous matches on different courts are valid. A cancelled booking releases its slot while its record stays in the database. The same rule applies whether a player or a class creates the next booking.