You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature]: Skip archive lookups that cannot succeed when the target of a planned switchover promotes #11638
Is there an existing issue already for this feature request/idea?
I have searched for an existing issue, and could not find anything. I believe this is a new feature request to be evaluated.
What problem is this feature going to solve? Why should it be added?
A planned switchover's promotion spends 2.7–3.4 s (4.9 s on a 250m-CPU instance) mostly in restore_command. PostgreSQL 18 runs with recovery_target_timeline=latest and CloudNativePG's restore_command. When the target promotes, it asks the archive for:
the next timeline's history file (rescanLatestTimeLine, findNewestTimeLine; xlogrecovery.c, timeline.c);
the parent history;
the next segment.
Each lookup is a barman-cloud restore through plugin-barman-cloud v0.15.0, 0.6–0.9 s each against Azure Blob, plus about 100 ms in walrestore after each miss. A promotion makes 3–6 of them. During a switchover clients are held for that time (Poolers paused).
Measured on 2026-10-09 across seven planned switchovers of two-instance Clusters on 1.30.1.
Describe the solution you'd like
On the target of a planned switchover, wal-restore answers "not found" at once for files the archive cannot hold, without asking the archive:
the next timeline's history;
segments past what was streamed.
This applies when status.targetPrimary is this pod, status.currentPrimary is not, and the WAL receiver stopped after the old primary's clean shutdown streamed everything. On a failover the archive may hold WAL the replica lacks, so the short cut would apply to the clean switchover alone.
Describe alternatives you've considered
wal.maxParallel prefetch with its end-of-wal-stream flag does not help here: the last segment is partial and not in the archive.
Emptying restore_command by hand is overwritten by the operator.
New measurements, and a safety finding that narrows what the short cut can be.
Setup. CloudNativePG 1.30.1 and plugin-barman-cloud v0.15.0 on Azure Blob. The cluster has 3 instances, PostgreSQL 16, recovery_target_timeline=latest and wal_retrieve_retry_interval=1s. The write gap was measured with a per-request probe that opens a new connection for every write, 5 staggered loops each running once a second. Switchover started with kubectl cnpg promote pg pg-2 on 2026-10-22.
Write gap: 6.42 s. A labeller of our own sets the primary role labels 0.15 s after currentPrimaryTimestamp, so #11637 is out of the picture here.
Lease and shutdown: 2.2 s. The target's Acquiring primary lease came at 34.298 and Acquired at 36.506. The old primary ended replication at 34.88.
Promotion: 3.5 s.received promote request came at 36.517, then redo done at 37.857, selected new timeline ID: 21 at 39.441 and restored log file "00000014.history" from archive at 40.006. The instance was ready at 40.049.
Plugin restore calls on the target. Times are the sidecar's Restoring WAL file. Each call takes about 0.5–0.65 s.
34.95 00000015.history not found (before the lease)
35.66 00000014000000150000008A not found
36.55 00000015.history not found
37.27 00000014000000150000008A not found (after the promote request)
37.90 00000014000000150000008A not found
38.65 00000015.history not found (findNewestTimeLine)
39.52 00000014.history restored
Every call after the promote request sits on the critical path, about 3.4 s in total.
On a customer database of 2 instances at 250m CPU, the handover held clients for 9.6–10 s. Lease and shutdown took 3.1 s, the archive lookups about 5 s and labelling 0.8 s. A client with a 10 s connect timeout failed during it.
Safety findings (from an independent review of a patch we wrote and did not ship):
Serving a history file that is in the target's own pg_wal is safe.
In archive recovery PostgreSQL asks restore_command for history files before it looks in pg_wal.
The replicas hold the history of the timeline they follow: here every instance had 00000015.history locally, streamed by the walreceiver.
So wal-restore on the target of a switchover can copy the local file and never ask the object store. That alone removes one lookup, about 0.5 s.
Answering "not found" at once for N+1.history (N = status.timelineID) is not safe in general.
instance_startup.go calls archiver.ArchiveAllReadyWALs on a former primary. That path goes through internalRun, which skips Run's currentPrimary check.
Failure sequence: a switchover target promotes to N+1 and is lost before currentPrimary is written. The cluster falls back to the former primary on N, and status.timelineID can go back to N. The lost instance returns and archives its stale N+1.history.
A later switchover target told "not found" would reuse N+1 where PostgreSQL would have picked N+2. The archive then holds two forks under one timeline ID.
The short cut would need that startup path to respect the same rule, or a single real lookup per switchover cached until promotion ends.
Repeated misses for the same file during one promotion (…8A three times, 00000015.history three times) look like the cheaper target.
A miss seen after the target holds the primary lease cannot change before the promotion ends in the planned case, because the former primary released the lease only after its shutdown and its archiving.
Caching such misses for the rest of the promotion would remove 2–4 lookups without any new assumption about what the archive cannot hold.
We have 1 implemented and unit-tested against v1.30.1, with no change of behaviour outside a planned switchover's target. We would be glad to open a PR for 1 (and 3, if you agree with the reasoning) once a maintainer assigns this issue.
Activity
pal-salih commentedon Oct 22, 2026
New measurements, and a safety finding that narrows what the short cut can be.
Setup. CloudNativePG 1.30.1 and plugin-barman-cloud v0.15.0 on Azure Blob. The cluster has 3 instances, PostgreSQL 16,
recovery_target_timeline=latestandwal_retrieve_retry_interval=1s. The write gap was measured with a per-request probe that opens a new connection for every write, 5 staggered loops each running once a second. Switchover started withkubectl cnpg promote pg pg-2on 2026-10-22.Write gap: 6.42 s. A labeller of our own sets the primary role labels 0.15 s after
currentPrimaryTimestamp, so #11637 is out of the picture here.Acquiring primary leasecame at 34.298 andAcquiredat 36.506. The old primary ended replication at 34.88.received promote requestcame at 36.517, thenredo doneat 37.857,selected new timeline ID: 21at 39.441 andrestored log file "00000014.history" from archiveat 40.006. The instance was ready at 40.049.Plugin restore calls on the target. Times are the sidecar's
Restoring WAL file. Each call takes about 0.5–0.65 s.Every call after the promote request sits on the critical path, about 3.4 s in total.
On a customer database of 2 instances at 250m CPU, the handover held clients for 9.6–10 s. Lease and shutdown took 3.1 s, the archive lookups about 5 s and labelling 0.8 s. A client with a 10 s connect timeout failed during it.
Safety findings (from an independent review of a patch we wrote and did not ship):
pg_walis safe.restore_commandfor history files before it looks inpg_wal.00000015.historylocally, streamed by the walreceiver.wal-restoreon the target of a switchover can copy the local file and never ask the object store. That alone removes one lookup, about 0.5 s.N+1.history(N =status.timelineID) is not safe in general.instance_startup.gocallsarchiver.ArchiveAllReadyWALson a former primary. That path goes throughinternalRun, which skipsRun'scurrentPrimarycheck.currentPrimaryis written. The cluster falls back to the former primary on N, andstatus.timelineIDcan go back to N. The lost instance returns and archives its staleN+1.history.…8Athree times,00000015.historythree times) look like the cheaper target.We have 1 implemented and unit-tested against v1.30.1, with no change of behaviour outside a planned switchover's target. We would be glad to open a PR for 1 (and 3, if you agree with the reasoning) once a maintainer assigns this issue.