Transactions make concurrency correct, but machines crash. What recovers the system? An append-only log, plus a marker for where to start replaying.
The final post in the plan series.
1. What’s in the WAL
walog is a file storing redo log entries sequentially:
| Type | Written when |
|---|---|
| insert / delete / update | a transaction writes |
| flush | a transaction commits |
| checkpoint | everything before it is already applied to tables |
| create table / schema / use table | DDL and table switching |
Key convention: entries between two flushes belong to one transaction, so replay needs no extra transaction-boundary metadata.
2. Replay in two phases
Startup order: load metadata → load checkpoint → read redo log → replay.
Replay itself:
- Scan the WAL for a checkpoint entry, take its checkpoint block id;
- if it matches the block id in metadata, the log is already applied — replay ends;
- Otherwise apply entries one by one: insert/delete/update modify data, flush commits and starts the next transaction, create table creates, use table switches.
Two details:
- replay does not generate redo and does not trigger checkpoints;
- after a successful replay, the WAL is cleared.
3. When a checkpoint happens
TxnMgr decides at commit time, based on:
- only one checkpoint at a time;
- committed transactions are all GC’d;
- redo size + WAL size exceed a threshold;
- this transaction committed successfully.
A checkpoint doesn’t involve transactions, and no active transaction can affect the data while it runs — that’s the precondition for safety.
4. What a checkpoint writes
Persist layer by layer:
schema → table → row group → column data → column segment → vector
→ row/column versions → stats
In code, nested loops. Afterwards, log entries before the checkpoint entry are no longer needed.
5. Two engineering corollaries
- Commit latency jitters periodically: checkpoints trigger after large commits, and the heavy work sits on the commit path;
- Recovery time depends on checkpoint interval: longer intervals mean more log to replay.
So “how often to checkpoint” is a trade-off between recovery time and write amplification, with no single right answer.
6. Checklist
| Check | Expectation |
|---|---|
| One transaction between two flushes | yes |
| Replay finds the checkpoint first | yes |
| Replay stops generating redo | yes |
| Old logs can be dropped after checkpoint | yes |
7. Series summary
Six posts trace one chain: bind → plan → execute → store → transact → recover. Every layer shares one habit: define correctness first, then talk performance.
Series: Database kernel · Project page
Get the next post
New posts on database kernels and engineering.