A crash happens while a page is being written, and only part of it reaches disk. How does a B-tree engine cope?
Sketch your own answer first, then compare it with the one below.
A strong answer
A torn page cannot be repaired by replaying a log record that says "change bytes 40 to 80", because the base page is no longer a valid page.
There are two common defences. One is to write a full image of a page into the log the first time it changes after a checkpoint, so recovery can restore the whole page and then apply later records. The other is to write pages first to a separate area and then copy them to their place, so a good copy always exists. Page checksums let the engine detect the damage in the first place.
The trade-off is more bytes written. This is one more reason write amplification in a B-tree is higher than the size of the record suggests.
Sources
- PostgreSQL Global Development Group (2024). Write-Ahead Logging (WAL).
- Goetz Graefe (2011). Modern B-Tree Techniques.