Skip to content

feat(failover): detect and contain a replica that diverged from the primary's timeline - #11251

Merged
gbartolini merged 11 commits into
mainfrom
fix/cnp-8534-failover-walreceiver-deadlock
Nov 24, 2026
Merged

gbartolini merged 11 commits into
mainfrom
fix/cnp-8534-failover-walreceiver-deadlock

Conversation

@armru

@armru armru commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

When a replica that is not the most advanced one is promoted, for example after the primary is lost abruptly, a replica that already replayed WAL past the fork point of the new timeline can never follow the new primary. PostgreSQL logs "new timeline N forked off current database system timeline N-1 before current recovery point" and the replica waits forever. The operator still reports it healthy, keeps it in the read services, can pick it for the next promotion, and keeps its slot on the primary, which retains WAL.

Each standby now checks its own pg_wal and reports when it has learned about the primary's newer timeline but already replayed past the point where that timeline forked away from its own. A replica that is merely behind is not diverged, since PostgreSQL switches it to the new timeline on its own.

A diverged standby fails its readiness probe, so it leaves the read services. It is excluded from the election and from the failover quorum count, so it is never promoted, and it no longer holds back the readiness guard that precedes the election. The operator lists it in the new divergedInstances status field, sets the new ReplicasHealthy condition to False, raises a ReplicaDiverged event and exports the cnpg_collector_replicas_diverged metric. It also fences the instance and then drops its slot on the primary. The fence is set again on every pass, so lifting it by hand, or removing a fence on the whole cluster, does not release the instance; a fence on the whole cluster is left untouched. The containment is lifted only when the instance's PGDATA PVC is replaced, or when both the PVC and the Pod are gone. Rebuilding is manual: kubectl cnpg destroy CLUSTER INSTANCE.

Not covered: the wrong promotion is not prevented in general, only for replicas already known to be diverged. A manual kubectl cnpg promote onto a diverged replica that is not fenced is not blocked. A replica that has not yet fetched the new history file is not detected. In a replica cluster the diverged instance still fails its readiness probe, but it is not reported or fenced. When every instance that still reports has diverged and the primary is gone, the election is skipped.

Closes #12007
Refs #11557
Refs #11715

@armru
armru requested a review from jsilvela as a code owner July 24, 2026 18:06
@armru
armru requested a review from a team July 24, 2026 18:06
@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jul 24, 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 24, 2026
@github-actions

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

@dosubot dosubot Bot added the bug 🐛 Something isn't working label Jul 24, 2026
@armru

armru commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@armru
armru force-pushed the fix/cnp-8534-failover-walreceiver-deadlock branch from f0dd588 to 084bd39 Compare July 28, 2026 14:50
@armru

armru commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@armru armru removed backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.29 release-1.30 labels Jul 29, 2026
@armru
armru force-pushed the fix/cnp-8534-failover-walreceiver-deadlock branch from 084bd39 to 7b10bee Compare July 29, 2026 14:33
@armru

armru commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Jul 29, 2026
@armru

armru commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jul 29, 2026
@armru
armru force-pushed the fix/cnp-8534-failover-walreceiver-deadlock branch from 8ecf4fe to b23aa9b Compare July 29, 2026 16:34
@mnencia

mnencia commented Nov 23, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia
mnencia force-pushed the fix/cnp-8534-failover-walreceiver-deadlock branch from 1ecacf7 to 372463c Compare November 23, 2026 23:31
@mnencia

