Re: Fix archive restore race that could unlink WAL before rename

Поиск
Список
Период
Сортировка
Искать

Re: Fix archive restore race that could unlink WAL before rename

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:


> On 24 Jul 2026, at 20:29, Stepan Neretin  wrote:
> 
>  Hi,
> 
>   KeepFileRestoredFromArchive() used to unlink an existing WAL segment
>   before renaming the restored file into place on non-Windows. Between
>   those two steps the segment is missing from pg_wal, so a concurrent
>   walsender (or anything else looking at that path) can observe a gap.
> 
>   On POSIX, durable_rename() already replaces the target atomically, so
>   the prior unlink is unnecessary. Windows still needs the
>   rename-to-.deletedN + unlink dance, so that path is unchanged.

Yup, the change seems correct to me.

The explicit unlink predates durable_rename(): it was added in 2012,
while this call site was switched to durable_rename() in 2016 by
1d4a0ab19a7.

>   The attached patch drops the non-Windows unlink and adds a TAP test that
>   uses an injection point between the old unlink site and the rename to
>   show the segment remains present.

The test needs some work. It fails on Windows [0]. Test has precisely
zero comments. It is named after walsender, but never starts a walsender.
advance_wal() switches WAL segments, but it does not wait for the archiver.

It would be good to actually produce walsender error to demonstrate the race.

Thanks!


Best regards, Andrey Borodin.

[0] https://github.com/x4m/postgres_g/actions/runs/30458446069/job/90598673326

Re: Fix archive restore race that could unlink WAL before rename

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Thu, Aug 6, 2026 at 8:39 AM Michael Paquier  wrote:
>
> On Thu, Aug 06, 2026 at 07:16:43AM +0900, Michael Paquier wrote:
> > I'll do that, and likely apply the result.  Another part is if this
> > should be backpatched or not, but I always find these recovery changes
> > quite stressing to do in stable branches, so for now I'd tend to just
> > do this change on HEAD, also due to the fact that we don't seem to
> > have reports about this tiny window being a problem in practice.
>
> I have added a note about the window still existing on WIN32, and
> applied the result as 1200dfd60c36 on HEAD.

Thanks for working on this!

This isn't directly related to this commit, but while reading the modified code
I found a small pre-existing issue in the WIN32 path.

In KeepFileRestoredFromArchive(), if unlink(oldpath) fails, the error message
reports xlogfpath, even though the file being removed is actually oldpath:

      if (unlink(oldpath) != 0)
          ereport(FATAL,
                  (errcode_for_file_access(),
                   errmsg("could not remove file \"%s\": %m",
                          xlogfpath)));

Should we fix that as well?

Regards,

-- 
Fujii Masao


FAQ