pg_rewind reports "no rewind required" due to stale minRecoveryPoint

Started by Jan Nidzwetzki1 day ago6 messageshackers
Jump to latest
#1Jan Nidzwetzki
jan@planetscale.com

Hi Hackers,

While working on a pg_rewind issue [1]/messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com, I found that pg_rewind sometimes
reports "no rewind required" for a standby that was cleanly stopped
after replaying WAL past the divergence point. The standby then cannot
follow the new primary:

LOG: new timeline 2 forked off current database system timeline 1
before current recovery point 0/03000138

Problem
=======

pg_rewind uses minRecoveryPoint as the end of WAL for a standby. When
shutting down, CreateRestartPoint() sets it to the replay position only
if the restartpoint is skipped. If a restartpoint is performed, it
assumes flushing the buffers will advance it. This only happens if a
written page has a newer LSN, such as a data page or a pg_xact page with
a commit replayed in hot standby. Many records leave no such page
behind, such as an abort, PREPARE TRANSACTION, a restore point, a WAL
switch, or a standby snapshot. If only these records were replayed since
minRecoveryPoint was last updated, it remains behind the replay position.

To reproduce this, set up a primary with two standbys, S1 and S2.
Restart S2, run CHECKPOINT on the primary, and promote S1. Next, run
"BEGIN; SELECT pg_current_xact_id(); ROLLBACK;" on the primary, let S2
replay the abort, stop S2, and then run pg_rewind on S2 using S1 as the
source:

pg_rewind: servers diverged at WAL location 0/03000110 on timeline 1
pg_rewind: no rewind required

The control file of S2 has minRecoveryPoint at 0/03000110, while the
replayed abort ends at 0/03000138.

If hot_standby is off on S2, a commit record acts the same way: after a
COMMIT PREPARED that only S2 replays, pg_rewind also reports "no rewind
required", pg_xact on S2 shows the transaction as committed, and it is
still prepared on S1.

Patch
=====

The attached patch updates minRecoveryPoint to the replay position at
every shutdown restartpoint, just like the skipped path does. It also
adds the steps above as a TAP test.

BUG #18575 [2]/messages/by-id/18575-1b9b3b60e2a480de@postgresql.org shows the same symptom, but it looks like the standby
received the WAL past the divergence point without replaying it before
shutdown. This patch does not affect that case. In that thread, Heikki
suggested that "perhaps a fast shutdown of a standby server should
update minRecoveryPoint before exiting" [3]/messages/by-id/e08235e3-b216-4bf3-8a9a-8ea819ae105e@iki.fi, and this patch does that
for the path that did not do so yet.

Any comments?

Best regards
Jan

URLs
=====
[1]: /messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com
/messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com
[2]: /messages/by-id/18575-1b9b3b60e2a480de@postgresql.org
/messages/by-id/18575-1b9b3b60e2a480de@postgresql.org
[3]: /messages/by-id/e08235e3-b216-4bf3-8a9a-8ea819ae105e@iki.fi
/messages/by-id/e08235e3-b216-4bf3-8a9a-8ea819ae105e@iki.fi

--
Jan Nidzwetzki
PlanetScale

Attachments:

