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

From: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
To: Stepan Neretin <slpmcf(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix archive restore race that could unlink WAL before rename
Date: 2026-07-30 06:19:16
Message-ID: 7F76E997-3482-4CDF-AE85-1E4AB361CAEE@yandex-team.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On 24 Jul 2026, at 20:29, Stepan Neretin <slpmcf(at)gmail(dot)com> 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nikita Malakhov 2026-07-30 06:24:38 Re: Direct Toast PoC
Previous Message Nisha Moond 2026-07-30 06:19:05 Re: Support EXCEPT for TABLES IN SCHEMA publications