Re: Timing issue in pg_basebackup

From: Jesper St John <jesper(at)stjohn(dot)se>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: Timing issue in pg_basebackup
Date: 2026-10-21 17:16:18
Message-ID: 4417791388e8f1df20f64013e944be8e@stjohn.se
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 2026-10-20 23:11, Kirill Reshke wrote:
> On Mon, 19 Oct 2026 at 19:33, Jesper St John <jesper(at)stjohn(dot)se> wrote:
>>
>> Hey!
>>
>> I recently came across this issue when running pg_basebackup from 2
>> nodes at the same time through ansible. One of the nodes sometimes
>> reported: "requested WAL segment ... has already been removed". Tested
>> on v16, v17 and v18. The workaround was running them in sequence or
>> pause between runs.
>>
>> It looks like there's a timing issue here:
>> 1. Backup A starts and runs a checkpoint - needs WAL from segment 2.
>> 2. Backup A hasn't reserved that WAL yet.
>> 3. Backup B starts and runs a checkpoint (or anyone runs a checkpoint)
>> -
>> deletes segment 2.
>> 4. Backup A asks for segment 2, which is gone, and fails.
>
> Hi!
> Looks like this is a real issue.

Thanks for looking into it.

>
>> I've included a patch that moves the replication slot handing to run
>> before the backup part as a separate function.
>>
>> Some possible caveats already:
>> * Since moving the slot creation, we might run in to trouble if for
>> example, --checkpoint=spread is used since the slot is open for
>> longer.
>> * If any error arises in the backup part, now there will be a
>> lingering
>> slot when previously no slot was created.
>
> lingering slots looks like a real regression here. can we remove them
> when basebackup client disconnects?

A lingering slot could also happen today - only with this patch there's
a higher risk of it happening.

Would some cleanup code in a function and use it with atexit() work?

>
> NIT: comment in PrepareLogStreamer loses its meaning after code motion:
>
> + /* Get a second connection */
> + bgconn = GetConnection();
>

I've moved the code to its own function - the comment were there
already.

>
>> *** DISCLAIMER ***
>> I've used an LLM to help me write the test - I'm rubbish at Perl.
>
> OK

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Dmytro Astapov 2026-10-21 17:44:37 Re: BUG #19725: PostgreSQL 18.6: pg_restore read failure with io_uring, not observed with worker
Previous Message Fujii Masao 2026-10-21 14:47:53 Re: Inconsistency in psql output with \d+x PATTERN