PostgreSQL Source Code git master
Data Structures | Typedefs | Enumerations | Functions | Variables
xlogrecovery.h File Reference
#include "access/xlogreader.h"
#include "catalog/pg_control.h"
#include "lib/stringinfo.h"
#include "utils/timestamp.h"
Include dependency graph for xlogrecovery.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

 

Typedefs

 

Enumerations

 
 
 
 

Functions

 
void  XLogRecoveryShmemInit (void)
 
void  InitWalRecovery (ControlFileData *ControlFile, bool *wasShutdown_ptr, bool *haveBackupLabel_ptr, bool *haveTblspcMap_ptr)
 
void  PerformWalRecovery (void)
 
 
void  ShutdownWalRecovery (void)
 
 
bool  HotStandbyActive (void)
 
 
 
void  SetRecoveryPause (bool recoveryPause)
 
void  GetXLogReceiptTime (TimestampTz *rtime, bool *fromStream)
 
 
 
 
bool  PromoteIsTriggered (void)
 
bool  CheckPromoteSignal (void)
 
void  WakeupRecovery (void)
 
 
 
void  RecoveryRequiresIntParameter (const char *param_name, int currValue, int minValue)
 
 

Variables

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Typedef Documentation

RecoveryPauseState

Enumeration Type Documentation

RecoveryPauseState

Enumerator
RECOVERY_NOT_PAUSED 
RECOVERY_PAUSE_REQUESTED 
RECOVERY_PAUSED 

Definition at line 54 of file xlogrecovery.h.

