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