The AI that fixes outages by itself — Patent pending
Talk to us ↗
Free now Early adopters: become a Design Partner — Limited places available 

Kubernetes · Reliability

Writes stall for half an hour: recovery_prefetch, synchronous standbys and cheap cloud disks

A write burst, a brief replication drop, and every write stops for half an hour while the cluster reports healthy. How Postgres recovery_prefetch, synchronous standbys and IOPS-capped disks combine, how to confirm it, and which CloudNativePG changes actually help.

A burst of writes on the primary, a brief interruption to the standby's replication connection, and every write in the database stops for half an hour. Nothing crashed, and the cluster kept reporting itself healthy. A Postgres setting most clusters run without anyone choosing it, recovery_prefetch, plays the central part.

Jeremy Schneider described the failure on 20 October and published a reproduction on CloudNativePG that triggers it on demand. If you run Postgres on Kubernetes with synchronous replication and ordinary cloud disks, here is how it unfolds, how to confirm it, and which changes actually help.

How the outage unfolds

Since Postgres 15, recovery_prefetch defaults to try. A server in recovery, in practice a standby, looks ahead in the WAL it is replaying and asks the kernel for the data blocks it will need next. On fast local storage that can help some workloads. On a cloud volume with a fixed IOPS budget it can do the opposite, and four ordinary events line up into an outage:

  1. 01

    A write burst on the primary

    A large UPDATE, a bulk load or a VACUUM writes a lot of data in large sequential I/O. The primary's disk handles it comfortably.

  2. 02

    The standby turns it into random reads

    Prefetch requests each referenced block as a separate 8 KB read, including blocks whose contents arrive in the WAL anyway as full-page images. For about the same number of bytes, the standby issues many times as many I/O operations; the author puts the ceiling at sixteen times. In his run the primary sat at about 300 IOPS for five minutes while the standby was pinned at its 500 IOPS limit for forty.

  3. 03

    The replication connection drops

    A network or system hiccup interrupts the WAL receiver (the reproduction stands in for this by killing that process). The standby falls back to replaying the WAL already on its disk and in the archive, and it does not try streaming again until it has replayed all of it, which a saturated disk makes slow.

  4. 04

    Synchronous commit waits

    With synchronous replication, the primary cannot confirm a commit until a standby has it. No standby is streaming, so every write waits. In the reproduction, writes stopped for about 35 minutes; with a larger table it can be hours.

Six boxes show how the stall unfolds: a sequential write burst, the standby replaying WAL, prefetch turning it into random reads, the disk hitting its IOPS cap, replication falling behind, synchronous commit parking writes on the primary, and writes stalling while the cluster reads healthy.
How the stall unfolds, step by step.

The signature is a database that is up, accepting connections and answering reads, while every write hangs, shortly after a period of heavy writing.

Confirm it on your cluster

Three questions settle it: is prefetch on, is a standby behind or not streaming, and is its volume at its IOPS limit.

On the primary: who is behind, and by how much
SELECT application_name, state, sync_state, replay_lag,
       pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_bytes_behind
FROM pg_stat_replication;
On the standby: is prefetch on, and is it streaming
SHOW recovery_prefetch;
SELECT status, sender_host FROM pg_stat_wal_receiver;
-- no row, or a status other than 'streaming': not streaming
-- (reading status needs the pg_read_all_stats role)
SELECT prefetch, hit, skip_fpw, io_depth FROM pg_stat_recovery_prefetch;

With CloudNativePG, kubectl cnpg status <cluster> -n <namespace> shows the replication picture from the primary in one view. Then open the standby volume's metrics in your cloud console: read operations pinned at the provisioned limit while the bytes read stay modest is the amplification at work.

What to change

The reproduction tested each change on a fresh cluster, which makes the trade-offs unusually clear.

CloudNativePG Cluster spec
spec:
  instances: 3
  postgresql:
    parameters:
      recovery_prefetch: "off"
    synchronous:
      method: any
      number: 1
  • Turn prefetch off on clusters with IOPS-capped volumes. With recovery_prefetch off, the standby reconnected to streaming about 0.6 seconds after the interruption and writes never stopped. Full-page writes stay on, as they should. Measure replay after a write burst before and after; on fast local disks prefetch may still earn its place.
  • Run two standbys with quorum commit. method: any with number: 1 on three instances means one stalled standby no longer holds every commit, as long as the other is streaming.
  • Know what "available" means before relying on dataDurability: preferred. CloudNativePG treats a standby as available while its pod is ready, and the default readiness check still passes on a standby that has stopped streaming. In the reproduction, preferred on its own still blocked writes for 37 minutes.
  • Pair it with a streaming readiness probe, knowing the cost. In CloudNativePG 1.26 and later, a readiness probe of type streaming marks a standby that is not streaming as not ready, which takes it out of the synchronous quorum. With preferred as well, writes resumed after 15 seconds; the price was about 35 minutes of commits without a synchronous copy while the standby caught up. An optional maximumLag does the same for a standby that is too far behind; CloudNativePG's documentation asks for extreme caution with it, because a tight limit takes replicas out of the quorum for ordinary lag.
  • Size standby IOPS for replay, not just for reads. A standby that serves no queries can still need as many I/O operations as the primary, and more.

Why it hides until the bad day

Most of the time the standby has headroom, so the amplified reads cost nothing anyone notices. The outage needs two things at once, a write burst and a broken connection, and either alone is harmless. And while it happens, the standby pod stays ready and the cluster reports itself healthy, because nothing in the default checks asks whether the standby is streaming. That is why it surfaces as a one-off mystery rather than a pattern, and why it is worth checking before the coincidence happens to you.

Srenix checks CloudNativePG clusters on each diagnosis run: a cluster that leaves its healthy phase, or reports healthy while fewer instances are ready than it declares, is reported with the commands to inspect it. In this particular failure, with CloudNativePG's default probes, the cluster stays healthy and every instance ready, so Srenix sees nothing to report either. With the streaming readiness probe above, a stalled standby shows up as a missing ready instance, and Srenix reports it. Srenix reads cluster state only: it does not measure replication lag and does not change Postgres settings.

Credit, the full analysis and the reproduction: Jeremy Schneider, Postgres recovery_prefetch considered harmful and causing outages.

On-call should be quieter every week.

One Helm install. No telemetry leaves your cluster. No per-investigation surprises.