pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Walreceiver create temp slot more than once when timeline switch
9+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-08-27 58ff54aee8 764d31b8ae 2bf6144b4e 4c8fa02c5f 58fb12f4aa 874c22c94f aec25d6107  Fix temporary WAL receiver slot handling on timeline switches
* Walreceiver create temp slot more than once when timeline switch
@ 2026-08-22 15:12 cca5507 <cca5507@qq.com>
  2026-08-24 23:57 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  2026-08-25 01:01 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  0 siblings, 2 replies; 9+ messages in thread

From: cca5507 @ 2026-08-22 15:12 UTC (permalink / raw)
  To: pgsql-hackers <pgsql-hackers@lists.postgresql.org>

Hi hackers,

Steps to repro
==============

1) start a primary and do a basebackup

initdb -D primary -c wal_receiver_create_temp_slot=on -c port=5432
pg_ctl -D primary -l primary.log start
pg_basebackup -D standby -R -p 5432

2) increase the timeline of primary

touch primary/standby.signal
pg_ctl -D primary -l primary.log restart
pg_ctl -D primary -l primary.log promote

3) start the standby

pg_ctl -D standby -l standby.log start -o "-c port=5433"

Logs of standby
===============

2026-08-22 22:59:08.663 CST [49194] LOG:  started streaming WAL from primary at 0/03000000 on timeline 1
2026-08-22 22:59:08.674 CST [49194] LOG:  replication terminated by primary server
2026-08-22 22:59:08.674 CST [49194] DETAIL:  End of WAL reached on timeline 1 at 0/03052948.
2026-08-22 22:59:08.677 CST [49193] LOG:  new target timeline is 2
2026-08-22 22:59:08.677 CST [49193] LOG:  invalid record length at 0/03052948: expected at least 24, got 0
2026-08-22 22:59:08.678 CST [49194] FATAL:  could not create replication slot "pg_walreceiver_49195": ERROR:  replication slot "pg_walreceiver_49195" already exists
2026-08-22 22:59:08.682 CST [49196] LOG:  started streaming WAL from primary at 0/03000000 on timeline 2

Fix
===

Make sure that the walreceiver creates temp slot only once.

Thoughts?

--
Regards,
ChangAo Chen


Attachments:

v1-0001-Fix-walreceiver-create-temp-slot-more-than-once-w.patch (application/octet-stream, 2.2K) download From b50c78fc239d76ee934b761d6c2c71c9f8cfd491 Mon Sep 17 00:00:00 2001 From: ChangAo Chen <cca5507@qq.com> Date: Sat, 22 Aug 2026 22:40:46 +0800 Subject: [PATCH v1] Fix walreceiver create temp slot more than once when timeline switch. --- src/backend/replication/walreceiver.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 61dc6a5588b..e7e5bcdedb7 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -157,7 +157,7 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) char conninfo[MAXCONNINFO]; char *tmp_conninfo; char slotname[NAMEDATALEN]; - bool is_temp_slot; + bool create_temp_slot; XLogRecPtr startpoint; TimeLineID startpointTLI; TimeLineID primaryTLI; @@ -225,15 +225,15 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) walrcv->ready_to_display = false; strlcpy(conninfo, walrcv->conninfo, MAXCONNINFO); strlcpy(slotname, walrcv->slotname, NAMEDATALEN); - is_temp_slot = walrcv->is_temp_slot; + create_temp_slot = walrcv->is_temp_slot; startpoint = walrcv->receiveStart; startpointTLI = walrcv->receiveStartTLI; /* - * At most one of is_temp_slot and slotname can be set; otherwise, + * At most one of create_temp_slot and slotname can be set; otherwise, * RequestXLogStreaming messed up. */ - Assert(!is_temp_slot || (slotname[0] == '\0')); + Assert(!create_temp_slot || (slotname[0] == '\0')); /* Initialise to a sanish value */ now = GetCurrentTimestamp(); @@ -435,7 +435,7 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) * name in shared memory. (Note the slot name cannot already be set * in this case.) */ - if (is_temp_slot) + if (create_temp_slot) { snprintf(slotname, sizeof(slotname), "pg_walreceiver_%lld", @@ -443,6 +443,9 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) walrcv_create_slot(wrconn, slotname, true, false, false, 0, NULL); + /* Only create once */ + create_temp_slot = false; + SpinLockAcquire(&walrcv->mutex); strlcpy(walrcv->slotname, slotname, NAMEDATALEN); SpinLockRelease(&walrcv->mutex); -- 2.34.1
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
@ 2026-08-24 23:57 ` Quan Zongliang <quanzongliang@yeah.net>
  1 sibling, 0 replies; 9+ messages in thread

From: Quan Zongliang @ 2026-08-24 23:57 UTC (permalink / raw)
  To: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>



On 8/22/26 11:12 PM, cca5507 wrote:

> 
> Logs of standby
> ===============
> 
> 2026-08-22 22:59:08.663 CST [49194] LOG:  started streaming WAL from primary at 0/03000000 on timeline 1
> 2026-08-22 22:59:08.674 CST [49194] LOG:  replication terminated by primary server
> 2026-08-22 22:59:08.674 CST [49194] DETAIL:  End of WAL reached on timeline 1 at 0/03052948.
> 2026-08-22 22:59:08.677 CST [49193] LOG:  new target timeline is 2
> 2026-08-22 22:59:08.677 CST [49193] LOG:  invalid record length at 0/03052948: expected at least 24, got 0
> 2026-08-22 22:59:08.678 CST [49194] FATAL:  could not create replication slot "pg_walreceiver_49195": ERROR:  replication slot "pg_walreceiver_49195" already exists
> 2026-08-22 22:59:08.682 CST [49196] LOG:  started streaming WAL from primary at 0/03000000 on timeline 2
> 
The bug has been reproduced.

> Fix
> ===
> 
> Make sure that the walreceiver creates temp slot only once.
After testing, it was confirmed that it did not occur again. I'm 
planning to conduct another long-term test.

> 
> Thoughts?
> 






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

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
@ 2026-08-25 01:01 ` Quan Zongliang <quanzongliang@yeah.net>
  2026-08-25 06:12   ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Quan Zongliang @ 2026-08-25 01:01 UTC (permalink / raw)
  To: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>



On 8/22/26 11:12 PM, cca5507 wrote:
> Fix
> ===
> 
> Make sure that the walreceiver creates temp slot only once.
> 
> Thoughts?
I think there is nothing wrong with the code of this patch.

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));


