Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Project

The booking system

Start after Part 14

Stage 3

Goal: Model a clinic from written requirements, enforce every rule with constraints, audit changes with a trigger — and prove, with two live sessions, that the same slot can never be sold twice. Time: ~5–6 hours

The brief

"Patients book appointments with practitioners. An appointment occupies one practitioner for a time slot (up to four hours). A practitioner can never be double-booked — but a cancelled appointment frees its slot. Every status change must be auditable. Two receptionists booking the same slot at the same moment must not both succeed."

Design it on paper first (Part 9), build it (Part 10), test every rule by breaking it (Part 10's habit), and run the concurrency proof in two terminals (Part 12). Only then compare with the reference files here:

File What it holds
schema.sql Tables, constraints, the exclusion rule, the audit trigger
seed.sql Two practitioners, three patients, three appointments
checks.sql Six acceptance checks — each must pass/fail exactly as commented
$ createdb clinic
$ psql -d clinic -f schema.sql
$ psql -d clinic -f seed.sql
$ psql -d clinic -f checks.sql

The design's one big idea

"UNIQUE (practitioner, start_time)" cannot express "no overlapping appointments" once durations vary. The honest tool is a range plus an exclusion constraint:

slot tstzrange NOT NULL,
CONSTRAINT appointments_no_double_booking
  EXCLUDE USING gist (practitioner_id WITH =, slot WITH &&)
  WHERE (status IN ('booked', 'completed'))

Read it as: no two rows may share a practitioner (=) while their slots overlap (&&) — and the WHERE makes it a partial constraint, so cancelled appointments release their time. btree_gist (a contrib extension) lets the equality and the range share one GiST index.

The acceptance checks

Running checks.sql against a fresh seed must produce exactly:

  1. Overlapping Dr. Rao's 09:00–09:30 fails with conflicting key value violates exclusion constraint "appointments_no_double_booking".
  2. The same time with the other practitioner inserts fine, because it is a different resource.
  3. A backwards slot is rejected by the range type itself (range lower bound must be less than or equal to range upper bound).
  4. An unknown patient fails with a foreign-key violation.
  5. A status of 'maybe' fails the appointments_status_valid CHECK constraint.
  6. After cancelling appointment 1, its exact slot books successfully, and appointment_audit holds one row: booked → cancelled.

The concurrency proof

Two terminals, both booking Dr. Fernandes for the same free slot inside open transactions — a real run:

-- Receptionist A                               -- Receptionist B
BEGIN;
INSERT INTO appointments (...)
VALUES (2, 1, tstzrange('2026-01-06 09:00+05:30',
                        '2026-01-06 09:30+05:30'))
RETURNING appointment_id;   → 8
                                                BEGIN;
                                                INSERT INTO appointments (...)   -- same slot
                                                …blocks: A's uncommitted row holds the slot…
COMMIT;
                                                ERROR:  conflicting key value violates
                                                exclusion constraint
                                                "appointments_no_double_booking"
                                                COMMIT;   → ROLLBACK

B waited on A's in-flight insert, then failed the moment A's commit made the conflict real. No isolation-level tuning, no application lock — the constraint alone guarantees the invariant, under any concurrency, from any client. That is the whole Stage 3 thesis in one transcript: rules that live in the schema cannot be raced, forgotten, or bypassed.

Stretch goals (optional)

  • Add working hours (a CHECK that slots fall 08:00–20:00 — lower(slot)::time is your friend).
  • A cancel_appointment(id) procedure that sets the status and reports whether the slot was rebooked within a day.
  • A patients-per-practitioner view with each practitioner's utilisation for the seeded day.

Search the guide