Skip to content

feat(instance-manager): let standbys on the WAL archive retry streaming - #11868

Merged
leonardoce merged 9 commits into
mainfrom
dev/11808
Nov 12, 2026
Merged

leonardoce merged 9 commits into
mainfrom
dev/11808

Conversation

@armru

@armru armru commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

PostgreSQL moves a standby from the WAL archive to streaming only when restore_command fails. While the archive keeps up, that never happens: the standby stays on the archive with no WAL receiver, an inactive slot and no part in synchronous replication.

After 30 seconds of continuous restores, wal-restore now fails every WAL segment request for 5 seconds, before calling any plugin. A single failure is not enough since PostgreSQL 15, which retries a restore that fails while prefetching. If the standby is back on the archive soon after, the interval doubles, up to 5 minutes.

The failure is never forced in rewind mode, outside standby mode, without a streaming source, or while a promotion is pending (a promote file, or the operator promoting the instance): the promotion would then skip the WAL still in the archive. Nor is it forced for a WAL file already prefetched into the spool, which would stay there once the standby streams.

When the source is unreachable, each try pauses the replay until the connection fails: about 6 seconds if refused, up to the TCP connect timeout if the host does not answer and connect_timeout is not set.

The other instances of a replica cluster stream from the designated primary. Before 18.6, 17.11, 16.15, 15.19 and 14.24, an upstream bug can make that connection fail with "requested starting point ... is ahead of the WAL flush position", and PostgreSQL goes back to the archive, as it does today.

The Wal-restore in parallel e2e runs wal-restore by hand on a streaming standby, up to 16 seconds after the first restore of the standby on KIND. It now disables the retry, which a slower run would trip.

The counter proposed in the issue is left out, to avoid adding a metric to the public API.

Closes #11808

@armru
armru requested review from a team, NiccoloFei and litaocdl as code owners November 6, 2026 14:59
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30 labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@armru

armru commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

@armru

armru commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

@gbartolini gbartolini added do not backport This PR must not be backported - it will be in the next minor release and removed backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30 labels Nov 6, 2026
@jmealo

jmealo commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Tested this on our staging AKS cluster today, with and without the patch.

Short version: it fixes the stuck-on-archive standby for us, including the stuck waiting walreceiver after a timeline switch we reported in #10419, and switchover time is unchanged.

Build: release-1.30 @ f49688d + cherry-pick of this PR's head 0acf666 (applied cleanly, unit tests in internal/cmd/manager/walrestore pass). Rolled out to the operator with in-place instance manager updates, no PostgreSQL restarts.

Stack: CNPG 1.30.1, plugin-barman-cloud v0.15.1 (isWALArchiver: true, Azure Blob via connection string, wal.maxParallel: 32), PostgreSQL 18.3 (arm64), HA slots enabled on the source.

Rig, modelled on the PR's e2e fixture: a 2-instance source cluster plus a 1-instance replica cluster bootstrapped with pg_basebackup, replica.minApplyDelay: 1m, WAL from the plugin archive. Load: pgbench (4 clients, ~550–830 tps) plus INSERT ...; SELECT pg_switch_wal() once per second through the -rw service, so the archive always had the next segment.

Without the patch (stock 1.30.1)

With the patch (same clusters, same load)

Scenario Observed
Already-stuck replica cluster, no restart, right after the in-place manager upgrade 21:15:52Z Failing the WAL restore on purpose to let PostgreSQL try streaming replication ×3 (00000003000000050000006C, ...6D), 21:15:52Z restarted WAL streaming at 5/6D000000 on timeline 3
Replica cluster pod deleted (the e2e scenario) 21:17:21Z entering standby mode → 21:18:29Z forced failures → 21:18:30Z started streaming WAL from primary at 5/72000000 on timeline 3 (≈60 s)
Source switchover TL 3 → 4 promote 21:27:18Z, source healthy 21:27:42Z (24 s, identical to the stock run); demoted standby streaming at 21:27:46Z; replica cluster new target timeline is 4 → restarted WAL streaming at 5/83000000 on timeline 4 at 21:28:23Z (46 s after the promote)

Steady state afterwards: no forced failures logged while standbys were streaming, and the state file under /controller stayed idle, so the retry does not fire on a healthy standby.

One data point for the red e2e: in all three patched runs the gap between the first forced failure and started/restarted WAL streaming was under 100 ms, and the first failure came ~60 s after entering standby mode. So the 120 s Eventually should be enough once the standby is in standby mode; if the job logs show the forced-failure line but no started streaming, I'd look at whether the source can still serve the requested start LSN (no slot on the replica-cluster connection in the fixture, wal_keep_size 512 MB default, one 16 MB segment per second).