55{
56 RECOVERY_NOT_PAUSED, /* pause not requested */
57 RECOVERY_PAUSE_REQUESTED, /* pause requested, but not yet paused */
58 RECOVERY_PAUSED, /* recovery is paused */
RecoveryPauseState
Definition: xlogrecovery.h:55
@ RECOVERY_PAUSED
Definition: xlogrecovery.h:58
@ RECOVERY_NOT_PAUSED
Definition: xlogrecovery.h:56
@ RECOVERY_PAUSE_REQUESTED
Definition: xlogrecovery.h:57

RecoveryTargetAction

Enumerator
RECOVERY_TARGET_ACTION_PAUSE 
RECOVERY_TARGET_ACTION_PROMOTE 
RECOVERY_TARGET_ACTION_SHUTDOWN 

Definition at line 46 of file xlogrecovery.h.

47{
RecoveryTargetAction
Definition: xlogrecovery.h:47
@ RECOVERY_TARGET_ACTION_PAUSE
Definition: xlogrecovery.h:48
@ RECOVERY_TARGET_ACTION_PROMOTE
Definition: xlogrecovery.h:49
@ RECOVERY_TARGET_ACTION_SHUTDOWN
Definition: xlogrecovery.h:50

RecoveryTargetTimeLineGoal

Enumerator
RECOVERY_TARGET_TIMELINE_CONTROLFILE 
RECOVERY_TARGET_TIMELINE_LATEST 
RECOVERY_TARGET_TIMELINE_NUMERIC 

Definition at line 36 of file xlogrecovery.h.

37{
RecoveryTargetTimeLineGoal
Definition: xlogrecovery.h:37
@ RECOVERY_TARGET_TIMELINE_NUMERIC
Definition: xlogrecovery.h:40
@ RECOVERY_TARGET_TIMELINE_CONTROLFILE
Definition: xlogrecovery.h:38
@ RECOVERY_TARGET_TIMELINE_LATEST
Definition: xlogrecovery.h:39

RecoveryTargetType

Enumerator
RECOVERY_TARGET_UNSET 
RECOVERY_TARGET_XID 
RECOVERY_TARGET_TIME 
RECOVERY_TARGET_NAME 
RECOVERY_TARGET_LSN 
RECOVERY_TARGET_IMMEDIATE 

Definition at line 23 of file xlogrecovery.h.

24{
RecoveryTargetType
Definition: xlogrecovery.h:24
@ RECOVERY_TARGET_IMMEDIATE
Definition: xlogrecovery.h:30
@ RECOVERY_TARGET_TIME
Definition: xlogrecovery.h:27
@ RECOVERY_TARGET_UNSET
Definition: xlogrecovery.h:25
@ RECOVERY_TARGET_XID
Definition: xlogrecovery.h:26
@ RECOVERY_TARGET_LSN
Definition: xlogrecovery.h:29
@ RECOVERY_TARGET_NAME
Definition: xlogrecovery.h:28

Function Documentation

CheckPromoteSignal()

bool CheckPromoteSignal ( void  )

Definition at line 4485 of file xlogrecovery.c.

4486{
4487 struct stat stat_buf;
4488
4489 if (stat(PROMOTE_SIGNAL_FILE, &stat_buf) == 0)
4490 return true;
4491
4492 return false;
4493}
Definition: win32_port.h:255
#define stat
Definition: win32_port.h:274
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:310

References PROMOTE_SIGNAL_FILE, and stat.

Referenced by CheckForStandbyTrigger(), and process_pm_pmsignal().

FinishWalRecovery()

EndOfWalRecoveryInfo * FinishWalRecovery ( void  )

Definition at line 1467 of file xlogrecovery.c.

1468{
1470 XLogRecPtr lastRec;
1471 TimeLineID lastRecTLI;
1472 XLogRecPtr endOfLog;
1473
1474 /*
1475 * Kill WAL receiver, if it's still running, before we continue to write
1476 * the startup checkpoint and aborted-contrecord records. It will trump
1477 * over these records and subsequent ones if it's still alive when we
1478 * start writing WAL.
1479 */
1481
1482 /*
1483 * Shutdown the slot sync worker to drop any temporary slots acquired by
1484 * it and to prevent it from keep trying to fetch the failover slots.
1485 *
1486 * We do not update the 'synced' column in 'pg_replication_slots' system
1487 * view from true to false here, as any failed update could leave 'synced'
1488 * column false for some slots. This could cause issues during slot sync
1489 * after restarting the server as a standby. While updating the 'synced'
1490 * column after switching to the new timeline is an option, it does not
1491 * simplify the handling for the 'synced' column. Therefore, we retain the
1492 * 'synced' column as true after promotion as it may provide useful
1493 * information about the slot origin.
1494 */
1496
1497 /*
1498 * We are now done reading the xlog from stream. Turn off streaming
1499 * recovery to force fetching the files (which would be required at end of
1500 * recovery, e.g., timeline history file) from archive or pg_wal.
1501 *
1502 * Note that standby mode must be turned off after killing WAL receiver,
1503 * i.e., calling XLogShutdownWalRcv().
1504 */
1506 StandbyMode = false;
1507
1508 /*
1509 * Determine where to start writing WAL next.
1510 *
1511 * Re-fetch the last valid or last applied record, so we can identify the
1512 * exact endpoint of what we consider the valid portion of WAL. There may
1513 * be an incomplete continuation record after that, in which case
1514 * 'abortedRecPtr' and 'missingContrecPtr' are set and the caller will
1515 * write a special OVERWRITE_CONTRECORD message to mark that the rest of
1516 * it is intentionally missing. See CreateOverwriteContrecordRecord().
1517 *
1518 * An important side-effect of this is to load the last page into
1519 * xlogreader. The caller uses it to initialize the WAL for writing.
1520 */
1521 if (!InRecovery)
1522 {
1523 lastRec = CheckPointLoc;
1524 lastRecTLI = CheckPointTLI;
1525 }
1526 else
1527 {
1529 lastRecTLI = XLogRecoveryCtl->lastReplayedTLI;
1530 }
1532 (void) ReadRecord(xlogprefetcher, PANIC, false, lastRecTLI);
1533 endOfLog = xlogreader->EndRecPtr;
1534
1535 /*
1536 * Remember the TLI in the filename of the XLOG segment containing the
1537 * end-of-log. It could be different from the timeline that endOfLog
1538 * nominally belongs to, if there was a timeline switch in that segment,
1539 * and we were reading the old WAL from a segment belonging to a higher
1540 * timeline.
1541 */
1542 result->endOfLogTLI = xlogreader->seg.ws_tli;
1543
1545 {
1546 /*
1547 * We are no longer in archive recovery state.
1548 *
1549 * We are now done reading the old WAL. Turn off archive fetching if
1550 * it was active.
1551 */
1553 InArchiveRecovery = false;
1554
1555 /*
1556 * If the ending log segment is still open, close it (to avoid
1557 * problems on Windows with trying to rename or delete an open file).
1558 */
1559 if (readFile >= 0)
1560 {
1561 close(readFile);
1562 readFile = -1;
1563 }
1564 }
1565
1566 /*
1567 * Copy the last partial block to the caller, for initializing the WAL
1568 * buffer for appending new WAL.
1569 */
1570 if (endOfLog % XLOG_BLCKSZ != 0)
1571 {
1572 char *page;
1573 int len;
1574 XLogRecPtr pageBeginPtr;
1575
1576 pageBeginPtr = endOfLog - (endOfLog % XLOG_BLCKSZ);
1578
1579 /* Copy the valid part of the last block */
1580 len = endOfLog % XLOG_BLCKSZ;
1581 page = palloc(len);
1582 memcpy(page, xlogreader->readBuf, len);
1583
1584 result->lastPageBeginPtr = pageBeginPtr;
1585 result->lastPage = page;
1586 }
1587 else
1588 {
1589 /* There is no partial block to copy. */
1590 result->lastPageBeginPtr = endOfLog;
1591 result->lastPage = NULL;
1592 }
1593
1594 /*
1595 * Create a comment for the history file to explain why and where timeline
1596 * changed.
1597 */
1599
1600 result->lastRec = lastRec;
1601 result->lastRecTLI = lastRecTLI;
1602 result->endOfLog = endOfLog;
1603
1604 result->abortedRecPtr = abortedRecPtr;
1606
1609
1610 return result;
1611}
#define PANIC
Definition: elog.h:42
Assert(PointerIsAligned(start, uint64))
#define close(a)
Definition: win32.h:12
void * palloc(Size size)
Definition: mcxt.c:1365
const void size_t len
void ShutDownSlotSync(void)
Definition: slotsync.c:1580
bool standby_signal_file_found
Definition: xlogrecovery.h:140
XLogRecPtr lastRec
Definition: xlogrecovery.h:116
XLogRecPtr endOfLog
Definition: xlogrecovery.h:118
TimeLineID lastRecTLI
Definition: xlogrecovery.h:117
XLogRecPtr lastPageBeginPtr
Definition: xlogrecovery.h:121
XLogRecPtr abortedRecPtr
Definition: xlogrecovery.h:130
char * recoveryStopReason
Definition: xlogrecovery.h:134
XLogRecPtr missingContrecPtr
Definition: xlogrecovery.h:131
TimeLineID endOfLogTLI
Definition: xlogrecovery.h:119
bool recovery_signal_file_found
Definition: xlogrecovery.h:141
TimeLineID ws_tli
Definition: xlogreader.h:49
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
char * readBuf
Definition: xlogreader.h:267
WALOpenSegment seg
Definition: xlogreader.h:272
TimeLineID lastReplayedTLI
Definition: xlogrecovery.c:347
XLogRecPtr lastReplayedReadRecPtr
Definition: xlogrecovery.c:345
bool WalRcvStreaming(void)
int wal_segment_size
Definition: xlog.c:144
void XLogShutdownWalRcv(void)
Definition: xlog.c:9517
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
Definition: xlog_internal.h:106
uint64 XLogRecPtr
Definition: xlogdefs.h:21
uint32 TimeLineID
Definition: xlogdefs.h:62
void XLogPrefetcherBeginRead(XLogPrefetcher *prefetcher, XLogRecPtr recPtr)
static char * getRecoveryStopReason(void)
Definition: xlogrecovery.c:2899
bool ArchiveRecoveryRequested
Definition: xlogrecovery.c:139
bool InArchiveRecovery
Definition: xlogrecovery.c:140
static XLogRecPtr missingContrecPtr
Definition: xlogrecovery.c:380
static XLogRecoveryCtlData * XLogRecoveryCtl
Definition: xlogrecovery.c:371
static uint32 readOff
Definition: xlogrecovery.c:234
static bool standby_signal_file_found
Definition: xlogrecovery.c:152
bool StandbyMode
Definition: xlogrecovery.c:149
static int readFile
Definition: xlogrecovery.c:232
static XLogRecPtr abortedRecPtr
Definition: xlogrecovery.c:379
static XLogRecord * ReadRecord(XLogPrefetcher *xlogprefetcher, int emode, bool fetching_ckpt, TimeLineID replayTLI)
Definition: xlogrecovery.c:3144
static XLogRecPtr CheckPointLoc
Definition: xlogrecovery.c:169
static bool recovery_signal_file_found
Definition: xlogrecovery.c:153
static XLogPrefetcher * xlogprefetcher
Definition: xlogrecovery.c:193
static XLogReaderState * xlogreader
Definition: xlogrecovery.c:190
static TimeLineID CheckPointTLI
Definition: xlogrecovery.c:170
bool InRecovery
Definition: xlogutils.c:50

References abortedRecPtr, EndOfWalRecoveryInfo::abortedRecPtr, ArchiveRecoveryRequested, Assert(), CheckPointLoc, CheckPointTLI, close, EndOfWalRecoveryInfo::endOfLog, EndOfWalRecoveryInfo::endOfLogTLI, XLogReaderState::EndRecPtr, getRecoveryStopReason(), InArchiveRecovery, InRecovery, EndOfWalRecoveryInfo::lastPage, EndOfWalRecoveryInfo::lastPageBeginPtr, EndOfWalRecoveryInfo::lastRec, EndOfWalRecoveryInfo::lastRecTLI, XLogRecoveryCtlData::lastReplayedReadRecPtr, XLogRecoveryCtlData::lastReplayedTLI, len, missingContrecPtr, EndOfWalRecoveryInfo::missingContrecPtr, palloc(), PANIC, XLogReaderState::readBuf, readFile, readOff, ReadRecord(), recovery_signal_file_found, EndOfWalRecoveryInfo::recovery_signal_file_found, EndOfWalRecoveryInfo::recoveryStopReason, XLogReaderState::seg, ShutDownSlotSync(), standby_signal_file_found, EndOfWalRecoveryInfo::standby_signal_file_found, StandbyMode, wal_segment_size, WalRcvStreaming(), WALOpenSegment::ws_tli, xlogprefetcher, XLogPrefetcherBeginRead(), xlogreader, XLogRecoveryCtl, XLogSegmentOffset, and XLogShutdownWalRcv().

Referenced by StartupXLOG().

GetCurrentChunkReplayStartTime()

TimestampTz GetCurrentChunkReplayStartTime ( void  )

Definition at line 4648 of file xlogrecovery.c.

4649{
4650 TimestampTz xtime;
4651
4655
4656 return xtime;
4657}
int64 TimestampTz
Definition: timestamp.h:39
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
TimestampTz currentChunkStartTime
Definition: xlogrecovery.c:363

References XLogRecoveryCtlData::currentChunkStartTime, XLogRecoveryCtlData::info_lck, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by GetReplicationApplyDelay().

GetCurrentReplayRecPtr()

XLogRecPtr GetCurrentReplayRecPtr ( TimeLineIDreplayEndTLI )

Definition at line 4584 of file xlogrecovery.c.

4585{
4586 XLogRecPtr recptr;
4587 TimeLineID tli;
4588
4593
4594 if (replayEndTLI)
4595 *replayEndTLI = tli;
4596 return recptr;
4597}
TimeLineID replayEndTLI
Definition: xlogrecovery.c:355
XLogRecPtr replayEndRecPtr
Definition: xlogrecovery.c:354

References XLogRecoveryCtlData::info_lck, XLogRecoveryCtlData::replayEndRecPtr, XLogRecoveryCtlData::replayEndTLI, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by UpdateMinRecoveryPoint(), and xlog_redo().

GetLatestXTime()

TimestampTz GetLatestXTime ( void  )

Definition at line 4618 of file xlogrecovery.c.

4619{
4620 TimestampTz xtime;
4621
4625
4626 return xtime;
4627}
TimestampTz recoveryLastXTime
Definition: xlogrecovery.c:357

References XLogRecoveryCtlData::info_lck, XLogRecoveryCtlData::recoveryLastXTime, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by CreateRestartPoint(), PerformWalRecovery(), and pg_last_xact_replay_timestamp().

GetRecoveryPauseState()

RecoveryPauseState GetRecoveryPauseState ( void  )

Definition at line 3083 of file xlogrecovery.c.

3084{
3086
3090
3091 return state;
3092}
RecoveryPauseState recoveryPauseState
Definition: xlogrecovery.c:365
Definition: regguts.h:323

References XLogRecoveryCtlData::info_lck, XLogRecoveryCtlData::recoveryPauseState, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by pg_get_wal_replay_pause_state(), pg_is_wal_replay_paused(), recoveryPausesHere(), and RecoveryRequiresIntParameter().

GetXLogReceiptTime()

void GetXLogReceiptTime ( TimestampTzrtime,
bool *  fromStream 
)

Definition at line 4664 of file xlogrecovery.c.

4665{
4666 /*
4667 * This must be executed in the startup process, since we don't export the
4668 * relevant state to shared memory.
4669 */
4671
4672 *rtime = XLogReceiptTime;
4673 *fromStream = (XLogReceiptSource == XLOG_FROM_STREAM);
4674}
static XLogSource XLogReceiptSource
Definition: xlogrecovery.c:261
@ XLOG_FROM_STREAM
Definition: xlogrecovery.c:216
static TimestampTz XLogReceiptTime
Definition: xlogrecovery.c:260

References Assert(), InRecovery, XLOG_FROM_STREAM, XLogReceiptSource, and XLogReceiptTime.

Referenced by GetStandbyLimitTime().

GetXLogReplayRecPtr()

XLogRecPtr GetXLogReplayRecPtr ( TimeLineIDreplayTLI )

Definition at line 4561 of file xlogrecovery.c.

4562{
4563 XLogRecPtr recptr;
4564 TimeLineID tli;
4565
4570
4571 if (replayTLI)
4572 *replayTLI = tli;
4573 return recptr;
4574}
XLogRecPtr lastReplayedEndRecPtr
Definition: xlogrecovery.c:346

References XLogRecoveryCtlData::info_lck, XLogRecoveryCtlData::lastReplayedEndRecPtr, XLogRecoveryCtlData::lastReplayedTLI, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by CheckpointerMain(), CreateRestartPoint(), GetCurrentLSN(), GetLatestLSN(), GetReplicationApplyDelay(), GetStandbyFlushRecPtr(), IsCheckpointOnSchedule(), logical_read_xlog_page(), pg_last_wal_replay_lsn(), pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), read_local_xlog_page_guts(), ReadReplicationSlot(), ReplicationSlotReserveWal(), WalReceiverMain(), WalSndWaitForWal(), XLogSendLogical(), and XLogWalRcvSendReply().

HotStandbyActive()

bool HotStandbyActive ( void  )

Definition at line 4524 of file xlogrecovery.c.

4525{
4526 /*
4527 * We check shared state each time only until Hot Standby is active. We
4528 * can't de-activate Hot Standby, so there's no need to keep checking
4529 * after the shared variable has once been seen true.
4530 */
4532 return true;
4533 else
4534 {
4535 /* spinlock is essential on machines with weak memory ordering! */
4539
4540 return LocalHotStandbyActive;
4541 }
4542}
bool SharedHotStandbyActive
Definition: xlogrecovery.c:317
static bool LocalHotStandbyActive
Definition: xlogrecovery.c:178

References XLogRecoveryCtlData::info_lck, LocalHotStandbyActive, XLogRecoveryCtlData::SharedHotStandbyActive, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by XLogWalRcvSendHSFeedback().

InitWalRecovery()

void InitWalRecovery ( ControlFileDataControlFile,
bool *  wasShutdown_ptr,
bool *  haveBackupLabel_ptr,
bool *  haveTblspcMap_ptr 
)

Definition at line 519 of file xlogrecovery.c.

521{
522 XLogPageReadPrivate *private;
523 struct stat st;
524 bool wasShutdown;
525 XLogRecord *record;
526 DBState dbstate_at_startup;
527 bool haveTblspcMap = false;
528 bool haveBackupLabel = false;
529 CheckPoint checkPoint;
530 bool backupFromStandby = false;
531
532 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 */
541 else
543
544 /*
545 * Check for signal files, and if so set up state for offline recovery
546 */
549
550 /*
551 * Take ownership of the wakeup latch if we're going to sleep during
552 * recovery, if required.
553 */
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 private = palloc0(sizeof(XLogPageReadPrivate));
562 xlogreader =
564 XL_ROUTINE(.page_read = &XLogPageRead,
565 .segment_open = NULL,
566 .segment_close = wal_segment_close),
567 private);
568 if (!xlogreader)
570 (errcode(ERRCODE_OUT_OF_MEMORY),
571 errmsg("out of memory"),
572 errdetail("Failed while allocating a WAL reading processor.")));
574
575 /*
576 * Set the WAL decode buffer size. This limits how far ahead we can read
577 * in the WAL.
578 */
580
581 /* Create a WAL prefetcher. */
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 replay_image_masked = (char *) palloc(BLCKSZ);
592 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 */
600 &backupFromStandby))
601 {
602 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 InArchiveRecovery = true;
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 ereport(LOG,
624 errmsg("starting backup recovery with redo LSN %X/%08X, checkpoint LSN %X/%08X, on timeline ID %u",
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 */
635 if (record != NULL)
636 {
637 memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
638 wasShutdown = ((record->xl_info & ~XLR_INFO_MASK) == XLOG_CHECKPOINT_SHUTDOWN);
640 errmsg_internal("checkpoint record is at %X/%08X",
642 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 if (checkPoint.redo < CheckPointLoc)
651 {
653 if (!ReadRecord(xlogprefetcher, LOG, false,
654 checkPoint.ThisTimeLineID))
656 errmsg("could not find redo location %X/%08X referenced by checkpoint record at %X/%08X",
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.",
662 }
663 }
664 else
665 {
667 errmsg("could not locate required checkpoint record at %X/%08X",
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.",
673 wasShutdown = false; /* keep compiler quiet */
674 }
675
676 /* Read the tablespace_map file if present and create symlinks. */
677 if (read_tablespace_map(&tablespaces))
678 {
679 ListCell *lc;
680
681 foreach(lc, tablespaces)
682 {
683 tablespaceinfo *ti = lfirst(lc);
684 char *linkloc;
685
686 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 */
693
694 if (symlink(ti->path, linkloc) < 0)
697 errmsg("could not create symbolic link \"%s\": %m",
698 linkloc)));
699
700 pfree(ti->path);
701 pfree(ti);
702 }
703
704 /* tell the caller to delete it later */
705 haveTblspcMap = true;
706 }
707
708 /* tell the caller to delete it later */
709 haveBackupLabel = true;
710 }
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 if (stat(TABLESPACE_MAP, &st) == 0)
726 {
727 unlink(TABLESPACE_MAP_OLD);
729 ereport(LOG,
730 (errmsg("ignoring file \"%s\" because no file \"%s\" exists",
732 errdetail("File \"%s\" was renamed to \"%s\".",
734 else
735 ereport(LOG,
736 (errmsg("ignoring file \"%s\" because no file \"%s\" exists",
738 errdetail("Could not rename file \"%s\" to \"%s\": %m.",
740 }
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 */
764 {
765 InArchiveRecovery = true;
768 }
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 */
776 ereport(LOG,
777 errmsg("restarting backup recovery with redo LSN %X/%08X",
779
780 /* Get the last valid checkpoint record. */
787 if (record != NULL)
788 {
790 errmsg_internal("checkpoint record is at %X/%08X",
792 }
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 */
802 errmsg("could not locate a valid checkpoint record at %X/%08X",
804 }
805 memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
806 wasShutdown = ((record->xl_info & ~XLR_INFO_MASK) == XLOG_CHECKPOINT_SHUTDOWN);
807 }
808
810 {
812 ereport(LOG,
813 (errmsg("entering standby mode")));
815 ereport(LOG,
816 (errmsg("starting point-in-time recovery to XID %u",
819 ereport(LOG,
820 (errmsg("starting point-in-time recovery to %s",
823 ereport(LOG,
824 (errmsg("starting point-in-time recovery to \"%s\"",
827 ereport(LOG,
828 errmsg("starting point-in-time recovery to WAL location (LSN) \"%X/%08X\"",
831 ereport(LOG,
832 (errmsg("starting point-in-time recovery to earliest consistent point")));
833 else
834 ereport(LOG,
835 (errmsg("starting archive recovery")));
836 }
837
838 /*
839 * If the location of the checkpoint record is not on the expected
840 * timeline in the history of the requested timeline, we cannot proceed:
841 * the backup is not part of the history of the requested timeline.
842 */
843 Assert(expectedTLEs); /* was initialized by reading checkpoint
844 * record */
847 {
848 XLogRecPtr switchpoint;
849
850 /*
851 * tliSwitchPoint will throw an error if the checkpoint's timeline is
852 * not in expectedTLEs at all.
853 */
854 switchpoint = tliSwitchPoint(CheckPointTLI, expectedTLEs, NULL);
856 (errmsg("requested timeline %u is not a child of this server's history",
858 /* translator: %s is a backup_label file or a pg_control file */
859 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.",
860 haveBackupLabel ? "backup_label" : "pg_control",
863 LSN_FORMAT_ARGS(switchpoint))));
864 }
865
866 /*
867 * The min recovery point should be part of the requested timeline's
868 * history, too.
869 */
874 errmsg("requested timeline %u does not contain minimum recovery point %X/%08X on timeline %u",
878
880 errmsg_internal("redo record is at %X/%08X; shutdown %s",
881 LSN_FORMAT_ARGS(checkPoint.redo),
882 wasShutdown ? "true" : "false"));
884 (errmsg_internal("next transaction ID: " UINT64_FORMAT "; next OID: %u",
886 checkPoint.nextOid)));
888 (errmsg_internal("next MultiXactId: %u; next MultiXactOffset: %u",
889 checkPoint.nextMulti, checkPoint.nextMultiOffset)));
891 (errmsg_internal("oldest unfrozen transaction ID: %u, in database %u",
892 checkPoint.oldestXid, checkPoint.oldestXidDB)));
894 (errmsg_internal("oldest MultiXactId: %u, in database %u",
895 checkPoint.oldestMulti, checkPoint.oldestMultiDB)));
897 (errmsg_internal("commit timestamp Xid oldest/newest: %u/%u",
898 checkPoint.oldestCommitTsXid,
899 checkPoint.newestCommitTsXid)));
902 (errmsg("invalid next transaction ID")));
903
904 /* sanity check */
905 if (checkPoint.redo > CheckPointLoc)
907 (errmsg("invalid redo in checkpoint record")));
908
909 /*
910 * Check whether we need to force recovery from WAL. If it appears to
911 * have been a clean shutdown and we did not have a recovery signal file,
912 * then assume no recovery needed.
913 */
914 if (checkPoint.redo < CheckPointLoc)
915 {
916 if (wasShutdown)
918 (errmsg("invalid redo record in shutdown checkpoint")));
919 InRecovery = true;
920 }
921 else if (ControlFile->state != DB_SHUTDOWNED)
922 InRecovery = true;
924 {
925 /* force recovery due to presence of recovery signal file */
926 InRecovery = true;
927 }
928
929 /*
930 * If recovery is needed, update our in-memory copy of pg_control to show
931 * that we are recovering and to show the selected checkpoint as the place
932 * we are starting from. We also mark pg_control with any minimum recovery
933 * stop point obtained from a backup history file.
934 *
935 * We don't write the changes to disk yet, though. Only do that after
936 * initializing various subsystems.
937 */
938 if (InRecovery)
939 {
941 {
943 }
944 else
945 {
946 ereport(LOG,
947 (errmsg("database system was not properly shut down; "
948 "automatic recovery in progress")));
950 ereport(LOG,
951 (errmsg("crash recovery starts in timeline %u "
952 "and has target timeline %u",
956 }
958 ControlFile->checkPointCopy = checkPoint;
960 {
961 /* initialize minRecoveryPoint if not set yet */
962 if (ControlFile->minRecoveryPoint < checkPoint.redo)
963 {
964 ControlFile->minRecoveryPoint = checkPoint.redo;
966 }
967 }
968
969 /*
970 * Set backupStartPoint if we're starting recovery from a base backup.
971 *
972 * Also set backupEndPoint and use minRecoveryPoint as the backup end
973 * location if we're starting recovery from a base backup which was
974 * taken from a standby. In this case, the database system status in
975 * pg_control must indicate that the database was already in recovery.
976 * Usually that will be DB_IN_ARCHIVE_RECOVERY but also can be
977 * DB_SHUTDOWNED_IN_RECOVERY if recovery previously was interrupted
978 * before reaching this point; e.g. because restore_command or
979 * primary_conninfo were faulty.
980 *
981 * Any other state indicates that the backup somehow became corrupted
982 * and we can't sensibly continue with recovery.
983 */
984 if (haveBackupLabel)
985 {
986 ControlFile->backupStartPoint = checkPoint.redo;
988
989 if (backupFromStandby)
990 {
991 if (dbstate_at_startup != DB_IN_ARCHIVE_RECOVERY &&
992 dbstate_at_startup != DB_SHUTDOWNED_IN_RECOVERY)
994 (errmsg("backup_label contains data inconsistent with control file"),
995 errhint("This means that the backup is corrupted and you will "
996 "have to use another backup for recovery.")));
998 }
999 }
1000 }
1001
1002 /* remember these, so that we know when we have reached consistency */
1007 {
1010 }
1011 else
1012 {
1015 }
1016
1017 /*
1018 * Start recovery assuming that the final record isn't lost.
1019 */
1022
1023 *wasShutdown_ptr = wasShutdown;
1024 *haveBackupLabel_ptr = haveBackupLabel;
1025 *haveTblspcMap_ptr = haveTblspcMap;
1026}
TimeLineID tliOfPointInHistory(XLogRecPtr ptr, List *history)
Definition: timeline.c:544
XLogRecPtr tliSwitchPoint(TimeLineID tli, List *history, TimeLineID *nextTLI)
Definition: timeline.c:572
void remove_tablespace_symlink(const char *linkloc)
Definition: tablespace.c:883
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1862
#define UINT64_FORMAT
Definition: c.h:557
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1161
int errcode_for_file_access(void)
Definition: elog.c:877
int errdetail(const char *fmt,...)
Definition: elog.c:1207
int errhint(const char *fmt,...)
Definition: elog.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LOG
Definition: elog.h:31
#define FATAL
Definition: elog.h:41
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:779
char * DataDir
Definition: globals.c:71
void OwnLatch(Latch *latch)
Definition: latch.c:126
void pfree(void *pointer)
Definition: mcxt.c:1594
void * palloc0(Size size)
Definition: mcxt.c:1395
DBState
Definition: pg_control.h:90
@ DB_IN_ARCHIVE_RECOVERY
Definition: pg_control.h:96
@ DB_SHUTDOWNED_IN_RECOVERY
Definition: pg_control.h:93
@ DB_SHUTDOWNED
Definition: pg_control.h:92
@ DB_IN_CRASH_RECOVERY
Definition: pg_control.h:95
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:68
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
#define PG_TBLSPC_DIR
Definition: relpath.h:41
Oid oldestMultiDB
Definition: pg_control.h:51
MultiXactId oldestMulti
Definition: pg_control.h:50
MultiXactOffset nextMultiOffset
Definition: pg_control.h:47
TransactionId newestCommitTsXid
Definition: pg_control.h:55
TransactionId oldestXid
Definition: pg_control.h:48
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
Oid nextOid
Definition: pg_control.h:45
MultiXactId nextMulti
Definition: pg_control.h:46
FullTransactionId nextXid
Definition: pg_control.h:44
TransactionId oldestCommitTsXid
Definition: pg_control.h:53
XLogRecPtr redo
Definition: pg_control.h:37
Oid oldestXidDB
Definition: pg_control.h:49
DBState state
Definition: pg_control.h:131
XLogRecPtr backupStartPoint
Definition: pg_control.h:170
bool backupEndRequired
Definition: pg_control.h:172
CheckPoint checkPointCopy
Definition: pg_control.h:135
XLogRecPtr backupEndPoint
Definition: pg_control.h:171
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:168
XLogRecPtr checkPoint
Definition: pg_control.h:133
uint64 system_identifier
Definition: pg_control.h:110
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:169
Definition: pg_list.h:54
uint64 system_identifier
Definition: xlogreader.h:191
uint8 xl_info
Definition: xlogrecord.h:46
Latch recoveryWakeupLatch
Definition: xlogrecovery.c:340
char * path
Definition: basebackup.h:31
#define U64FromFullTransactionId(x)
Definition: transam.h:49
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Definition: pg_list.h:46
#define symlink(oldpath, newpath)
Definition: win32_port.h:225
int wal_decode_buffer_size
Definition: xlog.c:137
static ControlFileData * ControlFile
Definition: xlog.c:574
#define TABLESPACE_MAP_OLD
Definition: xlog.h:307
#define TABLESPACE_MAP
Definition: xlog.h:306
#define BACKUP_LABEL_FILE
Definition: xlog.h:303
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:46
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogPrefetcher * XLogPrefetcherAllocate(XLogReaderState *reader)
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:107
void XLogReaderSetDecodeBuffer(XLogReaderState *state, void *buffer, size_t size)
Definition: xlogreader.c:91
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415
#define XL_ROUTINE(...)
Definition: xlogreader.h:117
static bool backupEndRequired
Definition: xlogrecovery.c:285
const char * recoveryTargetName
Definition: xlogrecovery.c:93
static XLogRecPtr minRecoveryPoint
Definition: xlogrecovery.c:280
static int XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
Definition: xlogrecovery.c:3311
static XLogRecPtr backupEndPoint
Definition: xlogrecovery.c:284
static void validateRecoveryParameters(void)
Definition: xlogrecovery.c:1118
static XLogRecord * ReadCheckpointRecord(XLogPrefetcher *xlogprefetcher, XLogRecPtr RecPtr, TimeLineID replayTLI)
Definition: xlogrecovery.c:4073
static TimeLineID RedoStartTLI
Definition: xlogrecovery.c:172
static void readRecoverySignalFile(void)
Definition: xlogrecovery.c:1036
XLogRecPtr recoveryTargetLSN
Definition: xlogrecovery.c:94
RecoveryTargetType recoveryTarget
Definition: xlogrecovery.c:87
static bool read_tablespace_map(List **tablespaces)
Definition: xlogrecovery.c:1363
static bool read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI, bool *backupEndRequired, bool *backupFromStandby)
Definition: xlogrecovery.c:1217
static List * expectedTLEs
Definition: xlogrecovery.c:125
static char * primary_image_masked
Definition: xlogrecovery.c:305
static TimeLineID minRecoveryPointTLI
Definition: xlogrecovery.c:281
TransactionId recoveryTargetXid
Definition: xlogrecovery.c:90
static char * replay_image_masked
Definition: xlogrecovery.c:304
TimeLineID recoveryTargetTLI
Definition: xlogrecovery.c:124
static XLogRecPtr RedoStartLSN
Definition: xlogrecovery.c:171
static void EnableStandbyMode(void)
Definition: xlogrecovery.c:485
TimestampTz recoveryTargetTime
Definition: xlogrecovery.c:92
static bool StandbyModeRequested
Definition: xlogrecovery.c:148
static XLogRecPtr backupStartPoint
Definition: xlogrecovery.c:283
void wal_segment_close(XLogReaderState *state)
Definition: xlogutils.c:831

References abortedRecPtr, ArchiveRecoveryRequested, Assert(), BACKUP_LABEL_FILE, backupEndPoint, ControlFileData::backupEndPoint, backupEndRequired, ControlFileData::backupEndRequired, backupStartPoint, ControlFileData::backupStartPoint, ControlFileData::checkPoint, ControlFileData::checkPointCopy, CheckPointLoc, CheckPointTLI, ControlFile, DataDir, DB_IN_ARCHIVE_RECOVERY, DB_IN_CRASH_RECOVERY, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY, DEBUG1, durable_rename(), EnableStandbyMode(), ereport, errcode(), errcode_for_file_access(), errdetail(), errhint(), errmsg(), errmsg_internal(), ERROR, expectedTLEs, FATAL, InArchiveRecovery, InRecovery, InvalidXLogRecPtr, lfirst, LOG, LSN_FORMAT_ARGS, minRecoveryPoint, ControlFileData::minRecoveryPoint, minRecoveryPointTLI, ControlFileData::minRecoveryPointTLI, missingContrecPtr, CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, CheckPoint::nextXid, NIL, tablespaceinfo::oid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, CheckPoint::oldestXid, CheckPoint::oldestXidDB, OwnLatch(), palloc(), palloc0(), PANIC, tablespaceinfo::path, pfree(), PG_TBLSPC_DIR, primary_image_masked, psprintf(), read_backup_label(), read_tablespace_map(), ReadCheckpointRecord(), ReadRecord(), readRecoverySignalFile(), RECOVERY_TARGET_IMMEDIATE, RECOVERY_TARGET_LSN, RECOVERY_TARGET_NAME, RECOVERY_TARGET_TIME, RECOVERY_TARGET_XID, recoveryTarget, recoveryTargetLSN, recoveryTargetName, recoveryTargetTime, recoveryTargetTLI, recoveryTargetXid, XLogRecoveryCtlData::recoveryWakeupLatch, CheckPoint::redo, RedoStartLSN, RedoStartTLI, remove_tablespace_symlink(), replay_image_masked, StandbyModeRequested, stat, ControlFileData::state, symlink, XLogReaderState::system_identifier, ControlFileData::system_identifier, TABLESPACE_MAP, TABLESPACE_MAP_OLD, CheckPoint::ThisTimeLineID, timestamptz_to_str(), tliOfPointInHistory(), tliSwitchPoint(), TransactionIdIsNormal, U64FromFullTransactionId, UINT64_FORMAT, validateRecoveryParameters(), wal_decode_buffer_size, wal_segment_close(), wal_segment_size, XidFromFullTransactionId, XLogRecord::xl_info, XL_ROUTINE, XLOG_CHECKPOINT_SHUTDOWN, XLogPageRead(), xlogprefetcher, XLogPrefetcherAllocate(), XLogPrefetcherBeginRead(), xlogreader, XLogReaderAllocate(), XLogReaderSetDecodeBuffer(), XLogRecGetData, XLogRecoveryCtl, and XLogRecPtrIsInvalid.

Referenced by StartupXLOG().

PerformWalRecovery()

void PerformWalRecovery ( void  )

Definition at line 1662 of file xlogrecovery.c.

1663{
1664 XLogRecord *record;
1665 bool reachedRecoveryTarget = false;
1666 TimeLineID replayTLI;
1667
1668 /*
1669 * Initialize shared variables for tracking progress of WAL replay, as if
1670 * we had just replayed the record before the REDO location (or the
1671 * checkpoint record itself, if it's a shutdown checkpoint).
1672 */
1675 {
1679 }
1680 else
1681 {
1685 }
1692
1693 /* Also ensure XLogReceiptTime has a sane value */
1695
1696 /*
1697 * Let postmaster know we've started redo now, so that it can launch the
1698 * archiver if necessary.
1699 */
1702
1703 /*
1704 * Allow read-only connections immediately if we're consistent already.
1705 */
1707
1708 /*
1709 * Find the first record that logically follows the checkpoint --- it
1710 * might physically precede it, though.
1711 */
1713 {
1714 /* back up to find the record */
1715 replayTLI = RedoStartTLI;
1717 record = ReadRecord(xlogprefetcher, PANIC, false, replayTLI);
1718
1719 /*
1720 * If a checkpoint record's redo pointer points back to an earlier
1721 * LSN, the record at that LSN should be an XLOG_CHECKPOINT_REDO
1722 * record.
1723 */
1724 if (record->xl_rmid != RM_XLOG_ID ||
1726 ereport(FATAL,
1727 errmsg("unexpected record type found at redo point %X/%08X",
1729 }
1730 else
1731 {
1732 /* just have to read next record after CheckPoint */
1734 replayTLI = CheckPointTLI;
1735 record = ReadRecord(xlogprefetcher, LOG, false, replayTLI);
1736 }
1737
1738 if (record != NULL)
1739 {
1740 TimestampTz xtime;
1741 PGRUsage ru0;
1742
1743 pg_rusage_init(&ru0);
1744
1745 InRedo = true;
1746
1747 RmgrStartup();
1748
1749 ereport(LOG,
1750 errmsg("redo starts at %X/%08X",
1752
1753 /* Prepare to report progress of the redo phase. */
1754 if (!StandbyMode)
1756
1757 /*
1758 * main redo apply loop
1759 */
1760 do
1761 {
1762 if (!StandbyMode)
1763 ereport_startup_progress("redo in progress, elapsed time: %ld.%02d s, current LSN: %X/%08X",
1765
1766#ifdef WAL_DEBUG
1767 if (XLOG_DEBUG)
1768 {
1770
1772 appendStringInfo(&buf, "REDO @ %X/%08X; LSN %X/%08X: ",
1775 xlog_outrec(&buf, xlogreader);
1776 appendStringInfoString(&buf, " - ");
1778 elog(LOG, "%s", buf.data);
1779 pfree(buf.data);
1780 }
1781#endif
1782
1783 /* Handle interrupt signals of startup process */
1785
1786 /*
1787 * Pause WAL replay, if requested by a hot-standby session via
1788 * SetRecoveryPause().
1789 *
1790 * Note that we intentionally don't take the info_lck spinlock
1791 * here. We might therefore read a slightly stale value of the
1792 * recoveryPause flag, but it can't be very stale (no worse than
1793 * the last spinlock we did acquire). Since a pause request is a
1794 * pretty asynchronous thing anyway, possibly responding to it one
1795 * WAL record later than we otherwise would is a minor issue, so
1796 * it doesn't seem worth adding another spinlock cycle to prevent
1797 * that.
1798 */
1799 if (((volatile XLogRecoveryCtlData *) XLogRecoveryCtl)->recoveryPauseState !=
1801 recoveryPausesHere(false);
1802
1803 /*
1804 * Have we reached our recovery target?
1805 */
1807 {
1808 reachedRecoveryTarget = true;
1809 break;
1810 }
1811
1812 /*
1813 * If we've been asked to lag the primary, wait on latch until
1814 * enough time has passed.
1815 */
1817 {
1818 /*
1819 * We test for paused recovery again here. If user sets
1820 * delayed apply, it may be because they expect to pause
1821 * recovery in case of problems, so we must test again here
1822 * otherwise pausing during the delay-wait wouldn't work.
1823 */
1824 if (((volatile XLogRecoveryCtlData *) XLogRecoveryCtl)->recoveryPauseState !=
1826 recoveryPausesHere(false);
1827 }
1828
1829 /*
1830 * Apply the record
1831 */
1832 ApplyWalRecord(xlogreader, record, &replayTLI);
1833
1834 /* Exit loop if we reached inclusive recovery target */
1836 {
1837 reachedRecoveryTarget = true;
1838 break;
1839 }
1840
1841 /* Else, try to fetch the next WAL record */
1842 record = ReadRecord(xlogprefetcher, LOG, false, replayTLI);
1843 } while (record != NULL);
1844
1845 /*
1846 * end of main redo apply loop
1847 */
1848
1849 if (reachedRecoveryTarget)
1850 {
1851 if (!reachedConsistency)
1852 ereport(FATAL,
1853 (errmsg("requested recovery stop point is before consistent recovery point")));
1854
1855 /*
1856 * This is the last point where we can restart recovery with a new
1857 * recovery target, if we shutdown and begin again. After this,
1858 * Resource Managers may choose to do permanent corrective actions
1859 * at end of recovery.
1860 */
1861 switch (recoveryTargetAction)
1862 {
1864
1865 /*
1866 * exit with special return code to request shutdown of
1867 * postmaster. Log messages issued from postmaster.
1868 */
1869 proc_exit(3);
1870
1872 SetRecoveryPause(true);
1873 recoveryPausesHere(true);
1874
1875 /* drop into promote */
1876
1878 break;
1879 }
1880 }
1881
1882 RmgrCleanup();
1883
1884 ereport(LOG,
1885 errmsg("redo done at %X/%08X system usage: %s",
1887 pg_rusage_show(&ru0)));
1888 xtime = GetLatestXTime();
1889 if (xtime)
1890 ereport(LOG,
1891 (errmsg("last completed transaction was at log time %s",
1892 timestamptz_to_str(xtime))));
1893
1894 InRedo = false;
1895 }
1896 else
1897 {
1898 /* there are no WAL records following the checkpoint */
1899 ereport(LOG,
1900 (errmsg("redo is not required")));
1901 }
1902
1903 /*
1904 * This check is intentionally after the above log messages that indicate
1905 * how far recovery went.
1906 */
1909 !reachedRecoveryTarget)
1910 ereport(FATAL,
1911 (errcode(ERRCODE_CONFIG_FILE_ERROR),
1912 errmsg("recovery ended before configured recovery target was reached")));
1913}
void begin_startup_progress_phase(void)
Definition: startup.c:343
void ProcessStartupProcInterrupts(void)
Definition: startup.c:154
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
#define elog(elevel,...)
Definition: elog.h:226
bool IsUnderPostmaster
Definition: globals.c:120
void proc_exit(int code)
Definition: ipc.c:104
#define XLOG_CHECKPOINT_REDO
Definition: pg_control.h:82
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
static char * buf
Definition: pg_test_fsync.c:72
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:165
@ PMSIGNAL_RECOVERY_STARTED
Definition: pmsignal.h:35
void RmgrStartup(void)
Definition: rmgr.c:58
void RmgrCleanup(void)
Definition: rmgr.c:74
#define ereport_startup_progress(msg,...)
Definition: startup.h:18
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:206
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
bool reachedConsistency
Definition: xlogrecovery.c:301
static bool recoveryStopsBefore(XLogReaderState *record)
Definition: xlogrecovery.c:2586
int recoveryTargetAction
Definition: xlogrecovery.c:89
static bool recoveryApplyDelay(XLogReaderState *record)
Definition: xlogrecovery.c:2995
static bool recoveryStopsAfter(XLogReaderState *record)
Definition: xlogrecovery.c:2739
static void CheckRecoveryConsistency(void)
Definition: xlogrecovery.c:2187
void SetRecoveryPause(bool recoveryPause)
Definition: xlogrecovery.c:3103
void xlog_outdesc(StringInfo buf, XLogReaderState *record)
Definition: xlogrecovery.c:2310
static bool InRedo
Definition: xlogrecovery.c:205
static void ApplyWalRecord(XLogReaderState *xlogreader, XLogRecord *record, TimeLineID *replayTLI)
Definition: xlogrecovery.c:1919
static void recoveryPausesHere(bool endOfRecovery)
Definition: xlogrecovery.c:2938
TimestampTz GetLatestXTime(void)
Definition: xlogrecovery.c:4618

References appendStringInfo(), appendStringInfoString(), ApplyWalRecord(), ArchiveRecoveryRequested, Assert(), begin_startup_progress_phase(), buf, CheckPointLoc, CheckPointTLI, CheckRecoveryConsistency(), XLogRecoveryCtlData::currentChunkStartTime, elog, XLogReaderState::EndRecPtr, ereport, ereport_startup_progress, errcode(), errmsg(), FATAL, GetCurrentTimestamp(), GetLatestXTime(), XLogRecoveryCtlData::info_lck, initStringInfo(), InRedo, InvalidXLogRecPtr, IsUnderPostmaster, XLogRecoveryCtlData::lastReplayedEndRecPtr, XLogRecoveryCtlData::lastReplayedReadRecPtr, XLogRecoveryCtlData::lastReplayedTLI, LOG, LSN_FORMAT_ARGS, PANIC, pfree(), pg_rusage_init(), pg_rusage_show(), PMSIGNAL_RECOVERY_STARTED, proc_exit(), ProcessStartupProcInterrupts(), reachedConsistency, ReadRecord(), XLogReaderState::ReadRecPtr, RECOVERY_NOT_PAUSED, RECOVERY_TARGET_ACTION_PAUSE, RECOVERY_TARGET_ACTION_PROMOTE, RECOVERY_TARGET_ACTION_SHUTDOWN, RECOVERY_TARGET_UNSET, recoveryApplyDelay(), XLogRecoveryCtlData::recoveryLastXTime, recoveryPausesHere(), XLogRecoveryCtlData::recoveryPauseState, recoveryStopsAfter(), recoveryStopsBefore(), recoveryTarget, recoveryTargetAction, RedoStartLSN, RedoStartTLI, XLogRecoveryCtlData::replayEndRecPtr, XLogRecoveryCtlData::replayEndTLI, RmgrCleanup(), RmgrStartup(), SendPostmasterSignal(), SetRecoveryPause(), SpinLockAcquire, SpinLockRelease, StandbyMode, timestamptz_to_str(), XLogRecord::xl_info, XLogRecord::xl_rmid, XLOG_CHECKPOINT_REDO, xlog_outdesc(), xlogprefetcher, XLogPrefetcherBeginRead(), xlogreader, XLogReceiptTime, XLogRecoveryCtl, and XLR_INFO_MASK.

Referenced by StartupXLOG().

PromoteIsTriggered()

bool PromoteIsTriggered ( void  )

Definition at line 4416 of file xlogrecovery.c.

4417{
4418 /*
4419 * We check shared state each time only until a standby promotion is
4420 * triggered. We can't trigger a promotion again, so there's no need to
4421 * keep checking after the shared variable has once been seen true.
4422 */
4424 return true;
4425
4429
4431}
bool SharedPromoteIsTriggered
Definition: xlogrecovery.c:323
static bool LocalPromoteIsTriggered
Definition: xlogrecovery.c:184

References XLogRecoveryCtlData::info_lck, LocalPromoteIsTriggered, XLogRecoveryCtlData::SharedPromoteIsTriggered, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by PerformRecoveryXLogAction(), pg_wal_replay_pause(), and pg_wal_replay_resume().

RecoveryRequiresIntParameter()

void RecoveryRequiresIntParameter ( const char *  param_name,
int  currValue,
int  minValue 
)

Definition at line 4681 of file xlogrecovery.c.

4682{
4683 if (currValue < minValue)
4684 {
4686 {
4687 bool warned_for_promote = false;
4688
4690 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4691 errmsg("hot standby is not possible because of insufficient parameter settings"),
4692 errdetail("%s = %d is a lower setting than on the primary server, where its value was %d.",
4693 param_name,
4694 currValue,
4695 minValue)));
4696
4697 SetRecoveryPause(true);
4698
4699 ereport(LOG,
4700 (errmsg("recovery has paused"),
4701 errdetail("If recovery is unpaused, the server will shut down."),
4702 errhint("You can then restart the server after making the necessary configuration changes.")));
4703
4705 {
4707
4709 {
4710 if (!warned_for_promote)
4712 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4713 errmsg("promotion is not possible because of insufficient parameter settings"),
4714
4715 /*
4716 * Repeat the detail from above so it's easy to find
4717 * in the log.
4718 */
4719 errdetail("%s = %d is a lower setting than on the primary server, where its value was %d.",
4720 param_name,
4721 currValue,
4722 minValue),
4723 errhint("Restart the server after making the necessary configuration changes.")));
4724 warned_for_promote = true;
4725 }
4726
4727 /*
4728 * If recovery pause is requested then set it paused. While
4729 * we are in the loop, user might resume and pause again so
4730 * set this every time.
4731 */
4733
4734 /*
4735 * We wait on a condition variable that will wake us as soon
4736 * as the pause ends, but we use a timeout so we can check the
4737 * above conditions periodically too.
4738 */
4740 WAIT_EVENT_RECOVERY_PAUSE);
4741 }
4743 }
4744
4745 ereport(FATAL,
4746 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4747 errmsg("recovery aborted because of insufficient parameter settings"),
4748 /* Repeat the detail from above so it's easy to find in the log. */
4749 errdetail("%s = %d is a lower setting than on the primary server, where its value was %d.",
4750 param_name,
4751 currValue,
4752 minValue),
4753 errhint("You can restart the server after making the necessary configuration changes.")));
4754 }
4755}
bool ConditionVariableCancelSleep(void)
bool ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, uint32 wait_event_info)
#define WARNING
Definition: elog.h:36
ConditionVariable recoveryNotPausedCV
Definition: xlogrecovery.c:366
static bool CheckForStandbyTrigger(void)
Definition: xlogrecovery.c:4455
static void ConfirmRecoveryPaused(void)
Definition: xlogrecovery.c:3123
static bool HotStandbyActiveInReplay(void)
Definition: xlogrecovery.c:4549
RecoveryPauseState GetRecoveryPauseState(void)
Definition: xlogrecovery.c:3083

References CheckForStandbyTrigger(), ConditionVariableCancelSleep(), ConditionVariableTimedSleep(), ConfirmRecoveryPaused(), ereport, errcode(), errdetail(), errhint(), errmsg(), FATAL, GetRecoveryPauseState(), HotStandbyActiveInReplay(), LOG, ProcessStartupProcInterrupts(), RECOVERY_NOT_PAUSED, XLogRecoveryCtlData::recoveryNotPausedCV, SetRecoveryPause(), WARNING, and XLogRecoveryCtl.

Referenced by CheckRequiredParameterValues().

RemovePromoteSignalFiles()

void RemovePromoteSignalFiles ( void  )

Definition at line 4476 of file xlogrecovery.c.

4477{
4478 unlink(PROMOTE_SIGNAL_FILE);
4479}

References PROMOTE_SIGNAL_FILE.

Referenced by CheckForStandbyTrigger(), and PostmasterMain().

SetRecoveryPause()

void SetRecoveryPause ( bool  recoveryPause )

Definition at line 3103 of file xlogrecovery.c.

References ConditionVariableBroadcast(), XLogRecoveryCtlData::info_lck, RECOVERY_NOT_PAUSED, RECOVERY_PAUSE_REQUESTED, XLogRecoveryCtlData::recoveryNotPausedCV, XLogRecoveryCtlData::recoveryPauseState, SpinLockAcquire, SpinLockRelease, and XLogRecoveryCtl.

Referenced by PerformWalRecovery(), pg_wal_replay_pause(), pg_wal_replay_resume(), RecoveryRequiresIntParameter(), and SetPromoteIsTriggered().

ShutdownWalRecovery()

void ShutdownWalRecovery ( void  )

Definition at line 1617 of file xlogrecovery.c.

1618{
1619 char recoveryPath[MAXPGPATH];
1620
1621 /* Final update of pg_stat_recovery_prefetch. */
1623
1624 /* Shut down xlogreader */
1625 if (readFile >= 0)
1626 {
1627 close(readFile);
1628 readFile = -1;
1629 }
1633
1635 {
1636 /*
1637 * Since there might be a partial WAL segment named RECOVERYXLOG, get
1638 * rid of it.
1639 */
1640 snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYXLOG");
1641 unlink(recoveryPath); /* ignore any error */
1642
1643 /* Get rid of any remaining recovered timeline-history file, too */
1644 snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYHISTORY");
1645 unlink(recoveryPath); /* ignore any error */
1646 }
1647
1648 /*
1649 * We don't need the latch anymore. It's not strictly necessary to disown
1650 * it, but let's do it for the sake of tidiness.
1651 */
1654}
void DisownLatch(Latch *latch)
Definition: latch.c:144
#define MAXPGPATH
#define snprintf
Definition: port.h:239
void * private_data
Definition: xlogreader.h:196
#define XLOGDIR
Definition: xlog_internal.h:149
void XLogPrefetcherComputeStats(XLogPrefetcher *prefetcher)
void XLogPrefetcherFree(XLogPrefetcher *prefetcher)
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:162

References ArchiveRecoveryRequested, close, DisownLatch(), MAXPGPATH, pfree(), XLogReaderState::private_data, readFile, XLogRecoveryCtlData::recoveryWakeupLatch, snprintf, XLOGDIR, xlogprefetcher, XLogPrefetcherComputeStats(), XLogPrefetcherFree(), xlogreader, XLogReaderFree(), and XLogRecoveryCtl.

Referenced by StartupXLOG().

StartupRequestWalReceiverRestart()

void StartupRequestWalReceiverRestart ( void  )

Definition at line 4397 of file xlogrecovery.c.

4398{
4400 {
4401 ereport(LOG,
4402 (errmsg("WAL receiver process shutdown requested")));
4403
4404 pendingWalRcvRestart = true;
4405 }
4406}
bool WalRcvRunning(void)
static bool pendingWalRcvRestart
Definition: xlogrecovery.c:250
static XLogSource currentSource
Definition: xlogrecovery.c:248

References currentSource, ereport, errmsg(), LOG, pendingWalRcvRestart, WalRcvRunning(), and XLOG_FROM_STREAM.

Referenced by StartupRereadConfig().

WakeupRecovery()

void WakeupRecovery ( void  )

Definition at line 4500 of file xlogrecovery.c.

4501{
4503}
void SetLatch(Latch *latch)
Definition: latch.c:290

References XLogRecoveryCtlData::recoveryWakeupLatch, SetLatch(), and XLogRecoveryCtl.

Referenced by pg_wal_replay_pause(), StartupProcShutdownHandler(), StartupProcSigHupHandler(), StartupProcTriggerHandler(), WalRcvDie(), WalRcvWaitForStartPosition(), and XLogWalRcvFlush().

xlog_outdesc()

void xlog_outdesc ( StringInfo  buf,
XLogReaderStaterecord 
)

Definition at line 2310 of file xlogrecovery.c.

2311{
2312 RmgrData rmgr = GetRmgr(XLogRecGetRmid(record));
2313 uint8 info = XLogRecGetInfo(record);
2314 const char *id;
2315
2318
2319 id = rmgr.rm_identify(info);
2320 if (id == NULL)
2321 appendStringInfo(buf, "UNKNOWN (%X): ", info & ~XLR_INFO_MASK);
2322 else
2323 appendStringInfo(buf, "%s: ", id);
2324
2325 rmgr.rm_desc(buf, record);
2326}
uint8_t uint8
Definition: c.h:536
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
const char *(* rm_identify)(uint8 info)
Definition: xlog_internal.h:344
const char * rm_name
Definition: xlog_internal.h:341
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
Definition: xlog_internal.h:343
static RmgrData GetRmgr(RmgrId rmid)
Definition: xlog_internal.h:366
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:411

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), buf, GetRmgr(), RmgrData::rm_desc, RmgrData::rm_identify, RmgrData::rm_name, XLogRecGetInfo, XLogRecGetRmid, and XLR_INFO_MASK.

Referenced by PerformWalRecovery(), rm_redo_error_callback(), and XLogInsertRecord().

XLogRecoveryShmemInit()

void XLogRecoveryShmemInit ( void  )

Definition at line 465 of file xlogrecovery.c.

466{
467 bool found;
468
470 ShmemInitStruct("XLOG Recovery Ctl", XLogRecoveryShmemSize(), &found);
471 if (found)
472 return;
473 memset(XLogRecoveryCtl, 0, sizeof(XLogRecoveryCtlData));
474
478}
void ConditionVariableInit(ConditionVariable *cv)
void InitSharedLatch(Latch *latch)
Definition: latch.c:93
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:57
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:454

References ConditionVariableInit(), XLogRecoveryCtlData::info_lck, InitSharedLatch(), XLogRecoveryCtlData::recoveryNotPausedCV, XLogRecoveryCtlData::recoveryWakeupLatch, ShmemInitStruct(), SpinLockInit, XLogRecoveryCtl, and XLogRecoveryShmemSize().

Referenced by CreateOrAttachShmemStructs().

XLogRecoveryShmemSize()

Size XLogRecoveryShmemSize ( void  )

Definition at line 454 of file xlogrecovery.c.

455{
456 Size size;
457
458 /* XLogRecoveryCtl */
459 size = sizeof(XLogRecoveryCtlData);
460
461 return size;
462}
size_t Size
Definition: c.h:610
struct XLogRecoveryCtlData XLogRecoveryCtlData

Referenced by CalculateShmemSize(), and XLogRecoveryShmemInit().

XLogRequestWalReceiverReply()

void XLogRequestWalReceiverReply ( void  )

Definition at line 4509 of file xlogrecovery.c.

4510{
4512}
static bool doRequestWalReceiverReply
Definition: xlogrecovery.c:187

References doRequestWalReceiverReply.

Referenced by xact_redo_commit().

Variable Documentation

archiveCleanupCommand

PGDLLIMPORT char* archiveCleanupCommand
extern

Definition at line 86 of file xlogrecovery.c.

Referenced by CreateRestartPoint().

PrimaryConnInfo

PGDLLIMPORT char* PrimaryConnInfo
extern

Definition at line 98 of file xlogrecovery.c.

PrimarySlotName

PGDLLIMPORT char* PrimarySlotName
extern

Definition at line 99 of file xlogrecovery.c.

reachedConsistency

recovery_min_apply_delay

PGDLLIMPORT int recovery_min_apply_delay
extern

Definition at line 95 of file xlogrecovery.c.

Referenced by recoveryApplyDelay().

recovery_target_time_string

PGDLLIMPORT char* recovery_target_time_string
extern

Definition at line 91 of file xlogrecovery.c.

Referenced by validateRecoveryParameters().

recoveryEndCommand

PGDLLIMPORT char* recoveryEndCommand
extern

Definition at line 85 of file xlogrecovery.c.

Referenced by CleanupAfterArchiveRecovery().

recoveryRestoreCommand

PGDLLIMPORT char* recoveryRestoreCommand
extern

Definition at line 84 of file xlogrecovery.c.

Referenced by RestoreArchivedFile(), and validateRecoveryParameters().

recoveryTarget

recoveryTargetAction

PGDLLIMPORT int recoveryTargetAction
extern

Definition at line 89 of file xlogrecovery.c.

Referenced by PerformWalRecovery(), and validateRecoveryParameters().

recoveryTargetInclusive

PGDLLIMPORT bool recoveryTargetInclusive
extern

Definition at line 88 of file xlogrecovery.c.

Referenced by recoveryStopsAfter(), and recoveryStopsBefore().

recoveryTargetLSN

PGDLLIMPORT XLogRecPtr recoveryTargetLSN
extern

recoveryTargetName

PGDLLIMPORT const char* recoveryTargetName
extern

Definition at line 93 of file xlogrecovery.c.

Referenced by assign_recovery_target_name(), InitWalRecovery(), and recoveryStopsAfter().

recoveryTargetTime

PGDLLIMPORT TimestampTz recoveryTargetTime
extern

Definition at line 92 of file xlogrecovery.c.

Referenced by InitWalRecovery(), recoveryStopsBefore(), and validateRecoveryParameters().

recoveryTargetTimeLineGoal

recoveryTargetTLI

recoveryTargetTLIRequested

PGDLLIMPORT TimeLineID recoveryTargetTLIRequested
extern

Definition at line 123 of file xlogrecovery.c.

Referenced by assign_recovery_target_timeline(), and validateRecoveryParameters().

recoveryTargetXid

PGDLLIMPORT TransactionId recoveryTargetXid
extern

StandbyMode

wal_receiver_create_temp_slot

PGDLLIMPORT bool wal_receiver_create_temp_slot
extern

Definition at line 100 of file xlogrecovery.c.

Referenced by StartupRereadConfig(), and WaitForWALToBecomeAvailable().

AltStyle によって変換されたページ (->オリジナル) /