From 412c298c9c677937fc7f5fdab5c0b21d2c9ec512 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Mon, 9 Mar 2020 15:31:31 +0900 Subject: [PATCH] Avoid assertion failure with targeted recovery in standby mode. At the end of recovery, standby mode is turned off to re-fetch the last valid record from archive or pg_wal. Previously, if recovery target was reached and standby mode was turned off while the current WAL source was stream, recovery could try to retrieve WAL file containing the last valid record unexpectedly from stream even though not in standby mode. This caused an assertion failure. That is, the assertion test confirms that WAL file should not be retrieved from stream if standby mode is not true. This commit moves back the current WAL source to archive if it's stream even though not in standby mode, to avoid that assertion failure. This issue doesn't cause the server to crash when built with assertion disabled. In this case, the attempt to retrieve WAL file from stream not in standby mode just fails. And then recovery tries to retrieve WAL file from archive or pg_wal. Back-patch to all supported branches. Author: Kyotaro Horiguchi Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/20200227.124830.2197604521555566121.horikyota.ntt@gmail.com --- src/backend/access/transam/xlog.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 63ab0ab6c2..05cb6aa022 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -7341,7 +7341,11 @@ StartupXLOG(void) * We are now done reading the xlog from stream. Turn off streaming * recovery to force fetching the files (which would be required at end of * recovery, e.g., timeline history file) from archive or pg_wal. + * + * Note that standby mode must be turned off after killing WAL receiver, + * i.e., calling ShutdownWalRcv(). */ + Assert(!WalRcvStreaming()); StandbyMode = false; /* @@ -11759,12 +11763,23 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess, * values for "check trigger", "rescan timelines", and "sleep" states, * those actions are taken when reading from the previous source fails, as * part of advancing to the next state. + * + * If standby mode is turned off while reading WAL from stream, we move + * to XLOG_FROM_ARCHIVE and reset lastSourceFailed, to force fetching + * the files (which would be required at end of recovery, e.g., timeline + * history file) from archive or pg_wal. We don't need to kill WAL receiver + * here because it's already stopped when standby mode is turned off at + * the end of recovery. *------- */ if (!InArchiveRecovery) currentSource = XLOG_FROM_PG_WAL; - else if (currentSource == 0) + else if (currentSource == 0 || + (!StandbyMode && currentSource == XLOG_FROM_STREAM)) + { + lastSourceFailed = false; currentSource = XLOG_FROM_ARCHIVE; + } for (;;) { @@ -11958,6 +11973,12 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess, { case XLOG_FROM_ARCHIVE: case XLOG_FROM_PG_WAL: + /* + * WAL receiver must not be running when reading WAL from + * archive or pg_wal. + */ + Assert(!WalRcvStreaming()); + /* Close any old file we might have open. */ if (readFile >= 0) {