Happy to re-run with different settings if useful. We're keeping this build on staging for a soak; details and raw observation logs are in our tracker if anyone wants them.

@armru

armru commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Thanks for testing, and you were right about the red e2e. At one WAL file per second, a one-minute delay put the replica about 55 files behind, while the source keeps only 32 after a checkpoint, so streaming failed with requested WAL segment ... has already been removed. 261cfaf lowers minApplyDelay to 20s, and raises both volumes to 2Gi because pg_wal reaches about 1.1GB.

@armru

armru commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@armru

armru commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@gbartolini

Copy link
Copy Markdown
Contributor

/test

@github-actions

Copy link
Copy Markdown
Contributor

@gbartolini
gbartolini force-pushed the dev/11808 branch 2 times, most recently from 621aac3 to 1cfe7c5 Compare November 11, 2026 11:09
armru and others added 8 commits November 12, 2026 10:42
PostgreSQL moves a standby from the WAL archive to streaming replication
only when restore_command fails. While the archive keeps up with the
standby, that never happens, and the standby can stay on archive
recovery indefinitely, with no WAL receiver, an inactive replication
slot and no role in synchronous replication.

After 30 seconds of continuous WAL restores, wal-restore now fails
every request for a WAL segment for 5 seconds, before calling any
plugin. A single failure is not enough from PostgreSQL 15 on, as a
restore that fails while prefetching WAL is retried before PostgreSQL
considers streaming. When the standby is back on the archive soon
after, streaming did not hold and the interval doubles, up to 5
minutes.

The failure is never forced in rewind mode, outside standby mode,
without a streaming source, on the instance the operator is promoting,
or while a promotion is pending: a pending pg_ctl promote waits for a
failed restore, and would then end the recovery before replaying the
WAL still in the archive. Nor is it forced for a WAL file already
prefetched into the spool, which would stay there once the standby
streams.

Closes #11808

Signed-off-by: Armando Ruocco <[email protected]>
…ention

The replica cluster of the streaming retry e2e replayed the WAL one
minute after the source wrote it, one WAL file per second. When it
tried streaming, it asked the source for a WAL file about 55 files
behind, while the source, with no replication slot for it and the
default wal_keep_size of 512MB, keeps only 32 of them after a
checkpoint. Depending on when the checkpoint ended, streaming failed,
and the replica went back to the archive and waited longer before the
next try.

A 20-second delay keeps the replica about 17 WAL files behind: within
the retention of the source, and still behind the archive.

At this rate, pg_wal grows to about 1.1GB on both clusters, more than
their 1Gi volumes hold where the size is enforced: they now get 2Gi.

Signed-off-by: Armando Ruocco <[email protected]>
…ource

The source of the streaming retry e2e has a single instance, so no
standby clone completes a WAL file after the bootstrap. On PostgreSQL
14 the first WAL file stays open until archive_timeout, five minutes by
default, and the ContinuousArchiving condition never appeared within
the 180 seconds the e2e waits for it.

The e2e now switches a WAL file and waits for it in the object store
before checking the condition, as the other single-instance plugin
e2e already does.

Signed-off-by: Armando Ruocco <[email protected]>
Tell whether streaming replication held by the WAL file the standby asks
for once the forced failures end: a different file means the standby moved
on by streaming, so the interval starts over; the same file means streaming
did not hold, so the interval doubles. The idle gap is kept as a fallback.

Previously a standby that streamed for just under the idle gap was
penalized like one that failed to stream at all.

Signed-off-by: Gabriele Bartolini <[email protected]>
…ry_interval

The forced failures last a fixed 5 seconds, the default value of
wal_retrieve_retry_interval, which suggests a link between the two.
There is none: PostgreSQL retries a failed restore right away, and the
parameter only delays the return to the archive when streaming
replication cannot start.

Signed-off-by: Armando Ruocco <[email protected]>
After a failed streaming attempt, PostgreSQL can ask again for the segment
before the one that failed. Only a later segment tells that the standby
moved on by streaming, so compare the names by order instead of by
equality, and back off on the same or an earlier one.

Signed-off-by: Gabriele Bartolini <[email protected]>
Name restore_command in the log line and in the error, log the interval
with its unit instead of a bare number, and add the failure window.
Rename ErrStreamingRetry to ErrForcedRestoreFailure, as it is not an
error of streaming replication, FailedWAL to ForcedFailureWAL, as the
file is not bad: it is restored later or received by streaming, and
streamingRetryIdleGap to streamingRetryResetAfter, which says what it
does. Explain in the comments why only a later WAL file counts as
streaming progress, and why the pause is skipped.