mnencia commented Nov 23, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia mnencia added do not backport This PR must not be backported - it will be in the next minor release enhancement 🪄 New feature or request and removed bug 🐛 Something isn't working labels Nov 24, 2026
@mnencia mnencia changed the title feat: detect and contain a replica diverged onto a discarded timeline feat(failover): detect and contain a replica that diverged from the primary's timeline Nov 24, 2026
@mnencia mnencia removed this from the 1.31.0 milestone Nov 24, 2026
@mnencia
mnencia force-pushed the fix/cnp-8534-failover-walreceiver-deadlock branch from 021bf75 to 4fdcb91 Compare November 24, 2026 10:56
armru and others added 10 commits November 24, 2026 12:01
When a replica that is not the most advanced one is promoted, a replica
that already replayed WAL past the fork point of the new timeline can
never follow the new primary. PostgreSQL logs "new timeline N forked off
current database system timeline N-1 before current recovery point" and
the replica waits forever. The operator still reports it healthy, keeps
it in the read services, can pick it for the next promotion, and keeps
its slot on the primary, which retains WAL.

Each standby now runs the same check from its own pg_wal and reports
the result in its status. Its readiness probe fails, so it leaves the
read services, and it always sorts after a replica that has not
diverged, so it is never promoted. The operator lists it under the new
`divergedInstances` status field, sets the new `ReplicasHealthy`
condition to `False`, raises a `ReplicaDiverged` event and exports the
`cnpg_collector_replicas_diverged` metric. It also fences the instance
and drops its slot on the primary, unless fencing it would leave too
few replicas for synchronous replication. The fence is lifted once the
instance's PGDATA PVC is replaced, as `kubectl cnpg destroy` does.

Rebuilding the instance is still a manual step.

Related: #11557, #11114

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
A replica that diverged onto a discarded timeline can never follow the
new primary, so it cannot acknowledge commits and does not count towards
the synchronous replication quorum. Fencing it therefore never reduces
the number of replicas able to satisfy the quorum.

Until now, on clusters with synchronous replication, the operator
skipped fencing a diverged replica when the remaining ready replicas
were fewer than `synchronous.number`. The replica was only reported,
and kept running with its data diverged. The operator now fences every
diverged replica, whatever the synchronous replication configuration.

Signed-off-by: Leonardo Cecchi <[email protected]>
A replica that diverged onto a discarded timeline holds writes the
primary never received and can't follow the current primary. Until now
it was only sorted after the healthy replicas, so it was still promoted
whenever it was the only candidate left, or the only one with a status.

The operator now takes diverged replicas out of the candidates before
electing a new primary, both in regular and in replica clusters. If
every remaining replica has diverged, no failover happens and the
cluster waits for a healthy replica instead of promoting data that was
discarded.

A diverged replica that fails its readiness probe no longer delays the
reconciliation loop either, so it gets fenced as soon as it is detected.

Signed-off-by: Leonardo Cecchi <[email protected]>
With the failover quorum check enabled, the operator only starts a
failover when a replica surely holds every acknowledged transaction.
A replica diverged onto a discarded timeline holds writes the primary
discarded instead, and is never promoted, but it could still be counted
as a promotable replica while its pod was ready.

The quorum check now ignores diverged replicas, so a failover is no
longer allowed on the strength of a replica that can't take over.

Signed-off-by: Leonardo Cecchi <[email protected]>
With a fence on the whole cluster set, rebuilding a diverged replica made
the lift fail on every pass, which stopped the reconcile loop until the
fence was cleared. A fence lifted by hand, or wiped by a cluster-wide
fence that was later lifted, was also never set again.

The entry in the status is now the source of truth, and the operator
asserts the fence on every pass. The lift ignores a fence on the whole
cluster, which is not the operator's to remove. It also runs when the
PGDATA PVC and its Pod are both gone, so the entry no longer outlives a
removed instance.

Detection keeps a parked entry, and any entry of an instance that might
be unavailable, when no divergence is reported: right after a fence is
lifted PostgreSQL has not started yet and reports nothing, and deleting
the entry then would leave the replica running.

Signed-off-by: Marco Nenciarini <[email protected]>
Their instances still fail the readiness probe when they diverge, so
they leave the read services, but the operator neither reports nor
fences them.

Signed-off-by: Marco Nenciarini <[email protected]>
The readiness probe checks for a timeline divergence on every instance,
including the primary, which has no replay position. The check listed
pg_wal before finding that out, and the listing grows with the retained
WAL. It now returns first when the replay position is empty.

