15 / 19 · Concurrency and Recovery
Logging and recovery
Follow a write-ahead log through commit and crash, then determine where recovery can start and which effects it can make visible.
A checkout commits the reservation of two packs. The modified data pages are still in the buffer pool when the machine loses power. The last copies on disk show five available packs and one reserved. To recover the committed state, the database needs a durable description of the changes that the data files have yet to receive.
Write the log ahead of the page#
A dirty page is a page in memory whose contents differ from the copy in the data file. Writing every dirty page before each commit would tie the latency of a small transaction to all the pages it touched. Write-ahead logging, or WAL, records enough information to reconstruct changes. The relevant log records must be durable before a modified data page can be written to durable storage. With synchronous local commit, the server also flushes WAL through the commit record before reporting success. It can write the data pages later. PostgreSQL 18, §§28.3–28.4
The page rule covers uncommitted changes as well. It permits a dirty page to be written before the transaction commits if the WAL required to recover it is already durable. The commit rule concerns what the server can promise the client. Together they allow the buffer manager to write pages on its own schedule while preserving the information recovery needs. Mohan et al., 1992, §1.1
For the following trace, assume regular logged PostgreSQL tables, synchronous local commit, durable storage that honors flushes, and a crash that loses memory while preserving the files that reached durable storage. We assign small illustrative log positions. Real PostgreSQL WAL locations are log sequence numbers (LSNs), which name positions in the WAL byte stream. The checkpoint’s redo position is 10. The last change record of the reservation is at 20, and its commit record is at 30. The two change records encode the stock deduction and the inserted reservation.
What reached durable storage
Find the outcome in the durable log#
In case A, WAL is durable through the change records at 20. The commit record did not survive. The server has not acknowledged a synchronous commit. PostgreSQL can redo the physical changes, but the transaction’s effects do not become visible as a committed reservation. The visible totals are five available and one reserved. An update can leave a version on a page without that version being visible to future transactions. PostgreSQL recovery replays page changes and transaction outcomes. Visibility follows the recovered outcome. PostgreSQL 18, §§13.1 and 28.4
In case B, WAL is durable through 30, but the data file may still have the old pages. The server can replay the missing changes and recover the commit. The visible totals become three available and three reserved. The commit can survive even if the crash occurred before the client received the response. A client that lost the connection at that boundary needs an operation identifier or another way to determine whether its request already took effect. PostgreSQL 18, §§28.3–28.4
Engines organize the treatment of incomplete transactions differently. In the ARIES algorithm, restart analysis finds the needed work, redo repeats history, and undo rolls back the actions of incomplete transactions. ARIES uses a page LSN to decide whether a logged update is already reflected in a page. A page that already contains the change does not need that update a second time. This prevents redo from subtracting the same two packs twice. PostgreSQL’s visibility rule for an incomplete transaction is a different implementation choice. Mohan et al., 1992, §§3 and 6
SQLite’s WAL mode illustrates another layout. It appends changed pages and commit information to its WAL. A checkpoint later transfers changes back to the main database file. Keeping track of the page state and the commit boundary serves the same recovery problem with a different file format. SQLite, WAL, §2
What a checkpoint bounds#
A checkpoint records a recovery boundary. PostgreSQL flushes the dirty pages required by a checkpoint and records a redo location from which crash recovery can start. Changes before that redo location are already reflected in the required data files. Some changes at or after it may also be on disk. The checkpoint record and the redo location it records need not be the same WAL position. PostgreSQL 18, §28.5
If a crash interrupts a page write, the page may contain parts of two versions. A small change record may then be insufficient to reconstruct the page. With full_page_writes enabled, PostgreSQL records a full page image on the first modification after a checkpoint. Recovery can rebuild the page from that image and apply later changes. Shorter checkpoint intervals can reduce redo work after a crash, while making page writes and full-page images more frequent. PostgreSQL 18, §§19.5.1 and 28.5
If PostgreSQL acknowledges an asynchronous commit, a crash can lose that recent transaction even though the client saw success. The durable log prefix still governs the recovered state. Disabling storage synchronization is a different change and can allow corruption after a system crash. The conclusions in the two-case trace rely on the stated durability settings. PostgreSQL 18, §28.4
Next#
Crash recovery assumes the required data files and WAL survive. It will also replay a committed deletion, even when the deletion was a mistake. Backups and restoration separates those cases and works out what a base backup and its archived WAL can recover.
References#
- PostgreSQL Global Development Group. PostgreSQL 18 documentation. MVCC, §13.1, WAL, §28.3, asynchronous commit, §28.4, checkpoints, §28.5 and WAL settings, §19.5.1 describe the page rule, durable commit, visibility and checkpoint behavior.
- C. Mohan, Don Haderle, Bruce Lindsay, Hamid Pirahesh and Peter Schwarz. ARIES: A Transaction Recovery Method Supporting Fine-Granularity Locking and Partial Rollbacks Using Write-Ahead Logging, 1992, §§1.1, 3 and 6. Gives the WAL rule, the page LSN and restart passes.
- SQLite Project. Write-Ahead Logging, §2. Describes SQLite’s WAL layout and checkpointing.