Skip to content

feat(wal): add restore mode to WALRestoreRequest - #351

Merged
leonardoce merged 1 commit into
mainfrom
dev/wal-restore-rewind-mode
Jul 20, 2026
Merged

leonardoce merged 1 commit into
mainfrom
dev/wal-restore-rewind-mode

Conversation

@armru

@armru armru commented Jul 15, 2026 •

Copy link
Copy Markdown
Member

pg_rewind fetches the WAL files it needs by running the cluster's restore_command, and when WAL restore is handled by a plugin those invocations reach the plugin with no way to tell that pg_rewind, and not an instance in recovery, is asking. The difference matters: pg_rewind walks the timeline backwards, fetches every file exactly once, and treats any restore failure as fatal, so the optimizations a plugin applies during recovery, like prefetching the following segments or caching an archive miss to signal the end of the WAL stream, actively break it. cloudnative-pg/cloudnative-pg#11200 shows the resulting failure on the in-tree restore path, fixed by cloudnative-pg/cloudnative-pg#11204; plugins need this field to do the same.

WALRestoreRequest gains a mode field stating the context the WAL file is being restored in. MODE_REWIND asks the plugin to retrieve exactly the requested file, without prefetching and without caching archive misses. MODE_UNSPECIFIED is what an operator predating the field sends and must be read as MODE_RECOVERY. The field is optional, so older operators and older plugins keep their current behavior.

Part of cloudnative-pg/cloudnative-pg#11200.

The operator can now tell WAL restore plugins whether a WAL file is
being restored by an instance in recovery or on behalf of pg_rewind,
which requires fetching exactly the requested file, without WAL
prefetching and without caching archive misses observed on nearby
segments.

