Skip to content

feat(instance): publish the primary's position and decide if a replica has all the data - #11885

Merged
mnencia merged 10 commits into
cloudnative-pg:mainfrom
leonardoce:cloudnative-pg-11884
Nov 16, 2026
Merged

mnencia merged 10 commits into
cloudnative-pg:mainfrom
leonardoce:cloudnative-pg-11884

Conversation

@leonardoce

@leonardoce leonardoce commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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, 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, unknown is published instead of removing the annotation.

A replica now marks the lease as promoting before 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

@leonardoce
leonardoce requested review from a team, NiccoloFei and litaocdl as code owners November 10, 2026 10:27
@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 10, 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

@leonardoce leonardoce 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 10, 2026
@armru

armru commented Nov 10, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Nov 10, 2026
@gbartolini
gbartolini force-pushed the cloudnative-pg-11884 branch 2 times, most recently from dd3834a to 03de498 Compare November 11, 2026 18:28
@gbartolini

Copy link
Copy Markdown
Contributor

/test

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia
mnencia force-pushed the cloudnative-pg-11884 branch from 8cfbab2 to 78bd13d Compare November 13, 2026 18:03
leonardoce and others added 6 commits November 15, 2026 22:10
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
mnencia force-pushed the cloudnative-pg-11884 branch from 78bd13d to 19764b7 Compare November 15, 2026 21:15
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
mnencia force-pushed the cloudnative-pg-11884 branch from 5062229 to ed71ab2 Compare November 15, 2026 21:47
@mnencia

mnencia commented Nov 15, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia
mnencia merged commit bae6e46 into cloudnative-pg:main Nov 16, 2026
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 ok to merge 👌 This PR can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Publish the primary's position on its lease and add the replica's "has all the data" decision

6 participants