Sub topic 1
Why a Database Needs a Log
Prerequisites
This chapter assumes you know basic SQL, transactions, and the ACID properties. You should also know that PostgreSQL stores table and index data in fixed-size pages, which are usually read from and written to disk as larger units.
The durability problem
A transaction is durable when PostgreSQL can guarantee that committed changes will survive a crash. A database server can crash while it is changing data pages in memory. The operating system or storage device can also stop during a write.
Writing every changed data page to durable storage at every commit would make transactions slow. A single transaction can change many pages, and those pages can be scattered across a large data directory. Waiting for all of them would create many random writes.
PostgreSQL solves this problem with a write-ahead log, or WAL. WAL is a sequential record of changes that PostgreSQL can use to reconstruct data after a crash.
| Problem | WAL-based solution |
|---|---|
| Data pages may still be in memory at commit | Flush the transaction's WAL records first |
| A crash may interrupt a data-page write | Replay WAL during recovery |
| Flushing every changed page is expensive | Write small, sequential log records |
What happens without a log
Suppose a transaction changes an account balance and inserts an audit row. PostgreSQL first changes copies of the affected pages in memory. The pages might remain there for some time before a background process writes them to disk.
If the server loses power before those pages reach storage, the committed transaction appears to have vanished. If only some pages reached storage, the database might contain the new balance but not the audit row. The database would then be internally inconsistent.
A database could avoid this by flushing every changed page before confirming COMMIT. That approach is correct but inefficient. It also does not completely solve interrupted page writes. A storage device might write only part of a page, leaving a torn page, which contains a mixture of old and new bytes.
The write-ahead idea
With WAL, PostgreSQL records enough information about a change in the log before it writes the changed data page. This is the write-ahead rule:
- Create WAL records describing a change.
- Flush the relevant WAL records to durable storage.
- Report
COMMITsuccess when the transaction's durability settings allow it. - Write changed data pages later.
- Replay the WAL if a crash occurs before those pages are written.
The log is sequential, so flushing it is usually cheaper than flushing many unrelated data pages. The data pages can be written later by background processes or during a checkpoint.
Why this design works
Recovery begins with the data pages that survived the crash. PostgreSQL then replays WAL records in order. A committed change is restored even when its data page was still dirty in memory at the time of the crash.
WAL also provides a stable ordering of changes. PostgreSQL identifies positions in the log with log sequence numbers, or LSNs. Later chapters use LSNs to connect transactions, checkpoints, recovery, replication, and archived backups.
The log does not remove every durability risk. Storage must honor flush requests, and PostgreSQL settings such as fsync and synchronous_commit affect when success is reported. These settings trade commit latency against the guarantees provided after a crash.
Exercise
Why does PostgreSQL flush WAL before writing the changed table page?
Show answer
WAL lets PostgreSQL recover the change after a crash. Flushing the sequential log first avoids flushing every changed data page at commit while preserving durability for acknowledged commits.