A ticket shop is a peaceful place when you’re the only customer. Then a missing seat number makes the whole seating chart disappear. Tickets go on sale two hours late. And the index meant to help checkout makes checkout wait.
Here are five small experiments you can run in a scratch database. The first seating chart has just three seats. That’s plenty for things to go sideways.
The site says sold out with two seats left
Picture a tiny venue with three seats. Someone books seat 2. Another customer starts a booking and leaves before picking a seat. The database saves that unfinished booking with NULL as the seat number. Seats 1 and 3 are still free.
The app uses NOT IN to ask whether seat 1 is outside the list of booked seats. PostgreSQL can rule out seat 2. It has no number to compare with for the unfinished booking, so the answer is unknown. A WHERE filter only keeps rows where the answer is true. Seat 1 disappears, and the same thing happens to seat 3. PostgreSQL subquery rules
Try both checks side by side. In this query, holds is the list of bookings.
WITH seats(id) AS (VALUES (1), (2), (3)),
holds(seat_id) AS (VALUES (2), (NULL::integer))
SELECT id,
id NOT IN (SELECT seat_id FROM holds) AS not_in,
NOT EXISTS (
SELECT 1 FROM holds WHERE holds.seat_id = seats.id
) AS not_exists
FROM seats
ORDER BY id; id | not_in | not_exists
----+--------+------------
1 | NULL | t
2 | f | f
3 | NULL | tHere, t means true and f means false. NOT EXISTS looks for a booking with the same seat number as the seat you’re checking. Only seat 2 has a match. Use the NOT EXISTS check in WHERE and seats 1 and 3 show up again. You can reopen the box office.
If every hold needs a seat number, make seat_id NOT NULL. Save unfinished bookings separately and create a hold once the buyer picks a seat. PostgreSQL not-null constraints
Why tests miss it — Every booking in the test data already has a seat number.
The clock lost its two-hour offset
Your ticket sale starts at 09:00 in a place that’s two hours ahead of UTC. You send the offset along with the time. PostgreSQL will use it if the value has the right type.
SET TIME ZONE 'UTC';
SELECT TIMESTAMP '2026-11-28 09:00:00+02' AS clock_time,
TIMESTAMPTZ '2026-11-28 09:00:00+02' AS sale_starts_at;clock_time
2026-11-28 09:00:00
sale_starts_at
2026-11-28 07:00:00+00The first column says 09:00:00. Plain timestamp drops the +02. If your app reads that value as UTC, the sale opens two hours late. The second column says 07:00:00+00, which is 09:00 at the venue. Use timestamptz for the moment a sale starts or a payment happens. PostgreSQL displays it in the time zone of the current connection. PostgreSQL date and time types
For a concert booked six months ahead, keep the venue’s local date and time and a named zone like Europe/Bucharest if the show needs to stay at the same local time. A named zone accounts for daylight saving, and you can recalculate the UTC time if the rules change. A fixed +02 always means two hours ahead of UTC. PostgreSQL time zone rules
Why tests miss it — If every test timestamp uses +00, dropping the offset leaves the same clock time.
Page two has a familiar face
The sales screen shows the newest ticket IDs first, two per page. You see 104 and 103. Somebody buys ticket 105 before you click Next.
CREATE TEMP TABLE sales (id integer PRIMARY KEY);
INSERT INTO sales VALUES (101), (102), (103), (104);
SELECT id FROM sales ORDER BY id DESC LIMIT 2; -- 104, 103
INSERT INTO sales VALUES (105);
SELECT id FROM sales ORDER BY id DESC LIMIT 2 OFFSET 2; -- 103, 102
SELECT id FROM sales WHERE id < 103 ORDER BY id DESC LIMIT 2; -- 102, 101Ticket 103 just got a second appearance. OFFSET 2 skips the first two rows as they stand now, which are 105 and 104. The second query starts after the last ticket you actually saw. That’s cursor pagination. If you sort newest first by created_at DESC, id DESC, pass the last ticket’s time as $1 and its ID as $2. The ID breaks ties when two tickets have the same timestamp. PostgreSQL LIMIT and OFFSET · Row comparisons
WHERE (created_at, id) < ($1, $2)Why tests miss it — Page two looks fine when nobody adds a ticket between requests.
The foreign key skips an index
A foreign key makes sure each ticket belongs to a show that exists. Peek at the indexes PostgreSQL created on the tickets table.
CREATE TABLE shows (id integer PRIMARY KEY);
INSERT INTO shows VALUES (42);
CREATE TABLE tickets (
id integer PRIMARY KEY,
show_id integer NOT NULL REFERENCES shows(id)
);
SELECT indexname FROM pg_indexes
WHERE schemaname = current_schema() AND tablename = 'tickets';You get tickets_pkey, which covers the ticket ID. When you sell a ticket, PostgreSQL can look up show 42 using the primary key on shows. When you delete a show, PostgreSQL has to check in the other direction for tickets that still refer to it. Without a useful index on tickets.show_id, that can mean scanning the tickets table. PostgreSQL foreign keys
Check your existing indexes before adding one. An index starting with show_id can already cover that lookup. If you do need a new index on a live table, the way you build it matters.
Why tests miss it — Scanning every ticket feels instant when there are only ten.
Your speed-up makes checkout wait
Open two connections to the scratch database. In connection A, run this and leave the transaction open.
BEGIN;
CREATE INDEX tickets_show_id_idx ON tickets (show_id);A has already built the index. In connection B, try selling a ticket.
SET lock_timeout = '1s';
INSERT INTO tickets VALUES (1, 42);The sale times out waiting for a lock. A regular index build lets reads through and makes writes wait. Leaving A’s transaction open gives you time to see the lock on a tiny table. A standalone CREATE INDEX still blocks writes for the whole build. Roll back A with ROLLBACK; so you can try the other kind of build. PostgreSQL CREATE INDEX
CREATE INDEX CONCURRENTLY tickets_show_id_idx ON tickets (show_id);The concurrent build allows ordinary writes while it works. In the PostgreSQL 18.3 test, a sale went through while the build was still waiting on another transaction. Run the command on its own, outside a transaction block. Some migration tools wrap their commands in a transaction, so check yours. Once the build succeeds, retry the sale in B. PostgreSQL’s concurrent build notes
If the build fails and \d tickets shows tickets_show_id_idx as INVALID, drop that index before retrying. Run the drop outside a transaction too. PostgreSQL DROP INDEX
DROP INDEX CONCURRENTLY tickets_show_id_idx;Why tests miss it — An idle test database has nobody trying to buy a ticket during the migration.
The next time a database change sails through review, try the awkward case. Leave a value blank, add a row between requests, or open a second connection while your migration runs. Three seats are enough to practice for opening night.
