Course contentsShow
← Databases & SQL
Lesson 844 of 1,9538. PostgreSQL InternalsFree lesson

Full Page Writes and Checkpointing

You will tune checkpoint parameters to balance write amplification against recovery speed.

In this lesson, you will learn how to balance the trade-off between the overhead of writing full pages to your logs and the time it takes to recover from a database crash.

The Conflict: Recovery vs. Efficiency

When PostgreSQL updates a small piece of data, it doesn't just write that change to the disk; it first writes a Full Page Write (FPW) to the Write-Ahead Log (WAL). Because a page might be 8KB, writing the entire page every time it is modified during the first update after a checkpoint creates write amplification—extra disk I/O that slows down your system.

However, we need this because of the "torn page" problem: if the OS crashes while writing a partial page to the main data files, that page becomes corrupted. The FPW in the WAL allows the database to restore a clean copy of the page during recovery.

Tuning the Balance

A Checkpoint is the process where PostgreSQL flushes all modified "dirty" pages from memory (buffer cache) to the actual data files. Once the checkpoint is complete, the old WAL files are no longer needed for recovery, and the FPW process resets for each page.

You can tune this relationship using two primary settings:

  1. max_wal_size: Increasing this allows more time between checkpoints. This reduces the frequency of heavy disk I/O bursts but forces the database to keep more WAL files on disk.
  2. checkpoint_timeout: If you set this higher, you reduce the number of checkpoints, which lowers overall write amplification. The downside is that if a crash occurs, the database has to "replay" more WAL entries, leading to a longer recovery time.

The Trade-off:

  • Low checkpoint_timeout: Faster recovery, but higher I/O overhead due to frequent checkpoints and repeated FPWs.
  • High checkpoint_timeout: Better system performance, but potentially slower recovery after a crash.

If you find your system is "I/O bound," consider increasing your checkpoint_timeout. If you find your recovery time after a power failure is unacceptable, you must lower it to force more frequent, smaller checkpoints.

Key Takeaway: Tuning checkpoints is a tug-of-war where frequent checkpoints shorten recovery time but increase write amplification, while infrequent checkpoints improve daily performance at the cost of slower crash recovery.