pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Timing issue in pg_basebackup
18+ messages / 7 participants
[nested] [flat]

* Timing issue in pg_basebackup
@ 2026-10-19 14:33 Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Jesper St John @ 2026-10-19 14:33 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org

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.

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.

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

Love to get some feedback.

Regards,
Jesper St John

Attachments:

v1-0001-pg_basebackup-Create-the-WAL-streaming-slot-befor.patch (text/x-diff, 9.9K) download From de8268b69969531952ca411b5c1490b4bb660b7f Mon Sep 17 00:00:00 2001 From: Jesper St John <jesper@stjohn.se> Date: Mon, 19 Oct 2026 15:35:59 +0200 Subject: [PATCH v1] pg_basebackup: Create the WAL streaming slot before starting the backup With --wal-method=stream, pg_basebackup created its replication slot only after BASE_BACKUP had returned the backup's start position. A checkpoint running in between could remove WAL from that position onwards, and the backup then failed with "requested WAL segment ... has already been removed". Open the WAL streaming connection and create its slot before sending BASE_BACKUP, rather than after it has returned the start position, so the slot reserves all the WAL the backup needs. Add a TAP test that uses an injection point to hold slot creation while WAL is advanced and a checkpoint is run. --- src/bin/pg_basebackup/Makefile | 4 +- src/bin/pg_basebackup/meson.build | 2 + src/bin/pg_basebackup/pg_basebackup.c | 96 ++++++++++++------- .../pg_basebackup/t/012_wal_slot_timing.pl | 82 ++++++++++++++++ 4 files changed, 149 insertions(+), 35 deletions(-) create mode 100644 src/bin/pg_basebackup/t/012_wal_slot_timing.pl diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile index df94fc27d02..12ff32e329b 100644 --- a/src/bin/pg_basebackup/Makefile +++ b/src/bin/pg_basebackup/Makefile @@ -12,13 +12,15 @@ PGFILEDESC = "pg_basebackup/pg_receivewal/pg_recvlogical - streaming WAL and backup receivers" PGAPPICON=win32 -EXTRA_INSTALL=contrib/test_decoding +EXTRA_INSTALL=contrib/test_decoding \ + src/test/modules/injection_points subdir = src/bin/pg_basebackup top_builddir = ../../.. include $(top_builddir)/src/Makefile.global # make these available to TAP test scripts +export enable_injection_points export LZ4 export TAR # Note that GZIP cannot be used directly as this environment variable is diff --git a/src/bin/pg_basebackup/meson.build b/src/bin/pg_basebackup/meson.build index d70ce5786a2..c10a56512a0 100644 --- a/src/bin/pg_basebackup/meson.build +++ b/src/bin/pg_basebackup/meson.build @@ -96,10 +96,12 @@ tests += { 'env': {'GZIP_PROGRAM': gzip.found() ? gzip.full_path() : '', 'TAR': tar.found() ? tar.full_path() : '', 'LZ4': program_lz4.found() ? program_lz4.full_path() : '', + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', }, 'tests': [ 't/010_pg_basebackup.pl', 't/011_in_place_tablespace.pl', + 't/012_wal_slot_timing.pl', 't/020_pg_receivewal.pl', 't/030_pg_recvlogical.pl', 't/040_pg_createsubscriber.pl', diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index c3b87a19e76..e48e75ddd6d 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -165,6 +165,9 @@ static bool found_existing_xlogdir = false; static bool made_tablespace_dirs = false; static bool found_tablespace_dirs = false; +/* Connection for the background WAL receiver, see PrepareLogStreamer() */ +static PGconn *bgconn = NULL; + /* Progress indicators */ static uint64 totalsize_kb; static uint64 totaldone; @@ -607,6 +610,55 @@ LogStreamerMain(logstreamer_param *param) return 0; } +/* + * Open the connection for the background WAL receiver and create its + * replication slot, if one is needed. + * + * This must be done before BASE_BACKUP is sent: the slot reserves WAL from + * its creation onwards, so creating it first guarantees that no WAL from the + * backup's start position onwards can be removed before the receiver starts + * streaming. If the slot were created only after BASE_BACKUP had returned + * the start position, a checkpoint in between could remove WAL the backup + * needs. + */ +static void +PrepareLogStreamer(void) +{ + /* Get a second connection */ + bgconn = GetConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + + /* Temporary replication slots are only supported in 10 and newer */ + if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) + temp_replication_slot = false; + + /* + * Create replication slot if requested + */ + if (temp_replication_slot && !replication_slot) + replication_slot = psprintf("pg_basebackup_%u", + (unsigned int) PQbackendPID(bgconn)); + if (temp_replication_slot || create_slot) + { + if (!CreateReplicationSlot(bgconn, replication_slot, NULL, + temp_replication_slot, true, true, false, + false, false)) + exit(1); + + if (verbose) + { + if (temp_replication_slot) + pg_log_info("created temporary replication slot \"%s\"", + replication_slot); + else + pg_log_info("created replication slot \"%s\"", + replication_slot); + } + } +} + /* * Initiate background process for receiving xlog during the backup. * The background stream will use its own database connection so we can @@ -620,7 +672,10 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, logstreamer_param *param; char statusdir[MAXPGPATH]; + Assert(bgconn != NULL); + param = pg_malloc0_object(logstreamer_param); + param->bgconn = bgconn; param->timeline = timeline; param->sysidentifier = sysidentifier; param->wal_compress_algorithm = wal_compress_algorithm; @@ -639,46 +694,12 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, pg_fatal("could not create pipe for background process: %m"); #endif - /* Get a second connection */ - param->bgconn = GetConnection(); - if (!param->bgconn) - /* Error message already written in GetConnection() */ - exit(1); - /* In post-10 cluster, pg_xlog has been renamed to pg_wal */ snprintf(param->xlog, sizeof(param->xlog), "%s/%s", basedir, PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ? "pg_xlog" : "pg_wal"); - /* Temporary replication slots are only supported in 10 and newer */ - if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) - temp_replication_slot = false; - - /* - * Create replication slot if requested - */ - if (temp_replication_slot && !replication_slot) - replication_slot = psprintf("pg_basebackup_%u", - (unsigned int) PQbackendPID(param->bgconn)); - if (temp_replication_slot || create_slot) - { - if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL, - temp_replication_slot, true, true, false, - false, false)) - exit(1); - - if (verbose) - { - if (temp_replication_slot) - pg_log_info("created temporary replication slot \"%s\"", - replication_slot); - else - pg_log_info("created replication slot \"%s\"", - replication_slot); - } - } - if (format == 'p') { /* @@ -1970,6 +1991,13 @@ BaseBackup(char *compression_algorithm, char *compression_detail, compression_detail); } + /* + * If streaming WAL, create the replication slot before starting the + * backup, so that it protects all the WAL the backup needs. + */ + if (includewal == STREAM_WAL) + PrepareLogStreamer(); + if (verbose) pg_log_info("initiating base backup, waiting for checkpoint to complete"); diff --git a/src/bin/pg_basebackup/t/012_wal_slot_timing.pl b/src/bin/pg_basebackup/t/012_wal_slot_timing.pl new file mode 100644 index 00000000000..0de5e24058b --- /dev/null +++ b/src/bin/pg_basebackup/t/012_wal_slot_timing.pl @@ -0,0 +1,82 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Check that pg_basebackup's replication slot reserves WAL from before the +# backup's start position. If the slot were created only after BASE_BACKUP +# had returned the start position, a checkpoint running in between could +# remove the WAL the backup needs, and streaming it would fail. +# +# We hold the slot creation with an injection point, force WAL past several +# segments and checkpoint, then let the slot creation continue. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $tempdir = PostgreSQL::Test::Utils::tempdir; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init(allows_streaming => 1); +$node->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +checkpoint_timeout = 1h +}); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +$node->safe_psql('postgres', + "SELECT injection_points_attach('replication-slot-create-begin', 'wait')" +); + +my $backupdir = "$tempdir/backup"; +my ($stdout, $stderr); +my $h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $backupdir, + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + '>' => \$stdout, + '2>' => \$stderr); + +# Wait until the walsender is about to create pg_basebackup's slot. +$node->wait_for_event('walsender', 'replication-slot-create-begin'); + +# Move WAL forward by several segments and checkpoint, so that every segment +# not protected by a slot or wal_keep_size is removed or recycled. +$node->advance_wal(3); +$node->safe_psql('postgres', 'CHECKPOINT'); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('replication-slot-create-begin')"); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('replication-slot-create-begin')"); + +my $ok = $h->finish; +ok($ok, 'pg_basebackup succeeds with WAL activity during slot creation') + or diag("pg_basebackup stderr:\n$stderr"); +unlike( + $stderr, + qr/has already been removed/, + 'no WAL needed by the backup was removed'); + +# pg_verifybackup also parses the WAL the backup needs, so this fails if +# any of it is missing. +$node->command_ok([ 'pg_verifybackup', $backupdir ], + 'backup with streamed WAL verifies'); + +done_testing(); -- 2.39.5
^ permalink raw reply [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
@ 2026-10-20 21:11 ` Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  0 siblings, 1 reply; 18+ messages in thread

From: Kirill Reshke @ 2026-10-20 21:11 UTC (permalink / raw)
  To: Jesper St John <jesper@stjohn.se>; +Cc: pgsql-bugs@lists.postgresql.org

On Mon, 19 Oct 2026 at 19:33, Jesper St John <jesper@stjohn.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.

> 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?

NIT: comment in PrepareLogStreamer loses its meaning after code motion:

+ /* Get a second connection */
+ bgconn = GetConnection();


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

OK



-- 
Best regards,
Kirill Reshke






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
@ 2026-10-21 17:16   ` Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Jesper St John @ 2026-10-21 17:16 UTC (permalink / raw)
  To: Kirill Reshke <reshkekirill@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

On 2026-10-20 23:11, Kirill Reshke wrote:
> On Mon, 19 Oct 2026 at 19:33, Jesper St John <jesper@stjohn.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






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
@ 2026-10-22 05:44     ` Kirill Reshke <reshkekirill@gmail.com>
  2026-10-29 19:26       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  0 siblings, 2 replies; 18+ messages in thread

From: Kirill Reshke @ 2026-10-22 05:44 UTC (permalink / raw)
  To: Jesper St John <jesper@stjohn.se>; +Cc: pgsql-bugs@lists.postgresql.org

On Wed, 21 Oct 2026 at 22:16, Jesper St John <jesper@stjohn.se> wrote:
>
> On 2026-10-20 23:11, Kirill Reshke wrote:

> >
> >> 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.

Higher risk of lingering slots is also a regression. But looks like
this risk is anyway close to zero.

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

Something like this should do, can be made a separate patch thought

> >
> > 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.
>

Yep, the point is It looks very out-of-context here. Like, the first
line of the function says we're taking a second connection. Second to
what (to basebackup)? Maybe rephrase here as 'Get a wal-serving
connection' or something.



-- 
Best regards,
Kirill Reshke






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
@ 2026-10-29 19:26       ` Jesper St John <jesper@stjohn.se>
  1 sibling, 0 replies; 18+ messages in thread

From: Jesper St John @ 2026-10-29 19:26 UTC (permalink / raw)
  To: Kirill Reshke <reshkekirill@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

Hi!

Here are 2 v2-patches as per your suggestion.

I'm a bit unsure if the check in 
drop_lingering_replication_slot_atexit() function is enough to not break 
anything.
Or too much.

Suggestions welcome, could also make it into 1 patch if you prefer that.

//Jesper

On 2026-10-22 07:44, Kirill Reshke wrote:
> On Wed, 21 Oct 2026 at 22:16, Jesper St John <jesper@stjohn.se> wrote:
>> 
>> On 2026-10-20 23:11, Kirill Reshke wrote:
> 
>> >
>> >> 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.
> 
> Higher risk of lingering slots is also a regression. But looks like
> this risk is anyway close to zero.
> 
>> Would some cleanup code in a function and use it with atexit() work?
> 
> Something like this should do, can be made a separate patch thought
> 
>> >
>> > 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.
>> 
> 
> Yep, the point is It looks very out-of-context here. Like, the first
> line of the function says we're taking a second connection. Second to
> what (to basebackup)? Maybe rephrase here as 'Get a wal-serving
> connection' or something.

Attachments: download all (2 files, .tar.gz)

v2-0002-pg_basebackup-Drop-a-lingering-slot-if-the-backup.patch (text/x-diff, 3.9K) download From a66192994b42d09a2a4134b5e655d4fa7c15aa61 Mon Sep 17 00:00:00 2001 From: Jesper St John <jesper@stjohn.se> Date: Wed, 28 Oct 2026 11:22:27 +0100 Subject: [PATCH v2 2/2] pg_basebackup: Drop a lingering slot if the backup fails to start Register an atexit handler, once the slot has been created, that drops it if pg_basebackup exits before the background WAL receiver has taken over the connection. Add tests to 010_pg_basebackup.pl that use a bogus --manifest-checksums value to fail BASE_BACKUP after the slot has been created, and check that only the new slot is dropped. --- src/bin/pg_basebackup/pg_basebackup.c | 23 +++++++++++ src/bin/pg_basebackup/t/010_pg_basebackup.pl | 42 ++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 18e36451290..86144f01aa4 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -317,6 +317,22 @@ kill_bgchild_atexit(void) } #endif +/* + * Drop the lingering slot created by PrepareLogStreamer() if we fail before the + * background WAL receiver has taken over its connection. Only registered + * once the slot exists, so a pre-existing slot of the same name should never + * be dropped. + */ +static void +drop_lingering_replication_slot_atexit(void) +{ + /* Check that we have a good state */ + if (success || in_log_streamer || bgchild > 0) + return; + + (void) DropReplicationSlot(bgconn, replication_slot); +} + /* * Split argument into old_dir and new_dir and append to tablespace mapping * list. @@ -647,6 +663,13 @@ PrepareLogStreamer(void) false, false)) exit(1); + /* + * Only register atexit function if create_slot is true. Temporary + * slots go away with connection + */ + if (create_slot) + atexit(drop_lingering_replication_slot_atexit); + if (verbose) { if (temp_replication_slot) diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl index 2442131e179..b6a31703262 100644 --- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl +++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl @@ -828,9 +828,51 @@ $node->command_fails_like( qr/replication slot "slot0" already exists/, 'pg_basebackup fails with --create-slot --slot and a previously existing slot' ); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot0'} + ), + 'slot0', + 'previously existing slot is not dropped when --create-slot fails'); + +# The slot is created before BASE_BACKUP is sent, so a failure of +# BASE_BACKUP itself must not leave a slot created by --create-slot behind. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot2", + '--create-slot', + '--slot' => 'slot2', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --create-slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT count(*) FROM pg_replication_slots WHERE slot_name = 'slot2'} + ), + '0', + 'slot created by failed pg_basebackup --create-slot is dropped'); $node->safe_psql('postgres', q{SELECT * FROM pg_create_physical_replication_slot('slot1')}); + +# Without --create-slot, a failed backup must leave the given slot alone. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot1_fail", + '--slot' => 'slot1', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot1'} + ), + 'slot1', + 'existing slot is not dropped when pg_basebackup --slot fails'); my $lsn = $node->safe_psql('postgres', q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'} ); -- 2.56.0
v2-0001-pg_basebackup-Create-the-WAL-streaming-slot-befor.patch (text/x-diff, 9.9K) download From c7ddb97cbd7f923cd9407b1d51313f86fbf587de Mon Sep 17 00:00:00 2001 From: Jesper St John <jesper@stjohn.se> Date: Mon, 19 Oct 2026 15:35:59 +0200 Subject: [PATCH v2 1/2] pg_basebackup: Create the WAL streaming slot before starting the backup With --wal-method=stream, pg_basebackup created its replication slot only after BASE_BACKUP had returned the backup's start position. A checkpoint running in between could remove WAL from that position onwards, and the backup then failed with "requested WAL segment ... has already been removed". Open the WAL streaming connection and create its slot before sending BASE_BACKUP, rather than after it has returned the start position, so the slot reserves all the WAL the backup needs. Add a TAP test that uses an injection point to hold slot creation while WAL is advanced and a checkpoint is run. --- src/bin/pg_basebackup/Makefile | 4 +- src/bin/pg_basebackup/meson.build | 2 + src/bin/pg_basebackup/pg_basebackup.c | 96 ++++++++++++------- .../pg_basebackup/t/012_wal_slot_timing.pl | 82 ++++++++++++++++ 4 files changed, 149 insertions(+), 35 deletions(-) create mode 100644 src/bin/pg_basebackup/t/012_wal_slot_timing.pl diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile index df94fc27d02..12ff32e329b 100644 --- a/src/bin/pg_basebackup/Makefile +++ b/src/bin/pg_basebackup/Makefile @@ -12,13 +12,15 @@ PGFILEDESC = "pg_basebackup/pg_receivewal/pg_recvlogical - streaming WAL and backup receivers" PGAPPICON=win32 -EXTRA_INSTALL=contrib/test_decoding +EXTRA_INSTALL=contrib/test_decoding \ + src/test/modules/injection_points subdir = src/bin/pg_basebackup top_builddir = ../../.. include $(top_builddir)/src/Makefile.global # make these available to TAP test scripts +export enable_injection_points export LZ4 export TAR # Note that GZIP cannot be used directly as this environment variable is diff --git a/src/bin/pg_basebackup/meson.build b/src/bin/pg_basebackup/meson.build index d70ce5786a2..c10a56512a0 100644 --- a/src/bin/pg_basebackup/meson.build +++ b/src/bin/pg_basebackup/meson.build @@ -96,10 +96,12 @@ tests += { 'env': {'GZIP_PROGRAM': gzip.found() ? gzip.full_path() : '', 'TAR': tar.found() ? tar.full_path() : '', 'LZ4': program_lz4.found() ? program_lz4.full_path() : '', + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', }, 'tests': [ 't/010_pg_basebackup.pl', 't/011_in_place_tablespace.pl', + 't/012_wal_slot_timing.pl', 't/020_pg_receivewal.pl', 't/030_pg_recvlogical.pl', 't/040_pg_createsubscriber.pl', diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index c3b87a19e76..18e36451290 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -165,6 +165,9 @@ static bool found_existing_xlogdir = false; static bool made_tablespace_dirs = false; static bool found_tablespace_dirs = false; +/* Connection for the background WAL receiver, see PrepareLogStreamer() */ +static PGconn *bgconn = NULL; + /* Progress indicators */ static uint64 totalsize_kb; static uint64 totaldone; @@ -607,6 +610,55 @@ LogStreamerMain(logstreamer_param *param) return 0; } +/* + * Open the connection for the background WAL receiver and create its + * replication slot, if one is needed. + * + * This must be done before BASE_BACKUP is sent: the slot reserves WAL from + * its creation onwards, so creating it first guarantees that no WAL from the + * backup's start position onwards can be removed before the receiver starts + * streaming. If the slot were created only after BASE_BACKUP had returned + * the start position, a checkpoint in between could remove WAL the backup + * needs. + */ +static void +PrepareLogStreamer(void) +{ + /* Get a WAL serving connection */ + bgconn = GetConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + + /* Temporary replication slots are only supported in 10 and newer */ + if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) + temp_replication_slot = false; + + /* + * Create replication slot if requested + */ + if (temp_replication_slot && !replication_slot) + replication_slot = psprintf("pg_basebackup_%u", + (unsigned int) PQbackendPID(bgconn)); + if (temp_replication_slot || create_slot) + { + if (!CreateReplicationSlot(bgconn, replication_slot, NULL, + temp_replication_slot, true, true, false, + false, false)) + exit(1); + + if (verbose) + { + if (temp_replication_slot) + pg_log_info("created temporary replication slot \"%s\"", + replication_slot); + else + pg_log_info("created replication slot \"%s\"", + replication_slot); + } + } +} + /* * Initiate background process for receiving xlog during the backup. * The background stream will use its own database connection so we can @@ -620,7 +672,10 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, logstreamer_param *param; char statusdir[MAXPGPATH]; + Assert(bgconn != NULL); + param = pg_malloc0_object(logstreamer_param); + param->bgconn = bgconn; param->timeline = timeline; param->sysidentifier = sysidentifier; param->wal_compress_algorithm = wal_compress_algorithm; @@ -639,46 +694,12 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, pg_fatal("could not create pipe for background process: %m"); #endif - /* Get a second connection */ - param->bgconn = GetConnection(); - if (!param->bgconn) - /* Error message already written in GetConnection() */ - exit(1); - /* In post-10 cluster, pg_xlog has been renamed to pg_wal */ snprintf(param->xlog, sizeof(param->xlog), "%s/%s", basedir, PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ? "pg_xlog" : "pg_wal"); - /* Temporary replication slots are only supported in 10 and newer */ - if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) - temp_replication_slot = false; - - /* - * Create replication slot if requested - */ - if (temp_replication_slot && !replication_slot) - replication_slot = psprintf("pg_basebackup_%u", - (unsigned int) PQbackendPID(param->bgconn)); - if (temp_replication_slot || create_slot) - { - if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL, - temp_replication_slot, true, true, false, - false, false)) - exit(1); - - if (verbose) - { - if (temp_replication_slot) - pg_log_info("created temporary replication slot \"%s\"", - replication_slot); - else - pg_log_info("created replication slot \"%s\"", - replication_slot); - } - } - if (format == 'p') { /* @@ -1970,6 +1991,13 @@ BaseBackup(char *compression_algorithm, char *compression_detail, compression_detail); } + /* + * If streaming WAL, create the replication slot before starting the + * backup, so that it protects all the WAL the backup needs. + */ + if (includewal == STREAM_WAL) + PrepareLogStreamer(); + if (verbose) pg_log_info("initiating base backup, waiting for checkpoint to complete"); diff --git a/src/bin/pg_basebackup/t/012_wal_slot_timing.pl b/src/bin/pg_basebackup/t/012_wal_slot_timing.pl new file mode 100644 index 00000000000..0de5e24058b --- /dev/null +++ b/src/bin/pg_basebackup/t/012_wal_slot_timing.pl @@ -0,0 +1,82 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Check that pg_basebackup's replication slot reserves WAL from before the +# backup's start position. If the slot were created only after BASE_BACKUP +# had returned the start position, a checkpoint running in between could +# remove the WAL the backup needs, and streaming it would fail. +# +# We hold the slot creation with an injection point, force WAL past several +# segments and checkpoint, then let the slot creation continue. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $tempdir = PostgreSQL::Test::Utils::tempdir; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init(allows_streaming => 1); +$node->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +checkpoint_timeout = 1h +}); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +$node->safe_psql('postgres', + "SELECT injection_points_attach('replication-slot-create-begin', 'wait')" +); + +my $backupdir = "$tempdir/backup"; +my ($stdout, $stderr); +my $h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $backupdir, + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + '>' => \$stdout, + '2>' => \$stderr); + +# Wait until the walsender is about to create pg_basebackup's slot. +$node->wait_for_event('walsender', 'replication-slot-create-begin'); + +# Move WAL forward by several segments and checkpoint, so that every segment +# not protected by a slot or wal_keep_size is removed or recycled. +$node->advance_wal(3); +$node->safe_psql('postgres', 'CHECKPOINT'); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('replication-slot-create-begin')"); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('replication-slot-create-begin')"); + +my $ok = $h->finish; +ok($ok, 'pg_basebackup succeeds with WAL activity during slot creation') + or diag("pg_basebackup stderr:\n$stderr"); +unlike( + $stderr, + qr/has already been removed/, + 'no WAL needed by the backup was removed'); + +# pg_verifybackup also parses the WAL the backup needs, so this fails if +# any of it is missing. +$node->command_ok([ 'pg_verifybackup', $backupdir ], + 'backup with streamed WAL verifies'); + +done_testing(); -- 2.56.0
^ permalink raw reply [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
@ 2026-11-09 13:19       ` Jesper St John <jesper@stjohn.se>
  2026-11-12 16:12         ` Re: Timing issue in pg_basebackup Joao Rito <joaovictor.rito@gmail.com>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  1 sibling, 2 replies; 18+ messages in thread

From: Jesper St John @ 2026-11-09 13:19 UTC (permalink / raw)
  To: Kirill Reshke <reshkekirill@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

Added this patch to the CF app.

Made it into one and rebased it.

//Jesper

On 2026-10-22 07:44, Kirill Reshke wrote:
> On Wed, 21 Oct 2026 at 22:16, Jesper St John <jesper@stjohn.se> wrote:
>> 
>> On 2026-10-20 23:11, Kirill Reshke wrote:
> 
>> >
>> >> 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.
> 
> Higher risk of lingering slots is also a regression. But looks like
> this risk is anyway close to zero.
> 
>> Would some cleanup code in a function and use it with atexit() work?
> 
> Something like this should do, can be made a separate patch thought
> 
>> >
>> > 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.
>> 
> 
> Yep, the point is It looks very out-of-context here. Like, the first
> line of the function says we're taking a second connection. Second to
> what (to basebackup)? Maybe rephrase here as 'Get a wal-serving
> connection' or something.

Attachments:

v3-0001-pg_basebackup-Create-the-WAL-streaming-slot-befor.patch (text/x-diff, 13.1K) download From fc95dd067f316913a8e0f75dca63c4dcf3bde0a0 Mon Sep 17 00:00:00 2001 From: Jesper St John <jesper@stjohn.se> Date: Mon, 19 Oct 2026 15:35:59 +0200 Subject: [PATCH v3] pg_basebackup: Create the WAL streaming slot before starting the backup With --wal-method=stream, pg_basebackup created its replication slot only after BASE_BACKUP had returned the backup's start position. A checkpoint running in between could remove WAL from that position onwards, and the backup then failed with "requested WAL segment ... has already been removed". Open the WAL streaming connection and create its slot before sending BASE_BACKUP, rather than after it has returned the start position, so the slot reserves all the WAL the backup needs. Add a new TAP test that uses an injection point to hold slot creation while WAL is advanced and a checkpoint is run. Register an atexit handler, once the slot has been created, that drops it if pg_basebackup exits before the background WAL receiver has taken over the connection. Add tests to existing 010_pg_basebackup.pl that use a bogus --manifest-checksums value to fail BASE_BACKUP after the slot has been created, and check that only the new slot is dropped. --- src/bin/pg_basebackup/Makefile | 4 +- src/bin/pg_basebackup/meson.build | 2 + src/bin/pg_basebackup/pg_basebackup.c | 119 +++++++++++++----- src/bin/pg_basebackup/t/010_pg_basebackup.pl | 42 +++++++ .../pg_basebackup/t/013_wal_slot_timing.pl | 82 ++++++++++++ 5 files changed, 214 insertions(+), 35 deletions(-) create mode 100644 src/bin/pg_basebackup/t/013_wal_slot_timing.pl diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile index df94fc27d02..12ff32e329b 100644 --- a/src/bin/pg_basebackup/Makefile +++ b/src/bin/pg_basebackup/Makefile @@ -12,13 +12,15 @@ PGFILEDESC = "pg_basebackup/pg_receivewal/pg_recvlogical - streaming WAL and backup receivers" PGAPPICON=win32 -EXTRA_INSTALL=contrib/test_decoding +EXTRA_INSTALL=contrib/test_decoding \ + src/test/modules/injection_points subdir = src/bin/pg_basebackup top_builddir = ../../.. include $(top_builddir)/src/Makefile.global # make these available to TAP test scripts +export enable_injection_points export LZ4 export TAR # Note that GZIP cannot be used directly as this environment variable is diff --git a/src/bin/pg_basebackup/meson.build b/src/bin/pg_basebackup/meson.build index 2d3b22ff06e..43905ed9fc3 100644 --- a/src/bin/pg_basebackup/meson.build +++ b/src/bin/pg_basebackup/meson.build @@ -96,11 +96,13 @@ tests += { 'env': {'GZIP_PROGRAM': gzip.found() ? gzip.full_path() : '', 'TAR': tar.found() ? tar.full_path() : '', 'LZ4': program_lz4.found() ? program_lz4.full_path() : '', + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', }, 'tests': [ 't/010_pg_basebackup.pl', 't/011_in_place_tablespace.pl', 't/012_incremental_progress.pl', + 't/013_wal_slot_timing.pl', 't/020_pg_receivewal.pl', 't/030_pg_recvlogical.pl', 't/040_pg_createsubscriber.pl', diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index c3b87a19e76..86144f01aa4 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -165,6 +165,9 @@ static bool found_existing_xlogdir = false; static bool made_tablespace_dirs = false; static bool found_tablespace_dirs = false; +/* Connection for the background WAL receiver, see PrepareLogStreamer() */ +static PGconn *bgconn = NULL; + /* Progress indicators */ static uint64 totalsize_kb; static uint64 totaldone; @@ -314,6 +317,22 @@ kill_bgchild_atexit(void) } #endif +/* + * Drop the lingering slot created by PrepareLogStreamer() if we fail before the + * background WAL receiver has taken over its connection. Only registered + * once the slot exists, so a pre-existing slot of the same name should never + * be dropped. + */ +static void +drop_lingering_replication_slot_atexit(void) +{ + /* Check that we have a good state */ + if (success || in_log_streamer || bgchild > 0) + return; + + (void) DropReplicationSlot(bgconn, replication_slot); +} + /* * Split argument into old_dir and new_dir and append to tablespace mapping * list. @@ -607,6 +626,62 @@ LogStreamerMain(logstreamer_param *param) return 0; } +/* + * Open the connection for the background WAL receiver and create its + * replication slot, if one is needed. + * + * This must be done before BASE_BACKUP is sent: the slot reserves WAL from + * its creation onwards, so creating it first guarantees that no WAL from the + * backup's start position onwards can be removed before the receiver starts + * streaming. If the slot were created only after BASE_BACKUP had returned + * the start position, a checkpoint in between could remove WAL the backup + * needs. + */ +static void +PrepareLogStreamer(void) +{ + /* Get a WAL serving connection */ + bgconn = GetConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + + /* Temporary replication slots are only supported in 10 and newer */ + if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) + temp_replication_slot = false; + + /* + * Create replication slot if requested + */ + if (temp_replication_slot && !replication_slot) + replication_slot = psprintf("pg_basebackup_%u", + (unsigned int) PQbackendPID(bgconn)); + if (temp_replication_slot || create_slot) + { + if (!CreateReplicationSlot(bgconn, replication_slot, NULL, + temp_replication_slot, true, true, false, + false, false)) + exit(1); + + /* + * Only register atexit function if create_slot is true. Temporary + * slots go away with connection + */ + if (create_slot) + atexit(drop_lingering_replication_slot_atexit); + + if (verbose) + { + if (temp_replication_slot) + pg_log_info("created temporary replication slot \"%s\"", + replication_slot); + else + pg_log_info("created replication slot \"%s\"", + replication_slot); + } + } +} + /* * Initiate background process for receiving xlog during the backup. * The background stream will use its own database connection so we can @@ -620,7 +695,10 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, logstreamer_param *param; char statusdir[MAXPGPATH]; + Assert(bgconn != NULL); + param = pg_malloc0_object(logstreamer_param); + param->bgconn = bgconn; param->timeline = timeline; param->sysidentifier = sysidentifier; param->wal_compress_algorithm = wal_compress_algorithm; @@ -639,46 +717,12 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, pg_fatal("could not create pipe for background process: %m"); #endif - /* Get a second connection */ - param->bgconn = GetConnection(); - if (!param->bgconn) - /* Error message already written in GetConnection() */ - exit(1); - /* In post-10 cluster, pg_xlog has been renamed to pg_wal */ snprintf(param->xlog, sizeof(param->xlog), "%s/%s", basedir, PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ? "pg_xlog" : "pg_wal"); - /* Temporary replication slots are only supported in 10 and newer */ - if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) - temp_replication_slot = false; - - /* - * Create replication slot if requested - */ - if (temp_replication_slot && !replication_slot) - replication_slot = psprintf("pg_basebackup_%u", - (unsigned int) PQbackendPID(param->bgconn)); - if (temp_replication_slot || create_slot) - { - if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL, - temp_replication_slot, true, true, false, - false, false)) - exit(1); - - if (verbose) - { - if (temp_replication_slot) - pg_log_info("created temporary replication slot \"%s\"", - replication_slot); - else - pg_log_info("created replication slot \"%s\"", - replication_slot); - } - } - if (format == 'p') { /* @@ -1970,6 +2014,13 @@ BaseBackup(char *compression_algorithm, char *compression_detail, compression_detail); } + /* + * If streaming WAL, create the replication slot before starting the + * backup, so that it protects all the WAL the backup needs. + */ + if (includewal == STREAM_WAL) + PrepareLogStreamer(); + if (verbose) pg_log_info("initiating base backup, waiting for checkpoint to complete"); diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl index 23a757b1c86..a5c05aec1bf 100644 --- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl +++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl @@ -829,9 +829,51 @@ $node->command_fails_like( qr/replication slot "slot0" already exists/, 'pg_basebackup fails with --create-slot --slot and a previously existing slot' ); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot0'} + ), + 'slot0', + 'previously existing slot is not dropped when --create-slot fails'); + +# The slot is created before BASE_BACKUP is sent, so a failure of +# BASE_BACKUP itself must not leave a slot created by --create-slot behind. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot2", + '--create-slot', + '--slot' => 'slot2', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --create-slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT count(*) FROM pg_replication_slots WHERE slot_name = 'slot2'} + ), + '0', + 'slot created by failed pg_basebackup --create-slot is dropped'); $node->safe_psql('postgres', q{SELECT * FROM pg_create_physical_replication_slot('slot1')}); + +# Without --create-slot, a failed backup must leave the given slot alone. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot1_fail", + '--slot' => 'slot1', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot1'} + ), + 'slot1', + 'existing slot is not dropped when pg_basebackup --slot fails'); my $lsn = $node->safe_psql('postgres', q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'} ); diff --git a/src/bin/pg_basebackup/t/013_wal_slot_timing.pl b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl new file mode 100644 index 00000000000..0de5e24058b --- /dev/null +++ b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl @@ -0,0 +1,82 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Check that pg_basebackup's replication slot reserves WAL from before the +# backup's start position. If the slot were created only after BASE_BACKUP +# had returned the start position, a checkpoint running in between could +# remove the WAL the backup needs, and streaming it would fail. +# +# We hold the slot creation with an injection point, force WAL past several +# segments and checkpoint, then let the slot creation continue. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $tempdir = PostgreSQL::Test::Utils::tempdir; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init(allows_streaming => 1); +$node->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +checkpoint_timeout = 1h +}); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +$node->safe_psql('postgres', + "SELECT injection_points_attach('replication-slot-create-begin', 'wait')" +); + +my $backupdir = "$tempdir/backup"; +my ($stdout, $stderr); +my $h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $backupdir, + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + '>' => \$stdout, + '2>' => \$stderr); + +# Wait until the walsender is about to create pg_basebackup's slot. +$node->wait_for_event('walsender', 'replication-slot-create-begin'); + +# Move WAL forward by several segments and checkpoint, so that every segment +# not protected by a slot or wal_keep_size is removed or recycled. +$node->advance_wal(3); +$node->safe_psql('postgres', 'CHECKPOINT'); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('replication-slot-create-begin')"); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('replication-slot-create-begin')"); + +my $ok = $h->finish; +ok($ok, 'pg_basebackup succeeds with WAL activity during slot creation') + or diag("pg_basebackup stderr:\n$stderr"); +unlike( + $stderr, + qr/has already been removed/, + 'no WAL needed by the backup was removed'); + +# pg_verifybackup also parses the WAL the backup needs, so this fails if +# any of it is missing. +$node->command_ok([ 'pg_verifybackup', $backupdir ], + 'backup with streamed WAL verifies'); + +done_testing(); -- 2.56.0
^ permalink raw reply [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
@ 2026-11-12 16:12         ` Joao Rito <joaovictor.rito@gmail.com>
  1 sibling, 0 replies; 18+ messages in thread

From: Joao Rito @ 2026-11-12 16:12 UTC (permalink / raw)
  To: Jesper St John <jesper@stjohn.se>; +Cc: pgsql-bugs@lists.postgresql.org; Kirill Reshke <reshkekirill@gmail.com>

Hi Jesper,

Thank you for the patch. I tested v3-0001 (CF entry 7613) and here are my
results.

Setup: master at c1694b7 (11 Nov 2026), configured with --enable-cassert,
--enable-tap-tests and --enable-injection-points, on Linux (WSL Ubuntu). The
patch applied cleanly with git apply.

1. The bug on unpatched master

I held the walsender at the existing injection point
replication-slot-create-begin (so BASE_BACKUP has returned, but the slot
does not exist yet). Then I switched WAL four times, ran CHECKPOINT, and
released the walsender. The server was running with wal_keep_size=0.

  pg_basebackup: error: unexpected termination of replication stream: ERROR:
  requested WAL segment 000000010000000000000002 has already been removed
  pg_basebackup: error: background process terminated unexpectedly

pg_basebackup exited with 1. So the race you describe is real, and it can be
triggered on demand.

2. With v3 applied

The same script gives pg_basebackup exit 0, no error in stderr or in the
server log, "backup successfully verified" from pg_verifybackup, and no
leftover slot.

3. Tests

"make -C src/bin/pg_basebackup check" with the patch: all 7 files pass (372
tests), including the new 013_wal_slot_timing.pl and the new cases in
010_pg_basebackup.pl. I ran 013 alone with prove -v to confirm it was not
skipped: "ok 1 - pg_basebackup succeeds with WAL activity during slot
creation", "ok 2 - no WAL needed by the backup was removed", "ok 3 - backup
with streamed WAL verifies", Files=1, Tests=3, Result: PASS. As a negative
control, I kept the new tests but put back the original pg_basebackup.c.
Then 013 fails all 3 subtests with the same "has already been removed"
error, so the test does detect the bug.

4. A note on lingering slots and signals

You and Kirill discussed the lingering slot in the thread. I tried to see
what happens on a signal. With v3 the slot is created before the checkpoint,
so the window in which an interrupted pg_basebackup can leave the slot
behind now also covers the checkpoint wait. With --checkpoint=spread that
can be long.

To compare fairly, I did the test after the slot exists on both builds (on
master the slot is only created after BASE_BACKUP returns, so a SIGTERM
during the checkpoint finds no slot there). I ran pg_basebackup -Xstream
--create-slot -S s1 --checkpoint=fast --max-rate=1M on a 216 MB database,
polled pg_replication_slots until s1 appeared, and then sent SIGTERM while
the backup was still running:

- master: s1 was still there after SIGTERM (and still there 11 s later).
- with v3: s1 was still there after SIGTERM (and still there 11 s later).

So master also leaves the slot behind on SIGTERM once the slot exists. I
think this is expected, since an atexit() handler probably does not run when
the process is killed by a signal, but I did not test that part directly. v3
does not make this worse for a signal after the slot is created. It only
starts the window earlier, so it is longer in time (the checkpoint wait).
The slot is not temporary here because I used --create-slot. The walsender
state differed between runs: in the first run s1 showed as inactive right
after the kill with v3 (active on master), and in a second run s1 was still
active on both builds 11 s after the kill.

Your atexit() handler works for the error cases covered by the TAP tests. If
you want to cover signals too, maybe a signal handler could drop the slot,
or the docs for --create-slot / --checkpoint could say that a slot may
remain if the process is killed. This is just a suggestion, I do not see it
as a regression of the patch. I only tested SIGTERM.

Small comment on the code: the check "success || in_log_streamer || bgchild
> 0" looks fine to me in the cases I ran, but I did not review all exit
paths in detail.

What I did NOT test:
- Other platforms (Windows, where pg_basebackup uses a thread instead of
  fork, and the new atexit() and bgconn handling matter).
- Other WAL methods and formats (--wal-method=fetch/none, tar format,
  compressed WAL, --format=tar with stream) beyond what the existing TAP
  suite covers.
- Older server versions (the MINIMUM_VERSION_FOR_TEMP_SLOTS branch).
- SIGINT, and failures after the background WAL receiver has started.
- Meson build (I used make/autoconf only), and the docs/pgindent formatting.
- The real two-node ansible scenario from your first mail (I only reproduced
  the race with an injection point), and the performance effect of holding
  the slot during long spread checkpoints (only the slot visibility was
  checked, above).
- Whether other tools that use the same pattern (for example pg_receivewal
  --create-slot) need the same change.

The scripts and the full log are available if you want them. Thanks again
for working on this.

Best regards,
Joao Rito






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
@ 2026-11-12 21:19         ` Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  1 sibling, 1 reply; 18+ messages in thread

From: Zsolt Parragi @ 2026-11-12 21:19 UTC (permalink / raw)
  To: Jesper St John <jesper@stjohn.se>; +Cc: pgsql-bugs@lists.postgresql.org; Kirill Reshke <reshkekirill@gmail.com>

Hello

I can also confirm the race on master and that v3 fixes it. However:

> * 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.

Moving the slot creation in front of BASE_BACKUP causes other new
failures, exactly like the concern you mentioned in the first email,
and I don't think overall it is better than the current issue.

1. The new slot can be invalidated by the backup's own checkpoint

A physical slot starts with the redo pointer of the previous
checkpoint. With the patch it is created, released (so it is
inactive), and stays like that for the whole start-of-backup
checkpoint. At the end of that checkpoint we call
InvalidateObsoleteReplicationSlots(), and our slot is one of the
candidates for invalidation, and can be invalidated based on
max_slot_wal_keep_size or idle_replication_slot_timeout.

This results in backup failures in multiple scenarios, with server log
entries such as

DETAIL: The slot's restart_lsn 0/8B317610 exceeds the limit by 164530672 bytes.

2. The second connection is killed by idle_session_timeout

bgconn is now opened before BASE_BACKUP and sits idle in walsender
command mode until the checkpoint is done. PostgresMain() arms
IDLE_SESSION_TIMEOUT for walsenders too, so with a cluster-wide
idle_session_timeout set and a checkpoint longer than that, the
walsender exits (dropping the temporary slot, and with it the
protection the patch adds), and the backup fails after the checkpoint:

pg_basebackup: error: could not send replication command "IDENTIFY_SYSTEM":
FATAL: terminating connection due to idle-session timeout

(2) is fixable, but (1) is a more tricky problem and I trickier to
solve. Maybe immediately after the slot is created we could use
READ_REPLICATION_SLOT and the START_REPLICATION with the restart_lsn?

Alternatively, instead of reordering, since this is an unlikely race,
maybe early detection and retry could be also an option instead of
reordering the operations? After creating the slot, check restart_lsn:
if it is behond startptr's segment then drop the connection and
restart.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
@ 2026-11-16 10:26           ` Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  0 siblings, 1 reply; 18+ messages in thread

From: Magnus Hagander @ 2026-11-16 10:26 UTC (permalink / raw)
  To: Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: Jesper St John <jesper@stjohn.se>; pgsql-bugs@lists.postgresql.org; Kirill Reshke <reshkekirill@gmail.com>

On Thu, 12 Nov 2026 at 22:19, Zsolt Parragi <zsolt.parragi@percona.com>
wrote:

> Hello
>
> I can also confirm the race on master and that v3 fixes it. However:
>
> * 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.
>
> Moving the slot creation in front of BASE_BACKUP causes other new
> failures, exactly like the concern you mentioned in the first email,
> and I don't think overall it is better than the current issue.
>
> 1. The new slot can be invalidated by the backup's own checkpoint
>
> A physical slot starts with the redo pointer of the previous
> checkpoint. With the patch it is created, released (so it is
> inactive), and stays like that for the whole start-of-backup
> checkpoint. At the end of that checkpoint we call
> InvalidateObsoleteReplicationSlots(), and our slot is one of the
> candidates for invalidation, and can be invalidated based on
> max_slot_wal_keep_size or idle_replication_slot_timeout.
>
> This results in backup failures in multiple scenarios, with server log
> entries such as
>
> DETAIL: The slot's restart_lsn 0/8B317610 exceeds the limit by 164530672
> bytes.


> 2. The second connection is killed by idle_session_timeout
>
> bgconn is now opened before BASE_BACKUP and sits idle in walsender
> command mode until the checkpoint is done. PostgresMain() arms
> IDLE_SESSION_TIMEOUT for walsenders too, so with a cluster-wide
> idle_session_timeout set and a checkpoint longer than that, the
> walsender exits (dropping the temporary slot, and with it the
> protection the patch adds), and the backup fails after the checkpoint:
>
> pg_basebackup: error: could not send replication command "IDENTIFY_SYSTEM":
> FATAL: terminating connection due to idle-session timeout
>
> (2) is fixable, but (1) is a more tricky problem and I trickier to
> solve. Maybe immediately after the slot is created we could use
> READ_REPLICATION_SLOT and the START_REPLICATION with the restart_lsn?
>
> Alternatively, instead of reordering, since this is an unlikely race,
> maybe early detection and retry could be also an option instead of
> reordering the operations? After creating the slot, check restart_lsn:
> if it is behond startptr's segment then drop the connection and
> restart.
>

Per some off-list discussion with Jesper. I don't think that wil work, as
READ_REPLICATION_SLOT won't actually advance the last use time on it, so it
would still time out.

I think the correct thing to do here is to start the full log streamer
*before* starting the base backup. This will lead to including just a
little bit unnecessarily much WAL in the backup, but it won't be much - we
already need to keep the WAL back to the start of the base backup
checkpoint start. So the only extra WAL to be included would be between the
log streaming started and the base backup command running, which would not
be much. The only real difference would be the streaming starts already
during the checkpoint, so with a slow cheakpoing it won't start the
streamer with a large backlog.

-- 
 Magnus Hagander
 Me: https://www.hagander.net/ <http://www.hagander.net/;
 Work: https://www.redpill-linpro.com/ <http://www.redpill-linpro.com/;

^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
@ 2026-11-16 12:08             ` Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
  0 siblings, 1 reply; 18+ messages in thread

From: Andrey Borodin @ 2026-11-16 12:08 UTC (permalink / raw)
  To: Magnus Hagander <magnus@hagander.net>; +Cc: Zsolt Parragi <zsolt.parragi@percona.com>; Jesper St John <jesper@stjohn.se>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>

Hi Magnus, all,

Nick reported the same startpoint/slot race in August [0], tracked in
CF 7233 [1]. The client patch there opens the streaming connection and
creates its slot before BASE_BACKUP, but starts streaming only after
receiving the backup startpoint. Your proposal goes further than that.

We also discussed cleanup of a newly created permanent slot when
BASE_BACKUP refuses to start. Zsolt's reports of slot invalidation and
idle timeout are relevant to that approach too.

Could we combine the work and keep both threads linked from one CF
entry? This thread is currently tracked separately as CF 7613 [2].

Thank you!

Best regards, Andrey Borodin.

[0] https://www.postgresql.org/message-id/CALP_NYTTvKzJ+chEUXLsyGaM+HYcP1WwGwXkZEnDfv-7FbPowQ@mail.gmail...
[1] https://commitfest.postgresql.org/patch/7233/
[2] https://commitfest.postgresql.org/patch/7613/






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
@ 2026-11-17 11:35               ` Jesper St John <admin@zonezero.se>
  2026-11-20 15:04                 ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  0 siblings, 1 reply; 18+ messages in thread

From: Jesper St John @ 2026-11-17 11:35 UTC (permalink / raw)
  To: Andrey Borodin <x4mmm@yandex-team.ru>; +Cc: Magnus Hagander <magnus@hagander.net>; Zsolt Parragi <zsolt.parragi@percona.com>; Jesper St John <jesper@stjohn.se>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>

Hi!

Sorry, even though I’m pretty sure I searched for similar issues before posting, I missed it.

Big thanks to Zsolt, Joao and Kirill for your input.

I think combining work sounds like the reasonable thing to do. I’d be happy to help out in either thread.

If any of you already know if one patch is preferable over the other, please do tell. If not I’ll review and try posting a follow-up.

I’ve slowly started to implement a “start streaming directly after slot creation”-functionality and perhaps that could work with either patch.

//Jesper


Sent from my iPhone

> On 16 Nov 2026, at 13:09, Andrey Borodin <x4mmm@yandex-team.ru> wrote:
> Hi Magnus, all,
> 
> Nick reported the same startpoint/slot race in August [0], tracked in
> CF 7233 [1]. The client patch there opens the streaming connection and
> creates its slot before BASE_BACKUP, but starts streaming only after
> receiving the backup startpoint. Your proposal goes further than that.
> 
> We also discussed cleanup of a newly created permanent slot when
> BASE_BACKUP refuses to start. Zsolt's reports of slot invalidation and
> idle timeout are relevant to that approach too.
> 
> Could we combine the work and keep both threads linked from one CF
> entry? This thread is currently tracked separately as CF 7613 [2].
> 
> Thank you!
> 
> Best regards, Andrey Borodin.
> 
> [0] https://www.postgresql.org/message-id/CALP_NYTTvKzJ+chEUXLsyGaM+HYcP1WwGwXkZEnDfv-7FbPowQ@mail.gmail...
> [1] https://commitfest.postgresql.org/patch/7233/
> [2] https://commitfest.postgresql.org/patch/7613/






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
@ 2026-11-20 15:04                 ` Jesper St John <jesper@stjohn.se>
  2026-11-20 22:59                   ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Jesper St John @ 2026-11-20 15:04 UTC (permalink / raw)
  To: Jesper St John <admin@zonezero.se>; +Cc: Andrey Borodin <x4mmm@yandex-team.ru>; Magnus Hagander <magnus@hagander.net>; Zsolt Parragi <zsolt.parragi@percona.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>

Here's a v4 patch that starts streaming immediately after slot creation 
on v15+ versions, and as a fallback if needed - use the previous flow. 
It is probably/most likely not done, but good to go for another round on 
the list if anyone want to have a go at it.

I have not yet merged anything from previous thread, they seem to do the 
same thing apart from the early streaming.

Many thanks to Magnus for off-list discussions and valuable feedback.

//Jesper




On 2026-11-17 12:35, Jesper St John wrote:
> Hi!
> 
> Sorry, even though I’m pretty sure I searched for similar issues before 
> posting, I missed it.
> 
> Big thanks to Zsolt, Joao and Kirill for your input.
> 
> I think combining work sounds like the reasonable thing to do. I’d be 
> happy to help out in either thread.
> 
> If any of you already know if one patch is preferable over the other, 
> please do tell. If not I’ll review and try posting a follow-up.
> 
> I’ve slowly started to implement a “start streaming directly after slot 
> creation”-functionality and perhaps that could work with either patch.
> 
> //Jesper
> 
> 
> Sent from my iPhone
> 
>> On 16 Nov 2026, at 13:09, Andrey Borodin <x4mmm@yandex-team.ru> wrote:
>> Hi Magnus, all,
>> 
>> Nick reported the same startpoint/slot race in August [0], tracked in
>> CF 7233 [1]. The client patch there opens the streaming connection and
>> creates its slot before BASE_BACKUP, but starts streaming only after
>> receiving the backup startpoint. Your proposal goes further than that.
>> 
>> We also discussed cleanup of a newly created permanent slot when
>> BASE_BACKUP refuses to start. Zsolt's reports of slot invalidation and
>> idle timeout are relevant to that approach too.
>> 
>> Could we combine the work and keep both threads linked from one CF
>> entry? This thread is currently tracked separately as CF 7613 [2].
>> 
>> Thank you!
>> 
>> Best regards, Andrey Borodin.
>> 
>> [0] 
>> https://www.postgresql.org/message-id/CALP_NYTTvKzJ+chEUXLsyGaM+HYcP1WwGwXkZEnDfv-7FbPowQ@mail.gmail...
>> [1] https://commitfest.postgresql.org/patch/7233/
>> [2] https://commitfest.postgresql.org/patch/7613/

Attachments:

v4-0001-pg_basebackup-Create-and-start-WAL-streaming-slot.patch (text/x-diff, 23.0K) download From 4d9fc283b9704abc2816294103aa30d4b0267878 Mon Sep 17 00:00:00 2001 From: Jesper St John <jesper@stjohn.se> Date: Mon, 19 Oct 2026 15:35:59 +0200 Subject: [PATCH v4] pg_basebackup: Create and start WAL streaming slot before starting backup With --wal-method=stream, pg_basebackup created its replication slot only after BASE_BACKUP had returned the backup's start position. A checkpoint running in between could remove WAL from that position onwards, and the backup then failed with "requested WAL segment ... has already been removed". Open the WAL streaming connection and create its slot before sending BASE_BACKUP, so that the slot reserves all the WAL the backup needs. Creating the slot early means it sits idle during the backup's checkpoint, which can take minutes with --checkpoint=spread. From version 18, idle_replication_slot_timeout can invalidate an idle slot at the end of a checkpoint, and the backup would then fail. To prevent that, also start the WAL receiver before BASE_BACKUP, so that the slot is in use. It streams from the slot's restart position, which is read with READ_REPLICATION_SLOT (version 15 and later). After BASE_BACKUP, check that the stream does not start after the backup's start position or on a later timeline. Without a slot, on older servers, or if the slot has no restart position, the receiver is started after BASE_BACKUP as before. As a result, the backup can contain WAL from before its start position, and a slot created with --create-slot is left behind if BASE_BACKUP fails. Document both. Add a TAP test with two cases. The first holds slot creation with an injection point while WAL is advanced and a checkpoint is run. The second holds the backup's checkpoint before it invalidates slots, and uses slot-timeout-inval to check that pg_basebackup's slot is in use by then. Also add tests in 010_pg_basebackup.pl for the slots left after BASE_BACKUP fails. --- doc/src/sgml/ref/pg_basebackup.sgml | 18 +- src/bin/pg_basebackup/Makefile | 4 +- src/bin/pg_basebackup/meson.build | 2 + src/bin/pg_basebackup/pg_basebackup.c | 219 +++++++++++++----- src/bin/pg_basebackup/t/010_pg_basebackup.pl | 44 ++++ .../pg_basebackup/t/013_wal_slot_timing.pl | 147 ++++++++++++ 6 files changed, 373 insertions(+), 61 deletions(-) create mode 100644 src/bin/pg_basebackup/t/013_wal_slot_timing.pl diff --git a/doc/src/sgml/ref/pg_basebackup.sgml b/doc/src/sgml/ref/pg_basebackup.sgml index b4a0faccf41..6385190a10c 100644 --- a/doc/src/sgml/ref/pg_basebackup.sgml +++ b/doc/src/sgml/ref/pg_basebackup.sgml @@ -398,6 +398,21 @@ PostgreSQL documentation with the write-ahead log data, using this method requires no extra write-ahead logs to be saved on the source server. </para> + <para> + When a replication slot is used for streaming and the server is + version 15 or later, streaming begins at the slot's restart + position before the backup is started, rather than at the + backup's start position, so the backup can contain write-ahead + log from before its start position. For a slot created by + <application>pg_basebackup</application> (a temporary slot, or one + requested with <option>--create-slot</option>), the restart + position is the server's most recent checkpoint, or on a standby + its most recent restartpoint, at the time the slot is created. + For an existing slot given with <option>--slot</option>, it is + the slot's current restart position, which can be considerably + older. With older servers, or with <option>--no-slot</option>, + streaming begins at the backup's start position. + </para> <para> When tar format is used, the write-ahead log files will be written to a separate file named <filename>pg_wal.tar</filename> @@ -514,7 +529,8 @@ PostgreSQL documentation <para> Specifies that the replication slot named by the <literal>--slot</literal> option should be created before starting - the backup. An error is raised if the slot already exists. + the backup. An error is raised if the slot already exists. The slot + is not dropped if the backup subsequently fails. </para> </listitem> </varlistentry> diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile index df94fc27d02..12ff32e329b 100644 --- a/src/bin/pg_basebackup/Makefile +++ b/src/bin/pg_basebackup/Makefile @@ -12,13 +12,15 @@ PGFILEDESC = "pg_basebackup/pg_receivewal/pg_recvlogical - streaming WAL and backup receivers" PGAPPICON=win32 -EXTRA_INSTALL=contrib/test_decoding +EXTRA_INSTALL=contrib/test_decoding \ + src/test/modules/injection_points subdir = src/bin/pg_basebackup top_builddir = ../../.. include $(top_builddir)/src/Makefile.global # make these available to TAP test scripts +export enable_injection_points export LZ4 export TAR # Note that GZIP cannot be used directly as this environment variable is diff --git a/src/bin/pg_basebackup/meson.build b/src/bin/pg_basebackup/meson.build index 2d3b22ff06e..43905ed9fc3 100644 --- a/src/bin/pg_basebackup/meson.build +++ b/src/bin/pg_basebackup/meson.build @@ -96,11 +96,13 @@ tests += { 'env': {'GZIP_PROGRAM': gzip.found() ? gzip.full_path() : '', 'TAR': tar.found() ? tar.full_path() : '', 'LZ4': program_lz4.found() ? program_lz4.full_path() : '', + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', }, 'tests': [ 't/010_pg_basebackup.pl', 't/011_in_place_tablespace.pl', 't/012_incremental_progress.pl', + 't/013_wal_slot_timing.pl', 't/020_pg_receivewal.pl', 't/030_pg_recvlogical.pl', 't/040_pg_createsubscriber.pl', diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index c3b87a19e76..57218cf962a 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -103,6 +103,11 @@ typedef void (*WriteDataCallback) (size_t nbytes, char *buf, */ #define MINIMUM_VERSION_FOR_TERMINATED_TARFILE 150000 +/* + * READ_REPLICATION_SLOT supported from version 15 + */ +#define MINIMUM_VERSION_FOR_READ_REPLICATION_SLOT 150000 + /* * pg_wal/summaries exists beginning with version 17. */ @@ -165,6 +170,12 @@ static bool found_existing_xlogdir = false; static bool made_tablespace_dirs = false; static bool found_tablespace_dirs = false; +/* Connection for the background WAL receiver, see PrepareLogStreamer() */ +static PGconn *bgconn = NULL; + +/* Has the background WAL receiver been started? */ +static bool log_streamer_started = false; + /* Progress indicators */ static uint64 totalsize_kb; static uint64 totaldone; @@ -608,49 +619,31 @@ LogStreamerMain(logstreamer_param *param) } /* - * Initiate background process for receiving xlog during the backup. - * The background stream will use its own database connection so we can - * stream the logfile in parallel with the backups. + * Open the connection for the background WAL receiver and create its + * replication slot, if one is needed. + * + * This is done before BASE_BACKUP, so that the slot reserves all the WAL the + * backup needs before the backup's checkpoint removes any of it. + * + * The slot must then not sit idle during that checkpoint, which can be long + * with --checkpoint=spread, or idle_replication_slot_timeout (version 18+) + * may invalidate it. So if the slot's restart position can be read + * (version 15 and later), return true with it in *startptr and *starttli, + * and the caller starts the receiver there right away. Otherwise return + * false, and the receiver is started after BASE_BACKUP. */ -static void -StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, - pg_compress_algorithm wal_compress_algorithm, - int wal_compress_level) +static bool +PrepareLogStreamer(XLogRecPtr *startptr, TimeLineID *starttli) { - logstreamer_param *param; - char statusdir[MAXPGPATH]; + XLogRecPtr restart_lsn; + TimeLineID restart_tli; - param = pg_malloc0_object(logstreamer_param); - param->timeline = timeline; - param->sysidentifier = sysidentifier; - param->wal_compress_algorithm = wal_compress_algorithm; - param->wal_compress_level = wal_compress_level; - - /* Convert the starting position */ - if (!pg_parse_lsn(startpos, &param->startptr)) - pg_fatal("could not parse write-ahead log location \"%s\"", - startpos); - /* Round off to even segment position */ - param->startptr -= XLogSegmentOffset(param->startptr, WalSegSz); - -#ifndef WIN32 - /* Create our background pipe */ - if (pipe(bgpipe) < 0) - pg_fatal("could not create pipe for background process: %m"); -#endif - - /* Get a second connection */ - param->bgconn = GetConnection(); - if (!param->bgconn) + /* Get a WAL serving connection */ + bgconn = GetConnection(); + if (!bgconn) /* Error message already written in GetConnection() */ exit(1); - /* In post-10 cluster, pg_xlog has been renamed to pg_wal */ - snprintf(param->xlog, sizeof(param->xlog), "%s/%s", - basedir, - PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ? - "pg_xlog" : "pg_wal"); - /* Temporary replication slots are only supported in 10 and newer */ if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) temp_replication_slot = false; @@ -660,10 +653,10 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, */ if (temp_replication_slot && !replication_slot) replication_slot = psprintf("pg_basebackup_%u", - (unsigned int) PQbackendPID(param->bgconn)); + (unsigned int) PQbackendPID(bgconn)); if (temp_replication_slot || create_slot) { - if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL, + if (!CreateReplicationSlot(bgconn, replication_slot, NULL, temp_replication_slot, true, true, false, false, false)) exit(1); @@ -679,6 +672,65 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, } } + /* if --no-slot is used */ + if (replication_slot == NULL) + return false; + + /* GetSlotInformation doesn't exist pre version 15 */ + if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_READ_REPLICATION_SLOT) + return false; + + if (!GetSlotInformation(bgconn, replication_slot, + &restart_lsn, &restart_tli)) + exit(1); + + /* Check invalid xlog */ + if (!XLogRecPtrIsValid(restart_lsn) || restart_tli == 0) + return false; + + *startptr = restart_lsn; + *starttli = restart_tli; + + return true; +} + +/* + * Initiate background process for receiving xlog during the backup. + * The background stream will use its own database connection so we can + * stream the logfile in parallel with the backups. + */ +static void +StartLogStreamer(XLogRecPtr startpos, uint32 timeline, char *sysidentifier, + pg_compress_algorithm wal_compress_algorithm, + int wal_compress_level) +{ + logstreamer_param *param; + char statusdir[MAXPGPATH]; + + Assert(bgconn != NULL); + + param = pg_malloc0_object(logstreamer_param); + param->bgconn = bgconn; + param->timeline = timeline; + param->sysidentifier = sysidentifier; + param->wal_compress_algorithm = wal_compress_algorithm; + param->wal_compress_level = wal_compress_level; + + /* Round off to even segment */ + param->startptr = startpos - XLogSegmentOffset(startpos, WalSegSz); + +#ifndef WIN32 + /* Create our background pipe */ + if (pipe(bgpipe) < 0) + pg_fatal("could not create pipe for background process: %m"); +#endif + + /* In post-10 cluster, pg_xlog has been renamed to pg_wal */ + snprintf(param->xlog, sizeof(param->xlog), "%s/%s", + basedir, + PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ? + "pg_xlog" : "pg_wal"); + if (format == 'p') { /* @@ -734,6 +786,9 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, if (bgchild == 0) pg_fatal("could not create background thread: %m"); #endif + + /* Mark streaming as started */ + log_streamer_started = true; } /* @@ -1747,6 +1802,11 @@ BaseBackup(char *compression_algorithm, char *compression_detail, int i; char xlogstart[64]; char xlogend[64] = {0}; + XLogRecPtr xlogstartptr = InvalidXLogRecPtr; + XLogRecPtr streamstartptr = InvalidXLogRecPtr; + TimeLineID streamstarttli = 0; + pg_compress_algorithm wal_compress_algorithm = PG_COMPRESSION_NONE; + int wal_compress_level = 0; int minServerMajor, maxServerMajor; int serverVersion, @@ -1970,6 +2030,37 @@ BaseBackup(char *compression_algorithm, char *compression_detail, compression_detail); } + /* + * If streaming WAL, create the replication slot before starting the + * backup, so that it protects all the WAL the backup needs. Start the + * receiver right away if possible, so that the slot is in use, and so + * cannot be invalidated as idle, while the server performs the + * checkpoint. See PrepareLogStreamer(). + */ + if (includewal == STREAM_WAL) + { + if (client_compress->algorithm == PG_COMPRESSION_GZIP) + { + wal_compress_algorithm = PG_COMPRESSION_GZIP; + wal_compress_level = client_compress->level; + } + else + { + wal_compress_algorithm = PG_COMPRESSION_NONE; + wal_compress_level = 0; + } + + if (PrepareLogStreamer(&streamstartptr, &streamstarttli)) + { + if (verbose) + pg_log_info("starting background WAL receiver"); + + StartLogStreamer(streamstartptr, streamstarttli, sysidentifier, + wal_compress_algorithm, + wal_compress_level); + } + } + if (verbose) pg_log_info("initiating base backup, waiting for checkpoint to complete"); @@ -2023,6 +2114,26 @@ BaseBackup(char *compression_algorithm, char *compression_detail, pg_log_info("write-ahead log start point: %s on timeline %u", xlogstart, starttli); + if (includewal == STREAM_WAL) + { + if (!pg_parse_lsn(xlogstart, &xlogstartptr)) + pg_fatal("could not parse write-ahead log location \"%s\"", + xlogstart); + + /* + * If the WAL receiver is running, make sure the stream start is older + * than the basebackup start, in WAL and in timeline. An older stream + * timeline is fine, the receiver should follow any timeline switches. + */ + if (log_streamer_started && xlogstartptr < streamstartptr) + pg_fatal("backup start point %s is older than the start of the WAL stream at %X/%08X", + xlogstart, LSN_FORMAT_ARGS(streamstartptr)); + + if (log_streamer_started && streamstarttli > starttli) + pg_fatal("backup start timeline %u is older than the WAL stream's start timeline %u", + starttli, streamstarttli); + } + /* * Get the header */ @@ -2078,31 +2189,21 @@ BaseBackup(char *compression_algorithm, char *compression_detail, PQntuples(res)); /* - * If we're streaming WAL, start the streaming session before we start - * receiving the actual data chunks. + * If the receiver could not be started before BASE_BACKUP (no slot, a + * server older than version 15, or a slot without a restart position), + * start it now, at the backup's start position. */ if (includewal == STREAM_WAL) { - pg_compress_algorithm wal_compress_algorithm; - int wal_compress_level; - - if (verbose) - pg_log_info("starting background WAL receiver"); - - if (client_compress->algorithm == PG_COMPRESSION_GZIP) + if (!log_streamer_started) { - wal_compress_algorithm = PG_COMPRESSION_GZIP; - wal_compress_level = client_compress->level; - } - else - { - wal_compress_algorithm = PG_COMPRESSION_NONE; - wal_compress_level = 0; - } + if (verbose) + pg_log_info("starting background WAL receiver"); - StartLogStreamer(xlogstart, starttli, sysidentifier, - wal_compress_algorithm, - wal_compress_level); + StartLogStreamer(xlogstartptr, starttli, sysidentifier, + wal_compress_algorithm, + wal_compress_level); + } } if (serverMajor >= 1500) diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl index 23a757b1c86..367cc1da56f 100644 --- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl +++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl @@ -829,9 +829,53 @@ $node->command_fails_like( qr/replication slot "slot0" already exists/, 'pg_basebackup fails with --create-slot --slot and a previously existing slot' ); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot0'} + ), + 'slot0', + 'previously existing slot is not dropped when --create-slot fails'); + +# The slot is created before BASE_BACKUP is sent, and is not dropped if +# BASE_BACKUP itself fails. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot2", + '--create-slot', + '--slot' => 'slot2', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --create-slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT count(*) FROM pg_replication_slots WHERE slot_name = 'slot2'} + ), + '1', + 'slot created by failed pg_basebackup --create-slot is kept'); +$node->safe_psql('postgres', + q{SELECT pg_drop_replication_slot('slot2')}); $node->safe_psql('postgres', q{SELECT * FROM pg_create_physical_replication_slot('slot1')}); + +# Without --create-slot, a failed backup must leave the given slot alone. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot1_fail", + '--slot' => 'slot1', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot1'} + ), + 'slot1', + 'existing slot is not dropped when pg_basebackup --slot fails'); my $lsn = $node->safe_psql('postgres', q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'} ); diff --git a/src/bin/pg_basebackup/t/013_wal_slot_timing.pl b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl new file mode 100644 index 00000000000..01bb37eb880 --- /dev/null +++ b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl @@ -0,0 +1,147 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Check that pg_basebackup's replication slot reserves WAL from before the +# backup's start position. If the slot were created only after BASE_BACKUP +# had returned the start position, a checkpoint running in between could +# remove the WAL the backup needs, and streaming it would fail. +# +# We hold the slot creation with an injection point, force WAL past several +# segments and checkpoint, then let the slot creation continue. +# +# Also check that the slot, created before BASE_BACKUP, is already in use +# when the backup's checkpoint runs, so that the checkpoint cannot +# invalidate it under idle_replication_slot_timeout. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $tempdir = PostgreSQL::Test::Utils::tempdir; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init(allows_streaming => 1); +$node->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +checkpoint_timeout = 1h +idle_replication_slot_timeout = 1h +}); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +$node->safe_psql('postgres', + "SELECT injection_points_attach('replication-slot-create-begin', 'wait')" +); + +my $backupdir = "$tempdir/backup"; +my ($stdout, $stderr); +my $h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $backupdir, + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + '>' => \$stdout, + '2>' => \$stderr); + +# Wait until the walsender is about to create pg_basebackup's slot. +$node->wait_for_event('walsender', 'replication-slot-create-begin'); + +# Move WAL forward by several segments and checkpoint, so that every segment +# not protected by a slot or wal_keep_size is removed or recycled. +$node->advance_wal(3); +$node->safe_psql('postgres', 'CHECKPOINT'); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('replication-slot-create-begin')"); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('replication-slot-create-begin')"); + +my $ok = $h->finish; +ok($ok, 'pg_basebackup succeeds with WAL activity during slot creation') + or diag("pg_basebackup stderr:\n$stderr"); +unlike( + $stderr, + qr/has already been removed/, + 'no WAL needed by the backup was removed'); + +# pg_verifybackup also parses the WAL the backup needs, so this fails if +# any of it is missing. +$node->command_ok([ 'pg_verifybackup', $backupdir ], + 'backup with streamed WAL verifies'); + +# Now the idle timeout. idle_replication_slot_timeout is set, and with +# slot-timeout-inval attached, every checkpoint invalidates every slot that +# is idle at that moment, however briefly. Hold the backup's checkpoint just +# before it checks the slots, wait until pg_basebackup's slot is being +# streamed from, and then let the checkpoint continue. An idle slot created +# beforehand shows that the checkpoint does invalidate idle slots. +$node->safe_psql('postgres', + "SELECT pg_create_physical_replication_slot('idle_slot', true)"); +$node->safe_psql('postgres', + "SELECT injection_points_attach('slot-timeout-inval', 'error')"); +$node->safe_psql('postgres', + "SELECT injection_points_attach('checkpoint-before-old-wal-removal', 'wait')" +); + +my $backupdir2 = "$tempdir/backup_idle"; +$h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $backupdir2, + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + '>' => \$stdout, + '2>' => \$stderr); + +$node->wait_for_event('checkpointer', 'checkpoint-before-old-wal-removal'); + +# A slot that is being streamed from has no inactive_since. +my $in_use = $node->poll_query_until( + 'postgres', q{ + SELECT count(*) = 1 FROM pg_replication_slots + WHERE slot_name LIKE 'pg\_basebackup\_%' AND inactive_since IS NULL +}); +ok($in_use, "pg_basebackup's slot is in use during the backup's checkpoint"); + +# Detach before waking up, so that later checkpoints, such as the one at +# shutdown, do not wait. +$node->safe_psql('postgres', + "SELECT injection_points_detach('checkpoint-before-old-wal-removal')"); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('checkpoint-before-old-wal-removal')"); + +$ok = $h->finish; +ok($ok, 'pg_basebackup succeeds when idle slots are invalidated') + or diag("pg_basebackup stderr:\n$stderr"); + +is( $node->safe_psql( + 'postgres', + q{SELECT invalidation_reason FROM pg_replication_slots + WHERE slot_name = 'idle_slot'}), + 'idle_timeout', + 'idle slot was invalidated by the backup\'s checkpoint'); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('slot-timeout-inval')"); + +$node->command_ok([ 'pg_verifybackup', $backupdir2 ], + 'backup taken while idle slots are invalidated verifies'); + +done_testing(); -- 2.56.1
^ permalink raw reply [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
  2026-11-20 15:04                 ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
@ 2026-11-20 22:59                   ` Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-23 11:50                     ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  0 siblings, 1 reply; 18+ messages in thread

From: Zsolt Parragi @ 2026-11-20 22:59 UTC (permalink / raw)
  To: Jesper St John <jesper@stjohn.se>; +Cc: pgsql-bugs@lists.postgresql.org; Jesper St John <admin@zonezero.se>; Andrey Borodin <x4mmm@yandex-team.ru>; Magnus Hagander <magnus@hagander.net>; Kirill Reshke <reshkekirill@gmail.com>

This seems to break rebuilding cascading standbys: from the
intermediate standby, reusing its existing slot (casc stopped, then
pg_basebackup -h stby -S casc -X stream -R --checkpoint=fast):

master client:  stby redo=0/03000028 slot=0/08000060 -> rc=0
patched client: stby redo=0/08000060 slot=0/0D000000 -> rc=1
pg_basebackup: error: backup start point 0/08000060 is older than the
start of the WAL stream at 0/0D000000

And from my previous email:

> and can be invalidated based on max_slot_wal_keep_size

still holds with --checkpoint=false (it works with spread)

And another new issue is that now a failed backup can leave the
created slot on the server:

pg_basebackup -D x -X stream -C -S s1 --manifest-checksums=bogus
pg_basebackup -D x -X stream -C -S s1

--> ERROR:  replication slot "s1" already exists






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
  2026-11-20 15:04                 ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-20 22:59                   ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
@ 2026-11-23 11:50                     ` Jesper St John <jesper@stjohn.se>
  2026-11-23 22:16                       ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Jesper St John @ 2026-11-23 11:50 UTC (permalink / raw)
  To: Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: pgsql-bugs@lists.postgresql.org; Jesper St John <admin@zonezero.se>; Andrey Borodin <x4mmm@yandex-team.ru>; Magnus Hagander <magnus@hagander.net>; Kirill Reshke <reshkekirill@gmail.com>

It seems starting the stream early introduced more problems than it was 
solving.

Here's v5 - reverting back to v3 but adding a recreation function for 
those cases where the slot gets invalidated due to 
max_slot_wal_keep_size. Also should fix failed backup, lingering slots - 
errors, with the atexit function that was there in v3. It should also 
work with replicas.

This v5 does not handle any v18 idle_replication_slot_timeout errors.
But it seems that in master today, a previously created slot which would 
be specified by --slot and not --create-slot could also be invalidated 
if a tiny idle_replication_slot_timeout is set. If I'm not wrong, does a 
pre-base-backup slot creation count as a regression?

Would it make sense to make READ_REPLICATION_SLOT return NULL for 
restart_lsn instead of the restart_lsn even though the slot is 
invalidated? In that case my recreation-function would also handle 
those. That would be a separate server-side patch though.

Thanks for the feedback and reviews!

//Jesper



On 2026-11-20 23:59, Zsolt Parragi wrote:
> This seems to break rebuilding cascading standbys: from the
> intermediate standby, reusing its existing slot (casc stopped, then
> pg_basebackup -h stby -S casc -X stream -R --checkpoint=fast):
> 
> master client:  stby redo=0/03000028 slot=0/08000060 -> rc=0
> patched client: stby redo=0/08000060 slot=0/0D000000 -> rc=1
> pg_basebackup: error: backup start point 0/08000060 is older than the
> start of the WAL stream at 0/0D000000
> 
> And from my previous email:
> 
>> and can be invalidated based on max_slot_wal_keep_size
> 
> still holds with --checkpoint=false (it works with spread)
> 
> And another new issue is that now a failed backup can leave the
> created slot on the server:
> 
> pg_basebackup -D x -X stream -C -S s1 --manifest-checksums=bogus
> pg_basebackup -D x -X stream -C -S s1
> 
> --> ERROR:  replication slot "s1" already exists

Attachments:

v5-0001-pg_basebackup-Create-the-WAL-streaming-slot-befor.patch (text/x-diff, 22.0K) download From 7018daf62eb6f79aa26e855802cf7448dd02a169 Mon Sep 17 00:00:00 2001 From: Jesper St John <jesper@stjohn.se> Date: Mon, 23 Nov 2026 09:51:09 +0100 Subject: [PATCH v5] pg_basebackup: Create the WAL streaming slot before starting the backup With --wal-method=stream, pg_basebackup created its replication slot only after BASE_BACKUP had returned the backup's start position. A checkpoint running in between could remove WAL from that position onwards, and the backup then failed with "requested WAL segment ... has already been removed". Open the WAL streaming connection and create its slot before sending BASE_BACKUP, rather than after it has returned the start position, so the slot reserves all the WAL the backup needs. The slot now starts at the previous checkpoint's redo point and is idle until the WAL receiver starts after BASE_BACKUP, so the backup's own checkpoint can invalidate it, under max_slot_wal_keep_size or idle_replication_slot_timeout. That checkpoint keeps the WAL from the backup's start position onwards, so if it invalidated the slot, create the slot again before starting the receiver, and print a warning. A temporary slot is invalidated by terminating its connection, which also drops it, so reconnect first. A slot created with --create-slot loses its restart_lsn, which is checked on version 15 and later, and is dropped first. A checkpoint between the invalidation and the new slot could still remove WAL the backup needs, as before this change. For a slot created with --create-slot, register an atexit handler that drops it if pg_basebackup exits before the WAL receiver has started. A slot given with --slot but not created by pg_basebackup is never dropped or recreated. Document this under --wal-method=stream and --create-slot. Add a TAP test that uses an injection point to hold slot creation while WAL is advanced and a checkpoint is run. Also test that a slot invalidated by the backup's checkpoint is recreated: under max_slot_wal_keep_size for a temporary and a --create-slot slot, and under idle_replication_slot_timeout, using slot-timeout-inval, for a temporary slot. In 010_pg_basebackup.pl, use a bogus --manifest-checksums value to fail BASE_BACKUP after the slot has been created, and check that a slot created with --create-slot is dropped, while existing slots are not. --- doc/src/sgml/ref/pg_basebackup.sgml | 20 +- src/bin/pg_basebackup/Makefile | 4 +- src/bin/pg_basebackup/meson.build | 2 + src/bin/pg_basebackup/pg_basebackup.c | 182 +++++++++++++---- src/bin/pg_basebackup/t/010_pg_basebackup.pl | 42 ++++ .../pg_basebackup/t/013_wal_slot_timing.pl | 183 ++++++++++++++++++ 6 files changed, 397 insertions(+), 36 deletions(-) create mode 100644 src/bin/pg_basebackup/t/013_wal_slot_timing.pl diff --git a/doc/src/sgml/ref/pg_basebackup.sgml b/doc/src/sgml/ref/pg_basebackup.sgml index b4a0faccf41..605728a2d75 100644 --- a/doc/src/sgml/ref/pg_basebackup.sgml +++ b/doc/src/sgml/ref/pg_basebackup.sgml @@ -398,6 +398,21 @@ PostgreSQL documentation with the write-ahead log data, using this method requires no extra write-ahead logs to be saved on the source server. </para> + <para> + If <application>pg_basebackup</application> creates the + replication slot used for streaming (a temporary slot, or one + requested with <option>--create-slot</option>), it does so before + the backup is started, so that the slot reserves all the + write-ahead log the backup needs. If the checkpoint at the start + of the backup invalidates the slot, because of + <xref linkend="guc-max-slot-wal-keep-size"/> or + <xref linkend="guc-idle-replication-slot-timeout"/>, + <application>pg_basebackup</application> prints a warning and + creates the slot again. For a slot requested with + <option>--create-slot</option>, this is only done with servers of + version 15 or later, and only if the slot was invalidated because + of <varname>max_slot_wal_keep_size</varname>. + </para> <para> When tar format is used, the write-ahead log files will be written to a separate file named <filename>pg_wal.tar</filename> @@ -514,7 +529,10 @@ PostgreSQL documentation <para> Specifies that the replication slot named by the <literal>--slot</literal> option should be created before starting - the backup. An error is raised if the slot already exists. + the backup. An error is raised if the slot already exists, and the + existing slot is left untouched. If the backup fails after the slot + has been created but before write-ahead log streaming has started, + the slot is dropped. </para> </listitem> </varlistentry> diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile index df94fc27d02..12ff32e329b 100644 --- a/src/bin/pg_basebackup/Makefile +++ b/src/bin/pg_basebackup/Makefile @@ -12,13 +12,15 @@ PGFILEDESC = "pg_basebackup/pg_receivewal/pg_recvlogical - streaming WAL and backup receivers" PGAPPICON=win32 -EXTRA_INSTALL=contrib/test_decoding +EXTRA_INSTALL=contrib/test_decoding \ + src/test/modules/injection_points subdir = src/bin/pg_basebackup top_builddir = ../../.. include $(top_builddir)/src/Makefile.global # make these available to TAP test scripts +export enable_injection_points export LZ4 export TAR # Note that GZIP cannot be used directly as this environment variable is diff --git a/src/bin/pg_basebackup/meson.build b/src/bin/pg_basebackup/meson.build index 2d3b22ff06e..43905ed9fc3 100644 --- a/src/bin/pg_basebackup/meson.build +++ b/src/bin/pg_basebackup/meson.build @@ -96,11 +96,13 @@ tests += { 'env': {'GZIP_PROGRAM': gzip.found() ? gzip.full_path() : '', 'TAR': tar.found() ? tar.full_path() : '', 'LZ4': program_lz4.found() ? program_lz4.full_path() : '', + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', }, 'tests': [ 't/010_pg_basebackup.pl', 't/011_in_place_tablespace.pl', 't/012_incremental_progress.pl', + 't/013_wal_slot_timing.pl', 't/020_pg_receivewal.pl', 't/030_pg_recvlogical.pl', 't/040_pg_createsubscriber.pl', diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index c3b87a19e76..97965c9b5aa 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -103,6 +103,11 @@ typedef void (*WriteDataCallback) (size_t nbytes, char *buf, */ #define MINIMUM_VERSION_FOR_TERMINATED_TARFILE 150000 +/* + * READ_REPLICATION_SLOT exists beginning with version 15. + */ +#define MINIMUM_VERSION_FOR_READ_REPLICATION_SLOT 150000 + /* * pg_wal/summaries exists beginning with version 17. */ @@ -165,6 +170,9 @@ static bool found_existing_xlogdir = false; static bool made_tablespace_dirs = false; static bool found_tablespace_dirs = false; +/* Connection for the background WAL receiver, see PrepareLogStreamer() */ +static PGconn *bgconn = NULL; + /* Progress indicators */ static uint64 totalsize_kb; static uint64 totaldone; @@ -314,6 +322,22 @@ kill_bgchild_atexit(void) } #endif +/* + * Drop the lingering slot created by PrepareLogStreamer() if we fail before the + * background WAL receiver has taken over its connection. Only registered + * once the slot exists, so a pre-existing slot of the same name should never + * be dropped. + */ +static void +drop_lingering_replication_slot_atexit(void) +{ + /* Check that we have a good state */ + if (success || in_log_streamer || bgchild > 0) + return; + + (void) DropReplicationSlot(bgconn, replication_slot); +} + /* * Split argument into old_dir and new_dir and append to tablespace mapping * list. @@ -607,6 +631,118 @@ LogStreamerMain(logstreamer_param *param) return 0; } +/* + * Open the connection for the background WAL receiver and create its + * replication slot, if one is needed. + * + * This must be done before BASE_BACKUP is sent: the slot reserves WAL from + * its creation onwards, so creating it first guarantees that no WAL from the + * backup's start position onwards can be removed before the receiver starts + * streaming. If the slot were created only after BASE_BACKUP had returned + * the start position, a checkpoint in between could remove WAL the backup + * needs. + */ +static void +PrepareLogStreamer(void) +{ + /* Get a WAL serving connection */ + bgconn = GetConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + + /* Temporary replication slots are only supported in 10 and newer */ + if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) + temp_replication_slot = false; + + /* + * Create replication slot if requested + */ + if (temp_replication_slot && !replication_slot) + replication_slot = psprintf("pg_basebackup_%u", + (unsigned int) PQbackendPID(bgconn)); + if (temp_replication_slot || create_slot) + { + if (!CreateReplicationSlot(bgconn, replication_slot, NULL, + temp_replication_slot, true, true, false, + false, false)) + exit(1); + + /* + * Only register atexit function if create_slot is true. Temporary + * slots go away with connection + */ + if (create_slot) + atexit(drop_lingering_replication_slot_atexit); + + if (verbose) + { + if (temp_replication_slot) + pg_log_info("created temporary replication slot \"%s\"", + replication_slot); + else + pg_log_info("created replication slot \"%s\"", + replication_slot); + } + } +} + +/* + * Recreate our slot if the backup's checkpoint invalidated it. That + * checkpoint keeps the WAL from the backup's start position onwards, so a + * new slot still protects everything the backup needs. + * + * Invalidating a temporary slot terminates bgconn, which drops the slot. A + * --create-slot slot loses its restart_lsn instead (version 15 and later). + */ +static void +RecreateInvalidatedSlot(void) +{ + if (temp_replication_slot) + { + PGresult *res; + bool alive; + + /* Check the connection */ + res = PQexec(bgconn, "IDENTIFY_SYSTEM"); + alive = PQresultStatus(res) == PGRES_TUPLES_OK; + PQclear(res); + if (alive) + return; + + PQfinish(bgconn); + bgconn = GetConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + } + else if (create_slot && + PQserverVersion(conn) >= MINIMUM_VERSION_FOR_READ_REPLICATION_SLOT) + { + XLogRecPtr restart_lsn; + + if (!GetSlotInformation(bgconn, replication_slot, &restart_lsn, NULL)) + exit(1); + if (XLogRecPtrIsValid(restart_lsn)) + return; + + if (!DropReplicationSlot(bgconn, replication_slot)) + exit(1); + } + else + return; + + pg_log_warning("replication slot \"%s\" was invalidated during the base backup checkpoint, recreating it", + replication_slot); + pg_log_warning_hint("You might need to increase \"%s\".", + "max_slot_wal_keep_size"); + + if (!CreateReplicationSlot(bgconn, replication_slot, NULL, + temp_replication_slot, true, true, false, + false, false)) + exit(1); +} + /* * Initiate background process for receiving xlog during the backup. * The background stream will use its own database connection so we can @@ -620,7 +756,10 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, logstreamer_param *param; char statusdir[MAXPGPATH]; + Assert(bgconn != NULL); + param = pg_malloc0_object(logstreamer_param); + param->bgconn = bgconn; param->timeline = timeline; param->sysidentifier = sysidentifier; param->wal_compress_algorithm = wal_compress_algorithm; @@ -639,46 +778,12 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, pg_fatal("could not create pipe for background process: %m"); #endif - /* Get a second connection */ - param->bgconn = GetConnection(); - if (!param->bgconn) - /* Error message already written in GetConnection() */ - exit(1); - /* In post-10 cluster, pg_xlog has been renamed to pg_wal */ snprintf(param->xlog, sizeof(param->xlog), "%s/%s", basedir, PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ? "pg_xlog" : "pg_wal"); - /* Temporary replication slots are only supported in 10 and newer */ - if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) - temp_replication_slot = false; - - /* - * Create replication slot if requested - */ - if (temp_replication_slot && !replication_slot) - replication_slot = psprintf("pg_basebackup_%u", - (unsigned int) PQbackendPID(param->bgconn)); - if (temp_replication_slot || create_slot) - { - if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL, - temp_replication_slot, true, true, false, - false, false)) - exit(1); - - if (verbose) - { - if (temp_replication_slot) - pg_log_info("created temporary replication slot \"%s\"", - replication_slot); - else - pg_log_info("created replication slot \"%s\"", - replication_slot); - } - } - if (format == 'p') { /* @@ -1970,6 +2075,13 @@ BaseBackup(char *compression_algorithm, char *compression_detail, compression_detail); } + /* + * If streaming WAL, create the replication slot before starting the + * backup, so that it protects all the WAL the backup needs. + */ + if (includewal == STREAM_WAL) + PrepareLogStreamer(); + if (verbose) pg_log_info("initiating base backup, waiting for checkpoint to complete"); @@ -2086,6 +2198,8 @@ BaseBackup(char *compression_algorithm, char *compression_detail, pg_compress_algorithm wal_compress_algorithm; int wal_compress_level; + RecreateInvalidatedSlot(); + if (verbose) pg_log_info("starting background WAL receiver"); diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl index 23a757b1c86..a5c05aec1bf 100644 --- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl +++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl @@ -829,9 +829,51 @@ $node->command_fails_like( qr/replication slot "slot0" already exists/, 'pg_basebackup fails with --create-slot --slot and a previously existing slot' ); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot0'} + ), + 'slot0', + 'previously existing slot is not dropped when --create-slot fails'); + +# The slot is created before BASE_BACKUP is sent, so a failure of +# BASE_BACKUP itself must not leave a slot created by --create-slot behind. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot2", + '--create-slot', + '--slot' => 'slot2', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --create-slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT count(*) FROM pg_replication_slots WHERE slot_name = 'slot2'} + ), + '0', + 'slot created by failed pg_basebackup --create-slot is dropped'); $node->safe_psql('postgres', q{SELECT * FROM pg_create_physical_replication_slot('slot1')}); + +# Without --create-slot, a failed backup must leave the given slot alone. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot1_fail", + '--slot' => 'slot1', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'slot1'} + ), + 'slot1', + 'existing slot is not dropped when pg_basebackup --slot fails'); my $lsn = $node->safe_psql('postgres', q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'} ); diff --git a/src/bin/pg_basebackup/t/013_wal_slot_timing.pl b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl new file mode 100644 index 00000000000..dffc8f99269 --- /dev/null +++ b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl @@ -0,0 +1,183 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Check that pg_basebackup's replication slot reserves WAL from before the +# backup's start position. If the slot were created only after BASE_BACKUP +# had returned the start position, a checkpoint running in between could +# remove the WAL the backup needs, and streaming it would fail. +# +# We hold the slot creation with an injection point, force WAL past several +# segments and checkpoint, then let the slot creation continue. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $tempdir = PostgreSQL::Test::Utils::tempdir; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init(allows_streaming => 1); +$node->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +checkpoint_timeout = 1h +}); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +$node->safe_psql('postgres', + "SELECT injection_points_attach('replication-slot-create-begin', 'wait')" +); + +my $backupdir = "$tempdir/backup"; +my ($stdout, $stderr); +my $h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $backupdir, + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + '>' => \$stdout, + '2>' => \$stderr); + +# Wait until the walsender is about to create pg_basebackup's slot. +$node->wait_for_event('walsender', 'replication-slot-create-begin'); + +# Move WAL forward by several segments and checkpoint, so that every segment +# not protected by a slot or wal_keep_size is removed or recycled. +$node->advance_wal(3); +$node->safe_psql('postgres', 'CHECKPOINT'); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('replication-slot-create-begin')"); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('replication-slot-create-begin')"); + +my $ok = $h->finish; +ok($ok, 'pg_basebackup succeeds with WAL activity during slot creation') + or diag("pg_basebackup stderr:\n$stderr"); +unlike( + $stderr, + qr/has already been removed/, + 'no WAL needed by the backup was removed'); + +# pg_verifybackup also parses the WAL the backup needs, so this fails if +# any of it is missing. +$node->command_ok([ 'pg_verifybackup', $backupdir ], + 'backup with streamed WAL verifies'); + +# The slot is idle until the receiver starts after BASE_BACKUP, so the +# backup's checkpoint can invalidate it under idle_replication_slot_timeout. +# A temporary slot stays owned by its session, so the checkpoint terminates +# that session, and pg_basebackup must recreate the slot. With +# slot-timeout-inval attached, every checkpoint invalidates every idle slot, +# however briefly it has been idle. +$node->append_conf('postgresql.conf', 'idle_replication_slot_timeout = 1h'); +$node->reload; +$node->safe_psql('postgres', + "SELECT injection_points_attach('slot-timeout-inval', 'error')"); + +my $idle_offset = -s $node->logfile; +$node->command_checks_all( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => "$tempdir/backup_idle", + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + 0, + [], + [ + qr/replication slot "pg_basebackup_\d+" was invalidated during the base backup checkpoint, recreating it/ + ], + 'backup with a temporary slot invalidated by idle_replication_slot_timeout' +); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('slot-timeout-inval')"); + +# Make sure it was the idle timeout that invalidated the slot. +ok( $node->log_contains( + qr/terminating process \d+ to release replication slot "pg_basebackup_\d+"\n.*DETAIL: The slot's idle time/, + $idle_offset), + "backup's checkpoint invalidated the idle temporary slot"); + +$node->command_ok([ 'pg_verifybackup', "$tempdir/backup_idle" ], + 'backup with a slot recreated after the idle timeout verifies'); + +# The slot is created before the backup's checkpoint, so it starts at the +# previous one. If more WAL than max_slot_wal_keep_size was written since +# then, the backup's checkpoint invalidates the slot. pg_basebackup must then +# recreate it and still get all the WAL the backup needs. +my $knode = PostgreSQL::Test::Cluster->new('keep'); +$knode->init(allows_streaming => 1, extra => ['--wal-segsize=1']); +$knode->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +max_slot_wal_keep_size = 2MB +checkpoint_timeout = 1h +}); +$knode->start; + +foreach my $case ( + [ 'temporary slot', 'backup_keep_temp', [] ], + [ + '--create-slot', 'backup_keep_created', + [ '--create-slot', '--slot' => 'keep_slot' ] + ]) +{ + my ($desc, $dirname, $slot_opts) = @$case; + my $dir = "$tempdir/$dirname"; + + $knode->safe_psql('postgres', 'CHECKPOINT'); + $knode->advance_wal(5); + + my $log_offset = -s $knode->logfile; + $knode->command_checks_all( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $dir, + '--wal-method' => 'stream', + @$slot_opts, + '--dbname' => $knode->connstr('postgres'), + ], + 0, + [], + [qr/replication slot ".*" was invalidated during the base backup checkpoint, recreating it/], + "backup with a $desc invalidated by max_slot_wal_keep_size"); + + # Make sure the backup's checkpoint really invalidated the slot. A + # temporary slot is owned by its session, so that session is terminated. + ok( $knode->log_contains( + qr/(?:invalidating obsolete|terminating process \d+ to release) replication slot "(?:pg_basebackup_\d+|keep_slot)"/, + $log_offset), + "backup's checkpoint invalidated the $desc"); + + $knode->command_ok([ 'pg_verifybackup', $dir ], + "backup with a recreated $desc verifies"); +} + +is( $knode->safe_psql( + 'postgres', + q{SELECT restart_lsn IS NOT NULL AND invalidation_reason IS NULL + FROM pg_replication_slots WHERE slot_name = 'keep_slot'}), + 't', + 'slot recreated for --create-slot is valid after the backup'); +$knode->safe_psql('postgres', "SELECT pg_drop_replication_slot('keep_slot')"); + +done_testing(); -- 2.56.1
^ permalink raw reply [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
  2026-11-20 15:04                 ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-20 22:59                   ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-23 11:50                     ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
@ 2026-11-23 22:16                       ` Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-24 13:06                         ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  0 siblings, 1 reply; 18+ messages in thread

From: Zsolt Parragi @ 2026-11-23 22:16 UTC (permalink / raw)
  To: Jesper St John <jesper@stjohn.se>; +Cc: pgsql-bugs@lists.postgresql.org; Jesper St John <admin@zonezero.se>; Andrey Borodin <x4mmm@yandex-team.ru>; Magnus Hagander <magnus@hagander.net>; Kirill Reshke <reshkekirill@gmail.com>

On Mon, 23 Nov 2026, Jesper St John <jesper@stjohn.se> wrote:
> But it seems that in master today, a previously created slot which would
> be specified by --slot and not --create-slot could also be invalidated
> if a tiny idle_replication_slot_timeout is set.

It seems like a new behavior to me with --create-slot (I agree that it
is preexisting with --slot).

Also, --create-slot leaves the slot behind in this case, which is also
new. And this happens in two more cases: when the connection is lost
and when the process is killed with SIGINT/SIGTERM during the
checkpoint.

The new hint can also be misleading in some cases:

PGOPTIONS='-c idle_session_timeout=2s' pg_basebackup -X stream
--checkpoint=spread   (no slot invalidated in the server log)
pg_basebackup: warning: replication slot "pg_basebackup_664448" was
invalidated during the base backup checkpoint, recreating it
pg_basebackup: hint: You might need to increase "max_slot_wal_keep_size".

I am not sure if this is bad or not, because that is probably the more
common case of why is this happening, and the hint says 'might', so it
might be okay.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
  2026-11-20 15:04                 ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-20 22:59                   ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-23 11:50                     ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-23 22:16                       ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
@ 2026-11-24 13:06                         ` Jesper St John <jesper@stjohn.se>
  2026-11-24 22:11                           ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  0 siblings, 1 reply; 18+ messages in thread

From: Jesper St John @ 2026-11-24 13:06 UTC (permalink / raw)
  To: Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: pgsql-bugs@lists.postgresql.org; Jesper St John <admin@zonezero.se>; Andrey Borodin <x4mmm@yandex-team.ru>; Magnus Hagander <magnus@hagander.net>; Kirill Reshke <reshkekirill@gmail.com>

On 2026-11-23 23:16, Zsolt Parragi wrote:
> On Mon, 23 Nov 2026, Jesper St John <jesper@stjohn.se> wrote:
>> But it seems that in master today, a previously created slot which 
>> would
>> be specified by --slot and not --create-slot could also be invalidated
>> if a tiny idle_replication_slot_timeout is set.
> 
> It seems like a new behavior to me with --create-slot (I agree that it
> is preexisting with --slot).

Ok, fair. I think the idle_replication_slot_timeout needs some more 
documentation though.

> 
> Also, --create-slot leaves the slot behind in this case, which is also
> new. And this happens in two more cases: when the connection is lost
> and when the process is killed with SIGINT/SIGTERM during the
> checkpoint.
> 
Yes.

> The new hint can also be misleading in some cases:
> 
> PGOPTIONS='-c idle_session_timeout=2s' pg_basebackup -X stream
> --checkpoint=spread   (no slot invalidated in the server log)
> pg_basebackup: warning: replication slot "pg_basebackup_664448" was
> invalidated during the base backup checkpoint, recreating it
> pg_basebackup: hint: You might need to increase 
> "max_slot_wal_keep_size".
> 
> I am not sure if this is bad or not, because that is probably the more
> common case of why is this happening, and the hint says 'might', so it
> might be okay.
It can be rephrased.

What if we create a temp slot before running base backup and then drop 
it and create a new slot if needed as it is done today? That way it 
shouldn't be hit by idle_replication_slot_timeout and any sigint/sigterm 
would not leave the slot behind.

The patch might be smaller...

The idle_session_timeout of something smaller than the time it takes to 
do checkpoint=spread would probably recreate the timing issue with temp 
slot- so perhaps that could be overriden?

Any thoughts before I start exploring?

//Jesper






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
  2026-11-20 15:04                 ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-20 22:59                   ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-23 11:50                     ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-23 22:16                       ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-24 13:06                         ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
@ 2026-11-24 22:11                           ` Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-25 14:33                             ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  0 siblings, 1 reply; 18+ messages in thread

From: Zsolt Parragi @ 2026-11-24 22:11 UTC (permalink / raw)
  To: Jesper St John <jesper@stjohn.se>; +Cc: pgsql-bugs@lists.postgresql.org; Jesper St John <admin@zonezero.se>; Andrey Borodin <x4mmm@yandex-team.ru>; Magnus Hagander <magnus@hagander.net>; Kirill Reshke <reshkekirill@gmail.com>

On Tue, 24 Nov 2026, Jesper St John <jesper@stjohn.se> wrote:
> What if we create a temp slot before running base backup and then drop
> it and create a new slot if needed as it is done today? That way it
> shouldn't be hit by idle_replication_slot_timeout and any sigint/sigterm
> would not leave the slot behind.
>
> The patch might be smaller...

That could work. The main downside is that for a short time, this
needs 2 replication slots, so that would need some documentation. I
think that approach will also leave a few very unlikely races where we
could still hit the original issue, but unless I'm wrong, that should
be very unlikely to happen in practice.

>
> The idle_session_timeout of something smaller than the time it takes to
> do checkpoint=spread would probably recreate the timing issue with temp
> slot- so perhaps that could be overriden?
>

Yes, that could work too. But we can't override
idle_replication_slot_timeout, but that requires a somewhat
misconfigured server.






^ permalink  raw  reply  [nested|flat] 18+ messages in thread

* Re: Timing issue in pg_basebackup
  2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-20 21:11 ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-10-21 17:16   ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-10-22 05:44     ` Re: Timing issue in pg_basebackup Kirill Reshke <reshkekirill@gmail.com>
  2026-11-09 13:19       ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-12 21:19         ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-16 10:26           ` Re: Timing issue in pg_basebackup Magnus Hagander <magnus@hagander.net>
  2026-11-16 12:08             ` Re: Timing issue in pg_basebackup Andrey Borodin <x4mmm@yandex-team.ru>
  2026-11-17 11:35               ` Re: Timing issue in pg_basebackup Jesper St John <admin@zonezero.se>
  2026-11-20 15:04                 ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-20 22:59                   ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-23 11:50                     ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-23 22:16                       ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
  2026-11-24 13:06                         ` Re: Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
  2026-11-24 22:11                           ` Re: Timing issue in pg_basebackup Zsolt Parragi <zsolt.parragi@percona.com>
@ 2026-11-25 14:33                             ` Jesper St John <jesper@stjohn.se>
  0 siblings, 0 replies; 18+ messages in thread

From: Jesper St John @ 2026-11-25 14:33 UTC (permalink / raw)
  To: Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: pgsql-bugs@lists.postgresql.org; Jesper St John <admin@zonezero.se>; Andrey Borodin <x4mmm@yandex-team.ru>; Magnus Hagander <magnus@hagander.net>; Kirill Reshke <reshkekirill@gmail.com>

Here's a v6 patch.

On 2026-11-24 23:11, Zsolt Parragi wrote:
> On Tue, 24 Nov 2026, Jesper St John <jesper@stjohn.se> wrote:
>> What if we create a temp slot before running base backup and then drop
>> it and create a new slot if needed as it is done today? That way it
>> shouldn't be hit by idle_replication_slot_timeout and any 
>> sigint/sigterm
>> would not leave the slot behind.
>> 
>> The patch might be smaller...
> 
> That could work. The main downside is that for a short time, this
> needs 2 replication slots, so that would need some documentation. I
> think that approach will also leave a few very unlikely races where we
> could still hit the original issue, but unless I'm wrong, that should
> be very unlikely to happen in practice.

It didn't get smaller, but I think it's easier to read. There might be a 
race between dropping the temp slot and creating a regular slot but it 
should be minimal.

> 
>> 
>> The idle_session_timeout of something smaller than the time it takes 
>> to
>> do checkpoint=spread would probably recreate the timing issue with 
>> temp
>> slot- so perhaps that could be overriden?
>> 
> 
> Yes, that could work too. But we can't override
> idle_replication_slot_timeout, but that requires a somewhat
> misconfigured server.

idle_session_timeout is now set to 0 on the connection, so that the temp 
slot is kept.

//Jesper

Attachments:

v6-0001-pg_basebackup-Create-the-WAL-streaming-slot-befor.patch (text/x-diff, 25.5K) download From 58e261be66ef1698d24ba78535800b5922a666cf Mon Sep 17 00:00:00 2001 From: Jesper St John <jesper@stjohn.se> Date: Mon, 23 Nov 2026 09:51:09 +0100 Subject: [PATCH v6] pg_basebackup: Create the WAL streaming slot before starting the backup With --wal-method=stream, pg_basebackup created its replication slot only after BASE_BACKUP had returned the backup's start position. A checkpoint running in between could remove WAL from that position onwards, and the backup then failed with "requested WAL segment ... has already been removed". Open the WAL streaming connection and create a temporary slot on it before sending BASE_BACKUP. With --create-slot, the permanent slot is still created after BASE_BACKUP has returned, so the backup's checkpoint cannot invalidate it and a failed backup does not leave it behind; the temporary slot is then dropped. With --no-slot, or with --slot naming an existing slot, nothing changes. The connection is idle until the backup's checkpoint has completed, so disable idle_session_timeout on it. That checkpoint can still terminate the connection if the temporary slot is past max_slot_wal_keep_size or idle_replication_slot_timeout, which drops the slot. If the connection was lost, warn, reconnect and create the slot again before streaming starts; the checkpoint should keep the WAL from the start position. Document under --wal-method=stream. Add a TAP test, which needs injection points, checking that the backup succeeds when a checkpoint removes WAL while the slot is being created, and when its own checkpoint terminates the connection because of max_slot_wal_keep_size. It also checks that the connection is opened early only when a slot is created, and that it survives idle_session_timeout and keeps its options. In 010_pg_basebackup.pl, check that a failed backup does not leave a --create-slot slot behind. --- doc/src/sgml/ref/pg_basebackup.sgml | 20 ++ src/bin/pg_basebackup/Makefile | 4 +- src/bin/pg_basebackup/meson.build | 2 + src/bin/pg_basebackup/pg_basebackup.c | 220 ++++++++++++--- src/bin/pg_basebackup/streamutil.c | 25 +- src/bin/pg_basebackup/streamutil.h | 1 + src/bin/pg_basebackup/t/010_pg_basebackup.pl | 19 ++ .../pg_basebackup/t/013_wal_slot_timing.pl | 263 ++++++++++++++++++ 8 files changed, 517 insertions(+), 37 deletions(-) create mode 100644 src/bin/pg_basebackup/t/013_wal_slot_timing.pl diff --git a/doc/src/sgml/ref/pg_basebackup.sgml b/doc/src/sgml/ref/pg_basebackup.sgml index b4a0faccf41..e9b38db0c2f 100644 --- a/doc/src/sgml/ref/pg_basebackup.sgml +++ b/doc/src/sgml/ref/pg_basebackup.sgml @@ -398,6 +398,26 @@ PostgreSQL documentation with the write-ahead log data, using this method requires no extra write-ahead logs to be saved on the source server. </para> + <para> + Unless <option>--no-slot</option> is given, or + <option>--slot</option> names an existing slot, + <application>pg_basebackup</application> creates a temporary + replication slot before the backup starts, to keep the + write-ahead log the backup needs. A slot requested with + <option>--create-slot</option> is created later, when streaming + starts, and replaces the temporary slot. + </para> + <para> + The connection holding the temporary slot is idle until the + checkpoint at the start of the backup has completed, so + <xref linkend="guc-idle-session-timeout"/> is disabled on it. + If that checkpoint finds the slot past + <xref linkend="guc-max-slot-wal-keep-size"/> or + <xref linkend="guc-idle-replication-slot-timeout"/>, the server + closes the connection, which drops the slot. If the connection + is closed, <application>pg_basebackup</application> prints a + warning and reconnects. + </para> <para> When tar format is used, the write-ahead log files will be written to a separate file named <filename>pg_wal.tar</filename> diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile index df94fc27d02..12ff32e329b 100644 --- a/src/bin/pg_basebackup/Makefile +++ b/src/bin/pg_basebackup/Makefile @@ -12,13 +12,15 @@ PGFILEDESC = "pg_basebackup/pg_receivewal/pg_recvlogical - streaming WAL and backup receivers" PGAPPICON=win32 -EXTRA_INSTALL=contrib/test_decoding +EXTRA_INSTALL=contrib/test_decoding \ + src/test/modules/injection_points subdir = src/bin/pg_basebackup top_builddir = ../../.. include $(top_builddir)/src/Makefile.global # make these available to TAP test scripts +export enable_injection_points export LZ4 export TAR # Note that GZIP cannot be used directly as this environment variable is diff --git a/src/bin/pg_basebackup/meson.build b/src/bin/pg_basebackup/meson.build index 2d3b22ff06e..43905ed9fc3 100644 --- a/src/bin/pg_basebackup/meson.build +++ b/src/bin/pg_basebackup/meson.build @@ -96,11 +96,13 @@ tests += { 'env': {'GZIP_PROGRAM': gzip.found() ? gzip.full_path() : '', 'TAR': tar.found() ? tar.full_path() : '', 'LZ4': program_lz4.found() ? program_lz4.full_path() : '', + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', }, 'tests': [ 't/010_pg_basebackup.pl', 't/011_in_place_tablespace.pl', 't/012_incremental_progress.pl', + 't/013_wal_slot_timing.pl', 't/020_pg_receivewal.pl', 't/030_pg_recvlogical.pl', 't/040_pg_createsubscriber.pl', diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 4daf985ab83..658838d4799 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -108,6 +108,11 @@ typedef void (*WriteDataCallback) (size_t nbytes, char *buf, */ #define MINIMUM_VERSION_FOR_WAL_SUMMARIES 170000 +/* + * idle_session_timeout was added in version 14. + */ +#define MINIMUM_VERSION_FOR_IDLE_SESSION_TIMEOUT 140000 + /* * Different ways to include WAL */ @@ -165,6 +170,15 @@ static bool found_existing_xlogdir = false; static bool made_tablespace_dirs = false; static bool found_tablespace_dirs = false; +/* Connection for the background WAL receiver, see PrepareLogStreamer() */ +static PGconn *bgconn = NULL; + +/* + * Temporary slot reserving WAL until streaming starts, or NULL. Without + * --slot it is also the slot we stream with. + */ +static char *temp_slot_name = NULL; + /* Progress indicators */ static uint64 totalsize_kb; static uint64 totaldone; @@ -607,6 +621,169 @@ LogStreamerMain(logstreamer_param *param) return 0; } +/* + * Open a connection for the background WAL receiver. + * + * With a temporary slot, the connection is opened before BASE_BACKUP and + * stays idle until the backup's checkpoint has completed, which with a spread + * checkpoint can take minutes. Disable idle_session_timeout on it, so that + * it is not closed while it waits, dropping its temporary slot. Any options + * the main connection was opened with are kept, with ours added last so that + * they take precedence. + */ +static PGconn * +GetStreamingConnection(void) +{ + char *options_val = NULL; + PGconn *tmpconn; + + if (PQserverVersion(conn) >= MINIMUM_VERSION_FOR_IDLE_SESSION_TIMEOUT) + { + PQconninfoOption *opts; + char *option = NULL; + + /* + * Take the options from the main connection rather than from the + * connection string, so that those given in PGOPTIONS are kept too. + */ + opts = PQconninfo(conn); + if (opts == NULL) + pg_fatal("out of memory"); + + for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt) + { + if (strcmp(opt->keyword, "options") == 0 && opt->val && + *opt->val) + option = opt->val; + } + + options_val = psprintf("%s -c idle_session_timeout=0", + (option == NULL) ? "" : option); + + PQconninfoFree(opts); + } + + tmpconn = GetConnectionWithOptions(options_val); + pg_free(options_val); + + return tmpconn; +} + +/* + * Open the connection for the background WAL receiver, and create a + * temporary slot on it that reserves WAL until streaming starts, also with + * --create-slot. This must be done before BASE_BACKUP, as a checkpoint could + * otherwise remove WAL from the backup's start position before the receiver + * gets to it. + * + * Without a temporary slot, that is with --no-slot or with --slot naming an + * existing slot, there is nothing to reserve, so the connection is opened + * only after BASE_BACKUP has returned, in SetupStreamingSlot(), and is not + * left idle during the backup's checkpoint. + */ +static void +PrepareLogStreamer(void) +{ + /* Temporary replication slots are only supported in 10 and newer */ + if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) + { + temp_replication_slot = false; + return; + } + + if (!temp_replication_slot && !create_slot) + return; + + /* Get a WAL serving connection */ + bgconn = GetStreamingConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + + temp_slot_name = psprintf("pg_basebackup_%u", + (unsigned int) PQbackendPID(bgconn)); + if (!CreateReplicationSlot(bgconn, temp_slot_name, NULL, true, true, true, + false, false, false)) + exit(1); + if (verbose) + pg_log_info("created temporary replication slot \"%s\"", + temp_slot_name); + + /* Without --slot, stream with the temporary slot */ + if (temp_replication_slot) + replication_slot = temp_slot_name; +} + +/* + * Set up the slot to stream with, after BASE_BACKUP has returned. + * + * If the temporary slot's connection was lost during the backup's checkpoint, + * the slot is gone too, so reconnect and create it again. The checkpoint + * should keep the WAL from the backup's start position, so the new slot + * covers it. + * + * The permanent slot for --create-slot is created here, after the checkpoint, + * and only then is the temporary slot dropped. + */ +static void +SetupStreamingSlot(void) +{ + if (bgconn == NULL) + { + Assert(temp_slot_name == NULL); + + bgconn = GetStreamingConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + } + else if (temp_slot_name) + { + PGresult *res = PQexec(bgconn, "IDENTIFY_SYSTEM"); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + { + pg_log_warning("connection for streaming write-ahead log was lost during the base backup checkpoint, reconnecting"); + pg_log_warning_detail("%s", PQerrorMessage(bgconn)); + + PQfinish(bgconn); + bgconn = GetStreamingConnection(); + if (!bgconn) + /* Error message already written in GetConnection() */ + exit(1); + + /* Recreate the temporary slot only if we stream with it */ + if (temp_replication_slot) + { + if (!CreateReplicationSlot(bgconn, temp_slot_name, NULL, true, + true, true, false, false, false)) + exit(1); + if (verbose) + pg_log_info("created temporary replication slot \"%s\"", + temp_slot_name); + } + else + temp_slot_name = NULL; + } + PQclear(res); + } + + if (create_slot) + { + if (!CreateReplicationSlot(bgconn, replication_slot, NULL, false, true, + true, false, false, false)) + exit(1); + if (verbose) + pg_log_info("created replication slot \"%s\"", replication_slot); + + /* + * The regular slot takes over. + */ + if (temp_slot_name && !DropReplicationSlot(bgconn, temp_slot_name)) + exit(1); + } +} + /* * Initiate background process for receiving xlog during the backup. * The background stream will use its own database connection so we can @@ -620,7 +797,10 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, logstreamer_param *param; char statusdir[MAXPGPATH]; + Assert(bgconn != NULL); + param = pg_malloc0_object(logstreamer_param); + param->bgconn = bgconn; param->timeline = timeline; param->sysidentifier = sysidentifier; param->wal_compress_algorithm = wal_compress_algorithm; @@ -639,46 +819,12 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier, pg_fatal("could not create pipe for background process: %m"); #endif - /* Get a second connection */ - param->bgconn = GetConnection(); - if (!param->bgconn) - /* Error message already written in GetConnection() */ - exit(1); - /* In post-10 cluster, pg_xlog has been renamed to pg_wal */ snprintf(param->xlog, sizeof(param->xlog), "%s/%s", basedir, PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ? "pg_xlog" : "pg_wal"); - /* Temporary replication slots are only supported in 10 and newer */ - if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS) - temp_replication_slot = false; - - /* - * Create replication slot if requested - */ - if (temp_replication_slot && !replication_slot) - replication_slot = psprintf("pg_basebackup_%u", - (unsigned int) PQbackendPID(param->bgconn)); - if (temp_replication_slot || create_slot) - { - if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL, - temp_replication_slot, true, true, false, - false, false)) - exit(1); - - if (verbose) - { - if (temp_replication_slot) - pg_log_info("created temporary replication slot \"%s\"", - replication_slot); - else - pg_log_info("created replication slot \"%s\"", - replication_slot); - } - } - if (format == 'p') { /* @@ -1970,6 +2116,10 @@ BaseBackup(char *compression_algorithm, char *compression_detail, compression_detail); } + /* If streaming WAL, reserve it before starting the backup */ + if (includewal == STREAM_WAL) + PrepareLogStreamer(); + if (verbose) pg_log_info("initiating base backup, waiting for checkpoint to complete"); @@ -2087,6 +2237,8 @@ BaseBackup(char *compression_algorithm, char *compression_detail, pg_compress_algorithm wal_compress_algorithm; int wal_compress_level; + SetupStreamingSlot(); + if (verbose) pg_log_info("starting background WAL receiver"); diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 8086fde84db..cdf3febafb6 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -59,10 +59,21 @@ PGconn *conn = NULL; */ PGconn * GetConnection(void) +{ + return GetConnectionWithOptions(NULL); +} + +/* + * Like GetConnection(), but if options is not NULL, use it as the "options" + * connection parameter, replacing any given in the connection string. The + * caller must include any options that should be kept. + */ +PGconn * +GetConnectionWithOptions(const char *options) { PGconn *tmpconn; - int argcount = 7; /* dbname, replication, fallback_app_name, - * host, user, port, password */ + int argcount = 8; /* dbname, replication, fallback_app_name, + * host, user, port, options, password */ int i; const char **keywords; const char **values; @@ -108,6 +119,10 @@ GetConnection(void) for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) { + /* Our own options are added below */ + if (options && strcmp(conn_opt->keyword, "options") == 0) + continue; + if (conn_opt->val != NULL && conn_opt->val[0] != '\0') { keywords[i] = conn_opt->keyword; @@ -150,6 +165,12 @@ GetConnection(void) values[i] = dbport; i++; } + if (options) + { + keywords[i] = "options"; + values[i] = options; + i++; + } /* If -W was given, force prompt for password, but only the first time */ need_password = (dbgetpassword == 1 && !password); diff --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h index 115703e9c6c..8827b3eb328 100644 --- a/src/bin/pg_basebackup/streamutil.h +++ b/src/bin/pg_basebackup/streamutil.h @@ -30,6 +30,7 @@ extern int WalSegSz; extern PGconn *conn; extern PGconn *GetConnection(void); +extern PGconn *GetConnectionWithOptions(const char *options); /* Replication commands */ extern bool CreateReplicationSlot(PGconn *conn, const char *slot_name, diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl index 23a757b1c86..3840cd5296a 100644 --- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl +++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl @@ -830,6 +830,25 @@ $node->command_fails_like( 'pg_basebackup fails with --create-slot --slot and a previously existing slot' ); +# A failure of BASE_BACKUP must not leave a slot created by --create-slot +# behind. +$node->command_fails_like( + [ + @pg_basebackup_defs, + '--pgdata' => "$tempdir/backupxs_slot2", + '--create-slot', + '--slot' => 'slot2', + '--manifest-checksums' => 'bogus' + ], + qr/unrecognized checksum algorithm/, + 'pg_basebackup --create-slot fails when BASE_BACKUP fails'); +is( $node->safe_psql( + 'postgres', + q{SELECT count(*) FROM pg_replication_slots WHERE slot_name = 'slot2'} + ), + '0', + 'no slot is left behind when pg_basebackup --create-slot fails'); + $node->safe_psql('postgres', q{SELECT * FROM pg_create_physical_replication_slot('slot1')}); my $lsn = $node->safe_psql('postgres', diff --git a/src/bin/pg_basebackup/t/013_wal_slot_timing.pl b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl new file mode 100644 index 00000000000..eb4c90f3ad7 --- /dev/null +++ b/src/bin/pg_basebackup/t/013_wal_slot_timing.pl @@ -0,0 +1,263 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Check that pg_basebackup's replication slot reserves WAL from before the +# backup's start position. If the slot were created only after BASE_BACKUP +# had returned the start position, a checkpoint running in between could +# remove the WAL the backup needs, and streaming it would fail. +# +# We hold the slot creation with an injection point, force WAL past several +# segments and checkpoint, then let the slot creation continue. +# +# Also check when the WAL streaming connection is opened, that it is kept +# open while it waits for the backup's checkpoint, and that pg_basebackup +# reconnects if that checkpoint terminates the connection holding the +# temporary slot. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $tempdir = PostgreSQL::Test::Utils::tempdir; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init(allows_streaming => 1); +$node->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +checkpoint_timeout = 1h +}); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +$node->safe_psql('postgres', + "SELECT injection_points_attach('replication-slot-create-begin', 'wait')" +); + +my $backupdir = "$tempdir/backup"; +my ($stdout, $stderr); +my $h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $backupdir, + '--wal-method' => 'stream', + '--dbname' => $node->connstr('postgres'), + ], + '>' => \$stdout, + '2>' => \$stderr); + +# Wait until the walsender is about to create pg_basebackup's slot. +$node->wait_for_event('walsender', 'replication-slot-create-begin'); + +# Move WAL forward by several segments and checkpoint, so that every segment +# not protected by a slot or wal_keep_size is removed or recycled. +$node->advance_wal(3); +$node->safe_psql('postgres', 'CHECKPOINT'); + +$node->safe_psql('postgres', + "SELECT injection_points_detach('replication-slot-create-begin')"); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('replication-slot-create-begin')"); + +my $ok = $h->finish; +ok($ok, 'pg_basebackup succeeds with WAL activity during slot creation') + or diag("pg_basebackup stderr:\n$stderr"); +unlike( + $stderr, + qr/has already been removed/, + 'no WAL needed by the backup was removed'); + +# pg_verifybackup also parses the WAL the backup needs, so this fails if +# any of it is missing. +$node->command_ok([ 'pg_verifybackup', $backupdir ], + 'backup with streamed WAL verifies'); + +# With a temporary slot, the WAL streaming connection is opened before +# BASE_BACKUP and is idle until the backup's checkpoint has completed. +# pg_basebackup disables idle_session_timeout on it, so it must not be closed +# while it waits, even if the checkpoint takes longer than the timeout. Hold +# the checkpoint until the connection has been idle for several times the +# timeout. If it were closed, this would wait until the test times out. +# +# The options given in the connection string must still be used on that +# connection. log_replication_commands is enabled only through them, so the +# temporary slot's creation is logged only if they were kept. +# +# Without a temporary slot, with --no-slot or with --slot naming an existing +# slot, the connection must be opened only after BASE_BACKUP has returned, so +# while the checkpoint is held, the only walsender is the one running +# BASE_BACKUP. +$node->append_conf( + 'postgresql.conf', q{ +idle_session_timeout = '1s' +log_replication_commands = off +}); +$node->reload; +$node->safe_psql('postgres', + "SELECT pg_create_physical_replication_slot('existing_slot', true)"); + +my $connstr_with_options = + $node->connstr('postgres') . " options='-c log_replication_commands=on'"; + +foreach my $case ( + [ 'temporary slot', 'backup_session_temp', 1, [] ], + [ '--no-slot', 'backup_session_noslot', 0, ['--no-slot'] ], + [ + '--slot', 'backup_session_existing', + 0, [ '--slot' => 'existing_slot' ] + ]) +{ + my ($desc, $dirname, $early, $slot_opts) = @$case; + my $dir = "$tempdir/$dirname"; + + $node->safe_psql('postgres', + "SELECT injection_points_attach('checkpoint-before-old-wal-removal', 'wait')" + ); + + my $log_offset = -s $node->logfile; + my ($s_stdout, $s_stderr); + my $s_h = IPC::Run::start( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $dir, + '--wal-method' => 'stream', + @$slot_opts, + '--dbname' => $connstr_with_options, + ], + '>' => \$s_stdout, + '2>' => \$s_stderr); + + $node->wait_for_event('checkpointer', + 'checkpoint-before-old-wal-removal'); + if ($early) + { + $node->poll_query_until( + 'postgres', q{ + SELECT count(*) > 0 FROM pg_stat_activity + WHERE backend_type = 'walsender' AND state = 'idle' + AND now() - state_change > interval '3s'}) + or die + "timed out waiting for the WAL streaming connection to stay idle"; + } + else + { + is( $node->safe_psql( + 'postgres', q{ + SELECT count(*) FROM pg_stat_activity + WHERE backend_type = 'walsender'}), + '1', + "no WAL streaming connection during the checkpoint with $desc"); + } + + $node->safe_psql('postgres', + "SELECT injection_points_detach('checkpoint-before-old-wal-removal')" + ); + $node->safe_psql('postgres', + "SELECT injection_points_wakeup('checkpoint-before-old-wal-removal')" + ); + + ok($s_h->finish, "backup with $desc and idle_session_timeout") + or diag("pg_basebackup stderr:\n$s_stderr"); + unlike( + $s_stderr, + qr/connection for streaming write-ahead log was lost/, + "WAL streaming connection with $desc was not lost"); + + if ($early) + { + ok( $node->log_contains( + qr/received replication command: CREATE_REPLICATION_SLOT "pg_basebackup_\d+" TEMPORARY/, + $log_offset), + "connection options are used for the WAL streaming connection with $desc" + ); + } + + $node->command_ok([ 'pg_verifybackup', $dir ], + "backup with $desc and idle_session_timeout verifies"); +} + +$node->safe_psql('postgres', + "SELECT pg_drop_replication_slot('existing_slot')"); +$node->append_conf( + 'postgresql.conf', q{ +idle_session_timeout = 0 +log_replication_commands = on +}); +$node->reload; + +# The temporary slot is created before the backup's checkpoint, so it starts +# at the previous one. If more WAL than max_slot_wal_keep_size was written +# since then, the backup's checkpoint terminates the connection holding the +# slot, which drops it. pg_basebackup must then reconnect and still get all +# the WAL the backup needs. +my $knode = PostgreSQL::Test::Cluster->new('keep'); +$knode->init(allows_streaming => 1, extra => ['--wal-segsize=1']); +$knode->append_conf( + 'postgresql.conf', q{ +wal_keep_size = 0 +max_slot_wal_keep_size = 2MB +checkpoint_timeout = 1h +}); +$knode->start; + +foreach my $case ( + [ 'temporary slot', 'backup_keep_temp', [] ], + [ + '--create-slot', 'backup_keep_created', + [ '--create-slot', '--slot' => 'keep_slot' ] + ]) +{ + my ($desc, $dirname, $slot_opts) = @$case; + my $dir = "$tempdir/$dirname"; + + $knode->safe_psql('postgres', 'CHECKPOINT'); + $knode->advance_wal(5); + + my $log_offset = -s $knode->logfile; + $knode->command_checks_all( + [ + 'pg_basebackup', '--no-sync', + '--checkpoint' => 'fast', + '--pgdata' => $dir, + '--wal-method' => 'stream', + @$slot_opts, + '--dbname' => $knode->connstr('postgres'), + ], + 0, + [], + [ + qr/connection for streaming write-ahead log was lost during the base backup checkpoint, reconnecting/ + ], + "backup with $desc and max_slot_wal_keep_size"); + + ok( $knode->log_contains( + qr/terminating process \d+ to release replication slot "pg_basebackup_\d+"/, + $log_offset), + "temporary slot's connection was terminated in backup with $desc"); + + $knode->command_ok([ 'pg_verifybackup', $dir ], + "backup with $desc and max_slot_wal_keep_size verifies"); +} + +is( $knode->safe_psql( + 'postgres', + q{SELECT restart_lsn IS NOT NULL AND invalidation_reason IS NULL + FROM pg_replication_slots WHERE slot_name = 'keep_slot'}), + 't', + 'slot created by --create-slot is valid after the backup'); +$knode->safe_psql('postgres', "SELECT pg_drop_replication_slot('keep_slot')"); + +done_testing(); -- 2.56.1
^ permalink raw reply [nested|flat] 18+ messages in thread


end of thread, other threads:[~2026-11-25 14:33 UTC | newest]

Thread overview: 18+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-19 14:33 Timing issue in pg_basebackup Jesper St John <jesper@stjohn.se>
2026-10-20 21:11 ` Kirill Reshke <reshkekirill@gmail.com>
2026-10-21 17:16   ` Jesper St John <jesper@stjohn.se>
2026-10-22 05:44     ` Kirill Reshke <reshkekirill@gmail.com>
2026-10-29 19:26       ` Jesper St John <jesper@stjohn.se>
2026-11-09 13:19       ` Jesper St John <jesper@stjohn.se>
2026-11-12 16:12         ` Joao Rito <joaovictor.rito@gmail.com>
2026-11-12 21:19         ` Zsolt Parragi <zsolt.parragi@percona.com>
2026-11-16 10:26           ` Magnus Hagander <magnus@hagander.net>
2026-11-16 12:08             ` Andrey Borodin <x4mmm@yandex-team.ru>
2026-11-17 11:35               ` Jesper St John <admin@zonezero.se>
2026-11-20 15:04                 ` Jesper St John <jesper@stjohn.se>
2026-11-20 22:59                   ` Zsolt Parragi <zsolt.parragi@percona.com>
2026-11-23 11:50                     ` Jesper St John <jesper@stjohn.se>
2026-11-23 22:16                       ` Zsolt Parragi <zsolt.parragi@percona.com>
2026-11-24 13:06                         ` Jesper St John <jesper@stjohn.se>
2026-11-24 22:11                           ` Zsolt Parragi <zsolt.parragi@percona.com>
2026-11-25 14:33                             ` Jesper St John <jesper@stjohn.se>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox