Skip to content

Commit 25b4840

Browse files
committed
Fix unconditional WAL receiver shutdown during stream-archive transition
Commit b4f584f (affecting v15~, later backpatched down to 13 as of 3635a0a) introduced an unconditional WAL receiver shutdown when switching from streaming to archive WAL sources. This causes problems during a timeline switch, when a WAL receiver enters WALRCV_WAITING state but remains alive, waiting for instructions. The unconditional shutdown can break some monitoring scenarios as the WAL receiver gets repeatedly terminated and re-spawned, causing pg_stat_wal_receiver.status to show a "streaming" instead of "waiting" status, masking the fact that the WAL receiver is waiting for a new TLI and a new LSN to be able to continue streaming. This commit changes the WAL receiver behavior so as the shutdown becomes conditional, with InstallXLogFileSegmentActive being always reset to prevent the regression fixed by b4f584f: only terminate the WAL receiver when it is actively streaming (WALRCV_STREAMING, WALRCV_STARTING, or WALRCV_RESTARTING). When in WALRCV_WAITING state, just reset InstallXLogFileSegmentActive flag to allow archive restoration without killing the process. WALRCV_STOPPED and WALRCV_STOPPING are not reachable states in this code path. For the latter, the startup process is the one in charge of setting WALRCV_STOPPING via ShutdownWalRcv(), waiting for the WAL receiver to reach a WALRCV_STOPPED state after switching walRcvState, so WaitForWALToBecomeAvailable() cannot be reached while a WAL receiver is in a WALRCV_STOPPING state. A regression test is added to check that a WAL receiver is not stopped on timeline jump, that fails when the fix of this commit is reverted. Reported-by: Ryan Bird <ryanzxg@gmail.com> Author: Xuneng Zhou <xunengzhou@gmail.com> Reviewed-by: Noah Misch <noah@leadboat.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/19093-c4fff49a608f82a0@postgresql.org Backpatch-through: 13
1 parent 438ccd3 commit 25b4840

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/backend/access/transam/xlog.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ static int XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
936936
int reqLen, XLogRecPtr targetRecPtr, char *readBuf);
937937
static bool WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
938938
bool fetching_ckpt, XLogRecPtr tliRecPtr);
939+
static void ResetInstallXLogFileSegmentActive(void);
939940
static void XLogShutdownWalRcv(void);
940941
static int emode_for_corrupt_record(int emode, XLogRecPtr RecPtr);
941942
static void XLogFileClose(void);
@@ -12611,8 +12612,18 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
1261112612
* Before we leave XLOG_FROM_STREAM state, make sure that
1261212613
* walreceiver is not active, so that it won't overwrite
1261312614
* WAL that we restore from archive.
12615+
* If walreceiver is actively streaming (or attempting to
12616+
* connect), we must shut it down. However, if it's
12617+
* already in WAITING state (e.g., due to timeline
12618+
* divergence), we only need to reset the install flag to
12619+
* allow archive restoration.
1261412620
*/
12615-
XLogShutdownWalRcv();
12621+
if (WalRcvStreaming())
12622+
XLogShutdownWalRcv();
12623+
else
12624+
{
12625+
ResetInstallXLogFileSegmentActive();
12626+
}
1261612627

1261712628
/*
1261812629
* Before we sleep, re-scan for possible new timelines if
@@ -12958,17 +12969,23 @@ StartupRequestWalReceiverRestart(void)
1295812969
}
1295912970
}
1296012971

12961-
/* Thin wrapper around ShutdownWalRcv(). */
12972+
/* Disable WAL file recycling and preallocation. */
1296212973
static void
12963-
XLogShutdownWalRcv(void)
12974+
ResetInstallXLogFileSegmentActive(void)
1296412975
{
12965-
ShutdownWalRcv();
12966-
1296712976
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
1296812977
XLogCtl->InstallXLogFileSegmentActive = false;
1296912978
LWLockRelease(ControlFileLock);
1297012979
}
1297112980

12981+
/* Thin wrapper around ShutdownWalRcv(). */
12982+
static void
12983+
XLogShutdownWalRcv(void)
12984+
{
12985+
ShutdownWalRcv();
12986+
ResetInstallXLogFileSegmentActive();
12987+
}
12988+
1297212989
/*
1297312990
* Determine what log level should be used to report a corrupt WAL record
1297412991
* in the current WAL page, previously read by XLogPageRead().

src/test/recovery/t/004_timeline_switch.pl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use File::Path qw(rmtree);
55
use PostgresNode;
66
use TestLib;
7-
use Test::More tests => 3;
7+
use Test::More tests => 4;
88

99
$ENV{PGDATABASE} = 'postgres';
1010

@@ -68,6 +68,14 @@
6868
$node_standby_2->safe_psql('postgres', "SELECT count(*) FROM tab_int");
6969
is($result, qq(2000), 'check content of standby 2');
7070

71+
# Check the logs, WAL receiver should not have been stopped while
72+
# transitioning to its new timeline. There is no need to rely on an
73+
# offset in this check of the server logs: a new log file is used on
74+
# node restart when primary_conninfo is updated above.
75+
ok( !$node_standby_2->log_contains(
76+
"FATAL: .* terminating walreceiver process due to administrator command"
77+
),
78+
'WAL receiver should not be stopped across timeline jumps');
7179

7280
# Ensure that a standby is able to follow a master on a newer timeline
7381
# when WAL archiving is enabled.

0 commit comments

Comments
 (0)