pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jesper St John <jesper@stjohn.se>
To: Zsolt Parragi <zsolt.parragi@percona.com>
Cc: pgsql-bugs@lists.postgresql.org
Cc: Jesper St John <admin@zonezero.se>
Cc: Andrey Borodin <x4mmm@yandex-team.ru>
Cc: Magnus Hagander <magnus@hagander.net>
Cc: Kirill Reshke <reshkekirill@gmail.com>
Subject: Re: Timing issue in pg_basebackup
Date: Mon, 23 Nov 2026 12:50:19 +0100
Message-ID: <bb286c1c536866fc3b18f632dc90ee10@stjohn.se> (raw)
In-Reply-To: <CAN4CZFMcG9v5os=m+NWhgorDt-cCiBBWFwq3atEGoUH24d-0PQ@mail.gmail.com>
References: <ddfa03dcf26f366ae493e8cc41bd479a@stjohn.se>
	<CALdSSPj_M=6RLR0XpXKVr_NWiaVAhT7g7wRaMx_--3kKZpT0SA@mail.gmail.com>
	<4417791388e8f1df20f64013e944be8e@stjohn.se>
	<CALdSSPg2hY=8SLZRk-o22oUAd=af7oNEXRJf8coTf-hjOtRg-A@mail.gmail.com>
	<80143a07e6732576c9f815a3058fa31d@stjohn.se>
	<CAN4CZFMbAcw4DZ-07PC9vt0tKVo0iaRvjswrs-bX19AfyY4vBg@mail.gmail.com>
	<CABUevEyyZqej1VS=JNhCgXJQP+2-VHYQccnXz079k=7c_7Wqsw@mail.gmail.com>
	<4E8EC951-B75E-4CB8-97B6-D5C21F88A991@yandex-team.ru>
	<4F71E8F4-32D9-4B37-9DA9-203564F00692@zonezero.se>
	<4fdcbef083932d15d6d03c4fd9076e42@stjohn.se>
	<CAN4CZFMcG9v5os=m+NWhgorDt-cCiBBWFwq3atEGoUH24d-0PQ@mail.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

view thread (18+ messages)  latest in thread

Message-ID: <bb286c1c536866fc3b18f632dc90ee10@stjohn.se>
Permalink:  ../bb286c1c536866fc3b18f632dc90ee10@stjohn.se/
Also on:    postgresql.org/message-id/bb286c1c536866fc3b18f632dc90ee10@stjohn.se

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: jesper@stjohn.se, zsolt.parragi@percona.com, admin@zonezero.se, x4mmm@yandex-team.ru, magnus@hagander.net, reshkekirill@gmail.com
  Subject: Re: Timing issue in pg_basebackup
  In-Reply-To: <bb286c1c536866fc3b18f632dc90ee10@stjohn.se>
  References: <ddfa03dcf26f366ae493e8cc41bd479a@stjohn.se> <CALdSSPj_M=6RLR0XpXKVr_NWiaVAhT7g7wRaMx_--3kKZpT0SA@mail.gmail.com> <4417791388e8f1df20f64013e944be8e@stjohn.se> <CALdSSPg2hY=8SLZRk-o22oUAd=af7oNEXRJf8coTf-hjOtRg-A@mail.gmail.com> <80143a07e6732576c9f815a3058fa31d@stjohn.se> <CAN4CZFMbAcw4DZ-07PC9vt0tKVo0iaRvjswrs-bX19AfyY4vBg@mail.gmail.com> <CABUevEyyZqej1VS=JNhCgXJQP+2-VHYQccnXz079k=7c_7Wqsw@mail.gmail.com> <4E8EC951-B75E-4CB8-97B6-D5C21F88A991@yandex-team.ru> <4F71E8F4-32D9-4B37-9DA9-203564F00692@zonezero.se> <4fdcbef083932d15d6d03c4fd9076e42@stjohn.se> <CAN4CZFMcG9v5os=m+NWhgorDt-cCiBBWFwq3atEGoUH24d-0PQ@mail.gmail.com> <bb286c1c536866fc3b18f632dc90ee10@stjohn.se>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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