Chapter Six · Data Access
Data Access
The engine keeps the truth; the service decides how it is reached. Six topics on the pool, the transaction, the mapper, the row two buyers reached for at once, the migration that ran during on-sale, and the replica that showed an order as missing for a second. This is the chapter that closes the first wound: seat 14C, sold twice.
Seat 14C was sold twice because hold_seat read the seat's status in one statement and updated it in the next, and under Read Committed two requests four milliseconds apart both read available. Every line was correct. The read was true when it ran, the write was valid when it ran, and nothing bound the two together. Topic 34 walks the six steps of that race and closes it three ways: a row lock before the decision, a version column checked by the write, and a unique constraint that refuses the second hold whatever the code does.
The rest of the chapter is what Marek found on the way there. The connection pool was sized at 100 per process against a database that allows 200, and nobody had multiplied by the eight processes. A transaction was held open across the 3-second Payrail call, with the seat row locked and the connection pinned for the length of a network round trip. The list-orders endpoint issued 51 statements to return 50 orders, one per order for its tickets, and the mapper had done exactly what it was told. A migration adding a column to seats queued for a lock for four minutes during on-sale and broke the old code when it finally ran. And the organizer reports on pg-replica-a showed a buyer's order as missing for a second after it was placed, because the replica was that second behind.
Each of those is the application's side of a boundary that PostgreSQL Deep Dive owns from the engine's side. This book does not teach the lock table or the WAL; it teaches where the transaction begins, which pool a read names, which ALTER TABLE is instant and which is the outage, and why a read followed by a write is never one operation until something makes it one.