t259928_1
v1-0001-Update-minRecoveryPoint-at-every-shutdown-restart.patchtext/plain; charset=UTF-8; name=v1-0001-Update-minRecoveryPoint-at-every-shutdown-restart.patchDownload+88-4
#2Osama Abdul Qader
osamaabdulqaderpostgres@gmail.com
In reply to: Jan Nidzwetzki (#1)
Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint

Hi Jan

On Fri, 27 Nov 2026 at 18:30, Jan Nidzwetzki <jan@planetscale.com> wrote:

Hi Hackers,

While working on a pg_rewind issue [1], I found that pg_rewind sometimes
reports "no rewind required" for a standby that was cleanly stopped
after replaying WAL past the divergence point. The standby then cannot
follow the new primary:

LOG: new timeline 2 forked off current database system timeline 1
before current recovery point 0/03000138

Problem
=======

pg_rewind uses minRecoveryPoint as the end of WAL for a standby. When
shutting down, CreateRestartPoint() sets it to the replay position only
if the restartpoint is skipped. If a restartpoint is performed, it
assumes flushing the buffers will advance it. This only happens if a
written page has a newer LSN, such as a data page or a pg_xact page with
a commit replayed in hot standby. Many records leave no such page
behind, such as an abort, PREPARE TRANSACTION, a restore point, a WAL
switch, or a standby snapshot. If only these records were replayed since
minRecoveryPoint was last updated, it remains behind the replay position.

To reproduce this, set up a primary with two standbys, S1 and S2.
Restart S2, run CHECKPOINT on the primary, and promote S1. Next, run
"BEGIN; SELECT pg_current_xact_id(); ROLLBACK;" on the primary, let S2
replay the abort, stop S2, and then run pg_rewind on S2 using S1 as the
source:

pg_rewind: servers diverged at WAL location 0/03000110 on timeline 1
pg_rewind: no rewind required

The control file of S2 has minRecoveryPoint at 0/03000110, while the
replayed abort ends at 0/03000138.

If hot_standby is off on S2, a commit record acts the same way: after a
COMMIT PREPARED that only S2 replays, pg_rewind also reports "no rewind
required", pg_xact on S2 shows the transaction as committed, and it is
still prepared on S1.

Patch
=====

The attached patch updates minRecoveryPoint to the replay position at
every shutdown restartpoint, just like the skipped path does. It also
adds the steps above as a TAP test.

BUG #18575 [2] shows the same symptom, but it looks like the standby
received the WAL past the divergence point without replaying it before
shutdown. This patch does not affect that case. In that thread, Heikki
suggested that "perhaps a fast shutdown of a standby server should
update minRecoveryPoint before exiting" [3], and this patch does that
for the path that did not do so yet.

Any comments?

Best regards
Jan

URLs
=====
[1]

/messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com
[2]

/messages/by-id/18575-1b9b3b60e2a480de@postgresql.org
[3]

/messages/by-id/e08235e3-b216-4bf3-8a9a-8ea819ae105e@iki.fi

--
Jan Nidzwetzki
PlanetScale

I'm interested in contributing and reviewing the patch, I'll start
reviewing it once I'm at my den.

Best Regards,
Osama Abdul Qader
Independent contributor

#3Osama Abdul Qader
osamaabdulqaderpostgres@gmail.com
In reply to: Osama Abdul Qader (#2)
Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint

Hi Jan,

On Fri, 27 Nov 2026 at 18:44, Osama Abdul Qader <
osamaabdulqaderpostgres@gmail.com> wrote:

Hi Jan

On Fri, 27 Nov 2026 at 18:30, Jan Nidzwetzki <jan@planetscale.com> wrote:

Hi Hackers,

While working on a pg_rewind issue [1], I found that pg_rewind sometimes
reports "no rewind required" for a standby that was cleanly stopped
after replaying WAL past the divergence point. The standby then cannot
follow the new primary:

LOG: new timeline 2 forked off current database system timeline 1
before current recovery point 0/03000138

Problem
=======

pg_rewind uses minRecoveryPoint as the end of WAL for a standby. When
shutting down, CreateRestartPoint() sets it to the replay position only
if the restartpoint is skipped. If a restartpoint is performed, it
assumes flushing the buffers will advance it. This only happens if a
written page has a newer LSN, such as a data page or a pg_xact page with
a commit replayed in hot standby. Many records leave no such page
behind, such as an abort, PREPARE TRANSACTION, a restore point, a WAL
switch, or a standby snapshot. If only these records were replayed since
minRecoveryPoint was last updated, it remains behind the replay position.

To reproduce this, set up a primary with two standbys, S1 and S2.
Restart S2, run CHECKPOINT on the primary, and promote S1. Next, run
"BEGIN; SELECT pg_current_xact_id(); ROLLBACK;" on the primary, let S2
replay the abort, stop S2, and then run pg_rewind on S2 using S1 as the
source:

pg_rewind: servers diverged at WAL location 0/03000110 on timeline 1
pg_rewind: no rewind required

The control file of S2 has minRecoveryPoint at 0/03000110, while the
replayed abort ends at 0/03000138.

If hot_standby is off on S2, a commit record acts the same way: after a
COMMIT PREPARED that only S2 replays, pg_rewind also reports "no rewind
required", pg_xact on S2 shows the transaction as committed, and it is
still prepared on S1.

Patch
=====

The attached patch updates minRecoveryPoint to the replay position at
every shutdown restartpoint, just like the skipped path does. It also
adds the steps above as a TAP test.

BUG #18575 [2] shows the same symptom, but it looks like the standby
received the WAL past the divergence point without replaying it before
shutdown. This patch does not affect that case. In that thread, Heikki
suggested that "perhaps a fast shutdown of a standby server should
update minRecoveryPoint before exiting" [3], and this patch does that
for the path that did not do so yet.

Any comments?

Best regards
Jan

URLs
=====
[1]

/messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com
[2]

/messages/by-id/18575-1b9b3b60e2a480de@postgresql.org
[3]

/messages/by-id/e08235e3-b216-4bf3-8a9a-8ea819ae105e@iki.fi

--
Jan Nidzwetzki
PlanetScale

I'm interested in contributing and reviewing the patch, I'll start
reviewing it once I'm at my den.

Best Regards,
Osama Abdul Qader
Independent contributor

I've tested your patch, *"Update minRecoveryPoint at every shutdown
restartpoint"*, against PostgreSQL.

I applied it to an isolated worktree based on commit 0607aa6109 and
successfully built the project using Meson.
I also ran the new TAP test, pg_rewind/012_standby_shutdown, in both
configurations

Without the patch:

- The assertion checking whether minRecoveryPoint covers the replayed
abort LSN failed.
- pg_rewind reported no rewind required instead of performing the rewind.
- Two of the three subtests failed.

With Patch:

- All three subtests passed.
-

minRecoveryPoint correctly covered the replayed abort.
-

pg_rewind correctly proceeded with the rewind.

This confirms that the regression test reproduces the reported issue
without the fix and passes with your patch applied.

Lastly, I searched on commitfest with the same title and I found that it's
ain't there, therefore I'm assuming that you haven't register it there, I'm
urging you to register the bug there, so I can claim myself as reviewer
which helps committer like Chao, Alvaro, Amit etc to commit this patch in
the main codebase which can solve a huge problem that us normal devs felt
in developing apps where we can hit to a bug as similar as this.

Let me know if you've registered the bug in commitfest and Thank you for
your service and I hope that this patch becomes extremely beneficial to
upcoming PostgreSQL versions.

With Warm Regards,
Osama Abdul Qader
Independent contributor.

#4Jan Nidzwetzki
jan@planetscale.com
In reply to: Osama Abdul Qader (#3)
Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint

Hi Osama,

On 27.11.26 23:13, Osama Abdul Qader wrote:

Let me know if you've registered the bug in commitfest and Thank you for
your service and I hope that this patch becomes extremely beneficial to
upcoming PostgreSQL versions.

Thanks for testing the patch and for confirming that the TAP test fails
without the fix and passes with it.

The patch is now registered in the Commitfest:
https://commitfest.postgresql.org/patch/7744/

Feel free to add yourself as a reviewer there.

For context, I found this issue while working on two related pg_rewind
problems:

- pg_rewind and hint bits leads to visibility corruption [1]/messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com (CF 7693)
- pg_rewind --source-pgdata ignores minRecoveryPoint of a stopped
standby [2]/messages/by-id/51de3e6e-01de-4740-ad11-1bf69ff0d68d@planetscale.com (CF 7745)

All three are pg_rewind bugs that involve a standby, either as the
target or the source. However, each one has an independent fix.

Best regards
Jan

[1]: /messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com
/messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com
[2]: /messages/by-id/51de3e6e-01de-4740-ad11-1bf69ff0d68d@planetscale.com
/messages/by-id/51de3e6e-01de-4740-ad11-1bf69ff0d68d@planetscale.com

--
Jan Nidzwetzki
PlanetScale

#5Osama Abdul Qader
osamaabdulqaderpostgres@gmail.com
In reply to: Jan Nidzwetzki (#4)
Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint

Hi Jan

On Sat, 28 Nov 2026 at 13:45, Jan Nidzwetzki <jan@planetscale.com> wrote:

Hi Osama,

On 27.11.26 23:13, Osama Abdul Qader wrote:

Let me know if you've registered the bug in commitfest and Thank you for
your service and I hope that this patch becomes extremely beneficial to
upcoming PostgreSQL versions.

Thanks for testing the patch and for confirming that the TAP test fails
without the fix and passes with it.

The patch is now registered in the Commitfest:
https://commitfest.postgresql.org/patch/7744/

Feel free to add yourself as a reviewer there.

For context, I found this issue while working on two related pg_rewind
problems:

- pg_rewind and hint bits leads to visibility corruption [1] (CF 7693)
- pg_rewind --source-pgdata ignores minRecoveryPoint of a stopped
standby [2] (CF 7745)

All three are pg_rewind bugs that involve a standby, either as the
target or the source. However, each one has an independent fix.

Best regards
Jan

[1]

/messages/by-id/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com
[2]

/messages/by-id/51de3e6e-01de-4740-ad11-1bf69ff0d68d@planetscale.com

--
Jan Nidzwetzki
PlanetScale

Thanks for registering it at commitfest, I've added myself as reviewer
there and let's work together further to make this patch extremely
beneficial in future PostgreSQL version releases.

With Best Regards,
Osama Abdul Qader
independent Contributor

#6Zsolt Parragi
zsolt.parragi@percona.com
In reply to: Osama Abdul Qader (#5)
Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint

Hello!

Isn't this the same change that Adam Lee proposed in [1]/messages/by-id/aczc9cxkr7SEHXV5@MAC-CVW1VHW5R6 earlier this year for recovery_target_action = shutdown? His patch advanced minRecoveryPoint to the replay position at every restartpoint and then Heikki suggested calling UpdateMinRecoveryPoint(), which is what your patch does.

In that thread Michael and Horiguchi-san argued against it, but this could be another example in favor of it.

From a different perspective, isn't this something that could be possibly fixed in pg_rewind?

For a standby target it could read the WAL forward from minRecoveryPoint until the first invalid record, and use that as the end of the target's WAL. That doesn't change what minRecoveryPoint means, and it also works for a standby that was stopped by a server without the fix. See the attached quick PoC.

However, looking at your other patch [2]/messages/by-id/51de3e6e-01de-4740-ad11-1bf69ff0d68d@planetscale.com: it uses the minRecoveryPoint of a stopped standby as the consistency point for the copied files. With hot_standby = off on the source a replayed commit reaches pg_xact without moving minRecoveryPoint, so doesn't that patch need this server side change? If yes, that's an argument for keeping it, even if pg_rewind looks at the target's WAL.

[1]: /messages/by-id/aczc9cxkr7SEHXV5@MAC-CVW1VHW5R6
[2]: /messages/by-id/51de3e6e-01de-4740-ad11-1bf69ff0d68d@planetscale.com

Attachments:

nocfbot-poc-end-of-target-wal.txttext/plainDownload+59-1