pg_rewind race condition just after promotion

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Oleksii Kliukin <alexk(at)hintbits(dot)com>, Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
Subject: pg_rewind race condition just after promotion
Date: 2020-12-07 18:13:25
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

There's a race condition between the checkpoint at promotion and
pg_rewind. When a server is promoted, the startup process writes an
end-of-recovery checkpoint that includes the new TLI, and the server is
immediate opened for business. The startup process requests the
checkpointer process to perform a checkpoint, but it can take a few
seconds or more to complete. If you run pg_rewind, using the just
promoted server as the source, pg_rewind will think that the server is
still on the old timeline, because it only looks at TLI in the control
file's copy of the checkpoint record. That's not updated until the
checkpoint is finished.

This isn't a new issue. Stephen Frost first reported it back 2015 [1].
Back then, it was deemed just a small annoyance, and we just worked
around it in the tests by issuing a checkpoint command after promotion,
to wait for the checkpoint to finish. I just ran into it again today,
with the new pg_rewind test, and silenced it in the similar way.

I think we should fix this properly. I'm not sure if it can lead to a
broken cluster, but at least it can cause pg_rewind to fail
unnecessarily and in a user-unfriendly way. But this is actually pretty
simple to fix. pg_rewind looks at the control file to find out the
timeline the server is on. When promotion happens, the startup process
updates minRecoveryPoint and minRecoveryPointTLI fields in the control
file. We just need to read it from there. Patch attached.

I think we should also backpatch this. Back in 2015, we decided that we
can live with this, but it's always been a bit bogus, and seems simple
enough to fix.



- Heikki

Attachment Content-Type Size
0001-pg_rewind-Fix-determining-TLI-when-server-was-just-p.patch text/x-patch 8.6 KB


Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Geoghegan 2020-12-07 18:56:09 Re: Yet another fast GiST build
Previous Message Stephen Frost 2020-12-07 17:53:29 Change default of checkpoint_completion_target