The log will output:
2026-08-25 08:44:23.199 CST [27043] LOG: creating replication slot 
"pg_walreceiver_####"

> 







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

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-25 01:01 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
@ 2026-08-25 06:12   ` Fujii Masao <masao.fujii@gmail.com>
  2026-08-25 13:43     ` Re: Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Fujii Masao @ 2026-08-25 06:12 UTC (permalink / raw)
  To: Quan Zongliang <quanzongliang@yeah.net>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Aug 25, 2026 at 10:02 AM Quan Zongliang <quanzongliang@yeah.net> wrote:
>
>
>
> On 8/22/26 11:12 PM, cca5507 wrote:
> > Fix
> > ===
> >
> > Make sure that the walreceiver creates temp slot only once.
> >
> > Thoughts?
> I think there is nothing wrong with the code of this patch.

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.


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?


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

BTW, as for the proposed logging, I'm not sure it would be an improvement.
If we decide to add it, though, I think DEBUG would be more appropriate
than LOG.

Regards,

-- 
Fujii Masao






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

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-25 01:01 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  2026-08-25 06:12   ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
@ 2026-08-25 13:43     ` cca5507 <cca5507@qq.com>
  2026-08-26 03:10       ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  2026-08-26 09:22       ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 2 replies; 9+ messages in thread

From: cca5507 @ 2026-08-25 13:43 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; Quan Zongliang <quanzongliang@yeah.net>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>

> 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
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-25 01:01 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  2026-08-25 06:12   ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  2026-08-25 13:43     ` Re: Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
@ 2026-08-26 03:10       ` Quan Zongliang <quanzongliang@yeah.net>
  1 sibling, 0 replies; 9+ messages in thread

From: Quan Zongliang @ 2026-08-26 03:10 UTC (permalink / raw)
  To: cca5507 <cca5507@qq.com>; Fujii Masao <masao.fujii@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>



On 8/25/26 9:43 PM, cca5507 wrote:
>> 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.
> 

v2 can show the correct slot_name.

Perform TAP tests using the original and v1 versions of walreceiver.c 
along with 004_timeline_switch.pl. Both issues (creation error and NULL 
slot_name) were reported correctly.

--
Quan Zongliang







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

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-25 01:01 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  2026-08-25 06:12   ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  2026-08-25 13:43     ` Re: Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
@ 2026-08-26 09:22       ` Fujii Masao <masao.fujii@gmail.com>
  2026-08-26 10:07         ` Re: Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Fujii Masao @ 2026-08-26 09:22 UTC (permalink / raw)
  To: cca5507 <cca5507@qq.com>; +Cc: Quan Zongliang <quanzongliang@yeah.net>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Aug 25, 2026 at 10:43 PM cca5507 <cca5507@qq.com> wrote:
> Please see the v2 patch.

Thanks for updating the patch! LGTM.

I've applied only cosmetic changes. The updated patch is attached.

This should be backpatched to all supported versions, i.e., through v14, right?
I've also prepared patches for the back branches.

Regards,

-- 
Fujii Masao

Attachments:

v3-0001-PG14-Fix-temporary-WAL-receiver-slot-handling-on-timel.txt (text/plain, 5.2K) download From 98e8199242db0c175f229887a261965cf2161f9c Mon Sep 17 00:00:00 2001 From: ChangAo Chen <cca5507@qq.com> Date: Tue, 25 Aug 2026 21:31:23 +0800 Subject: [PATCH v3] Fix temporary WAL receiver slot handling on timeline switches Previously, when wal_receiver_create_temp_slot was enabled, a timeline switch could cause the walreceiver to try to create the same temporary replication slot again on the same connection. The slot had already been created before the first streaming attempt and still existed, so the second creation attempt failed with a FATAL error such as "could not create replication slot ...". The walreceiver would later be restarted and streaming replication could continue, so this did not permanently break replication. Nevertheless, the unexpected failure is a bug and should be fixed. Fix this by tracking whether the temporary replication slot has already been created for the lifetime of the walreceiver and skipping subsequent creation attempts. Also copy the retained slot name to shared memory on each streaming attempt, since RequestXLogStreaming() clears it when streaming is restarted without a configured primary slot. This also keeps pg_stat_wal_receiver.slot_name populated after timeline switches. Backpatch to all supported versions. --- src/backend/replication/walreceiver.c | 23 +++++++---- src/test/recovery/t/004_timeline_switch.pl | 46 +++++++++++++++++++++- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 220c97d2afa..df9ab04ccb8 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -178,6 +178,7 @@ WalReceiverMain(void) char *tmp_conninfo; char slotname[NAMEDATALEN]; bool is_temp_slot; + bool temp_slot_created = false; XLogRecPtr startpoint; TimeLineID startpointTLI; TimeLineID primaryTLI; @@ -438,17 +439,25 @@ WalReceiverMain(void) 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 streaming attempt on this connection + * because the slot remains available while this connection is reused + * for later streaming attempts. Update the slot name in shared + * memory each time because RequestXLogStreaming() clears it when + * restarting streaming. */ 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, 0, NULL); - walrcv_create_slot(wrconn, slotname, true, 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 ef994854730..65384fa7948 100644 --- a/src/test/recovery/t/004_timeline_switch.pl +++ b/src/test/recovery/t/004_timeline_switch.pl @@ -7,7 +7,7 @@ use warnings; use File::Path qw(rmtree); use PostgresNode; use TestLib; -use Test::More tests => 6; +use Test::More tests => 8; $ENV{PGDATABASE} = 'postgres'; @@ -149,3 +149,47 @@ $node_primary_2->wait_for_catchup($node_standby_3, 'replay', 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 = get_new_node('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 = get_new_node('standby_4'); +$node_standby_4->init_from_backup($node_primary_3, $backup_name, + has_streaming => 1); +$node_standby_4->append_conf( + 'postgresql.conf', qq( +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, 'replay', + $node_primary_3->lsn('write')); + +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'); -- 2.55.0
v3-0001-Fix-temporary-WAL-receiver-slot-handling-on-timel.patch (application/octet-stream, 5.1K) download From 8379f819dff5a510f27215ea7dac405c1101fd3d Mon Sep 17 00:00:00 2001 From: ChangAo Chen <cca5507@qq.com> Date: Tue, 25 Aug 2026 21:31:23 +0800 Subject: [PATCH v3] Fix temporary WAL receiver slot handling on timeline switches Previously, when wal_receiver_create_temp_slot was enabled, a timeline switch could cause the walreceiver to try to create the same temporary replication slot again on the same connection. The slot had already been created before the first streaming attempt and still existed, so the second creation attempt failed with a FATAL error such as "could not create replication slot ...". The walreceiver would later be restarted and streaming replication could continue, so this did not permanently break replication. Nevertheless, the unexpected failure is a bug and should be fixed. Fix this by tracking whether the temporary replication slot has already been created for the lifetime of the walreceiver and skipping subsequent creation attempts. Also copy the retained slot name to shared memory on each streaming attempt, since RequestXLogStreaming() clears it when streaming is restarted without a configured primary slot. This also keeps pg_stat_wal_receiver.slot_name populated after timeline switches. Backpatch to all supported versions. --- src/backend/replication/walreceiver.c | 23 ++++++++---- src/test/recovery/t/004_timeline_switch.pl | 43 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 61dc6a5588b..b93e699ba4b 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,25 @@ 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 streaming attempt on this connection + * because the slot remains available while this connection is reused + * for later streaming attempts. Update the slot name in shared + * memory each time because RequestXLogStreaming() clears it when + * restarting streaming. */ 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..4bf111346e7 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', qq( +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.55.0
v3-0001-PG15-PG16-Fix-temporary-WAL-receiver-slot-handling-on-timel.txt (text/plain, 5.0K) download From 1de875713b57c7940dc0264b2b35e9780e9d79ff Mon Sep 17 00:00:00 2001 From: ChangAo Chen <cca5507@qq.com> Date: Tue, 25 Aug 2026 21:31:23 +0800 Subject: [PATCH v3] Fix temporary WAL receiver slot handling on timeline switches Previously, when wal_receiver_create_temp_slot was enabled, a timeline switch could cause the walreceiver to try to create the same temporary replication slot again on the same connection. The slot had already been created before the first streaming attempt and still existed, so the second creation attempt failed with a FATAL error such as "could not create replication slot ...". The walreceiver would later be restarted and streaming replication could continue, so this did not permanently break replication. Nevertheless, the unexpected failure is a bug and should be fixed. Fix this by tracking whether the temporary replication slot has already been created for the lifetime of the walreceiver and skipping subsequent creation attempts. Also copy the retained slot name to shared memory on each streaming attempt, since RequestXLogStreaming() clears it when streaming is restarted without a configured primary slot. This also keeps pg_stat_wal_receiver.slot_name populated after timeline switches. Backpatch to all supported versions. --- src/backend/replication/walreceiver.c | 23 ++++++++---- src/test/recovery/t/004_timeline_switch.pl | 43 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 2da915aa3ce..c95f0e88f38 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -197,6 +197,7 @@ WalReceiverMain(void) char *tmp_conninfo; char slotname[NAMEDATALEN]; bool is_temp_slot; + bool temp_slot_created = false; XLogRecPtr startpoint; TimeLineID startpointTLI; TimeLineID primaryTLI; @@ -454,17 +455,25 @@ WalReceiverMain(void) 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 streaming attempt on this connection + * because the slot remains available while this connection is reused + * for later streaming attempts. Update the slot name in shared + * memory each time because RequestXLogStreaming() clears it when + * restarting streaming. */ 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, 0, NULL); - walrcv_create_slot(wrconn, slotname, true, 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 0aa4cf0c188..6008b1fba8a 100644 --- a/src/test/recovery/t/004_timeline_switch.pl +++ b/src/test/recovery/t/004_timeline_switch.pl @@ -147,4 +147,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', qq( +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.55.0
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-25 01:01 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  2026-08-25 06:12   ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  2026-08-25 13:43     ` Re: Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-26 09:22       ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
@ 2026-08-26 10:07         ` cca5507 <cca5507@qq.com>
  2026-08-27 16:20           ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: cca5507 @ 2026-08-26 10:07 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Quan Zongliang <quanzongliang@yeah.net>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>

> > Please see the v2 patch.
> 
> Thanks for updating the patch! LGTM.
> 
> I've applied only cosmetic changes. The updated patch is attached.
> 
> This should be backpatched to all supported versions, i.e., through v14, right?

Yes, I think so.

> I've also prepared patches for the back branches.

Thanks for the v3 patches, all LGTM!

--
Regards,
ChangAo Chen


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

* Re: Walreceiver create temp slot more than once when timeline switch
  2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-25 01:01 ` Re: Walreceiver create temp slot more than once when timeline switch Quan Zongliang <quanzongliang@yeah.net>
  2026-08-25 06:12   ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  2026-08-25 13:43     ` Re: Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
  2026-08-26 09:22       ` Re: Walreceiver create temp slot more than once when timeline switch Fujii Masao <masao.fujii@gmail.com>
  2026-08-26 10:07         ` Re: Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
@ 2026-08-27 16:20           ` Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Fujii Masao @ 2026-08-27 16:20 UTC (permalink / raw)
  To: cca5507 <cca5507@qq.com>; +Cc: Quan Zongliang <quanzongliang@yeah.net>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>

On Wed, Aug 26, 2026 at 7:07 PM cca5507 <cca5507@qq.com> wrote:
> Thanks for the v3 patches, all LGTM!

Thanks for the review! I've pushed the patch.

Regards,

-- 
Fujii Masao






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


end of thread, other threads:[~2026-08-27 16:20 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-22 15:12 Walreceiver create temp slot more than once when timeline switch cca5507 <cca5507@qq.com>
2026-08-24 23:57 ` Quan Zongliang <quanzongliang@yeah.net>
2026-08-25 01:01 ` Quan Zongliang <quanzongliang@yeah.net>
2026-08-25 06:12   ` Fujii Masao <masao.fujii@gmail.com>
2026-08-25 13:43     ` cca5507 <cca5507@qq.com>
2026-08-26 03:10       ` Quan Zongliang <quanzongliang@yeah.net>
2026-08-26 09:22       ` Fujii Masao <masao.fujii@gmail.com>
2026-08-26 10:07         ` cca5507 <cca5507@qq.com>
2026-08-27 16:20           ` Fujii Masao <masao.fujii@gmail.com>

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