Signed-off-by: Gabriele Bartolini <[email protected]>
Leave out the internals that users do not need: how long restore_command
fails, how it relates to wal_retrieve_retry_interval, and when the
interval starts over. Link the section from the hybrid approach of the
replica cluster documentation.

Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
@gbartolini

Copy link
Copy Markdown
Contributor

Given that we are exceptionally releasing 1.30.2 due to the new PostgreSQL version, I suggest we backport this.

@gbartolini gbartolini added backport-requested ◀️ This pull request should be backported to all supported releases release-1.30 and removed do not backport This PR must not be backported - it will be in the next minor release labels Nov 16, 2026
gbartolini added a commit that referenced this pull request Nov 16, 2026
…ng (#11868)

PostgreSQL moves a standby from the WAL archive to streaming only when
`restore_command` fails. While the archive keeps up, that never happens:
the standby stays on the archive with no WAL receiver, an inactive slot
and no part in synchronous replication.

After 30 seconds of continuous restores, `wal-restore` now fails every
WAL segment request for 5 seconds, before calling any plugin. A single
failure is not enough since PostgreSQL 15, which retries a restore that
fails while prefetching. If the standby is back on the archive soon
after, the interval doubles, up to 5 minutes.

The failure is never forced in rewind mode, outside standby mode,
without a streaming source, or while a promotion is pending (a `promote`
file, or the operator promoting the instance): the promotion would then
skip the WAL still in the archive. Nor is it forced for a WAL file
already prefetched into the spool, which would stay there once the
standby streams.

When the source is unreachable, each try pauses the replay until the
connection fails: about 6 seconds if refused, up to the TCP connect
timeout if the host does not answer and `connect_timeout` is not set.

The other instances of a replica cluster stream from the designated
primary. Before 18.6, 17.11, 16.15, 15.19 and 14.24, an upstream bug can
make that connection fail with "requested starting point ... is ahead of
the WAL flush position", and PostgreSQL goes back to the archive, as it
does today.

The `Wal-restore in parallel` e2e runs `wal-restore` by hand on a
streaming standby, up to 16 seconds after the first restore of the
standby on KIND. It now disables the retry, which a slower run would
trip.

The counter proposed in the issue is left out, to avoid adding a metric
to the public API.

Closes #11808

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
mnencia pushed a commit that referenced this pull request Nov 16, 2026
…ng (#11868)

PostgreSQL moves a standby from the WAL archive to streaming only when
`restore_command` fails. While the archive keeps up, that never happens:
the standby stays on the archive with no WAL receiver, an inactive slot
and no part in synchronous replication.

After 30 seconds of continuous restores, `wal-restore` now fails every
WAL segment request for 5 seconds, before calling any plugin. A single
failure is not enough since PostgreSQL 15, which retries a restore that
fails while prefetching. If the standby is back on the archive soon
after, the interval doubles, up to 5 minutes.

The failure is never forced in rewind mode, outside standby mode,
without a streaming source, or while a promotion is pending (a `promote`
file, or the operator promoting the instance): the promotion would then
skip the WAL still in the archive. Nor is it forced for a WAL file
already prefetched into the spool, which would stay there once the
standby streams.

When the source is unreachable, each try pauses the replay until the
connection fails: about 6 seconds if refused, up to the TCP connect
timeout if the host does not answer and `connect_timeout` is not set.

The other instances of a replica cluster stream from the designated
primary. Before 18.6, 17.11, 16.15, 15.19 and 14.24, an upstream bug can
make that connection fail with "requested starting point ... is ahead of
the WAL flush position", and PostgreSQL goes back to the archive, as it
does today.

The `Wal-restore in parallel` e2e runs `wal-restore` by hand on a
streaming standby, up to 16 seconds after the first restore of the
standby on KIND. It now disables the retry, which a slower run would
trip.

The counter proposed in the issue is left out, to avoid adding a metric
to the public API.

Closes #11808

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit bc0889a)
mnencia added a commit that referenced this pull request Nov 17, 2026
Drop the link to the release notes tracking issue from the PostgreSQL
18.7 entry, mark #11868 and #11935 as backported to 1.28, and add the
contributor handles and requeue forms to the spellcheck wordlist.

Signed-off-by: Marco Nenciarini <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-requested ◀️ This pull request should be backported to all supported releases ok to merge 👌 This PR can be merged release-1.28 release-1.30

Projects

None yet

6 participants