Walreceiver create temp slot more than once when timeline switch

Started by cca55073 months ago9 messageshackers
Jump to latest
#1cca5507
cca5507@qq.com

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:

t253521_1
v1-0001-Fix-walreceiver-create-temp-slot-more-than-once-w.patchapplication/octet-stream; charset=utf-8; name=v1-0001-Fix-walreceiver-create-temp-slot-more-than-once-w.patchDownload+8-6
#2Quan Zongliang
quanzongliang@yeah.net
In reply to: cca5507 (#1)
Re: Walreceiver create temp slot more than once when timeline switch

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.

Show quoted text

Thoughts?

#3Quan Zongliang
quanzongliang@yeah.net
In reply to: cca5507 (#1)
Re: Walreceiver create temp slot more than once when timeline switch

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_####"

Show quoted text
#4Fujii Masao
masao.fujii@gmail.com
In reply to: Quan Zongliang (#3)
Re: Walreceiver create temp slot more than once when timeline switch

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

#5cca5507
cca5507@qq.com
In reply to: Fujii Masao (#4)
Re: Walreceiver create temp slot more than once when timeline switch

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:

t253521_5
v2-0001-Fix-temporary-WAL-receiver-slot-handling-on-timel.patchapplication/octet-stream; charset=utf-8; name=v2-0001-Fix-temporary-WAL-receiver-slot-handling-on-timel.patchDownload+58-8
#6Quan Zongliang
quanzongliang@yeah.net
In reply to: cca5507 (#5)
Re: Walreceiver create temp slot more than once when timeline switch

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

#7Fujii Masao
masao.fujii@gmail.com
In reply to: cca5507 (#5)
Re: Walreceiver create temp slot more than once when timeline switch

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:

t253521_7
v3-0001-PG14-Fix-temporary-WAL-receiver-slot-handling-on-timel.txttext/plain; charset=US-ASCII; name=v3-0001-PG14-Fix-temporary-WAL-receiver-slot-handling-on-timel.txtDownload+61-9
v3-0001-Fix-temporary-WAL-receiver-slot-handling-on-timel.patchapplication/octet-stream; name=v3-0001-Fix-temporary-WAL-receiver-slot-handling-on-timel.patchDownload+59-8
v3-0001-PG15-PG16-Fix-temporary-WAL-receiver-slot-handling-on-timel.txttext/plain; charset=US-ASCII; name=v3-0001-PG15-PG16-Fix-temporary-WAL-receiver-slot-handling-on-timel.txtDownload+59-8
#8cca5507
cca5507@qq.com
In reply to: Fujii Masao (#7)
Re: Walreceiver create temp slot more than once when timeline switch

 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

#9Fujii Masao
masao.fujii@gmail.com
In reply to: cca5507 (#8)
Re: Walreceiver create temp slot more than once when timeline switch

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