Skip to content

fix(instance): skip startup probe during active WAL replay - #11138

Closed
ardentperf wants to merge 3 commits into
cloudnative-pg:mainfrom
ardentperf:pr-fix-startup-restart-loop
Closed

ardentperf wants to merge 3 commits into
cloudnative-pg:mainfrom
ardentperf:pr-fix-startup-restart-loop

Conversation

@ardentperf

@ardentperf ardentperf commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

The startup probe currently depends on PostgreSQL accepting connections. That can make a replica look failed while PostgreSQL is still making forward progress through crash recovery or WAL replay, causing Kubernetes to restart the pod and repeat the same work. This builds on the logpipe infrastructure from #11135 and records the timestamp of PostgreSQL redo in progress log messages that include the current LSN. While one of those messages was seen in the last five minutes, /startupz reports Skipped instead of failing the startup probe. If the signal is stale or absent, the code stops replay-progress detection for this process and falls back to the existing startup checker. The detector also stops as soon as PostgreSQL logs that the database system is ready to accept connections. At that point replay-progress detection is no longer useful, and stopping it avoids running the regex against later log messages.

Prerequisite: #11135
Fixes: #11024

Detect replicas stuck in repeated unrecoverable WAL replay errors and
marks the instance unhealthy using existing CNPG health paths. Adds an
instance `statusError` field and adds a log monitor path for CSV log
records that detects unrecoverable WAL replay loops.

Signed-off-by: Jeremy Schneider <[email protected]>
to support multiple detectors, base this on a multirecordwriter now.
this is a better direction than chaining writers.

Signed-off-by: Jeremy Schneider <[email protected]>
The startup probe currently depends on PostgreSQL accepting connections.
That can make a replica look failed while PostgreSQL is still making
forward progress through crash recovery or WAL replay, causing
Kubernetes to restart the pod and repeat the same work. This builds on
the logpipe infrastructure from cloudnative-pg#11135 and records the timestamp of
PostgreSQL `redo in progress` log messages that include the current LSN.
While one of those messages was seen in the last five minutes,
`/startupz` reports `Skipped` instead of failing the startup probe. If
the signal is stale or absent, the code stops replay-progress detection
for this process and falls back to the existing startup checker. The
detector also stops as soon as PostgreSQL logs that the database system
is ready to accept connections. At that point replay-progress detection
is no longer useful, and stopping it avoids running the regex against
later log messages.

Prerequisite: cloudnative-pg#11135
Fixed: cloudnative-pg#11024

Signed-off-by: Jeremy Schneider <[email protected]>
@ardentperf
ardentperf requested a review from a team July 7, 2026 00:31
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 7, 2026
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.29 release-1.30 labels Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 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 Jul 7, 2026

Copy link
Copy Markdown
Member

Thanks for this work, and especially for the diagnosis in #11024 — the
restartpoint analysis there is spot on, and I fully agree with the
direction: while replay is making progress, failing the startup probe and
losing the work is the worst outcome.

While reviewing I ran into a few things that, as far as I can tell,
prevent the detection from engaging in practice. I may well be missing
something, so please double check my reasoning — I'm sharing what I
found in the PostgreSQL sources and what I observed running this branch
on a kind cluster.

1. I couldn't find the redo in progress message on standbys. It
seems to be guarded by if (!StandbyMode) in PerformWalRecovery
(xlogrecovery.c, REL_15 through REL_18), and if I read InitWalRecovery
correctly, a replica enables standby mode before redo starts (both the
backup_label and the minRecoveryPoint paths). That would mean the pod
class from #11024, a restarted replica, doesn't produce matching log
lines. In my tests (full standby replays on PostgreSQL 18.4 with this
branch deployed) I indeed saw zero matches and the detector never fired —
but if you observed it firing on a replica in your environment I'd really
like to see those logs, since that would mean I'm misreading the guard.
Also worth noting: the message seems to arrive with PG 15
(log_startup_progress_interval), so PG 14 wouldn't have it either way.

2. On primaries, the first probe may disable detection before the
first message can appear.
The first redo in progress line can only
show up log_startup_progress_interval (10s) after redo starts, while
the first startup probe tends to reach the handler earlier (we set no
initialDelaySeconds, 10s period). When it finds no signal it calls
StopWALReplayProgressDetection(), which if I understand the intent
correctly is one-way. In my crash recovery test (22.9s recovery) the
first message appeared at T+10s but a probe had already come at T+3s, so
the pod went through recovery on the standard path. Maybe I got unlucky
with timing, but I couldn't construct a run where detection survived the
first probe.

3. I'm not sure the staleness fallback can ever run. Once the probe
returns 200, the kubelet marks the container Started and stops calling
the startup probe (kubelet prober/worker.go), and I couldn't find
another caller of /startupz. If that's right, the 5-minute window is
never re-evaluated after the first skip, and since our replica liveness
always returns OK, a replay that stalls after that point would leave the
pod unready indefinitely, which quietly changes the meaning of
startDelay. It would at least deserve a docs note.

To be clear on severity: even if I'm right about 1 and 2, the effect is
that the change becomes a no-op rather than something harmful — nothing
breaks, it just wouldn't help #11024.

Since I believe the probe-side idea itself is the right one, I also
explored an alternative signal on top of it: sampling the startup process
title (postgres: ... startup recovering <segment>, set in
XLogFileRead for all WAL sources), which appears to advance per segment
in every recovery form, on every supported version, independently of the
log locale. I'd like to open it as an alternative PR crediting you as
co-author for the diagnosis and the design idea, and iterate on it
together — but first I'd love your take on the findings above, in case
there's context I'm missing. #11135 looks unaffected by all of this and
I'll review it separately.

@ardentperf

Copy link
Copy Markdown
Contributor Author

thanks for taking a look. sampling the startup process title actually seems like a better approach to me than looking for the redo in progress message - i agree with a pivot to that approach

@ardentperf

Copy link
Copy Markdown
Contributor Author

i'm going to close this in favor of #11141

@ardentperf ardentperf closed this Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 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

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 bug 🐛 Something isn't working release-1.28 release-1.30 size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Replica enters crash/restart loop after restart with overloaded primary checkpointer

3 participants