Repository navigation
[Feature request]: wait for replica to catch up on WAL before triggering promotion/switchover #11153
Replies: 1 comment
|
if it's a user-triggered promote, then i think the promote needs to happen immediately
this case is interesting, it's a promote that's happening as part of a config change, or some other maint operation which requires restarts of the database (and in this case, moving pods to new nodes) i feel a bit leery of code that tries to monitor lag, because there isn't really a guarantee of catch-up. would need to reason through this very carefully (with a very strong understand of postgres internals and what large production workloads look like) so that we don't end up just making maintenance take longer across the board. if we slow down the maintenance, and if a maintenance is rolling across a fleet with hundreds or thousands of database, that is going to have a multiplying effect which could make it more difficult to complete maint within the defined window - it could ultimately slow down the rate at which nodes can be cordoned and drained |
Uh oh!
There was an error while loading. Please reload this page.
Summary
During CNPG cluster rolling updates, we've repeatedly observed the operator triggering replica promotion (
pg_ctl promote) as soon as the replica starts streaming WAL from the primary — without waiting for it to actually catch up. When the replica is significantly behind (e.g. because a slow PVC unmount/remount kept it down for several minutes during the pod restart),pg_ctl promotethen blocks for minutes while PostgreSQL replays the WAL backlog, turning what should be a sub-second switchover into a multi-minute outage.We've seen this across multiple production clusters on
GKE,AKS, andEKS. The two most severe cases had promotion times of 6m15s and 7m28s instead of the usual ~1s; a third case saw ~37s.What's happening
FailedMount/Multi-Attachevents observed on bothGKE/GCE-PDandEKS/EBS).pg_ctl promotethen blocks while PostgreSQL replays all the WAL that piled up during steps 3–5, extending the downtime by minutes.So the observed promotion time ends up being a direct function of how long the PVC remount took (a separate, unrelated infra issue we're tracking on our side), rather than of any inherent cost of promotion itself.
Detailed timeline — worst observed case, June 10, 2026
Kubernetes cluster (GKE / GCE-PD).
pg_ctl_promotetook 448 seconds (7m28s); the replica had been down for ~4 minutes due to a slow PVC remount before that.pg_ctl60s timeoutFailedMounton the new replica pod — "An operation with the given Volume ID already exists"pg_ctl_promote— blocks while replaying the remaining WAL backlogService interruption: ~8 minutes, measured from switchover trigger to database-ready.
Two other clusters we monitored showed the same pattern (6m15s and 37s promotion times), correlating directly with how long the PVC remount took and how far behind the replica was when the switchover was triggered.
Proposed fix
handlePromotion(internal/management/controller/instance_controller.go#L1291) currently does this:waitForWalReceiverDownonly guards the failover path (making sure the WAL receiver is down before promoting). There's no equivalent guard on the planned switchover path to ensure the replica has actually caught up beforePromoteAndWaitis invoked — today the operator only checks that streaming has started, not that replication lag is near zero.We'd like to propose adding a catch-up wait inside
handlePromotion, before callingPromoteAndWait: poll the instance's replication status (e.g. compare pg_last_wal_replay_lsn() against the primary's current LSN, or watch pg_stat_wal_receiver lag) and block — with a bounded timeout — until the lag drops below a safe threshold, only then proceed to promote:This decouples the visible promotion/downtime window from however long the replica took to get healthy again, and keeps pg_ctl promote itself fast, since there would be little to no WAL left to replay by the time it's actually called.
Questions for maintainers
Co-authored with Claude
All reactions