All posts

Why PostgreSQL Recovery Prefetch Can Induce Recovery Outages

a bunch of blue wires connected to each other
Photo by Scott Rodgerson (https://unsplash.com/@scottrodgerson) on Unsplash (https://unsplash.com/photos/a-bunch-of-blue-wires-connected-to-each-other-PSpf_XgOM5w)

PostgreSQL includes a recovery_prefetch setting designed to accelerate write-ahead log (WAL) replay during crash recovery and replica catchup. By looking ahead in the WAL stream and issuing prefetch requests for referenced data blocks before the replay worker needs them, the database attempts to overlap I/O latency with CPU execution. However, reports highlighted by Ardent Performance and subsequent Hacker News discussion demonstrate that under high-throughput write workloads, this mechanism can backfire, causing severe I/O storms and memory thrashing that can prolong downtime.

The Mechanics of the Bottleneck

Under normal sequential replay, the recovery process requests data pages synchronously or in small, tightly bound batches. While this approach can stall on storage latency, it places a predictable, self-regulating load on the underlying I/O subsystem.

Recovery prefetch alters this behavior by scanning ahead in the WAL log to dispatch asynchronous prefetch requests (unverified: typically issued via posix_fadvise). In workloads characterized by high write throughput across large datasets, the WAL stream references a wide distribution of random disk blocks. When prefetching reads far ahead into these streams, several systemic issues emerge:

  1. I/O Queue Saturation: The volume of concurrent read requests floods the storage controller and kernel queues. On cloud block storage or shared SAN environments with fixed IOPS and bandwidth ceilings, this burst completely exhausts available I/O capacity, driving read latency up across the entire instance.
  2. Buffer and Page Cache Thrashing: As pages are brought into memory ahead of time, they consume operating system page cache or shared buffers. If the distance between the prefetched record and the actual replay position is large, or if memory pressure is acute, prefetched pages are evicted before WAL replay ever executes the redo step on them. The engine is then forced to issue a secondary, synchronous read for the same block, doubling the total I/O required.
  3. Cascading Failure During Failover: The failure state rarely surfaces during low-traffic testing. Instead, it manifests precisely when a node crashes under peak production traffic. When the database attempts to restart or a replica attempts to assume primary status, recovery stalls inside an I/O storm.

Operational Considerations for SREs

For systems processing high transaction volumes, database administrators and SREs should treat recovery prefetching as a workload-dependent optimization rather than an unambiguous performance improvement.

Teams validating disaster recovery procedures should evaluate the impact of this setting before unexpected failovers occur: