pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: cca5507 <cca5507@qq.com>
To: Fujii Masao <masao.fujii@gmail.com>
To: Quan Zongliang <quanzongliang@yeah.net>
Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Walreceiver create temp slot more than once when timeline switch
Date: Tue, 25 Aug 2026 21:43:35 +0800
Message-ID: <tencent_EC70BBBC5097C2E55AC0140C19063CA10307@qq.com> (raw)
In-Reply-To: <CAHGQGwGvMKj_6iLVWUVsFPJrtYOhkKxR7fwrYLrtPjLkhKh4uA@mail.gmail.com>
References: <tencent_628FDAF814231923BC8E8357BBBC50F94207@qq.com>
	<fd040b35-7fa4-4d9a-9210-d79ccd75d2e8@yeah.net>
	<CAHGQGwGvMKj_6iLVWUVsFPJrtYOhkKxR7fwrYLrtPjLkhKh4uA@mail.gmail.com>

> I also confirmed that the patch fixes the reported issue. But,
> during that test, I found that pg_stat_wal_receiver.slot_name became
> NULL after a timeline switch.
> 
> I think this can happen because RequestXLogStreaming() clears
> walrcv->slotname when primary_slot_name is not configured.
> The walreceiver then does not write the existing temporary slot name
> back to shared memory because create_temp_slot is already false.
> Then, streaming continues to use the temporary slot, but
> pg_stat_wal_receiver.slot_name can show NULL.

Good catch, fixed.

> The reported case could be covered easily by 004_timeline_switch.pl.
> For example, we could enable wal_receiver_create_temp_slot without
> setting primary_slot_name, switch the upstream to a new timeline,
> check that the standby log does not contain "could not create replication
> slot ... already exists", and verify that pg_stat_wal_receiver.slot_name
> still reports a pg_walreceiver_% slot after the timeline switch.
> Thought?

Good idea.

> > Furthermore, if a log output is added before walrcv_create_slot(). This
> > might make the log more clear.
> >
> > ereport(LOG, errmsg("creating replication slot \"%s\"", slotname));
> 
> I think this would be better discussed separately from the bug fix
> as an improvement, since it isn't necessary for fixing the reported
> issue.

+1

Please see the v2 patch.

--
Regards,
ChangAo Chen


Attachments:

v2-0001-Fix-temporary-WAL-receiver-slot-handling-on-timel.patch (application/octet-stream, 4.9K) download From fd744df4a49dd5b1b7f14113458a546f562ce486 Mon Sep 17 00:00:00 2001 From: ChangAo Chen <cca5507@qq.com> Date: Tue, 25 Aug 2026 21:31:23 +0800 Subject: [PATCH v2] Fix temporary WAL receiver slot handling on timeline switches. When wal_receiver_create_temp_slot is enabled, a WAL receiver creates a temporary replication slot before starting streaming. If it reaches the end of an old timeline and is instructed to continue on a newer one, it reuses the same connection but enters the streaming setup again. Since is_temp_slot remains true, it attempts to create the same temporary slot again and fails because the slot already exists. Track whether the temporary slot has already been created for the lifetime of the WAL receiver and skip subsequent creation attempts. Continue to copy the locally retained slot name to shared memory on each streaming attempt, since RequestXLogStreaming() clears it when primary_slot_name is not configured. This also keeps pg_stat_wal_receiver.slot_name populated after a timeline switch. Add coverage for both behaviors to the timeline-switch recovery test. --- src/backend/replication/walreceiver.c | 22 +++++++---- src/test/recovery/t/004_timeline_switch.pl | 43 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 61dc6a5588b..26271bacd9d 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -158,6 +158,7 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) char *tmp_conninfo; char slotname[NAMEDATALEN]; bool is_temp_slot; + bool temp_slot_created = false; XLogRecPtr startpoint; TimeLineID startpointTLI; TimeLineID primaryTLI; @@ -431,17 +432,24 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) WalRcvFetchTimeLineHistoryFiles(startpointTLI, primaryTLI); /* - * Create temporary replication slot if requested, and update slot - * name in shared memory. (Note the slot name cannot already be set - * in this case.) + * Create a temporary replication slot if requested. This only needs + * to be done for the first stream because the slot remains available + * while this connection is reused for subsequent timelines. Update + * the slot name in shared memory each time because + * RequestXLogStreaming() clears it when restarting on a new timeline. */ if (is_temp_slot) { - snprintf(slotname, sizeof(slotname), - "pg_walreceiver_%lld", - (long long int) walrcv_get_backend_pid(wrconn)); + if (!temp_slot_created) + { + snprintf(slotname, sizeof(slotname), + "pg_walreceiver_%lld", + (long long int) walrcv_get_backend_pid(wrconn)); + + walrcv_create_slot(wrconn, slotname, true, false, false, 0, NULL); - walrcv_create_slot(wrconn, slotname, true, false, false, 0, NULL); + temp_slot_created = true; + } SpinLockAcquire(&walrcv->mutex); strlcpy(walrcv->slotname, slotname, NAMEDATALEN); diff --git a/src/test/recovery/t/004_timeline_switch.pl b/src/test/recovery/t/004_timeline_switch.pl index f9955b2b4eb..2301aa9e5d0 100644 --- a/src/test/recovery/t/004_timeline_switch.pl +++ b/src/test/recovery/t/004_timeline_switch.pl @@ -145,4 +145,47 @@ my $result_2 = $node_standby_3->safe_psql('postgres', "SELECT count(*) FROM tab_int"); is($result_2, qq(1), 'check content of standby 3'); +# Ensure that a WAL receiver creates a temporary replication slot only once +# when following an upstream across a timeline switch. + +# Initialize primary node +my $node_primary_3 = PostgreSQL::Test::Cluster->new('primary_3'); +$node_primary_3->init(allows_streaming => 1); +$node_primary_3->start; + +# Take backup +$node_primary_3->backup($backup_name); + +# Create standby node +my $node_standby_4 = PostgreSQL::Test::Cluster->new('standby_4'); +$node_standby_4->init_from_backup($node_primary_3, $backup_name, + has_streaming => 1); +$node_standby_4->append_conf( + 'postgresql.conf', "wal_receiver_create_temp_slot = on"); + +# Restart primary node in standby mode and promote it, switching it +# to a new timeline. +$node_primary_3->set_standby_mode; +$node_primary_3->restart; +$node_primary_3->promote; + +# Start standby node, create some content on primary and check its presence +# in standby, to ensure that the timeline switch has been done. +$node_standby_4->start; +$node_primary_3->safe_psql('postgres', + "CREATE TABLE tab_int AS SELECT 1 AS a"); +$node_primary_3->wait_for_catchup($node_standby_4); + +ok( !$node_standby_4->log_contains( + 'could not create replication slot "pg_walreceiver_[0-9]+".*already exists' + ), + 'temporary replication slot is not recreated across timeline jumps'); + +my $temp_slot_name = $node_standby_4->safe_psql('postgres', + "SELECT slot_name FROM pg_stat_wal_receiver"); +like( + $temp_slot_name, + qr/^pg_walreceiver_[0-9]+$/, + 'pg_stat_wal_receiver.slot_name remains set across timeline jumps'); + done_testing(); -- 2.53.0

view thread (9+ messages)  latest in thread

Message-ID: <tencent_EC70BBBC5097C2E55AC0140C19063CA10307@qq.com>
Permalink:  ../tencent_EC70BBBC5097C2E55AC0140C19063CA10307@qq.com/
Also on:    postgresql.org/message-id/tencent_EC70BBBC5097C2E55AC0140C19063CA10307@qq.com

 ·  · 

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-hackers@postgresql.org
  Cc: cca5507@qq.com, masao.fujii@gmail.com, quanzongliang@yeah.net
  Subject: Re: Walreceiver create temp slot more than once when timeline switch
  In-Reply-To: <tencent_EC70BBBC5097C2E55AC0140C19063CA10307@qq.com>
  References: <tencent_628FDAF814231923BC8E8357BBBC50F94207@qq.com> <fd040b35-7fa4-4d9a-9210-d79ccd75d2e8@yeah.net> <CAHGQGwGvMKj_6iLVWUVsFPJrtYOhkKxR7fwrYLrtPjLkhKh4uA@mail.gmail.com> <tencent_EC70BBBC5097C2E55AC0140C19063CA10307@qq.com>

* 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