pg_rewind --source-pgdata ignores minRecoveryPoint of a stopped standby
Hi Hackers,
When you run pg_rewind with --source-pgdata and the source is a cleanly
stopped standby, it sets the target's minRecoveryPoint to the source's
last restartpoint instead of the source's minRecoveryPoint. This causes
the target to be marked consistent too soon.
Problem
=======
For a local source, pg_rewind assumes there was a shutdown checkpoint
and uses the latest checkpoint location as the point the target must
replay to. But it also accepts a source that is shut down in recovery,
and in that case, the standby's data files include changes up to its
minRecoveryPoint. When using --source-server, pg_rewind already uses
minRecoveryPoint for a standby source.
To reproduce this, it is enough that the source standby replays a
transaction after its last restartpoint before it is stopped. In my
test, it replayed a CHECKPOINT followed by a transaction:
Latest checkpoint location: 0/030001A8
Minimum recovery ending location: 0/03000388
After pg_rewind, the control file of the target contains the checkpoint
location of the source, not its minRecoveryPoint:
Minimum recovery ending location: 0/030001A8
This is important if recovery ends at the consistency point, such as
with recovery_target = 'immediate'. In this case, the target has blocks
from the source that are newer than the WAL it has replayed. As a
result, a transaction that was not replayed can become partially
visible: once a new transaction on the target gets the same xid, its
changes in the copied blocks show up, while its changes in other blocks
are missing.
Patch
=====
The attached patch makes pg_rewind use the source's minRecoveryPoint if
it is later than the last checkpoint. It also adds a TAP test for this case.
Comments welcome.
Best regards
Jan
--
Jan Nidzwetzki
PlanetScale