LCOV - code coverage report
Current view: top level - src/backend/access/transam - xlogrecovery.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 87.4 % 1420 1241
Test Date: 2026-08-16 22:15:49 Functions: 98.5 % 66 65
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 66.5 % 1159 771

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * xlogrecovery.c
       4                 :             :  *      Functions for WAL recovery, standby mode
       5                 :             :  *
       6                 :             :  * This source file contains functions controlling WAL recovery.
       7                 :             :  * InitWalRecovery() initializes the system for crash or archive recovery,
       8                 :             :  * or standby mode, depending on configuration options and the state of
       9                 :             :  * the control file and possible backup label file.  PerformWalRecovery()
      10                 :             :  * performs the actual WAL replay, calling the rmgr-specific redo routines.
      11                 :             :  * FinishWalRecovery() performs end-of-recovery checks and cleanup actions,
      12                 :             :  * and prepares information needed to initialize the WAL for writes.  In
      13                 :             :  * addition to these three main functions, there are a bunch of functions
      14                 :             :  * for interrogating recovery state and controlling the recovery process.
      15                 :             :  *
      16                 :             :  *
      17                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      18                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      19                 :             :  *
      20                 :             :  * src/backend/access/transam/xlogrecovery.c
      21                 :             :  *
      22                 :             :  *-------------------------------------------------------------------------
      23                 :             :  */
      24                 :             : 
      25                 :             : #include "postgres.h"
      26                 :             : 
      27                 :             : #include <ctype.h>
      28                 :             : #include <time.h>
      29                 :             : #include <sys/stat.h>
      30                 :             : #include <sys/time.h>
      31                 :             : #include <unistd.h>
      32                 :             : 
      33                 :             : #include "access/timeline.h"
      34                 :             : #include "access/transam.h"
      35                 :             : #include "access/xact.h"
      36                 :             : #include "access/xlog_internal.h"
      37                 :             : #include "access/xlogarchive.h"
      38                 :             : #include "access/xlogprefetcher.h"
      39                 :             : #include "access/xlogreader.h"
      40                 :             : #include "access/xlogrecovery.h"
      41                 :             : #include "access/xlogutils.h"
      42                 :             : #include "access/xlogwait.h"
      43                 :             : #include "backup/basebackup.h"
      44                 :             : #include "catalog/pg_control.h"
      45                 :             : #include "commands/tablespace.h"
      46                 :             : #include "common/file_utils.h"
      47                 :             : #include "miscadmin.h"
      48                 :             : #include "nodes/miscnodes.h"
      49                 :             : #include "pgstat.h"
      50                 :             : #include "postmaster/bgwriter.h"
      51                 :             : #include "postmaster/startup.h"
      52                 :             : #include "replication/slot.h"
      53                 :             : #include "replication/slotsync.h"
      54                 :             : #include "replication/walreceiver.h"
      55                 :             : #include "storage/fd.h"
      56                 :             : #include "storage/ipc.h"
      57                 :             : #include "storage/latch.h"
      58                 :             : #include "storage/pmsignal.h"
      59                 :             : #include "storage/procarray.h"
      60                 :             : #include "storage/spin.h"
      61                 :             : #include "storage/subsystems.h"
      62                 :             : #include "utils/datetime.h"
      63                 :             : #include "utils/fmgrprotos.h"
      64                 :             : #include "utils/guc.h"
      65                 :             : #include "utils/guc_hooks.h"
      66                 :             : #include "utils/pgstat_internal.h"
      67                 :             : #include "utils/pg_lsn.h"
      68                 :             : #include "utils/ps_status.h"
      69                 :             : #include "utils/pg_rusage.h"
      70                 :             : #include "utils/wait_event.h"
      71                 :             : 
      72                 :             : /* Unsupported old recovery command file names (relative to $PGDATA) */
      73                 :             : #define RECOVERY_COMMAND_FILE   "recovery.conf"
      74                 :             : #define RECOVERY_COMMAND_DONE   "recovery.done"
      75                 :             : 
      76                 :             : /*
      77                 :             :  * GUC support
      78                 :             :  */
      79                 :             : const struct config_enum_entry recovery_target_action_options[] = {
      80                 :             :     {"pause", RECOVERY_TARGET_ACTION_PAUSE, false},
      81                 :             :     {"promote", RECOVERY_TARGET_ACTION_PROMOTE, false},
      82                 :             :     {"shutdown", RECOVERY_TARGET_ACTION_SHUTDOWN, false},
      83                 :             :     {NULL, 0, false}
      84                 :             : };
      85                 :             : 
      86                 :             : /* options formerly taken from recovery.conf for archive recovery */
      87                 :             : char       *recoveryRestoreCommand = NULL;
      88                 :             : char       *recoveryEndCommand = NULL;
      89                 :             : char       *archiveCleanupCommand = NULL;
      90                 :             : RecoveryTargetType recoveryTarget = RECOVERY_TARGET_UNSET;
      91                 :             : bool        recoveryTargetInclusive = true;
      92                 :             : int         recoveryTargetAction = RECOVERY_TARGET_ACTION_PAUSE;
      93                 :             : TransactionId recoveryTargetXid;
      94                 :             : char       *recovery_target_time_string;
      95                 :             : TimestampTz recoveryTargetTime;
      96                 :             : char       *recoveryTargetName;
      97                 :             : XLogRecPtr  recoveryTargetLSN;
      98                 :             : int         recovery_min_apply_delay = 0;
      99                 :             : 
     100                 :             : /* options formerly taken from recovery.conf for XLOG streaming */
     101                 :             : char       *PrimaryConnInfo = NULL;
     102                 :             : char       *PrimarySlotName = NULL;
     103                 :             : bool        wal_receiver_create_temp_slot = false;
     104                 :             : 
     105                 :             : /*
     106                 :             :  * recoveryTargetTimeLineGoal: what the user requested, if any
     107                 :             :  *
     108                 :             :  * recoveryTargetTLIRequested: numeric value of requested timeline, if constant
     109                 :             :  *
     110                 :             :  * recoveryTargetTLI: the currently understood target timeline; changes
     111                 :             :  *
     112                 :             :  * expectedTLEs: a list of TimeLineHistoryEntries for recoveryTargetTLI and
     113                 :             :  * the timelines of its known parents, newest first (so recoveryTargetTLI is
     114                 :             :  * always the first list member).  Only these TLIs are expected to be seen in
     115                 :             :  * the WAL segments we read, and indeed only these TLIs will be considered as
     116                 :             :  * candidate WAL files to open at all.
     117                 :             :  *
     118                 :             :  * curFileTLI: the TLI appearing in the name of the current input WAL file.
     119                 :             :  * (This is not necessarily the same as the timeline from which we are
     120                 :             :  * replaying WAL, which StartupXLOG calls replayTLI, because we could be
     121                 :             :  * scanning data that was copied from an ancestor timeline when the current
     122                 :             :  * file was created.)  During a sequential scan we do not allow this value
     123                 :             :  * to decrease.
     124                 :             :  */
     125                 :             : RecoveryTargetTimeLineGoal recoveryTargetTimeLineGoal = RECOVERY_TARGET_TIMELINE_LATEST;
     126                 :             : TimeLineID  recoveryTargetTLIRequested = 0;
     127                 :             : TimeLineID  recoveryTargetTLI = 0;
     128                 :             : static List *expectedTLEs;
     129                 :             : static TimeLineID curFileTLI;
     130                 :             : 
     131                 :             : /*
     132                 :             :  * When ArchiveRecoveryRequested is set, archive recovery was requested,
     133                 :             :  * ie. signal files were present.  When InArchiveRecovery is set, we are
     134                 :             :  * currently recovering using offline XLOG archives.  These variables are only
     135                 :             :  * valid in the startup process.
     136                 :             :  *
     137                 :             :  * When ArchiveRecoveryRequested is true, but InArchiveRecovery is false, we're
     138                 :             :  * currently performing crash recovery using only XLOG files in pg_wal, but
     139                 :             :  * will switch to using offline XLOG archives as soon as we reach the end of
     140                 :             :  * WAL in pg_wal.
     141                 :             :  */
     142                 :             : bool        ArchiveRecoveryRequested = false;
     143                 :             : bool        InArchiveRecovery = false;
     144                 :             : 
     145                 :             : /*
     146                 :             :  * When StandbyModeRequested is set, standby mode was requested, i.e.
     147                 :             :  * standby.signal file was present.  When StandbyMode is set, we are currently
     148                 :             :  * in standby mode.  These variables are only valid in the startup process.
     149                 :             :  * They work similarly to ArchiveRecoveryRequested and InArchiveRecovery.
     150                 :             :  */
     151                 :             : static bool StandbyModeRequested = false;
     152                 :             : bool        StandbyMode = false;
     153                 :             : 
     154                 :             : /* was a signal file present at startup? */
     155                 :             : static bool standby_signal_file_found = false;
     156                 :             : static bool recovery_signal_file_found = false;
     157                 :             : 
     158                 :             : /*
     159                 :             :  * CheckPointLoc is the position of the checkpoint record that determines
     160                 :             :  * where to start the replay.  It comes from the backup label file or the
     161                 :             :  * control file.
     162                 :             :  *
     163                 :             :  * RedoStartLSN is the checkpoint's REDO location, also from the backup label
     164                 :             :  * file or the control file.  In standby mode, XLOG streaming usually starts
     165                 :             :  * from the position where an invalid record was found.  But if we fail to
     166                 :             :  * read even the initial checkpoint record, we use the REDO location instead
     167                 :             :  * of the checkpoint location as the start position of XLOG streaming.
     168                 :             :  * Otherwise we would have to jump backwards to the REDO location after
     169                 :             :  * reading the checkpoint record, because the REDO record can precede the
     170                 :             :  * checkpoint record.
     171                 :             :  */
     172                 :             : static XLogRecPtr CheckPointLoc = InvalidXLogRecPtr;
     173                 :             : static TimeLineID CheckPointTLI = 0;
     174                 :             : static XLogRecPtr RedoStartLSN = InvalidXLogRecPtr;
     175                 :             : static TimeLineID RedoStartTLI = 0;
     176                 :             : 
     177                 :             : /*
     178                 :             :  * Local copy of SharedHotStandbyActive variable. False actually means "not
     179                 :             :  * known, need to check the shared state".
     180                 :             :  */
     181                 :             : static bool LocalHotStandbyActive = false;
     182                 :             : 
     183                 :             : /*
     184                 :             :  * Local copy of SharedPromoteIsTriggered variable. False actually means "not
     185                 :             :  * known, need to check the shared state".
     186                 :             :  */
     187                 :             : static bool LocalPromoteIsTriggered = false;
     188                 :             : 
     189                 :             : /* Has the recovery code requested a walreceiver wakeup? */
     190                 :             : static bool doRequestWalReceiverReply;
     191                 :             : 
     192                 :             : /* XLogReader object used to parse the WAL records */
     193                 :             : static XLogReaderState *xlogreader = NULL;
     194                 :             : 
     195                 :             : /* XLogPrefetcher object used to consume WAL records with read-ahead */
     196                 :             : static XLogPrefetcher *xlogprefetcher = NULL;
     197                 :             : 
     198                 :             : /* Parameters passed down from ReadRecord to the XLogPageRead callback. */
     199                 :             : typedef struct XLogPageReadPrivate
     200                 :             : {
     201                 :             :     int         emode;
     202                 :             :     bool        fetching_ckpt;  /* are we fetching a checkpoint record? */
     203                 :             :     bool        randAccess;
     204                 :             :     TimeLineID  replayTLI;
     205                 :             : } XLogPageReadPrivate;
     206                 :             : 
     207                 :             : /* flag to tell XLogPageRead that we have started replaying */
     208                 :             : static bool InRedo = false;
     209                 :             : 
     210                 :             : /*
     211                 :             :  * Codes indicating where we got a WAL file from during recovery, or where
     212                 :             :  * to attempt to get one.
     213                 :             :  */
     214                 :             : typedef enum
     215                 :             : {
     216                 :             :     XLOG_FROM_ANY = 0,          /* request to read WAL from any source */
     217                 :             :     XLOG_FROM_ARCHIVE,          /* restored using restore_command */
     218                 :             :     XLOG_FROM_PG_WAL,           /* existing file in pg_wal */
     219                 :             :     XLOG_FROM_STREAM,           /* streamed from primary */
     220                 :             : } XLogSource;
     221                 :             : 
     222                 :             : /* human-readable names for XLogSources, for debugging output */
     223                 :             : static const char *const xlogSourceNames[] = {"any", "archive", "pg_wal", "stream"};
     224                 :             : 
     225                 :             : /*
     226                 :             :  * readFile is -1 or a kernel FD for the log file segment that's currently
     227                 :             :  * open for reading.  readSegNo identifies the segment.  readOff is the offset
     228                 :             :  * of the page just read, readLen indicates how much of it has been read into
     229                 :             :  * readBuf, and readSource indicates where we got the currently open file from.
     230                 :             :  *
     231                 :             :  * Note: we could use Reserve/ReleaseExternalFD to track consumption of this
     232                 :             :  * FD too (like for openLogFile in xlog.c); but it doesn't currently seem
     233                 :             :  * worthwhile, since the XLOG is not read by general-purpose sessions.
     234                 :             :  */
     235                 :             : static int  readFile = -1;
     236                 :             : static XLogSegNo readSegNo = 0;
     237                 :             : static uint32 readOff = 0;
     238                 :             : static uint32 readLen = 0;
     239                 :             : static XLogSource readSource = XLOG_FROM_ANY;
     240                 :             : 
     241                 :             : /*
     242                 :             :  * Keeps track of which source we're currently reading from. This is
     243                 :             :  * different from readSource in that this is always set, even when we don't
     244                 :             :  * currently have a WAL file open. If lastSourceFailed is set, our last
     245                 :             :  * attempt to read from currentSource failed, and we should try another source
     246                 :             :  * next.
     247                 :             :  *
     248                 :             :  * pendingWalRcvRestart is set when a config change occurs that requires a
     249                 :             :  * walreceiver restart.  This is only valid in XLOG_FROM_STREAM state.
     250                 :             :  */
     251                 :             : static XLogSource currentSource = XLOG_FROM_ANY;
     252                 :             : static bool lastSourceFailed = false;
     253                 :             : static bool pendingWalRcvRestart = false;
     254                 :             : 
     255                 :             : /*
     256                 :             :  * These variables track when we last obtained some WAL data to process,
     257                 :             :  * and where we got it from.  (XLogReceiptSource is initially the same as
     258                 :             :  * readSource, but readSource gets reset to zero when we don't have data
     259                 :             :  * to process right now.  It is also different from currentSource, which
     260                 :             :  * also changes when we try to read from a source and fail, while
     261                 :             :  * XLogReceiptSource tracks where we last successfully read some WAL.)
     262                 :             :  */
     263                 :             : static TimestampTz XLogReceiptTime = 0;
     264                 :             : static XLogSource XLogReceiptSource = XLOG_FROM_ANY;
     265                 :             : 
     266                 :             : /* Local copy of WalRcv->flushedUpto */
     267                 :             : static XLogRecPtr flushedUpto = InvalidXLogRecPtr;
     268                 :             : static TimeLineID receiveTLI = 0;
     269                 :             : 
     270                 :             : /*
     271                 :             :  * Copy of minRecoveryPoint and backupEndPoint from the control file.
     272                 :             :  *
     273                 :             :  * In order to reach consistency, we must replay the WAL up to
     274                 :             :  * minRecoveryPoint.  If backupEndRequired is true, we must also reach
     275                 :             :  * backupEndPoint, or if it's invalid, an end-of-backup record corresponding
     276                 :             :  * to backupStartPoint.
     277                 :             :  *
     278                 :             :  * Note: In archive recovery, after consistency has been reached, the
     279                 :             :  * functions in xlog.c will start updating minRecoveryPoint in the control
     280                 :             :  * file.  But this copy of minRecoveryPoint variable reflects the value at the
     281                 :             :  * beginning of recovery, and is *not* updated after consistency is reached.
     282                 :             :  */
     283                 :             : static XLogRecPtr minRecoveryPoint;
     284                 :             : static TimeLineID minRecoveryPointTLI;
     285                 :             : 
     286                 :             : static XLogRecPtr backupStartPoint;
     287                 :             : static XLogRecPtr backupEndPoint;
     288                 :             : static bool backupEndRequired = false;
     289                 :             : 
     290                 :             : /*
     291                 :             :  * Have we reached a consistent database state?  In crash recovery, we have
     292                 :             :  * to replay all the WAL, so reachedConsistency is never set.  During archive
     293                 :             :  * recovery, the database is consistent once minRecoveryPoint is reached.
     294                 :             :  *
     295                 :             :  * Consistent state means that the system is internally consistent, all
     296                 :             :  * the WAL has been replayed up to a certain point, and importantly, there
     297                 :             :  * is no trace of later actions on disk.
     298                 :             :  *
     299                 :             :  * This flag is used only by the startup process and postmaster. When
     300                 :             :  * minRecoveryPoint is reached, the startup process sets it to true and
     301                 :             :  * sends a PMSIGNAL_RECOVERY_CONSISTENT signal to the postmaster,
     302                 :             :  * which then sets it to true upon receiving the signal.
     303                 :             :  */
     304                 :             : bool        reachedConsistency = false;
     305                 :             : 
     306                 :             : /* Buffers dedicated to consistency checks of size BLCKSZ */
     307                 :             : static char *replay_image_masked = NULL;
     308                 :             : static char *primary_image_masked = NULL;
     309                 :             : 
     310                 :             : XLogRecoveryCtlData *XLogRecoveryCtl = NULL;
     311                 :             : 
     312                 :             : static void XLogRecoveryShmemRequest(void *arg);
     313                 :             : static void XLogRecoveryShmemInit(void *arg);
     314                 :             : 
     315                 :             : const ShmemCallbacks XLogRecoveryShmemCallbacks = {
     316                 :             :     .request_fn = XLogRecoveryShmemRequest,
     317                 :             :     .init_fn = XLogRecoveryShmemInit,
     318                 :             : };
     319                 :             : 
     320                 :             : /*
     321                 :             :  * abortedRecPtr is the start pointer of a broken record at end of WAL when
     322                 :             :  * recovery completes; missingContrecPtr is the location of the first
     323                 :             :  * contrecord that went missing.  See CreateOverwriteContrecordRecord for
     324                 :             :  * details.
     325                 :             :  */
     326                 :             : static XLogRecPtr abortedRecPtr;
     327                 :             : static XLogRecPtr missingContrecPtr;
     328                 :             : 
     329                 :             : /*
     330                 :             :  * if recoveryStopsBefore/After returns true, it saves information of the stop
     331                 :             :  * point here
     332                 :             :  */
     333                 :             : static TransactionId recoveryStopXid;
     334                 :             : static TimestampTz recoveryStopTime;
     335                 :             : static XLogRecPtr recoveryStopLSN;
     336                 :             : static char recoveryStopName[MAXFNAMELEN];
     337                 :             : static bool recoveryStopAfter;
     338                 :             : 
     339                 :             : /* prototypes for local functions */
     340                 :             : static void ApplyWalRecord(XLogReaderState *xlogreader, XLogRecord *record, TimeLineID *replayTLI);
     341                 :             : 
     342                 :             : static void EnableStandbyMode(void);
     343                 :             : static void readRecoverySignalFile(void);
     344                 :             : static void validateRecoveryParameters(void);
     345                 :             : static bool read_backup_label(XLogRecPtr *checkPointLoc,
     346                 :             :                               TimeLineID *backupLabelTLI,
     347                 :             :                               bool *backupEndRequired, bool *backupFromStandby);
     348                 :             : static bool read_tablespace_map(List **tablespaces);
     349                 :             : 
     350                 :             : static void xlogrecovery_redo(XLogReaderState *record, TimeLineID replayTLI);
     351                 :             : static void CheckRecoveryConsistency(void);
     352                 :             : static void rm_redo_error_callback(void *arg);
     353                 :             : #ifdef WAL_DEBUG
     354                 :             : static void xlog_outrec(StringInfo buf, XLogReaderState *record);
     355                 :             : #endif
     356                 :             : static void xlog_block_info(StringInfo buf, XLogReaderState *record);
     357                 :             : static void checkTimeLineSwitch(XLogRecPtr lsn, TimeLineID newTLI,
     358                 :             :                                 TimeLineID prevTLI, TimeLineID replayTLI);
     359                 :             : static bool getRecordTimestamp(XLogReaderState *record, TimestampTz *recordXtime);
     360                 :             : static void verifyBackupPageConsistency(XLogReaderState *record);
     361                 :             : 
     362                 :             : static bool recoveryStopsBefore(XLogReaderState *record);
     363                 :             : static bool recoveryStopsAfter(XLogReaderState *record);
     364                 :             : static char *getRecoveryStopReason(void);
     365                 :             : static void recoveryPausesHere(bool endOfRecovery);
     366                 :             : static bool recoveryApplyDelay(XLogReaderState *record);
     367                 :             : static void ConfirmRecoveryPaused(void);
     368                 :             : 
     369                 :             : static XLogRecord *ReadRecord(XLogPrefetcher *xlogprefetcher,
     370                 :             :                               int emode, bool fetching_ckpt,
     371                 :             :                               TimeLineID replayTLI);
     372                 :             : 
     373                 :             : static int  XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
     374                 :             :                          int reqLen, XLogRecPtr targetRecPtr, char *readBuf);
     375                 :             : static XLogPageReadResult WaitForWALToBecomeAvailable(XLogRecPtr RecPtr,
     376                 :             :                                                       bool randAccess,
     377                 :             :                                                       bool fetching_ckpt,
     378                 :             :                                                       XLogRecPtr tliRecPtr,
     379                 :             :                                                       TimeLineID replayTLI,
     380                 :             :                                                       XLogRecPtr replayLSN,
     381                 :             :                                                       bool nonblocking);
     382                 :             : static int  emode_for_corrupt_record(int emode, XLogRecPtr RecPtr);
     383                 :             : static XLogRecord *ReadCheckpointRecord(XLogPrefetcher *xlogprefetcher,
     384                 :             :                                         XLogRecPtr RecPtr, TimeLineID replayTLI);
     385                 :             : static bool rescanLatestTimeLine(TimeLineID replayTLI, XLogRecPtr replayLSN);
     386                 :             : static int  XLogFileRead(XLogSegNo segno, TimeLineID tli,
     387                 :             :                          XLogSource source, bool notfoundOk);
     388                 :             : static int  XLogFileReadAnyTLI(XLogSegNo segno, XLogSource source);
     389                 :             : 
     390                 :             : static bool CheckForStandbyTrigger(void);
     391                 :             : static void SetPromoteIsTriggered(void);
     392                 :             : static bool HotStandbyActiveInReplay(void);
     393                 :             : 
     394                 :             : static void SetCurrentChunkStartTime(TimestampTz xtime);
     395                 :             : static void SetLatestXTime(TimestampTz xtime);
     396                 :             : static RecoveryTargetType DetermineRecoveryTargetType(void);
     397                 :             : 
     398                 :             : /*
     399                 :             :  * Register shared memory for WAL recovery
     400                 :             :  */
     401                 :             : static void
     402                 :        1271 : XLogRecoveryShmemRequest(void *arg)
     403                 :             : {
     404                 :        1271 :     ShmemRequestStruct(.name = "XLOG Recovery Ctl",
     405                 :             :                        .size = sizeof(XLogRecoveryCtlData),
     406                 :             :                        .ptr = (void **) &XLogRecoveryCtl,
     407                 :             :         );
     408                 :        1271 : }
     409                 :             : 
     410                 :             : static void
     411                 :        1268 : XLogRecoveryShmemInit(void *arg)
     412                 :             : {
     413                 :        1268 :     memset(XLogRecoveryCtl, 0, sizeof(XLogRecoveryCtlData));
     414                 :             : 
     415                 :        1268 :     SpinLockInit(&XLogRecoveryCtl->info_lck);
     416                 :        1268 :     InitSharedLatch(&XLogRecoveryCtl->recoveryWakeupLatch);
     417                 :        1268 :     ConditionVariableInit(&XLogRecoveryCtl->recoveryNotPausedCV);
     418                 :        1268 : }
     419                 :             : 
     420                 :             : /*
     421                 :             :  * A thin wrapper to enable StandbyMode and do other preparatory work as
     422                 :             :  * needed.
     423                 :             :  */
     424                 :             : static void
     425                 :         126 : EnableStandbyMode(void)
     426                 :             : {
     427                 :         126 :     StandbyMode = true;
     428                 :             : 
     429                 :             :     /*
     430                 :             :      * To avoid server log bloat, we don't report recovery progress in a
     431                 :             :      * standby as it will always be in recovery unless promoted. We disable
     432                 :             :      * startup progress timeout in standby mode to avoid calling
     433                 :             :      * startup_progress_timeout_handler() unnecessarily.
     434                 :             :      */
     435                 :         126 :     disable_startup_progress_timeout();
     436                 :         126 : }
     437                 :             : 
     438                 :             : /*
     439                 :             :  * Prepare the system for WAL recovery, if needed.
     440                 :             :  *
     441                 :             :  * This is called by StartupXLOG() which coordinates the server startup
     442                 :             :  * sequence.  This function analyzes the control file and the backup label
     443                 :             :  * file, if any, and figures out whether we need to perform crash recovery or
     444                 :             :  * archive recovery, and how far we need to replay the WAL to reach a
     445                 :             :  * consistent state.
     446                 :             :  *
     447                 :             :  * This doesn't yet change the on-disk state, except for creating the symlinks
     448                 :             :  * from table space map file if any, and for fetching WAL files needed to find
     449                 :             :  * the checkpoint record.  On entry, the caller has already read the control
     450                 :             :  * file into memory, and passes it as argument.  This function updates it to
     451                 :             :  * reflect the recovery state, and the caller is expected to write it back to
     452                 :             :  * disk after initializing other subsystems, but before calling
     453                 :             :  * PerformWalRecovery().
     454                 :             :  *
     455                 :             :  * This initializes some global variables like ArchiveRecoveryRequested, and
     456                 :             :  * StandbyModeRequested and InRecovery.
     457                 :             :  */
     458                 :             : void
     459                 :        1106 : InitWalRecovery(ControlFileData *ControlFile, bool *wasShutdown_ptr,
     460                 :             :                 bool *haveBackupLabel_ptr, bool *haveTblspcMap_ptr)
     461                 :             : {
     462                 :             :     XLogPageReadPrivate *private;
     463                 :             :     struct stat st;
     464                 :             :     bool        wasShutdown;
     465                 :             :     XLogRecord *record;
     466                 :             :     DBState     dbstate_at_startup;
     467                 :        1106 :     bool        haveTblspcMap = false;
     468                 :        1106 :     bool        haveBackupLabel = false;
     469                 :             :     CheckPoint  checkPoint;
     470                 :        1106 :     bool        backupFromStandby = false;
     471                 :             : 
     472                 :        1106 :     dbstate_at_startup = ControlFile->state;
     473                 :             : 
     474                 :             :     /*
     475                 :             :      * A startup process always starts with an inconsistent database. Set the
     476                 :             :      * flag accordingly, even if it was inherited from a postmaster that had
     477                 :             :      * already marked the database as consistent. This keeps the invariant
     478                 :             :      * local to the startup process without requiring every fork path to clear
     479                 :             :      * the flag.
     480                 :             :      */
     481                 :        1106 :     reachedConsistency = false;
     482                 :             : 
     483                 :             :     /*
     484                 :             :      * Initialize on the assumption we want to recover to the latest timeline
     485                 :             :      * that's active according to pg_control.
     486                 :             :      */
     487                 :        1106 :     if (ControlFile->minRecoveryPointTLI >
     488         [ +  + ]:        1106 :         ControlFile->checkPointCopy.ThisTimeLineID)
     489                 :           2 :         recoveryTargetTLI = ControlFile->minRecoveryPointTLI;
     490                 :             :     else
     491                 :        1104 :         recoveryTargetTLI = ControlFile->checkPointCopy.ThisTimeLineID;
     492                 :             : 
     493                 :             :     /*
     494                 :             :      * Check for signal files, and if so set up state for offline recovery
     495                 :             :      */
     496                 :        1106 :     readRecoverySignalFile();
     497                 :        1106 :     validateRecoveryParameters();
     498                 :             : 
     499                 :             :     /*
     500                 :             :      * Take ownership of the wakeup latch if we're going to sleep during
     501                 :             :      * recovery, if required.
     502                 :             :      */
     503         [ +  + ]:        1104 :     if (ArchiveRecoveryRequested)
     504                 :         131 :         OwnLatch(&XLogRecoveryCtl->recoveryWakeupLatch);
     505                 :             : 
     506                 :             :     /*
     507                 :             :      * Set the WAL reading processor now, as it will be needed when reading
     508                 :             :      * the checkpoint record required (backup_label or not).
     509                 :             :      */
     510                 :        1104 :     private = palloc0_object(XLogPageReadPrivate);
     511                 :        1104 :     xlogreader =
     512                 :        1104 :         XLogReaderAllocate(wal_segment_size, NULL,
     513                 :        1104 :                            XL_ROUTINE(.page_read = &XLogPageRead,
     514                 :             :                                       .segment_open = NULL,
     515                 :             :                                       .segment_close = wal_segment_close),
     516                 :             :                            private);
     517         [ -  + ]:        1104 :     if (!xlogreader)
     518         [ #  # ]:           0 :         ereport(ERROR,
     519                 :             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
     520                 :             :                  errmsg("out of memory"),
     521                 :             :                  errdetail("Failed while allocating a WAL reading processor.")));
     522                 :        1104 :     xlogreader->system_identifier = ControlFile->system_identifier;
     523                 :             : 
     524                 :             :     /*
     525                 :             :      * Set the WAL decode buffer size.  This limits how far ahead we can read
     526                 :             :      * in the WAL.
     527                 :             :      */
     528                 :        1104 :     XLogReaderSetDecodeBuffer(xlogreader, NULL, wal_decode_buffer_size);
     529                 :             : 
     530                 :             :     /* Create a WAL prefetcher. */
     531                 :        1104 :     xlogprefetcher = XLogPrefetcherAllocate(xlogreader);
     532                 :             : 
     533                 :             :     /*
     534                 :             :      * Allocate two page buffers dedicated to WAL consistency checks.  We do
     535                 :             :      * it this way, rather than just making static arrays, for two reasons:
     536                 :             :      * (1) no need to waste the storage in most instantiations of the backend;
     537                 :             :      * (2) a static char array isn't guaranteed to have any particular
     538                 :             :      * alignment, whereas palloc() will provide MAXALIGN'd storage.
     539                 :             :      */
     540                 :        1104 :     replay_image_masked = (char *) palloc(BLCKSZ);
     541                 :        1104 :     primary_image_masked = (char *) palloc(BLCKSZ);
     542                 :             : 
     543                 :             :     /*
     544                 :             :      * Read the backup_label file.  We want to run this part of the recovery
     545                 :             :      * process after checking for signal files and after performing validation
     546                 :             :      * of the recovery parameters.
     547                 :             :      */
     548         [ +  + ]:        1104 :     if (read_backup_label(&CheckPointLoc, &CheckPointTLI, &backupEndRequired,
     549                 :             :                           &backupFromStandby))
     550                 :             :     {
     551                 :          96 :         List       *tablespaces = NIL;
     552                 :             : 
     553                 :             :         /*
     554                 :             :          * Archive recovery was requested, and thanks to the backup label
     555                 :             :          * file, we know how far we need to replay to reach consistency. Enter
     556                 :             :          * archive recovery directly.
     557                 :             :          */
     558                 :          96 :         InArchiveRecovery = true;
     559         [ +  + ]:          96 :         if (StandbyModeRequested)
     560                 :          81 :             EnableStandbyMode();
     561                 :             : 
     562                 :             :         /*
     563                 :             :          * Omitting backup_label when creating a new replica, PITR node etc.
     564                 :             :          * unfortunately is a common cause of corruption.  Logging that
     565                 :             :          * backup_label was used makes it a bit easier to exclude that as the
     566                 :             :          * cause of observed corruption.
     567                 :             :          *
     568                 :             :          * Do so before we try to read the checkpoint record (which can fail),
     569                 :             :          * as otherwise it can be hard to understand why a checkpoint other
     570                 :             :          * than ControlFile->checkPoint is used.
     571                 :             :          */
     572         [ +  - ]:          96 :         ereport(LOG,
     573                 :             :                 errmsg("starting backup recovery with redo LSN %X/%08X, checkpoint LSN %X/%08X, on timeline ID %u",
     574                 :             :                        LSN_FORMAT_ARGS(RedoStartLSN),
     575                 :             :                        LSN_FORMAT_ARGS(CheckPointLoc),
     576                 :             :                        CheckPointTLI));
     577                 :             : 
     578                 :             :         /*
     579                 :             :          * When a backup_label file is present, we want to roll forward from
     580                 :             :          * the checkpoint it identifies, rather than using pg_control.
     581                 :             :          */
     582                 :          96 :         record = ReadCheckpointRecord(xlogprefetcher, CheckPointLoc,
     583                 :             :                                       CheckPointTLI);
     584         [ +  + ]:          96 :         if (record != NULL)
     585                 :             :         {
     586                 :          95 :             memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
     587                 :          95 :             wasShutdown = ((record->xl_info & ~XLR_INFO_MASK) == XLOG_CHECKPOINT_SHUTDOWN);
     588         [ +  + ]:          95 :             ereport(DEBUG1,
     589                 :             :                     errmsg_internal("checkpoint record is at %X/%08X",
     590                 :             :                                     LSN_FORMAT_ARGS(CheckPointLoc)));
     591                 :          95 :             InRecovery = true;  /* force recovery even if SHUTDOWNED */
     592                 :             : 
     593                 :             :             /*
     594                 :             :              * Make sure that REDO location exists. This may not be the case
     595                 :             :              * if there was a crash during an online backup, which left a
     596                 :             :              * backup_label around that references a WAL segment that's
     597                 :             :              * already been archived.
     598                 :             :              */
     599         [ +  - ]:          95 :             if (checkPoint.redo < CheckPointLoc)
     600                 :             :             {
     601                 :          95 :                 XLogPrefetcherBeginRead(xlogprefetcher, checkPoint.redo);
     602         [ +  + ]:          95 :                 if (!ReadRecord(xlogprefetcher, LOG, false,
     603                 :             :                                 checkPoint.ThisTimeLineID))
     604         [ +  - ]:           1 :                     ereport(FATAL,
     605                 :             :                             errmsg("could not find redo location %X/%08X referenced by checkpoint record at %X/%08X",
     606                 :             :                                    LSN_FORMAT_ARGS(checkPoint.redo), LSN_FORMAT_ARGS(CheckPointLoc)),
     607                 :             :                             errhint("If you are restoring from a backup, touch \"%s/recovery.signal\" or \"%s/standby.signal\" and add required recovery options.\n"
     608                 :             :                                     "If you are not restoring from a backup, try removing the file \"%s/backup_label\".\n"
     609                 :             :                                     "Be careful: removing \"%s/backup_label\" will result in a corrupt cluster if restoring from a backup.",
     610                 :             :                                     DataDir, DataDir, DataDir, DataDir));
     611                 :             :             }
     612                 :             :         }
     613                 :             :         else
     614                 :             :         {
     615         [ +  - ]:           1 :             ereport(FATAL,
     616                 :             :                     errmsg("could not locate required checkpoint record at %X/%08X",
     617                 :             :                            LSN_FORMAT_ARGS(CheckPointLoc)),
     618                 :             :                     errhint("If you are restoring from a backup, touch \"%s/recovery.signal\" or \"%s/standby.signal\" and add required recovery options.\n"
     619                 :             :                             "If you are not restoring from a backup, try removing the file \"%s/backup_label\".\n"
     620                 :             :                             "Be careful: removing \"%s/backup_label\" will result in a corrupt cluster if restoring from a backup.",
     621                 :             :                             DataDir, DataDir, DataDir, DataDir));
     622                 :             :             wasShutdown = false;    /* keep compiler quiet */
     623                 :             :         }
     624                 :             : 
     625                 :             :         /* Read the tablespace_map file if present and create symlinks. */
     626         [ +  + ]:          94 :         if (read_tablespace_map(&tablespaces))
     627                 :             :         {
     628                 :             :             ListCell   *lc;
     629                 :             : 
     630   [ +  -  +  +  :           4 :             foreach(lc, tablespaces)
                   +  + ]
     631                 :             :             {
     632                 :           2 :                 tablespaceinfo *ti = lfirst(lc);
     633                 :             :                 char       *linkloc;
     634                 :             : 
     635                 :           2 :                 linkloc = psprintf("%s/%u", PG_TBLSPC_DIR, ti->oid);
     636                 :             : 
     637                 :             :                 /*
     638                 :             :                  * Remove the existing symlink if any and Create the symlink
     639                 :             :                  * under PGDATA.
     640                 :             :                  */
     641                 :           2 :                 remove_tablespace_symlink(linkloc);
     642                 :             : 
     643         [ -  + ]:           2 :                 if (symlink(ti->path, linkloc) < 0)
     644         [ #  # ]:           0 :                     ereport(ERROR,
     645                 :             :                             (errcode_for_file_access(),
     646                 :             :                              errmsg("could not create symbolic link \"%s\": %m",
     647                 :             :                                     linkloc)));
     648                 :             : 
     649                 :           2 :                 pfree(ti->path);
     650                 :           2 :                 pfree(ti);
     651                 :             :             }
     652                 :             : 
     653                 :             :             /* tell the caller to delete it later */
     654                 :           2 :             haveTblspcMap = true;
     655                 :             :         }
     656                 :             : 
     657                 :             :         /* tell the caller to delete it later */
     658                 :          94 :         haveBackupLabel = true;
     659                 :             :     }
     660                 :             :     else
     661                 :             :     {
     662                 :             :         /* No backup_label file has been found if we are here. */
     663                 :             : 
     664                 :             :         /*
     665                 :             :          * If tablespace_map file is present without backup_label file, there
     666                 :             :          * is no use of such file.  There is no harm in retaining it, but it
     667                 :             :          * is better to get rid of the map file so that we don't have any
     668                 :             :          * redundant file in data directory and it will avoid any sort of
     669                 :             :          * confusion.  It seems prudent though to just rename the file out of
     670                 :             :          * the way rather than delete it completely, also we ignore any error
     671                 :             :          * that occurs in rename operation as even if map file is present
     672                 :             :          * without backup_label file, it is harmless.
     673                 :             :          */
     674         [ +  + ]:        1008 :         if (stat(TABLESPACE_MAP, &st) == 0)
     675                 :             :         {
     676                 :           1 :             unlink(TABLESPACE_MAP_OLD);
     677         [ +  - ]:           1 :             if (durable_rename(TABLESPACE_MAP, TABLESPACE_MAP_OLD, DEBUG1) == 0)
     678         [ +  - ]:           1 :                 ereport(LOG,
     679                 :             :                         (errmsg("ignoring file \"%s\" because no file \"%s\" exists",
     680                 :             :                                 TABLESPACE_MAP, BACKUP_LABEL_FILE),
     681                 :             :                          errdetail("File \"%s\" was renamed to \"%s\".",
     682                 :             :                                    TABLESPACE_MAP, TABLESPACE_MAP_OLD)));
     683                 :             :             else
     684         [ #  # ]:           0 :                 ereport(LOG,
     685                 :             :                         (errmsg("ignoring file \"%s\" because no file \"%s\" exists",
     686                 :             :                                 TABLESPACE_MAP, BACKUP_LABEL_FILE),
     687                 :             :                          errdetail("Could not rename file \"%s\" to \"%s\": %m.",
     688                 :             :                                    TABLESPACE_MAP, TABLESPACE_MAP_OLD)));
     689                 :             :         }
     690                 :             : 
     691                 :             :         /*
     692                 :             :          * It's possible that archive recovery was requested, but we don't
     693                 :             :          * know how far we need to replay the WAL before we reach consistency.
     694                 :             :          * This can happen for example if a base backup is taken from a
     695                 :             :          * running server using an atomic filesystem snapshot, without calling
     696                 :             :          * pg_backup_start/stop. Or if you just kill a running primary server
     697                 :             :          * and put it into archive recovery by creating a recovery signal
     698                 :             :          * file.
     699                 :             :          *
     700                 :             :          * Our strategy in that case is to perform crash recovery first,
     701                 :             :          * replaying all the WAL present in pg_wal, and only enter archive
     702                 :             :          * recovery after that.
     703                 :             :          *
     704                 :             :          * But usually we already know how far we need to replay the WAL (up
     705                 :             :          * to minRecoveryPoint, up to backupEndPoint, or until we see an
     706                 :             :          * end-of-backup record), and we can enter archive recovery directly.
     707                 :             :          */
     708         [ +  + ]:        1008 :         if (ArchiveRecoveryRequested &&
     709         [ +  + ]:          45 :             (XLogRecPtrIsValid(ControlFile->minRecoveryPoint) ||
     710         [ +  - ]:           9 :              ControlFile->backupEndRequired ||
     711         [ +  - ]:           9 :              XLogRecPtrIsValid(ControlFile->backupEndPoint) ||
     712         [ +  + ]:           9 :              ControlFile->state == DB_SHUTDOWNED))
     713                 :             :         {
     714                 :          44 :             InArchiveRecovery = true;
     715         [ +  - ]:          44 :             if (StandbyModeRequested)
     716                 :          44 :                 EnableStandbyMode();
     717                 :             :         }
     718                 :             : 
     719                 :             :         /*
     720                 :             :          * For the same reason as when starting up with backup_label present,
     721                 :             :          * emit a log message when we continue initializing from a base
     722                 :             :          * backup.
     723                 :             :          */
     724         [ -  + ]:        1008 :         if (XLogRecPtrIsValid(ControlFile->backupStartPoint))
     725         [ #  # ]:           0 :             ereport(LOG,
     726                 :             :                     errmsg("restarting backup recovery with redo LSN %X/%08X",
     727                 :             :                            LSN_FORMAT_ARGS(ControlFile->backupStartPoint)));
     728                 :             : 
     729                 :             :         /* Get the last valid checkpoint record. */
     730                 :        1008 :         CheckPointLoc = ControlFile->checkPoint;
     731                 :        1008 :         CheckPointTLI = ControlFile->checkPointCopy.ThisTimeLineID;
     732                 :        1008 :         RedoStartLSN = ControlFile->checkPointCopy.redo;
     733                 :        1008 :         RedoStartTLI = ControlFile->checkPointCopy.ThisTimeLineID;
     734                 :        1008 :         record = ReadCheckpointRecord(xlogprefetcher, CheckPointLoc,
     735                 :             :                                       CheckPointTLI);
     736         [ +  + ]:        1008 :         if (record != NULL)
     737                 :             :         {
     738         [ +  + ]:        1007 :             ereport(DEBUG1,
     739                 :             :                     errmsg_internal("checkpoint record is at %X/%08X",
     740                 :             :                                     LSN_FORMAT_ARGS(CheckPointLoc)));
     741                 :             :         }
     742                 :             :         else
     743                 :             :         {
     744                 :             :             /*
     745                 :             :              * We used to attempt to go back to a secondary checkpoint record
     746                 :             :              * here, but only when not in standby mode. We now just fail if we
     747                 :             :              * can't read the last checkpoint because this allows us to
     748                 :             :              * simplify processing around checkpoints.
     749                 :             :              */
     750         [ +  - ]:           1 :             ereport(FATAL,
     751                 :             :                     errmsg("could not locate a valid checkpoint record at %X/%08X",
     752                 :             :                            LSN_FORMAT_ARGS(CheckPointLoc)));
     753                 :             :         }
     754                 :        1007 :         memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
     755                 :        1007 :         wasShutdown = ((record->xl_info & ~XLR_INFO_MASK) == XLOG_CHECKPOINT_SHUTDOWN);
     756                 :             : 
     757                 :             :         /* Make sure that REDO location exists. */
     758         [ +  + ]:        1007 :         if (checkPoint.redo < CheckPointLoc)
     759                 :             :         {
     760                 :          45 :             XLogPrefetcherBeginRead(xlogprefetcher, checkPoint.redo);
     761         [ +  + ]:          45 :             if (!ReadRecord(xlogprefetcher, LOG, false, checkPoint.ThisTimeLineID))
     762         [ +  - ]:           1 :                 ereport(FATAL,
     763                 :             :                         errmsg("could not find redo location %X/%08X referenced by checkpoint record at %X/%08X",
     764                 :             :                                LSN_FORMAT_ARGS(checkPoint.redo), LSN_FORMAT_ARGS(CheckPointLoc)));
     765                 :             :         }
     766                 :             :     }
     767                 :             : 
     768         [ +  + ]:        1100 :     if (ArchiveRecoveryRequested)
     769                 :             :     {
     770         [ +  + ]:         131 :         if (StandbyModeRequested)
     771         [ +  - ]:         126 :             ereport(LOG,
     772                 :             :                     (errmsg("entering standby mode")));
     773         [ -  + ]:           5 :         else if (recoveryTarget == RECOVERY_TARGET_XID)
     774         [ #  # ]:           0 :             ereport(LOG,
     775                 :             :                     (errmsg("starting point-in-time recovery to XID %u",
     776                 :             :                             recoveryTargetXid)));
     777         [ -  + ]:           5 :         else if (recoveryTarget == RECOVERY_TARGET_TIME)
     778         [ #  # ]:           0 :             ereport(LOG,
     779                 :             :                     (errmsg("starting point-in-time recovery to %s",
     780                 :             :                             timestamptz_to_str(recoveryTargetTime))));
     781         [ +  + ]:           5 :         else if (recoveryTarget == RECOVERY_TARGET_NAME)
     782         [ +  - ]:           3 :             ereport(LOG,
     783                 :             :                     (errmsg("starting point-in-time recovery to \"%s\"",
     784                 :             :                             recoveryTargetName)));
     785         [ -  + ]:           2 :         else if (recoveryTarget == RECOVERY_TARGET_LSN)
     786         [ #  # ]:           0 :             ereport(LOG,
     787                 :             :                     errmsg("starting point-in-time recovery to WAL location (LSN) \"%X/%08X\"",
     788                 :             :                            LSN_FORMAT_ARGS(recoveryTargetLSN)));
     789         [ -  + ]:           2 :         else if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE)
     790         [ #  # ]:           0 :             ereport(LOG,
     791                 :             :                     (errmsg("starting point-in-time recovery to earliest consistent point")));
     792                 :             :         else
     793         [ +  - ]:           2 :             ereport(LOG,
     794                 :             :                     (errmsg("starting archive recovery")));
     795                 :             :     }
     796                 :             : 
     797                 :             :     /*
     798                 :             :      * If the location of the checkpoint record is not on the expected
     799                 :             :      * timeline in the history of the requested timeline, we cannot proceed:
     800                 :             :      * the backup is not part of the history of the requested timeline.
     801                 :             :      */
     802                 :             :     Assert(expectedTLEs);       /* was initialized by reading checkpoint
     803                 :             :                                  * record */
     804         [ -  + ]:        1100 :     if (tliOfPointInHistory(CheckPointLoc, expectedTLEs) !=
     805                 :             :         CheckPointTLI)
     806                 :             :     {
     807                 :             :         XLogRecPtr  switchpoint;
     808                 :             : 
     809                 :             :         /*
     810                 :             :          * tliSwitchPoint will throw an error if the checkpoint's timeline is
     811                 :             :          * not in expectedTLEs at all.
     812                 :             :          */
     813                 :           0 :         switchpoint = tliSwitchPoint(CheckPointTLI, expectedTLEs, NULL);
     814   [ #  #  #  # ]:           0 :         ereport(FATAL,
     815                 :             :                 (errmsg("requested timeline %u is not a child of this server's history",
     816                 :             :                         recoveryTargetTLI),
     817                 :             :         /* translator: %s is a backup_label file or a pg_control file */
     818                 :             :                  errdetail("Latest checkpoint in file \"%s\" is at %X/%08X on timeline %u, but in the history of the requested timeline, the server forked off from that timeline at %X/%08X.",
     819                 :             :                            haveBackupLabel ? "backup_label" : "pg_control",
     820                 :             :                            LSN_FORMAT_ARGS(CheckPointLoc),
     821                 :             :                            CheckPointTLI,
     822                 :             :                            LSN_FORMAT_ARGS(switchpoint))));
     823                 :             :     }
     824                 :             : 
     825                 :             :     /*
     826                 :             :      * The min recovery point should be part of the requested timeline's
     827                 :             :      * history, too.
     828                 :             :      */
     829         [ +  + ]:        1100 :     if (XLogRecPtrIsValid(ControlFile->minRecoveryPoint) &&
     830                 :          47 :         tliOfPointInHistory(ControlFile->minRecoveryPoint - 1, expectedTLEs) !=
     831         [ -  + ]:          47 :         ControlFile->minRecoveryPointTLI)
     832         [ #  # ]:           0 :         ereport(FATAL,
     833                 :             :                 errmsg("requested timeline %u does not contain minimum recovery point %X/%08X on timeline %u",
     834                 :             :                        recoveryTargetTLI,
     835                 :             :                        LSN_FORMAT_ARGS(ControlFile->minRecoveryPoint),
     836                 :             :                        ControlFile->minRecoveryPointTLI));
     837                 :             : 
     838   [ +  +  +  + ]:        1100 :     ereport(DEBUG1,
     839                 :             :             errmsg_internal("redo record is at %X/%08X; shutdown %s",
     840                 :             :                             LSN_FORMAT_ARGS(checkPoint.redo),
     841                 :             :                             wasShutdown ? "true" : "false"));
     842         [ +  + ]:        1100 :     ereport(DEBUG1,
     843                 :             :             (errmsg_internal("next transaction ID: " UINT64_FORMAT "; next OID: %u",
     844                 :             :                              U64FromFullTransactionId(checkPoint.nextXid),
     845                 :             :                              checkPoint.nextOid)));
     846         [ +  + ]:        1100 :     ereport(DEBUG1,
     847                 :             :             (errmsg_internal("next MultiXactId: %u; next MultiXactOffset: %" PRIu64,
     848                 :             :                              checkPoint.nextMulti, checkPoint.nextMultiOffset)));
     849         [ +  + ]:        1100 :     ereport(DEBUG1,
     850                 :             :             (errmsg_internal("oldest unfrozen transaction ID: %u, in database %u",
     851                 :             :                              checkPoint.oldestXid, checkPoint.oldestXidDB)));
     852         [ +  + ]:        1100 :     ereport(DEBUG1,
     853                 :             :             (errmsg_internal("oldest MultiXactId: %u, in database %u",
     854                 :             :                              checkPoint.oldestMulti, checkPoint.oldestMultiDB)));
     855         [ +  + ]:        1100 :     ereport(DEBUG1,
     856                 :             :             (errmsg_internal("commit timestamp Xid oldest/newest: %u/%u",
     857                 :             :                              checkPoint.oldestCommitTsXid,
     858                 :             :                              checkPoint.newestCommitTsXid)));
     859         [ -  + ]:        1100 :     if (!TransactionIdIsNormal(XidFromFullTransactionId(checkPoint.nextXid)))
     860         [ #  # ]:           0 :         ereport(PANIC,
     861                 :             :                 (errmsg("invalid next transaction ID")));
     862                 :             : 
     863                 :             :     /* sanity check */
     864         [ -  + ]:        1100 :     if (checkPoint.redo > CheckPointLoc)
     865         [ #  # ]:           0 :         ereport(PANIC,
     866                 :             :                 (errmsg("invalid redo in checkpoint record")));
     867                 :             : 
     868                 :             :     /*
     869                 :             :      * Check whether we need to force recovery from WAL.  If it appears to
     870                 :             :      * have been a clean shutdown and we did not have a recovery signal file,
     871                 :             :      * then assume no recovery needed.
     872                 :             :      */
     873         [ +  + ]:        1100 :     if (checkPoint.redo < CheckPointLoc)
     874                 :             :     {
     875         [ -  + ]:         138 :         if (wasShutdown)
     876         [ #  # ]:           0 :             ereport(PANIC,
     877                 :             :                     (errmsg("invalid redo record in shutdown checkpoint")));
     878                 :         138 :         InRecovery = true;
     879                 :             :     }
     880         [ +  + ]:         962 :     else if (ControlFile->state != DB_SHUTDOWNED)
     881                 :          95 :         InRecovery = true;
     882         [ +  + ]:         867 :     else if (ArchiveRecoveryRequested)
     883                 :             :     {
     884                 :             :         /* force recovery due to presence of recovery signal file */
     885                 :           8 :         InRecovery = true;
     886                 :             :     }
     887                 :             : 
     888                 :             :     /*
     889                 :             :      * If recovery is needed, update our in-memory copy of pg_control to show
     890                 :             :      * that we are recovering and to show the selected checkpoint as the place
     891                 :             :      * we are starting from. We also mark pg_control with any minimum recovery
     892                 :             :      * stop point obtained from a backup history file.
     893                 :             :      *
     894                 :             :      * We don't write the changes to disk yet, though. Only do that after
     895                 :             :      * initializing various subsystems.
     896                 :             :      */
     897         [ +  + ]:        1100 :     if (InRecovery)
     898                 :             :     {
     899         [ +  + ]:         241 :         if (InArchiveRecovery)
     900                 :             :         {
     901                 :         138 :             ControlFile->state = DB_IN_ARCHIVE_RECOVERY;
     902                 :             :         }
     903                 :             :         else
     904                 :             :         {
     905         [ +  - ]:         103 :             ereport(LOG,
     906                 :             :                     (errmsg("database system was not properly shut down; "
     907                 :             :                             "automatic recovery in progress")));
     908         [ +  + ]:         103 :             if (recoveryTargetTLI > ControlFile->checkPointCopy.ThisTimeLineID)
     909         [ +  - ]:           2 :                 ereport(LOG,
     910                 :             :                         (errmsg("crash recovery starts in timeline %u "
     911                 :             :                                 "and has target timeline %u",
     912                 :             :                                 ControlFile->checkPointCopy.ThisTimeLineID,
     913                 :             :                                 recoveryTargetTLI)));
     914                 :         103 :             ControlFile->state = DB_IN_CRASH_RECOVERY;
     915                 :             :         }
     916                 :         241 :         ControlFile->checkPoint = CheckPointLoc;
     917                 :         241 :         ControlFile->checkPointCopy = checkPoint;
     918         [ +  + ]:         241 :         if (InArchiveRecovery)
     919                 :             :         {
     920                 :             :             /* initialize minRecoveryPoint if not set yet */
     921         [ +  + ]:         138 :             if (ControlFile->minRecoveryPoint < checkPoint.redo)
     922                 :             :             {
     923                 :          93 :                 ControlFile->minRecoveryPoint = checkPoint.redo;
     924                 :          93 :                 ControlFile->minRecoveryPointTLI = checkPoint.ThisTimeLineID;
     925                 :             :             }
     926                 :             :         }
     927                 :             : 
     928                 :             :         /*
     929                 :             :          * Set backupStartPoint if we're starting recovery from a base backup.
     930                 :             :          *
     931                 :             :          * Also set backupEndPoint and use minRecoveryPoint as the backup end
     932                 :             :          * location if we're starting recovery from a base backup which was
     933                 :             :          * taken from a standby. In this case, the database system status in
     934                 :             :          * pg_control must indicate that the database was already in recovery.
     935                 :             :          * Usually that will be DB_IN_ARCHIVE_RECOVERY but also can be
     936                 :             :          * DB_SHUTDOWNED_IN_RECOVERY if recovery previously was interrupted
     937                 :             :          * before reaching this point; e.g. because restore_command or
     938                 :             :          * primary_conninfo were faulty.
     939                 :             :          *
     940                 :             :          * Any other state indicates that the backup somehow became corrupted
     941                 :             :          * and we can't sensibly continue with recovery.
     942                 :             :          */
     943         [ +  + ]:         241 :         if (haveBackupLabel)
     944                 :             :         {
     945                 :          94 :             ControlFile->backupStartPoint = checkPoint.redo;
     946                 :          94 :             ControlFile->backupEndRequired = backupEndRequired;
     947                 :             : 
     948         [ +  + ]:          94 :             if (backupFromStandby)
     949                 :             :             {
     950   [ -  +  -  - ]:           9 :                 if (dbstate_at_startup != DB_IN_ARCHIVE_RECOVERY &&
     951                 :             :                     dbstate_at_startup != DB_SHUTDOWNED_IN_RECOVERY)
     952         [ #  # ]:           0 :                     ereport(FATAL,
     953                 :             :                             (errmsg("backup_label contains data inconsistent with control file"),
     954                 :             :                              errhint("This means that the backup is corrupted and you will "
     955                 :             :                                      "have to use another backup for recovery.")));
     956                 :           9 :                 ControlFile->backupEndPoint = ControlFile->minRecoveryPoint;
     957                 :             :             }
     958                 :             :         }
     959                 :             :     }
     960                 :             : 
     961                 :             :     /* remember these, so that we know when we have reached consistency */
     962                 :        1100 :     backupStartPoint = ControlFile->backupStartPoint;
     963                 :        1100 :     backupEndRequired = ControlFile->backupEndRequired;
     964                 :        1100 :     backupEndPoint = ControlFile->backupEndPoint;
     965         [ +  + ]:        1100 :     if (InArchiveRecovery)
     966                 :             :     {
     967                 :         138 :         minRecoveryPoint = ControlFile->minRecoveryPoint;
     968                 :         138 :         minRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
     969                 :             :     }
     970                 :             :     else
     971                 :             :     {
     972                 :         962 :         minRecoveryPoint = InvalidXLogRecPtr;
     973                 :         962 :         minRecoveryPointTLI = 0;
     974                 :             :     }
     975                 :             : 
     976                 :             :     /*
     977                 :             :      * Start recovery assuming that the final record isn't lost.
     978                 :             :      */
     979                 :        1100 :     abortedRecPtr = InvalidXLogRecPtr;
     980                 :        1100 :     missingContrecPtr = InvalidXLogRecPtr;
     981                 :             : 
     982                 :        1100 :     *wasShutdown_ptr = wasShutdown;
     983                 :        1100 :     *haveBackupLabel_ptr = haveBackupLabel;
     984                 :        1100 :     *haveTblspcMap_ptr = haveTblspcMap;
     985                 :        1100 : }
     986                 :             : 
     987                 :             : /*
     988                 :             :  * See if there are any recovery signal files and if so, set state for
     989                 :             :  * recovery.
     990                 :             :  *
     991                 :             :  * See if there is a recovery command file (recovery.conf), and if so
     992                 :             :  * throw an ERROR since as of PG12 we no longer recognize that.
     993                 :             :  */
     994                 :             : static void
     995                 :        1106 : readRecoverySignalFile(void)
     996                 :             : {
     997                 :             :     struct stat stat_buf;
     998                 :             : 
     999         [ +  + ]:        1106 :     if (IsBootstrapProcessingMode())
    1000                 :         975 :         return;
    1001                 :             : 
    1002                 :             :     /*
    1003                 :             :      * Check for old recovery API file: recovery.conf
    1004                 :             :      */
    1005         [ -  + ]:        1049 :     if (stat(RECOVERY_COMMAND_FILE, &stat_buf) == 0)
    1006         [ #  # ]:           0 :         ereport(FATAL,
    1007                 :             :                 (errcode_for_file_access(),
    1008                 :             :                  errmsg("using recovery command file \"%s\" is not supported",
    1009                 :             :                         RECOVERY_COMMAND_FILE)));
    1010                 :             : 
    1011                 :             :     /*
    1012                 :             :      * Remove unused .done file, if present. Ignore if absent.
    1013                 :             :      */
    1014                 :        1049 :     unlink(RECOVERY_COMMAND_DONE);
    1015                 :             : 
    1016                 :             :     /*
    1017                 :             :      * Check for recovery signal files and if found, fsync them since they
    1018                 :             :      * represent server state information.  We don't sweat too much about the
    1019                 :             :      * possibility of fsync failure, however.
    1020                 :             :      */
    1021         [ +  + ]:        1049 :     if (stat(STANDBY_SIGNAL_FILE, &stat_buf) == 0)
    1022                 :             :     {
    1023                 :             :         int         fd;
    1024                 :             : 
    1025                 :         126 :         fd = BasicOpenFilePerm(STANDBY_SIGNAL_FILE, O_RDWR | PG_BINARY,
    1026                 :             :                                S_IRUSR | S_IWUSR);
    1027         [ +  - ]:         126 :         if (fd >= 0)
    1028                 :             :         {
    1029                 :         126 :             (void) pg_fsync(fd);
    1030                 :         126 :             close(fd);
    1031                 :             :         }
    1032                 :         126 :         standby_signal_file_found = true;
    1033                 :             :     }
    1034                 :             : 
    1035         [ +  + ]:        1049 :     if (stat(RECOVERY_SIGNAL_FILE, &stat_buf) == 0)
    1036                 :             :     {
    1037                 :             :         int         fd;
    1038                 :             : 
    1039                 :           8 :         fd = BasicOpenFilePerm(RECOVERY_SIGNAL_FILE, O_RDWR | PG_BINARY,
    1040                 :             :                                S_IRUSR | S_IWUSR);
    1041         [ +  - ]:           8 :         if (fd >= 0)
    1042                 :             :         {
    1043                 :           8 :             (void) pg_fsync(fd);
    1044                 :           8 :             close(fd);
    1045                 :             :         }
    1046                 :           8 :         recovery_signal_file_found = true;
    1047                 :             :     }
    1048                 :             : 
    1049                 :             :     /*
    1050                 :             :      * If both signal files are present, standby signal file takes precedence.
    1051                 :             :      * If neither is present then we won't enter archive recovery.
    1052                 :             :      */
    1053                 :        1049 :     StandbyModeRequested = false;
    1054                 :        1049 :     ArchiveRecoveryRequested = false;
    1055         [ +  + ]:        1049 :     if (standby_signal_file_found)
    1056                 :             :     {
    1057                 :         126 :         StandbyModeRequested = true;
    1058                 :         126 :         ArchiveRecoveryRequested = true;
    1059                 :             :     }
    1060         [ +  + ]:         923 :     else if (recovery_signal_file_found)
    1061                 :             :     {
    1062                 :           5 :         StandbyModeRequested = false;
    1063                 :           5 :         ArchiveRecoveryRequested = true;
    1064                 :             :     }
    1065                 :             :     else
    1066                 :         918 :         return;
    1067                 :             : 
    1068                 :             :     /*
    1069                 :             :      * We don't support standby mode in standalone backends; that requires
    1070                 :             :      * other processes such as the WAL receiver to be alive.
    1071                 :             :      */
    1072   [ +  +  -  + ]:         131 :     if (StandbyModeRequested && !IsUnderPostmaster)
    1073         [ #  # ]:           0 :         ereport(FATAL,
    1074                 :             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1075                 :             :                  errmsg("standby mode is not supported by single-user servers")));
    1076                 :             : }
    1077                 :             : 
    1078                 :             : static void
    1079                 :        1106 : validateRecoveryParameters(void)
    1080                 :             : {
    1081                 :             :     /* Reject conflicting targets even when recovery was not requested */
    1082                 :        1106 :     recoveryTarget = DetermineRecoveryTargetType();
    1083                 :             : 
    1084         [ +  + ]:        1104 :     if (!ArchiveRecoveryRequested)
    1085                 :         973 :         return;
    1086                 :             : 
    1087                 :             :     /*
    1088                 :             :      * Check for compulsory parameters
    1089                 :             :      */
    1090         [ +  + ]:         131 :     if (StandbyModeRequested)
    1091                 :             :     {
    1092   [ +  -  +  + ]:         126 :         if ((PrimaryConnInfo == NULL || strcmp(PrimaryConnInfo, "") == 0) &&
    1093   [ +  -  +  + ]:          12 :             (recoveryRestoreCommand == NULL || strcmp(recoveryRestoreCommand, "") == 0))
    1094         [ +  - ]:           2 :             ereport(WARNING,
    1095                 :             :                     (errmsg("specified neither \"primary_conninfo\" nor \"restore_command\""),
    1096                 :             :                      errhint("The database server will regularly poll the pg_wal subdirectory to check for files placed there.")));
    1097                 :             :     }
    1098                 :             :     else
    1099                 :             :     {
    1100         [ +  - ]:           5 :         if (recoveryRestoreCommand == NULL ||
    1101         [ -  + ]:           5 :             strcmp(recoveryRestoreCommand, "") == 0)
    1102         [ #  # ]:           0 :             ereport(FATAL,
    1103                 :             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1104                 :             :                      errmsg("must specify \"restore_command\" when standby mode is not enabled")));
    1105                 :             :     }
    1106                 :             : 
    1107                 :             :     /*
    1108                 :             :      * Override any inconsistent requests. Note that this is a change of
    1109                 :             :      * behaviour in 9.5; prior to this we simply ignored a request to pause if
    1110                 :             :      * hot_standby = off, which was surprising behaviour.
    1111                 :             :      */
    1112         [ +  + ]:         131 :     if (recoveryTargetAction == RECOVERY_TARGET_ACTION_PAUSE &&
    1113         [ +  + ]:         124 :         !EnableHotStandby)
    1114                 :           3 :         recoveryTargetAction = RECOVERY_TARGET_ACTION_SHUTDOWN;
    1115                 :             : 
    1116                 :             :     /*
    1117                 :             :      * Final parsing of recovery_target_time string; see also
    1118                 :             :      * check_recovery_target_time().
    1119                 :             :      */
    1120         [ -  + ]:         131 :     if (recoveryTarget == RECOVERY_TARGET_TIME)
    1121                 :             :     {
    1122                 :           0 :         recoveryTargetTime = DatumGetTimestampTz(DirectFunctionCall3(timestamptz_in,
    1123                 :             :                                                                      CStringGetDatum(recovery_target_time_string),
    1124                 :             :                                                                      ObjectIdGetDatum(InvalidOid),
    1125                 :             :                                                                      Int32GetDatum(-1)));
    1126                 :             :     }
    1127                 :             : 
    1128                 :             :     /*
    1129                 :             :      * If user specified recovery_target_timeline, validate it or compute the
    1130                 :             :      * "latest" value.  We can't do this until after we've gotten the restore
    1131                 :             :      * command and set InArchiveRecovery, because we need to fetch timeline
    1132                 :             :      * history files from the archive.
    1133                 :             :      */
    1134         [ -  + ]:         131 :     if (recoveryTargetTimeLineGoal == RECOVERY_TARGET_TIMELINE_NUMERIC)
    1135                 :             :     {
    1136                 :           0 :         TimeLineID  rtli = recoveryTargetTLIRequested;
    1137                 :             : 
    1138                 :             :         /* Timeline 1 does not have a history file, all else should */
    1139   [ #  #  #  # ]:           0 :         if (rtli != 1 && !existsTimeLineHistory(rtli))
    1140         [ #  # ]:           0 :             ereport(FATAL,
    1141                 :             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1142                 :             :                      errmsg("recovery target timeline %u does not exist",
    1143                 :             :                             rtli)));
    1144                 :           0 :         recoveryTargetTLI = rtli;
    1145                 :             :     }
    1146         [ +  - ]:         131 :     else if (recoveryTargetTimeLineGoal == RECOVERY_TARGET_TIMELINE_LATEST)
    1147                 :             :     {
    1148                 :             :         /* We start the "latest" search from pg_control's timeline */
    1149                 :         131 :         recoveryTargetTLI = findNewestTimeLine(recoveryTargetTLI);
    1150                 :             :     }
    1151                 :             :     else
    1152                 :             :     {
    1153                 :             :         /*
    1154                 :             :          * else we just use the recoveryTargetTLI as already read from
    1155                 :             :          * ControlFile
    1156                 :             :          */
    1157                 :             :         Assert(recoveryTargetTimeLineGoal == RECOVERY_TARGET_TIMELINE_CONTROLFILE);
    1158                 :             :     }
    1159                 :             : }
    1160                 :             : 
    1161                 :             : /*
    1162                 :             :  * read_backup_label: check to see if a backup_label file is present
    1163                 :             :  *
    1164                 :             :  * If we see a backup_label during recovery, we assume that we are recovering
    1165                 :             :  * from a backup dump file, and we therefore roll forward from the checkpoint
    1166                 :             :  * identified by the label file, NOT what pg_control says.  This avoids the
    1167                 :             :  * problem that pg_control might have been archived one or more checkpoints
    1168                 :             :  * later than the start of the dump, and so if we rely on it as the start
    1169                 :             :  * point, we will fail to restore a consistent database state.
    1170                 :             :  *
    1171                 :             :  * Returns true if a backup_label was found (and fills the checkpoint
    1172                 :             :  * location and TLI into *checkPointLoc and *backupLabelTLI, respectively);
    1173                 :             :  * returns false if not. If this backup_label came from a streamed backup,
    1174                 :             :  * *backupEndRequired is set to true. If this backup_label was created during
    1175                 :             :  * recovery, *backupFromStandby is set to true.
    1176                 :             :  *
    1177                 :             :  * Also sets the global variables RedoStartLSN and RedoStartTLI with the LSN
    1178                 :             :  * and TLI read from the backup file.
    1179                 :             :  */
    1180                 :             : static bool
    1181                 :        1104 : read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
    1182                 :             :                   bool *backupEndRequired, bool *backupFromStandby)
    1183                 :             : {
    1184                 :             :     char        startxlogfilename[MAXFNAMELEN];
    1185                 :             :     TimeLineID  tli_from_walseg,
    1186                 :             :                 tli_from_file;
    1187                 :             :     FILE       *lfp;
    1188                 :             :     char        ch;
    1189                 :             :     char        backuptype[20];
    1190                 :             :     char        backupfrom[20];
    1191                 :             :     char        backuplabel[MAXPGPATH];
    1192                 :             :     char        backuptime[128];
    1193                 :             :     uint32      hi,
    1194                 :             :                 lo;
    1195                 :             : 
    1196                 :             :     /* suppress possible uninitialized-variable warnings */
    1197                 :        1104 :     *checkPointLoc = InvalidXLogRecPtr;
    1198                 :        1104 :     *backupLabelTLI = 0;
    1199                 :        1104 :     *backupEndRequired = false;
    1200                 :        1104 :     *backupFromStandby = false;
    1201                 :             : 
    1202                 :             :     /*
    1203                 :             :      * See if label file is present
    1204                 :             :      */
    1205                 :        1104 :     lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
    1206         [ +  + ]:        1104 :     if (!lfp)
    1207                 :             :     {
    1208         [ -  + ]:        1008 :         if (errno != ENOENT)
    1209         [ #  # ]:           0 :             ereport(FATAL,
    1210                 :             :                     (errcode_for_file_access(),
    1211                 :             :                      errmsg("could not read file \"%s\": %m",
    1212                 :             :                             BACKUP_LABEL_FILE)));
    1213                 :        1008 :         return false;           /* it's not there, all is fine */
    1214                 :             :     }
    1215                 :             : 
    1216                 :             :     /*
    1217                 :             :      * Read and parse the START WAL LOCATION and CHECKPOINT lines (this code
    1218                 :             :      * is pretty crude, but we are not expecting any variability in the file
    1219                 :             :      * format).
    1220                 :             :      */
    1221         [ +  - ]:          96 :     if (fscanf(lfp, "START WAL LOCATION: %X/%08X (file %08X%16s)%c",
    1222         [ -  + ]:          96 :                &hi, &lo, &tli_from_walseg, startxlogfilename, &ch) != 5 || ch != '\n')
    1223         [ #  # ]:           0 :         ereport(FATAL,
    1224                 :             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1225                 :             :                  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
    1226                 :          96 :     RedoStartLSN = ((uint64) hi) << 32 | lo;
    1227                 :          96 :     RedoStartTLI = tli_from_walseg;
    1228         [ +  - ]:          96 :     if (fscanf(lfp, "CHECKPOINT LOCATION: %X/%08X%c",
    1229         [ -  + ]:          96 :                &hi, &lo, &ch) != 3 || ch != '\n')
    1230         [ #  # ]:           0 :         ereport(FATAL,
    1231                 :             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1232                 :             :                  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
    1233                 :          96 :     *checkPointLoc = ((uint64) hi) << 32 | lo;
    1234                 :          96 :     *backupLabelTLI = tli_from_walseg;
    1235                 :             : 
    1236                 :             :     /*
    1237                 :             :      * BACKUP METHOD lets us know if this was a typical backup ("streamed",
    1238                 :             :      * which could mean either pg_basebackup or the pg_backup_start/stop
    1239                 :             :      * method was used) or if this label came from somewhere else (the only
    1240                 :             :      * other option today being from pg_rewind).  If this was a streamed
    1241                 :             :      * backup then we know that we need to play through until we get to the
    1242                 :             :      * end of the WAL which was generated during the backup (at which point we
    1243                 :             :      * will have reached consistency and backupEndRequired will be reset to be
    1244                 :             :      * false).
    1245                 :             :      */
    1246         [ +  - ]:          96 :     if (fscanf(lfp, "BACKUP METHOD: %19s\n", backuptype) == 1)
    1247                 :             :     {
    1248         [ +  + ]:          96 :         if (strcmp(backuptype, "streamed") == 0)
    1249                 :          94 :             *backupEndRequired = true;
    1250                 :             :     }
    1251                 :             : 
    1252                 :             :     /*
    1253                 :             :      * BACKUP FROM lets us know if this was from a primary or a standby.  If
    1254                 :             :      * it was from a standby, we'll double-check that the control file state
    1255                 :             :      * matches that of a standby.
    1256                 :             :      */
    1257         [ +  - ]:          96 :     if (fscanf(lfp, "BACKUP FROM: %19s\n", backupfrom) == 1)
    1258                 :             :     {
    1259         [ +  + ]:          96 :         if (strcmp(backupfrom, "standby") == 0)
    1260                 :           9 :             *backupFromStandby = true;
    1261                 :             :     }
    1262                 :             : 
    1263                 :             :     /*
    1264                 :             :      * Parse START TIME and LABEL. Those are not mandatory fields for recovery
    1265                 :             :      * but checking for their presence is useful for debugging and the next
    1266                 :             :      * sanity checks. Cope also with the fact that the result buffers have a
    1267                 :             :      * pre-allocated size, hence if the backup_label file has been generated
    1268                 :             :      * with strings longer than the maximum assumed here an incorrect parsing
    1269                 :             :      * happens. That's fine as only minor consistency checks are done
    1270                 :             :      * afterwards.
    1271                 :             :      */
    1272         [ +  + ]:          96 :     if (fscanf(lfp, "START TIME: %127[^\n]\n", backuptime) == 1)
    1273         [ +  + ]:          95 :         ereport(DEBUG1,
    1274                 :             :                 (errmsg_internal("backup time %s in file \"%s\"",
    1275                 :             :                                  backuptime, BACKUP_LABEL_FILE)));
    1276                 :             : 
    1277         [ +  + ]:          96 :     if (fscanf(lfp, "LABEL: %1023[^\n]\n", backuplabel) == 1)
    1278         [ +  + ]:          94 :         ereport(DEBUG1,
    1279                 :             :                 (errmsg_internal("backup label %s in file \"%s\"",
    1280                 :             :                                  backuplabel, BACKUP_LABEL_FILE)));
    1281                 :             : 
    1282                 :             :     /*
    1283                 :             :      * START TIMELINE is new as of 11. Its parsing is not mandatory, still use
    1284                 :             :      * it as a sanity check if present.
    1285                 :             :      */
    1286         [ +  + ]:          96 :     if (fscanf(lfp, "START TIMELINE: %u\n", &tli_from_file) == 1)
    1287                 :             :     {
    1288         [ -  + ]:          94 :         if (tli_from_walseg != tli_from_file)
    1289         [ #  # ]:           0 :             ereport(FATAL,
    1290                 :             :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1291                 :             :                      errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE),
    1292                 :             :                      errdetail("Timeline ID parsed is %u, but expected %u.",
    1293                 :             :                                tli_from_file, tli_from_walseg)));
    1294                 :             : 
    1295         [ +  + ]:          94 :         ereport(DEBUG1,
    1296                 :             :                 (errmsg_internal("backup timeline %u in file \"%s\"",
    1297                 :             :                                  tli_from_file, BACKUP_LABEL_FILE)));
    1298                 :             :     }
    1299                 :             : 
    1300         [ -  + ]:          96 :     if (fscanf(lfp, "INCREMENTAL FROM LSN: %X/%08X\n", &hi, &lo) > 0)
    1301         [ #  # ]:           0 :         ereport(FATAL,
    1302                 :             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1303                 :             :                  errmsg("this is an incremental backup, not a data directory"),
    1304                 :             :                  errhint("Use pg_combinebackup to reconstruct a valid data directory.")));
    1305                 :             : 
    1306   [ +  -  -  + ]:          96 :     if (ferror(lfp) || FreeFile(lfp))
    1307         [ #  # ]:           0 :         ereport(FATAL,
    1308                 :             :                 (errcode_for_file_access(),
    1309                 :             :                  errmsg("could not read file \"%s\": %m",
    1310                 :             :                         BACKUP_LABEL_FILE)));
    1311                 :             : 
    1312                 :          96 :     return true;
    1313                 :             : }
    1314                 :             : 
    1315                 :             : /*
    1316                 :             :  * read_tablespace_map: check to see if a tablespace_map file is present
    1317                 :             :  *
    1318                 :             :  * If we see a tablespace_map file during recovery, we assume that we are
    1319                 :             :  * recovering from a backup dump file, and we therefore need to create symlinks
    1320                 :             :  * as per the information present in tablespace_map file.
    1321                 :             :  *
    1322                 :             :  * Returns true if a tablespace_map file was found (and fills *tablespaces
    1323                 :             :  * with a tablespaceinfo struct for each tablespace listed in the file);
    1324                 :             :  * returns false if not.
    1325                 :             :  */
    1326                 :             : static bool
    1327                 :          94 : read_tablespace_map(List **tablespaces)
    1328                 :             : {
    1329                 :             :     tablespaceinfo *ti;
    1330                 :             :     FILE       *lfp;
    1331                 :             :     char        str[MAXPGPATH];
    1332                 :             :     int         ch,
    1333                 :             :                 i,
    1334                 :             :                 n;
    1335                 :             :     bool        was_backslash;
    1336                 :             : 
    1337                 :             :     /*
    1338                 :             :      * See if tablespace_map file is present
    1339                 :             :      */
    1340                 :          94 :     lfp = AllocateFile(TABLESPACE_MAP, "r");
    1341         [ +  + ]:          94 :     if (!lfp)
    1342                 :             :     {
    1343         [ -  + ]:          92 :         if (errno != ENOENT)
    1344         [ #  # ]:           0 :             ereport(FATAL,
    1345                 :             :                     (errcode_for_file_access(),
    1346                 :             :                      errmsg("could not read file \"%s\": %m",
    1347                 :             :                             TABLESPACE_MAP)));
    1348                 :          92 :         return false;           /* it's not there, all is fine */
    1349                 :             :     }
    1350                 :             : 
    1351                 :             :     /*
    1352                 :             :      * Read and parse the link name and path lines from tablespace_map file
    1353                 :             :      * (this code is pretty crude, but we are not expecting any variability in
    1354                 :             :      * the file format).  De-escape any backslashes that were inserted.
    1355                 :             :      */
    1356                 :           2 :     i = 0;
    1357                 :           2 :     was_backslash = false;
    1358         [ +  + ]:          77 :     while ((ch = fgetc(lfp)) != EOF)
    1359                 :             :     {
    1360   [ +  -  +  +  :          75 :         if (!was_backslash && (ch == '\n' || ch == '\r'))
                   -  + ]
    1361                 :           2 :         {
    1362                 :             :             char       *endp;
    1363                 :             : 
    1364         [ -  + ]:           2 :             if (i == 0)
    1365                 :           0 :                 continue;       /* \r immediately followed by \n */
    1366                 :             : 
    1367                 :             :             /*
    1368                 :             :              * The de-escaped line should contain an OID followed by exactly
    1369                 :             :              * one space followed by a path.  The path might start with
    1370                 :             :              * spaces, so don't be too liberal about parsing.
    1371                 :             :              */
    1372                 :           2 :             str[i] = '\0';
    1373                 :           2 :             n = 0;
    1374   [ +  -  +  + ]:          12 :             while (str[n] && str[n] != ' ')
    1375                 :          10 :                 n++;
    1376   [ +  -  -  + ]:           2 :             if (n < 1 || n >= i - 1)
    1377         [ #  # ]:           0 :                 ereport(FATAL,
    1378                 :             :                         (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1379                 :             :                          errmsg("invalid data in file \"%s\"", TABLESPACE_MAP)));
    1380                 :           2 :             str[n++] = '\0';
    1381                 :             : 
    1382                 :           2 :             ti = palloc0_object(tablespaceinfo);
    1383                 :           2 :             errno = 0;
    1384                 :           2 :             ti->oid = strtoul(str, &endp, 10);
    1385   [ +  -  +  -  :           2 :             if (*endp != '\0' || errno == EINVAL || errno == ERANGE)
                   -  + ]
    1386         [ #  # ]:           0 :                 ereport(FATAL,
    1387                 :             :                         (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1388                 :             :                          errmsg("invalid data in file \"%s\"", TABLESPACE_MAP)));
    1389                 :           2 :             ti->path = pstrdup(str + n);
    1390                 :           2 :             *tablespaces = lappend(*tablespaces, ti);
    1391                 :             : 
    1392                 :           2 :             i = 0;
    1393                 :           2 :             continue;
    1394                 :             :         }
    1395   [ +  -  -  + ]:          73 :         else if (!was_backslash && ch == '\\')
    1396                 :           0 :             was_backslash = true;
    1397                 :             :         else
    1398                 :             :         {
    1399         [ +  - ]:          73 :             if (i < sizeof(str) - 1)
    1400                 :          73 :                 str[i++] = ch;
    1401                 :          73 :             was_backslash = false;
    1402                 :             :         }
    1403                 :             :     }
    1404                 :             : 
    1405   [ +  -  -  + ]:           2 :     if (i != 0 || was_backslash)    /* last line not terminated? */
    1406         [ #  # ]:           0 :         ereport(FATAL,
    1407                 :             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1408                 :             :                  errmsg("invalid data in file \"%s\"", TABLESPACE_MAP)));
    1409                 :             : 
    1410   [ +  -  -  + ]:           2 :     if (ferror(lfp) || FreeFile(lfp))
    1411         [ #  # ]:           0 :         ereport(FATAL,
    1412                 :             :                 (errcode_for_file_access(),
    1413                 :             :                  errmsg("could not read file \"%s\": %m",
    1414                 :             :                         TABLESPACE_MAP)));
    1415                 :             : 
    1416                 :           2 :     return true;
    1417                 :             : }
    1418                 :             : 
    1419                 :             : /*
    1420                 :             :  * Finish WAL recovery.
    1421                 :             :  *
    1422                 :             :  * This does not close the 'xlogreader' yet, because in some cases the caller
    1423                 :             :  * still wants to re-read the last checkpoint record by calling
    1424                 :             :  * ReadCheckpointRecord().
    1425                 :             :  *
    1426                 :             :  * Returns the position of the last valid or applied record, after which new
    1427                 :             :  * WAL should be appended, information about why recovery was ended, and some
    1428                 :             :  * other things. See the EndOfWalRecoveryInfo struct for details.
    1429                 :             :  */
    1430                 :             : EndOfWalRecoveryInfo *
    1431                 :        1030 : FinishWalRecovery(void)
    1432                 :             : {
    1433                 :        1030 :     EndOfWalRecoveryInfo *result = palloc_object(EndOfWalRecoveryInfo);
    1434                 :             :     XLogRecPtr  lastRec;
    1435                 :             :     TimeLineID  lastRecTLI;
    1436                 :             :     XLogRecPtr  endOfLog;
    1437                 :             : 
    1438                 :             :     /*
    1439                 :             :      * Kill WAL receiver, if it's still running, before we continue to write
    1440                 :             :      * the startup checkpoint and aborted-contrecord records. It will trump
    1441                 :             :      * over these records and subsequent ones if it's still alive when we
    1442                 :             :      * start writing WAL.
    1443                 :             :      */
    1444                 :        1030 :     XLogShutdownWalRcv();
    1445                 :             : 
    1446                 :             :     /*
    1447                 :             :      * Shutdown the slot sync worker to drop any temporary slots acquired by
    1448                 :             :      * it and to prevent it from keep trying to fetch the failover slots.
    1449                 :             :      *
    1450                 :             :      * We do not update the 'synced' column in 'pg_replication_slots' system
    1451                 :             :      * view from true to false here, as any failed update could leave 'synced'
    1452                 :             :      * column false for some slots. This could cause issues during slot sync
    1453                 :             :      * after restarting the server as a standby. While updating the 'synced'
    1454                 :             :      * column after switching to the new timeline is an option, it does not
    1455                 :             :      * simplify the handling for the 'synced' column. Therefore, we retain the
    1456                 :             :      * 'synced' column as true after promotion as it may provide useful
    1457                 :             :      * information about the slot origin.
    1458                 :             :      */
    1459                 :        1030 :     ShutDownSlotSync();
    1460                 :             : 
    1461                 :             :     /*
    1462                 :             :      * We are now done reading the xlog from stream. Turn off streaming
    1463                 :             :      * recovery to force fetching the files (which would be required at end of
    1464                 :             :      * recovery, e.g., timeline history file) from archive or pg_wal.
    1465                 :             :      *
    1466                 :             :      * Note that standby mode must be turned off after killing WAL receiver,
    1467                 :             :      * i.e., calling XLogShutdownWalRcv().
    1468                 :             :      */
    1469                 :             :     Assert(!WalRcvStreaming());
    1470                 :        1030 :     StandbyMode = false;
    1471                 :             : 
    1472                 :             :     /*
    1473                 :             :      * Determine where to start writing WAL next.
    1474                 :             :      *
    1475                 :             :      * Re-fetch the last valid or last applied record, so we can identify the
    1476                 :             :      * exact endpoint of what we consider the valid portion of WAL.  There may
    1477                 :             :      * be an incomplete continuation record after that, in which case
    1478                 :             :      * 'abortedRecPtr' and 'missingContrecPtr' are set and the caller will
    1479                 :             :      * write a special OVERWRITE_CONTRECORD message to mark that the rest of
    1480                 :             :      * it is intentionally missing.  See CreateOverwriteContrecordRecord().
    1481                 :             :      *
    1482                 :             :      * An important side-effect of this is to load the last page into
    1483                 :             :      * xlogreader. The caller uses it to initialize the WAL for writing.
    1484                 :             :      */
    1485         [ +  + ]:        1030 :     if (!InRecovery)
    1486                 :             :     {
    1487                 :         858 :         lastRec = CheckPointLoc;
    1488                 :         858 :         lastRecTLI = CheckPointTLI;
    1489                 :             :     }
    1490                 :             :     else
    1491                 :             :     {
    1492                 :         172 :         lastRec = XLogRecoveryCtl->lastReplayedReadRecPtr;
    1493                 :         172 :         lastRecTLI = XLogRecoveryCtl->lastReplayedTLI;
    1494                 :             :     }
    1495                 :        1030 :     XLogPrefetcherBeginRead(xlogprefetcher, lastRec);
    1496                 :        1030 :     (void) ReadRecord(xlogprefetcher, PANIC, false, lastRecTLI);
    1497                 :        1030 :     endOfLog = xlogreader->EndRecPtr;
    1498                 :             : 
    1499                 :             :     /*
    1500                 :             :      * Remember the TLI in the filename of the XLOG segment containing the
    1501                 :             :      * end-of-log.  It could be different from the timeline that endOfLog
    1502                 :             :      * nominally belongs to, if there was a timeline switch in that segment,
    1503                 :             :      * and we were reading the old WAL from a segment belonging to a higher
    1504                 :             :      * timeline.
    1505                 :             :      */
    1506                 :        1030 :     result->endOfLogTLI = xlogreader->seg.ws_tli;
    1507                 :             : 
    1508         [ +  + ]:        1030 :     if (ArchiveRecoveryRequested)
    1509                 :             :     {
    1510                 :             :         /*
    1511                 :             :          * We are no longer in archive recovery state.
    1512                 :             :          *
    1513                 :             :          * We are now done reading the old WAL.  Turn off archive fetching if
    1514                 :             :          * it was active.
    1515                 :             :          */
    1516                 :             :         Assert(InArchiveRecovery);
    1517                 :          62 :         InArchiveRecovery = false;
    1518                 :             : 
    1519                 :             :         /*
    1520                 :             :          * If the ending log segment is still open, close it (to avoid
    1521                 :             :          * problems on Windows with trying to rename or delete an open file).
    1522                 :             :          */
    1523         [ +  - ]:          62 :         if (readFile >= 0)
    1524                 :             :         {
    1525                 :          62 :             close(readFile);
    1526                 :          62 :             readFile = -1;
    1527                 :             :         }
    1528                 :             :     }
    1529                 :             : 
    1530                 :             :     /*
    1531                 :             :      * Copy the last partial block to the caller, for initializing the WAL
    1532                 :             :      * buffer for appending new WAL.
    1533                 :             :      */
    1534         [ +  + ]:        1030 :     if (endOfLog % XLOG_BLCKSZ != 0)
    1535                 :             :     {
    1536                 :             :         char       *page;
    1537                 :             :         int         len;
    1538                 :             :         XLogRecPtr  pageBeginPtr;
    1539                 :             : 
    1540                 :        1012 :         pageBeginPtr = endOfLog - (endOfLog % XLOG_BLCKSZ);
    1541                 :             :         Assert(readOff == XLogSegmentOffset(pageBeginPtr, wal_segment_size));
    1542                 :             : 
    1543                 :             :         /* Copy the valid part of the last block */
    1544                 :        1012 :         len = endOfLog % XLOG_BLCKSZ;
    1545                 :        1012 :         page = palloc(len);
    1546                 :        1012 :         memcpy(page, xlogreader->readBuf, len);
    1547                 :             : 
    1548                 :        1012 :         result->lastPageBeginPtr = pageBeginPtr;
    1549                 :        1012 :         result->lastPage = page;
    1550                 :             :     }
    1551                 :             :     else
    1552                 :             :     {
    1553                 :             :         /* There is no partial block to copy. */
    1554                 :          18 :         result->lastPageBeginPtr = endOfLog;
    1555                 :          18 :         result->lastPage = NULL;
    1556                 :             :     }
    1557                 :             : 
    1558                 :             :     /*
    1559                 :             :      * Create a comment for the history file to explain why and where timeline
    1560                 :             :      * changed.
    1561                 :             :      */
    1562                 :        1030 :     result->recoveryStopReason = getRecoveryStopReason();
    1563                 :             : 
    1564                 :        1030 :     result->lastRec = lastRec;
    1565                 :        1030 :     result->lastRecTLI = lastRecTLI;
    1566                 :        1030 :     result->endOfLog = endOfLog;
    1567                 :             : 
    1568                 :        1030 :     result->abortedRecPtr = abortedRecPtr;
    1569                 :        1030 :     result->missingContrecPtr = missingContrecPtr;
    1570                 :             : 
    1571                 :        1030 :     result->standby_signal_file_found = standby_signal_file_found;
    1572                 :        1030 :     result->recovery_signal_file_found = recovery_signal_file_found;
    1573                 :             : 
    1574                 :        1030 :     return result;
    1575                 :             : }
    1576                 :             : 
    1577                 :             : /*
    1578                 :             :  * Clean up the WAL reader and leftovers from restoring WAL from archive
    1579                 :             :  */
    1580                 :             : void
    1581                 :        1030 : ShutdownWalRecovery(void)
    1582                 :             : {
    1583                 :             :     char        recoveryPath[MAXPGPATH];
    1584                 :             : 
    1585                 :             :     /* Final update of pg_stat_recovery_prefetch. */
    1586                 :        1030 :     XLogPrefetcherComputeStats(xlogprefetcher);
    1587                 :             : 
    1588                 :             :     /* Shut down xlogreader */
    1589         [ +  + ]:        1030 :     if (readFile >= 0)
    1590                 :             :     {
    1591                 :         968 :         close(readFile);
    1592                 :         968 :         readFile = -1;
    1593                 :             :     }
    1594                 :        1030 :     pfree(xlogreader->private_data);
    1595                 :        1030 :     XLogReaderFree(xlogreader);
    1596                 :        1030 :     XLogPrefetcherFree(xlogprefetcher);
    1597                 :             : 
    1598         [ +  + ]:        1030 :     if (ArchiveRecoveryRequested)
    1599                 :             :     {
    1600                 :             :         /*
    1601                 :             :          * Since there might be a partial WAL segment named RECOVERYXLOG, get
    1602                 :             :          * rid of it.
    1603                 :             :          */
    1604                 :          62 :         snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYXLOG");
    1605                 :          62 :         unlink(recoveryPath);   /* ignore any error */
    1606                 :             : 
    1607                 :             :         /* Get rid of any remaining recovered timeline-history file, too */
    1608                 :          62 :         snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYHISTORY");
    1609                 :          62 :         unlink(recoveryPath);   /* ignore any error */
    1610                 :             :     }
    1611                 :             : 
    1612                 :             :     /*
    1613                 :             :      * We don't need the latch anymore. It's not strictly necessary to disown
    1614                 :             :      * it, but let's do it for the sake of tidiness.
    1615                 :             :      */
    1616         [ +  + ]:        1030 :     if (ArchiveRecoveryRequested)
    1617                 :          62 :         DisownLatch(&XLogRecoveryCtl->recoveryWakeupLatch);
    1618                 :        1030 : }
    1619                 :             : 
    1620                 :             : /*
    1621                 :             :  * Perform WAL recovery.
    1622                 :             :  *
    1623                 :             :  * If the system was shut down cleanly, this is never called.
    1624                 :             :  */
    1625                 :             : void
    1626                 :         240 : PerformWalRecovery(void)
    1627                 :             : {
    1628                 :             :     XLogRecord *record;
    1629                 :         240 :     bool        reachedRecoveryTarget = false;
    1630                 :             :     TimeLineID  replayTLI;
    1631                 :             : 
    1632                 :             :     /*
    1633                 :             :      * Initialize shared variables for tracking progress of WAL replay, as if
    1634                 :             :      * we had just replayed the record before the REDO location (or the
    1635                 :             :      * checkpoint record itself, if it's a shutdown checkpoint).
    1636                 :             :      */
    1637                 :         240 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    1638         [ +  + ]:         240 :     if (RedoStartLSN < CheckPointLoc)
    1639                 :             :     {
    1640                 :         137 :         XLogRecoveryCtl->lastReplayedReadRecPtr = InvalidXLogRecPtr;
    1641                 :         137 :         XLogRecoveryCtl->lastReplayedEndRecPtr = RedoStartLSN;
    1642                 :         137 :         XLogRecoveryCtl->lastReplayedTLI = RedoStartTLI;
    1643                 :             :     }
    1644                 :             :     else
    1645                 :             :     {
    1646                 :         103 :         XLogRecoveryCtl->lastReplayedReadRecPtr = xlogreader->ReadRecPtr;
    1647                 :         103 :         XLogRecoveryCtl->lastReplayedEndRecPtr = xlogreader->EndRecPtr;
    1648                 :         103 :         XLogRecoveryCtl->lastReplayedTLI = CheckPointTLI;
    1649                 :             :     }
    1650                 :         240 :     XLogRecoveryCtl->replayEndRecPtr = XLogRecoveryCtl->lastReplayedEndRecPtr;
    1651                 :         240 :     XLogRecoveryCtl->replayEndTLI = XLogRecoveryCtl->lastReplayedTLI;
    1652                 :         240 :     XLogRecoveryCtl->recoveryLastXTime = 0;
    1653                 :         240 :     XLogRecoveryCtl->currentChunkStartTime = 0;
    1654                 :         240 :     XLogRecoveryCtl->recoveryPauseState = RECOVERY_NOT_PAUSED;
    1655                 :         240 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    1656                 :             : 
    1657                 :             :     /* Also ensure XLogReceiptTime has a sane value */
    1658                 :         240 :     XLogReceiptTime = GetCurrentTimestamp();
    1659                 :             : 
    1660                 :             :     /*
    1661                 :             :      * Let postmaster know we've started redo now, so that it can launch the
    1662                 :             :      * archiver if necessary.
    1663                 :             :      */
    1664         [ +  + ]:         240 :     if (IsUnderPostmaster)
    1665                 :         231 :         SendPostmasterSignal(PMSIGNAL_RECOVERY_STARTED);
    1666                 :             : 
    1667                 :             :     /*
    1668                 :             :      * Allow read-only connections immediately if we're consistent already.
    1669                 :             :      */
    1670                 :         240 :     CheckRecoveryConsistency();
    1671                 :             : 
    1672                 :             :     /*
    1673                 :             :      * Find the first record that logically follows the checkpoint --- it
    1674                 :             :      * might physically precede it, though.
    1675                 :             :      */
    1676         [ +  + ]:         240 :     if (RedoStartLSN < CheckPointLoc)
    1677                 :             :     {
    1678                 :             :         /* back up to find the record */
    1679                 :         137 :         replayTLI = RedoStartTLI;
    1680                 :         137 :         XLogPrefetcherBeginRead(xlogprefetcher, RedoStartLSN);
    1681                 :         137 :         record = ReadRecord(xlogprefetcher, PANIC, false, replayTLI);
    1682                 :             : 
    1683                 :             :         /*
    1684                 :             :          * If a checkpoint record's redo pointer points back to an earlier
    1685                 :             :          * LSN, the record at that LSN should be an XLOG_CHECKPOINT_REDO
    1686                 :             :          * record.
    1687                 :             :          */
    1688         [ +  - ]:         137 :         if (record->xl_rmid != RM_XLOG_ID ||
    1689         [ -  + ]:         137 :             (record->xl_info & ~XLR_INFO_MASK) != XLOG_CHECKPOINT_REDO)
    1690         [ #  # ]:           0 :             ereport(FATAL,
    1691                 :             :                     errmsg("unexpected record type found at redo point %X/%08X",
    1692                 :             :                            LSN_FORMAT_ARGS(xlogreader->ReadRecPtr)));
    1693                 :             :     }
    1694                 :             :     else
    1695                 :             :     {
    1696                 :             :         /* just have to read next record after CheckPoint */
    1697                 :             :         Assert(xlogreader->ReadRecPtr == CheckPointLoc);
    1698                 :         103 :         replayTLI = CheckPointTLI;
    1699                 :         103 :         record = ReadRecord(xlogprefetcher, LOG, false, replayTLI);
    1700                 :             :     }
    1701                 :             : 
    1702         [ +  + ]:         240 :     if (record != NULL)
    1703                 :             :     {
    1704                 :             :         TimestampTz xtime;
    1705                 :             :         PGRUsage    ru0;
    1706                 :             : 
    1707                 :         231 :         pg_rusage_init(&ru0);
    1708                 :             : 
    1709                 :         231 :         InRedo = true;
    1710                 :             : 
    1711                 :         231 :         RmgrStartup();
    1712                 :             : 
    1713         [ +  - ]:         231 :         ereport(LOG,
    1714                 :             :                 errmsg("redo starts at %X/%08X",
    1715                 :             :                        LSN_FORMAT_ARGS(xlogreader->ReadRecPtr)));
    1716                 :             : 
    1717                 :             :         /* Prepare to report progress of the redo phase. */
    1718         [ +  + ]:         231 :         if (!StandbyMode)
    1719                 :         110 :             begin_startup_progress_phase();
    1720                 :             : 
    1721                 :             :         /*
    1722                 :             :          * main redo apply loop
    1723                 :             :          */
    1724                 :             :         do
    1725                 :             :         {
    1726         [ +  + ]:     2972378 :             if (!StandbyMode)
    1727   [ -  +  -  - ]:      327121 :                 ereport_startup_progress("redo in progress, elapsed time: %ld.%02d s, current LSN: %X/%08X",
    1728                 :             :                                          LSN_FORMAT_ARGS(xlogreader->ReadRecPtr));
    1729                 :             : 
    1730                 :             : #ifdef WAL_DEBUG
    1731                 :             :             if (XLOG_DEBUG)
    1732                 :             :             {
    1733                 :             :                 StringInfoData buf;
    1734                 :             : 
    1735                 :             :                 initStringInfo(&buf);
    1736                 :             :                 appendStringInfo(&buf, "REDO @ %X/%08X; LSN %X/%08X: ",
    1737                 :             :                                  LSN_FORMAT_ARGS(xlogreader->ReadRecPtr),
    1738                 :             :                                  LSN_FORMAT_ARGS(xlogreader->EndRecPtr));
    1739                 :             :                 xlog_outrec(&buf, xlogreader);
    1740                 :             :                 appendStringInfoString(&buf, " - ");
    1741                 :             :                 xlog_outdesc(&buf, xlogreader);
    1742                 :             :                 elog(LOG, "%s", buf.data);
    1743                 :             :                 pfree(buf.data);
    1744                 :             :             }
    1745                 :             : #endif
    1746                 :             : 
    1747                 :             :             /* Handle interrupt signals of startup process */
    1748                 :     2972378 :             ProcessStartupProcInterrupts();
    1749                 :             : 
    1750                 :             :             /*
    1751                 :             :              * Pause WAL replay, if requested by a hot-standby session via
    1752                 :             :              * SetRecoveryPause().
    1753                 :             :              *
    1754                 :             :              * Note that we intentionally don't take the info_lck spinlock
    1755                 :             :              * here.  We might therefore read a slightly stale value of the
    1756                 :             :              * recoveryPause flag, but it can't be very stale (no worse than
    1757                 :             :              * the last spinlock we did acquire).  Since a pause request is a
    1758                 :             :              * pretty asynchronous thing anyway, possibly responding to it one
    1759                 :             :              * WAL record later than we otherwise would is a minor issue, so
    1760                 :             :              * it doesn't seem worth adding another spinlock cycle to prevent
    1761                 :             :              * that.
    1762                 :             :              */
    1763         [ -  + ]:     2972378 :             if (((volatile XLogRecoveryCtlData *) XLogRecoveryCtl)->recoveryPauseState !=
    1764                 :             :                 RECOVERY_NOT_PAUSED)
    1765                 :           0 :                 recoveryPausesHere(false);
    1766                 :             : 
    1767                 :             :             /*
    1768                 :             :              * Have we reached our recovery target?
    1769                 :             :              */
    1770         [ +  + ]:     2972378 :             if (recoveryStopsBefore(xlogreader))
    1771                 :             :             {
    1772                 :           2 :                 reachedRecoveryTarget = true;
    1773                 :           2 :                 break;
    1774                 :             :             }
    1775                 :             : 
    1776                 :             :             /*
    1777                 :             :              * If we've been asked to lag the primary, wait on latch until
    1778                 :             :              * enough time has passed.
    1779                 :             :              */
    1780         [ +  + ]:     2972376 :             if (recoveryApplyDelay(xlogreader))
    1781                 :             :             {
    1782                 :             :                 /*
    1783                 :             :                  * We test for paused recovery again here. If user sets
    1784                 :             :                  * delayed apply, it may be because they expect to pause
    1785                 :             :                  * recovery in case of problems, so we must test again here
    1786                 :             :                  * otherwise pausing during the delay-wait wouldn't work.
    1787                 :             :                  */
    1788         [ -  + ]:          25 :                 if (((volatile XLogRecoveryCtlData *) XLogRecoveryCtl)->recoveryPauseState !=
    1789                 :             :                     RECOVERY_NOT_PAUSED)
    1790                 :           0 :                     recoveryPausesHere(false);
    1791                 :             :             }
    1792                 :             : 
    1793                 :             :             /*
    1794                 :             :              * Apply the record
    1795                 :             :              */
    1796                 :     2972376 :             ApplyWalRecord(xlogreader, record, &replayTLI);
    1797                 :             : 
    1798                 :             :             /*
    1799                 :             :              * Wake up processes waiting for standby replay, write, or flush
    1800                 :             :              * LSN to reach current replay position.  Replay implies that the
    1801                 :             :              * WAL was already written and flushed to disk, so write and flush
    1802                 :             :              * waiters can be woken at the replay position too.
    1803                 :             :              */
    1804                 :     2972374 :             WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY,
    1805                 :     2972374 :                           XLogRecoveryCtl->lastReplayedEndRecPtr);
    1806                 :     2972374 :             WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE,
    1807                 :     2972374 :                           XLogRecoveryCtl->lastReplayedEndRecPtr);
    1808                 :     2972374 :             WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH,
    1809                 :     2972374 :                           XLogRecoveryCtl->lastReplayedEndRecPtr);
    1810                 :             : 
    1811                 :             :             /* Exit loop if we reached inclusive recovery target */
    1812         [ +  + ]:     2972374 :             if (recoveryStopsAfter(xlogreader))
    1813                 :             :             {
    1814                 :           5 :                 reachedRecoveryTarget = true;
    1815                 :           5 :                 break;
    1816                 :             :             }
    1817                 :             : 
    1818                 :             :             /* Else, try to fetch the next WAL record */
    1819                 :     2972369 :             record = ReadRecord(xlogprefetcher, LOG, false, replayTLI);
    1820         [ +  + ]:     2972304 :         } while (record != NULL);
    1821                 :             : 
    1822                 :             :         /*
    1823                 :             :          * end of main redo apply loop
    1824                 :             :          */
    1825                 :             : 
    1826         [ +  + ]:         164 :         if (reachedRecoveryTarget)
    1827                 :             :         {
    1828         [ -  + ]:           7 :             if (!reachedConsistency)
    1829         [ #  # ]:           0 :                 ereport(FATAL,
    1830                 :             :                         (errmsg("requested recovery stop point is before consistent recovery point")));
    1831                 :             : 
    1832                 :             :             /*
    1833                 :             :              * This is the last point where we can restart recovery with a new
    1834                 :             :              * recovery target, if we shutdown and begin again. After this,
    1835                 :             :              * Resource Managers may choose to do permanent corrective actions
    1836                 :             :              * at end of recovery.
    1837                 :             :              */
    1838   [ -  +  +  - ]:           7 :             switch (recoveryTargetAction)
    1839                 :             :             {
    1840                 :           0 :                 case RECOVERY_TARGET_ACTION_SHUTDOWN:
    1841                 :             : 
    1842                 :             :                     /*
    1843                 :             :                      * exit with special return code to request shutdown of
    1844                 :             :                      * postmaster.  Log messages issued from postmaster.
    1845                 :             :                      */
    1846                 :           0 :                     proc_exit(3);
    1847                 :             : 
    1848                 :           1 :                 case RECOVERY_TARGET_ACTION_PAUSE:
    1849                 :           1 :                     SetRecoveryPause(true);
    1850                 :           1 :                     recoveryPausesHere(true);
    1851                 :             : 
    1852                 :             :                     /* drop into promote */
    1853                 :             :                     pg_fallthrough;
    1854                 :             : 
    1855                 :           7 :                 case RECOVERY_TARGET_ACTION_PROMOTE:
    1856                 :           7 :                     break;
    1857                 :             :             }
    1858                 :             :         }
    1859                 :             : 
    1860                 :         164 :         RmgrCleanup();
    1861                 :             : 
    1862         [ +  - ]:         164 :         ereport(LOG,
    1863                 :             :                 errmsg("redo done at %X/%08X system usage: %s",
    1864                 :             :                        LSN_FORMAT_ARGS(xlogreader->ReadRecPtr),
    1865                 :             :                        pg_rusage_show(&ru0)));
    1866                 :         164 :         xtime = GetLatestXTime();
    1867         [ +  + ]:         164 :         if (xtime)
    1868         [ +  - ]:          45 :             ereport(LOG,
    1869                 :             :                     (errmsg("last completed transaction was at log time %s",
    1870                 :             :                             timestamptz_to_str(xtime))));
    1871                 :             : 
    1872                 :         164 :         InRedo = false;
    1873                 :             :     }
    1874                 :             :     else
    1875                 :             :     {
    1876                 :             :         /* there are no WAL records following the checkpoint */
    1877         [ +  - ]:           9 :         ereport(LOG,
    1878                 :             :                 (errmsg("redo is not required")));
    1879                 :             :     }
    1880                 :             : 
    1881                 :             :     /*
    1882                 :             :      * This check is intentionally after the above log messages that indicate
    1883                 :             :      * how far recovery went.
    1884                 :             :      */
    1885         [ +  + ]:         173 :     if (ArchiveRecoveryRequested &&
    1886         [ +  + ]:          63 :         recoveryTarget != RECOVERY_TARGET_UNSET &&
    1887         [ +  + ]:           8 :         !reachedRecoveryTarget)
    1888         [ +  - ]:           1 :         ereport(FATAL,
    1889                 :             :                 (errcode(ERRCODE_CONFIG_FILE_ERROR),
    1890                 :             :                  errmsg("recovery ended before configured recovery target was reached")));
    1891                 :         172 : }
    1892                 :             : 
    1893                 :             : /*
    1894                 :             :  * Subroutine of PerformWalRecovery, to apply one WAL record.
    1895                 :             :  */
    1896                 :             : static void
    1897                 :     2972376 : ApplyWalRecord(XLogReaderState *xlogreader, XLogRecord *record, TimeLineID *replayTLI)
    1898                 :             : {
    1899                 :             :     ErrorContextCallback errcallback;
    1900                 :     2972376 :     bool        switchedTLI = false;
    1901                 :             : 
    1902                 :             :     /* Setup error traceback support for ereport() */
    1903                 :     2972376 :     errcallback.callback = rm_redo_error_callback;
    1904                 :     2972376 :     errcallback.arg = xlogreader;
    1905                 :     2972376 :     errcallback.previous = error_context_stack;
    1906                 :     2972376 :     error_context_stack = &errcallback;
    1907                 :             : 
    1908                 :             :     /*
    1909                 :             :      * TransamVariables->nextXid must be beyond record's xid.
    1910                 :             :      */
    1911                 :     2972376 :     AdvanceNextFullTransactionIdPastXid(record->xl_xid);
    1912                 :             : 
    1913                 :             :     /*
    1914                 :             :      * Before replaying this record, check if this record causes the current
    1915                 :             :      * timeline to change. The record is already considered to be part of the
    1916                 :             :      * new timeline, so we update replayTLI before replaying it. That's
    1917                 :             :      * important so that replayEndTLI, which is recorded as the minimum
    1918                 :             :      * recovery point's TLI if recovery stops after this record, is set
    1919                 :             :      * correctly.
    1920                 :             :      */
    1921         [ +  + ]:     2972376 :     if (record->xl_rmid == RM_XLOG_ID)
    1922                 :             :     {
    1923                 :      117551 :         TimeLineID  newReplayTLI = *replayTLI;
    1924                 :      117551 :         TimeLineID  prevReplayTLI = *replayTLI;
    1925                 :      117551 :         uint8       info = record->xl_info & ~XLR_INFO_MASK;
    1926                 :             : 
    1927         [ +  + ]:      117551 :         if (info == XLOG_CHECKPOINT_SHUTDOWN)
    1928                 :             :         {
    1929                 :             :             CheckPoint  checkPoint;
    1930                 :             : 
    1931                 :          46 :             memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
    1932                 :          46 :             newReplayTLI = checkPoint.ThisTimeLineID;
    1933                 :          46 :             prevReplayTLI = checkPoint.PrevTimeLineID;
    1934                 :             :         }
    1935         [ +  + ]:      117505 :         else if (info == XLOG_END_OF_RECOVERY)
    1936                 :             :         {
    1937                 :             :             xl_end_of_recovery xlrec;
    1938                 :             : 
    1939                 :          14 :             memcpy(&xlrec, XLogRecGetData(xlogreader), sizeof(xl_end_of_recovery));
    1940                 :          14 :             newReplayTLI = xlrec.ThisTimeLineID;
    1941                 :          14 :             prevReplayTLI = xlrec.PrevTimeLineID;
    1942                 :             :         }
    1943                 :             : 
    1944         [ +  + ]:      117551 :         if (newReplayTLI != *replayTLI)
    1945                 :             :         {
    1946                 :             :             /* Check that it's OK to switch to this TLI */
    1947                 :          15 :             checkTimeLineSwitch(xlogreader->EndRecPtr,
    1948                 :             :                                 newReplayTLI, prevReplayTLI, *replayTLI);
    1949                 :             : 
    1950                 :             :             /* Following WAL records should be run with new TLI */
    1951                 :          15 :             *replayTLI = newReplayTLI;
    1952                 :          15 :             switchedTLI = true;
    1953                 :             :         }
    1954                 :             :     }
    1955                 :             : 
    1956                 :             :     /*
    1957                 :             :      * Update shared replayEndRecPtr before replaying this record, so that
    1958                 :             :      * XLogFlush will update minRecoveryPoint correctly.
    1959                 :             :      */
    1960                 :     2972376 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    1961                 :     2972376 :     XLogRecoveryCtl->replayEndRecPtr = xlogreader->EndRecPtr;
    1962                 :     2972376 :     XLogRecoveryCtl->replayEndTLI = *replayTLI;
    1963                 :     2972376 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    1964                 :             : 
    1965                 :             :     /*
    1966                 :             :      * If we are attempting to enter Hot Standby mode, process XIDs we see
    1967                 :             :      */
    1968         [ +  + ]:     2972376 :     if (standbyState >= STANDBY_INITIALIZED &&
    1969         [ +  + ]:     2664717 :         TransactionIdIsValid(record->xl_xid))
    1970                 :     2602532 :         RecordKnownAssignedTransactionIds(record->xl_xid);
    1971                 :             : 
    1972                 :             :     /*
    1973                 :             :      * Some XLOG record types that are related to recovery are processed
    1974                 :             :      * directly here, rather than in xlog_redo()
    1975                 :             :      */
    1976         [ +  + ]:     2972376 :     if (record->xl_rmid == RM_XLOG_ID)
    1977                 :      117551 :         xlogrecovery_redo(xlogreader, *replayTLI);
    1978                 :             : 
    1979                 :             :     /* Now apply the WAL record itself */
    1980                 :     2972376 :     GetRmgr(record->xl_rmid).rm_redo(xlogreader);
    1981                 :             : 
    1982                 :             :     /*
    1983                 :             :      * After redo, check whether the backup pages associated with the WAL
    1984                 :             :      * record are consistent with the existing pages. This check is done only
    1985                 :             :      * if consistency check is enabled for this record.
    1986                 :             :      */
    1987         [ +  + ]:     2972374 :     if ((record->xl_info & XLR_CHECK_CONSISTENCY) != 0)
    1988                 :     2282793 :         verifyBackupPageConsistency(xlogreader);
    1989                 :             : 
    1990                 :             :     /* Pop the error context stack */
    1991                 :     2972374 :     error_context_stack = errcallback.previous;
    1992                 :             : 
    1993                 :             :     /*
    1994                 :             :      * Update lastReplayedEndRecPtr after this record has been successfully
    1995                 :             :      * replayed.
    1996                 :             :      */
    1997                 :     2972374 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    1998                 :     2972374 :     XLogRecoveryCtl->lastReplayedReadRecPtr = xlogreader->ReadRecPtr;
    1999                 :     2972374 :     XLogRecoveryCtl->lastReplayedEndRecPtr = xlogreader->EndRecPtr;
    2000                 :     2972374 :     XLogRecoveryCtl->lastReplayedTLI = *replayTLI;
    2001                 :     2972374 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    2002                 :             : 
    2003                 :             :     /* ------
    2004                 :             :      * Wakeup walsenders:
    2005                 :             :      *
    2006                 :             :      * On the standby, the WAL is flushed first (which will only wake up
    2007                 :             :      * physical walsenders) and then applied, which will only wake up logical
    2008                 :             :      * walsenders.
    2009                 :             :      *
    2010                 :             :      * Indeed, logical walsenders on standby can't decode and send data until
    2011                 :             :      * it's been applied.
    2012                 :             :      *
    2013                 :             :      * Physical walsenders don't need to be woken up during replay unless
    2014                 :             :      * cascading replication is allowed and time line change occurred (so that
    2015                 :             :      * they can notice that they are on a new time line).
    2016                 :             :      *
    2017                 :             :      * That's why the wake up conditions are for:
    2018                 :             :      *
    2019                 :             :      *  - physical walsenders in case of new time line and cascade
    2020                 :             :      *    replication is allowed
    2021                 :             :      *  - logical walsenders in case cascade replication is allowed (could not
    2022                 :             :      *    be created otherwise)
    2023                 :             :      * ------
    2024                 :             :      */
    2025   [ +  +  +  + ]:     2972374 :     if (AllowCascadeReplication())
    2026                 :     2720489 :         WalSndWakeup(switchedTLI, true);
    2027                 :             : 
    2028                 :             :     /*
    2029                 :             :      * If rm_redo called XLogRequestWalReceiverReply, then we wake up the
    2030                 :             :      * receiver so that it notices the updated lastReplayedEndRecPtr and sends
    2031                 :             :      * a reply to the primary.
    2032                 :             :      */
    2033         [ +  + ]:     2972374 :     if (doRequestWalReceiverReply)
    2034                 :             :     {
    2035                 :           2 :         doRequestWalReceiverReply = false;
    2036                 :           2 :         WalRcvRequestApplyReply();
    2037                 :             :     }
    2038                 :             : 
    2039                 :             :     /* Allow read-only connections if we're consistent now */
    2040                 :     2972374 :     CheckRecoveryConsistency();
    2041                 :             : 
    2042                 :             :     /* Is this a timeline switch? */
    2043         [ +  + ]:     2972374 :     if (switchedTLI)
    2044                 :             :     {
    2045                 :             :         /*
    2046                 :             :          * Before we continue on the new timeline, clean up any (possibly
    2047                 :             :          * bogus) future WAL segments on the old timeline.
    2048                 :             :          */
    2049                 :          15 :         RemoveNonParentXlogFiles(xlogreader->EndRecPtr, *replayTLI);
    2050                 :             : 
    2051                 :             :         /* Reset the prefetcher. */
    2052                 :          15 :         XLogPrefetchReconfigure();
    2053                 :             :     }
    2054                 :     2972374 : }
    2055                 :             : 
    2056                 :             : /*
    2057                 :             :  * Some XLOG RM record types that are directly related to WAL recovery are
    2058                 :             :  * handled here rather than in the xlog_redo()
    2059                 :             :  */
    2060                 :             : static void
    2061                 :      117551 : xlogrecovery_redo(XLogReaderState *record, TimeLineID replayTLI)
    2062                 :             : {
    2063                 :      117551 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
    2064                 :      117551 :     XLogRecPtr  lsn = record->EndRecPtr;
    2065                 :             : 
    2066                 :             :     Assert(XLogRecGetRmid(record) == RM_XLOG_ID);
    2067                 :             : 
    2068         [ +  + ]:      117551 :     if (info == XLOG_OVERWRITE_CONTRECORD)
    2069                 :             :     {
    2070                 :             :         /* Verify the payload of a XLOG_OVERWRITE_CONTRECORD record. */
    2071                 :             :         xl_overwrite_contrecord xlrec;
    2072                 :             : 
    2073                 :           1 :         memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_overwrite_contrecord));
    2074         [ -  + ]:           1 :         if (xlrec.overwritten_lsn != record->overwrittenRecPtr)
    2075         [ #  # ]:           0 :             elog(FATAL, "mismatching overwritten LSN %X/%08X -> %X/%08X",
    2076                 :             :                  LSN_FORMAT_ARGS(xlrec.overwritten_lsn),
    2077                 :             :                  LSN_FORMAT_ARGS(record->overwrittenRecPtr));
    2078                 :             : 
    2079                 :             :         /* We have safely skipped the aborted record */
    2080                 :           1 :         abortedRecPtr = InvalidXLogRecPtr;
    2081                 :           1 :         missingContrecPtr = InvalidXLogRecPtr;
    2082                 :             : 
    2083         [ +  - ]:           1 :         ereport(LOG,
    2084                 :             :                 errmsg("successfully skipped missing contrecord at %X/%08X, overwritten at %s",
    2085                 :             :                        LSN_FORMAT_ARGS(xlrec.overwritten_lsn),
    2086                 :             :                        timestamptz_to_str(xlrec.overwrite_time)));
    2087                 :             : 
    2088                 :             :         /* Verifying the record should only happen once */
    2089                 :           1 :         record->overwrittenRecPtr = InvalidXLogRecPtr;
    2090                 :             :     }
    2091         [ +  + ]:      117550 :     else if (info == XLOG_BACKUP_END)
    2092                 :             :     {
    2093                 :             :         XLogRecPtr  startpoint;
    2094                 :             : 
    2095                 :         109 :         memcpy(&startpoint, XLogRecGetData(record), sizeof(startpoint));
    2096                 :             : 
    2097         [ +  + ]:         109 :         if (backupStartPoint == startpoint)
    2098                 :             :         {
    2099                 :             :             /*
    2100                 :             :              * We have reached the end of base backup, the point where
    2101                 :             :              * pg_backup_stop() was done.  The data on disk is now consistent
    2102                 :             :              * (assuming we have also reached minRecoveryPoint).  Set
    2103                 :             :              * backupEndPoint to the current LSN, so that the next call to
    2104                 :             :              * CheckRecoveryConsistency() will notice it and do the
    2105                 :             :              * end-of-backup processing.
    2106                 :             :              */
    2107         [ +  + ]:          91 :             elog(DEBUG1, "end of backup record reached");
    2108                 :             : 
    2109                 :          91 :             backupEndPoint = lsn;
    2110                 :             :         }
    2111                 :             :         else
    2112         [ +  + ]:          18 :             elog(DEBUG1, "saw end-of-backup record for backup starting at %X/%08X, waiting for %X/%08X",
    2113                 :             :                  LSN_FORMAT_ARGS(startpoint), LSN_FORMAT_ARGS(backupStartPoint));
    2114                 :             :     }
    2115                 :      117551 : }
    2116                 :             : 
    2117                 :             : /*
    2118                 :             :  * Verify that, in non-test mode, ./pg_tblspc doesn't contain any real
    2119                 :             :  * directories.
    2120                 :             :  *
    2121                 :             :  * Replay of database creation XLOG records for databases that were later
    2122                 :             :  * dropped can create fake directories in pg_tblspc.  By the time consistency
    2123                 :             :  * is reached these directories should have been removed; here we verify
    2124                 :             :  * that this did indeed happen.  This is to be called at the point where
    2125                 :             :  * consistent state is reached.
    2126                 :             :  *
    2127                 :             :  * allow_in_place_tablespaces turns the PANIC into a WARNING, which is
    2128                 :             :  * useful for testing purposes, and also allows for an escape hatch in case
    2129                 :             :  * things go south.
    2130                 :             :  */
    2131                 :             : static void
    2132                 :         138 : CheckTablespaceDirectory(void)
    2133                 :             : {
    2134                 :             :     DIR        *dir;
    2135                 :             :     struct dirent *de;
    2136                 :             : 
    2137                 :         138 :     dir = AllocateDir(PG_TBLSPC_DIR);
    2138         [ +  + ]:         421 :     while ((de = ReadDir(dir, PG_TBLSPC_DIR)) != NULL)
    2139                 :             :     {
    2140                 :             :         char        path[MAXPGPATH + sizeof(PG_TBLSPC_DIR)];
    2141                 :             : 
    2142                 :             :         /* Skip entries of non-oid names */
    2143         [ +  + ]:         283 :         if (strspn(de->d_name, "0123456789") != strlen(de->d_name))
    2144                 :         276 :             continue;
    2145                 :             : 
    2146                 :           7 :         snprintf(path, sizeof(path), "%s/%s", PG_TBLSPC_DIR, de->d_name);
    2147                 :             : 
    2148         [ +  + ]:           7 :         if (get_dirent_type(path, de, false, ERROR) != PGFILETYPE_LNK)
    2149   [ +  -  +  - ]:           4 :             ereport(allow_in_place_tablespaces ? WARNING : PANIC,
    2150                 :             :                     (errcode(ERRCODE_DATA_CORRUPTED),
    2151                 :             :                      errmsg("unexpected directory entry \"%s\" found in %s",
    2152                 :             :                             de->d_name, PG_TBLSPC_DIR),
    2153                 :             :                      errdetail("All directory entries in %s/ should be symbolic links.",
    2154                 :             :                                PG_TBLSPC_DIR),
    2155                 :             :                      errhint("Remove those directories, or set \"allow_in_place_tablespaces\" to ON transiently to let recovery complete.")));
    2156                 :             :     }
    2157                 :         138 : }
    2158                 :             : 
    2159                 :             : /*
    2160                 :             :  * Checks if recovery has reached a consistent state. When consistency is
    2161                 :             :  * reached and we have a valid starting standby snapshot, tell postmaster
    2162                 :             :  * that it can start accepting read-only connections.
    2163                 :             :  */
    2164                 :             : static void
    2165                 :     2972615 : CheckRecoveryConsistency(void)
    2166                 :             : {
    2167                 :             :     XLogRecPtr  lastReplayedEndRecPtr;
    2168                 :             :     TimeLineID  lastReplayedTLI;
    2169                 :             : 
    2170                 :             :     /*
    2171                 :             :      * During crash recovery, we don't reach a consistent state until we've
    2172                 :             :      * replayed all the WAL.
    2173                 :             :      */
    2174         [ +  + ]:     2972615 :     if (!XLogRecPtrIsValid(minRecoveryPoint))
    2175                 :      322004 :         return;
    2176                 :             : 
    2177                 :             :     Assert(InArchiveRecovery);
    2178                 :             : 
    2179                 :             :     /*
    2180                 :             :      * assume that we are called in the startup process, and hence don't need
    2181                 :             :      * a lock to read lastReplayedEndRecPtr
    2182                 :             :      */
    2183                 :     2650611 :     lastReplayedEndRecPtr = XLogRecoveryCtl->lastReplayedEndRecPtr;
    2184                 :     2650611 :     lastReplayedTLI = XLogRecoveryCtl->lastReplayedTLI;
    2185                 :             : 
    2186                 :             :     /*
    2187                 :             :      * Have we reached the point where our base backup was completed?
    2188                 :             :      */
    2189         [ +  + ]:     2650611 :     if (XLogRecPtrIsValid(backupEndPoint) &&
    2190         [ +  + ]:        1570 :         backupEndPoint <= lastReplayedEndRecPtr)
    2191                 :             :     {
    2192                 :          94 :         XLogRecPtr  saveBackupStartPoint = backupStartPoint;
    2193                 :          94 :         XLogRecPtr  saveBackupEndPoint = backupEndPoint;
    2194                 :             : 
    2195         [ +  + ]:          94 :         elog(DEBUG1, "end of backup reached");
    2196                 :             : 
    2197                 :             :         /*
    2198                 :             :          * We have reached the end of base backup, as indicated by pg_control.
    2199                 :             :          * Update the control file accordingly.
    2200                 :             :          */
    2201                 :          94 :         ReachedEndOfBackup(lastReplayedEndRecPtr, lastReplayedTLI);
    2202                 :          94 :         backupStartPoint = InvalidXLogRecPtr;
    2203                 :          94 :         backupEndPoint = InvalidXLogRecPtr;
    2204                 :          94 :         backupEndRequired = false;
    2205                 :             : 
    2206         [ +  - ]:          94 :         ereport(LOG,
    2207                 :             :                 errmsg("completed backup recovery with redo LSN %X/%08X and end LSN %X/%08X",
    2208                 :             :                        LSN_FORMAT_ARGS(saveBackupStartPoint),
    2209                 :             :                        LSN_FORMAT_ARGS(saveBackupEndPoint)));
    2210                 :             :     }
    2211                 :             : 
    2212                 :             :     /*
    2213                 :             :      * Have we passed our safe starting point? Note that minRecoveryPoint is
    2214                 :             :      * known to be incorrectly set if recovering from a backup, until the
    2215                 :             :      * XLOG_BACKUP_END arrives to advise us of the correct minRecoveryPoint.
    2216                 :             :      * All we know prior to that is that we're not consistent yet.
    2217                 :             :      */
    2218   [ +  +  +  + ]:     2650611 :     if (!reachedConsistency && !backupEndRequired &&
    2219         [ +  + ]:        7995 :         minRecoveryPoint <= lastReplayedEndRecPtr)
    2220                 :             :     {
    2221                 :             :         /*
    2222                 :             :          * Check to see if the XLOG sequence contained any unresolved
    2223                 :             :          * references to uninitialized pages.
    2224                 :             :          */
    2225                 :         138 :         XLogCheckInvalidPages();
    2226                 :             : 
    2227                 :             :         /*
    2228                 :             :          * Check that pg_tblspc doesn't contain any real directories. Replay
    2229                 :             :          * of Database/CREATE_* records may have created fictitious tablespace
    2230                 :             :          * directories that should have been removed by the time consistency
    2231                 :             :          * was reached.
    2232                 :             :          */
    2233                 :         138 :         CheckTablespaceDirectory();
    2234                 :             : 
    2235                 :         138 :         reachedConsistency = true;
    2236                 :         138 :         SendPostmasterSignal(PMSIGNAL_RECOVERY_CONSISTENT);
    2237         [ +  - ]:         138 :         ereport(LOG,
    2238                 :             :                 errmsg("consistent recovery state reached at %X/%08X",
    2239                 :             :                        LSN_FORMAT_ARGS(lastReplayedEndRecPtr)));
    2240                 :             :     }
    2241                 :             : 
    2242                 :             :     /*
    2243                 :             :      * Have we got a valid starting snapshot that will allow queries to be
    2244                 :             :      * run? If so, we can tell postmaster that the database is consistent now,
    2245                 :             :      * enabling connections.
    2246                 :             :      */
    2247         [ +  + ]:     2650611 :     if (standbyState == STANDBY_SNAPSHOT_READY &&
    2248   [ +  +  +  + ]:     2650335 :         !LocalHotStandbyActive &&
    2249         [ +  - ]:         128 :         reachedConsistency &&
    2250                 :             :         IsUnderPostmaster)
    2251                 :             :     {
    2252                 :         128 :         SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    2253                 :         128 :         XLogRecoveryCtl->SharedHotStandbyActive = true;
    2254                 :         128 :         SpinLockRelease(&XLogRecoveryCtl->info_lck);
    2255                 :             : 
    2256                 :         128 :         LocalHotStandbyActive = true;
    2257                 :             : 
    2258                 :         128 :         SendPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY);
    2259                 :             :     }
    2260                 :             : }
    2261                 :             : 
    2262                 :             : /*
    2263                 :             :  * Error context callback for errors occurring during rm_redo().
    2264                 :             :  */
    2265                 :             : static void
    2266                 :         181 : rm_redo_error_callback(void *arg)
    2267                 :             : {
    2268                 :         181 :     XLogReaderState *record = (XLogReaderState *) arg;
    2269                 :             :     StringInfoData buf;
    2270                 :             : 
    2271                 :         181 :     initStringInfo(&buf);
    2272                 :         181 :     xlog_outdesc(&buf, record);
    2273                 :         181 :     xlog_block_info(&buf, record);
    2274                 :             : 
    2275                 :             :     /* translator: %s is a WAL record description */
    2276                 :         181 :     errcontext("WAL redo at %X/%08X for %s",
    2277                 :         181 :                LSN_FORMAT_ARGS(record->ReadRecPtr),
    2278                 :             :                buf.data);
    2279                 :             : 
    2280                 :         181 :     pfree(buf.data);
    2281                 :         181 : }
    2282                 :             : 
    2283                 :             : /*
    2284                 :             :  * Returns a string describing an XLogRecord, consisting of its identity
    2285                 :             :  * optionally followed by a colon, a space, and a further description.
    2286                 :             :  */
    2287                 :             : void
    2288                 :         181 : xlog_outdesc(StringInfo buf, XLogReaderState *record)
    2289                 :             : {
    2290                 :         181 :     RmgrData    rmgr = GetRmgr(XLogRecGetRmid(record));
    2291                 :         181 :     uint8       info = XLogRecGetInfo(record);
    2292                 :             :     const char *id;
    2293                 :             : 
    2294                 :         181 :     appendStringInfoString(buf, rmgr.rm_name);
    2295                 :         181 :     appendStringInfoChar(buf, '/');
    2296                 :             : 
    2297                 :         181 :     id = rmgr.rm_identify(info);
    2298         [ -  + ]:         181 :     if (id == NULL)
    2299                 :           0 :         appendStringInfo(buf, "UNKNOWN (%X): ", info & ~XLR_INFO_MASK);
    2300                 :             :     else
    2301                 :         181 :         appendStringInfo(buf, "%s: ", id);
    2302                 :             : 
    2303                 :         181 :     rmgr.rm_desc(buf, record);
    2304                 :         181 : }
    2305                 :             : 
    2306                 :             : #ifdef WAL_DEBUG
    2307                 :             : 
    2308                 :             : static void
    2309                 :             : xlog_outrec(StringInfo buf, XLogReaderState *record)
    2310                 :             : {
    2311                 :             :     appendStringInfo(buf, "prev %X/%08X; xid %u",
    2312                 :             :                      LSN_FORMAT_ARGS(XLogRecGetPrev(record)),
    2313                 :             :                      XLogRecGetXid(record));
    2314                 :             : 
    2315                 :             :     appendStringInfo(buf, "; len %u",
    2316                 :             :                      XLogRecGetDataLen(record));
    2317                 :             : 
    2318                 :             :     xlog_block_info(buf, record);
    2319                 :             : }
    2320                 :             : #endif                          /* WAL_DEBUG */
    2321                 :             : 
    2322                 :             : /*
    2323                 :             :  * Returns a string giving information about all the blocks in an
    2324                 :             :  * XLogRecord.
    2325                 :             :  */
    2326                 :             : static void
    2327                 :         181 : xlog_block_info(StringInfo buf, XLogReaderState *record)
    2328                 :             : {
    2329                 :             :     int         block_id;
    2330                 :             : 
    2331                 :             :     /* decode block references */
    2332         [ +  + ]:         234 :     for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
    2333                 :             :     {
    2334                 :             :         RelFileLocator rlocator;
    2335                 :             :         ForkNumber  forknum;
    2336                 :             :         BlockNumber blk;
    2337                 :             : 
    2338         [ -  + ]:          53 :         if (!XLogRecGetBlockTagExtended(record, block_id,
    2339                 :             :                                         &rlocator, &forknum, &blk, NULL))
    2340                 :           0 :             continue;
    2341                 :             : 
    2342         [ +  + ]:          53 :         if (forknum != MAIN_FORKNUM)
    2343                 :           7 :             appendStringInfo(buf, "; blkref #%d: rel %u/%u/%u, fork %u, blk %u",
    2344                 :             :                              block_id,
    2345                 :             :                              rlocator.spcOid, rlocator.dbOid,
    2346                 :             :                              rlocator.relNumber,
    2347                 :             :                              forknum,
    2348                 :             :                              blk);
    2349                 :             :         else
    2350                 :          46 :             appendStringInfo(buf, "; blkref #%d: rel %u/%u/%u, blk %u",
    2351                 :             :                              block_id,
    2352                 :             :                              rlocator.spcOid, rlocator.dbOid,
    2353                 :             :                              rlocator.relNumber,
    2354                 :             :                              blk);
    2355         [ +  + ]:          53 :         if (XLogRecHasBlockImage(record, block_id))
    2356                 :          32 :             appendStringInfoString(buf, " FPW");
    2357                 :             :     }
    2358                 :         181 : }
    2359                 :             : 
    2360                 :             : 
    2361                 :             : /*
    2362                 :             :  * Check that it's OK to switch to new timeline during recovery.
    2363                 :             :  *
    2364                 :             :  * 'lsn' is the address of the shutdown checkpoint record we're about to
    2365                 :             :  * replay. (Currently, timeline can only change at a shutdown checkpoint).
    2366                 :             :  */
    2367                 :             : static void
    2368                 :          15 : checkTimeLineSwitch(XLogRecPtr lsn, TimeLineID newTLI, TimeLineID prevTLI,
    2369                 :             :                     TimeLineID replayTLI)
    2370                 :             : {
    2371                 :             :     /* Check that the record agrees on what the current (old) timeline is */
    2372         [ -  + ]:          15 :     if (prevTLI != replayTLI)
    2373         [ #  # ]:           0 :         ereport(PANIC,
    2374                 :             :                 (errmsg("unexpected previous timeline ID %u (current timeline ID %u) in checkpoint record",
    2375                 :             :                         prevTLI, replayTLI)));
    2376                 :             : 
    2377                 :             :     /*
    2378                 :             :      * The new timeline better be in the list of timelines we expect to see,
    2379                 :             :      * according to the timeline history. It should also not decrease.
    2380                 :             :      */
    2381   [ +  -  -  + ]:          15 :     if (newTLI < replayTLI || !tliInHistory(newTLI, expectedTLEs))
    2382         [ #  # ]:           0 :         ereport(PANIC,
    2383                 :             :                 (errmsg("unexpected timeline ID %u (after %u) in checkpoint record",
    2384                 :             :                         newTLI, replayTLI)));
    2385                 :             : 
    2386                 :             :     /*
    2387                 :             :      * If we have not yet reached min recovery point, and we're about to
    2388                 :             :      * switch to a timeline greater than the timeline of the min recovery
    2389                 :             :      * point: trouble. After switching to the new timeline, we could not
    2390                 :             :      * possibly visit the min recovery point on the correct timeline anymore.
    2391                 :             :      * This can happen if there is a newer timeline in the archive that
    2392                 :             :      * branched before the timeline the min recovery point is on, and you
    2393                 :             :      * attempt to do PITR to the new timeline.
    2394                 :             :      */
    2395         [ +  + ]:          15 :     if (XLogRecPtrIsValid(minRecoveryPoint) &&
    2396         [ +  + ]:          13 :         lsn < minRecoveryPoint &&
    2397         [ -  + ]:           1 :         newTLI > minRecoveryPointTLI)
    2398         [ #  # ]:           0 :         ereport(PANIC,
    2399                 :             :                 errmsg("unexpected timeline ID %u in checkpoint record, before reaching minimum recovery point %X/%08X on timeline %u",
    2400                 :             :                        newTLI,
    2401                 :             :                        LSN_FORMAT_ARGS(minRecoveryPoint),
    2402                 :             :                        minRecoveryPointTLI));
    2403                 :             : 
    2404                 :             :     /* Looks good */
    2405                 :          15 : }
    2406                 :             : 
    2407                 :             : 
    2408                 :             : /*
    2409                 :             :  * Extract timestamp from WAL record.
    2410                 :             :  *
    2411                 :             :  * If the record contains a timestamp, returns true, and saves the timestamp
    2412                 :             :  * in *recordXtime. If the record type has no timestamp, returns false.
    2413                 :             :  * Currently, only transaction commit/abort records and restore points contain
    2414                 :             :  * timestamps.
    2415                 :             :  */
    2416                 :             : static bool
    2417                 :       48478 : getRecordTimestamp(XLogReaderState *record, TimestampTz *recordXtime)
    2418                 :             : {
    2419                 :       48478 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
    2420                 :       48478 :     uint8       xact_info = info & XLOG_XACT_OPMASK;
    2421                 :       48478 :     uint8       rmid = XLogRecGetRmid(record);
    2422                 :             : 
    2423   [ +  +  +  - ]:       48478 :     if (rmid == RM_XLOG_ID && info == XLOG_RESTORE_POINT)
    2424                 :             :     {
    2425                 :           2 :         *recordXtime = ((xl_restore_point *) XLogRecGetData(record))->rp_time;
    2426                 :           2 :         return true;
    2427                 :             :     }
    2428   [ +  -  +  +  :       48476 :     if (rmid == RM_XACT_ID && (xact_info == XLOG_XACT_COMMIT ||
                   +  + ]
    2429                 :             :                                xact_info == XLOG_XACT_COMMIT_PREPARED))
    2430                 :             :     {
    2431                 :       44382 :         *recordXtime = ((xl_xact_commit *) XLogRecGetData(record))->xact_time;
    2432                 :       44382 :         return true;
    2433                 :             :     }
    2434   [ +  -  +  +  :        4094 :     if (rmid == RM_XACT_ID && (xact_info == XLOG_XACT_ABORT ||
                   +  - ]
    2435                 :             :                                xact_info == XLOG_XACT_ABORT_PREPARED))
    2436                 :             :     {
    2437                 :        4094 :         *recordXtime = ((xl_xact_abort *) XLogRecGetData(record))->xact_time;
    2438                 :        4094 :         return true;
    2439                 :             :     }
    2440                 :           0 :     return false;
    2441                 :             : }
    2442                 :             : 
    2443                 :             : /*
    2444                 :             :  * Checks whether the current buffer page and backup page stored in the
    2445                 :             :  * WAL record are consistent or not. Before comparing the two pages, a
    2446                 :             :  * masking can be applied to the pages to ignore certain areas like hint bits,
    2447                 :             :  * unused space between pd_lower and pd_upper among other things. This
    2448                 :             :  * function should be called once WAL replay has been completed for a
    2449                 :             :  * given record.
    2450                 :             :  */
    2451                 :             : static void
    2452                 :     2282793 : verifyBackupPageConsistency(XLogReaderState *record)
    2453                 :             : {
    2454                 :     2282793 :     RmgrData    rmgr = GetRmgr(XLogRecGetRmid(record));
    2455                 :             :     RelFileLocator rlocator;
    2456                 :             :     ForkNumber  forknum;
    2457                 :             :     BlockNumber blkno;
    2458                 :             :     int         block_id;
    2459                 :             : 
    2460                 :             :     /* Records with no backup blocks have no need for consistency checks. */
    2461         [ +  + ]:     2282793 :     if (!XLogRecHasAnyBlockRefs(record))
    2462                 :          92 :         return;
    2463                 :             : 
    2464                 :             :     Assert((XLogRecGetInfo(record) & XLR_CHECK_CONSISTENCY) != 0);
    2465                 :             : 
    2466         [ +  + ]:     4740976 :     for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
    2467                 :             :     {
    2468                 :             :         Buffer      buf;
    2469                 :             :         Page        page;
    2470                 :             : 
    2471         [ +  + ]:     2458275 :         if (!XLogRecGetBlockTagExtended(record, block_id,
    2472                 :             :                                         &rlocator, &forknum, &blkno, NULL))
    2473                 :             :         {
    2474                 :             :             /*
    2475                 :             :              * WAL record doesn't contain a block reference with the given id.
    2476                 :             :              * Do nothing.
    2477                 :             :              */
    2478                 :        2646 :             continue;
    2479                 :             :         }
    2480                 :             : 
    2481                 :             :         Assert(XLogRecHasBlockImage(record, block_id));
    2482                 :             : 
    2483         [ +  + ]:     2455629 :         if (XLogRecBlockImageApply(record, block_id))
    2484                 :             :         {
    2485                 :             :             /*
    2486                 :             :              * WAL record has already applied the page, so bypass the
    2487                 :             :              * consistency check as that would result in comparing the full
    2488                 :             :              * page stored in the record with itself.
    2489                 :             :              */
    2490                 :       29650 :             continue;
    2491                 :             :         }
    2492                 :             : 
    2493                 :             :         /*
    2494                 :             :          * Read the contents from the current buffer and store it in a
    2495                 :             :          * temporary page.
    2496                 :             :          */
    2497                 :     2425979 :         buf = XLogReadBufferExtended(rlocator, forknum, blkno,
    2498                 :             :                                      RBM_NORMAL_NO_LOG,
    2499                 :             :                                      InvalidBuffer);
    2500         [ -  + ]:     2425979 :         if (!BufferIsValid(buf))
    2501                 :           0 :             continue;
    2502                 :             : 
    2503                 :     2425979 :         LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
    2504                 :     2425979 :         page = BufferGetPage(buf);
    2505                 :             : 
    2506                 :             :         /*
    2507                 :             :          * Take a copy of the local page where WAL has been applied to have a
    2508                 :             :          * comparison base before masking it...
    2509                 :             :          */
    2510                 :     2425979 :         memcpy(replay_image_masked, page, BLCKSZ);
    2511                 :             : 
    2512                 :             :         /* No need for this page anymore now that a copy is in. */
    2513                 :     2425979 :         UnlockReleaseBuffer(buf);
    2514                 :             : 
    2515                 :             :         /*
    2516                 :             :          * If the block LSN is already ahead of this WAL record, we can't
    2517                 :             :          * expect contents to match.  This can happen if recovery is
    2518                 :             :          * restarted.
    2519                 :             :          */
    2520         [ -  + ]:     2425979 :         if (PageGetLSN(replay_image_masked) > record->EndRecPtr)
    2521                 :           0 :             continue;
    2522                 :             : 
    2523                 :             :         /*
    2524                 :             :          * Read the contents from the backup copy, stored in WAL record and
    2525                 :             :          * store it in a temporary page. There is no need to allocate a new
    2526                 :             :          * page here, a local buffer is fine to hold its contents and a mask
    2527                 :             :          * can be directly applied on it.
    2528                 :             :          */
    2529         [ -  + ]:     2425979 :         if (!RestoreBlockImage(record, block_id, primary_image_masked))
    2530         [ #  # ]:           0 :             ereport(ERROR,
    2531                 :             :                     (errcode(ERRCODE_INTERNAL_ERROR),
    2532                 :             :                      errmsg_internal("%s", record->errormsg_buf)));
    2533                 :             : 
    2534                 :             :         /*
    2535                 :             :          * If masking function is defined, mask both the primary and replay
    2536                 :             :          * images
    2537                 :             :          */
    2538         [ +  - ]:     2425979 :         if (rmgr.rm_mask != NULL)
    2539                 :             :         {
    2540                 :     2425979 :             rmgr.rm_mask(replay_image_masked, blkno);
    2541                 :     2425979 :             rmgr.rm_mask(primary_image_masked, blkno);
    2542                 :             :         }
    2543                 :             : 
    2544                 :             :         /* Time to compare the primary and replay images. */
    2545         [ -  + ]:     2425979 :         if (memcmp(replay_image_masked, primary_image_masked, BLCKSZ) != 0)
    2546                 :             :         {
    2547         [ #  # ]:           0 :             elog(FATAL,
    2548                 :             :                  "inconsistent page found, rel %u/%u/%u, forknum %u, blkno %u",
    2549                 :             :                  rlocator.spcOid, rlocator.dbOid, rlocator.relNumber,
    2550                 :             :                  forknum, blkno);
    2551                 :             :         }
    2552                 :             :     }
    2553                 :             : }
    2554                 :             : 
    2555                 :             : /*
    2556                 :             :  * For point-in-time recovery, this function decides whether we want to
    2557                 :             :  * stop applying the XLOG before the current record.
    2558                 :             :  *
    2559                 :             :  * Returns true if we are stopping, false otherwise. If stopping, some
    2560                 :             :  * information is saved in recoveryStopXid et al for use in annotating the
    2561                 :             :  * new timeline's history file.
    2562                 :             :  */
    2563                 :             : static bool
    2564                 :     2972378 : recoveryStopsBefore(XLogReaderState *record)
    2565                 :             : {
    2566                 :     2972378 :     bool        stopsHere = false;
    2567                 :             :     uint8       xact_info;
    2568                 :             :     bool        isCommit;
    2569                 :     2972378 :     TimestampTz recordXtime = 0;
    2570                 :             :     TransactionId recordXid;
    2571                 :             : 
    2572                 :             :     /*
    2573                 :             :      * Ignore recovery target settings when not in archive recovery (meaning
    2574                 :             :      * we are in crash recovery).
    2575                 :             :      */
    2576         [ +  + ]:     2972378 :     if (!ArchiveRecoveryRequested)
    2577                 :      307645 :         return false;
    2578                 :             : 
    2579                 :             :     /* Check if we should stop as soon as reaching consistency */
    2580   [ -  +  -  - ]:     2664733 :     if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE && reachedConsistency)
    2581                 :             :     {
    2582         [ #  # ]:           0 :         ereport(LOG,
    2583                 :             :                 (errmsg("recovery stopping after reaching consistency")));
    2584                 :             : 
    2585                 :           0 :         recoveryStopAfter = false;
    2586                 :           0 :         recoveryStopXid = InvalidTransactionId;
    2587                 :           0 :         recoveryStopLSN = InvalidXLogRecPtr;
    2588                 :           0 :         recoveryStopTime = 0;
    2589                 :           0 :         recoveryStopName[0] = '\0';
    2590                 :           0 :         return true;
    2591                 :             :     }
    2592                 :             : 
    2593                 :             :     /* Check if target LSN has been reached */
    2594         [ +  + ]:     2664733 :     if (recoveryTarget == RECOVERY_TARGET_LSN &&
    2595         [ +  + ]:        8565 :         !recoveryTargetInclusive &&
    2596         [ +  + ]:         492 :         record->ReadRecPtr >= recoveryTargetLSN)
    2597                 :             :     {
    2598                 :           2 :         recoveryStopAfter = false;
    2599                 :           2 :         recoveryStopXid = InvalidTransactionId;
    2600                 :           2 :         recoveryStopLSN = record->ReadRecPtr;
    2601                 :           2 :         recoveryStopTime = 0;
    2602                 :           2 :         recoveryStopName[0] = '\0';
    2603         [ +  - ]:           2 :         ereport(LOG,
    2604                 :             :                 errmsg("recovery stopping before WAL location (LSN) \"%X/%08X\"",
    2605                 :             :                        LSN_FORMAT_ARGS(recoveryStopLSN)));
    2606                 :           2 :         return true;
    2607                 :             :     }
    2608                 :             : 
    2609                 :             :     /* Otherwise we only consider stopping before COMMIT or ABORT records. */
    2610         [ +  + ]:     2664731 :     if (XLogRecGetRmid(record) != RM_XACT_ID)
    2611                 :     2640177 :         return false;
    2612                 :             : 
    2613                 :       24554 :     xact_info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
    2614                 :             : 
    2615         [ +  + ]:       24554 :     if (xact_info == XLOG_XACT_COMMIT)
    2616                 :             :     {
    2617                 :       22151 :         isCommit = true;
    2618                 :       22151 :         recordXid = XLogRecGetXid(record);
    2619                 :             :     }
    2620         [ +  + ]:        2403 :     else if (xact_info == XLOG_XACT_COMMIT_PREPARED)
    2621                 :             :     {
    2622                 :          26 :         xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
    2623                 :             :         xl_xact_parsed_commit parsed;
    2624                 :             : 
    2625                 :          26 :         isCommit = true;
    2626                 :          26 :         ParseCommitRecord(XLogRecGetInfo(record),
    2627                 :             :                           xlrec,
    2628                 :             :                           &parsed);
    2629                 :          26 :         recordXid = parsed.twophase_xid;
    2630                 :             :     }
    2631         [ +  + ]:        2377 :     else if (xact_info == XLOG_XACT_ABORT)
    2632                 :             :     {
    2633                 :        2032 :         isCommit = false;
    2634                 :        2032 :         recordXid = XLogRecGetXid(record);
    2635                 :             :     }
    2636         [ +  + ]:         345 :     else if (xact_info == XLOG_XACT_ABORT_PREPARED)
    2637                 :             :     {
    2638                 :          15 :         xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
    2639                 :             :         xl_xact_parsed_abort parsed;
    2640                 :             : 
    2641                 :          15 :         isCommit = false;
    2642                 :          15 :         ParseAbortRecord(XLogRecGetInfo(record),
    2643                 :             :                          xlrec,
    2644                 :             :                          &parsed);
    2645                 :          15 :         recordXid = parsed.twophase_xid;
    2646                 :             :     }
    2647                 :             :     else
    2648                 :         330 :         return false;
    2649                 :             : 
    2650   [ -  +  -  - ]:       24224 :     if (recoveryTarget == RECOVERY_TARGET_XID && !recoveryTargetInclusive)
    2651                 :             :     {
    2652                 :             :         /*
    2653                 :             :          * There can be only one transaction end record with this exact
    2654                 :             :          * transactionid
    2655                 :             :          *
    2656                 :             :          * when testing for an xid, we MUST test for equality only, since
    2657                 :             :          * transactions are numbered in the order they start, not the order
    2658                 :             :          * they complete. A higher numbered xid will complete before you about
    2659                 :             :          * 50% of the time...
    2660                 :             :          */
    2661                 :           0 :         stopsHere = (recordXid == recoveryTargetXid);
    2662                 :             :     }
    2663                 :             : 
    2664                 :             :     /*
    2665                 :             :      * Note: we must fetch recordXtime regardless of recoveryTarget setting.
    2666                 :             :      * We don't expect getRecordTimestamp ever to fail, since we already know
    2667                 :             :      * this is a commit or abort record; but test its result anyway.
    2668                 :             :      */
    2669         [ +  - ]:       24224 :     if (getRecordTimestamp(record, &recordXtime) &&
    2670         [ -  + ]:       24224 :         recoveryTarget == RECOVERY_TARGET_TIME)
    2671                 :             :     {
    2672                 :             :         /*
    2673                 :             :          * There can be many transactions that share the same commit time, so
    2674                 :             :          * we stop after the last one, if we are inclusive, or stop at the
    2675                 :             :          * first one if we are exclusive
    2676                 :             :          */
    2677         [ #  # ]:           0 :         if (recoveryTargetInclusive)
    2678                 :           0 :             stopsHere = (recordXtime > recoveryTargetTime);
    2679                 :             :         else
    2680                 :           0 :             stopsHere = (recordXtime >= recoveryTargetTime);
    2681                 :             :     }
    2682                 :             : 
    2683         [ -  + ]:       24224 :     if (stopsHere)
    2684                 :             :     {
    2685                 :           0 :         recoveryStopAfter = false;
    2686                 :           0 :         recoveryStopXid = recordXid;
    2687                 :           0 :         recoveryStopTime = recordXtime;
    2688                 :           0 :         recoveryStopLSN = InvalidXLogRecPtr;
    2689                 :           0 :         recoveryStopName[0] = '\0';
    2690                 :             : 
    2691         [ #  # ]:           0 :         if (isCommit)
    2692                 :             :         {
    2693         [ #  # ]:           0 :             ereport(LOG,
    2694                 :             :                     (errmsg("recovery stopping before commit of transaction %u, time %s",
    2695                 :             :                             recoveryStopXid,
    2696                 :             :                             timestamptz_to_str(recoveryStopTime))));
    2697                 :             :         }
    2698                 :             :         else
    2699                 :             :         {
    2700         [ #  # ]:           0 :             ereport(LOG,
    2701                 :             :                     (errmsg("recovery stopping before abort of transaction %u, time %s",
    2702                 :             :                             recoveryStopXid,
    2703                 :             :                             timestamptz_to_str(recoveryStopTime))));
    2704                 :             :         }
    2705                 :             :     }
    2706                 :             : 
    2707                 :       24224 :     return stopsHere;
    2708                 :             : }
    2709                 :             : 
    2710                 :             : /*
    2711                 :             :  * Same as recoveryStopsBefore, but called after applying the record.
    2712                 :             :  *
    2713                 :             :  * We also track the timestamp of the latest applied COMMIT/ABORT
    2714                 :             :  * record in XLogRecoveryCtl->recoveryLastXTime.
    2715                 :             :  */
    2716                 :             : static bool
    2717                 :     2972374 : recoveryStopsAfter(XLogReaderState *record)
    2718                 :             : {
    2719                 :             :     uint8       info;
    2720                 :             :     uint8       xact_info;
    2721                 :             :     uint8       rmid;
    2722                 :     2972374 :     TimestampTz recordXtime = 0;
    2723                 :             : 
    2724                 :             :     /*
    2725                 :             :      * Ignore recovery target settings when not in archive recovery (meaning
    2726                 :             :      * we are in crash recovery).
    2727                 :             :      */
    2728         [ +  + ]:     2972374 :     if (!ArchiveRecoveryRequested)
    2729                 :      307645 :         return false;
    2730                 :             : 
    2731                 :     2664729 :     info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
    2732                 :     2664729 :     rmid = XLogRecGetRmid(record);
    2733                 :             : 
    2734                 :             :     /*
    2735                 :             :      * There can be many restore points that share the same name; we stop at
    2736                 :             :      * the first one.
    2737                 :             :      */
    2738   [ +  +  +  + ]:     2664729 :     if (recoveryTarget == RECOVERY_TARGET_NAME &&
    2739         [ +  + ]:          20 :         rmid == RM_XLOG_ID && info == XLOG_RESTORE_POINT)
    2740                 :             :     {
    2741                 :             :         xl_restore_point *recordRestorePointData;
    2742                 :             : 
    2743                 :           3 :         recordRestorePointData = (xl_restore_point *) XLogRecGetData(record);
    2744                 :             : 
    2745         [ +  + ]:           3 :         if (strcmp(recordRestorePointData->rp_name, recoveryTargetName) == 0)
    2746                 :             :         {
    2747                 :           2 :             recoveryStopAfter = true;
    2748                 :           2 :             recoveryStopXid = InvalidTransactionId;
    2749                 :           2 :             recoveryStopLSN = InvalidXLogRecPtr;
    2750                 :           2 :             (void) getRecordTimestamp(record, &recoveryStopTime);
    2751                 :           2 :             strlcpy(recoveryStopName, recordRestorePointData->rp_name, MAXFNAMELEN);
    2752                 :             : 
    2753         [ +  - ]:           2 :             ereport(LOG,
    2754                 :             :                     (errmsg("recovery stopping at restore point \"%s\", time %s",
    2755                 :             :                             recoveryStopName,
    2756                 :             :                             timestamptz_to_str(recoveryStopTime))));
    2757                 :           2 :             return true;
    2758                 :             :         }
    2759                 :             :     }
    2760                 :             : 
    2761                 :             :     /* Check if the target LSN has been reached */
    2762   [ +  +  +  + ]:     2664727 :     if (recoveryTarget == RECOVERY_TARGET_LSN &&
    2763                 :        8073 :         recoveryTargetInclusive &&
    2764         [ +  + ]:        8073 :         record->ReadRecPtr >= recoveryTargetLSN)
    2765                 :             :     {
    2766                 :           3 :         recoveryStopAfter = true;
    2767                 :           3 :         recoveryStopXid = InvalidTransactionId;
    2768                 :           3 :         recoveryStopLSN = record->ReadRecPtr;
    2769                 :           3 :         recoveryStopTime = 0;
    2770                 :           3 :         recoveryStopName[0] = '\0';
    2771         [ +  - ]:           3 :         ereport(LOG,
    2772                 :             :                 errmsg("recovery stopping after WAL location (LSN) \"%X/%08X\"",
    2773                 :             :                        LSN_FORMAT_ARGS(recoveryStopLSN)));
    2774                 :           3 :         return true;
    2775                 :             :     }
    2776                 :             : 
    2777         [ +  + ]:     2664724 :     if (rmid != RM_XACT_ID)
    2778                 :     2640172 :         return false;
    2779                 :             : 
    2780                 :       24552 :     xact_info = info & XLOG_XACT_OPMASK;
    2781                 :             : 
    2782   [ +  +  +  + ]:       24552 :     if (xact_info == XLOG_XACT_COMMIT ||
    2783         [ +  + ]:        2377 :         xact_info == XLOG_XACT_COMMIT_PREPARED ||
    2784         [ +  + ]:         345 :         xact_info == XLOG_XACT_ABORT ||
    2785                 :             :         xact_info == XLOG_XACT_ABORT_PREPARED)
    2786                 :             :     {
    2787                 :             :         TransactionId recordXid;
    2788                 :             : 
    2789                 :             :         /* Update the last applied transaction timestamp */
    2790         [ +  - ]:       24222 :         if (getRecordTimestamp(record, &recordXtime))
    2791                 :       24222 :             SetLatestXTime(recordXtime);
    2792                 :             : 
    2793                 :             :         /* Extract the XID of the committed/aborted transaction */
    2794         [ +  + ]:       24222 :         if (xact_info == XLOG_XACT_COMMIT_PREPARED)
    2795                 :             :         {
    2796                 :          26 :             xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
    2797                 :             :             xl_xact_parsed_commit parsed;
    2798                 :             : 
    2799                 :          26 :             ParseCommitRecord(XLogRecGetInfo(record),
    2800                 :             :                               xlrec,
    2801                 :             :                               &parsed);
    2802                 :          26 :             recordXid = parsed.twophase_xid;
    2803                 :             :         }
    2804         [ +  + ]:       24196 :         else if (xact_info == XLOG_XACT_ABORT_PREPARED)
    2805                 :             :         {
    2806                 :          15 :             xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
    2807                 :             :             xl_xact_parsed_abort parsed;
    2808                 :             : 
    2809                 :          15 :             ParseAbortRecord(XLogRecGetInfo(record),
    2810                 :             :                              xlrec,
    2811                 :             :                              &parsed);
    2812                 :          15 :             recordXid = parsed.twophase_xid;
    2813                 :             :         }
    2814                 :             :         else
    2815                 :       24181 :             recordXid = XLogRecGetXid(record);
    2816                 :             : 
    2817                 :             :         /*
    2818                 :             :          * There can be only one transaction end record with this exact
    2819                 :             :          * transactionid
    2820                 :             :          *
    2821                 :             :          * when testing for an xid, we MUST test for equality only, since
    2822                 :             :          * transactions are numbered in the order they start, not the order
    2823                 :             :          * they complete. A higher numbered xid will complete before you about
    2824                 :             :          * 50% of the time...
    2825                 :             :          */
    2826   [ -  +  -  - ]:       24222 :         if (recoveryTarget == RECOVERY_TARGET_XID && recoveryTargetInclusive &&
    2827         [ #  # ]:           0 :             recordXid == recoveryTargetXid)
    2828                 :             :         {
    2829                 :           0 :             recoveryStopAfter = true;
    2830                 :           0 :             recoveryStopXid = recordXid;
    2831                 :           0 :             recoveryStopTime = recordXtime;
    2832                 :           0 :             recoveryStopLSN = InvalidXLogRecPtr;
    2833                 :           0 :             recoveryStopName[0] = '\0';
    2834                 :             : 
    2835   [ #  #  #  # ]:           0 :             if (xact_info == XLOG_XACT_COMMIT ||
    2836                 :             :                 xact_info == XLOG_XACT_COMMIT_PREPARED)
    2837                 :             :             {
    2838         [ #  # ]:           0 :                 ereport(LOG,
    2839                 :             :                         (errmsg("recovery stopping after commit of transaction %u, time %s",
    2840                 :             :                                 recoveryStopXid,
    2841                 :             :                                 timestamptz_to_str(recoveryStopTime))));
    2842                 :             :             }
    2843   [ #  #  #  # ]:           0 :             else if (xact_info == XLOG_XACT_ABORT ||
    2844                 :             :                      xact_info == XLOG_XACT_ABORT_PREPARED)
    2845                 :             :             {
    2846         [ #  # ]:           0 :                 ereport(LOG,
    2847                 :             :                         (errmsg("recovery stopping after abort of transaction %u, time %s",
    2848                 :             :                                 recoveryStopXid,
    2849                 :             :                                 timestamptz_to_str(recoveryStopTime))));
    2850                 :             :             }
    2851                 :           0 :             return true;
    2852                 :             :         }
    2853                 :             :     }
    2854                 :             : 
    2855                 :             :     /* Check if we should stop as soon as reaching consistency */
    2856   [ -  +  -  - ]:       24552 :     if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE && reachedConsistency)
    2857                 :             :     {
    2858         [ #  # ]:           0 :         ereport(LOG,
    2859                 :             :                 (errmsg("recovery stopping after reaching consistency")));
    2860                 :             : 
    2861                 :           0 :         recoveryStopAfter = true;
    2862                 :           0 :         recoveryStopXid = InvalidTransactionId;
    2863                 :           0 :         recoveryStopTime = 0;
    2864                 :           0 :         recoveryStopLSN = InvalidXLogRecPtr;
    2865                 :           0 :         recoveryStopName[0] = '\0';
    2866                 :           0 :         return true;
    2867                 :             :     }
    2868                 :             : 
    2869                 :       24552 :     return false;
    2870                 :             : }
    2871                 :             : 
    2872                 :             : /*
    2873                 :             :  * Create a comment for the history file to explain why and where
    2874                 :             :  * timeline changed.
    2875                 :             :  */
    2876                 :             : static char *
    2877                 :        1030 : getRecoveryStopReason(void)
    2878                 :             : {
    2879                 :             :     char        reason[200];
    2880                 :             : 
    2881         [ -  + ]:        1030 :     if (recoveryTarget == RECOVERY_TARGET_XID)
    2882                 :           0 :         snprintf(reason, sizeof(reason),
    2883                 :             :                  "%s transaction %u",
    2884         [ #  # ]:           0 :                  recoveryStopAfter ? "after" : "before",
    2885                 :             :                  recoveryStopXid);
    2886         [ -  + ]:        1030 :     else if (recoveryTarget == RECOVERY_TARGET_TIME)
    2887                 :           0 :         snprintf(reason, sizeof(reason),
    2888                 :             :                  "%s %s\n",
    2889         [ #  # ]:           0 :                  recoveryStopAfter ? "after" : "before",
    2890                 :             :                  timestamptz_to_str(recoveryStopTime));
    2891         [ +  + ]:        1030 :     else if (recoveryTarget == RECOVERY_TARGET_LSN)
    2892                 :           6 :         snprintf(reason, sizeof(reason),
    2893                 :             :                  "%s LSN %X/%08X\n",
    2894                 :           6 :                  recoveryStopAfter ? "after" : "before",
    2895         [ +  + ]:           6 :                  LSN_FORMAT_ARGS(recoveryStopLSN));
    2896         [ +  + ]:        1024 :     else if (recoveryTarget == RECOVERY_TARGET_NAME)
    2897                 :           3 :         snprintf(reason, sizeof(reason),
    2898                 :             :                  "at restore point \"%s\"",
    2899                 :             :                  recoveryStopName);
    2900         [ -  + ]:        1021 :     else if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE)
    2901                 :           0 :         snprintf(reason, sizeof(reason), "reached consistency");
    2902                 :             :     else
    2903                 :        1021 :         snprintf(reason, sizeof(reason), "no recovery target specified");
    2904                 :             : 
    2905                 :        1030 :     return pstrdup(reason);
    2906                 :             : }
    2907                 :             : 
    2908                 :             : /*
    2909                 :             :  * Wait until shared recoveryPauseState is set to RECOVERY_NOT_PAUSED.
    2910                 :             :  *
    2911                 :             :  * endOfRecovery is true if the recovery target is reached and
    2912                 :             :  * the paused state starts at the end of recovery because of
    2913                 :             :  * recovery_target_action=pause, and false otherwise.
    2914                 :             :  */
    2915                 :             : static void
    2916                 :           8 : recoveryPausesHere(bool endOfRecovery)
    2917                 :             : {
    2918                 :             :     /* Don't pause unless users can connect! */
    2919         [ -  + ]:           8 :     if (!LocalHotStandbyActive)
    2920                 :           0 :         return;
    2921                 :             : 
    2922                 :             :     /* Don't pause after standby promotion has been triggered */
    2923         [ -  + ]:           8 :     if (LocalPromoteIsTriggered)
    2924                 :           0 :         return;
    2925                 :             : 
    2926         [ +  + ]:           8 :     if (endOfRecovery)
    2927         [ +  - ]:           1 :         ereport(LOG,
    2928                 :             :                 (errmsg("pausing at the end of recovery"),
    2929                 :             :                  errhint("Execute pg_wal_replay_resume() to promote.")));
    2930                 :             :     else
    2931         [ +  - ]:           7 :         ereport(LOG,
    2932                 :             :                 (errmsg("recovery has paused"),
    2933                 :             :                  errhint("Execute pg_wal_replay_resume() to continue.")));
    2934                 :             : 
    2935                 :             :     /* loop until recoveryPauseState is set to RECOVERY_NOT_PAUSED */
    2936         [ +  + ]:          26 :     while (GetRecoveryPauseState() != RECOVERY_NOT_PAUSED)
    2937                 :             :     {
    2938                 :          20 :         ProcessStartupProcInterrupts();
    2939         [ +  + ]:          20 :         if (CheckForStandbyTrigger())
    2940                 :           2 :             return;
    2941                 :             : 
    2942                 :             :         /*
    2943                 :             :          * If recovery pause is requested then set it paused.  While we are in
    2944                 :             :          * the loop, user might resume and pause again so set this every time.
    2945                 :             :          */
    2946                 :          18 :         ConfirmRecoveryPaused();
    2947                 :             : 
    2948                 :             :         /*
    2949                 :             :          * We wait on a condition variable that will wake us as soon as the
    2950                 :             :          * pause ends, but we use a timeout so we can check the above exit
    2951                 :             :          * condition periodically too.
    2952                 :             :          */
    2953                 :          18 :         ConditionVariableTimedSleep(&XLogRecoveryCtl->recoveryNotPausedCV, 1000,
    2954                 :             :                                     WAIT_EVENT_RECOVERY_PAUSE);
    2955                 :             :     }
    2956                 :           6 :     ConditionVariableCancelSleep();
    2957                 :             : }
    2958                 :             : 
    2959                 :             : /*
    2960                 :             :  * When recovery_min_apply_delay is set, we wait long enough to make sure
    2961                 :             :  * certain record types are applied at least that interval behind the primary.
    2962                 :             :  *
    2963                 :             :  * Returns true if we waited.
    2964                 :             :  *
    2965                 :             :  * Note that the delay is calculated between the WAL record log time and
    2966                 :             :  * the current time on standby. We would prefer to keep track of when this
    2967                 :             :  * standby received each WAL record, which would allow a more consistent
    2968                 :             :  * approach and one not affected by time synchronisation issues, but that
    2969                 :             :  * is significantly more effort and complexity for little actual gain in
    2970                 :             :  * usability.
    2971                 :             :  */
    2972                 :             : static bool
    2973                 :     2972376 : recoveryApplyDelay(XLogReaderState *record)
    2974                 :             : {
    2975                 :             :     uint8       xact_info;
    2976                 :             :     TimestampTz xtime;
    2977                 :             :     TimestampTz delayUntil;
    2978                 :             :     long        msecs;
    2979                 :             : 
    2980                 :             :     /* nothing to do if no delay configured */
    2981         [ +  + ]:     2972376 :     if (recovery_min_apply_delay <= 0)
    2982                 :     2972222 :         return false;
    2983                 :             : 
    2984                 :             :     /* no delay is applied on a database not yet consistent */
    2985         [ +  + ]:         154 :     if (!reachedConsistency)
    2986                 :           4 :         return false;
    2987                 :             : 
    2988                 :             :     /* nothing to do if crash recovery is requested */
    2989         [ -  + ]:         150 :     if (!ArchiveRecoveryRequested)
    2990                 :           0 :         return false;
    2991                 :             : 
    2992                 :             :     /*
    2993                 :             :      * Is it a COMMIT record?
    2994                 :             :      *
    2995                 :             :      * We deliberately choose not to delay aborts since they have no effect on
    2996                 :             :      * MVCC. We already allow replay of records that don't have a timestamp,
    2997                 :             :      * so there is already opportunity for issues caused by early conflicts on
    2998                 :             :      * standbys.
    2999                 :             :      */
    3000         [ +  + ]:         150 :     if (XLogRecGetRmid(record) != RM_XACT_ID)
    3001                 :         120 :         return false;
    3002                 :             : 
    3003                 :          30 :     xact_info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
    3004                 :             : 
    3005   [ -  +  -  - ]:          30 :     if (xact_info != XLOG_XACT_COMMIT &&
    3006                 :             :         xact_info != XLOG_XACT_COMMIT_PREPARED)
    3007                 :           0 :         return false;
    3008                 :             : 
    3009         [ -  + ]:          30 :     if (!getRecordTimestamp(record, &xtime))
    3010                 :           0 :         return false;
    3011                 :             : 
    3012                 :          30 :     delayUntil = TimestampTzPlusMilliseconds(xtime, recovery_min_apply_delay);
    3013                 :             : 
    3014                 :             :     /*
    3015                 :             :      * Exit without arming the latch if it's already past time to apply this
    3016                 :             :      * record
    3017                 :             :      */
    3018                 :          30 :     msecs = TimestampDifferenceMilliseconds(GetCurrentTimestamp(), delayUntil);
    3019         [ +  + ]:          30 :     if (msecs <= 0)
    3020                 :           5 :         return false;
    3021                 :             : 
    3022                 :             :     while (true)
    3023                 :             :     {
    3024                 :          65 :         ResetLatch(&XLogRecoveryCtl->recoveryWakeupLatch);
    3025                 :             : 
    3026                 :             :         /* This might change recovery_min_apply_delay. */
    3027                 :          65 :         ProcessStartupProcInterrupts();
    3028                 :             : 
    3029         [ -  + ]:          65 :         if (CheckForStandbyTrigger())
    3030                 :           0 :             break;
    3031                 :             : 
    3032                 :             :         /*
    3033                 :             :          * Recalculate delayUntil as recovery_min_apply_delay could have
    3034                 :             :          * changed while waiting in this loop.
    3035                 :             :          */
    3036                 :          65 :         delayUntil = TimestampTzPlusMilliseconds(xtime, recovery_min_apply_delay);
    3037                 :             : 
    3038                 :             :         /*
    3039                 :             :          * Wait for difference between GetCurrentTimestamp() and delayUntil.
    3040                 :             :          */
    3041                 :          65 :         msecs = TimestampDifferenceMilliseconds(GetCurrentTimestamp(),
    3042                 :             :                                                 delayUntil);
    3043                 :             : 
    3044         [ +  + ]:          65 :         if (msecs <= 0)
    3045                 :          25 :             break;
    3046                 :             : 
    3047         [ -  + ]:          40 :         elog(DEBUG2, "recovery apply delay %ld milliseconds", msecs);
    3048                 :             : 
    3049                 :          40 :         (void) WaitLatch(&XLogRecoveryCtl->recoveryWakeupLatch,
    3050                 :             :                          WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
    3051                 :             :                          msecs,
    3052                 :             :                          WAIT_EVENT_RECOVERY_APPLY_DELAY);
    3053                 :             :     }
    3054                 :          25 :     return true;
    3055                 :             : }
    3056                 :             : 
    3057                 :             : /*
    3058                 :             :  * Get the current state of the recovery pause request.
    3059                 :             :  */
    3060                 :             : RecoveryPauseState
    3061                 :          35 : GetRecoveryPauseState(void)
    3062                 :             : {
    3063                 :             :     RecoveryPauseState state;
    3064                 :             : 
    3065                 :          35 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    3066                 :          35 :     state = XLogRecoveryCtl->recoveryPauseState;
    3067                 :          35 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    3068                 :             : 
    3069                 :          35 :     return state;
    3070                 :             : }
    3071                 :             : 
    3072                 :             : /*
    3073                 :             :  * Set the recovery pause state.
    3074                 :             :  *
    3075                 :             :  * If recovery pause is requested then sets the recovery pause state to
    3076                 :             :  * 'pause requested' if it is not already 'paused'.  Otherwise, sets it
    3077                 :             :  * to 'not paused' to resume the recovery.  The recovery pause will be
    3078                 :             :  * confirmed by the ConfirmRecoveryPaused.
    3079                 :             :  */
    3080                 :             : void
    3081                 :          71 : SetRecoveryPause(bool recoveryPause)
    3082                 :             : {
    3083                 :          71 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    3084                 :             : 
    3085         [ +  + ]:          71 :     if (!recoveryPause)
    3086                 :          62 :         XLogRecoveryCtl->recoveryPauseState = RECOVERY_NOT_PAUSED;
    3087         [ +  - ]:           9 :     else if (XLogRecoveryCtl->recoveryPauseState == RECOVERY_NOT_PAUSED)
    3088                 :           9 :         XLogRecoveryCtl->recoveryPauseState = RECOVERY_PAUSE_REQUESTED;
    3089                 :             : 
    3090                 :          71 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    3091                 :             : 
    3092         [ +  + ]:          71 :     if (!recoveryPause)
    3093                 :          62 :         ConditionVariableBroadcast(&XLogRecoveryCtl->recoveryNotPausedCV);
    3094                 :          71 : }
    3095                 :             : 
    3096                 :             : /*
    3097                 :             :  * Confirm the recovery pause by setting the recovery pause state to
    3098                 :             :  * RECOVERY_PAUSED.
    3099                 :             :  */
    3100                 :             : static void
    3101                 :          18 : ConfirmRecoveryPaused(void)
    3102                 :             : {
    3103                 :             :     /* If recovery pause is requested then set it paused */
    3104                 :          18 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    3105         [ +  + ]:          18 :     if (XLogRecoveryCtl->recoveryPauseState == RECOVERY_PAUSE_REQUESTED)
    3106                 :           8 :         XLogRecoveryCtl->recoveryPauseState = RECOVERY_PAUSED;
    3107                 :          18 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    3108                 :          18 : }
    3109                 :             : 
    3110                 :             : 
    3111                 :             : /*
    3112                 :             :  * Attempt to read the next XLOG record.
    3113                 :             :  *
    3114                 :             :  * Before first call, the reader needs to be positioned to the first record
    3115                 :             :  * by calling XLogPrefetcherBeginRead().
    3116                 :             :  *
    3117                 :             :  * If no valid record is available, returns NULL, or fails if emode is PANIC.
    3118                 :             :  * (emode must be either PANIC, LOG). In standby mode, retries until a valid
    3119                 :             :  * record is available.
    3120                 :             :  */
    3121                 :             : static XLogRecord *
    3122                 :     2974883 : ReadRecord(XLogPrefetcher *xlogprefetcher, int emode,
    3123                 :             :            bool fetching_ckpt, TimeLineID replayTLI)
    3124                 :             : {
    3125                 :             :     XLogRecord *record;
    3126                 :     2974883 :     XLogReaderState *xlogreader = XLogPrefetcherGetReader(xlogprefetcher);
    3127                 :     2974883 :     XLogPageReadPrivate *private = (XLogPageReadPrivate *) xlogreader->private_data;
    3128                 :             : 
    3129                 :             :     Assert(AmStartupProcess() || !IsUnderPostmaster);
    3130                 :             : 
    3131                 :             :     /* Pass through parameters to XLogPageRead */
    3132                 :     2974883 :     private->fetching_ckpt = fetching_ckpt;
    3133                 :     2974883 :     private->emode = emode;
    3134                 :     2974883 :     private->randAccess = !XLogRecPtrIsValid(xlogreader->ReadRecPtr);
    3135                 :     2974883 :     private->replayTLI = replayTLI;
    3136                 :             : 
    3137                 :             :     /* This is the first attempt to read this page. */
    3138                 :     2974883 :     lastSourceFailed = false;
    3139                 :             : 
    3140                 :             :     for (;;)
    3141                 :         164 :     {
    3142                 :             :         char       *errormsg;
    3143                 :             : 
    3144                 :     2975047 :         record = XLogPrefetcherReadRecord(xlogprefetcher, &errormsg);
    3145         [ +  + ]:     2974982 :         if (record == NULL)
    3146                 :             :         {
    3147                 :             :             /*
    3148                 :             :              * When we find that WAL ends in an incomplete record, keep track
    3149                 :             :              * of that record.  After recovery is done, we'll write a record
    3150                 :             :              * to indicate to downstream WAL readers that that portion is to
    3151                 :             :              * be ignored.
    3152                 :             :              *
    3153                 :             :              * However, when ArchiveRecoveryRequested = true, we're going to
    3154                 :             :              * switch to a new timeline at the end of recovery. We will only
    3155                 :             :              * copy WAL over to the new timeline up to the end of the last
    3156                 :             :              * complete record, so if we did this, we would later create an
    3157                 :             :              * overwrite contrecord in the wrong place, breaking everything.
    3158                 :             :              */
    3159         [ +  + ]:         334 :             if (!ArchiveRecoveryRequested &&
    3160         [ +  + ]:         114 :                 XLogRecPtrIsValid(xlogreader->abortedRecPtr))
    3161                 :             :             {
    3162                 :          11 :                 abortedRecPtr = xlogreader->abortedRecPtr;
    3163                 :          11 :                 missingContrecPtr = xlogreader->missingContrecPtr;
    3164                 :             :             }
    3165                 :             : 
    3166         [ +  + ]:         334 :             if (readFile >= 0)
    3167                 :             :             {
    3168                 :         310 :                 close(readFile);
    3169                 :         310 :                 readFile = -1;
    3170                 :             :             }
    3171                 :             : 
    3172                 :             :             /*
    3173                 :             :              * We only end up here without a message when XLogPageRead()
    3174                 :             :              * failed - in that case we already logged something. In
    3175                 :             :              * StandbyMode that only happens if we have been triggered, so we
    3176                 :             :              * shouldn't loop anymore in that case.
    3177                 :             :              */
    3178         [ +  + ]:         334 :             if (errormsg)
    3179         [ +  + ]:         310 :                 ereport(emode_for_corrupt_record(emode, xlogreader->EndRecPtr),
    3180                 :             :                         (errmsg_internal("%s", errormsg) /* already translated */ ));
    3181                 :             :         }
    3182                 :             : 
    3183                 :             :         /*
    3184                 :             :          * Check page TLI is one of the expected values.
    3185                 :             :          */
    3186         [ -  + ]:     2974648 :         else if (!tliInHistory(xlogreader->latestPageTLI, expectedTLEs))
    3187                 :             :         {
    3188                 :             :             char        fname[MAXFNAMELEN];
    3189                 :             :             XLogSegNo   segno;
    3190                 :             :             int32       offset;
    3191                 :             : 
    3192                 :           0 :             XLByteToSeg(xlogreader->latestPagePtr, segno, wal_segment_size);
    3193                 :           0 :             offset = XLogSegmentOffset(xlogreader->latestPagePtr,
    3194                 :             :                                        wal_segment_size);
    3195                 :           0 :             XLogFileName(fname, xlogreader->seg.ws_tli, segno,
    3196                 :             :                          wal_segment_size);
    3197         [ #  # ]:           0 :             ereport(emode_for_corrupt_record(emode, xlogreader->EndRecPtr),
    3198                 :             :                     errmsg("unexpected timeline ID %u in WAL segment %s, LSN %X/%08X, offset %u",
    3199                 :             :                            xlogreader->latestPageTLI,
    3200                 :             :                            fname,
    3201                 :             :                            LSN_FORMAT_ARGS(xlogreader->latestPagePtr),
    3202                 :             :                            offset));
    3203                 :           0 :             record = NULL;
    3204                 :             :         }
    3205                 :             : 
    3206         [ +  + ]:     2974982 :         if (record)
    3207                 :             :         {
    3208                 :             :             /* Great, got a record */
    3209                 :     2974818 :             return record;
    3210                 :             :         }
    3211                 :             :         else
    3212                 :             :         {
    3213                 :             :             /* No valid record available from this source */
    3214                 :         334 :             lastSourceFailed = true;
    3215                 :             : 
    3216                 :             :             /*
    3217                 :             :              * If archive recovery was requested, but we were still doing
    3218                 :             :              * crash recovery, switch to archive recovery and retry using the
    3219                 :             :              * offline archive. We have now replayed all the valid WAL in
    3220                 :             :              * pg_wal, so we are presumably now consistent.
    3221                 :             :              *
    3222                 :             :              * We require that there's at least some valid WAL present in
    3223                 :             :              * pg_wal, however (!fetching_ckpt).  We could recover using the
    3224                 :             :              * WAL from the archive, even if pg_wal is completely empty, but
    3225                 :             :              * we'd have no idea how far we'd have to replay to reach
    3226                 :             :              * consistency.  So err on the safe side and give up.
    3227                 :             :              */
    3228   [ +  +  +  + ]:         334 :             if (!InArchiveRecovery && ArchiveRecoveryRequested &&
    3229         [ +  - ]:           1 :                 !fetching_ckpt)
    3230                 :             :             {
    3231         [ -  + ]:           1 :                 ereport(DEBUG1,
    3232                 :             :                         (errmsg_internal("reached end of WAL in pg_wal, entering archive recovery")));
    3233                 :           1 :                 InArchiveRecovery = true;
    3234         [ +  - ]:           1 :                 if (StandbyModeRequested)
    3235                 :           1 :                     EnableStandbyMode();
    3236                 :             : 
    3237                 :           1 :                 SwitchIntoArchiveRecovery(xlogreader->EndRecPtr, replayTLI);
    3238                 :           1 :                 minRecoveryPoint = xlogreader->EndRecPtr;
    3239                 :           1 :                 minRecoveryPointTLI = replayTLI;
    3240                 :             : 
    3241                 :           1 :                 CheckRecoveryConsistency();
    3242                 :             : 
    3243                 :             :                 /*
    3244                 :             :                  * Before we retry, reset lastSourceFailed and currentSource
    3245                 :             :                  * so that we will check the archive next.
    3246                 :             :                  */
    3247                 :           1 :                 lastSourceFailed = false;
    3248                 :           1 :                 currentSource = XLOG_FROM_ANY;
    3249                 :             : 
    3250                 :         164 :                 continue;
    3251                 :             :             }
    3252                 :             : 
    3253                 :             :             /* In standby mode, loop back to retry. Otherwise, give up. */
    3254   [ +  +  +  + ]:         333 :             if (StandbyMode && !CheckForStandbyTrigger())
    3255                 :         163 :                 continue;
    3256                 :             :             else
    3257                 :         170 :                 return NULL;
    3258                 :             :         }
    3259                 :             :     }
    3260                 :             : }
    3261                 :             : 
    3262                 :             : /*
    3263                 :             :  * Read the XLOG page containing targetPagePtr into readBuf (if not read
    3264                 :             :  * already).  Returns number of bytes read, if the page is read successfully,
    3265                 :             :  * or XLREAD_FAIL in case of errors.  When errors occur, they are ereport'ed,
    3266                 :             :  * but only if they have not been previously reported.
    3267                 :             :  *
    3268                 :             :  * See XLogReaderRoutine.page_read for more details.
    3269                 :             :  *
    3270                 :             :  * While prefetching, xlogreader->nonblocking may be set.  In that case,
    3271                 :             :  * returns XLREAD_WOULDBLOCK if we'd otherwise have to wait for more WAL.
    3272                 :             :  *
    3273                 :             :  * This is responsible for restoring files from archive as needed, as well
    3274                 :             :  * as for waiting for the requested WAL record to arrive in standby mode.
    3275                 :             :  *
    3276                 :             :  * xlogreader->private_data->emode specifies the log level used for reporting
    3277                 :             :  * "file not found" or "end of WAL" situations in archive recovery, or in
    3278                 :             :  * standby mode when promotion is triggered. If set to WARNING or below,
    3279                 :             :  * XLogPageRead() returns XLREAD_FAIL in those situations, on higher log
    3280                 :             :  * levels the ereport() won't return.
    3281                 :             :  *
    3282                 :             :  * In standby mode, if after a successful return of XLogPageRead() the
    3283                 :             :  * caller finds the record it's interested in to be broken, it should
    3284                 :             :  * ereport the error with the level determined by
    3285                 :             :  * emode_for_corrupt_record(), and then set lastSourceFailed
    3286                 :             :  * and call XLogPageRead() again with the same arguments. This lets
    3287                 :             :  * XLogPageRead() to try fetching the record from another source, or to
    3288                 :             :  * sleep and retry.
    3289                 :             :  */
    3290                 :             : static int
    3291                 :     1535680 : XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
    3292                 :             :              XLogRecPtr targetRecPtr, char *readBuf)
    3293                 :             : {
    3294                 :     1535680 :     XLogPageReadPrivate *private =
    3295                 :             :         (XLogPageReadPrivate *) xlogreader->private_data;
    3296                 :     1535680 :     int         emode = private->emode;
    3297                 :             :     uint32      targetPageOff;
    3298                 :             :     XLogSegNo   targetSegNo PG_USED_FOR_ASSERTS_ONLY;
    3299                 :             :     ssize_t     r;
    3300                 :             :     instr_time  io_start;
    3301                 :             : 
    3302                 :             :     Assert(AmStartupProcess() || !IsUnderPostmaster);
    3303                 :             : 
    3304                 :     1535680 :     XLByteToSeg(targetPagePtr, targetSegNo, wal_segment_size);
    3305                 :     1535680 :     targetPageOff = XLogSegmentOffset(targetPagePtr, wal_segment_size);
    3306                 :             : 
    3307                 :             :     /*
    3308                 :             :      * See if we need to switch to a new segment because the requested record
    3309                 :             :      * is not in the currently open one.
    3310                 :             :      */
    3311         [ +  + ]:     1535680 :     if (readFile >= 0 &&
    3312         [ +  + ]:     1533793 :         !XLByteInSeg(targetPagePtr, readSegNo, wal_segment_size))
    3313                 :             :     {
    3314                 :             :         /*
    3315                 :             :          * Request a restartpoint if we've replayed too much xlog since the
    3316                 :             :          * last one.
    3317                 :             :          */
    3318   [ +  +  +  - ]:        1519 :         if (ArchiveRecoveryRequested && IsUnderPostmaster)
    3319                 :             :         {
    3320         [ +  + ]:        1500 :             if (XLogCheckpointNeeded(readSegNo))
    3321                 :             :             {
    3322                 :        1363 :                 (void) GetRedoRecPtr();
    3323         [ +  + ]:        1363 :                 if (XLogCheckpointNeeded(readSegNo))
    3324                 :        1360 :                     RequestCheckpoint(CHECKPOINT_CAUSE_XLOG);
    3325                 :             :             }
    3326                 :             :         }
    3327                 :             : 
    3328                 :        1519 :         close(readFile);
    3329                 :        1519 :         readFile = -1;
    3330                 :        1519 :         readSource = XLOG_FROM_ANY;
    3331                 :             :     }
    3332                 :             : 
    3333                 :     1535680 :     XLByteToSeg(targetPagePtr, readSegNo, wal_segment_size);
    3334                 :             : 
    3335                 :     1535685 : retry:
    3336                 :             :     /* See if we need to retrieve more data */
    3337         [ +  + ]:     1535685 :     if (readFile < 0 ||
    3338         [ +  + ]:     1532274 :         (readSource == XLOG_FROM_STREAM &&
    3339         [ +  + ]:     1518990 :          flushedUpto < targetPagePtr + reqLen))
    3340                 :             :     {
    3341         [ +  + ]:       35186 :         if (readFile >= 0 &&
    3342         [ +  + ]:       31775 :             xlogreader->nonblocking &&
    3343         [ +  - ]:       15720 :             readSource == XLOG_FROM_STREAM &&
    3344         [ +  - ]:       15720 :             flushedUpto < targetPagePtr + reqLen)
    3345                 :       15720 :             return XLREAD_WOULDBLOCK;
    3346                 :             : 
    3347   [ +  +  +  - ]:       19401 :         switch (WaitForWALToBecomeAvailable(targetPagePtr + reqLen,
    3348                 :       19466 :                                             private->randAccess,
    3349                 :       19466 :                                             private->fetching_ckpt,
    3350                 :             :                                             targetRecPtr,
    3351                 :             :                                             private->replayTLI,
    3352                 :             :                                             xlogreader->EndRecPtr,
    3353                 :       19466 :                                             xlogreader->nonblocking))
    3354                 :             :         {
    3355                 :         432 :             case XLREAD_WOULDBLOCK:
    3356                 :         432 :                 return XLREAD_WOULDBLOCK;
    3357                 :          46 :             case XLREAD_FAIL:
    3358         [ -  + ]:          46 :                 if (readFile >= 0)
    3359                 :           0 :                     close(readFile);
    3360                 :          46 :                 readFile = -1;
    3361                 :          46 :                 readLen = 0;
    3362                 :          46 :                 readSource = XLOG_FROM_ANY;
    3363                 :          46 :                 return XLREAD_FAIL;
    3364                 :       18923 :             case XLREAD_SUCCESS:
    3365                 :       18923 :                 break;
    3366                 :             :         }
    3367                 :             :     }
    3368                 :             : 
    3369                 :             :     /*
    3370                 :             :      * At this point, we have the right segment open and if we're streaming we
    3371                 :             :      * know the requested record is in it.
    3372                 :             :      */
    3373                 :             :     Assert(readFile != -1);
    3374                 :             : 
    3375                 :             :     /*
    3376                 :             :      * If the current segment is being streamed from the primary, calculate
    3377                 :             :      * how much of the current page we have received already. We know the
    3378                 :             :      * requested record has been received, but this is for the benefit of
    3379                 :             :      * future calls, to allow quick exit at the top of this function.
    3380                 :             :      */
    3381         [ +  + ]:     1519422 :     if (readSource == XLOG_FROM_STREAM)
    3382                 :             :     {
    3383         [ +  + ]:     1504343 :         if (((targetPagePtr) / XLOG_BLCKSZ) != (flushedUpto / XLOG_BLCKSZ))
    3384                 :     1492736 :             readLen = XLOG_BLCKSZ;
    3385                 :             :         else
    3386                 :       11607 :             readLen = XLogSegmentOffset(flushedUpto, wal_segment_size) -
    3387                 :             :                 targetPageOff;
    3388                 :             :     }
    3389                 :             :     else
    3390                 :       15079 :         readLen = XLOG_BLCKSZ;
    3391                 :             : 
    3392                 :             :     /* Read the requested page */
    3393                 :     1519422 :     readOff = targetPageOff;
    3394                 :             : 
    3395                 :             :     /* Measure I/O timing when reading segment */
    3396                 :     1519422 :     io_start = pgstat_prepare_io_time(track_wal_io_timing);
    3397                 :             : 
    3398                 :     1519422 :     pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
    3399                 :     1519422 :     r = pg_pread(readFile, readBuf, XLOG_BLCKSZ, (pgoff_t) readOff);
    3400         [ -  + ]:     1519422 :     if (r != XLOG_BLCKSZ)
    3401                 :             :     {
    3402                 :             :         char        fname[MAXFNAMELEN];
    3403                 :           0 :         int         save_errno = errno;
    3404                 :             : 
    3405                 :           0 :         pgstat_report_wait_end();
    3406                 :             : 
    3407                 :             :         /* Count I/O stats only for successful short reads */
    3408         [ #  # ]:           0 :         if (r > 0)
    3409                 :           0 :             pgstat_count_io_op_time(IOOBJECT_WAL, IOCONTEXT_NORMAL, IOOP_READ,
    3410                 :             :                                     io_start, 1, r);
    3411                 :             : 
    3412                 :           0 :         XLogFileName(fname, curFileTLI, readSegNo, wal_segment_size);
    3413         [ #  # ]:           0 :         if (r < 0)
    3414                 :             :         {
    3415                 :           0 :             errno = save_errno;
    3416         [ #  # ]:           0 :             ereport(emode_for_corrupt_record(emode, targetPagePtr + reqLen),
    3417                 :             :                     (errcode_for_file_access(),
    3418                 :             :                      errmsg("could not read from WAL segment %s, LSN %X/%08X, offset %u: %m",
    3419                 :             :                             fname, LSN_FORMAT_ARGS(targetPagePtr),
    3420                 :             :                             readOff)));
    3421                 :             :         }
    3422                 :             :         else
    3423         [ #  # ]:           0 :             ereport(emode_for_corrupt_record(emode, targetPagePtr + reqLen),
    3424                 :             :                     (errcode(ERRCODE_DATA_CORRUPTED),
    3425                 :             :                      errmsg("could not read from WAL segment %s, LSN %X/%08X, offset %u: read %zd of %zu",
    3426                 :             :                             fname, LSN_FORMAT_ARGS(targetPagePtr),
    3427                 :             :                             readOff, r, (Size) XLOG_BLCKSZ)));
    3428                 :           0 :         goto next_record_is_invalid;
    3429                 :             :     }
    3430                 :     1519422 :     pgstat_report_wait_end();
    3431                 :             : 
    3432                 :     1519422 :     pgstat_count_io_op_time(IOOBJECT_WAL, IOCONTEXT_NORMAL, IOOP_READ,
    3433                 :             :                             io_start, 1, r);
    3434                 :             : 
    3435                 :             :     Assert(targetSegNo == readSegNo);
    3436                 :             :     Assert(targetPageOff == readOff);
    3437                 :             :     Assert(reqLen <= readLen);
    3438                 :             : 
    3439                 :     1519422 :     xlogreader->seg.ws_tli = curFileTLI;
    3440                 :             : 
    3441                 :             :     /*
    3442                 :             :      * Check the page header immediately, so that we can retry immediately if
    3443                 :             :      * it's not valid. This may seem unnecessary, because ReadPageInternal()
    3444                 :             :      * validates the page header anyway, and would propagate the failure up to
    3445                 :             :      * ReadRecord(), which would retry. However, there's a corner case with
    3446                 :             :      * continuation records, if a record is split across two pages such that
    3447                 :             :      * we would need to read the two pages from different sources across two
    3448                 :             :      * WAL segments.
    3449                 :             :      *
    3450                 :             :      * The first page is only available locally, in pg_wal, because it's
    3451                 :             :      * already been recycled on the primary. The second page, however, is not
    3452                 :             :      * present in pg_wal, and we should stream it from the primary. There is a
    3453                 :             :      * recycled WAL segment present in pg_wal, with garbage contents, however.
    3454                 :             :      * We would read the first page from the local WAL segment, but when
    3455                 :             :      * reading the second page, we would read the bogus, recycled, WAL
    3456                 :             :      * segment. If we didn't catch that case here, we would never recover,
    3457                 :             :      * because ReadRecord() would retry reading the whole record from the
    3458                 :             :      * beginning.
    3459                 :             :      *
    3460                 :             :      * Of course, this only catches errors in the page header, which is what
    3461                 :             :      * happens in the case of a recycled WAL segment. Other kinds of errors or
    3462                 :             :      * corruption still has the same problem. But this at least fixes the
    3463                 :             :      * common case, which can happen as part of normal operation.
    3464                 :             :      *
    3465                 :             :      * Validating the page header is cheap enough that doing it twice
    3466                 :             :      * shouldn't be a big deal from a performance point of view.
    3467                 :             :      *
    3468                 :             :      * When not in standby mode, an invalid page header should cause recovery
    3469                 :             :      * to end, not retry reading the page, so we don't need to validate the
    3470                 :             :      * page header here for the retry. Instead, ReadPageInternal() is
    3471                 :             :      * responsible for the validation.
    3472                 :             :      */
    3473         [ +  + ]:     1519422 :     if (StandbyMode &&
    3474         [ +  + ]:     1508817 :         (targetPagePtr % wal_segment_size) == 0 &&
    3475         [ +  + ]:        1515 :         !XLogReaderValidatePageHeader(xlogreader, targetPagePtr, readBuf))
    3476                 :             :     {
    3477                 :             :         /*
    3478                 :             :          * Emit this error right now then retry this page immediately. Use
    3479                 :             :          * errmsg_internal() because the message was already translated.
    3480                 :             :          */
    3481         [ +  - ]:           6 :         if (xlogreader->errormsg_buf[0])
    3482         [ +  + ]:           6 :             ereport(emode_for_corrupt_record(emode, xlogreader->EndRecPtr),
    3483                 :             :                     (errmsg_internal("%s", xlogreader->errormsg_buf)));
    3484                 :             : 
    3485                 :             :         /* reset any error XLogReaderValidatePageHeader() might have set */
    3486                 :           6 :         XLogReaderResetError(xlogreader);
    3487                 :           6 :         goto next_record_is_invalid;
    3488                 :             :     }
    3489                 :             : 
    3490                 :     1519416 :     return readLen;
    3491                 :             : 
    3492                 :           6 : next_record_is_invalid:
    3493                 :             : 
    3494                 :             :     /*
    3495                 :             :      * If we're reading ahead, give up fast.  Retries and error reporting will
    3496                 :             :      * be handled by a later read when recovery catches up to this point.
    3497                 :             :      */
    3498         [ +  + ]:           6 :     if (xlogreader->nonblocking)
    3499                 :           1 :         return XLREAD_WOULDBLOCK;
    3500                 :             : 
    3501                 :           5 :     lastSourceFailed = true;
    3502                 :             : 
    3503         [ +  - ]:           5 :     if (readFile >= 0)
    3504                 :           5 :         close(readFile);
    3505                 :           5 :     readFile = -1;
    3506                 :           5 :     readLen = 0;
    3507                 :           5 :     readSource = XLOG_FROM_ANY;
    3508                 :             : 
    3509                 :             :     /* In standby-mode, keep trying */
    3510         [ +  - ]:           5 :     if (StandbyMode)
    3511                 :           5 :         goto retry;
    3512                 :             :     else
    3513                 :           0 :         return XLREAD_FAIL;
    3514                 :             : }
    3515                 :             : 
    3516                 :             : /*
    3517                 :             :  * Open the WAL segment containing WAL location 'RecPtr'.
    3518                 :             :  *
    3519                 :             :  * The segment can be fetched via restore_command, or via walreceiver having
    3520                 :             :  * streamed the record, or it can already be present in pg_wal. Checking
    3521                 :             :  * pg_wal is mainly for crash recovery, but it will be polled in standby mode
    3522                 :             :  * too, in case someone copies a new segment directly to pg_wal. That is not
    3523                 :             :  * documented or recommended, though.
    3524                 :             :  *
    3525                 :             :  * If 'fetching_ckpt' is true, we're fetching a checkpoint record, and should
    3526                 :             :  * prepare to read WAL starting from RedoStartLSN after this.
    3527                 :             :  *
    3528                 :             :  * 'RecPtr' might not point to the beginning of the record we're interested
    3529                 :             :  * in, it might also point to the page or segment header. In that case,
    3530                 :             :  * 'tliRecPtr' is the position of the WAL record we're interested in. It is
    3531                 :             :  * used to decide which timeline to stream the requested WAL from.
    3532                 :             :  *
    3533                 :             :  * 'replayLSN' is the current replay LSN, so that if we scan for new
    3534                 :             :  * timelines, we can reject a switch to a timeline that branched off before
    3535                 :             :  * this point.
    3536                 :             :  *
    3537                 :             :  * If the record is not immediately available, the function returns XLREAD_FAIL
    3538                 :             :  * if we're not in standby mode. In standby mode, the function waits for it to
    3539                 :             :  * become available.
    3540                 :             :  *
    3541                 :             :  * When the requested record becomes available, the function opens the file
    3542                 :             :  * containing it (if not open already), and returns XLREAD_SUCCESS. When end
    3543                 :             :  * of standby mode is triggered by the user, and there is no more WAL
    3544                 :             :  * available, returns XLREAD_FAIL.
    3545                 :             :  *
    3546                 :             :  * If nonblocking is true, then give up immediately if we can't satisfy the
    3547                 :             :  * request, returning XLREAD_WOULDBLOCK instead of waiting.
    3548                 :             :  */
    3549                 :             : static XLogPageReadResult
    3550                 :       19466 : WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
    3551                 :             :                             bool fetching_ckpt, XLogRecPtr tliRecPtr,
    3552                 :             :                             TimeLineID replayTLI, XLogRecPtr replayLSN,
    3553                 :             :                             bool nonblocking)
    3554                 :             : {
    3555                 :             :     static TimestampTz last_fail_time = 0;
    3556                 :             :     TimestampTz now;
    3557                 :       19466 :     bool        streaming_reply_sent = false;
    3558                 :             : 
    3559                 :             :     /*-------
    3560                 :             :      * Standby mode is implemented by a state machine:
    3561                 :             :      *
    3562                 :             :      * 1. Read from either archive or pg_wal (XLOG_FROM_ARCHIVE), or just
    3563                 :             :      *    pg_wal (XLOG_FROM_PG_WAL)
    3564                 :             :      * 2. Check for promotion trigger request
    3565                 :             :      * 3. Read from primary server via walreceiver (XLOG_FROM_STREAM)
    3566                 :             :      * 4. Rescan timelines
    3567                 :             :      * 5. Sleep wal_retrieve_retry_interval milliseconds, and loop back to 1.
    3568                 :             :      *
    3569                 :             :      * Failure to read from the current source advances the state machine to
    3570                 :             :      * the next state.
    3571                 :             :      *
    3572                 :             :      * 'currentSource' indicates the current state. There are no currentSource
    3573                 :             :      * values for "check trigger", "rescan timelines", and "sleep" states,
    3574                 :             :      * those actions are taken when reading from the previous source fails, as
    3575                 :             :      * part of advancing to the next state.
    3576                 :             :      *
    3577                 :             :      * If standby mode is turned off while reading WAL from stream, we move
    3578                 :             :      * to XLOG_FROM_ARCHIVE and reset lastSourceFailed, to force fetching
    3579                 :             :      * the files (which would be required at end of recovery, e.g., timeline
    3580                 :             :      * history file) from archive or pg_wal. We don't need to kill WAL receiver
    3581                 :             :      * here because it's already stopped when standby mode is turned off at
    3582                 :             :      * the end of recovery.
    3583                 :             :      *-------
    3584                 :             :      */
    3585         [ +  + ]:       19466 :     if (!InArchiveRecovery)
    3586                 :        1084 :         currentSource = XLOG_FROM_PG_WAL;
    3587         [ +  + ]:       18382 :     else if (currentSource == XLOG_FROM_ANY ||
    3588   [ +  +  -  + ]:       18241 :              (!StandbyMode && currentSource == XLOG_FROM_STREAM))
    3589                 :             :     {
    3590                 :         141 :         lastSourceFailed = false;
    3591                 :         141 :         currentSource = XLOG_FROM_ARCHIVE;
    3592                 :             :     }
    3593                 :             : 
    3594                 :             :     for (;;)
    3595                 :       17080 :     {
    3596                 :       36546 :         XLogSource  oldSource = currentSource;
    3597                 :       36546 :         bool        startWalReceiver = false;
    3598                 :             : 
    3599                 :             :         /*
    3600                 :             :          * First check if we failed to read from the current source, and
    3601                 :             :          * advance the state machine if so. The failure to read might've
    3602                 :             :          * happened outside this function, e.g when a CRC check fails on a
    3603                 :             :          * record, or within this loop.
    3604                 :             :          */
    3605         [ +  + ]:       36546 :         if (lastSourceFailed)
    3606                 :             :         {
    3607                 :             :             /*
    3608                 :             :              * Don't allow any retry loops to occur during nonblocking
    3609                 :             :              * readahead.  Let the caller process everything that has been
    3610                 :             :              * decoded already first.
    3611                 :             :              */
    3612         [ +  + ]:         616 :             if (nonblocking)
    3613                 :          91 :                 return XLREAD_WOULDBLOCK;
    3614                 :             : 
    3615      [ +  +  - ]:         525 :             switch (currentSource)
    3616                 :             :             {
    3617                 :         310 :                 case XLOG_FROM_ARCHIVE:
    3618                 :             :                 case XLOG_FROM_PG_WAL:
    3619                 :             : 
    3620                 :             :                     /*
    3621                 :             :                      * Check to see if promotion is requested. Note that we do
    3622                 :             :                      * this only after failure, so when you promote, we still
    3623                 :             :                      * finish replaying as much as we can from archive and
    3624                 :             :                      * pg_wal before failover.
    3625                 :             :                      */
    3626   [ +  +  +  + ]:         310 :                     if (StandbyMode && CheckForStandbyTrigger())
    3627                 :             :                     {
    3628                 :          16 :                         XLogShutdownWalRcv();
    3629                 :          16 :                         return XLREAD_FAIL;
    3630                 :             :                     }
    3631                 :             : 
    3632                 :             :                     /*
    3633                 :             :                      * Not in standby mode, and we've now tried the archive
    3634                 :             :                      * and pg_wal.
    3635                 :             :                      */
    3636         [ +  + ]:         294 :                     if (!StandbyMode)
    3637                 :          30 :                         return XLREAD_FAIL;
    3638                 :             : 
    3639                 :             :                     /*
    3640                 :             :                      * Move to XLOG_FROM_STREAM state, and set to start a
    3641                 :             :                      * walreceiver if necessary.
    3642                 :             :                      */
    3643                 :         264 :                     currentSource = XLOG_FROM_STREAM;
    3644                 :         264 :                     startWalReceiver = true;
    3645                 :         264 :                     break;
    3646                 :             : 
    3647                 :         215 :                 case XLOG_FROM_STREAM:
    3648                 :             : 
    3649                 :             :                     /*
    3650                 :             :                      * Failure while streaming. Most likely, we got here
    3651                 :             :                      * because streaming replication was terminated, or
    3652                 :             :                      * promotion was triggered. But we also get here if we
    3653                 :             :                      * find an invalid record in the WAL streamed from the
    3654                 :             :                      * primary, in which case something is seriously wrong.
    3655                 :             :                      * There's little chance that the problem will just go
    3656                 :             :                      * away, but PANIC is not good for availability either,
    3657                 :             :                      * especially in hot standby mode. So, we treat that the
    3658                 :             :                      * same as disconnection, and retry from archive/pg_wal
    3659                 :             :                      * again. The WAL in the archive should be identical to
    3660                 :             :                      * what was streamed, so it's unlikely that it helps, but
    3661                 :             :                      * one can hope...
    3662                 :             :                      */
    3663                 :             : 
    3664                 :             :                     /*
    3665                 :             :                      * We should be able to move to XLOG_FROM_STREAM only in
    3666                 :             :                      * standby mode.
    3667                 :             :                      */
    3668                 :             :                     Assert(StandbyMode);
    3669                 :             : 
    3670                 :             :                     /*
    3671                 :             :                      * Before we leave XLOG_FROM_STREAM state, make sure that
    3672                 :             :                      * walreceiver is not active, so that it won't overwrite
    3673                 :             :                      * WAL that we restore from archive.
    3674                 :             :                      *
    3675                 :             :                      * If walreceiver is actively streaming (or attempting to
    3676                 :             :                      * connect), we must shut it down. However, if it's
    3677                 :             :                      * already in WAITING state (e.g., due to timeline
    3678                 :             :                      * divergence), we only need to reset the install flag to
    3679                 :             :                      * allow archive restoration.
    3680                 :             :                      */
    3681         [ +  + ]:         215 :                     if (WalRcvStreaming())
    3682                 :          38 :                         XLogShutdownWalRcv();
    3683                 :             :                     else
    3684                 :             :                     {
    3685                 :             :                         /*
    3686                 :             :                          * WALRCV_STOPPING state is a transient state while
    3687                 :             :                          * the startup process is in ShutdownWalRcv().  It
    3688                 :             :                          * should never appear here since we would be waiting
    3689                 :             :                          * for the walreceiver to reach WALRCV_STOPPED in that
    3690                 :             :                          * case.
    3691                 :             :                          */
    3692                 :             :                         Assert(WalRcvGetState() != WALRCV_STOPPING);
    3693                 :         177 :                         ResetInstallXLogFileSegmentActive();
    3694                 :             :                     }
    3695                 :             : 
    3696                 :             :                     /*
    3697                 :             :                      * Before we sleep, re-scan for possible new timelines if
    3698                 :             :                      * we were requested to recover to the latest timeline.
    3699                 :             :                      */
    3700         [ +  - ]:         215 :                     if (recoveryTargetTimeLineGoal == RECOVERY_TARGET_TIMELINE_LATEST)
    3701                 :             :                     {
    3702         [ +  + ]:         215 :                         if (rescanLatestTimeLine(replayTLI, replayLSN))
    3703                 :             :                         {
    3704                 :           9 :                             currentSource = XLOG_FROM_ARCHIVE;
    3705                 :           9 :                             break;
    3706                 :             :                         }
    3707                 :             :                     }
    3708                 :             : 
    3709                 :             :                     /*
    3710                 :             :                      * XLOG_FROM_STREAM is the last state in our state
    3711                 :             :                      * machine, so we've exhausted all the options for
    3712                 :             :                      * obtaining the requested WAL. We're going to loop back
    3713                 :             :                      * and retry from the archive, but if it hasn't been long
    3714                 :             :                      * since last attempt, sleep wal_retrieve_retry_interval
    3715                 :             :                      * milliseconds to avoid busy-waiting.
    3716                 :             :                      */
    3717                 :         206 :                     now = GetCurrentTimestamp();
    3718         [ +  + ]:         206 :                     if (!TimestampDifferenceExceeds(last_fail_time, now,
    3719                 :             :                                                     wal_retrieve_retry_interval))
    3720                 :             :                     {
    3721                 :             :                         long        wait_time;
    3722                 :             : 
    3723                 :         214 :                         wait_time = wal_retrieve_retry_interval -
    3724                 :         107 :                             TimestampDifferenceMilliseconds(last_fail_time, now);
    3725                 :             : 
    3726         [ +  - ]:         107 :                         elog(LOG, "waiting for WAL to become available at %X/%08X",
    3727                 :             :                              LSN_FORMAT_ARGS(RecPtr));
    3728                 :             : 
    3729                 :             :                         /* Do background tasks that might benefit us later. */
    3730                 :         107 :                         KnownAssignedTransactionIdsIdleMaintenance();
    3731                 :             : 
    3732                 :         107 :                         (void) WaitLatch(&XLogRecoveryCtl->recoveryWakeupLatch,
    3733                 :             :                                          WL_LATCH_SET | WL_TIMEOUT |
    3734                 :             :                                          WL_EXIT_ON_PM_DEATH,
    3735                 :             :                                          wait_time,
    3736                 :             :                                          WAIT_EVENT_RECOVERY_RETRIEVE_RETRY_INTERVAL);
    3737                 :         107 :                         ResetLatch(&XLogRecoveryCtl->recoveryWakeupLatch);
    3738                 :         107 :                         now = GetCurrentTimestamp();
    3739                 :             : 
    3740                 :             :                         /* Handle interrupt signals of startup process */
    3741                 :         107 :                         ProcessStartupProcInterrupts();
    3742                 :             :                     }
    3743                 :         189 :                     last_fail_time = now;
    3744                 :         189 :                     currentSource = XLOG_FROM_ARCHIVE;
    3745                 :         189 :                     break;
    3746                 :             : 
    3747                 :           0 :                 default:
    3748         [ #  # ]:           0 :                     elog(ERROR, "unexpected WAL source %d", currentSource);
    3749                 :             :             }
    3750                 :             :         }
    3751         [ +  + ]:       35930 :         else if (currentSource == XLOG_FROM_PG_WAL)
    3752                 :             :         {
    3753                 :             :             /*
    3754                 :             :              * We just successfully read a file in pg_wal. We prefer files in
    3755                 :             :              * the archive over ones in pg_wal, so try the next file again
    3756                 :             :              * from the archive first.
    3757                 :             :              */
    3758         [ -  + ]:        1080 :             if (InArchiveRecovery)
    3759                 :           0 :                 currentSource = XLOG_FROM_ARCHIVE;
    3760                 :             :         }
    3761                 :             : 
    3762         [ +  + ]:       36392 :         if (currentSource != oldSource)
    3763   [ +  +  +  - ]:         462 :             elog(DEBUG2, "switched WAL source from %s to %s after %s",
    3764                 :             :                  xlogSourceNames[oldSource], xlogSourceNames[currentSource],
    3765                 :             :                  lastSourceFailed ? "failure" : "success");
    3766                 :             : 
    3767                 :             :         /*
    3768                 :             :          * We've now handled possible failure. Try to read from the chosen
    3769                 :             :          * source.
    3770                 :             :          */
    3771                 :       36392 :         lastSourceFailed = false;
    3772                 :             : 
    3773      [ +  +  - ]:       36392 :         switch (currentSource)
    3774                 :             :         {
    3775                 :        2007 :             case XLOG_FROM_ARCHIVE:
    3776                 :             :             case XLOG_FROM_PG_WAL:
    3777                 :             : 
    3778                 :             :                 /*
    3779                 :             :                  * WAL receiver must not be running when reading WAL from
    3780                 :             :                  * archive or pg_wal.
    3781                 :             :                  */
    3782                 :             :                 Assert(!WalRcvStreaming());
    3783                 :             : 
    3784                 :             :                 /* Close any old file we might have open. */
    3785         [ +  + ]:        2007 :                 if (readFile >= 0)
    3786                 :             :                 {
    3787                 :         113 :                     close(readFile);
    3788                 :         113 :                     readFile = -1;
    3789                 :             :                 }
    3790                 :             :                 /* Reset curFileTLI if random fetch. */
    3791         [ +  + ]:        2007 :                 if (randAccess)
    3792                 :        1274 :                     curFileTLI = 0;
    3793                 :             : 
    3794                 :             :                 /*
    3795                 :             :                  * Try to restore the file from archive, or read an existing
    3796                 :             :                  * file from pg_wal.
    3797                 :             :                  */
    3798                 :        2007 :                 readFile = XLogFileReadAnyTLI(readSegNo,
    3799         [ +  + ]:        2007 :                                               currentSource == XLOG_FROM_ARCHIVE ? XLOG_FROM_ANY :
    3800                 :             :                                               currentSource);
    3801         [ +  + ]:        2006 :                 if (readFile >= 0)
    3802                 :        1795 :                     return XLREAD_SUCCESS;  /* success! */
    3803                 :             : 
    3804                 :             :                 /*
    3805                 :             :                  * Nope, not found in archive or pg_wal.
    3806                 :             :                  */
    3807                 :         211 :                 lastSourceFailed = true;
    3808                 :         211 :                 break;
    3809                 :             : 
    3810                 :       34385 :             case XLOG_FROM_STREAM:
    3811                 :             :                 {
    3812                 :             :                     bool        havedata;
    3813                 :             : 
    3814                 :             :                     /*
    3815                 :             :                      * We should be able to move to XLOG_FROM_STREAM only in
    3816                 :             :                      * standby mode.
    3817                 :             :                      */
    3818                 :             :                     Assert(StandbyMode);
    3819                 :             : 
    3820                 :             :                     /*
    3821                 :             :                      * First, shutdown walreceiver if its restart has been
    3822                 :             :                      * requested -- but no point if we're already slated for
    3823                 :             :                      * starting it.
    3824                 :             :                      */
    3825   [ +  +  +  - ]:       34385 :                     if (pendingWalRcvRestart && !startWalReceiver)
    3826                 :             :                     {
    3827                 :           8 :                         XLogShutdownWalRcv();
    3828                 :             : 
    3829                 :             :                         /*
    3830                 :             :                          * Re-scan for possible new timelines if we were
    3831                 :             :                          * requested to recover to the latest timeline.
    3832                 :             :                          */
    3833         [ +  - ]:           8 :                         if (recoveryTargetTimeLineGoal ==
    3834                 :             :                             RECOVERY_TARGET_TIMELINE_LATEST)
    3835                 :           8 :                             rescanLatestTimeLine(replayTLI, replayLSN);
    3836                 :             : 
    3837                 :           8 :                         startWalReceiver = true;
    3838                 :             :                     }
    3839                 :       34385 :                     pendingWalRcvRestart = false;
    3840                 :             : 
    3841                 :             :                     /*
    3842                 :             :                      * Launch walreceiver if needed.
    3843                 :             :                      *
    3844                 :             :                      * If fetching_ckpt is true, RecPtr points to the initial
    3845                 :             :                      * checkpoint location. In that case, we use RedoStartLSN
    3846                 :             :                      * as the streaming start position instead of RecPtr, so
    3847                 :             :                      * that when we later jump backwards to start redo at
    3848                 :             :                      * RedoStartLSN, we will have the logs streamed already.
    3849                 :             :                      */
    3850   [ +  +  +  - ]:       34385 :                     if (startWalReceiver &&
    3851         [ +  + ]:         272 :                         PrimaryConnInfo && strcmp(PrimaryConnInfo, "") != 0)
    3852                 :             :                     {
    3853                 :             :                         XLogRecPtr  ptr;
    3854                 :             :                         TimeLineID  tli;
    3855                 :             : 
    3856         [ +  + ]:         222 :                         if (fetching_ckpt)
    3857                 :             :                         {
    3858                 :           1 :                             ptr = RedoStartLSN;
    3859                 :           1 :                             tli = RedoStartTLI;
    3860                 :             :                         }
    3861                 :             :                         else
    3862                 :             :                         {
    3863                 :         221 :                             ptr = RecPtr;
    3864                 :             : 
    3865                 :             :                             /*
    3866                 :             :                              * Use the record begin position to determine the
    3867                 :             :                              * TLI, rather than the position we're reading.
    3868                 :             :                              */
    3869                 :         221 :                             tli = tliOfPointInHistory(tliRecPtr, expectedTLEs);
    3870                 :             : 
    3871   [ +  -  -  + ]:         221 :                             if (curFileTLI > 0 && tli < curFileTLI)
    3872         [ #  # ]:           0 :                                 elog(ERROR, "according to history file, WAL location %X/%08X belongs to timeline %u, but previous recovered WAL file came from timeline %u",
    3873                 :             :                                      LSN_FORMAT_ARGS(tliRecPtr),
    3874                 :             :                                      tli, curFileTLI);
    3875                 :             :                         }
    3876                 :         222 :                         curFileTLI = tli;
    3877                 :         222 :                         SetInstallXLogFileSegmentActive();
    3878                 :         222 :                         RequestXLogStreaming(tli, ptr, PrimaryConnInfo,
    3879                 :             :                                              PrimarySlotName,
    3880                 :             :                                              wal_receiver_create_temp_slot);
    3881                 :         222 :                         flushedUpto = InvalidXLogRecPtr;
    3882                 :             :                     }
    3883                 :             : 
    3884                 :             :                     /*
    3885                 :             :                      * Check if WAL receiver is active or wait to start up.
    3886                 :             :                      */
    3887         [ +  + ]:       34385 :                     if (!WalRcvStreaming())
    3888                 :             :                     {
    3889                 :         177 :                         lastSourceFailed = true;
    3890                 :         177 :                         break;
    3891                 :             :                     }
    3892                 :             : 
    3893                 :             :                     /*
    3894                 :             :                      * Walreceiver is active, so see if new data has arrived.
    3895                 :             :                      *
    3896                 :             :                      * We only advance XLogReceiptTime when we obtain fresh
    3897                 :             :                      * WAL from walreceiver and observe that we had already
    3898                 :             :                      * processed everything before the most recent "chunk"
    3899                 :             :                      * that it flushed to disk.  In steady state where we are
    3900                 :             :                      * keeping up with the incoming data, XLogReceiptTime will
    3901                 :             :                      * be updated on each cycle. When we are behind,
    3902                 :             :                      * XLogReceiptTime will not advance, so the grace time
    3903                 :             :                      * allotted to conflicting queries will decrease.
    3904                 :             :                      */
    3905         [ +  + ]:       34208 :                     if (RecPtr < flushedUpto)
    3906                 :        1831 :                         havedata = true;
    3907                 :             :                     else
    3908                 :             :                     {
    3909                 :             :                         XLogRecPtr  latestChunkStart;
    3910                 :             : 
    3911                 :       32377 :                         flushedUpto = GetWalRcvFlushRecPtr(&latestChunkStart, &receiveTLI);
    3912   [ +  +  +  - ]:       32377 :                         if (RecPtr < flushedUpto && receiveTLI == curFileTLI)
    3913                 :             :                         {
    3914                 :       16521 :                             havedata = true;
    3915         [ +  + ]:       16521 :                             if (latestChunkStart <= RecPtr)
    3916                 :             :                             {
    3917                 :       12771 :                                 XLogReceiptTime = GetCurrentTimestamp();
    3918                 :       12771 :                                 SetCurrentChunkStartTime(XLogReceiptTime);
    3919                 :             :                             }
    3920                 :             :                         }
    3921                 :             :                         else
    3922                 :       15856 :                             havedata = false;
    3923                 :             :                     }
    3924         [ +  + ]:       34208 :                     if (havedata)
    3925                 :             :                     {
    3926                 :             :                         /*
    3927                 :             :                          * Great, streamed far enough.  Open the file if it's
    3928                 :             :                          * not open already.  Also read the timeline history
    3929                 :             :                          * file if we haven't initialized timeline history
    3930                 :             :                          * yet; it should be streamed over and present in
    3931                 :             :                          * pg_wal by now.  Use XLOG_FROM_STREAM so that source
    3932                 :             :                          * info is set correctly and XLogReceiptTime isn't
    3933                 :             :                          * changed.
    3934                 :             :                          *
    3935                 :             :                          * NB: We must set readTimeLineHistory based on
    3936                 :             :                          * recoveryTargetTLI, not receiveTLI. Normally they'll
    3937                 :             :                          * be the same, but if recovery_target_timeline is
    3938                 :             :                          * 'latest' and archiving is configured, then it's
    3939                 :             :                          * possible that we managed to retrieve one or more
    3940                 :             :                          * new timeline history files from the archive,
    3941                 :             :                          * updating recoveryTargetTLI.
    3942                 :             :                          */
    3943         [ +  + ]:       18352 :                         if (readFile < 0)
    3944                 :             :                         {
    3945         [ +  + ]:        1224 :                             if (!expectedTLEs)
    3946                 :           1 :                                 expectedTLEs = readTimeLineHistory(recoveryTargetTLI);
    3947                 :        1224 :                             readFile = XLogFileRead(readSegNo, receiveTLI,
    3948                 :             :                                                     XLOG_FROM_STREAM, false);
    3949                 :             :                             Assert(readFile >= 0);
    3950                 :             :                         }
    3951                 :             :                         else
    3952                 :             :                         {
    3953                 :             :                             /* just make sure source info is correct... */
    3954                 :       17128 :                             readSource = XLOG_FROM_STREAM;
    3955                 :       17128 :                             XLogReceiptSource = XLOG_FROM_STREAM;
    3956                 :       17128 :                             return XLREAD_SUCCESS;
    3957                 :             :                         }
    3958                 :        1224 :                         break;
    3959                 :             :                     }
    3960                 :             : 
    3961                 :             :                     /* In nonblocking mode, return rather than sleeping. */
    3962         [ +  + ]:       15856 :                     if (nonblocking)
    3963                 :         341 :                         return XLREAD_WOULDBLOCK;
    3964                 :             : 
    3965                 :             :                     /*
    3966                 :             :                      * Data not here yet. Check for trigger, then wait for
    3967                 :             :                      * walreceiver to wake us up when new WAL arrives.
    3968                 :             :                      */
    3969         [ +  + ]:       15515 :                     if (CheckForStandbyTrigger())
    3970                 :             :                     {
    3971                 :             :                         /*
    3972                 :             :                          * Note that we don't return XLREAD_FAIL immediately
    3973                 :             :                          * here. After being triggered, we still want to
    3974                 :             :                          * replay all the WAL that was already streamed. It's
    3975                 :             :                          * in pg_wal now, so we just treat this as a failure,
    3976                 :             :                          * and the state machine will move on to replay the
    3977                 :             :                          * streamed WAL from pg_wal, and then recheck the
    3978                 :             :                          * trigger and exit replay.
    3979                 :             :                          */
    3980                 :          38 :                         lastSourceFailed = true;
    3981                 :          38 :                         break;
    3982                 :             :                     }
    3983                 :             : 
    3984                 :             :                     /*
    3985                 :             :                      * Since we have replayed everything we have received so
    3986                 :             :                      * far and are about to start waiting for more WAL, let's
    3987                 :             :                      * tell the upstream server our replay location now so
    3988                 :             :                      * that pg_stat_replication doesn't show stale
    3989                 :             :                      * information.
    3990                 :             :                      */
    3991         [ +  + ]:       15477 :                     if (!streaming_reply_sent)
    3992                 :             :                     {
    3993                 :       13403 :                         WalRcvRequestApplyReply();
    3994                 :       13403 :                         streaming_reply_sent = true;
    3995                 :             :                     }
    3996                 :             : 
    3997                 :             :                     /* Do any background tasks that might benefit us later. */
    3998                 :       15477 :                     KnownAssignedTransactionIdsIdleMaintenance();
    3999                 :             : 
    4000                 :             :                     /* Update pg_stat_recovery_prefetch before sleeping. */
    4001                 :       15477 :                     XLogPrefetcherComputeStats(xlogprefetcher);
    4002                 :             : 
    4003                 :             :                     /*
    4004                 :             :                      * Wait for more WAL to arrive, when we will be woken
    4005                 :             :                      * immediately by the WAL receiver.
    4006                 :             :                      */
    4007                 :       15477 :                     (void) WaitLatch(&XLogRecoveryCtl->recoveryWakeupLatch,
    4008                 :             :                                      WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
    4009                 :             :                                      -1L,
    4010                 :             :                                      WAIT_EVENT_RECOVERY_WAL_STREAM);
    4011                 :       15477 :                     ResetLatch(&XLogRecoveryCtl->recoveryWakeupLatch);
    4012                 :       15477 :                     break;
    4013                 :             :                 }
    4014                 :             : 
    4015                 :           0 :             default:
    4016         [ #  # ]:           0 :                 elog(ERROR, "unexpected WAL source %d", currentSource);
    4017                 :             :         }
    4018                 :             : 
    4019                 :             :         /*
    4020                 :             :          * Check for recovery pause here so that we can confirm more quickly
    4021                 :             :          * that a requested pause has actually taken effect.
    4022                 :             :          */
    4023         [ +  + ]:       17127 :         if (((volatile XLogRecoveryCtlData *) XLogRecoveryCtl)->recoveryPauseState !=
    4024                 :             :             RECOVERY_NOT_PAUSED)
    4025                 :           7 :             recoveryPausesHere(false);
    4026                 :             : 
    4027                 :             :         /*
    4028                 :             :          * This possibly-long loop needs to handle interrupts of startup
    4029                 :             :          * process.
    4030                 :             :          */
    4031                 :       17127 :         ProcessStartupProcInterrupts();
    4032                 :             :     }
    4033                 :             : 
    4034                 :             :     return XLREAD_FAIL;         /* not reached */
    4035                 :             : }
    4036                 :             : 
    4037                 :             : 
    4038                 :             : /*
    4039                 :             :  * Determine what log level should be used to report a corrupt WAL record
    4040                 :             :  * in the current WAL page, previously read by XLogPageRead().
    4041                 :             :  *
    4042                 :             :  * 'emode' is the error mode that would be used to report a file-not-found
    4043                 :             :  * or legitimate end-of-WAL situation.   Generally, we use it as-is, but if
    4044                 :             :  * we're retrying the exact same record that we've tried previously, only
    4045                 :             :  * complain the first time to keep the noise down.  However, we only do when
    4046                 :             :  * reading from pg_wal, because we don't expect any invalid records in archive
    4047                 :             :  * or in records streamed from the primary. Files in the archive should be complete,
    4048                 :             :  * and we should never hit the end of WAL because we stop and wait for more WAL
    4049                 :             :  * to arrive before replaying it.
    4050                 :             :  *
    4051                 :             :  * NOTE: This function remembers the RecPtr value it was last called with,
    4052                 :             :  * to suppress repeated messages about the same record. Only call this when
    4053                 :             :  * you are about to ereport(), or you might cause a later message to be
    4054                 :             :  * erroneously suppressed.
    4055                 :             :  */
    4056                 :             : static int
    4057                 :         316 : emode_for_corrupt_record(int emode, XLogRecPtr RecPtr)
    4058                 :             : {
    4059                 :             :     static XLogRecPtr lastComplaint = InvalidXLogRecPtr;
    4060                 :             : 
    4061   [ +  +  +  - ]:         316 :     if (readSource == XLOG_FROM_PG_WAL && emode == LOG)
    4062                 :             :     {
    4063         [ +  + ]:         312 :         if (RecPtr == lastComplaint)
    4064                 :          85 :             emode = DEBUG1;
    4065                 :             :         else
    4066                 :         227 :             lastComplaint = RecPtr;
    4067                 :             :     }
    4068                 :         316 :     return emode;
    4069                 :             : }
    4070                 :             : 
    4071                 :             : 
    4072                 :             : /*
    4073                 :             :  * Subroutine to try to fetch and validate a prior checkpoint record.
    4074                 :             :  */
    4075                 :             : static XLogRecord *
    4076                 :        1104 : ReadCheckpointRecord(XLogPrefetcher *xlogprefetcher, XLogRecPtr RecPtr,
    4077                 :             :                      TimeLineID replayTLI)
    4078                 :             : {
    4079                 :             :     XLogRecord *record;
    4080                 :             :     uint8       info;
    4081                 :             : 
    4082                 :             :     Assert(xlogreader != NULL);
    4083                 :             : 
    4084         [ -  + ]:        1104 :     if (!XRecOffIsValid(RecPtr))
    4085                 :             :     {
    4086         [ #  # ]:           0 :         ereport(LOG,
    4087                 :             :                 (errmsg("invalid checkpoint location")));
    4088                 :           0 :         return NULL;
    4089                 :             :     }
    4090                 :             : 
    4091                 :        1104 :     XLogPrefetcherBeginRead(xlogprefetcher, RecPtr);
    4092                 :        1104 :     record = ReadRecord(xlogprefetcher, LOG, true, replayTLI);
    4093                 :             : 
    4094         [ +  + ]:        1104 :     if (record == NULL)
    4095                 :             :     {
    4096         [ +  - ]:           2 :         ereport(LOG,
    4097                 :             :                 (errmsg("invalid checkpoint record")));
    4098                 :           2 :         return NULL;
    4099                 :             :     }
    4100         [ -  + ]:        1102 :     if (record->xl_rmid != RM_XLOG_ID)
    4101                 :             :     {
    4102         [ #  # ]:           0 :         ereport(LOG,
    4103                 :             :                 (errmsg("invalid resource manager ID in checkpoint record")));
    4104                 :           0 :         return NULL;
    4105                 :             :     }
    4106                 :        1102 :     info = record->xl_info & ~XLR_INFO_MASK;
    4107   [ +  +  -  + ]:        1102 :     if (info != XLOG_CHECKPOINT_SHUTDOWN &&
    4108                 :             :         info != XLOG_CHECKPOINT_ONLINE)
    4109                 :             :     {
    4110         [ #  # ]:           0 :         ereport(LOG,
    4111                 :             :                 (errmsg("invalid xl_info in checkpoint record")));
    4112                 :           0 :         return NULL;
    4113                 :             :     }
    4114         [ -  + ]:        1102 :     if (record->xl_tot_len != SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(CheckPoint))
    4115                 :             :     {
    4116         [ #  # ]:           0 :         ereport(LOG,
    4117                 :             :                 (errmsg("invalid length of checkpoint record")));
    4118                 :           0 :         return NULL;
    4119                 :             :     }
    4120                 :        1102 :     return record;
    4121                 :             : }
    4122                 :             : 
    4123                 :             : /*
    4124                 :             :  * Scan for new timelines that might have appeared in the archive since we
    4125                 :             :  * started recovery.
    4126                 :             :  *
    4127                 :             :  * If there are any, the function changes recovery target TLI to the latest
    4128                 :             :  * one and returns 'true'.
    4129                 :             :  */
    4130                 :             : static bool
    4131                 :         223 : rescanLatestTimeLine(TimeLineID replayTLI, XLogRecPtr replayLSN)
    4132                 :             : {
    4133                 :             :     List       *newExpectedTLEs;
    4134                 :             :     bool        found;
    4135                 :             :     ListCell   *cell;
    4136                 :             :     TimeLineID  newtarget;
    4137                 :         223 :     TimeLineID  oldtarget = recoveryTargetTLI;
    4138                 :         223 :     TimeLineHistoryEntry *currentTle = NULL;
    4139                 :             : 
    4140                 :         223 :     newtarget = findNewestTimeLine(recoveryTargetTLI);
    4141         [ +  + ]:         223 :     if (newtarget == recoveryTargetTLI)
    4142                 :             :     {
    4143                 :             :         /* No new timelines found */
    4144                 :         214 :         return false;
    4145                 :             :     }
    4146                 :             : 
    4147                 :             :     /*
    4148                 :             :      * Determine the list of expected TLIs for the new TLI
    4149                 :             :      */
    4150                 :             : 
    4151                 :           9 :     newExpectedTLEs = readTimeLineHistory(newtarget);
    4152                 :             : 
    4153                 :             :     /*
    4154                 :             :      * If the current timeline is not part of the history of the new timeline,
    4155                 :             :      * we cannot proceed to it.
    4156                 :             :      */
    4157                 :           9 :     found = false;
    4158   [ +  -  +  -  :          18 :     foreach(cell, newExpectedTLEs)
                   +  - ]
    4159                 :             :     {
    4160                 :          18 :         currentTle = (TimeLineHistoryEntry *) lfirst(cell);
    4161                 :             : 
    4162         [ +  + ]:          18 :         if (currentTle->tli == recoveryTargetTLI)
    4163                 :             :         {
    4164                 :           9 :             found = true;
    4165                 :           9 :             break;
    4166                 :             :         }
    4167                 :             :     }
    4168         [ -  + ]:           9 :     if (!found)
    4169                 :             :     {
    4170         [ #  # ]:           0 :         ereport(LOG,
    4171                 :             :                 (errmsg("new timeline %u is not a child of database system timeline %u",
    4172                 :             :                         newtarget,
    4173                 :             :                         replayTLI)));
    4174                 :           0 :         return false;
    4175                 :             :     }
    4176                 :             : 
    4177                 :             :     /*
    4178                 :             :      * The current timeline was found in the history file, but check that the
    4179                 :             :      * next timeline was forked off from it *after* the current recovery
    4180                 :             :      * location.
    4181                 :             :      */
    4182         [ -  + ]:           9 :     if (currentTle->end < replayLSN)
    4183                 :             :     {
    4184         [ #  # ]:           0 :         ereport(LOG,
    4185                 :             :                 errmsg("new timeline %u forked off current database system timeline %u before current recovery point %X/%08X",
    4186                 :             :                        newtarget,
    4187                 :             :                        replayTLI,
    4188                 :             :                        LSN_FORMAT_ARGS(replayLSN)));
    4189                 :           0 :         return false;
    4190                 :             :     }
    4191                 :             : 
    4192                 :             :     /* The new timeline history seems valid. Switch target */
    4193                 :           9 :     recoveryTargetTLI = newtarget;
    4194                 :           9 :     list_free_deep(expectedTLEs);
    4195                 :           9 :     expectedTLEs = newExpectedTLEs;
    4196                 :             : 
    4197                 :             :     /*
    4198                 :             :      * As in StartupXLOG(), try to ensure we have all the history files
    4199                 :             :      * between the old target and new target in pg_wal.
    4200                 :             :      */
    4201                 :           9 :     restoreTimeLineHistoryFiles(oldtarget + 1, newtarget);
    4202                 :             : 
    4203         [ +  - ]:           9 :     ereport(LOG,
    4204                 :             :             (errmsg("new target timeline is %u",
    4205                 :             :                     recoveryTargetTLI)));
    4206                 :             : 
    4207                 :           9 :     return true;
    4208                 :             : }
    4209                 :             : 
    4210                 :             : 
    4211                 :             : /*
    4212                 :             :  * Open a logfile segment for reading (during recovery).
    4213                 :             :  *
    4214                 :             :  * If source == XLOG_FROM_ARCHIVE, the segment is retrieved from archive.
    4215                 :             :  * Otherwise, it's assumed to be already available in pg_wal.
    4216                 :             :  */
    4217                 :             : static int
    4218                 :        3819 : XLogFileRead(XLogSegNo segno, TimeLineID tli,
    4219                 :             :              XLogSource source, bool notfoundOk)
    4220                 :             : {
    4221                 :             :     char        xlogfname[MAXFNAMELEN];
    4222                 :             :     char        activitymsg[MAXFNAMELEN + 16];
    4223                 :             :     char        path[MAXPGPATH];
    4224                 :             :     int         fd;
    4225                 :             : 
    4226                 :        3819 :     XLogFileName(xlogfname, tli, segno, wal_segment_size);
    4227                 :             : 
    4228      [ +  +  - ]:        3819 :     switch (source)
    4229                 :             :     {
    4230                 :         941 :         case XLOG_FROM_ARCHIVE:
    4231                 :             :             /* Report recovery progress in PS display */
    4232                 :         941 :             snprintf(activitymsg, sizeof(activitymsg), "waiting for %s",
    4233                 :             :                      xlogfname);
    4234                 :         941 :             set_ps_display(activitymsg);
    4235                 :             : 
    4236         [ +  + ]:         941 :             if (!RestoreArchivedFile(path, xlogfname,
    4237                 :             :                                      "RECOVERYXLOG",
    4238                 :             :                                      wal_segment_size,
    4239                 :             :                                      InRedo))
    4240                 :         574 :                 return -1;
    4241                 :         366 :             break;
    4242                 :             : 
    4243                 :        2878 :         case XLOG_FROM_PG_WAL:
    4244                 :             :         case XLOG_FROM_STREAM:
    4245                 :        2878 :             XLogFilePath(path, tli, segno, wal_segment_size);
    4246                 :        2878 :             break;
    4247                 :             : 
    4248                 :           0 :         default:
    4249         [ #  # ]:           0 :             elog(ERROR, "invalid XLogFileRead source %d", source);
    4250                 :             :     }
    4251                 :             : 
    4252                 :             :     /*
    4253                 :             :      * If the segment was fetched from archival storage, replace the existing
    4254                 :             :      * xlog segment (if any) with the archival version.
    4255                 :             :      */
    4256         [ +  + ]:        3244 :     if (source == XLOG_FROM_ARCHIVE)
    4257                 :             :     {
    4258                 :             :         Assert(!IsInstallXLogFileSegmentActive());
    4259                 :         366 :         KeepFileRestoredFromArchive(path, xlogfname);
    4260                 :             : 
    4261                 :             :         /*
    4262                 :             :          * Set path to point at the new file in pg_wal.
    4263                 :             :          */
    4264                 :         366 :         snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
    4265                 :             :     }
    4266                 :             : 
    4267                 :        3244 :     fd = BasicOpenFile(path, O_RDONLY | PG_BINARY);
    4268         [ +  + ]:        3244 :     if (fd >= 0)
    4269                 :             :     {
    4270                 :             :         /* Success! */
    4271                 :        3019 :         curFileTLI = tli;
    4272                 :             : 
    4273                 :             :         /* Report recovery progress in PS display */
    4274                 :        3019 :         snprintf(activitymsg, sizeof(activitymsg), "recovering %s",
    4275                 :             :                  xlogfname);
    4276                 :        3019 :         set_ps_display(activitymsg);
    4277                 :             : 
    4278                 :             :         /* Track source of data in assorted state variables */
    4279                 :        3019 :         readSource = source;
    4280                 :        3019 :         XLogReceiptSource = source;
    4281                 :             :         /* In FROM_STREAM case, caller tracks receipt time, not me */
    4282         [ +  + ]:        3019 :         if (source != XLOG_FROM_STREAM)
    4283                 :        1795 :             XLogReceiptTime = GetCurrentTimestamp();
    4284                 :             : 
    4285                 :        3019 :         return fd;
    4286                 :             :     }
    4287   [ +  -  -  + ]:         225 :     if (errno != ENOENT || !notfoundOk) /* unexpected failure? */
    4288         [ #  # ]:           0 :         ereport(PANIC,
    4289                 :             :                 (errcode_for_file_access(),
    4290                 :             :                  errmsg("could not open file \"%s\": %m", path)));
    4291                 :         225 :     return -1;
    4292                 :             : }
    4293                 :             : 
    4294                 :             : /*
    4295                 :             :  * Open a logfile segment for reading (during recovery).
    4296                 :             :  *
    4297                 :             :  * This version searches for the segment with any TLI listed in expectedTLEs.
    4298                 :             :  */
    4299                 :             : static int
    4300                 :        2007 : XLogFileReadAnyTLI(XLogSegNo segno, XLogSource source)
    4301                 :             : {
    4302                 :             :     char        path[MAXPGPATH];
    4303                 :             :     ListCell   *cell;
    4304                 :             :     int         fd;
    4305                 :             :     List       *tles;
    4306                 :             : 
    4307                 :             :     /*
    4308                 :             :      * Loop looking for a suitable timeline ID: we might need to read any of
    4309                 :             :      * the timelines listed in expectedTLEs.
    4310                 :             :      *
    4311                 :             :      * We expect curFileTLI on entry to be the TLI of the preceding file in
    4312                 :             :      * sequence, or 0 if there was no predecessor.  We do not allow curFileTLI
    4313                 :             :      * to go backwards; this prevents us from picking up the wrong file when a
    4314                 :             :      * parent timeline extends to higher segment numbers than the child we
    4315                 :             :      * want to read.
    4316                 :             :      *
    4317                 :             :      * If we haven't read the timeline history file yet, read it now, so that
    4318                 :             :      * we know which TLIs to scan.  We don't save the list in expectedTLEs,
    4319                 :             :      * however, unless we actually find a valid segment.  That way if there is
    4320                 :             :      * neither a timeline history file nor a WAL segment in the archive, and
    4321                 :             :      * streaming replication is set up, we'll read the timeline history file
    4322                 :             :      * streamed from the primary when we start streaming, instead of
    4323                 :             :      * recovering with a dummy history generated here.
    4324                 :             :      */
    4325         [ +  + ]:        2007 :     if (expectedTLEs)
    4326                 :         903 :         tles = expectedTLEs;
    4327                 :             :     else
    4328                 :        1104 :         tles = readTimeLineHistory(recoveryTargetTLI);
    4329                 :             : 
    4330   [ +  -  +  +  :        2240 :     foreach(cell, tles)
                   +  + ]
    4331                 :             :     {
    4332                 :        2035 :         TimeLineHistoryEntry *hent = (TimeLineHistoryEntry *) lfirst(cell);
    4333                 :        2035 :         TimeLineID  tli = hent->tli;
    4334                 :             : 
    4335         [ +  + ]:        2035 :         if (tli < curFileTLI)
    4336                 :           6 :             break;              /* don't bother looking at too-old TLIs */
    4337                 :             : 
    4338                 :             :         /*
    4339                 :             :          * Skip scanning the timeline ID that the logfile segment to read
    4340                 :             :          * doesn't belong to
    4341                 :             :          */
    4342         [ +  + ]:        2029 :         if (XLogRecPtrIsValid(hent->begin))
    4343                 :             :         {
    4344                 :          87 :             XLogSegNo   beginseg = 0;
    4345                 :             : 
    4346                 :          87 :             XLByteToSeg(hent->begin, beginseg, wal_segment_size);
    4347                 :             : 
    4348                 :             :             /*
    4349                 :             :              * The logfile segment that doesn't belong to the timeline is
    4350                 :             :              * older or newer than the segment that the timeline started or
    4351                 :             :              * ended at, respectively. It's sufficient to check only the
    4352                 :             :              * starting segment of the timeline here. Since the timelines are
    4353                 :             :              * scanned in descending order in this loop, any segments newer
    4354                 :             :              * than the ending segment should belong to newer timeline and
    4355                 :             :              * have already been read before. So it's not necessary to check
    4356                 :             :              * the ending segment of the timeline here.
    4357                 :             :              */
    4358         [ +  + ]:          87 :             if (segno < beginseg)
    4359                 :           8 :                 continue;
    4360                 :             :         }
    4361                 :             : 
    4362   [ +  +  -  + ]:        2021 :         if (source == XLOG_FROM_ANY || source == XLOG_FROM_ARCHIVE)
    4363                 :             :         {
    4364                 :         941 :             fd = XLogFileRead(segno, tli, XLOG_FROM_ARCHIVE, true);
    4365         [ +  + ]:         940 :             if (fd != -1)
    4366                 :             :             {
    4367         [ -  + ]:         366 :                 elog(DEBUG1, "got WAL segment from archive");
    4368         [ +  + ]:         366 :                 if (!expectedTLEs)
    4369                 :          20 :                     expectedTLEs = tles;
    4370                 :        1795 :                 return fd;
    4371                 :             :             }
    4372                 :             :         }
    4373                 :             : 
    4374   [ +  +  +  - ]:        1654 :         if (source == XLOG_FROM_ANY || source == XLOG_FROM_PG_WAL)
    4375                 :             :         {
    4376                 :        1654 :             fd = XLogFileRead(segno, tli, XLOG_FROM_PG_WAL, true);
    4377         [ +  + ]:        1654 :             if (fd != -1)
    4378                 :             :             {
    4379         [ +  + ]:        1429 :                 if (!expectedTLEs)
    4380                 :        1081 :                     expectedTLEs = tles;
    4381                 :        1429 :                 return fd;
    4382                 :             :             }
    4383                 :             :         }
    4384                 :             :     }
    4385                 :             : 
    4386                 :             :     /* Couldn't find it.  For simplicity, complain about front timeline */
    4387                 :         211 :     XLogFilePath(path, recoveryTargetTLI, segno, wal_segment_size);
    4388                 :         211 :     errno = ENOENT;
    4389         [ +  + ]:         211 :     ereport(DEBUG2,
    4390                 :             :             (errcode_for_file_access(),
    4391                 :             :              errmsg("could not open file \"%s\": %m", path)));
    4392                 :         211 :     return -1;
    4393                 :             : }
    4394                 :             : 
    4395                 :             : /*
    4396                 :             :  * Set flag to signal the walreceiver to restart.  (The startup process calls
    4397                 :             :  * this on noticing a relevant configuration change.)
    4398                 :             :  */
    4399                 :             : void
    4400                 :          13 : StartupRequestWalReceiverRestart(void)
    4401                 :             : {
    4402   [ +  -  +  + ]:          13 :     if (currentSource == XLOG_FROM_STREAM && WalRcvRunning())
    4403                 :             :     {
    4404         [ +  - ]:           8 :         ereport(LOG,
    4405                 :             :                 (errmsg("WAL receiver process shutdown requested")));
    4406                 :             : 
    4407                 :           8 :         pendingWalRcvRestart = true;
    4408                 :             :     }
    4409                 :          13 : }
    4410                 :             : 
    4411                 :             : 
    4412                 :             : /*
    4413                 :             :  * Has a standby promotion already been triggered?
    4414                 :             :  *
    4415                 :             :  * Unlike CheckForStandbyTrigger(), this works in any process
    4416                 :             :  * that's connected to shared memory.
    4417                 :             :  */
    4418                 :             : bool
    4419                 :          87 : PromoteIsTriggered(void)
    4420                 :             : {
    4421                 :             :     /*
    4422                 :             :      * We check shared state each time only until a standby promotion is
    4423                 :             :      * triggered. We can't trigger a promotion again, so there's no need to
    4424                 :             :      * keep checking after the shared variable has once been seen true.
    4425                 :             :      */
    4426         [ +  + ]:          87 :     if (LocalPromoteIsTriggered)
    4427                 :          58 :         return true;
    4428                 :             : 
    4429                 :          29 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4430                 :          29 :     LocalPromoteIsTriggered = XLogRecoveryCtl->SharedPromoteIsTriggered;
    4431                 :          29 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4432                 :             : 
    4433                 :          29 :     return LocalPromoteIsTriggered;
    4434                 :             : }
    4435                 :             : 
    4436                 :             : static void
    4437                 :          55 : SetPromoteIsTriggered(void)
    4438                 :             : {
    4439                 :          55 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4440                 :          55 :     XLogRecoveryCtl->SharedPromoteIsTriggered = true;
    4441                 :          55 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4442                 :             : 
    4443                 :             :     /*
    4444                 :             :      * Mark the recovery pause state as 'not paused' because the paused state
    4445                 :             :      * ends and promotion continues if a promotion is triggered while recovery
    4446                 :             :      * is paused. Otherwise pg_get_wal_replay_pause_state() can mistakenly
    4447                 :             :      * return 'paused' while a promotion is ongoing.
    4448                 :             :      */
    4449                 :          55 :     SetRecoveryPause(false);
    4450                 :             : 
    4451                 :          55 :     LocalPromoteIsTriggered = true;
    4452                 :          55 : }
    4453                 :             : 
    4454                 :             : /*
    4455                 :             :  * Check whether a promote request has arrived.
    4456                 :             :  */
    4457                 :             : static bool
    4458                 :       16097 : CheckForStandbyTrigger(void)
    4459                 :             : {
    4460         [ +  + ]:       16097 :     if (LocalPromoteIsTriggered)
    4461                 :          55 :         return true;
    4462                 :             : 
    4463   [ +  +  +  - ]:       16042 :     if (IsPromoteSignaled() && CheckPromoteSignal())
    4464                 :             :     {
    4465         [ +  - ]:          55 :         ereport(LOG, (errmsg("received promote request")));
    4466                 :          55 :         RemovePromoteSignalFiles();
    4467                 :          55 :         ResetPromoteSignaled();
    4468                 :          55 :         SetPromoteIsTriggered();
    4469                 :          55 :         return true;
    4470                 :             :     }
    4471                 :             : 
    4472                 :       15987 :     return false;
    4473                 :             : }
    4474                 :             : 
    4475                 :             : /*
    4476                 :             :  * Remove the files signaling a standby promotion request.
    4477                 :             :  */
    4478                 :             : void
    4479                 :        1071 : RemovePromoteSignalFiles(void)
    4480                 :             : {
    4481                 :        1071 :     unlink(PROMOTE_SIGNAL_FILE);
    4482                 :        1071 : }
    4483                 :             : 
    4484                 :             : /*
    4485                 :             :  * Check to see if a promote request has arrived.
    4486                 :             :  */
    4487                 :             : bool
    4488                 :         779 : CheckPromoteSignal(void)
    4489                 :             : {
    4490                 :             :     struct stat stat_buf;
    4491                 :             : 
    4492         [ +  + ]:         779 :     if (stat(PROMOTE_SIGNAL_FILE, &stat_buf) == 0)
    4493                 :         110 :         return true;
    4494                 :             : 
    4495                 :         669 :     return false;
    4496                 :             : }
    4497                 :             : 
    4498                 :             : /*
    4499                 :             :  * Wake up startup process to replay newly arrived WAL, or to notice that
    4500                 :             :  * failover has been requested.
    4501                 :             :  */
    4502                 :             : void
    4503                 :       44808 : WakeupRecovery(void)
    4504                 :             : {
    4505                 :       44808 :     SetLatch(&XLogRecoveryCtl->recoveryWakeupLatch);
    4506                 :       44808 : }
    4507                 :             : 
    4508                 :             : /*
    4509                 :             :  * Schedule a walreceiver wakeup in the main recovery loop.
    4510                 :             :  */
    4511                 :             : void
    4512                 :           2 : XLogRequestWalReceiverReply(void)
    4513                 :             : {
    4514                 :           2 :     doRequestWalReceiverReply = true;
    4515                 :           2 : }
    4516                 :             : 
    4517                 :             : /*
    4518                 :             :  * Is HotStandby active yet? This is only important in special backends
    4519                 :             :  * since normal backends won't ever be able to connect until this returns
    4520                 :             :  * true. Postmaster knows this by way of signal, not via shared memory.
    4521                 :             :  *
    4522                 :             :  * Unlike testing standbyState, this works in any process that's connected to
    4523                 :             :  * shared memory.  (And note that standbyState alone doesn't tell the truth
    4524                 :             :  * anyway.)
    4525                 :             :  */
    4526                 :             : bool
    4527                 :         187 : HotStandbyActive(void)
    4528                 :             : {
    4529                 :             :     /*
    4530                 :             :      * We check shared state each time only until Hot Standby is active. We
    4531                 :             :      * can't de-activate Hot Standby, so there's no need to keep checking
    4532                 :             :      * after the shared variable has once been seen true.
    4533                 :             :      */
    4534         [ +  + ]:         187 :     if (LocalHotStandbyActive)
    4535                 :          26 :         return true;
    4536                 :             :     else
    4537                 :             :     {
    4538                 :             :         /* spinlock is essential on machines with weak memory ordering! */
    4539                 :         161 :         SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4540                 :         161 :         LocalHotStandbyActive = XLogRecoveryCtl->SharedHotStandbyActive;
    4541                 :         161 :         SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4542                 :             : 
    4543                 :         161 :         return LocalHotStandbyActive;
    4544                 :             :     }
    4545                 :             : }
    4546                 :             : 
    4547                 :             : /*
    4548                 :             :  * Like HotStandbyActive(), but to be used only in WAL replay code,
    4549                 :             :  * where we don't need to ask any other process what the state is.
    4550                 :             :  */
    4551                 :             : static bool
    4552                 :           0 : HotStandbyActiveInReplay(void)
    4553                 :             : {
    4554                 :             :     Assert(AmStartupProcess() || !IsPostmasterEnvironment);
    4555                 :           0 :     return LocalHotStandbyActive;
    4556                 :             : }
    4557                 :             : 
    4558                 :             : /*
    4559                 :             :  * Get latest redo apply position.
    4560                 :             :  *
    4561                 :             :  * Exported to allow WALReceiver to read the pointer directly.
    4562                 :             :  */
    4563                 :             : XLogRecPtr
    4564                 :      114452 : GetXLogReplayRecPtr(TimeLineID *replayTLI)
    4565                 :             : {
    4566                 :             :     XLogRecPtr  recptr;
    4567                 :             :     TimeLineID  tli;
    4568                 :             : 
    4569                 :      114452 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4570                 :      114452 :     recptr = XLogRecoveryCtl->lastReplayedEndRecPtr;
    4571                 :      114452 :     tli = XLogRecoveryCtl->lastReplayedTLI;
    4572                 :      114452 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4573                 :             : 
    4574         [ +  + ]:      114452 :     if (replayTLI)
    4575                 :        3380 :         *replayTLI = tli;
    4576                 :      114452 :     return recptr;
    4577                 :             : }
    4578                 :             : 
    4579                 :             : 
    4580                 :             : /*
    4581                 :             :  * Get position of last applied, or the record being applied.
    4582                 :             :  *
    4583                 :             :  * This is different from GetXLogReplayRecPtr() in that if a WAL
    4584                 :             :  * record is currently being applied, this includes that record.
    4585                 :             :  */
    4586                 :             : XLogRecPtr
    4587                 :        6894 : GetCurrentReplayRecPtr(TimeLineID *replayEndTLI)
    4588                 :             : {
    4589                 :             :     XLogRecPtr  recptr;
    4590                 :             :     TimeLineID  tli;
    4591                 :             : 
    4592                 :        6894 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4593                 :        6894 :     recptr = XLogRecoveryCtl->replayEndRecPtr;
    4594                 :        6894 :     tli = XLogRecoveryCtl->replayEndTLI;
    4595                 :        6894 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4596                 :             : 
    4597         [ +  - ]:        6894 :     if (replayEndTLI)
    4598                 :        6894 :         *replayEndTLI = tli;
    4599                 :        6894 :     return recptr;
    4600                 :             : }
    4601                 :             : 
    4602                 :             : /*
    4603                 :             :  * Save timestamp of latest processed commit/abort record.
    4604                 :             :  *
    4605                 :             :  * We keep this in XLogRecoveryCtl, not a simple static variable, so that it can be
    4606                 :             :  * seen by processes other than the startup process.  Note in particular
    4607                 :             :  * that CreateRestartPoint is executed in the checkpointer.
    4608                 :             :  */
    4609                 :             : static void
    4610                 :       24222 : SetLatestXTime(TimestampTz xtime)
    4611                 :             : {
    4612                 :       24222 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4613                 :       24222 :     XLogRecoveryCtl->recoveryLastXTime = xtime;
    4614                 :       24222 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4615                 :       24222 : }
    4616                 :             : 
    4617                 :             : /*
    4618                 :             :  * Fetch timestamp of latest processed commit/abort record.
    4619                 :             :  */
    4620                 :             : TimestampTz
    4621                 :         380 : GetLatestXTime(void)
    4622                 :             : {
    4623                 :             :     TimestampTz xtime;
    4624                 :             : 
    4625                 :         380 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4626                 :         380 :     xtime = XLogRecoveryCtl->recoveryLastXTime;
    4627                 :         380 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4628                 :             : 
    4629                 :         380 :     return xtime;
    4630                 :             : }
    4631                 :             : 
    4632                 :             : /*
    4633                 :             :  * Save timestamp of the next chunk of WAL records to apply.
    4634                 :             :  *
    4635                 :             :  * We keep this in XLogRecoveryCtl, not a simple static variable, so that it can be
    4636                 :             :  * seen by all backends.
    4637                 :             :  */
    4638                 :             : static void
    4639                 :       12771 : SetCurrentChunkStartTime(TimestampTz xtime)
    4640                 :             : {
    4641                 :       12771 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4642                 :       12771 :     XLogRecoveryCtl->currentChunkStartTime = xtime;
    4643                 :       12771 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4644                 :       12771 : }
    4645                 :             : 
    4646                 :             : /*
    4647                 :             :  * Fetch timestamp of latest processed commit/abort record.
    4648                 :             :  * Startup process maintains an accurate local copy in XLogReceiptTime
    4649                 :             :  */
    4650                 :             : TimestampTz
    4651                 :         277 : GetCurrentChunkReplayStartTime(void)
    4652                 :             : {
    4653                 :             :     TimestampTz xtime;
    4654                 :             : 
    4655                 :         277 :     SpinLockAcquire(&XLogRecoveryCtl->info_lck);
    4656                 :         277 :     xtime = XLogRecoveryCtl->currentChunkStartTime;
    4657                 :         277 :     SpinLockRelease(&XLogRecoveryCtl->info_lck);
    4658                 :             : 
    4659                 :         277 :     return xtime;
    4660                 :             : }
    4661                 :             : 
    4662                 :             : /*
    4663                 :             :  * Returns time of receipt of current chunk of XLOG data, as well as
    4664                 :             :  * whether it was received from streaming replication or from archives.
    4665                 :             :  */
    4666                 :             : void
    4667                 :          30 : GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream)
    4668                 :             : {
    4669                 :             :     /*
    4670                 :             :      * This must be executed in the startup process, since we don't export the
    4671                 :             :      * relevant state to shared memory.
    4672                 :             :      */
    4673                 :             :     Assert(InRecovery);
    4674                 :             : 
    4675                 :          30 :     *rtime = XLogReceiptTime;
    4676                 :          30 :     *fromStream = (XLogReceiptSource == XLOG_FROM_STREAM);
    4677                 :          30 : }
    4678                 :             : 
    4679                 :             : /*
    4680                 :             :  * Note that text field supplied is a parameter name and does not require
    4681                 :             :  * translation
    4682                 :             :  */
    4683                 :             : void
    4684                 :         760 : RecoveryRequiresIntParameter(const char *param_name, int currValue, int minValue)
    4685                 :             : {
    4686         [ -  + ]:         760 :     if (currValue < minValue)
    4687                 :             :     {
    4688         [ #  # ]:           0 :         if (HotStandbyActiveInReplay())
    4689                 :             :         {
    4690                 :           0 :             bool        warned_for_promote = false;
    4691                 :             : 
    4692         [ #  # ]:           0 :             ereport(WARNING,
    4693                 :             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4694                 :             :                      errmsg("hot standby is not possible because of insufficient parameter settings"),
    4695                 :             :                      errdetail("%s = %d is a lower setting than on the primary server, where its value was %d.",
    4696                 :             :                                param_name,
    4697                 :             :                                currValue,
    4698                 :             :                                minValue)));
    4699                 :             : 
    4700                 :           0 :             SetRecoveryPause(true);
    4701                 :             : 
    4702         [ #  # ]:           0 :             ereport(LOG,
    4703                 :             :                     (errmsg("recovery has paused"),
    4704                 :             :                      errdetail("If recovery is unpaused, the server will shut down."),
    4705                 :             :                      errhint("You can then restart the server after making the necessary configuration changes.")));
    4706                 :             : 
    4707         [ #  # ]:           0 :             while (GetRecoveryPauseState() != RECOVERY_NOT_PAUSED)
    4708                 :             :             {
    4709                 :           0 :                 ProcessStartupProcInterrupts();
    4710                 :             : 
    4711         [ #  # ]:           0 :                 if (CheckForStandbyTrigger())
    4712                 :             :                 {
    4713         [ #  # ]:           0 :                     if (!warned_for_promote)
    4714         [ #  # ]:           0 :                         ereport(WARNING,
    4715                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4716                 :             :                                  errmsg("promotion is not possible because of insufficient parameter settings"),
    4717                 :             : 
    4718                 :             :                         /*
    4719                 :             :                          * Repeat the detail from above so it's easy to find
    4720                 :             :                          * in the log.
    4721                 :             :                          */
    4722                 :             :                                  errdetail("%s = %d is a lower setting than on the primary server, where its value was %d.",
    4723                 :             :                                            param_name,
    4724                 :             :                                            currValue,
    4725                 :             :                                            minValue),
    4726                 :             :                                  errhint("Restart the server after making the necessary configuration changes.")));
    4727                 :           0 :                     warned_for_promote = true;
    4728                 :             :                 }
    4729                 :             : 
    4730                 :             :                 /*
    4731                 :             :                  * If recovery pause is requested then set it paused.  While
    4732                 :             :                  * we are in the loop, user might resume and pause again so
    4733                 :             :                  * set this every time.
    4734                 :             :                  */
    4735                 :           0 :                 ConfirmRecoveryPaused();
    4736                 :             : 
    4737                 :             :                 /*
    4738                 :             :                  * We wait on a condition variable that will wake us as soon
    4739                 :             :                  * as the pause ends, but we use a timeout so we can check the
    4740                 :             :                  * above conditions periodically too.
    4741                 :             :                  */
    4742                 :           0 :                 ConditionVariableTimedSleep(&XLogRecoveryCtl->recoveryNotPausedCV, 1000,
    4743                 :             :                                             WAIT_EVENT_RECOVERY_PAUSE);
    4744                 :             :             }
    4745                 :           0 :             ConditionVariableCancelSleep();
    4746                 :             :         }
    4747                 :             : 
    4748         [ #  # ]:           0 :         ereport(FATAL,
    4749                 :             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4750                 :             :                  errmsg("recovery aborted because of insufficient parameter settings"),
    4751                 :             :         /* Repeat the detail from above so it's easy to find in the log. */
    4752                 :             :                  errdetail("%s = %d is a lower setting than on the primary server, where its value was %d.",
    4753                 :             :                            param_name,
    4754                 :             :                            currValue,
    4755                 :             :                            minValue),
    4756                 :             :                  errhint("You can restart the server after making the necessary configuration changes.")));
    4757                 :             :     }
    4758                 :         760 : }
    4759                 :             : 
    4760                 :             : 
    4761                 :             : /*
    4762                 :             :  * GUC check_hook for primary_slot_name
    4763                 :             :  */
    4764                 :             : bool
    4765                 :        1504 : check_primary_slot_name(char **newval, void **extra, GucSource source)
    4766                 :             : {
    4767                 :             :     int         err_code;
    4768                 :        1504 :     char       *err_msg = NULL;
    4769                 :        1504 :     char       *err_hint = NULL;
    4770                 :             : 
    4771   [ +  -  +  + ]:        1504 :     if (*newval && strcmp(*newval, "") != 0 &&
    4772         [ -  + ]:         191 :         !ReplicationSlotValidateNameInternal(*newval, false, &err_code,
    4773                 :             :                                              &err_msg, &err_hint))
    4774                 :             :     {
    4775                 :           0 :         GUC_check_errcode(err_code);
    4776                 :           0 :         GUC_check_errdetail("%s", err_msg);
    4777         [ #  # ]:           0 :         if (err_hint != NULL)
    4778                 :           0 :             GUC_check_errhint("%s", err_hint);
    4779                 :           0 :         return false;
    4780                 :             :     }
    4781                 :             : 
    4782                 :        1504 :     return true;
    4783                 :             : }
    4784                 :             : 
    4785                 :             : /*
    4786                 :             :  * Return the recovery target derived from the recovery_target* settings,
    4787                 :             :  * raising an error if more than one of them is set.
    4788                 :             :  */
    4789                 :             : static RecoveryTargetType
    4790                 :        1106 : DetermineRecoveryTargetType(void)
    4791                 :             : {
    4792                 :        1106 :     int         ntargets = 0;
    4793                 :        1106 :     RecoveryTargetType target = RECOVERY_TARGET_UNSET;
    4794                 :             :     const char *val;
    4795                 :             :     StringInfoData buf;
    4796                 :             : 
    4797                 :        1106 :     initStringInfo(&buf);
    4798                 :             : 
    4799                 :             : #define ADD_TARGET_IF_SET(gucname, kind) \
    4800                 :             :     do { \
    4801                 :             :         val = GetConfigOption(gucname, false, false); \
    4802                 :             :         if (val[0] != '\0') \
    4803                 :             :         { \
    4804                 :             :             ntargets++; \
    4805                 :             :             target = (kind); \
    4806                 :             :             if (buf.len == 0) \
    4807                 :             :                 appendStringInfo(&buf, _("\"%s\""), gucname); \
    4808                 :             :             else \
    4809                 :             :                 appendStringInfo(&buf, _(", \"%s\""), gucname); \
    4810                 :             :         } \
    4811                 :             :     } while (0)
    4812                 :             : 
    4813   [ +  +  +  - ]:        1106 :     ADD_TARGET_IF_SET("recovery_target", RECOVERY_TARGET_IMMEDIATE);
    4814   [ +  +  +  - ]:        1106 :     ADD_TARGET_IF_SET("recovery_target_lsn", RECOVERY_TARGET_LSN);
    4815   [ +  +  +  - ]:        1106 :     ADD_TARGET_IF_SET("recovery_target_name", RECOVERY_TARGET_NAME);
    4816   [ +  +  -  + ]:        1106 :     ADD_TARGET_IF_SET("recovery_target_time", RECOVERY_TARGET_TIME);
    4817   [ +  +  -  + ]:        1106 :     ADD_TARGET_IF_SET("recovery_target_xid", RECOVERY_TARGET_XID);
    4818                 :             : #undef ADD_TARGET_IF_SET
    4819                 :             : 
    4820         [ +  + ]:        1106 :     if (ntargets > 1)
    4821         [ +  - ]:           2 :         ereport(FATAL,
    4822                 :             :                 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4823                 :             :                 errmsg("cannot specify more than one recovery target"),
    4824                 :             :                 errdetail("Parameters set are: %s.",
    4825                 :             :                           buf.data));
    4826                 :             : 
    4827                 :        1104 :     pfree(buf.data);
    4828                 :             : 
    4829                 :        1104 :     return target;
    4830                 :             : }
    4831                 :             : 
    4832                 :             : /*
    4833                 :             :  * GUC check_hook for recovery_target
    4834                 :             :  */
    4835                 :             : bool
    4836                 :        1314 : check_recovery_target(char **newval, void **extra, GucSource source)
    4837                 :             : {
    4838   [ +  +  -  + ]:        1314 :     if (strcmp(*newval, "immediate") != 0 && strcmp(*newval, "") != 0)
    4839                 :             :     {
    4840                 :           0 :         GUC_check_errdetail("The only allowed value is \"immediate\".");
    4841                 :           0 :         return false;
    4842                 :             :     }
    4843                 :        1314 :     return true;
    4844                 :             : }
    4845                 :             : 
    4846                 :             : /*
    4847                 :             :  * GUC check_hook for recovery_target_lsn
    4848                 :             :  */
    4849                 :             : bool
    4850                 :        1319 : check_recovery_target_lsn(char **newval, void **extra, GucSource source)
    4851                 :             : {
    4852         [ +  + ]:        1319 :     if (strcmp(*newval, "") != 0)
    4853                 :             :     {
    4854                 :             :         XLogRecPtr  lsn;
    4855                 :             :         XLogRecPtr *myextra;
    4856                 :           8 :         ErrorSaveContext escontext = {T_ErrorSaveContext};
    4857                 :             : 
    4858                 :           8 :         lsn = pg_lsn_in_safe(*newval, (Node *) &escontext);
    4859         [ -  + ]:           8 :         if (escontext.error_occurred)
    4860                 :           0 :             return false;
    4861                 :             : 
    4862                 :           8 :         myextra = (XLogRecPtr *) guc_malloc(LOG, sizeof(XLogRecPtr));
    4863         [ -  + ]:           8 :         if (!myextra)
    4864                 :           0 :             return false;
    4865                 :           8 :         *myextra = lsn;
    4866                 :           8 :         *extra = myextra;
    4867                 :             :     }
    4868                 :        1319 :     return true;
    4869                 :             : }
    4870                 :             : 
    4871                 :             : /*
    4872                 :             :  * GUC assign_hook for recovery_target_lsn
    4873                 :             :  */
    4874                 :             : void
    4875                 :        1319 : assign_recovery_target_lsn(const char *newval, void *extra)
    4876                 :             : {
    4877   [ +  -  +  + ]:        1319 :     if (newval && strcmp(newval, "") != 0)
    4878                 :           8 :         recoveryTargetLSN = *((XLogRecPtr *) extra);
    4879                 :        1319 : }
    4880                 :             : 
    4881                 :             : /*
    4882                 :             :  * GUC check_hook for recovery_target_name
    4883                 :             :  */
    4884                 :             : bool
    4885                 :        1321 : check_recovery_target_name(char **newval, void **extra, GucSource source)
    4886                 :             : {
    4887                 :             :     /* Use the value of newval directly */
    4888         [ -  + ]:        1321 :     if (strlen(*newval) >= MAXFNAMELEN)
    4889                 :             :     {
    4890                 :           0 :         GUC_check_errdetail("\"%s\" is too long (maximum %d characters).",
    4891                 :             :                             "recovery_target_name", MAXFNAMELEN - 1);
    4892                 :           0 :         return false;
    4893                 :             :     }
    4894                 :        1321 :     return true;
    4895                 :             : }
    4896                 :             : 
    4897                 :             : /*
    4898                 :             :  * GUC check_hook for recovery_target_time
    4899                 :             :  *
    4900                 :             :  * The interpretation of the recovery_target_time string can depend on the
    4901                 :             :  * time zone setting, so we need to wait until after all GUC processing is
    4902                 :             :  * done before we can do the final parsing of the string.  This check function
    4903                 :             :  * only does a parsing pass to catch syntax errors, but we store the string
    4904                 :             :  * and parse it again when we need to use it.
    4905                 :             :  */
    4906                 :             : bool
    4907                 :        1316 : check_recovery_target_time(char **newval, void **extra, GucSource source)
    4908                 :             : {
    4909         [ +  + ]:        1316 :     if (strcmp(*newval, "") != 0)
    4910                 :             :     {
    4911                 :             :         /* reject some special values */
    4912         [ +  - ]:           3 :         if (strcmp(*newval, "now") == 0 ||
    4913         [ +  - ]:           3 :             strcmp(*newval, "today") == 0 ||
    4914         [ +  - ]:           3 :             strcmp(*newval, "tomorrow") == 0 ||
    4915         [ -  + ]:           3 :             strcmp(*newval, "yesterday") == 0)
    4916                 :             :         {
    4917                 :           0 :             return false;
    4918                 :             :         }
    4919                 :             : 
    4920                 :             :         /*
    4921                 :             :          * parse timestamp value (see also timestamptz_in())
    4922                 :             :          */
    4923                 :             :         {
    4924                 :           3 :             char       *str = *newval;
    4925                 :             :             fsec_t      fsec;
    4926                 :             :             struct pg_tm tt,
    4927                 :           3 :                        *tm = &tt;
    4928                 :             :             int         tz;
    4929                 :             :             int         dtype;
    4930                 :             :             int         nf;
    4931                 :             :             int         dterr;
    4932                 :             :             char       *field[MAXDATEFIELDS];
    4933                 :             :             int         ftype[MAXDATEFIELDS];
    4934                 :             :             char        workbuf[MAXDATELEN + MAXDATEFIELDS];
    4935                 :             :             DateTimeErrorExtra dtextra;
    4936                 :             :             TimestampTz timestamp;
    4937                 :             : 
    4938                 :           3 :             dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
    4939                 :             :                                   field, ftype, MAXDATEFIELDS, &nf);
    4940         [ +  - ]:           3 :             if (dterr == 0)
    4941                 :           3 :                 dterr = DecodeDateTime(field, ftype, nf,
    4942                 :             :                                        &dtype, tm, &fsec, &tz, &dtextra);
    4943         [ -  + ]:           3 :             if (dterr != 0)
    4944                 :           0 :                 return false;
    4945         [ -  + ]:           3 :             if (dtype != DTK_DATE)
    4946                 :           0 :                 return false;
    4947                 :             : 
    4948         [ -  + ]:           3 :             if (tm2timestamp(tm, fsec, &tz, &timestamp) != 0)
    4949                 :             :             {
    4950                 :           0 :                 GUC_check_errdetail("Timestamp out of range: \"%s\".", str);
    4951                 :           0 :                 return false;
    4952                 :             :             }
    4953                 :             :         }
    4954                 :             :     }
    4955                 :        1316 :     return true;
    4956                 :             : }
    4957                 :             : 
    4958                 :             : /*
    4959                 :             :  * GUC check_hook for recovery_target_timeline
    4960                 :             :  */
    4961                 :             : bool
    4962                 :        1316 : check_recovery_target_timeline(char **newval, void **extra, GucSource source)
    4963                 :             : {
    4964                 :             :     RecoveryTargetTimeLineGoal rttg;
    4965                 :             :     RecoveryTargetTimeLineGoal *myextra;
    4966                 :             : 
    4967         [ -  + ]:        1316 :     if (strcmp(*newval, "current") == 0)
    4968                 :           0 :         rttg = RECOVERY_TARGET_TIMELINE_CONTROLFILE;
    4969         [ +  + ]:        1316 :     else if (strcmp(*newval, "latest") == 0)
    4970                 :        1313 :         rttg = RECOVERY_TARGET_TIMELINE_LATEST;
    4971                 :             :     else
    4972                 :             :     {
    4973                 :             :         char       *endp;
    4974                 :             :         uint64      timeline;
    4975                 :             : 
    4976                 :           3 :         rttg = RECOVERY_TARGET_TIMELINE_NUMERIC;
    4977                 :             : 
    4978                 :           3 :         errno = 0;
    4979                 :           3 :         timeline = strtou64(*newval, &endp, 0);
    4980                 :             : 
    4981   [ +  +  +  -  :           3 :         if (*endp != '\0' || errno == EINVAL || errno == ERANGE)
                   -  + ]
    4982                 :             :         {
    4983                 :           1 :             GUC_check_errdetail("\"%s\" is not a valid number.",
    4984                 :             :                                 "recovery_target_timeline");
    4985                 :           3 :             return false;
    4986                 :             :         }
    4987                 :             : 
    4988   [ +  +  +  - ]:           2 :         if (timeline < 1 || timeline > PG_UINT32_MAX)
    4989                 :             :         {
    4990                 :           2 :             GUC_check_errdetail("\"%s\" must be between %u and %u.",
    4991                 :             :                                 "recovery_target_timeline", 1, PG_UINT32_MAX);
    4992                 :           2 :             return false;
    4993                 :             :         }
    4994                 :             :     }
    4995                 :             : 
    4996                 :        1313 :     myextra = (RecoveryTargetTimeLineGoal *) guc_malloc(LOG, sizeof(RecoveryTargetTimeLineGoal));
    4997         [ -  + ]:        1313 :     if (!myextra)
    4998                 :           0 :         return false;
    4999                 :        1313 :     *myextra = rttg;
    5000                 :        1313 :     *extra = myextra;
    5001                 :             : 
    5002                 :        1313 :     return true;
    5003                 :             : }
    5004                 :             : 
    5005                 :             : /*
    5006                 :             :  * GUC assign_hook for recovery_target_timeline
    5007                 :             :  */
    5008                 :             : void
    5009                 :        1313 : assign_recovery_target_timeline(const char *newval, void *extra)
    5010                 :             : {
    5011                 :        1313 :     recoveryTargetTimeLineGoal = *((RecoveryTargetTimeLineGoal *) extra);
    5012         [ -  + ]:        1313 :     if (recoveryTargetTimeLineGoal == RECOVERY_TARGET_TIMELINE_NUMERIC)
    5013                 :           0 :         recoveryTargetTLIRequested = (TimeLineID) strtoul(newval, NULL, 0);
    5014                 :             :     else
    5015                 :        1313 :         recoveryTargetTLIRequested = 0;
    5016                 :        1313 : }
    5017                 :             : 
    5018                 :             : /*
    5019                 :             :  * GUC check_hook for recovery_target_xid
    5020                 :             :  */
    5021                 :             : bool
    5022                 :        1320 : check_recovery_target_xid(char **newval, void **extra, GucSource source)
    5023                 :             : {
    5024         [ +  + ]:        1320 :     if (strcmp(*newval, "") != 0)
    5025                 :             :     {
    5026                 :             :         TransactionId xid;
    5027                 :             :         TransactionId *myextra;
    5028                 :             :         char       *endp;
    5029                 :             :         char       *val;
    5030                 :             : 
    5031                 :           7 :         errno = 0;
    5032                 :             : 
    5033                 :             :         /*
    5034                 :             :          * Consume leading whitespace to determine if number is negative
    5035                 :             :          */
    5036                 :           7 :         val = *newval;
    5037                 :             : 
    5038         [ -  + ]:           7 :         while (isspace((unsigned char) *val))
    5039                 :           0 :             val++;
    5040                 :             : 
    5041                 :             :         /*
    5042                 :             :          * This cast will remove the epoch, if any
    5043                 :             :          */
    5044                 :           7 :         xid = (TransactionId) strtou64(val, &endp, 0);
    5045                 :             : 
    5046   [ +  +  +  -  :           7 :         if (*endp != '\0' || errno == EINVAL || errno == ERANGE || *val == '-')
             +  -  +  + ]
    5047                 :             :         {
    5048                 :           2 :             GUC_check_errdetail("\"%s\" is not a valid number.",
    5049                 :             :                                 "recovery_target_xid");
    5050                 :           2 :             return false;
    5051                 :             :         }
    5052                 :             : 
    5053         [ -  + ]:           5 :         if (xid < FirstNormalTransactionId)
    5054                 :             :         {
    5055                 :           0 :             GUC_check_errdetail("\"%s\" without epoch must be greater than or equal to %u.",
    5056                 :             :                                 "recovery_target_xid",
    5057                 :             :                                 FirstNormalTransactionId);
    5058                 :           0 :             return false;
    5059                 :             :         }
    5060                 :             : 
    5061                 :           5 :         myextra = (TransactionId *) guc_malloc(LOG, sizeof(TransactionId));
    5062         [ -  + ]:           5 :         if (!myextra)
    5063                 :           0 :             return false;
    5064                 :           5 :         *myextra = xid;
    5065                 :           5 :         *extra = myextra;
    5066                 :             :     }
    5067                 :        1318 :     return true;
    5068                 :             : }
    5069                 :             : 
    5070                 :             : /*
    5071                 :             :  * GUC assign_hook for recovery_target_xid
    5072                 :             :  */
    5073                 :             : void
    5074                 :        1318 : assign_recovery_target_xid(const char *newval, void *extra)
    5075                 :             : {
    5076   [ +  -  +  + ]:        1318 :     if (newval && strcmp(newval, "") != 0)
    5077                 :           5 :         recoveryTargetXid = *((TransactionId *) extra);
    5078                 :        1318 : }
        

Generated by: LCOV version 2.0-1