diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index f6cd4fde2b..40eb77425e 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -7452,7 +7452,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; /* @@ -11896,12 +11900,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 (;;) { @@ -12093,6 +12108,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) {