Signed-off-by: Marco Nenciarini <[email protected]>
The readiness check for a diverged standby was only exercised by the e2e.
It now has unit tests for a standby past the fork point, one that did not
replay past it while holding the segments it wrote there (a rewound former
primary or a lagging standby), a primary, and a failure to read the replay
position or to list pg_wal. The replay position is read through an optional
field so the tests do not need a database.

A history line that is empty, has no LSN or does not start with the
standby's timeline gives no fork point, which counts as a divergence, as
for a sibling branch. A table pins that down.

Signed-off-by: Marco Nenciarini <[email protected]>
The diverged replica e2e resumed the stopped WAL receiver as soon as the
former primary was not ready. If the operator had not started the failover
yet, the former primary was still in the services, and the receiver could
reach it and catch up, which breaks the divergence the test needs.

The test now waits for the pending failover marker. The failover waits for
the stopped receiver, so the marker stays set until it is resumed.

Signed-off-by: Marco Nenciarini <[email protected]>
@mnencia
mnencia force-pushed the fix/cnp-8534-failover-walreceiver-deadlock branch from 4fdcb91 to ddbe634 Compare November 24, 2026 11:04
Signed-off-by: Gabriele Bartolini <[email protected]>
@gbartolini
gbartolini merged commit f4f0f33 into main Nov 24, 2026
37 checks passed
@gbartolini
gbartolini deleted the fix/cnp-8534-failover-walreceiver-deadlock branch November 24, 2026 12:17
cnpg-bot pushed a commit to cloudnative-pg/api that referenced this pull request Nov 24, 2026
…rimary's timeline (cloudnative-pg/cloudnative-pg#11251)

When a replica that is not the most advanced one is promoted, for
example after the primary is lost abruptly, a replica that already
replayed WAL past the fork point of the new timeline can never follow
the new primary. PostgreSQL logs "new timeline N forked off current
database system timeline N-1 before current recovery point" and the
replica waits forever. The operator still reports it healthy, keeps it
in the read services, can pick it for the next promotion, and keeps its
slot on the primary, which retains WAL.

Each standby now checks its own pg_wal and reports when it has learned
about the primary's newer timeline but already replayed past the point
where that timeline forked away from its own. A replica that is merely
behind is not diverged, since PostgreSQL switches it to the new timeline
on its own.

A diverged standby fails its readiness probe, so it leaves the read
services. It is excluded from the election and from the failover quorum
count, so it is never promoted, and it no longer holds back the
readiness guard that precedes the election. The operator lists it in the
new `divergedInstances` status field, sets the new `ReplicasHealthy`
condition to `False`, raises a `ReplicaDiverged` event and exports the
`cnpg_collector_replicas_diverged` metric. It also fences the instance
and then drops its slot on the primary. The fence is set again on every
pass, so lifting it by hand, or removing a fence on the whole cluster,
does not release the instance; a fence on the whole cluster is left
untouched. The containment is lifted only when the instance's PGDATA PVC
is replaced, or when both the PVC and the Pod are gone. Rebuilding is
manual: `kubectl cnpg destroy CLUSTER INSTANCE`.

Not covered: the wrong promotion is not prevented in general, only for
replicas already known to be diverged. A manual `kubectl cnpg promote`
onto a diverged replica that is not fenced is not blocked. A replica
that has not yet fetched the new history file is not detected. In a
replica cluster the diverged instance still fails its readiness probe,
but it is not reported or fenced. When every instance that still reports
has diverged and the primary is gone, the election is skipped.

Closes cloudnative-pg/cloudnative-pg#12007
Refs cloudnative-pg/cloudnative-pg#11557
Refs cloudnative-pg/cloudnative-pg#11715

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>

(cherry picked from commit cloudnative-pg/cloudnative-pg@f4f0f33)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not backport This PR must not be backported - it will be in the next minor release enhancement 🪄 New feature or request ok to merge 👌 This PR can be merged size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: detect a replica that diverged from the primary's timeline and keep it out of service

7 participants