Signed-off-by: Armando Ruocco <[email protected]>
@mnencia
mnencia force-pushed the dev/wal-restore-rewind-mode branch from 10dead4 to 80f3079 Compare July 17, 2026 08:41
mnencia pushed a commit to cloudnative-pg/plugin-barman-cloud that referenced this pull request Jul 17, 2026
The operator now tells WAL restore plugins when a restore request is
made on behalf of pg_rewind (cloudnative-pg/cnpg-i#351). pg_rewind walks
the timeline backwards, fetches every WAL file it needs exactly once,
and treats any restore failure as fatal, so both optimizations meant for
an instance in recovery must stay off: prefetching the following
segments is wasted work that ends in an archive miss, and the
end-of-wal-stream flag recorded by such a miss makes a later invocation
fail on a segment that is available in the archive, aborting the whole
rewind.

The cnpg-i dependency points to a pseudo-version of that pull request
and will be moved to the next tagged release once it is available.

Ref: cloudnative-pg/cloudnative-pg#11200
Signed-off-by: Armando Ruocco <[email protected]>
mnencia pushed a commit to cloudnative-pg/plugin-barman-cloud that referenced this pull request Jul 17, 2026
The operator now tells WAL restore plugins when a restore request is
made on behalf of pg_rewind (cloudnative-pg/cnpg-i#351). pg_rewind walks
the timeline backwards, fetches every WAL file it needs exactly once,
and treats any restore failure as fatal, so both optimizations meant for
an instance in recovery must stay off: prefetching the following
segments is wasted work that ends in an archive miss, and the
end-of-wal-stream flag recorded by such a miss makes a later invocation
fail on a segment that is available in the archive, aborting the whole
rewind.

The cnpg-i dependency points to a pseudo-version of that pull request
and will be moved to the next tagged release once it is available.

Ref: cloudnative-pg/cloudnative-pg#11200
Signed-off-by: Armando Ruocco <[email protected]>
@leonardoce
leonardoce merged commit f1298e9 into main Jul 20, 2026
6 checks passed
@leonardoce
leonardoce deleted the dev/wal-restore-rewind-mode branch July 20, 2026 13:10
mnencia pushed a commit to cloudnative-pg/plugin-barman-cloud that referenced this pull request Jul 20, 2026
The operator now tells WAL restore plugins when a restore request is
made on behalf of pg_rewind (cloudnative-pg/cnpg-i#351). pg_rewind walks
the timeline backwards, fetches every WAL file it needs exactly once,
and treats any restore failure as fatal, so both optimizations meant for
an instance in recovery must stay off: prefetching the following
segments is wasted work that ends in an archive miss, and the
end-of-wal-stream flag recorded by such a miss makes a later invocation
fail on a segment that is available in the archive, aborting the whole
rewind.

The cnpg-i dependency points to a pseudo-version of that pull request
and will be moved to the next tagged release once it is available.

Ref: cloudnative-pg/cloudnative-pg#11200
Signed-off-by: Armando Ruocco <[email protected]>
mnencia pushed a commit to cloudnative-pg/plugin-barman-cloud that referenced this pull request Jul 20, 2026
The operator now tells WAL restore plugins when a restore request is
made on behalf of pg_rewind (cloudnative-pg/cnpg-i#351). pg_rewind walks
the timeline backwards, fetches every WAL file it needs exactly once,
and treats any restore failure as fatal, so both optimizations meant for
an instance in recovery must stay off: prefetching the following
segments is wasted work that ends in an archive miss, and the
end-of-wal-stream flag recorded by such a miss makes a later invocation
fail on a segment that is available in the archive, aborting the whole
rewind.

The cnpg-i dependency points to a pseudo-version of that pull request
and will be moved to the next tagged release once it is available.

Ref: cloudnative-pg/cloudnative-pg#11200
Signed-off-by: Armando Ruocco <[email protected]>
mnencia pushed a commit to cloudnative-pg/plugin-barman-cloud that referenced this pull request Jul 20, 2026
The operator now tells WAL restore plugins when a restore request is
made on behalf of pg_rewind (cloudnative-pg/cnpg-i#351). pg_rewind walks
the timeline backwards, fetches every WAL file it needs exactly once,
and treats any restore failure as fatal, so both optimizations meant for
an instance in recovery must stay off: prefetching the following
segments is wasted work that ends in an archive miss, and the
end-of-wal-stream flag recorded by such a miss makes a later invocation
fail on a segment that is available in the archive, aborting the whole
rewind.

The cnpg-i dependency points to a pseudo-version of that pull request
and will be moved to the next tagged release once it is available.

Ref: cloudnative-pg/cloudnative-pg#11200
Signed-off-by: Armando Ruocco <[email protected]>
leonardoce pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Jul 21, 2026
The WAL restore protocol gained a mode field stating the context a WAL
file is being restored in (cloudnative-pg/cnpg-i#351). Set it on every
plugin restore request, so WAL restore plugins can drop the prefetching
and end-of-wal-stream optimizations when the restore runs on behalf of
pg_rewind, like the in-tree restore path already does.

The cnpg-i dependency points to a pseudo-version of that pull request
and will be moved to the next tagged release once it is available.

Signed-off-by: Armando Ruocco <[email protected]>
leonardoce added a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Jul 21, 2026
When a demoted primary rejoins the cluster after a failover, the
instance manager runs `pg_rewind --restore-target-wal` so the WAL
segments no longer in its `pg_wal` can be fetched from the archive. The
`restore_command` handed to pg_rewind was the same `manager wal-restore`
invocation used during normal recovery, WAL prefetching included.
pg_rewind walks the timeline backwards, fetches every file it needs
exactly once, and treats any restore failure as fatal: with
`wal.maxParallel > 1` a prefetch miss is practically guaranteed, the
miss records the end-of-wal-stream flag on disk, and the next invocation
consumes the flag and fails without even contacting the object store,
aborting the whole rewind. A flag left over from the pod's previous life
aborts even the first invocation. To make things worse, a failed
pg_rewind was only retried by restarting the whole join under the
reconcile exponential backoff, which grows to roughly 1000 seconds, so a
transient restore failure could stall the rejoin for hours.

`manager wal-restore` now has a `--rewind` flag that disables
prefetching and the end-of-wal-stream flag machinery. The instance
manager writes a rewind-specific `restore_command` into `override.conf`
right before running pg_rewind, and the demotion path rewrites the
standard configuration immediately after, so the flag never leaks into
normal recovery. pg_rewind itself is now retried in-process with a
bounded backoff before the error surfaces, which is safe because
pg_rewind collects every WAL segment it needs before writing anything
into the target data directory: a failed run leaves the target
unchanged.

The same context is conveyed to WAL restore plugins through the new
`mode` field of `WALRestoreRequest` (cloudnative-pg/cnpg-i#351), and
plugin-barman-cloud honors it in
cloudnative-pg/plugin-barman-cloud#1007. Plugins that ignore the field
keep their current behavior, helped by the in-process retry advancing
one segment per attempt. The cnpg-i dependency points to a
pseudo-version of that pull request and will move to the next tagged
release once it is available.

Related to #10419, which shares the flag machinery but is a distinct
failure mode and is not closed here.

Closes #11200

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
cnpg-bot pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Jul 21, 2026
When a demoted primary rejoins the cluster after a failover, the
instance manager runs `pg_rewind --restore-target-wal` so the WAL
segments no longer in its `pg_wal` can be fetched from the archive. The
`restore_command` handed to pg_rewind was the same `manager wal-restore`
invocation used during normal recovery, WAL prefetching included.
pg_rewind walks the timeline backwards, fetches every file it needs
exactly once, and treats any restore failure as fatal: with
`wal.maxParallel > 1` a prefetch miss is practically guaranteed, the
miss records the end-of-wal-stream flag on disk, and the next invocation
consumes the flag and fails without even contacting the object store,
aborting the whole rewind. A flag left over from the pod's previous life
aborts even the first invocation. To make things worse, a failed
pg_rewind was only retried by restarting the whole join under the
reconcile exponential backoff, which grows to roughly 1000 seconds, so a
transient restore failure could stall the rejoin for hours.

`manager wal-restore` now has a `--rewind` flag that disables
prefetching and the end-of-wal-stream flag machinery. The instance
manager writes a rewind-specific `restore_command` into `override.conf`
right before running pg_rewind, and the demotion path rewrites the
standard configuration immediately after, so the flag never leaks into
normal recovery. pg_rewind itself is now retried in-process with a
bounded backoff before the error surfaces, which is safe because
pg_rewind collects every WAL segment it needs before writing anything
into the target data directory: a failed run leaves the target
unchanged.

The same context is conveyed to WAL restore plugins through the new
`mode` field of `WALRestoreRequest` (cloudnative-pg/cnpg-i#351), and
plugin-barman-cloud honors it in
cloudnative-pg/plugin-barman-cloud#1007. Plugins that ignore the field
keep their current behavior, helped by the in-process retry advancing
one segment per attempt. The cnpg-i dependency points to a
pseudo-version of that pull request and will move to the next tagged
release once it is available.

Related to #10419, which shares the flag machinery but is a distinct
failure mode and is not closed here.

Closes #11200

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit ad1d52c)
cnpg-bot pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Jul 21, 2026
When a demoted primary rejoins the cluster after a failover, the
instance manager runs `pg_rewind --restore-target-wal` so the WAL
segments no longer in its `pg_wal` can be fetched from the archive. The
`restore_command` handed to pg_rewind was the same `manager wal-restore`
invocation used during normal recovery, WAL prefetching included.
pg_rewind walks the timeline backwards, fetches every file it needs
exactly once, and treats any restore failure as fatal: with
`wal.maxParallel > 1` a prefetch miss is practically guaranteed, the
miss records the end-of-wal-stream flag on disk, and the next invocation
consumes the flag and fails without even contacting the object store,
aborting the whole rewind. A flag left over from the pod's previous life
aborts even the first invocation. To make things worse, a failed
pg_rewind was only retried by restarting the whole join under the
reconcile exponential backoff, which grows to roughly 1000 seconds, so a
transient restore failure could stall the rejoin for hours.

`manager wal-restore` now has a `--rewind` flag that disables
prefetching and the end-of-wal-stream flag machinery. The instance
manager writes a rewind-specific `restore_command` into `override.conf`
right before running pg_rewind, and the demotion path rewrites the
standard configuration immediately after, so the flag never leaks into
normal recovery. pg_rewind itself is now retried in-process with a
bounded backoff before the error surfaces, which is safe because
pg_rewind collects every WAL segment it needs before writing anything
into the target data directory: a failed run leaves the target
unchanged.

The same context is conveyed to WAL restore plugins through the new
`mode` field of `WALRestoreRequest` (cloudnative-pg/cnpg-i#351), and
plugin-barman-cloud honors it in
cloudnative-pg/plugin-barman-cloud#1007. Plugins that ignore the field
keep their current behavior, helped by the in-process retry advancing
one segment per attempt. The cnpg-i dependency points to a
pseudo-version of that pull request and will move to the next tagged
release once it is available.

Related to #10419, which shares the flag machinery but is a distinct
failure mode and is not closed here.

Closes #11200

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit ad1d52c)
leonardoce added a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Jul 21, 2026
When a demoted primary rejoins the cluster after a failover, the
instance manager runs `pg_rewind --restore-target-wal` so the WAL
segments no longer in its `pg_wal` can be fetched from the archive. The
`restore_command` handed to pg_rewind was the same `manager wal-restore`
invocation used during normal recovery, WAL prefetching included.
pg_rewind walks the timeline backwards, fetches every file it needs
exactly once, and treats any restore failure as fatal: with
`wal.maxParallel > 1` a prefetch miss is practically guaranteed, the
miss records the end-of-wal-stream flag on disk, and the next invocation
consumes the flag and fails without even contacting the object store,
aborting the whole rewind. A flag left over from the pod's previous life
aborts even the first invocation. To make things worse, a failed
pg_rewind was only retried by restarting the whole join under the
reconcile exponential backoff, which grows to roughly 1000 seconds, so a
transient restore failure could stall the rejoin for hours.

`manager wal-restore` now has a `--rewind` flag that disables
prefetching and the end-of-wal-stream flag machinery. The instance
manager writes a rewind-specific `restore_command` into `override.conf`
right before running pg_rewind, and the demotion path rewrites the
standard configuration immediately after, so the flag never leaks into
normal recovery. pg_rewind itself is now retried in-process with a
bounded backoff before the error surfaces, which is safe because
pg_rewind collects every WAL segment it needs before writing anything
into the target data directory: a failed run leaves the target
unchanged.

The same context is conveyed to WAL restore plugins through the new
`mode` field of `WALRestoreRequest` (cloudnative-pg/cnpg-i#351), and
plugin-barman-cloud honors it in
cloudnative-pg/plugin-barman-cloud#1007. Plugins that ignore the field
keep their current behavior, helped by the in-process retry advancing
one segment per attempt. The cnpg-i dependency points to a
pseudo-version of that pull request and will move to the next tagged
release once it is available.

Related to #10419, which shares the flag machinery but is a distinct
failure mode and is not closed here.

Closes #11200

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit ad1d52c)
(cherry picked from commit f5f1b8a)
leonardoce pushed a commit to cloudnative-pg/plugin-barman-cloud that referenced this pull request Jul 21, 2026
The operator now tells WAL restore plugins when a restore request is
made on behalf of pg_rewind (cloudnative-pg/cnpg-i#351). pg_rewind walks
the timeline backwards, fetches every WAL file it needs exactly once,
and treats any restore failure as fatal, so both optimizations meant for
an instance in recovery must stay off: prefetching the following
segments is wasted work that ends in an archive miss, and the
end-of-wal-stream flag recorded by such a miss makes a later invocation
fail on a segment that is available in the archive, aborting the whole
rewind.

The cnpg-i dependency points to a pseudo-version of that pull request
and will be moved to the next tagged release once it is available.

Ref: cloudnative-pg/cloudnative-pg#11200
Signed-off-by: Armando Ruocco <[email protected]>
leonardoce pushed a commit to cloudnative-pg/plugin-barman-cloud that referenced this pull request Jul 21, 2026
…ry (#1007)

When a demoted primary rejoins the cluster after a failover, the
instance manager runs `pg_rewind --restore-target-wal`, and the
resulting `restore_command` invocations are served by this sidecar
exactly as if an instance in recovery were asking. pg_rewind walks the
timeline backwards, fetches every WAL file it needs exactly once, and
treats any restore failure as fatal, so two optimizations meant for
recovery break it: prefetching the following segments is wasted work
that, past the end of the timeline, is guaranteed to end in an archive
miss, and the end-of-wal-stream flag recorded by that miss makes a later
invocation fail without contacting the object store, on a segment the
archive actually has, aborting the whole rewind.

The restore request now carries the context it is made in
(cloudnative-pg/cnpg-i#351). When it says `MODE_REWIND`, the sidecar
restores exactly the requested file: no prefetching, no
end-of-wal-stream flag check, no flag recording. Requests from operators
predating the field keep the current behavior.

This is the plugin-side counterpart of
cloudnative-pg/cloudnative-pg#11204. The cnpg-i dependency points to a
pseudo-version of the protocol pull request and will move to the next
tagged release once it is available.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants