6.4 Checkpoint: Writing Dirty Pages and the Recovery Starting Point
Chapter 5 said that dirty pages are written to disk later, independent of commits. When is that "later"? Dirty page writes cannot be postponed indefinitely, for two reasons.
First, the buffer pool fills up. As seen in Section 1.5, the buffer pool must evict old pages to load new ones, but a dirty page cannot be evicted before it is written to disk. The more dirty pages accumulate, the fewer pages can be evicted.
Second, recovery time grows. When a server goes down and comes back up, it must read the log and perform Redo. If it ran for a month without writing dirty pages to disk, a month of log would have to be reapplied in full. Recovery could take hours.
So the database performs a checkpoint periodically. A checkpoint writes the dirty pages up to that point to disk and leaves a mark in the log saying "all changes up to here have been reflected in the data pages." Thanks to this mark, recovery can start from the last checkpoint rather than from the beginning of the log, because changes before the checkpoint are already in the data pages and do not need to be applied again.
Figure 6-3. Checkpoint and the recovery range
block-beta
columns 8
lg<["log (append only, time ->)"]>(right):8
st["start"] a["already in data pages"]:3 cp["checkpoint"] b["redo / undo range"]:2 cr["crash"]
space n1["not needed for redo"]:3 space n2["read at recovery"]:2 space
space n3["reusable or archivable if no transaction was open at checkpoint"]:6 space
class lg,n1,n2,n3 plain
class st,cp meta
class a muted
class b hot
class cr warn
The horizontal band is the log accumulated from left to right. The checkpoint in the middle is the mark the last checkpoint left in the log, and the crash at the far right is where the failure occurred.
- Left of the checkpoint: Changes in this range are already reflected in the data pages, because the dirty pages were written to disk at the checkpoint. They are not read again during recovery. If no transaction was open at the checkpoint, they are not needed for Undo either, so the log space in this range can be reused or moved to archive.
- Right of the checkpoint: Changes after the checkpoint are not guaranteed to have been reflected in the data pages. Recovery reads only this range to perform Redo and Undo.
The more frequently checkpoints are taken, the shorter the right-hand range becomes and the faster recovery is.
A checkpoint is a trade-off between recovery time and normal-operation performance. Frequent checkpoints make recovery fast but require writing dirty pages often, which increases disk writes during normal operation. Infrequent checkpoints do the opposite. And when a checkpoint pours out many dirty pages at once, the disk fills with writes at that moment and other work slows down, causing a write storm. Many of the periodic spikes of latency seen in response-time graphs are due to checkpoints. For this reason, recent DBMSs by default do not write dirty pages all at once but spread the writes out little by little over the time until the next checkpoint.
A checkpoint has one more role. Section 4.5 said that the log cannot be deleted while it is needed for recovery. The log before a checkpoint is no longer needed for Redo (and, if no transaction was open at that moment, not for Undo either), so it can be reused or moved to archive. The checkpoint is, in effect, the moment log space is reclaimed. Its relationship to the phenomenon of the log disk filling up is covered in Chapter 10.