Highlights

Contents

Part XII
12

The complete reservation query

Asha’s receipt depends on more than finding a row. Several mechanisms must agree before success is acknowledged. Follow the claim through planning, storage, concurrency, and durable recording.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Database model2Keys and ER3Algebra and SQL4Dependencies5Storage and indexes6Query execution7Transactions8Concurrency control9Recovery and engines10Distributed databases11Application design12The complete query

#Section 57A reservation from SQL to durable commit

Asha’s booking crosses validation, SQL planning, execution, and concurrency rules before commit. No single fast lookup establishes the whole outcome.

The access path traverses index structure and locates the row. A buffer hit can serve a logical page visit without physical device I/O.

ONE RESERVATION TOUCHES AN INDEX, A ROW, A LOCK AND THE LOGONE RESERVATION TOUCHES AN INDEX, A ROW, A LOCK AND THE LOGreservationseat A7index leafA7locatortuplelock + recheckbooking + WAL commitacknowledgement follows the declared durable transaction boundary
Figure 1.69One reservation passes from parsing and planning to page access, concurrency control and durable commit. Orange marks the atomic seat-state check after competing access is controlled.

A conditional update can claim the seat only while it is available. The transaction records a booking and publication intent only if it obtains that claim. The engine preserves required WAL before the associated changed page reaches persistent storage, and the configured durable commit boundary precedes a success response. Pages can remain dirty in the buffer pool after a no-force commit.

sql
BEGIN;
UPDATE show_seats
SET state = 'held', version = version + 1
WHERE show_id = :show AND seat_id = :seat AND state = 'free'
RETURNING show_id, seat_id, version;
COMMIT;

This short statement shows the claim mechanism, not the complete application transaction. The handler must inspect the returned row count and insert the booking and outbox in the same transaction before commit. If no row is returned, it cannot create a successful booking. The actual identifier types and SQL parameter syntax depend on the driver.

Reconcile a lost response with the same stable submission identity. Schema, access, concurrency, and durable receipt each support a different part of the successful booking.

#Section 58Two buyers and one seat

Bharat’s browser may still show free while Asha’s claim is open. Recheck the admitted state at the atomic write boundary.

Under a row-locking path, Asha’s update obtains the needed lock. Bharat’s conflicting update waits or fails under the configured policy. After Asha commits, Bharat must evaluate the current permitted row state under the engine’s isolation behavior. At PostgreSQL Read Committed, a conflicting update can wait and then re-evaluate its condition on the updated row; an available-state condition cannot keep winning merely because Bharat’s original view was free.

ADA LOCKS SEAT A7 FIRST; BO WAITS, RECHECKS, AND UPDATES ZERO ROWSADA LOCKS SEAT A7 FIRST; BO WAITS, RECHECKS, AND UPDATES ZERO ROWSA7freeheld by AdaAdaBolocklock?waits for AdaAda: claim, COMMITWHERE state = 'free'UPDATE 0the recheck after waiting is what stops a second booking
Figure 1.70Asha claims and commits first. Orange marks Bharat’s resumed check finding held state and returning no claimed row, rather than creating another successful booking.

An optimistic claim compares and increments the version atomically. The stale buyer then conflicts. A separate version read followed by an unconditional write would recreate the race.

A uniqueness constraint is a final enforcement boundary when the schema models active seat claims as unique rows. The application still needs to interpret the conflict and handle expiration, cancellation and confirmation under one allowed state machine. Deleting and recreating claim rows casually can let a late payment confirm the wrong booking generation.

The interview answer should identify the invariant first, one valid active owner per show-seat. Then name the atomic conditional operation or lock, the database constraint where applicable, and the retry identity. Isolation labels alone do not prove that a particular application’s sequence implements its invariant. PostgreSQL’s locking reference describes the concrete conflicting row-lock behavior used in this trace.

#Section 59Storage overhead and model assumptions

The storage figure uses a declared toy layout. It illustrates why partially filled pages and index levels cost space, rather than defining PostgreSQL’s real layout.

Page headers and slots consume space, so capacity admits only whole records that fit. The last partially filled page still occupies a complete allocation.

Packed page arithmetic describes a teaching layout. Live occupancy changes with versions, variable records, and reserved free space.

Index leaves and routing pages add their own storage. Their cost is separate from heap rows and must follow the chosen layout.

26 HEAP PAGES + 5 INDEX PAGES = 31 PAGES = 126,976 BYTES26 HEAP PAGES + 5 INDEX PAGES = 31 PAGES = 126,976 BYTESheap 26 × 4096 = 106,496index 5 × 4096 = 20,480one copy126,976bytes× 3 = 380,928toy layout: every page full size, no free-space map or metadata pages
Figure 1.71Illustrative inventory heap and index totals reconcile to 126,976 bytes for one copy. Orange marks that sum; three identical copies occupy 380,928 bytes under the same toy layout.

Replicas copy the modeled footprint, but WAL, retained history, backups, and metadata add other costs. Estimate the real engine with its own physical inputs.

#Section 60Responsibilities of every database component

Asha’s held-seat receipt requires several mechanisms to agree. An index locates data, concurrency control protects the claim, and durable recording preserves it.

Component Contribution Failure it does not solve alone
Schema and constraints Valid identities and references Race in an unchecked application sequence
Relational query and SQL State the required row computation Storage and scheduling cost
Optimizer Choose an estimated access plan Wrong or stale selectivity evidence
Index Locate ordered keys efficiently Permission and tuple visibility
Buffer manager Reuse pages with pin and dirty rules Durable commit acknowledgement
Locks and atomic predicates Coordinate competing effects Replay of an external command
MVCC and snapshots Select allowed row versions Reclamation without maintenance
WAL and recovery Restore the durable state contract Loss outside that configured boundary
Replication Maintain additional state copies Promotion without valid authority
Outbox and identity Recover publication and repeated attempts Atomic remote effects without a protocol
A DATABASE ENGINE CUT OPEN: SIX PARTS, SIX DIFFERENT PROMISESA DATABASE ENGINE CUT OPEN: SIX PARTS, SIX DIFFERENT PROMISESschema + constraintsonly legal rows existplanner + executorfind and compute the rowsindex + buffer managerlocate keys, reuse pageslocks + visibilitywho sees and changes whatWAL + recoveryacknowledged work survives a crashreplication + ownershipcopies and one valid writerSQL indisk
Figure 1.72Component jobs kept distinct. Orange marks WAL and recovery, which preserve acknowledged state while the other mechanisms establish what that state is allowed to contain.

Logical rows, resident pages, and retained physical versions are different counts. One booking can add a logical header while changing existing inventory and producing additional physical history.

The study loop is to redraw the reservation path and change one assumption. Remove the index, hold an old snapshot, lose power after durable commit, let the response disappear, or promote a lagging follower. Each change should lead to a named mechanism and a permitted outcome rather than another vague claim that the database is safe.

This chapter did not implement a SQL optimizer, a full consensus protocol, storage-device guarantees or a payment provider. Computer Networks follows the packets that carry the request. Operating Systems follows the process, memory and I/O machinery on which the database depends. The database boundary is now explicit enough to know what those next chapters must supply.