pg_advisory_xact_lock
Serialises colliding booking transactions inside PostgreSQL without Redis or an external lock service.
Flagship · Booking, payments & IoT · Colombo
A booking and payments platform running three live sports venues — wired to Raspberry Pi floodlight control at two of them.
01 / Database invariants
Half-court, full-court and dual-zone gym claims overlap physically. Try booking both for the same slot.
Court open for 19:00. Book half court A, then try an overlapping full court.
Serialises colliding booking transactions inside PostgreSQL without Redis or an external lock service.
Half-court and full-court exclusion enforced as a database invariant, even if application code is bypassed.
Translates a PostgreSQL 23505 unique violation into a clear HTTP 400 response.
02 / Multi-merchant payments
Webhooks are a hint. The gateway re-query is the truth.
03 / Incident subsystem
Historical transactions reconciled to zero unaccounted charges.
04 / Daylight gating & inrush
Dual battery Zigbee lux sensors · Fails closed on stale readings
During daytime bookings, natural daylight clears the threshold and suppresses floodlights. When darkness arrives, banks strike in sequence to prevent inrush spikes.
05 / Positive confirmation
A light is off when the light says it’s off.
06 / Proven abstraction
Intermittent drops
0 unrecovered failures since
Deep dive · Part A
A court sold twice is not a scheduling annoyance. It is two paying customers standing on the same floor at the same time, and a front desk with no good answer. Half-court, full-court and dual-zone gym reservations overlap, so the rule is “no overlapping claim on the same physical space”.
pg_advisory_xact_lock serialises requests that could collide, with no Redis or external lock service.23505 unique violation translated into HTTP 400.The three venues operate across distinct merchant accounts. Card payments run through Dialog Genie with merchant credentials selected per venue slug. The server re-queries the payment provider for authoritative state; webhooks are verified with HMAC-SHA256, a five-minute replay window and timingSafeEqual, and treated only as a hint to check.
After three real-world gateway edge cases in July 2026 (no void API, unreliable webhooks, silent expiry fallbacks), an idempotent payment_incidents ledger, classification taxonomy, received_under_review outcome and cancellation_source provenance column were added, reconciling 46 historical transactions down to zero unaccounted charges.
A single shared SQL fragment (payable_until) defines the payment deadline for both UI countdown and server expiry. 342 backend tests include assertions on generated SQL text so a refactor cannot quietly drop an advisory lock or partial index.
Deep dive · Part B
Command acknowledgement was removed as a success signal after logs revealed the app reported success 0.85 s before a 10 s ZCL timeout. The controller now waits up to 12 seconds for the device to republish its own strictly newer state, and subscribes to the zigbee2mqtt bridge logging topic to surface silent radio faults.
Dual battery Zigbee lux sensors gate daytime floodlights with 15/20/30-minute freshness tiers and per-sensor calibration, failing closed (allowed: false) if no fresh reading is available.
os.replace.AI-assisted, human-accountable. AI tools contributed significantly to the implementation described here. The specification, the review of every change, the production operation and the accountability are mine.