Repository navigation
feat(instance): publish the primary's position and decide if a replica has all the data - #11885
Merged
Merged
Conversation
leonardoce
requested review from
a team,
NiccoloFei and
litaocdl
as code owners
November 10, 2026 10:27
Contributor
|
❗ By default, the pull request is configured to backport to all release branches.
|
leonardoce
force-pushed
the
cloudnative-pg-11884
branch
from
November 10, 2026 10:32
bb28c87 to
0683fd2
Compare
Member
|
/test |
Contributor
|
@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/42827931631 |
armru
approved these changes
Nov 11, 2026
gbartolini
force-pushed
the
cloudnative-pg-11884
branch
2 times, most recently
from
November 11, 2026 18:28
dd3834a to
03de498
Compare
Contributor
|
/test |
Contributor
|
@gbartolini, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/43172672220 |
gbartolini
force-pushed
the
cloudnative-pg-11884
branch
from
November 12, 2026 11:20
03de498 to
78bd13d
Compare
gbartolini
approved these changes
Nov 12, 2026
mnencia
force-pushed
the
cloudnative-pg-11884
branch
from
November 13, 2026 18:03
8cfbab2 to
78bd13d
Compare
The primary lease now carries a `cnpg.io/position` annotation with the
LSN and the timeline of the primary, the state of its PostgreSQL
instance and the pod that wrote it, for example:
{"lsn":"0/7000028","timeline":3,"state":"in production","pod":"cluster-example-1"}
While the primary is running, every write of the lease refreshes the
annotation with the current WAL flush position. When the primary is
shut down, the lease is released with the final position read from
`pg_controldata`, and the state tells whether PostgreSQL stopped
cleanly (`shut down`) or the position is only a lower bound. If that
position cannot be determined, `unknown` is published instead of
removing the annotation.
A replica that holds the lease before it is promoted leaves the
annotation of the previous primary untouched, so the position of the
old primary is still there for the new one until it publishes its own.
Nothing reads the annotation yet: this is the first step towards
checking, before a replica promotes, that it has all the data of the
primary that has just been shut down.
Signed-off-by: Leonardo Cecchi <[email protected]>
…oting Add the decision that a replica will take, before it promotes, by comparing its position with the one that the previous primary published on the primary lease: - after a clean shutdown of the previous primary, a replica that has all of its data promotes, one that is missing some of it waits for the WAL to arrive from the archive, and one on a later timeline than the previous primary's last one is blocked, with a message telling how to promote it anyway or how to promote the previous primary back; - after a failover, or when the position is not known, the replica promotes and the amount of WAL that may not be on it is reported. Nothing uses this decision yet. Signed-off-by: Leonardo Cecchi <[email protected]>
The timeline of the live position was parsed by hand from the first 8 digits of the WAL file name as an unsigned 32-bit number and converted to int, which CodeQL reports as a conversion without an upper bound check. SegmentFromName already parses and validates a WAL file name into 32-bit signed components, so use it instead. Signed-off-by: Armando Ruocco <[email protected]>
With archiving enabled, PostgreSQL switches to a new WAL segment before the shutdown checkpoint, which becomes the first record of a segment that is not archived. The REDO location published at release is then 40 bytes past the start of that segment, the size of its page header, while a replica that restored all the WAL from the archive stops at the start of the segment. That replica has all the data of the previous primary, but the promotion gate finds it 40 bytes short and waits for WAL that the archive never receives. Publish the end of the WAL before the REDO location instead: the start of its page when the record is the first one there, as told by the WAL block and segment sizes in pg_controldata, and the REDO location itself otherwise or when they are missing. Signed-off-by: Armando Ruocco <[email protected]>
Describe the cnpg.io/position annotation that the instance manager publishes on the primary lease, with its fields and states, in the failover page, and list it in the labels and annotations page. Note that it is read-only and its format may change in future releases. Assisted-by: Claude Signed-off-by: Gabriele Bartolini <[email protected]>
Wrap the commands of the hint in parentheses, so that no trailing punctuation is copied along with them, and fix the syntax of "kubectl cnpg promote", which takes the cluster and the instance. Assisted-by: Claude Signed-off-by: Gabriele Bartolini <[email protected]>
…tion type The comment referred to the documentation of the annotation, which did not exist. Refer to the comment of the LeasePosition type instead. Assisted-by: Claude Signed-off-by: Gabriele Bartolini <[email protected]>
…tion gate One branch of the promotion gate logged it as leasePosition, although it is only the LSN, while the others use leaseLSN. Use one key so that logs can be filtered on it. Assisted-by: Claude Signed-off-by: Gabriele Bartolini <[email protected]>
mnencia
force-pushed
the
cloudnative-pg-11884
branch
from
November 15, 2026 21:15
78bd13d to
19764b7
Compare
A replica takes the primary lease before it promotes, and until its first renewal as a primary the lease still has the final position of the previous primary, on a timeline that the replica is leaving. If the replica is lost in that window, the next one finds a clean shutdown that never happened. Write a promoting state on the lease before pg_ctl promote. The renewals leave it alone while the instance is in recovery and replace it once it is a primary. The mark keeps the final position it replaces, so that if the pod dies before it promotes, the next replica on that timeline can still check that it has all the data. A replica on a later timeline has followed a promotion that happened, so it is not held back. The write is bounded by the renew deadline. Signed-off-by: Marco Nenciarini <[email protected]>
Nobody renews the primary lease in a replica cluster, so its annotation is the final position of the primary the cluster had before it was demoted. The pod has followed another cluster since, on other timelines. Without this case the gate would block the promotion on the timeline of the old position. The caller sets the flag from the promotion that ends the replica cluster mode, not from the replica cluster configuration, which stays after it. Signed-off-by: Marco Nenciarini <[email protected]>
mnencia
force-pushed
the
cloudnative-pg-11884
branch
from
November 15, 2026 21:47
5062229 to
ed71ab2
Compare
Member
|
/test |
Contributor
|
@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/43766698153 |
mnencia
approved these changes
Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The primary lease now carries a
cnpg.io/positionannotation with the LSN and the timeline of the primary, the state of its PostgreSQL instance and the pod that wrote it, for example:While the primary is running, the annotation is refreshed on every write of the lease that can read the position. When the primary is shut down, the lease is released with the final position, and the state tells whether PostgreSQL stopped cleanly or the position is only a lower bound. If the position cannot be determined,
unknownis published instead of removing the annotation.A replica now marks the lease as
promotingbefore it promotes. The mark replaces the position of the previous primary, which does not hold once the replica is a primary, and keeps its final position if there was one, so that a replica that finds the mark after the pod died can still check it. The mark is part of the promotion: if the lease write fails, the promotion does not start and is retried.A promotion gate, not used yet, decides before a replica promotes whether it has all the data of the previous primary. After a clean shutdown, a replica that has all of its data promotes, one that is missing some of it waits for the WAL to arrive from the archive, and one on a later timeline is blocked. After a failover, or when the position is not known, the replica promotes and the WAL that may be missing is reported. A promotion that ends the replica cluster mode skips the check, because the position on the lease is from before the cluster was demoted.
Nothing reads the annotation or calls the gate yet. The visible changes are the annotation on the lease, one local query per lease renewal, and the lease write before the promotion.
Closes #11884