Sub topic 10
Write-Ahead Logging and Crash Recovery
Assume you know basic SQL, primary keys, constraints, and how concurrent database sessions work. This chapter explains how a database keeps committed changes after a process crash or machine failure.
Why a database writes a log first
Write-ahead logging (WAL) is a recovery method. Before the database writes a changed data page to its table files, it writes a description of that change to a log on durable storage.
The log records enough information to repeat or reverse changes. The database can therefore recover even when a crash happens halfway through a transaction.
The central rule is:
- Write the change to the WAL.
- Flush the necessary WAL records to storage.
- Write changed table pages later.
- Report
COMMITonly after the commit record is safely flushed.
The table page may still be in memory when the client receives a successful commit. The WAL is the durable record that allows the database to rebuild that page after a crash.
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric(12, 2) NOT NULL
);
INSERT INTO accounts (id, balance) VALUES (1, 100.00), (2, 50.00);
BEGIN;
UPDATE accounts SET balance = balance - 30 WHERE id = 1;
UPDATE accounts SET balance = balance + 30 WHERE id = 2;
COMMIT;The database writes log records for both updates and then a commit record. If the server crashes before the changed table pages reach disk, recovery can replay the WAL and restore both balances.
What happens during recovery
After an unclean shutdown, the database examines the WAL from a known checkpoint.
A checkpoint is a point at which the database records enough information about dirty pages and active transactions to shorten future recovery. It does not remove the need for WAL. It gives recovery a safer starting point.
Recovery commonly has these steps:
- Read WAL records after the checkpoint.
- Redo changes that might not have reached the table files.
- Keep changes from committed transactions.
- Remove, undo, or ignore changes from transactions that had not committed.
- Make the database available for new connections.
The exact undo process differs between database engines. Some use undo records. MVCC systems may leave row versions and mark uncommitted work as invisible or roll it back later.
Durability has limits
WAL protects against many process and operating-system crashes, but durability depends on the storage stack. If a database or operating system falsely reports that data reached stable storage, a power loss can still lose committed work. Databases often use filesystem flush operations, battery-backed caches, or replicated storage to reduce this risk.
WAL is also useful for replication. A replica can receive and replay WAL records instead of receiving every changed table page. However, WAL is not a complete backup. It normally depends on a base backup and a usable sequence of log records.
Practical design rule
Keep transactions short enough to limit lock time and recovery work. Do not treat a client-side success message as proof that a transaction committed unless the database client received the commit acknowledgment.
Exercise
Why must a database flush the WAL commit record before confirming COMMIT to the client?
Show answer
If the server crashes before the commit record is durable, recovery cannot reliably distinguish the transaction from work that should be discarded. Confirming first could violate durability.