Postgres recovery_prefetch can cause outages
Recorded: Oct. 22, 2026, 3 a.m.
| Original | Summarized |
Postgres recovery_prefetch Considered Harmful and Causing Outages – Ardent Performance Computing
Ardent Performance Computing Jeremy Schneider Search Search for: Go HomeAbout Old Links (Not Maintained) > Planet, PostgreSQL, Technical Posted by Jeremy ⋅ October 20, 2026 ⋅ 1 Comment Filed Under incident, operations, outage, performance, postgres, PostgreSQL, troubleshooting Another story of something I saw in the field which surprised me. Turns out recovery_prefetch can cause outages. Issuing unnecessary prefetches for blocks we’ve already sent a prefetch for is arguably a concern even if FPWs are off but the benefit of doing the prefetching almost certainly will outweight that and mean that finding a way to address it is something we could certainly do later as a future improvement. I wouldn’t have any issue with that. Just doesn’t seem as clear-cut to me when thinking about the FPW-enabled case. Ultimately, if you, Andres and Munro are all not concerned about it and no one else speaks up then I’m not going to pitch a fuss over it being committed, but, as you said above, it seemed like a good point to raise for everyone to consider. https://www.postgresql.org/message-id/20210214231815.GO27507@tamriel.snowman.net One customer complained that physical standby has significant replay lag (few hours) on modern powerful bare metal servers. According to output of “top”, startup process and few kernel workers utilized CPU, mostly in kernel space. The incident has been resolved by disabling recovery_prefetch after quick analysis of perf and strace outputs. Actual XLog prefetching code doesn’t consider that shared buffer may be loaded from WAL FPI rather than from disk and makes fadvise calls in such cases. https://www.postgresql.org/message-id/9bdb428a-fc8e-4801-8fff-1ad0bc6fa76b%40postgrespro.ru Start up postgres with synchronous replication to a standby, on small servers with inexpensive hard drives (for example: cloud disk with 500 IOPS, 2 CPUs, 8GB memory) It really is this easy to consistently reproduce this outage. https://github.com/ardentperf-labs/walreceiver-outage-repro Remember that with synchronous replication, we’re depending on at least one healthy standby server – otherwise we can’t take writes. (We want to guarantee durability to clients and ensure every transaction is on a remote machine before commit.) WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess, /*------- Fundamentally it looks like this: Basically, when a Postgres standby switches into WAL-restore mode, it will not go back to streaming until it’s caught up. Here are the disk status for the replica/standby during the same time period (40 minutes at the disk limit of 500 IOPS): These charts don’t illustrate it, but the number of bytes read from disk is about the same. There’s still the pause during UPDATE but we didn’t see the long pause after UPDATE completes. There is no burst of read IOPS, and the standby server basically stays caught up – applying the 5% update in less than 10 minutes. Share this: Share on Facebook (Opens in new window) Share on LinkedIn (Opens in new window) Share on Reddit (Opens in new window) Share on WhatsApp (Opens in new window) Share on Telegram (Opens in new window) Email a link to a friend (Opens in new window) Share on Threads (Opens in new window) Share on Mastodon (Opens in new window) Share on X (Opens in new window) Share on Bluesky (Opens in new window) Related
About Jeremy about.me/jeremy_schneider View all posts by Jeremy » « Agents Gone Awry on Postgres SBOMs: Start Over Discussion One thought on “Postgres recovery_prefetch Considered Harmful and Causing Outages” This reminds me of a related issue I hit with a customer on PostgreSQL 18.x. It’s a different trigger than recovery_prefetch, but it seems to involve the same logic around WaitForWALToBecomeAvailable and the standby state machine. Posted by Matt | October 21, 2026, 9:19 am Reply to this comment Leave a New Comment Cancel replyCommentNameEmailWebsite Turn on JavaScript to leave a comment. Δ Disclaimer This is my personal website. The views expressed here are mine alone and may not reflect the views of my employer.I am currently looking for consulting and/or contracting work in the USA around the oracle database ecosystem. about.me/jeremy_schneider (a) Email Updates Enter your email address to receive notifications of new posts by email. Email Address: Join 80 other subscribers Recent Posts Postgres recovery_prefetch Considered Harmful and Causing Outages Agents Gone Awry on Postgres SBOMs: Start Over Misc Learnings: SBOMs, Provenance and Attestations Why Postgres Breaks Kubernetes container_memory_working_set_bytes Metric Postgres Checkpoint Followup and Collation Visualization and Codex Luna Recent Comments Matt on Postgres recovery_prefetch Considered Harmful and Causing Outages Agents Gone Awry on Postgres SBOMs: Start Over – Ardent Performance Computing on Misc Learnings: SBOMs, Provenance and Attestations Postgres vs Kubernetes container_memory_working_set_bytes – Curated SQL on Why Postgres Breaks Kubernetes container_memory_working_set_bytes Metric Postgres Checkpoint Followup and Collation Visualization and Codex Luna | Ardent Performance Computing on Happiness Hint: Alarm on Checkpoint Time Alerting on Checkpoint Time in Postgres – Curated SQL on Happiness Hint: Alarm on Checkpoint Time Ardent Performance Computing Create a website or blog at WordPress.com
Comment Reblog Subscribe Subscribed Ardent Performance Computing Join 80 other subscribers Sign me up Have a WordPress.com account? Log in now. Privacy
Ardent Performance ComputingCopy shortlinkView post in ReaderManage subscriptionsSign upLog inReport this contentCollapse this bar Loading Comments... Write a Comment... Email (Required) Name (Required) Website %d |
Jeremy Schneider, an expert in data platform reliability, examines the detrimental effects of the `recovery_prefetch` setting in PostgreSQL, demonstrating how it can lead to severe outages in production environments. The article, rooted in field observations and technical investigations, presents a comprehensive analysis of historical incidents, the underlying mechanisms, and a reproducible scenario that illustrates the risk. The account begins with a prior discussion in February 2021, as recorded in Exhibit A, where Stephen Frost raised concerns about issuing unnecessary prefetches for blocks already targeted by prefetch requests, even when full-page writeback was disabled. Although this was considered a minor issue at the time, it was noted as a potential future improvement. The situation escalated in March 2025, as documented in Exhibit B by Michael Zhilin of Postgres Pro. A customer reported that a physical standby experienced hours of replay lag on modern bare metal servers with `recovery_prefetch` enabled. The incident was resolved by disabling `recovery_prefetch` after analysis of performance and strace outputs revealed that the XLog prefetching code did not account for shared buffers being loaded from WAL file pointers rather than disk, resulting in unnecessary fadvise calls. This incident highlights the tangible impact of the setting. Schneider explains the technical root cause: `recovery_prefetch` is designed to prefetch WAL data for improved performance, but it can inadvertently cause significant performance degradation. When the primary instance performs large sequential I/O operations, `recovery_prefetch` converts these into 8k random reads on replicas. For the same data volume, this amplifies IOPS by up to 16-fold on replicas, easily exhausting available IOPS and causing standby servers to lag substantially after write bursts. The critical combination is synchronous replication, which requires at least one healthy standby to accept writes. When a standby enters WAL-restore mode, it cannot resume streaming until it catches up, and the IOPS overload can prevent this catch-up, leading to prolonged outages. The author provides a clear reproduction recipe to demonstrate the outage. The steps involve starting PostgreSQL with synchronous replication on small, low-IOPS servers, initializing a database with pgbench, generating a burst of write activity by updating 5% of the data, and then killing the walreceiver process to simulate network or system instability. This consistently results in write hangs lasting over 30 minutes or hours. The author notes that the outage is reproducible, linking to a GitHub repository for the reproduction code. They also mention that VACUUM can produce write activity, making the outage a matter of "enough bad luck": a network disruption coinciding with write bursts. To understand the system hang, Schneider points to the standby state machine defined in `src/backend/access/transam/xlogrecovery.c`, particularly the function `WaitForWALToBecomeAvailable`. This function governs the standby's behavior: it reads from archive or pg_wal, checks for promotion triggers, reads from the primary via walreceiver, rescans timelines, and loops until caught up. When a standby switches to WAL-restore mode, it waits until all necessary data is applied before streaming resumes. The IOPS amplification from `recovery_prefetch` causes the standby to lag, preventing it from resuming streaming, and thus blocking all writes. Schneider demonstrates that disabling `recovery_prefetch` (while keeping full-page writeback enabled) eliminates the read IOPS burst. In the variation, the standby server remains caught up, applying the update in under 10 minutes, and no prolonged pause after the update occurs. This shows that the outage is directly tied to `recovery_prefetch`'s contribution to IOPS overhead. The author's conclusion leans toward disabling `recovery_prefetch` across the board, arguing that its benefits are dubious when full-page writeback is enabled by default. While `recovery_prefetch` may offer some performance advantages, the risk of outages outweighs them. The article also explores related considerations: the necessity of synchronous replication for data durability, the fragility of streaming replication connections, and the potential benefits of using two standbys with quorum commit (ANY 1) to increase robustness despite higher operational cost. Schneider also references a related issue involving `recovery_min_apply_delay`, where delayed replay can cause the walreceiver to fail to start appropriately after certain events, such as OS updates, highlighting the broader complexity of standby behavior. The article emphasizes that these insights are based on field observations and technical analysis, and the author's views are personal, as they are not necessarily representative of all PostgreSQL deployment contexts. |