PostgreSQL Source Code  git master
xlog.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/heaptoast.h"
#include "access/multixact.h"
#include "access/rewriteheap.h"
#include "access/subtrans.h"
#include "access/timeline.h"
#include "access/transam.h"
#include "access/twophase.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "access/xlogarchive.h"
#include "access/xloginsert.h"
#include "access/xlogreader.h"
#include "access/xlogutils.h"
#include "catalog/catversion.h"
#include "catalog/pg_control.h"
#include "catalog/pg_database.h"
#include "commands/progress.h"
#include "commands/tablespace.h"
#include "common/controldata_utils.h"
#include "executor/instrument.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "pgstat.h"
#include "port/atomics.h"
#include "postmaster/bgwriter.h"
#include "postmaster/startup.h"
#include "postmaster/walwriter.h"
#include "replication/basebackup.h"
#include "replication/logical.h"
#include "replication/origin.h"
#include "replication/slot.h"
#include "replication/snapbuild.h"
#include "replication/walreceiver.h"
#include "replication/walsender.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/large_object.h"
#include "storage/latch.h"
#include "storage/pmsignal.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/reinit.h"
#include "storage/smgr.h"
#include "storage/spin.h"
#include "storage/sync.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/relmapper.h"
#include "utils/snapmgr.h"
#include "utils/timestamp.h"
Include dependency graph for xlog.c:

Go to the source code of this file.

Data Structures

struct  XLogwrtRqst
 
struct  XLogwrtResult
 
struct  WALInsertLock
 
union  WALInsertLockPadded
 
struct  XLogCtlInsert
 
struct  XLogCtlData
 
struct  XLogPageReadPrivate
 

Macros

#define RECOVERY_COMMAND_FILE   "recovery.conf"
 
#define RECOVERY_COMMAND_DONE   "recovery.done"
 
#define NUM_XLOGINSERT_LOCKS   8
 
#define INSERT_FREESPACE(endptr)   (((endptr) % XLOG_BLCKSZ == 0) ? 0 : (XLOG_BLCKSZ - (endptr) % XLOG_BLCKSZ))
 
#define NextBufIdx(idx)   (((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))
 
#define XLogRecPtrToBufIdx(recptr)   (((recptr) / XLOG_BLCKSZ) % (XLogCtl->XLogCacheBlck + 1))
 
#define UsableBytesInPage   (XLOG_BLCKSZ - SizeOfXLogShortPHD)
 
#define ConvertToXSegs(x, segsize)   XLogMBVarToSegs((x), (segsize))
 
#define RecoveryRequiresIntParameter(param_name, currValue, minValue)
 

Typedefs

typedef struct XLogwrtRqst XLogwrtRqst
 
typedef struct XLogwrtResult XLogwrtResult
 
typedef union WALInsertLockPadded WALInsertLockPadded
 
typedef enum ExclusiveBackupState ExclusiveBackupState
 
typedef struct XLogCtlInsert XLogCtlInsert
 
typedef struct XLogCtlData XLogCtlData
 
typedef struct XLogPageReadPrivate XLogPageReadPrivate
 

Enumerations

enum  ExclusiveBackupState { EXCLUSIVE_BACKUP_NONE = 0, EXCLUSIVE_BACKUP_STARTING, EXCLUSIVE_BACKUP_IN_PROGRESS, EXCLUSIVE_BACKUP_STOPPING }
 
enum  XLogSource { XLOG_FROM_ANY = 0, XLOG_FROM_ARCHIVE, XLOG_FROM_PG_WAL, XLOG_FROM_STREAM }
 

Functions

static void readRecoverySignalFile (void)
 
static void validateRecoveryParameters (void)
 
static void exitArchiveRecovery (TimeLineID endTLI, XLogRecPtr endOfLog)
 
static bool recoveryStopsBefore (XLogReaderState *record)
 
static bool recoveryStopsAfter (XLogReaderState *record)
 
static void recoveryPausesHere (bool endOfRecovery)
 
static bool recoveryApplyDelay (XLogReaderState *record)
 
static void SetLatestXTime (TimestampTz xtime)
 
static void SetCurrentChunkStartTime (TimestampTz xtime)
 
static void CheckRequiredParameterValues (void)
 
static void XLogReportParameters (void)
 
static void checkTimeLineSwitch (XLogRecPtr lsn, TimeLineID newTLI, TimeLineID prevTLI)
 
static void LocalSetXLogInsertAllowed (void)
 
static void CreateEndOfRecoveryRecord (void)
 
static void CheckPointGuts (XLogRecPtr checkPointRedo, int flags)
 
static void KeepLogSeg (XLogRecPtr recptr, XLogSegNo *logSegNo)
 
static XLogRecPtr XLogGetReplicationSlotMinimumLSN (void)
 
static void AdvanceXLInsertBuffer (XLogRecPtr upto, bool opportunistic)
 
static bool XLogCheckpointNeeded (XLogSegNo new_segno)
 
static void XLogWrite (XLogwrtRqst WriteRqst, bool flexible)
 
static bool InstallXLogFileSegment (XLogSegNo *segno, char *tmppath, bool find_free, XLogSegNo max_segno, bool use_lock)
 
static int XLogFileRead (XLogSegNo segno, int emode, TimeLineID tli, XLogSource source, bool notfoundOk)
 
static int XLogFileReadAnyTLI (XLogSegNo segno, int emode, XLogSource source)
 
static int XLogPageRead (XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
 
static bool WaitForWALToBecomeAvailable (XLogRecPtr RecPtr, bool randAccess, bool fetching_ckpt, XLogRecPtr tliRecPtr)
 
static int emode_for_corrupt_record (int emode, XLogRecPtr RecPtr)
 
static void XLogFileClose (void)
 
static void PreallocXlogFiles (XLogRecPtr endptr)
 
static void RemoveTempXlogFiles (void)
 
static void RemoveOldXlogFiles (XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr)
 
static void RemoveXlogFile (const char *segname, XLogRecPtr lastredoptr, XLogRecPtr endptr)
 
static void UpdateLastRemovedPtr (char *filename)
 
static void ValidateXLOGDirectoryStructure (void)
 
static void CleanupBackupHistory (void)
 
static void UpdateMinRecoveryPoint (XLogRecPtr lsn, bool force)
 
static XLogRecordReadRecord (XLogReaderState *xlogreader, int emode, bool fetching_ckpt)
 
static void CheckRecoveryConsistency (void)
 
static XLogRecordReadCheckpointRecord (XLogReaderState *xlogreader, XLogRecPtr RecPtr, int whichChkpt, bool report)
 
static bool rescanLatestTimeLine (void)
 
static void InitControlFile (uint64 sysidentifier)
 
static void WriteControlFile (void)
 
static void ReadControlFile (void)
 
static char * str_time (pg_time_t tnow)
 
static void SetPromoteIsTriggered (void)
 
static bool CheckForStandbyTrigger (void)
 
static void xlog_outdesc (StringInfo buf, XLogReaderState *record)
 
static void pg_start_backup_callback (int code, Datum arg)
 
static void pg_stop_backup_callback (int code, Datum arg)
 
static bool read_backup_label (XLogRecPtr *checkPointLoc, bool *backupEndRequired, bool *backupFromStandby)
 
static bool read_tablespace_map (List **tablespaces)
 
static void rm_redo_error_callback (void *arg)
 
static int get_sync_bit (int method)
 
static void CopyXLogRecordToWAL (int write_len, bool isLogSwitch, XLogRecData *rdata, XLogRecPtr StartPos, XLogRecPtr EndPos)
 
static void ReserveXLogInsertLocation (int size, XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
 
static bool ReserveXLogSwitch (XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
 
static XLogRecPtr WaitXLogInsertionsToFinish (XLogRecPtr upto)
 
static char * GetXLogBuffer (XLogRecPtr ptr)
 
static XLogRecPtr XLogBytePosToRecPtr (uint64 bytepos)
 
static XLogRecPtr XLogBytePosToEndRecPtr (uint64 bytepos)
 
static uint64 XLogRecPtrToBytePos (XLogRecPtr ptr)
 
static void checkXLogConsistency (XLogReaderState *record)
 
static void WALInsertLockAcquire (void)
 
static void WALInsertLockAcquireExclusive (void)
 
static void WALInsertLockRelease (void)
 
static void WALInsertLockUpdateInsertingAt (XLogRecPtr insertingAt)
 
XLogRecPtr XLogInsertRecord (XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi)
 
static void CalculateCheckpointSegments (void)
 
void assign_max_wal_size (int newval, void *extra)
 
void assign_checkpoint_completion_target (double newval, void *extra)
 
static XLogSegNo XLOGfileslop (XLogRecPtr lastredoptr)
 
void XLogSetAsyncXactLSN (XLogRecPtr asyncXactLSN)
 
void XLogSetReplicationSlotMinimumLSN (XLogRecPtr lsn)
 
void XLogFlush (XLogRecPtr record)
 
bool XLogBackgroundFlush (void)
 
bool XLogNeedsFlush (XLogRecPtr record)
 
int XLogFileInit (XLogSegNo logsegno, bool *use_existent, bool use_lock)
 
static void XLogFileCopy (XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno, int upto)
 
int XLogFileOpen (XLogSegNo segno)
 
void CheckXLogRemoved (XLogSegNo segno, TimeLineID tli)
 
XLogSegNo XLogGetLastRemovedSegno (void)
 
static void RemoveNonParentXlogFiles (XLogRecPtr switchpoint, TimeLineID newTLI)
 
void UpdateControlFile (void)
 
uint64 GetSystemIdentifier (void)
 
char * GetMockAuthenticationNonce (void)
 
bool DataChecksumsEnabled (void)
 
XLogRecPtr GetFakeLSNForUnloggedRel (void)
 
static int XLOGChooseNumBuffers (void)
 
bool check_wal_buffers (int *newval, void **extra, GucSource source)
 
void LocalProcessControlFile (bool reset)
 
Size XLOGShmemSize (void)
 
void XLOGShmemInit (void)
 
void BootStrapXLOG (void)
 
static bool getRecordTimestamp (XLogReaderState *record, TimestampTz *recordXtime)
 
bool RecoveryIsPaused (void)
 
void SetRecoveryPause (bool recoveryPause)
 
TimestampTz GetLatestXTime (void)
 
TimestampTz GetCurrentChunkReplayStartTime (void)
 
void GetXLogReceiptTime (TimestampTz *rtime, bool *fromStream)
 
void StartupXLOG (void)
 
bool RecoveryInProgress (void)
 
RecoveryState GetRecoveryState (void)
 
bool HotStandbyActive (void)
 
bool HotStandbyActiveInReplay (void)
 
bool XLogInsertAllowed (void)
 
void InitXLOGAccess (void)
 
XLogRecPtr GetRedoRecPtr (void)
 
void GetFullPageWriteInfo (XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
 
XLogRecPtr GetInsertRecPtr (void)
 
XLogRecPtr GetFlushRecPtr (void)
 
XLogRecPtr GetLastImportantRecPtr (void)
 
pg_time_t GetLastSegSwitchData (XLogRecPtr *lastSwitchLSN)
 
void ShutdownXLOG (int code, Datum arg)
 
static void LogCheckpointStart (int flags, bool restartpoint)
 
static void LogCheckpointEnd (bool restartpoint)
 
static void UpdateCheckPointDistanceEstimate (uint64 nbytes)
 
void CreateCheckPoint (int flags)
 
static void RecoveryRestartPoint (const CheckPoint *checkPoint)
 
bool CreateRestartPoint (int flags)
 
WALAvailability GetWALAvailability (XLogRecPtr targetLSN)
 
void XLogPutNextOid (Oid nextOid)
 
XLogRecPtr RequestXLogSwitch (bool mark_unimportant)
 
XLogRecPtr XLogRestorePoint (const char *rpName)
 
void UpdateFullPageWrites (void)
 
void xlog_redo (XLogReaderState *record)
 
void assign_xlog_sync_method (int new_sync_method, void *extra)
 
void issue_xlog_fsync (int fd, XLogSegNo segno)
 
XLogRecPtr do_pg_start_backup (const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, List **tablespaces, StringInfo tblspcmapfile, bool needtblspcmapfile)
 
SessionBackupState get_backup_status (void)
 
XLogRecPtr do_pg_stop_backup (char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
 
void do_pg_abort_backup (int code, Datum arg)
 
void register_persistent_abort_backup_handler (void)
 
XLogRecPtr GetXLogReplayRecPtr (TimeLineID *replayTLI)
 
XLogRecPtr GetXLogInsertRecPtr (void)
 
XLogRecPtr GetXLogWriteRecPtr (void)
 
void GetOldestRestartPoint (XLogRecPtr *oldrecptr, TimeLineID *oldtli)
 
bool BackupInProgress (void)
 
void CancelBackup (void)
 
void StartupRequestWalReceiverRestart (void)
 
bool PromoteIsTriggered (void)
 
void RemovePromoteSignalFiles (void)
 
bool CheckPromoteSignal (void)
 
void WakeupRecovery (void)
 
void SetWalWriterSleeping (bool sleeping)
 
void XLogRequestWalReceiverReply (void)
 

Variables

uint32 bootstrap_data_checksum_version
 
int max_wal_size_mb = 1024
 
int min_wal_size_mb = 80
 
int wal_keep_size_mb = 0
 
int XLOGbuffers = -1
 
int XLogArchiveTimeout = 0
 
int XLogArchiveMode = ARCHIVE_MODE_OFF
 
char * XLogArchiveCommand = NULL
 
bool EnableHotStandby = false
 
bool fullPageWrites = true
 
bool wal_log_hints = false
 
bool wal_compression = false
 
char * wal_consistency_checking_string = NULL
 
boolwal_consistency_checking = NULL
 
bool wal_init_zero = true
 
bool wal_recycle = true
 
bool log_checkpoints = false
 
int sync_method = DEFAULT_SYNC_METHOD
 
int wal_level = WAL_LEVEL_MINIMAL
 
int CommitDelay = 0
 
int CommitSiblings = 5
 
int wal_retrieve_retry_interval = 5000
 
int max_slot_wal_keep_size_mb = -1
 
int wal_segment_size = DEFAULT_XLOG_SEG_SIZE
 
int CheckPointSegments
 
static double CheckPointDistanceEstimate = 0
 
static double PrevCheckPointDistance = 0
 
const struct config_enum_entry sync_method_options []
 
const struct config_enum_entry archive_mode_options []
 
const struct config_enum_entry recovery_target_action_options []
 
CheckpointStatsData CheckpointStats
 
TimeLineID ThisTimeLineID = 0
 
bool InRecovery = false
 
HotStandbyState standbyState = STANDBY_DISABLED
 
static XLogRecPtr LastRec
 
static XLogRecPtr flushedUpto = 0
 
static TimeLineID receiveTLI = 0
 
static bool lastFullPageWrites
 
static bool LocalRecoveryInProgress = true
 
static bool LocalHotStandbyActive = false
 
static bool LocalPromoteIsTriggered = false
 
static int LocalXLogInsertAllowed = -1
 
bool ArchiveRecoveryRequested = false
 
bool InArchiveRecovery = false
 
static bool standby_signal_file_found = false
 
static bool recovery_signal_file_found = false
 
static bool restoredFromArchive = false
 
static char * replay_image_masked = NULL
 
static char * primary_image_masked = NULL
 
char * recoveryRestoreCommand = NULL
 
char * recoveryEndCommand = NULL
 
char * archiveCleanupCommand = NULL
 
RecoveryTargetType recoveryTarget = RECOVERY_TARGET_UNSET
 
bool recoveryTargetInclusive = true
 
int recoveryTargetAction = RECOVERY_TARGET_ACTION_PAUSE
 
TransactionId recoveryTargetXid
 
char * recovery_target_time_string
 
static TimestampTz recoveryTargetTime
 
const char * recoveryTargetName
 
XLogRecPtr recoveryTargetLSN
 
int recovery_min_apply_delay = 0
 
bool StandbyModeRequested = false
 
char * PrimaryConnInfo = NULL
 
char * PrimarySlotName = NULL
 
char * PromoteTriggerFile = NULL
 
bool wal_receiver_create_temp_slot = false
 
bool StandbyMode = false
 
static TransactionId recoveryStopXid
 
static TimestampTz recoveryStopTime
 
static XLogRecPtr recoveryStopLSN
 
static char recoveryStopName [MAXFNAMELEN]
 
static bool recoveryStopAfter
 
RecoveryTargetTimeLineGoal recoveryTargetTimeLineGoal = RECOVERY_TARGET_TIMELINE_LATEST
 
TimeLineID recoveryTargetTLIRequested = 0
 
TimeLineID recoveryTargetTLI = 0
 
static ListexpectedTLEs
 
static TimeLineID curFileTLI
 
XLogRecPtr ProcLastRecPtr = InvalidXLogRecPtr
 
XLogRecPtr XactLastRecEnd = InvalidXLogRecPtr
 
XLogRecPtr XactLastCommitEnd = InvalidXLogRecPtr
 
static XLogRecPtr RedoRecPtr
 
static bool doPageWrites
 
static bool doRequestWalReceiverReply
 
static XLogRecPtr RedoStartLSN = InvalidXLogRecPtr
 
static SessionBackupState sessionBackupState = SESSION_BACKUP_NONE
 
static XLogCtlDataXLogCtl = NULL
 
static WALInsertLockPaddedWALInsertLocks = NULL
 
static ControlFileDataControlFile = NULL
 
static int UsableBytesInSegment
 
static XLogwrtResult LogwrtResult = {0, 0}
 
static const char *const xlogSourceNames [] = {"any", "archive", "pg_wal", "stream"}
 
static int openLogFile = -1
 
static XLogSegNo openLogSegNo = 0
 
static int readFile = -1
 
static XLogSegNo readSegNo = 0
 
static uint32 readOff = 0
 
static uint32 readLen = 0
 
static XLogSource readSource = XLOG_FROM_ANY
 
static XLogSource currentSource = XLOG_FROM_ANY
 
static bool lastSourceFailed = false
 
static bool pendingWalRcvRestart = false
 
static TimestampTz XLogReceiptTime = 0
 
static XLogSource XLogReceiptSource = XLOG_FROM_ANY
 
static XLogRecPtr ReadRecPtr
 
static XLogRecPtr EndRecPtr
 
static XLogRecPtr minRecoveryPoint
 
static TimeLineID minRecoveryPointTLI
 
static bool updateMinRecoveryPoint = true
 
bool reachedConsistency = false
 
static bool InRedo = false
 
static bool bgwriterLaunched = false
 
static int MyLockNo = 0
 
static bool holdingAllLocks = false
 

Macro Definition Documentation

◆ ConvertToXSegs

#define ConvertToXSegs (   x,
  segsize 
)    XLogMBVarToSegs((x), (segsize))

◆ INSERT_FREESPACE

#define INSERT_FREESPACE (   endptr)    (((endptr) % XLOG_BLCKSZ == 0) ? 0 : (XLOG_BLCKSZ - (endptr) % XLOG_BLCKSZ))

Definition at line 742 of file xlog.c.

Referenced by CopyXLogRecordToWAL(), and CreateCheckPoint().

◆ NextBufIdx

#define NextBufIdx (   idx)    (((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))

Definition at line 746 of file xlog.c.

Referenced by XLogWrite().

◆ NUM_XLOGINSERT_LOCKS

◆ RECOVERY_COMMAND_DONE

#define RECOVERY_COMMAND_DONE   "recovery.done"

Definition at line 86 of file xlog.c.

Referenced by readRecoverySignalFile().

◆ RECOVERY_COMMAND_FILE

#define RECOVERY_COMMAND_FILE   "recovery.conf"

Definition at line 85 of file xlog.c.

Referenced by readRecoverySignalFile().

◆ RecoveryRequiresIntParameter

#define RecoveryRequiresIntParameter (   param_name,
  currValue,
  minValue 
)
Value:
do { \
if ((currValue) < (minValue)) \
ereport(ERROR, \
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
errmsg("hot standby is not possible because %s = %d is a lower setting than on the primary server (its value was %d)", \
param_name, \
currValue, \
minValue))); \
} while(0)
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
int errmsg(const char *fmt,...)
Definition: elog.c:824

Definition at line 6235 of file xlog.c.

Referenced by CheckRequiredParameterValues().

◆ UsableBytesInPage

#define UsableBytesInPage   (XLOG_BLCKSZ - SizeOfXLogShortPHD)

◆ XLogRecPtrToBufIdx

#define XLogRecPtrToBufIdx (   recptr)    (((recptr) / XLOG_BLCKSZ) % (XLogCtl->XLogCacheBlck + 1))

Definition at line 753 of file xlog.c.

Referenced by AdvanceXLInsertBuffer(), GetXLogBuffer(), StartupXLOG(), and XLogWrite().

Typedef Documentation

◆ ExclusiveBackupState

◆ WALInsertLockPadded

◆ XLogCtlData

typedef struct XLogCtlData XLogCtlData

◆ XLogCtlInsert

typedef struct XLogCtlInsert XLogCtlInsert

◆ XLogPageReadPrivate

◆ XLogwrtResult

typedef struct XLogwrtResult XLogwrtResult

◆ XLogwrtRqst

typedef struct XLogwrtRqst XLogwrtRqst

Enumeration Type Documentation

◆ ExclusiveBackupState

Enumerator
EXCLUSIVE_BACKUP_NONE 
EXCLUSIVE_BACKUP_STARTING 
EXCLUSIVE_BACKUP_IN_PROGRESS 
EXCLUSIVE_BACKUP_STOPPING 

Definition at line 521 of file xlog.c.

◆ XLogSource

enum XLogSource
Enumerator
XLOG_FROM_ANY 
XLOG_FROM_ARCHIVE 
XLOG_FROM_PG_WAL 
XLOG_FROM_STREAM 

Definition at line 780 of file xlog.c.

781 {
782  XLOG_FROM_ANY = 0, /* request to read WAL from any source */
783  XLOG_FROM_ARCHIVE, /* restored using restore_command */
784  XLOG_FROM_PG_WAL, /* existing file in pg_wal */
785  XLOG_FROM_STREAM /* streamed from primary */
786 } XLogSource;
XLogSource
Definition: xlog.c:780

Function Documentation

◆ AdvanceXLInsertBuffer()

static void AdvanceXLInsertBuffer ( XLogRecPtr  upto,
bool  opportunistic 
)
static

Definition at line 2122 of file xlog.c.

References Assert, DEBUG1, elog, XLogwrtRqst::Flush, XLogCtlInsert::forcePageWrites, XLogCtlData::info_lck, XLogCtlData::InitializedUpTo, Insert(), XLogCtlData::Insert, InvalidXLogRecPtr, XLogCtlData::LogwrtResult, XLogCtlData::LogwrtRqst, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemSet, XLogCtlData::pages, pg_write_barrier, SpinLockAcquire, SpinLockRelease, ControlFileData::system_identifier, ThisTimeLineID, WaitXLogInsertionsToFinish(), wal_segment_size, XLogwrtRqst::Write, XLogwrtResult::Write, XLogCtlData::xlblocks, XLOG_PAGE_MAGIC, XLogRecPtrToBufIdx, XLogSegmentOffset, XLogWrite(), XLP_BKP_REMOVABLE, XLP_LONG_HEADER, XLogLongPageHeaderData::xlp_seg_size, XLogLongPageHeaderData::xlp_sysid, and XLogLongPageHeaderData::xlp_xlog_blcksz.

Referenced by GetXLogBuffer(), and XLogBackgroundFlush().

2123 {
2125  int nextidx;
2126  XLogRecPtr OldPageRqstPtr;
2127  XLogwrtRqst WriteRqst;
2128  XLogRecPtr NewPageEndPtr = InvalidXLogRecPtr;
2129  XLogRecPtr NewPageBeginPtr;
2130  XLogPageHeader NewPage;
2131  int npages = 0;
2132 
2133  LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE);
2134 
2135  /*
2136  * Now that we have the lock, check if someone initialized the page
2137  * already.
2138  */
2139  while (upto >= XLogCtl->InitializedUpTo || opportunistic)
2140  {
2142 
2143  /*
2144  * Get ending-offset of the buffer page we need to replace (this may
2145  * be zero if the buffer hasn't been used yet). Fall through if it's
2146  * already written out.
2147  */
2148  OldPageRqstPtr = XLogCtl->xlblocks[nextidx];
2149  if (LogwrtResult.Write < OldPageRqstPtr)
2150  {
2151  /*
2152  * Nope, got work to do. If we just want to pre-initialize as much
2153  * as we can without flushing, give up now.
2154  */
2155  if (opportunistic)
2156  break;
2157 
2158  /* Before waiting, get info_lck and update LogwrtResult */
2160  if (XLogCtl->LogwrtRqst.Write < OldPageRqstPtr)
2161  XLogCtl->LogwrtRqst.Write = OldPageRqstPtr;
2164 
2165  /*
2166  * Now that we have an up-to-date LogwrtResult value, see if we
2167  * still need to write it or if someone else already did.
2168  */
2169  if (LogwrtResult.Write < OldPageRqstPtr)
2170  {
2171  /*
2172  * Must acquire write lock. Release WALBufMappingLock first,
2173  * to make sure that all insertions that we need to wait for
2174  * can finish (up to this same position). Otherwise we risk
2175  * deadlock.
2176  */
2177  LWLockRelease(WALBufMappingLock);
2178 
2179  WaitXLogInsertionsToFinish(OldPageRqstPtr);
2180 
2181  LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
2182 
2184  if (LogwrtResult.Write >= OldPageRqstPtr)
2185  {
2186  /* OK, someone wrote it already */
2187  LWLockRelease(WALWriteLock);
2188  }
2189  else
2190  {
2191  /* Have to write it ourselves */
2192  TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_START();
2193  WriteRqst.Write = OldPageRqstPtr;
2194  WriteRqst.Flush = 0;
2195  XLogWrite(WriteRqst, false);
2196  LWLockRelease(WALWriteLock);
2197  TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_DONE();
2198  }
2199  /* Re-acquire WALBufMappingLock and retry */
2200  LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE);
2201  continue;
2202  }
2203  }
2204 
2205  /*
2206  * Now the next buffer slot is free and we can set it up to be the
2207  * next output page.
2208  */
2209  NewPageBeginPtr = XLogCtl->InitializedUpTo;
2210  NewPageEndPtr = NewPageBeginPtr + XLOG_BLCKSZ;
2211 
2212  Assert(XLogRecPtrToBufIdx(NewPageBeginPtr) == nextidx);
2213 
2214  NewPage = (XLogPageHeader) (XLogCtl->pages + nextidx * (Size) XLOG_BLCKSZ);
2215 
2216  /*
2217  * Be sure to re-zero the buffer so that bytes beyond what we've
2218  * written will look like zeroes and not valid XLOG records...
2219  */
2220  MemSet((char *) NewPage, 0, XLOG_BLCKSZ);
2221 
2222  /*
2223  * Fill the new page's header
2224  */
2225  NewPage->xlp_magic = XLOG_PAGE_MAGIC;
2226 
2227  /* NewPage->xlp_info = 0; */ /* done by memset */
2228  NewPage->xlp_tli = ThisTimeLineID;
2229  NewPage->xlp_pageaddr = NewPageBeginPtr;
2230 
2231  /* NewPage->xlp_rem_len = 0; */ /* done by memset */
2232 
2233  /*
2234  * If online backup is not in progress, mark the header to indicate
2235  * that WAL records beginning in this page have removable backup
2236  * blocks. This allows the WAL archiver to know whether it is safe to
2237  * compress archived WAL data by transforming full-block records into
2238  * the non-full-block format. It is sufficient to record this at the
2239  * page level because we force a page switch (in fact a segment
2240  * switch) when starting a backup, so the flag will be off before any
2241  * records can be written during the backup. At the end of a backup,
2242  * the last page will be marked as all unsafe when perhaps only part
2243  * is unsafe, but at worst the archiver would miss the opportunity to
2244  * compress a few records.
2245  */
2246  if (!Insert->forcePageWrites)
2247  NewPage->xlp_info |= XLP_BKP_REMOVABLE;
2248 
2249  /*
2250  * If first page of an XLOG segment file, make it a long header.
2251  */
2252  if ((XLogSegmentOffset(NewPage->xlp_pageaddr, wal_segment_size)) == 0)
2253  {
2254  XLogLongPageHeader NewLongPage = (XLogLongPageHeader) NewPage;
2255 
2256  NewLongPage->xlp_sysid = ControlFile->system_identifier;
2257  NewLongPage->xlp_seg_size = wal_segment_size;
2258  NewLongPage->xlp_xlog_blcksz = XLOG_BLCKSZ;
2259  NewPage->xlp_info |= XLP_LONG_HEADER;
2260  }
2261 
2262  /*
2263  * Make sure the initialization of the page becomes visible to others
2264  * before the xlblocks update. GetXLogBuffer() reads xlblocks without
2265  * holding a lock.
2266  */
2267  pg_write_barrier();
2268 
2269  *((volatile XLogRecPtr *) &XLogCtl->xlblocks[nextidx]) = NewPageEndPtr;
2270 
2271  XLogCtl->InitializedUpTo = NewPageEndPtr;
2272 
2273  npages++;
2274  }
2275  LWLockRelease(WALBufMappingLock);
2276 
2277 #ifdef WAL_DEBUG
2278  if (XLOG_DEBUG && npages > 0)
2279  {
2280  elog(DEBUG1, "initialized %d pages, up to %X/%X",
2281  npages, (uint32) (NewPageEndPtr >> 32), (uint32) NewPageEndPtr);
2282  }
2283 #endif
2284 }
XLogRecPtr InitializedUpTo
Definition: xlog.c:634
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
int wal_segment_size
Definition: xlog.c:116
XLogRecPtr * xlblocks
Definition: xlog.c:642
static XLogwrtResult LogwrtResult
Definition: xlog.c:774
slock_t info_lck
Definition: xlog.c:725
#define MemSet(start, val, len)
Definition: c.h:949
static XLogRecPtr WaitXLogInsertionsToFinish(XLogRecPtr upto)
Definition: xlog.c:1785
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
XLogCtlInsert Insert
Definition: xlog.c:599
XLogRecPtr Flush
Definition: xlog.c:441
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define XLP_BKP_REMOVABLE
Definition: xlog_internal.h:81
bool forcePageWrites
Definition: xlog.c:573
uint64 system_identifier
Definition: pg_control.h:106
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
XLogwrtResult LogwrtResult
Definition: xlog.c:622
unsigned int uint32
Definition: c.h:374
static void Insert(File file)
Definition: fd.c:1174
TimeLineID xlp_tli
Definition: xlog_internal.h:40
static void XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
Definition: xlog.c:2412
XLogRecPtr xlp_pageaddr
Definition: xlog_internal.h:41
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr Write
Definition: xlog.c:440
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static ControlFileData * ControlFile
Definition: xlog.c:736
XLogwrtRqst LogwrtRqst
Definition: xlog.c:602
TimeLineID ThisTimeLineID
Definition: xlog.c:191
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
size_t Size
Definition: c.h:473
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define XLogRecPtrToBufIdx(recptr)
Definition: xlog.c:753
XLogRecPtr Write
Definition: xlog.c:446
#define elog(elevel,...)
Definition: elog.h:214
#define pg_write_barrier()
Definition: atomics.h:159
char * pages
Definition: xlog.c:641

◆ assign_checkpoint_completion_target()

void assign_checkpoint_completion_target ( double  newval,
void *  extra 
)

Definition at line 2327 of file xlog.c.

References CalculateCheckpointSegments(), CheckPointCompletionTarget, and newval.

2328 {
2331 }
static void CalculateCheckpointSegments(void)
Definition: xlog.c:2291
#define newval
double CheckPointCompletionTarget
Definition: checkpointer.c:148

◆ assign_max_wal_size()

void assign_max_wal_size ( int  newval,
void *  extra 
)

Definition at line 2320 of file xlog.c.

References CalculateCheckpointSegments(), max_wal_size_mb, and newval.

2321 {
2324 }
static void CalculateCheckpointSegments(void)
Definition: xlog.c:2291
int max_wal_size_mb
Definition: xlog.c:89
#define newval

◆ assign_xlog_sync_method()

void assign_xlog_sync_method ( int  new_sync_method,
void *  extra 
)

Definition at line 10361 of file xlog.c.

References ereport, errcode_for_file_access(), errmsg(), get_sync_bit(), MAXFNAMELEN, openLogFile, openLogSegNo, PANIC, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), sync_method, ThisTimeLineID, WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN, wal_segment_size, XLogFileClose(), and XLogFileName.

10362 {
10363  if (sync_method != new_sync_method)
10364  {
10365  /*
10366  * To ensure that no blocks escape unsynced, force an fsync on the
10367  * currently open log segment (if any). Also, if the open flag is
10368  * changing, close the log file so it will be reopened (with new flag
10369  * bit) at next use.
10370  */
10371  if (openLogFile >= 0)
10372  {
10374  if (pg_fsync(openLogFile) != 0)
10375  {
10376  char xlogfname[MAXFNAMELEN];
10377  int save_errno;
10378 
10379  save_errno = errno;
10382  errno = save_errno;
10383  ereport(PANIC,
10385  errmsg("could not fsync file \"%s\": %m", xlogfname)));
10386  }
10387 
10389  if (get_sync_bit(sync_method) != get_sync_bit(new_sync_method))
10390  XLogFileClose();
10391  }
10392  }
10393 }
int wal_segment_size
Definition: xlog.c:116
static int get_sync_bit(int method)
Definition: xlog.c:10305
#define PANIC
Definition: elog.h:53
static XLogSegNo openLogSegNo
Definition: xlog.c:798
static void XLogFileClose(void)
Definition: xlog.c:3850
int errcode_for_file_access(void)
Definition: elog.c:633
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1381
#define MAXFNAMELEN
static int openLogFile
Definition: xlog.c:797
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define ereport(elevel,...)
Definition: elog.h:144
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1357
int sync_method
Definition: xlog.c:105
int errmsg(const char *fmt,...)
Definition: elog.c:824
int pg_fsync(int fd)
Definition: fd.c:345

◆ BackupInProgress()

bool BackupInProgress ( void  )

Definition at line 11771 of file xlog.c.

References BACKUP_LABEL_FILE, and stat.

Referenced by pg_is_in_backup(), and PostmasterStateMachine().

11772 {
11773  struct stat stat_buf;
11774 
11775  return (stat(BACKUP_LABEL_FILE, &stat_buf) == 0);
11776 }
struct stat stat_buf
Definition: pg_standby.c:100
#define stat(a, b)
Definition: win32_port.h:255
#define BACKUP_LABEL_FILE
Definition: xlog.h:389

◆ BootStrapXLOG()

void BootStrapXLOG ( void  )

Definition at line 5193 of file xlog.c.

References AdvanceOldestClogXid(), Assert, BootStrapCLOG(), BootStrapCommitTs(), BootStrapMultiXact(), BootStrapSUBTRANS(), ControlFileData::checkPoint, ControlFileData::checkPointCopy, close, COMP_CRC32C, ereport, errcode_for_file_access(), errmsg(), FIN_CRC32C, FirstBootstrapObjectId, FirstMultiXactId, FirstNormalTransactionId, CheckPoint::fullPageWrites, fullPageWrites, FullTransactionIdFromEpochAndXid(), gettimeofday(), INIT_CRC32C, InitControlFile(), InvalidTransactionId, MultiXactSetNextMXact(), CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, VariableCacheData::nextOid, CheckPoint::nextXid, VariableCacheData::nextXid, offsetof, VariableCacheData::oidCount, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, CheckPoint::oldestXid, CheckPoint::oldestXidDB, openLogFile, palloc(), PANIC, pfree(), pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), CheckPoint::PrevTimeLineID, ReadControlFile(), CheckPoint::redo, SetCommitTsLimit(), SetMultiXactIdLimit(), SetTransactionIdLimit(), ShmemVariableCache, SizeOfXLogLongPHD, SizeOfXLogRecord, SizeOfXLogRecordDataHeaderShort, CheckPoint::ThisTimeLineID, ThisTimeLineID, CheckPoint::time, ControlFileData::time, TYPEALIGN, WAIT_EVENT_WAL_BOOTSTRAP_SYNC, WAIT_EVENT_WAL_BOOTSTRAP_WRITE, wal_segment_size, write, WriteControlFile(), XLogRecord::xl_info, XLogRecord::xl_prev, XLogRecord::xl_rmid, XLogRecord::xl_tot_len, XLogRecord::xl_xid, XLOG_CHECKPOINT_SHUTDOWN, XLOG_PAGE_MAGIC, XLogFileInit(), XLogPageHeaderData::xlp_info, XLP_LONG_HEADER, XLogPageHeaderData::xlp_magic, XLogPageHeaderData::xlp_pageaddr, XLogLongPageHeaderData::xlp_seg_size, XLogLongPageHeaderData::xlp_sysid, XLogPageHeaderData::xlp_tli, XLogLongPageHeaderData::xlp_xlog_blcksz, and XLR_BLOCK_ID_DATA_SHORT.

Referenced by AuxiliaryProcessMain().

5194 {
5195  CheckPoint checkPoint;
5196  char *buffer;
5197  XLogPageHeader page;
5198  XLogLongPageHeader longpage;
5199  XLogRecord *record;
5200  char *recptr;
5201  bool use_existent;
5202  uint64 sysidentifier;
5203  struct timeval tv;
5204  pg_crc32c crc;
5205 
5206  /*
5207  * Select a hopefully-unique system identifier code for this installation.
5208  * We use the result of gettimeofday(), including the fractional seconds
5209  * field, as being about as unique as we can easily get. (Think not to
5210  * use random(), since it hasn't been seeded and there's no portable way
5211  * to seed it other than the system clock value...) The upper half of the
5212  * uint64 value is just the tv_sec part, while the lower half contains the
5213  * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
5214  * PID for a little extra uniqueness. A person knowing this encoding can
5215  * determine the initialization time of the installation, which could
5216  * perhaps be useful sometimes.
5217  */
5218  gettimeofday(&tv, NULL);
5219  sysidentifier = ((uint64) tv.tv_sec) << 32;
5220  sysidentifier |= ((uint64) tv.tv_usec) << 12;
5221  sysidentifier |= getpid() & 0xFFF;
5222 
5223  /* First timeline ID is always 1 */
5224  ThisTimeLineID = 1;
5225 
5226  /* page buffer must be aligned suitably for O_DIRECT */
5227  buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
5228  page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
5229  memset(page, 0, XLOG_BLCKSZ);
5230 
5231  /*
5232  * Set up information for the initial checkpoint record
5233  *
5234  * The initial checkpoint record is written to the beginning of the WAL
5235  * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not
5236  * used, so that we can use 0/0 to mean "before any valid WAL segment".
5237  */
5238  checkPoint.redo = wal_segment_size + SizeOfXLogLongPHD;
5239  checkPoint.ThisTimeLineID = ThisTimeLineID;
5240  checkPoint.PrevTimeLineID = ThisTimeLineID;
5241  checkPoint.fullPageWrites = fullPageWrites;
5242  checkPoint.nextXid =
5244  checkPoint.nextOid = FirstBootstrapObjectId;
5245  checkPoint.nextMulti = FirstMultiXactId;
5246  checkPoint.nextMultiOffset = 0;
5247  checkPoint.oldestXid = FirstNormalTransactionId;
5248  checkPoint.oldestXidDB = TemplateDbOid;
5249  checkPoint.oldestMulti = FirstMultiXactId;
5250  checkPoint.oldestMultiDB = TemplateDbOid;
5253  checkPoint.time = (pg_time_t) time(NULL);
5255 
5256  ShmemVariableCache->nextXid = checkPoint.nextXid;
5257  ShmemVariableCache->nextOid = checkPoint.nextOid;
5259  MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5260  AdvanceOldestClogXid(checkPoint.oldestXid);
5261  SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5262  SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5264 
5265  /* Set up the XLOG page header */
5266  page->xlp_magic = XLOG_PAGE_MAGIC;
5267  page->xlp_info = XLP_LONG_HEADER;
5268  page->xlp_tli = ThisTimeLineID;
5270  longpage = (XLogLongPageHeader) page;
5271  longpage->xlp_sysid = sysidentifier;
5272  longpage->xlp_seg_size = wal_segment_size;
5273  longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
5274 
5275  /* Insert the initial checkpoint record */
5276  recptr = ((char *) page + SizeOfXLogLongPHD);
5277  record = (XLogRecord *) recptr;
5278  record->xl_prev = 0;
5279  record->xl_xid = InvalidTransactionId;
5280  record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(checkPoint);
5282  record->xl_rmid = RM_XLOG_ID;
5283  recptr += SizeOfXLogRecord;
5284  /* fill the XLogRecordDataHeaderShort struct */
5285  *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
5286  *(recptr++) = sizeof(checkPoint);
5287  memcpy(recptr, &checkPoint, sizeof(checkPoint));
5288  recptr += sizeof(checkPoint);
5289  Assert(recptr - (char *) record == record->xl_tot_len);
5290 
5291  INIT_CRC32C(crc);
5292  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
5293  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
5294  FIN_CRC32C(crc);
5295  record->xl_crc = crc;
5296 
5297  /* Create first XLOG segment file */
5298  use_existent = false;
5299  openLogFile = XLogFileInit(1, &use_existent, false);
5300 
5301  /*
5302  * We needn't bother with Reserve/ReleaseExternalFD here, since we'll
5303  * close the file again in a moment.
5304  */
5305 
5306  /* Write the first page with the initial record */
5307  errno = 0;
5309  if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
5310  {
5311  /* if write didn't set errno, assume problem is no disk space */
5312  if (errno == 0)
5313  errno = ENOSPC;
5314  ereport(PANIC,
5316  errmsg("could not write bootstrap write-ahead log file: %m")));
5317  }
5319 
5321  if (pg_fsync(openLogFile) != 0)
5322  ereport(PANIC,
5324  errmsg("could not fsync bootstrap write-ahead log file: %m")));
5326 
5327  if (close(openLogFile) != 0)
5328  ereport(PANIC,
5330  errmsg("could not close bootstrap write-ahead log file: %m")));
5331 
5332  openLogFile = -1;
5333 
5334  /* Now create pg_control */
5335  InitControlFile(sysidentifier);
5336  ControlFile->time = checkPoint.time;
5337  ControlFile->checkPoint = checkPoint.redo;
5338  ControlFile->checkPointCopy = checkPoint;
5339 
5340  /* some additional ControlFile fields are set in WriteControlFile() */
5341  WriteControlFile();
5342 
5343  /* Bootstrap the commit log, too */
5344  BootStrapCLOG();
5348 
5349  pfree(buffer);
5350 
5351  /*
5352  * Force control file to be read - in contrast to normal processing we'd
5353  * otherwise never run the checks and GUC related initializations therein.
5354  */
5355  ReadControlFile();
5356 }
static void WriteControlFile(void)
Definition: xlog.c:4610
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104
int64 pg_time_t
Definition: pgtime.h:23
int wal_segment_size
Definition: xlog.c:116
pg_time_t time
Definition: pg_control.h:128
void SetCommitTsLimit(TransactionId oldestXact, TransactionId newestXact)
Definition: commit_ts.c:909
uint32 oidCount
Definition: transam.h:200
#define write(a, b, c)
Definition: win32.h:14
#define SizeOfXLogRecordDataHeaderShort
Definition: xlogrecord.h:200
uint32 pg_crc32c
Definition: pg_crc32c.h:38
TransactionId oldestActiveXid
Definition: pg_control.h:63
int XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
Definition: xlog.c:3251
void BootStrapMultiXact(void)
Definition: multixact.c:1868
MultiXactId oldestMulti
Definition: pg_control.h:49
TimeLineID PrevTimeLineID
Definition: pg_control.h:40
static void InitControlFile(uint64 sysidentifier)
Definition: xlog.c:4575
RmgrId xl_rmid
Definition: xlogrecord.h:47
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
CheckPoint checkPointCopy
Definition: pg_control.h:131
TransactionId oldestXid
Definition: pg_control.h:47
FullTransactionId nextXid
Definition: transam.h:205
pg_time_t time
Definition: pg_control.h:51
#define PANIC
Definition: elog.h:53
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
bool fullPageWrites
Definition: xlog.c:97
void BootStrapSUBTRANS(void)
Definition: subtrans.c:212
MultiXactOffset nextMultiOffset
Definition: pg_control.h:46
void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid)
Definition: varsup.c:313
TransactionId oldestCommitTsXid
Definition: pg_control.h:52
void pfree(void *pointer)
Definition: mcxt.c:1056
#define FirstNormalTransactionId
Definition: transam.h:34
uint32 xl_tot_len
Definition: xlogrecord.h:43
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
static void ReadControlFile(void)
Definition: xlog.c:4701
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:67
int errcode_for_file_access(void)
Definition: elog.c:633
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
#define FirstBootstrapObjectId
Definition: transam.h:181
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1381
#define FirstMultiXactId
Definition: multixact.h:24
TimeLineID xlp_tli
Definition: xlog_internal.h:40
XLogRecPtr xlp_pageaddr
Definition: xlog_internal.h:41
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
TransactionId newestCommitTsXid
Definition: pg_control.h:54
Oid oldestMultiDB
Definition: pg_control.h:50
static int openLogFile
Definition: xlog.c:797
static ControlFileData * ControlFile
Definition: xlog.c:736
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2196
TimeLineID ThisTimeLineID
Definition: xlog.c:191
Oid nextOid
Definition: pg_control.h:44
#define ereport(elevel,...)
Definition: elog.h:144
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:691
bool fullPageWrites
Definition: pg_control.h:42
void BootStrapCLOG(void)
Definition: clog.c:705
#define Assert(condition)
Definition: c.h:745
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
Oid oldestXidDB
Definition: pg_control.h:48
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1357
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:330
uint8 xl_info
Definition: xlogrecord.h:46
MultiXactId nextMulti
Definition: pg_control.h:45
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:223
TransactionId xl_xid
Definition: xlogrecord.h:44
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
int pg_fsync(int fd)
Definition: fd.c:345
#define close(a)
Definition: win32.h:12
void BootStrapCommitTs(void)
Definition: commit_ts.c:584
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
XLogRecPtr checkPoint
Definition: pg_control.h:129
XLogRecPtr redo
Definition: pg_control.h:37
#define offsetof(type, field)
Definition: c.h:668
FullTransactionId nextXid
Definition: pg_control.h:43
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72
void MultiXactSetNextMXact(MultiXactId nextMulti, MultiXactOffset nextMultiOffset)
Definition: multixact.c:2162

◆ CalculateCheckpointSegments()

static void CalculateCheckpointSegments ( void  )
static

Definition at line 2291 of file xlog.c.

References CheckPointCompletionTarget, CheckPointSegments, ConvertToXSegs, max_wal_size_mb, and wal_segment_size.

Referenced by assign_checkpoint_completion_target(), assign_max_wal_size(), and ReadControlFile().

2292 {
2293  double target;
2294 
2295  /*-------
2296  * Calculate the distance at which to trigger a checkpoint, to avoid
2297  * exceeding max_wal_size_mb. This is based on two assumptions:
2298  *
2299  * a) we keep WAL for only one checkpoint cycle (prior to PG11 we kept
2300  * WAL for two checkpoint cycles to allow us to recover from the
2301  * secondary checkpoint if the first checkpoint failed, though we
2302  * only did this on the primary anyway, not on standby. Keeping just
2303  * one checkpoint simplifies processing and reduces disk space in
2304  * many smaller databases.)
2305  * b) during checkpoint, we consume checkpoint_completion_target *
2306  * number of segments consumed between checkpoints.
2307  *-------
2308  */
2309  target = (double) ConvertToXSegs(max_wal_size_mb, wal_segment_size) /
2311 
2312  /* round down */
2313  CheckPointSegments = (int) target;
2314 
2315  if (CheckPointSegments < 1)
2316  CheckPointSegments = 1;
2317 }
#define ConvertToXSegs(x, segsize)
Definition: xlog.c:765
int wal_segment_size
Definition: xlog.c:116
int max_wal_size_mb
Definition: xlog.c:89
int CheckPointSegments
Definition: xlog.c:129
double CheckPointCompletionTarget
Definition: checkpointer.c:148

◆ CancelBackup()

void CancelBackup ( void  )

Definition at line 11791 of file xlog.c.

References BACKUP_LABEL_FILE, BACKUP_LABEL_OLD, DEBUG1, durable_rename(), ereport, errcode_for_file_access(), errdetail(), errmsg(), LOG, stat, TABLESPACE_MAP, TABLESPACE_MAP_OLD, and WARNING.

Referenced by PostmasterStateMachine().

11792 {
11793  struct stat stat_buf;
11794 
11795  /* if the backup_label file is not there, return */
11796  if (stat(BACKUP_LABEL_FILE, &stat_buf) < 0)
11797  return;
11798 
11799  /* remove leftover file from previously canceled backup if it exists */
11800  unlink(BACKUP_LABEL_OLD);
11801 
11803  {
11804  ereport(WARNING,
11806  errmsg("online backup mode was not canceled"),
11807  errdetail("File \"%s\" could not be renamed to \"%s\": %m.",
11809  return;
11810  }
11811 
11812  /* if the tablespace_map file is not there, return */
11813  if (stat(TABLESPACE_MAP, &stat_buf) < 0)
11814  {
11815  ereport(LOG,
11816  (errmsg("online backup mode canceled"),
11817  errdetail("File \"%s\" was renamed to \"%s\".",
11819  return;
11820  }
11821 
11822  /* remove leftover file from previously canceled backup if it exists */
11823  unlink(TABLESPACE_MAP_OLD);
11824 
11826  {
11827  ereport(LOG,
11828  (errmsg("online backup mode canceled"),
11829  errdetail("Files \"%s\" and \"%s\" were renamed to "
11830  "\"%s\" and \"%s\", respectively.",
11833  }
11834  else
11835  {
11836  ereport(WARNING,
11838  errmsg("online backup mode canceled"),
11839  errdetail("File \"%s\" was renamed to \"%s\", but "
11840  "file \"%s\" could not be renamed to \"%s\": %m.",
11843  }
11844 }
#define DEBUG1
Definition: elog.h:25
#define LOG
Definition: elog.h:26
#define BACKUP_LABEL_OLD
Definition: xlog.h:390
#define TABLESPACE_MAP
Definition: xlog.h:392
struct stat stat_buf
Definition: pg_standby.c:100
int errdetail(const char *fmt,...)
Definition: elog.c:957
int errcode_for_file_access(void)
Definition: elog.c:633
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:658
#define WARNING
Definition: elog.h:40
#define stat(a, b)
Definition: win32_port.h:255
#define ereport(elevel,...)
Definition: elog.h:144
#define TABLESPACE_MAP_OLD
Definition: xlog.h:393
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define BACKUP_LABEL_FILE
Definition: xlog.h:389

◆ check_wal_buffers()

bool check_wal_buffers ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 4986 of file xlog.c.

References XLOGbuffers, and XLOGChooseNumBuffers().

4987 {
4988  /*
4989  * -1 indicates a request for auto-tune.
4990  */
4991  if (*newval == -1)
4992  {
4993  /*
4994  * If we haven't yet changed the boot_val default of -1, just let it
4995  * be. We'll fix it when XLOGShmemSize is called.
4996  */
4997  if (XLOGbuffers == -1)
4998  return true;
4999 
5000  /* Otherwise, substitute the auto-tune value */
5002  }
5003 
5004  /*
5005  * We clamp manually-set values to at least 4 blocks. Prior to PostgreSQL
5006  * 9.1, a minimum of 4 was enforced by guc.c, but since that is no longer
5007  * the case, we just silently treat such values as a request for the
5008  * minimum. (We could throw an error instead, but that doesn't seem very
5009  * helpful.)
5010  */
5011  if (*newval < 4)
5012  *newval = 4;
5013 
5014  return true;
5015 }
static int XLOGChooseNumBuffers(void)
Definition: xlog.c:4970
#define newval
int XLOGbuffers
Definition: xlog.c:92

◆ CheckForStandbyTrigger()

static bool CheckForStandbyTrigger ( void  )
static

Definition at line 12585 of file xlog.c.

References CheckPromoteSignal(), ereport, errcode_for_file_access(), errmsg(), ERROR, IsPromoteSignaled(), LocalPromoteIsTriggered, LOG, PromoteTriggerFile, RemovePromoteSignalFiles(), ResetPromoteSignaled(), SetPromoteIsTriggered(), and stat.

Referenced by ReadRecord(), recoveryApplyDelay(), recoveryPausesHere(), and WaitForWALToBecomeAvailable().

12586 {
12587  struct stat stat_buf;
12588 
12590  return true;
12591 
12593  {
12594  ereport(LOG, (errmsg("received promote request")));
12598  return true;
12599  }
12600 
12601  if (PromoteTriggerFile == NULL || strcmp(PromoteTriggerFile, "") == 0)
12602  return false;
12603 
12604  if (stat(PromoteTriggerFile, &stat_buf) == 0)
12605  {
12606  ereport(LOG,
12607  (errmsg("promote trigger file found: %s", PromoteTriggerFile)));
12608  unlink(PromoteTriggerFile);
12610  return true;
12611  }
12612  else if (errno != ENOENT)
12613  ereport(ERROR,
12615  errmsg("could not stat promote trigger file \"%s\": %m",
12616  PromoteTriggerFile)));
12617 
12618  return false;
12619 }
char * PromoteTriggerFile
Definition: xlog.c:296
bool CheckPromoteSignal(void)
Definition: xlog.c:12634
bool IsPromoteSignaled(void)
Definition: startup.c:234
static bool LocalPromoteIsTriggered
Definition: xlog.c:240
#define LOG
Definition: elog.h:26
static void SetPromoteIsTriggered(void)
Definition: xlog.c:12571
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:100
int errcode_for_file_access(void)
Definition: elog.c:633
void RemovePromoteSignalFiles(void)
Definition: xlog.c:12625
#define stat(a, b)
Definition: win32_port.h:255
#define ereport(elevel,...)
Definition: elog.h:144
void ResetPromoteSignaled(void)
Definition: startup.c:240
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ CheckPointGuts()

static void CheckPointGuts ( XLogRecPtr  checkPointRedo,
int  flags 
)
static

Definition at line 9172 of file xlog.c.

References CheckPointBuffers(), CheckPointCLOG(), CheckPointCommitTs(), CheckPointLogicalRewriteHeap(), CheckPointMultiXact(), CheckPointPredicate(), CheckPointRelationMap(), CheckPointReplicationOrigin(), CheckPointReplicationSlots(), CheckPointSnapBuild(), CheckPointSUBTRANS(), and CheckPointTwoPhase().

Referenced by CreateCheckPoint(), and CreateRestartPoint().

9173 {
9174  CheckPointCLOG();
9183  CheckPointBuffers(flags); /* performs all required fsyncs */
9185  /* We deliberately delay 2PC checkpointing as long as possible */
9186  CheckPointTwoPhase(checkPointRedo);
9187 }
void CheckPointBuffers(int flags)
Definition: bufmgr.c:2637
void CheckPointLogicalRewriteHeap(void)
Definition: rewriteheap.c:1195
void CheckPointReplicationOrigin(void)
Definition: origin.c:540
void CheckPointSnapBuild(void)
Definition: snapbuild.c:1932
void CheckPointCLOG(void)
Definition: clog.c:835
void CheckPointMultiXact(void)
Definition: multixact.c:2142
void CheckPointCommitTs(void)
Definition: commit_ts.c:821
void CheckPointSUBTRANS(void)
Definition: subtrans.c:302
void CheckPointRelationMap(void)
Definition: relmapper.c:546
void CheckPointPredicate(void)
Definition: predicate.c:1015
void CheckPointTwoPhase(XLogRecPtr redo_horizon)
Definition: twophase.c:1699
void CheckPointReplicationSlots(void)
Definition: slot.c:1249

◆ CheckPromoteSignal()

bool CheckPromoteSignal ( void  )

Definition at line 12634 of file xlog.c.

References PROMOTE_SIGNAL_FILE, and stat.

Referenced by CheckForStandbyTrigger(), and sigusr1_handler().

12635 {
12636  struct stat stat_buf;
12637 
12638  if (stat(PROMOTE_SIGNAL_FILE, &stat_buf) == 0)
12639  return true;
12640 
12641  return false;
12642 }
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:396
struct stat stat_buf
Definition: pg_standby.c:100
#define stat(a, b)
Definition: win32_port.h:255

◆ CheckRecoveryConsistency()

static void CheckRecoveryConsistency ( void  )
static

Definition at line 7968 of file xlog.c.

References Assert, ControlFileData::backupEndPoint, ControlFileData::backupEndRequired, ControlFileData::backupStartPoint, DEBUG1, elog, ereport, errmsg(), InArchiveRecovery, XLogCtlData::info_lck, InvalidXLogRecPtr, IsUnderPostmaster, XLogCtlData::lastReplayedEndRecPtr, LocalHotStandbyActive, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ControlFileData::minRecoveryPoint, minRecoveryPoint, PMSIGNAL_BEGIN_HOT_STANDBY, reachedConsistency, SendPostmasterSignal(), XLogCtlData::SharedHotStandbyActive, SpinLockAcquire, SpinLockRelease, STANDBY_SNAPSHOT_READY, standbyState, UpdateControlFile(), XLogCheckInvalidPages(), and XLogRecPtrIsInvalid.

Referenced by ReadRecord(), and StartupXLOG().

7969 {
7970  XLogRecPtr lastReplayedEndRecPtr;
7971 
7972  /*
7973  * During crash recovery, we don't reach a consistent state until we've
7974  * replayed all the WAL.
7975  */
7977  return;
7978 
7980 
7981  /*
7982  * assume that we are called in the startup process, and hence don't need
7983  * a lock to read lastReplayedEndRecPtr
7984  */
7985  lastReplayedEndRecPtr = XLogCtl->lastReplayedEndRecPtr;
7986 
7987  /*
7988  * Have we reached the point where our base backup was completed?
7989  */
7991  ControlFile->backupEndPoint <= lastReplayedEndRecPtr)
7992  {
7993  /*
7994  * We have reached the end of base backup, as indicated by pg_control.
7995  * The data on disk is now consistent. Reset backupStartPoint and
7996  * backupEndPoint, and update minRecoveryPoint to make sure we don't
7997  * allow starting up at an earlier point even if recovery is stopped
7998  * and restarted soon after this.
7999  */
8000  elog(DEBUG1, "end of backup reached");
8001 
8002  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8003 
8004  if (ControlFile->minRecoveryPoint < lastReplayedEndRecPtr)
8005  ControlFile->minRecoveryPoint = lastReplayedEndRecPtr;
8006 
8009  ControlFile->backupEndRequired = false;
8011 
8012  LWLockRelease(ControlFileLock);
8013  }
8014 
8015  /*
8016  * Have we passed our safe starting point? Note that minRecoveryPoint is
8017  * known to be incorrectly set if ControlFile->backupEndRequired, until
8018  * the XLOG_BACKUP_END arrives to advise us of the correct
8019  * minRecoveryPoint. All we know prior to that is that we're not
8020  * consistent yet.
8021  */
8023  minRecoveryPoint <= lastReplayedEndRecPtr &&
8025  {
8026  /*
8027  * Check to see if the XLOG sequence contained any unresolved
8028  * references to uninitialized pages.
8029  */
8031 
8032  reachedConsistency = true;
8033  ereport(LOG,
8034  (errmsg("consistent recovery state reached at %X/%X",
8035  (uint32) (lastReplayedEndRecPtr >> 32),
8036  (uint32) lastReplayedEndRecPtr)));
8037  }
8038 
8039  /*
8040  * Have we got a valid starting snapshot that will allow queries to be
8041  * run? If so, we can tell postmaster that the database is consistent now,
8042  * enabling connections.
8043  */
8048  {
8052 
8053  LocalHotStandbyActive = true;
8054 
8056  }
8057 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void XLogCheckInvalidPages(void)
Definition: xlogutils.c:224
bool SharedHotStandbyActive
Definition: xlog.c:664
slock_t info_lck
Definition: xlog.c:725
#define LOG
Definition: elog.h:26
bool InArchiveRecovery
Definition: xlog.c:266
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
bool backupEndRequired
Definition: pg_control.h:170
#define SpinLockAcquire(lock)
Definition: spin.h:62
static bool LocalHotStandbyActive
Definition: xlog.c:234
void UpdateControlFile(void)
Definition: xlog.c:4901
bool IsUnderPostmaster
Definition: globals.c:109
unsigned int uint32
Definition: c.h:374
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
static ControlFileData * ControlFile
Definition: xlog.c:736
#define ereport(elevel,...)
Definition: elog.h:144
XLogRecPtr backupEndPoint
Definition: pg_control.h:169
bool reachedConsistency
Definition: xlog.c:869
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:146
HotStandbyState standbyState
Definition: xlog.c:207
XLogRecPtr backupStartPoint
Definition: pg_control.h:168
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:166
static XLogRecPtr minRecoveryPoint
Definition: xlog.c:860
XLogRecPtr lastReplayedEndRecPtr
Definition: xlog.c:704

◆ CheckRequiredParameterValues()

static void CheckRequiredParameterValues ( void  )
static

Definition at line 6255 of file xlog.c.

References ArchiveRecoveryRequested, EnableHotStandby, ereport, errhint(), errmsg(), ERROR, max_locks_per_xact, ControlFileData::max_locks_per_xact, max_prepared_xacts, ControlFileData::max_prepared_xacts, max_wal_senders, ControlFileData::max_wal_senders, max_worker_processes, ControlFileData::max_worker_processes, MaxConnections, ControlFileData::MaxConnections, RecoveryRequiresIntParameter, ControlFileData::wal_level, WAL_LEVEL_MINIMAL, WAL_LEVEL_REPLICA, and WARNING.

Referenced by StartupXLOG(), and xlog_redo().

6256 {
6257  /*
6258  * For archive recovery, the WAL must be generated with at least 'replica'
6259  * wal_level.
6260  */
6262  {
6263  ereport(WARNING,
6264  (errmsg("WAL was generated with wal_level=minimal, data may be missing"),
6265  errhint("This happens if you temporarily set wal_level=minimal without taking a new base backup.")));
6266  }
6267 
6268  /*
6269  * For Hot Standby, the WAL must be generated with 'replica' mode, and we
6270  * must have at least as many backend slots as the primary.
6271  */
6273  {
6275  ereport(ERROR,
6276  (errmsg("hot standby is not possible because wal_level was not set to \"replica\" or higher on the primary server"),
6277  errhint("Either set wal_level to \"replica\" on the primary, or turn off hot_standby here.")));
6278 
6279  /* We ignore autovacuum_max_workers when we make this test. */
6280  RecoveryRequiresIntParameter("max_connections",
6283  RecoveryRequiresIntParameter("max_worker_processes",
6286  RecoveryRequiresIntParameter("max_wal_senders",
6289  RecoveryRequiresIntParameter("max_prepared_transactions",
6292  RecoveryRequiresIntParameter("max_locks_per_transaction",
6295  }
6296 }
bool ArchiveRecoveryRequested
Definition: xlog.c:265
int max_locks_per_xact
Definition: pg_control.h:182
int errhint(const char *fmt,...)
Definition: elog.c:1071
int max_prepared_xacts
Definition: pg_control.h:181
int max_worker_processes
Definition: pg_control.h:179
#define ERROR
Definition: elog.h:43
int max_prepared_xacts
Definition: twophase.c:117
int max_locks_per_xact
Definition: lock.c:54
int max_wal_senders
Definition: walsender.c:121
#define WARNING
Definition: elog.h:40
int MaxConnections
Definition: globals.c:133
static ControlFileData * ControlFile
Definition: xlog.c:736
#define ereport(elevel,...)
Definition: elog.h:144
bool EnableHotStandby
Definition: xlog.c:96
int errmsg(const char *fmt,...)
Definition: elog.c:824
int max_worker_processes
Definition: globals.c:134
#define RecoveryRequiresIntParameter(param_name, currValue, minValue)
Definition: xlog.c:6235

◆ checkTimeLineSwitch()

static void checkTimeLineSwitch ( XLogRecPtr  lsn,
TimeLineID  newTLI,
TimeLineID  prevTLI 
)
static

Definition at line 9850 of file xlog.c.

References ereport, errmsg(), minRecoveryPoint, minRecoveryPointTLI, PANIC, ThisTimeLineID, tliInHistory(), and XLogRecPtrIsInvalid.

Referenced by StartupXLOG().

9851 {
9852  /* Check that the record agrees on what the current (old) timeline is */
9853  if (prevTLI != ThisTimeLineID)
9854  ereport(PANIC,
9855  (errmsg("unexpected previous timeline ID %u (current timeline ID %u) in checkpoint record",
9856  prevTLI, ThisTimeLineID)));
9857 
9858  /*
9859  * The new timeline better be in the list of timelines we expect to see,
9860  * according to the timeline history. It should also not decrease.
9861  */
9862  if (newTLI < ThisTimeLineID || !tliInHistory(newTLI, expectedTLEs))
9863  ereport(PANIC,
9864  (errmsg("unexpected timeline ID %u (after %u) in checkpoint record",
9865  newTLI, ThisTimeLineID)));
9866 
9867  /*
9868  * If we have not yet reached min recovery point, and we're about to
9869  * switch to a timeline greater than the timeline of the min recovery
9870  * point: trouble. After switching to the new timeline, we could not
9871  * possibly visit the min recovery point on the correct timeline anymore.
9872  * This can happen if there is a newer timeline in the archive that
9873  * branched before the timeline the min recovery point is on, and you
9874  * attempt to do PITR to the new timeline.
9875  */
9877  lsn < minRecoveryPoint &&
9878  newTLI > minRecoveryPointTLI)
9879  ereport(PANIC,
9880  (errmsg("unexpected timeline ID %u in checkpoint record, before reaching minimum recovery point %X/%X on timeline %u",
9881  newTLI,
9882  (uint32) (minRecoveryPoint >> 32),
9885 
9886  /* Looks good */
9887 }
static List * expectedTLEs
Definition: xlog.c:341
#define PANIC
Definition: elog.h:53
unsigned int uint32
Definition: c.h:374
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
static TimeLineID minRecoveryPointTLI
Definition: xlog.c:861
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
bool tliInHistory(TimeLineID tli, List *expectedTLEs)
Definition: timeline.c:534
static XLogRecPtr minRecoveryPoint
Definition: xlog.c:860

◆ checkXLogConsistency()

static void checkXLogConsistency ( XLogReaderState record)
static

Definition at line 1404 of file xlog.c.

References Assert, buf, BUFFER_LOCK_EXCLUSIVE, BufferGetPage, BufferIsValid, RelFileNode::dbNode, elog, XLogReaderState::EndRecPtr, ERROR, FATAL, LockBuffer(), XLogReaderState::max_block_id, PageGetLSN, primary_image_masked, RBM_NORMAL_NO_LOG, RelFileNode::relNode, replay_image_masked, RestoreBlockImage(), RmgrData::rm_mask, RmgrTable, RelFileNode::spcNode, UnlockReleaseBuffer(), XLogReadBufferExtended(), XLogRecBlockImageApply, XLogRecGetBlockTag(), XLogRecGetInfo, XLogRecGetRmid, XLogRecHasAnyBlockRefs, XLogRecHasBlockImage, and XLR_CHECK_CONSISTENCY.

Referenced by StartupXLOG().

1405 {
1406  RmgrId rmid = XLogRecGetRmid(record);
1407  RelFileNode rnode;
1408  ForkNumber forknum;
1409  BlockNumber blkno;
1410  int block_id;
1411 
1412  /* Records with no backup blocks have no need for consistency checks. */
1413  if (!XLogRecHasAnyBlockRefs(record))
1414  return;
1415 
1416  Assert((XLogRecGetInfo(record) & XLR_CHECK_CONSISTENCY) != 0);
1417 
1418  for (block_id = 0; block_id <= record->max_block_id; block_id++)
1419  {
1420  Buffer buf;
1421  Page page;
1422 
1423  if (!XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blkno))
1424  {
1425  /*
1426  * WAL record doesn't contain a block reference with the given id.
1427  * Do nothing.
1428  */
1429  continue;
1430  }
1431 
1432  Assert(XLogRecHasBlockImage(record, block_id));
1433 
1434  if (XLogRecBlockImageApply(record, block_id))
1435  {
1436  /*
1437  * WAL record has already applied the page, so bypass the
1438  * consistency check as that would result in comparing the full
1439  * page stored in the record with itself.
1440  */
1441  continue;
1442  }
1443 
1444  /*
1445  * Read the contents from the current buffer and store it in a
1446  * temporary page.
1447  */
1448  buf = XLogReadBufferExtended(rnode, forknum, blkno,
1450  if (!BufferIsValid(buf))
1451  continue;
1452 
1454  page = BufferGetPage(buf);
1455 
1456  /*
1457  * Take a copy of the local page where WAL has been applied to have a
1458  * comparison base before masking it...
1459  */
1460  memcpy(replay_image_masked, page, BLCKSZ);
1461 
1462  /* No need for this page anymore now that a copy is in. */
1463  UnlockReleaseBuffer(buf);
1464 
1465  /*
1466  * If the block LSN is already ahead of this WAL record, we can't
1467  * expect contents to match. This can happen if recovery is
1468  * restarted.
1469  */
1470  if (PageGetLSN(replay_image_masked) > record->EndRecPtr)
1471  continue;
1472 
1473  /*
1474  * Read the contents from the backup copy, stored in WAL record and
1475  * store it in a temporary page. There is no need to allocate a new
1476  * page here, a local buffer is fine to hold its contents and a mask
1477  * can be directly applied on it.
1478  */
1479  if (!RestoreBlockImage(record, block_id, primary_image_masked))
1480  elog(ERROR, "failed to restore block image");
1481 
1482  /*
1483  * If masking function is defined, mask both the primary and replay
1484  * images
1485  */
1486  if (RmgrTable[rmid].rm_mask != NULL)
1487  {
1488  RmgrTable[rmid].rm_mask(replay_image_masked, blkno);
1489  RmgrTable[rmid].rm_mask(primary_image_masked, blkno);
1490  }
1491 
1492  /* Time to compare the primary and replay images. */
1493  if (memcmp(replay_image_masked, primary_image_masked, BLCKSZ) != 0)
1494  {
1495  elog(FATAL,
1496  "inconsistent page found, rel %u/%u/%u, forknum %u, blkno %u",
1497  rnode.spcNode, rnode.dbNode, rnode.relNode,
1498  forknum, blkno);
1499  }
1500  }
1501 }
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:315
Buffer XLogReadBufferExtended(RelFileNode rnode, ForkNumber forknum, BlockNumber blkno, ReadBufferMode mode)
Definition: xlogutils.c:442
const RmgrData RmgrTable[RM_MAX_ID+1]
Definition: rmgr.c:36
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define XLR_CHECK_CONSISTENCY
Definition: xlogrecord.h:80
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
static char * replay_image_masked
Definition: xlog.c:275
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static char * buf
Definition: pg_test_fsync.c:67
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
ForkNumber
Definition: relpath.h:40
void(* rm_mask)(char *pagedata, BlockNumber blkno)
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1497
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
uint8 RmgrId
Definition: rmgr.h:11
#define Assert(condition)
Definition: c.h:745
static char * primary_image_masked
Definition: xlog.c:276
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
Definition: xlogreader.c:1550
#define PageGetLSN(page)
Definition: bufpage.h:366
#define elog(elevel,...)
Definition: elog.h:214
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:312
#define XLogRecBlockImageApply(decoder, block_id)
Definition: xlogreader.h:317
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:306

◆ CheckXLogRemoved()

void CheckXLogRemoved ( XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 3925 of file xlog.c.

References ereport, errcode_for_file_access(), errmsg(), ERROR, filename, XLogCtlData::info_lck, XLogCtlData::lastRemovedSegNo, MAXFNAMELEN, SpinLockAcquire, SpinLockRelease, wal_segment_size, and XLogFileName.

Referenced by logical_read_xlog_page(), perform_base_backup(), and XLogSendPhysical().

3926 {
3927  int save_errno = errno;
3928  XLogSegNo lastRemovedSegNo;
3929 
3931  lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3933 
3934  if (segno <= lastRemovedSegNo)
3935  {
3936  char filename[MAXFNAMELEN];
3937 
3938  XLogFileName(filename, tli, segno, wal_segment_size);
3939  errno = save_errno;
3940  ereport(ERROR,
3942  errmsg("requested WAL segment %s has already been removed",
3943  filename)));
3944  }
3945  errno = save_errno;
3946 }
int wal_segment_size
Definition: xlog.c:116
slock_t info_lck
Definition: xlog.c:725
XLogSegNo lastRemovedSegNo
Definition: xlog.c:608
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
uint64 XLogSegNo
Definition: xlogdefs.h:41
int errcode_for_file_access(void)
Definition: elog.c:633
#define MAXFNAMELEN
#define SpinLockRelease(lock)
Definition: spin.h:64
#define ereport(elevel,...)
Definition: elog.h:144
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static XLogCtlData * XLogCtl
Definition: xlog.c:728
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ CleanupBackupHistory()

static void CleanupBackupHistory ( void  )
static

Definition at line 4289 of file xlog.c.

References AllocateDir(), dirent::d_name, DEBUG2, elog, FreeDir(), IsBackupHistoryFileName, MAXPGPATH, ReadDir(), snprintf, XLogArchiveCheckDone(), XLogArchiveCleanup(), and XLOGDIR.

Referenced by do_pg_stop_backup().

4290 {
4291  DIR *xldir;
4292  struct dirent *xlde;
4293  char path[MAXPGPATH + sizeof(XLOGDIR)];
4294 
4295  xldir = AllocateDir(XLOGDIR);
4296 
4297  while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
4298  {
4299  if (IsBackupHistoryFileName(xlde->d_name))
4300  {
4301  if (XLogArchiveCheckDone(xlde->d_name))
4302  {
4303  elog(DEBUG2, "removing WAL backup history file \"%s\"",
4304  xlde->d_name);
4305  snprintf(path, sizeof(path), XLOGDIR "/%s", xlde->d_name);
4306  unlink(path);
4307  XLogArchiveCleanup(xlde->d_name);
4308  }
4309  }
4310  }
4311 
4312  FreeDir(xldir);
4313 }
Definition: dirent.h:9
void XLogArchiveCleanup(const char *xlog)
Definition: xlogarchive.c:718
Definition: dirent.c:25
bool XLogArchiveCheckDone(const char *xlog)
Definition: xlogarchive.c:571
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2583
#define IsBackupHistoryFileName(fname)
#define XLOGDIR
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
#define elog(elevel,...)
Definition: elog.h:214
char d_name[MAX_PATH]
Definition: dirent.h:14
#define snprintf
Definition: port.h:193
int FreeDir(DIR *dir)
Definition: fd.c:2701

◆ CopyXLogRecordToWAL()

static void CopyXLogRecordToWAL ( int  write_len,
bool  isLogSwitch,
XLogRecData rdata,
XLogRecPtr  StartPos,
XLogRecPtr  EndPos 
)
static

Definition at line 1508 of file xlog.c.

References Assert, XLogRecData::data, elog, GetXLogBuffer(), INSERT_FREESPACE, XLogRecData::len, MAXALIGN64, MemSet, XLogRecData::next, PANIC, SizeOfXLogLongPHD, SizeOfXLogRecord, SizeOfXLogShortPHD, wal_segment_size, XLogSegmentOffset, XLP_FIRST_IS_CONTRECORD, XLogPageHeaderData::xlp_info, and XLogPageHeaderData::xlp_rem_len.

Referenced by XLogInsertRecord().

1510 {
1511  char *currpos;
1512  int freespace;
1513  int written;
1514  XLogRecPtr CurrPos;
1515  XLogPageHeader pagehdr;
1516 
1517  /*
1518  * Get a pointer to the right place in the right WAL buffer to start
1519  * inserting to.
1520  */
1521  CurrPos = StartPos;
1522  currpos = GetXLogBuffer(CurrPos);
1523  freespace = INSERT_FREESPACE(CurrPos);
1524 
1525  /*
1526  * there should be enough space for at least the first field (xl_tot_len)
1527  * on this page.
1528  */
1529  Assert(freespace >= sizeof(uint32));
1530 
1531  /* Copy record data */
1532  written = 0;
1533  while (rdata != NULL)
1534  {
1535  char *rdata_data = rdata->data;
1536  int rdata_len = rdata->len;
1537 
1538  while (rdata_len > freespace)
1539  {
1540  /*
1541  * Write what fits on this page, and continue on the next page.
1542  */
1543  Assert(CurrPos % XLOG_BLCKSZ >= SizeOfXLogShortPHD || freespace == 0);
1544  memcpy(currpos, rdata_data, freespace);
1545  rdata_data += freespace;
1546  rdata_len -= freespace;
1547  written += freespace;
1548  CurrPos += freespace;
1549 
1550  /*
1551  * Get pointer to beginning of next page, and set the xlp_rem_len
1552  * in the page header. Set XLP_FIRST_IS_CONTRECORD.
1553  *
1554  * It's safe to set the contrecord flag and xlp_rem_len without a
1555  * lock on the page. All the other flags were already set when the
1556  * page was initialized, in AdvanceXLInsertBuffer, and we're the
1557  * only backend that needs to set the contrecord flag.
1558  */
1559  currpos = GetXLogBuffer(CurrPos);
1560  pagehdr = (XLogPageHeader) currpos;
1561  pagehdr->xlp_rem_len = write_len - written;
1562  pagehdr->xlp_info |= XLP_FIRST_IS_CONTRECORD;
1563 
1564  /* skip over the page header */
1565  if (XLogSegmentOffset(CurrPos, wal_segment_size) == 0)
1566  {
1567  CurrPos += SizeOfXLogLongPHD;
1568  currpos += SizeOfXLogLongPHD;
1569  }
1570  else
1571  {
1572  CurrPos += SizeOfXLogShortPHD;
1573  currpos += SizeOfXLogShortPHD;
1574  }
1575  freespace = INSERT_FREESPACE(CurrPos);
1576  }
1577 
1578  Assert(CurrPos % XLOG_BLCKSZ >= SizeOfXLogShortPHD || rdata_len == 0);
1579  memcpy(currpos, rdata_data, rdata_len);
1580  currpos += rdata_len;
1581  CurrPos += rdata_len;
1582  freespace -= rdata_len;
1583  written += rdata_len;
1584 
1585  rdata = rdata->next;
1586  }
1587  Assert(written == write_len);
1588 
1589  /*
1590  * If this was an xlog-switch, it's not enough to write the switch record,
1591  * we also have to consume all the remaining space in the WAL segment. We
1592  * have already reserved that space, but we need to actually fill it.
1593  */
1594  if (isLogSwitch && XLogSegmentOffset(CurrPos, wal_segment_size) != 0)
1595  {
1596  /* An xlog-switch record doesn't contain any data besides the header */
1597  Assert(write_len == SizeOfXLogRecord);
1598 
1599  /* Assert that we did reserve the right amount of space */
1600  Assert(XLogSegmentOffset(EndPos, wal_segment_size) == 0);
1601 
1602  /* Use up all the remaining space on the current page */
1603  CurrPos += freespace;
1604 
1605  /*
1606  * Cause all remaining pages in the segment to be flushed, leaving the
1607  * XLog position where it should be, at the start of the next segment.
1608  * We do this one page at a time, to make sure we don't deadlock
1609  * against ourselves if wal_buffers < wal_segment_size.
1610  */
1611  while (CurrPos < EndPos)
1612  {
1613  /*
1614  * The minimal action to flush the page would be to call
1615  * WALInsertLockUpdateInsertingAt(CurrPos) followed by
1616  * AdvanceXLInsertBuffer(...). The page would be left initialized
1617  * mostly to zeros, except for the page header (always the short
1618  * variant, as this is never a segment's first page).
1619  *
1620  * The large vistas of zeros are good for compressibility, but the
1621  * headers interrupting them every XLOG_BLCKSZ (with values that
1622  * differ from page to page) are not. The effect varies with
1623  * compression tool, but bzip2 for instance compresses about an
1624  * order of magnitude worse if those headers are left in place.
1625  *
1626  * Rather than complicating AdvanceXLInsertBuffer itself (which is
1627  * called in heavily-loaded circumstances as well as this lightly-
1628  * loaded one) with variant behavior, we just use GetXLogBuffer
1629  * (which itself calls the two methods we need) to get the pointer
1630  * and zero most of the page. Then we just zero the page header.
1631  */
1632  currpos = GetXLogBuffer(CurrPos);
1633  MemSet(currpos, 0, SizeOfXLogShortPHD);
1634 
1635  CurrPos += XLOG_BLCKSZ;
1636  }
1637  }
1638  else
1639  {
1640  /* Align the end position, so that the next record starts aligned */
1641  CurrPos = MAXALIGN64(CurrPos);
1642  }
1643 
1644  if (CurrPos != EndPos)
1645  elog(PANIC, "space reserved for WAL record does not match what was written");
1646 }
int wal_segment_size
Definition: xlog.c:116
#define MemSet(start, val, len)
Definition: c.h:949
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
#define PANIC
Definition: elog.h:53
static char * GetXLogBuffer(XLogRecPtr ptr)
Definition: xlog.c:1884
#define MAXALIGN64(LEN)
Definition: c.h:723
unsigned int uint32
Definition: c.h:374
#define INSERT_FREESPACE(endptr)
Definition: xlog.c:742
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
#define XLP_FIRST_IS_CONTRECORD
Definition: xlog_internal.h:77
struct XLogRecData * next
#define elog(elevel,...)
Definition: elog.h:214
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72

◆ CreateCheckPoint()

void CreateCheckPoint ( int  flags)

Definition at line 8701 of file xlog.c.

References ControlFileData::checkPoint, CHECKPOINT_END_OF_RECOVERY, CHECKPOINT_FORCE, CHECKPOINT_IS_SHUTDOWN, ControlFileData::checkPointCopy, CheckPointGuts(), CheckpointStatsData::ckpt_bufs_written, CheckpointStatsData::ckpt_segs_added, CheckpointStatsData::ckpt_segs_recycled, CheckpointStatsData::ckpt_segs_removed, CheckpointStatsData::ckpt_start_t, XLogCtlData::ckptFullXid, XLogCtlInsert::CurrBytePos, DB_SHUTDOWNED, DB_SHUTDOWNING, DEBUG1, elog, END_CRIT_SECTION, ereport, errmsg(), ERROR, CheckPoint::fullPageWrites, XLogCtlInsert::fullPageWrites, GetCurrentTimestamp(), GetLastImportantRecPtr(), GetOldestActiveTransactionId(), GetOldestXmin(), GetVirtualXIDsDelayingChkpt(), HaveVirtualXIDsDelayingChkpt(), XLogCtlData::info_lck, InitXLogInsert(), Insert(), XLogCtlData::Insert, INSERT_FREESPACE, InvalidateObsoleteReplicationSlots(), InvalidTransactionId, InvalidXLogRecPtr, KeepLogSeg(), LocalSetXLogInsertAllowed(), LocalXLogInsertAllowed, log_checkpoints, LogCheckpointEnd(), LogCheckpointStart(), LogStandbySnapshot(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemSet, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, MultiXactGetCheckptMulti(), NBuffers, CheckPoint::newestCommitTsXid, VariableCacheData::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, VariableCacheData::nextOid, CheckPoint::nextXid, VariableCacheData::nextXid, VariableCacheData::oidCount, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, VariableCacheData::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, CheckPoint::oldestXid, VariableCacheData::oldestXid, CheckPoint::oldestXidDB, VariableCacheData::oldestXidDB, PANIC, pfree(), pg_usleep(), PreallocXlogFiles(), CheckPoint::PrevTimeLineID, XLogCtlData::PrevTimeLineID, PROCARRAY_FLAGS_DEFAULT, ProcLastRecPtr, RecoveryInProgress(), CheckPoint::redo, RedoRecPtr, XLogCtlInsert::RedoRecPtr, XLogCtlData::RedoRecPtr, RemoveOldXlogFiles(), ShmemVariableCache, SizeOfXLogLongPHD, SizeOfXLogShortPHD, SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, ControlFileData::state, SyncPostCheckpoint(), SyncPreCheckpoint(), CheckPoint::ThisTimeLineID, ThisTimeLineID, CheckPoint::time, ControlFileData::time, TruncateSUBTRANS(), XLogCtlData::ulsn_lck, ControlFileData::unloggedLSN, XLogCtlData::unloggedLSN, UpdateCheckPointDistanceEstimate(), UpdateControlFile(), wal_segment_size, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLByteToSeg, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_SHUTDOWN, XLogBeginInsert(), XLogBytePosToRecPtr(), XLogFlush(), XLogInsert(), XLogRegisterData(), XLogSegmentOffset, and XLogStandbyInfoActive.

Referenced by CheckpointerMain(), RequestCheckpoint(), ShutdownXLOG(), and StartupXLOG().

8702 {
8703  bool shutdown;
8704  CheckPoint checkPoint;
8705  XLogRecPtr recptr;
8706  XLogSegNo _logSegNo;
8708  uint32 freespace;
8709  XLogRecPtr PriorRedoPtr;
8710  XLogRecPtr curInsert;
8711  XLogRecPtr last_important_lsn;
8712  VirtualTransactionId *vxids;
8713  int nvxids;
8714 
8715  /*
8716  * An end-of-recovery checkpoint is really a shutdown checkpoint, just
8717  * issued at a different time.
8718  */
8720  shutdown = true;
8721  else
8722  shutdown = false;
8723 
8724  /* sanity check */
8725  if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
8726  elog(ERROR, "can't create a checkpoint during recovery");
8727 
8728  /*
8729  * Initialize InitXLogInsert working areas before entering the critical
8730  * section. Normally, this is done by the first call to
8731  * RecoveryInProgress() or LocalSetXLogInsertAllowed(), but when creating
8732  * an end-of-recovery checkpoint, the LocalSetXLogInsertAllowed call is
8733  * done below in a critical section, and InitXLogInsert cannot be called
8734  * in a critical section.
8735  */
8736  InitXLogInsert();
8737 
8738  /*
8739  * Acquire CheckpointLock to ensure only one checkpoint happens at a time.
8740  * (This is just pro forma, since in the present system structure there is
8741  * only one process that is allowed to issue checkpoints at any given
8742  * time.)
8743  */
8744  LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
8745 
8746  /*
8747  * Prepare to accumulate statistics.
8748  *
8749  * Note: because it is possible for log_checkpoints to change while a
8750  * checkpoint proceeds, we always accumulate stats, even if
8751  * log_checkpoints is currently off.
8752  */
8753  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
8755 
8756  /*
8757  * Use a critical section to force system panic if we have trouble.
8758  */
8760 
8761  if (shutdown)
8762  {
8763  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8765  ControlFile->time = (pg_time_t) time(NULL);
8767  LWLockRelease(ControlFileLock);
8768  }
8769 
8770  /*
8771  * Let smgr prepare for checkpoint; this has to happen before we determine
8772  * the REDO pointer. Note that smgr must not do anything that'd have to
8773  * be undone if we decide no checkpoint is needed.
8774  */
8776 
8777  /* Begin filling in the checkpoint WAL record */
8778  MemSet(&checkPoint, 0, sizeof(checkPoint));
8779  checkPoint.time = (pg_time_t) time(NULL);
8780 
8781  /*
8782  * For Hot Standby, derive the oldestActiveXid before we fix the redo
8783  * pointer. This allows us to begin accumulating changes to assemble our
8784  * starting snapshot of locks and transactions.
8785  */
8786  if (!shutdown && XLogStandbyInfoActive())
8788  else
8790 
8791  /*
8792  * Get location of last important record before acquiring insert locks (as
8793  * GetLastImportantRecPtr() also locks WAL locks).
8794  */
8795  last_important_lsn = GetLastImportantRecPtr();
8796 
8797  /*
8798  * We must block concurrent insertions while examining insert state to
8799  * determine the checkpoint REDO pointer.
8800  */
8802  curInsert = XLogBytePosToRecPtr(Insert->CurrBytePos);
8803 
8804  /*
8805  * If this isn't a shutdown or forced checkpoint, and if there has been no
8806  * WAL activity requiring a checkpoint, skip it. The idea here is to
8807  * avoid inserting duplicate checkpoints when the system is idle.
8808  */
8809  if ((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
8810  CHECKPOINT_FORCE)) == 0)
8811  {
8812  if (last_important_lsn == ControlFile->checkPoint)
8813  {
8815  LWLockRelease(CheckpointLock);
8816  END_CRIT_SECTION();
8817  ereport(DEBUG1,
8818  (errmsg("checkpoint skipped because system is idle")));
8819  return;
8820  }
8821  }
8822 
8823  /*
8824  * An end-of-recovery checkpoint is created before anyone is allowed to
8825  * write WAL. To allow us to write the checkpoint record, temporarily
8826  * enable XLogInsertAllowed. (This also ensures ThisTimeLineID is
8827  * initialized, which we need here and in AdvanceXLInsertBuffer.)
8828  */
8829  if (flags & CHECKPOINT_END_OF_RECOVERY)
8831 
8832  checkPoint.ThisTimeLineID = ThisTimeLineID;
8833  if (flags & CHECKPOINT_END_OF_RECOVERY)
8834  checkPoint.PrevTimeLineID = XLogCtl->PrevTimeLineID;
8835  else
8836  checkPoint.PrevTimeLineID = ThisTimeLineID;
8837 
8838  checkPoint.fullPageWrites = Insert->fullPageWrites;
8839 
8840  /*
8841  * Compute new REDO record ptr = location of next XLOG record.
8842  *
8843  * NB: this is NOT necessarily where the checkpoint record itself will be,
8844  * since other backends may insert more XLOG records while we're off doing
8845  * the buffer flush work. Those XLOG records are logically after the
8846  * checkpoint, even though physically before it. Got that?
8847  */
8848  freespace = INSERT_FREESPACE(curInsert);
8849  if (freespace == 0)
8850  {
8851  if (XLogSegmentOffset(curInsert, wal_segment_size) == 0)
8852  curInsert += SizeOfXLogLongPHD;
8853  else
8854  curInsert += SizeOfXLogShortPHD;
8855  }
8856  checkPoint.redo = curInsert;
8857 
8858  /*
8859  * Here we update the shared RedoRecPtr for future XLogInsert calls; this
8860  * must be done while holding all the insertion locks.
8861  *
8862  * Note: if we fail to complete the checkpoint, RedoRecPtr will be left
8863  * pointing past where it really needs to point. This is okay; the only
8864  * consequence is that XLogInsert might back up whole buffers that it
8865  * didn't really need to. We can't postpone advancing RedoRecPtr because
8866  * XLogInserts that happen while we are dumping buffers must assume that
8867  * their buffer changes are not included in the checkpoint.
8868  */
8869  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
8870 
8871  /*
8872  * Now we can release the WAL insertion locks, allowing other xacts to
8873  * proceed while we are flushing disk buffers.
8874  */
8876 
8877  /* Update the info_lck-protected copy of RedoRecPtr as well */
8879  XLogCtl->RedoRecPtr = checkPoint.redo;
8881 
8882  /*
8883  * If enabled, log checkpoint start. We postpone this until now so as not
8884  * to log anything if we decided to skip the checkpoint.
8885  */
8886  if (log_checkpoints)
8887  LogCheckpointStart(flags, false);
8888 
8889  TRACE_POSTGRESQL_CHECKPOINT_START(flags);
8890 
8891  /*
8892  * Get the other info we need for the checkpoint record.
8893  *
8894  * We don't need to save oldestClogXid in the checkpoint, it only matters
8895  * for the short period in which clog is being truncated, and if we crash
8896  * during that we'll redo the clog truncation and fix up oldestClogXid
8897  * there.
8898  */
8899  LWLockAcquire(XidGenLock, LW_SHARED);
8900  checkPoint.nextXid = ShmemVariableCache->nextXid;
8901  checkPoint.oldestXid = ShmemVariableCache->oldestXid;
8903  LWLockRelease(XidGenLock);
8904 
8905  LWLockAcquire(CommitTsLock, LW_SHARED);
8908  LWLockRelease(CommitTsLock);
8909 
8910  LWLockAcquire(OidGenLock, LW_SHARED);
8911  checkPoint.nextOid = ShmemVariableCache->nextOid;
8912  if (!shutdown)
8913  checkPoint.nextOid += ShmemVariableCache->oidCount;
8914  LWLockRelease(OidGenLock);
8915 
8916  MultiXactGetCheckptMulti(shutdown,
8917  &checkPoint.nextMulti,
8918  &checkPoint.nextMultiOffset,
8919  &checkPoint.oldestMulti,
8920  &checkPoint.oldestMultiDB);
8921 
8922  /*
8923  * Having constructed the checkpoint record, ensure all shmem disk buffers
8924  * and commit-log buffers are flushed to disk.
8925  *
8926  * This I/O could fail for various reasons. If so, we will fail to
8927  * complete the checkpoint, but there is no reason to force a system
8928  * panic. Accordingly, exit critical section while doing it.
8929  */
8930  END_CRIT_SECTION();
8931 
8932  /*
8933  * In some cases there are groups of actions that must all occur on one
8934  * side or the other of a checkpoint record. Before flushing the
8935  * checkpoint record we must explicitly wait for any backend currently
8936  * performing those groups of actions.
8937  *
8938  * One example is end of transaction, so we must wait for any transactions
8939  * that are currently in commit critical sections. If an xact inserted
8940  * its commit record into XLOG just before the REDO point, then a crash
8941  * restart from the REDO point would not replay that record, which means
8942  * that our flushing had better include the xact's update of pg_xact. So
8943  * we wait till he's out of his commit critical section before proceeding.
8944  * See notes in RecordTransactionCommit().
8945  *
8946  * Because we've already released the insertion locks, this test is a bit
8947  * fuzzy: it is possible that we will wait for xacts we didn't really need
8948  * to wait for. But the delay should be short and it seems better to make
8949  * checkpoint take a bit longer than to hold off insertions longer than
8950  * necessary. (In fact, the whole reason we have this issue is that xact.c
8951  * does commit record XLOG insertion and clog update as two separate steps
8952  * protected by different locks, but again that seems best on grounds of
8953  * minimizing lock contention.)
8954  *
8955  * A transaction that has not yet set delayChkpt when we look cannot be at
8956  * risk, since he's not inserted his commit record yet; and one that's
8957  * already cleared it is not at risk either, since he's done fixing clog
8958  * and we will correctly flush the update below. So we cannot miss any
8959  * xacts we need to wait for.
8960  */
8961  vxids = GetVirtualXIDsDelayingChkpt(&nvxids);
8962  if (nvxids > 0)
8963  {
8964  do
8965  {
8966  pg_usleep(10000L); /* wait for 10 msec */
8967  } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids));
8968  }
8969  pfree(vxids);
8970 
8971  CheckPointGuts(checkPoint.redo, flags);
8972 
8973  /*
8974  * Take a snapshot of running transactions and write this to WAL. This
8975  * allows us to reconstruct the state of running transactions during
8976  * archive recovery, if required. Skip, if this info disabled.
8977  *
8978  * If we are shutting down, or Startup process is completing crash
8979  * recovery we don't need to write running xact data.
8980  */
8981  if (!shutdown && XLogStandbyInfoActive())
8983 
8985 
8986  /*
8987  * Now insert the checkpoint record into XLOG.
8988  */
8989  XLogBeginInsert();
8990  XLogRegisterData((char *) (&checkPoint), sizeof(checkPoint));
8991  recptr = XLogInsert(RM_XLOG_ID,
8992  shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
8994 
8995  XLogFlush(recptr);
8996 
8997  /*
8998  * We mustn't write any new WAL after a shutdown checkpoint, or it will be
8999  * overwritten at next startup. No-one should even try, this just allows
9000  * sanity-checking. In the case of an end-of-recovery checkpoint, we want
9001  * to just temporarily disable writing until the system has exited
9002  * recovery.
9003  */
9004  if (shutdown)
9005  {
9006  if (flags & CHECKPOINT_END_OF_RECOVERY)
9007  LocalXLogInsertAllowed = -1; /* return to "check" state */
9008  else
9009  LocalXLogInsertAllowed = 0; /* never again write WAL */
9010  }
9011 
9012  /*
9013  * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
9014  * = end of actual checkpoint record.
9015  */
9016  if (shutdown && checkPoint.redo != ProcLastRecPtr)
9017  ereport(PANIC,
9018  (errmsg("concurrent write-ahead log activity while database system is shutting down")));
9019 
9020  /*
9021  * Remember the prior checkpoint's redo ptr for
9022  * UpdateCheckPointDistanceEstimate()
9023  */
9024  PriorRedoPtr = ControlFile->checkPointCopy.redo;
9025 
9026  /*
9027  * Update the control file.
9028  */
9029  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9030  if (shutdown)
9033  ControlFile->checkPointCopy = checkPoint;
9034  ControlFile->time = (pg_time_t) time(NULL);
9035  /* crash recovery should always recover to the end of WAL */
9038 
9039  /*
9040  * Persist unloggedLSN value. It's reset on crash recovery, so this goes
9041  * unused on non-shutdown checkpoints, but seems useful to store it always
9042  * for debugging purposes.
9043  */
9047 
9049  LWLockRelease(ControlFileLock);
9050 
9051  /* Update shared-memory copy of checkpoint XID/epoch */
9053  XLogCtl->ckptFullXid = checkPoint.nextXid;
9055 
9056  /*
9057  * We are now done with critical updates; no need for system panic if we
9058  * have trouble while fooling with old log segments.
9059  */
9060  END_CRIT_SECTION();
9061 
9062  /*
9063  * Let smgr do post-checkpoint cleanup (eg, deleting old files).
9064  */
9066 
9067  /*
9068  * Update the average distance between checkpoints if the prior checkpoint
9069  * exists.
9070  */
9071  if (PriorRedoPtr != InvalidXLogRecPtr)
9073 
9074  /*
9075  * Delete old log files, those no longer needed for last checkpoint to
9076  * prevent the disk holding the xlog from growing full.
9077  */
9079  KeepLogSeg(recptr, &_logSegNo);
9081  _logSegNo--;
9082  RemoveOldXlogFiles(_logSegNo, RedoRecPtr, recptr);
9083 
9084  /*
9085  * Make more log segments if needed. (Do this after recycling old log
9086  * segments, since that may supply some of the needed files.)
9087  */
9088  if (!shutdown)
9089  PreallocXlogFiles(recptr);
9090 
9091  /*
9092  * Truncate pg_subtrans if possible. We can throw away all data before
9093  * the oldest XMIN of any running transaction. No future transaction will
9094  * attempt to reference any pg_subtrans entry older than that (see Asserts
9095  * in subtrans.c). During recovery, though, we mustn't do this because
9096  * StartupSUBTRANS hasn't been called yet.
9097  */
9098  if (!RecoveryInProgress())
9100 
9101  /* Real work is done, but log and update stats before releasing lock. */
9102  LogCheckpointEnd(false);
9103 
9104  TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats.ckpt_bufs_written,
9105  NBuffers,
9109 
9110  LWLockRelease(CheckpointLock);
9111 }
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:8439
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:8639
static int LocalXLogInsertAllowed
Definition: xlog.c:252
bool log_checkpoints
Definition: xlog.c:104
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
int64 pg_time_t
Definition: pgtime.h:23
static void WALInsertLockRelease(void)
Definition: xlog.c:1726
int wal_segment_size
Definition: xlog.c:116
pg_time_t time
Definition: pg_control.h:128
#define XLOG_CHECKPOINT_ONLINE
Definition: pg_control.h:68
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:167
uint32 oidCount
Definition: transam.h:200
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1995
XLogRecPtr unloggedLSN
Definition: xlog.c:611
XLogRecPtr ProcLastRecPtr
Definition: xlog.c:359
TransactionId oldestActiveXid
Definition: pg_control.h:63
void InitXLogInsert(void)
Definition: xloginsert.c:1140
TimestampTz ckpt_start_t
Definition: xlog.h:246
slock_t info_lck
Definition: xlog.c:725
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
VirtualTransactionId * GetVirtualXIDsDelayingChkpt(int *nvxids)
Definition: procarray.c:2329
MultiXactId oldestMulti
Definition: pg_control.h:49
TimeLineID PrevTimeLineID
Definition: xlog.c:652
TimeLineID PrevTimeLineID
Definition: pg_control.h:40
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
int ckpt_segs_recycled
Definition: xlog.h:256
TransactionId oldestXid
Definition: transam.h:207
#define MemSet(start, val, len)
Definition: c.h:949
void MultiXactGetCheckptMulti(bool is_shutdown, MultiXactId *nextMulti, MultiXactOffset *nextMultiOffset, MultiXactId *oldestMulti, Oid *oldestMultiDB)
Definition: multixact.c:2120
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
Definition: xlog.c:9172
CheckPoint checkPointCopy
Definition: pg_control.h:131
XLogCtlInsert Insert
Definition: xlog.c:599
TransactionId oldestXid
Definition: pg_control.h:47
bool RecoveryInProgress(void)
Definition: xlog.c:8069
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:356
FullTransactionId nextXid
Definition: transam.h:205
pg_time_t time
Definition: pg_control.h:51
#define PANIC
Definition: elog.h:53
bool fullPageWrites
Definition: xlog.c:574
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2844
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define SpinLockAcquire(lock)
Definition: spin.h:62
void pg_usleep(long microsec)
Definition: signal.c:53
MultiXactOffset nextMultiOffset
Definition: pg_control.h:46
void UpdateControlFile(void)
Definition: xlog.c:4901
TransactionId oldestCommitTsXid
Definition: pg_control.h:52
void pfree(void *pointer)
Definition: mcxt.c:1056
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:923
#define ERROR
Definition: elog.h:43
static void LogCheckpointEnd(bool restartpoint)
Definition: xlog.c:8554
static XLogRecPtr RedoRecPtr
Definition: xlog.c:373
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:67
XLogRecPtr unloggedLSN
Definition: pg_control.h:133
static void PreallocXlogFiles(XLogRecPtr endptr)
Definition: xlog.c:3892
uint64 XLogSegNo
Definition: xlogdefs.h:41
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:222
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
uint64 CurrBytePos
Definition: xlog.c:549
unsigned int uint32
Definition: c.h:374
XLogRecPtr RedoRecPtr
Definition: xlog.c:603
int ckpt_segs_removed
Definition: xlog.h:255
#define CHECKPOINT_FORCE
Definition: xlog.h:225
#define INSERT_FREESPACE(endptr)
Definition: xlog.c:742
TransactionId oldestCommitTsXid
Definition: transam.h:217
static void Insert(File file)
Definition: fd.c:1174
int ckpt_bufs_written
Definition: xlog.h:252
static void LocalSetXLogInsertAllowed(void)
Definition: xlog.c:8213
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:330
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
TransactionId newestCommitTsXid
Definition: pg_control.h:54
#define PROCARRAY_FLAGS_DEFAULT
Definition: procarray.h:47
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:9585
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
Oid oldestMultiDB
Definition: pg_control.h:50
FullTransactionId ckptFullXid
Definition: xlog.c:604
#define XLogStandbyInfoActive()
Definition: xlog.h:205
static ControlFileData * ControlFile
Definition: xlog.c:736
TimeLineID ThisTimeLineID
Definition: xlog.c:191
Oid nextOid
Definition: pg_control.h:44
#define ereport(elevel,...)
Definition: elog.h:144
bool fullPageWrites
Definition: pg_control.h:42
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1359
uint64 XLogRecPtr
Definition: xlogdefs.h:21
Oid oldestXidDB
Definition: pg_control.h:48
TransactionId newestCommitTsXid
Definition: transam.h:218
CheckpointStatsData CheckpointStats
Definition: xlog.c:185
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
MultiXactId nextMulti
Definition: pg_control.h:45
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1697
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
int ckpt_segs_added
Definition: xlog.h:254
slock_t ulsn_lck
Definition: xlog.c:612
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:824
void InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno)
Definition: slot.c:1133
#define elog(elevel,...)
Definition: elog.h:214
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr)
Definition: xlog.c:4023
void SyncPostCheckpoint(void)
Definition: sync.c:174
TransactionId GetOldestActiveTransactionId(void)
Definition: procarray.c:2167
int NBuffers
Definition: globals.c:132
bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids)
Definition: procarray.c:2373
void XLogBeginInsert(void)
Definition: xloginsert.c:123
XLogRecPtr RedoRecPtr
Definition: xlog.c:572
XLogRecPtr checkPoint
Definition: pg_control.h:129
XLogRecPtr redo
Definition: pg_control.h:37
static void LogCheckpointStart(int flags, bool restartpoint)
Definition: xlog.c:8536
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:221
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:166
void SyncPreCheckpoint(void)
Definition: sync.c:159
FullTransactionId nextXid
Definition: pg_control.h:43
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ CreateEndOfRecoveryRecord()

static void CreateEndOfRecoveryRecord ( void  )
static

Definition at line 9123 of file xlog.c.

References elog, END_CRIT_SECTION, xl_end_of_recovery::end_time, ERROR, GetCurrentTimestamp(), LocalSetXLogInsertAllowed(), LocalXLogInsertAllowed, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, xl_end_of_recovery::PrevTimeLineID, XLogCtlData::PrevTimeLineID, RecoveryInProgress(), START_CRIT_SECTION, ThisTimeLineID, xl_end_of_recovery::ThisTimeLineID, ControlFileData::time, UpdateControlFile(), WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLOG_END_OF_RECOVERY, XLogBeginInsert(), XLogFlush(), XLogInsert(), and XLogRegisterData().

Referenced by StartupXLOG().

9124 {
9125  xl_end_of_recovery xlrec;
9126  XLogRecPtr recptr;
9127 
9128  /* sanity check */
9129  if (!RecoveryInProgress())
9130  elog(ERROR, "can only be used to end recovery");
9131 
9132  xlrec.end_time = GetCurrentTimestamp();
9133 
9138 
9140 
9142 
9143  XLogBeginInsert();
9144  XLogRegisterData((char *) &xlrec, sizeof(xl_end_of_recovery));
9145  recptr = XLogInsert(RM_XLOG_ID, XLOG_END_OF_RECOVERY);
9146 
9147  XLogFlush(recptr);
9148 
9149  /*
9150  * Update the control file so that crash recovery can follow the timeline
9151  * changes to this point.
9152  */
9153  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9154  ControlFile->time = (pg_time_t) time(NULL);
9155  ControlFile->minRecoveryPoint = recptr;
9158  LWLockRelease(ControlFileLock);
9159 
9160  END_CRIT_SECTION();
9161 
9162  LocalXLogInsertAllowed = -1; /* return to "check" state */
9163 }
static int LocalXLogInsertAllowed
Definition: xlog.c:252
int64 pg_time_t
Definition: pgtime.h:23
static void WALInsertLockRelease(void)
Definition: xlog.c:1726
pg_time_t time
Definition: pg_control.h:128
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:167
TimeLineID PrevTimeLineID
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
TimeLineID PrevTimeLineID
Definition: xlog.c:652
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
bool RecoveryInProgress(void)
Definition: xlog.c:8069
#define XLOG_END_OF_RECOVERY
Definition: pg_control.h:76
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2844
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
void UpdateControlFile(void)
Definition: xlog.c:4901
#define ERROR
Definition: elog.h:43
static void LocalSetXLogInsertAllowed(void)
Definition: xlog.c:8213
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:330
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
static ControlFileData * ControlFile
Definition: xlog.c:736
TimeLineID ThisTimeLineID
Definition: xlog.c:191
TimestampTz end_time
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1697
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
TimeLineID ThisTimeLineID
#define elog(elevel,...)
Definition: elog.h:214
void XLogBeginInsert(void)
Definition: xloginsert.c:123
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:166

◆ CreateRestartPoint()

bool CreateRestartPoint ( int  flags)

Definition at line 9242 of file xlog.c.

References archiveCleanupCommand, ControlFileData::checkPoint, CHECKPOINT_IS_SHUTDOWN, ControlFileData::checkPointCopy, CheckPointGuts(), CheckpointStatsData::ckpt_start_t, DB_IN_ARCHIVE_RECOVERY, DB_SHUTDOWNED_IN_RECOVERY, DEBUG2, EnableHotStandby, ereport, errdetail(), errmsg(), ExecuteRecoveryCommand(), GetCurrentTimestamp(), GetLatestXTime(), GetOldestXmin(), GetWalRcvFlushRecPtr(), GetXLogReplayRecPtr(), XLogCtlData::info_lck, XLogCtlData::Insert, InvalidateObsoleteReplicationSlots(), InvalidXLogRecPtr, KeepLogSeg(), XLogCtlData::lastCheckPoint, XLogCtlData::lastCheckPointEndPtr, XLogCtlData::lastCheckPointRecPtr, LOG, log_checkpoints, LogCheckpointEnd(), LogCheckpointStart(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemSet, ControlFileData::minRecoveryPoint, minRecoveryPoint, ControlFileData::minRecoveryPointTLI, minRecoveryPointTLI, PreallocXlogFiles(), PROCARRAY_FLAGS_DEFAULT, RecoveryInProgress(), CheckPoint::redo, RedoRecPtr, XLogCtlInsert::RedoRecPtr, XLogCtlData::RedoRecPtr, RemoveOldXlogFiles(), SpinLockAcquire, SpinLockRelease, ControlFileData::state, CheckPoint::ThisTimeLineID, ThisTimeLineID, ControlFileData::time, timestamptz_to_str(), TruncateSUBTRANS(), UpdateCheckPointDistanceEstimate(), UpdateControlFile(), UpdateMinRecoveryPoint(), wal_segment_size, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLByteToSeg, and XLogRecPtrIsInvalid.

Referenced by CheckpointerMain(), and ShutdownXLOG().

9243 {
9244  XLogRecPtr lastCheckPointRecPtr;
9245  XLogRecPtr lastCheckPointEndPtr;
9246  CheckPoint lastCheckPoint;
9247  XLogRecPtr PriorRedoPtr;
9248  XLogRecPtr receivePtr;
9249  XLogRecPtr replayPtr;
9250  TimeLineID replayTLI;
9251  XLogRecPtr endptr;
9252  XLogSegNo _logSegNo;
9253  TimestampTz xtime;
9254 
9255  /*
9256  * Acquire CheckpointLock to ensure only one restartpoint or checkpoint
9257  * happens at a time.
9258  */
9259  LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
9260 
9261  /* Get a local copy of the last safe checkpoint record. */
9263  lastCheckPointRecPtr = XLogCtl->lastCheckPointRecPtr;
9264  lastCheckPointEndPtr = XLogCtl->lastCheckPointEndPtr;
9265  lastCheckPoint = XLogCtl->lastCheckPoint;
9267 
9268  /*
9269  * Check that we're still in recovery mode. It's ok if we exit recovery
9270  * mode after this check, the restart point is valid anyway.
9271  */
9272  if (!RecoveryInProgress())
9273  {
9274  ereport(DEBUG2,
9275  (errmsg("skipping restartpoint, recovery has already ended")));
9276  LWLockRelease(CheckpointLock);
9277  return false;
9278  }
9279 
9280  /*
9281  * If the last checkpoint record we've replayed is already our last
9282  * restartpoint, we can't perform a new restart point. We still update
9283  * minRecoveryPoint in that case, so that if this is a shutdown restart
9284  * point, we won't start up earlier than before. That's not strictly
9285  * necessary, but when hot standby is enabled, it would be rather weird if
9286  * the database opened up for read-only connections at a point-in-time
9287  * before the last shutdown. Such time travel is still possible in case of
9288  * immediate shutdown, though.
9289  *
9290  * We don't explicitly advance minRecoveryPoint when we do create a
9291  * restartpoint. It's assumed that flushing the buffers will do that as a
9292  * side-effect.
9293  */
9294  if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||
9295  lastCheckPoint.redo <= ControlFile->checkPointCopy.redo)
9296  {
9297  ereport(DEBUG2,
9298  (errmsg("skipping restartpoint, already performed at %X/%X",
9299  (uint32) (lastCheckPoint.redo >> 32),
9300  (uint32) lastCheckPoint.redo)));
9301 
9303  if (flags & CHECKPOINT_IS_SHUTDOWN)
9304  {
9305  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9307  ControlFile->time = (pg_time_t) time(NULL);
9309  LWLockRelease(ControlFileLock);
9310  }
9311  LWLockRelease(CheckpointLock);
9312  return false;
9313  }
9314 
9315  /*
9316  * Update the shared RedoRecPtr so that the startup process can calculate
9317  * the number of segments replayed since last restartpoint, and request a
9318  * restartpoint if it exceeds CheckPointSegments.
9319  *
9320  * Like in CreateCheckPoint(), hold off insertions to update it, although
9321  * during recovery this is just pro forma, because no WAL insertions are
9322  * happening.
9323  */
9325  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = lastCheckPoint.redo;
9327 
9328  /* Also update the info_lck-protected copy */
9330  XLogCtl->RedoRecPtr = lastCheckPoint.redo;
9332 
9333  /*
9334  * Prepare to accumulate statistics.
9335  *
9336  * Note: because it is possible for log_checkpoints to change while a
9337  * checkpoint proceeds, we always accumulate stats, even if
9338  * log_checkpoints is currently off.
9339  */
9340  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
9342 
9343  if (log_checkpoints)
9344  LogCheckpointStart(flags, true);
9345 
9346  CheckPointGuts(lastCheckPoint.redo, flags);
9347 
9348  /*
9349  * Remember the prior checkpoint's redo ptr for
9350  * UpdateCheckPointDistanceEstimate()
9351  */
9352  PriorRedoPtr = ControlFile->checkPointCopy.redo;
9353 
9354  /*
9355  * Update pg_control, using current time. Check that it still shows
9356  * DB_IN_ARCHIVE_RECOVERY state and an older checkpoint, else do nothing;
9357  * this is a quick hack to make sure nothing really bad happens if somehow
9358  * we get here after the end-of-recovery checkpoint.
9359  */
9360  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9362  ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
9363  {
9364  ControlFile->checkPoint = lastCheckPointRecPtr;
9365  ControlFile->checkPointCopy = lastCheckPoint;
9366  ControlFile->time = (pg_time_t) time(NULL);
9367 
9368  /*
9369  * Ensure minRecoveryPoint is past the checkpoint record. Normally,
9370  * this will have happened already while writing out dirty buffers,
9371  * but not necessarily - e.g. because no buffers were dirtied. We do
9372  * this because a non-exclusive base backup uses minRecoveryPoint to
9373  * determine which WAL files must be included in the backup, and the
9374  * file (or files) containing the checkpoint record must be included,
9375  * at a minimum. Note that for an ordinary restart of recovery there's
9376  * no value in having the minimum recovery point any earlier than this
9377  * anyway, because redo will begin just after the checkpoint record.
9378  */
9379  if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
9380  {
9381  ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
9383 
9384  /* update local copy */
9387  }
9388  if (flags & CHECKPOINT_IS_SHUTDOWN)
9391  }
9392  LWLockRelease(ControlFileLock);
9393 
9394  /*
9395  * Update the average distance between checkpoints/restartpoints if the
9396  * prior checkpoint exists.
9397  */
9398  if (PriorRedoPtr != InvalidXLogRecPtr)
9400 
9401  /*
9402  * Delete old log files, those no longer needed for last restartpoint to
9403  * prevent the disk holding the xlog from growing full.
9404  */
9406 
9407  /*
9408  * Retreat _logSegNo using the current end of xlog replayed or received,
9409  * whichever is later.
9410  */
9411  receivePtr = GetWalRcvFlushRecPtr(NULL, NULL);
9412  replayPtr = GetXLogReplayRecPtr(&replayTLI);
9413  endptr = (receivePtr < replayPtr) ? replayPtr : receivePtr;
9414  KeepLogSeg(endptr, &_logSegNo);
9416  _logSegNo--;
9417 
9418  /*
9419  * Try to recycle segments on a useful timeline. If we've been promoted
9420  * since the beginning of this restartpoint, use the new timeline chosen
9421  * at end of recovery (RecoveryInProgress() sets ThisTimeLineID in that
9422  * case). If we're still in recovery, use the timeline we're currently
9423  * replaying.
9424  *
9425  * There is no guarantee that the WAL segments will be useful on the
9426  * current timeline; if recovery proceeds to a new timeline right after
9427  * this, the pre-allocated WAL segments on this timeline will not be used,
9428  * and will go wasted until recycled on the next restartpoint. We'll live
9429  * with that.
9430  */
9431  if (RecoveryInProgress())
9432  ThisTimeLineID = replayTLI;
9433 
9434  RemoveOldXlogFiles(_logSegNo, RedoRecPtr, endptr);
9435 
9436  /*
9437  * Make more log segments if needed. (Do this after recycling old log
9438  * segments, since that may supply some of the needed files.)
9439  */
9440  PreallocXlogFiles(endptr);
9441 
9442  /*
9443  * ThisTimeLineID is normally not set when we're still in recovery.
9444  * However, recycling/preallocating segments above needed ThisTimeLineID
9445  * to determine which timeline to install the segments on. Reset it now,
9446  * to restore the normal state of affairs for debugging purposes.
9447  */
9448  if (RecoveryInProgress())
9449  ThisTimeLineID = 0;
9450 
9451  /*
9452  * Truncate pg_subtrans if possible. We can throw away all data before
9453  * the oldest XMIN of any running transaction. No future transaction will
9454  * attempt to reference any pg_subtrans entry older than that (see Asserts
9455  * in subtrans.c). When hot standby is disabled, though, we mustn't do
9456  * this because StartupSUBTRANS hasn't been called yet.
9457  */
9458  if (EnableHotStandby)
9460 
9461  /* Real work is done, but log and update before releasing lock. */
9462  LogCheckpointEnd(true);
9463 
9464  xtime = GetLatestXTime();
9466  (errmsg("recovery restart point at %X/%X",
9467  (uint32) (lastCheckPoint.redo >> 32), (uint32) lastCheckPoint.redo),
9468  xtime ? errdetail("Last completed transaction was at log time %s.",
9469  timestamptz_to_str(xtime)) : 0));
9470 
9471  LWLockRelease(CheckpointLock);
9472 
9473  /*
9474  * Finally, execute archive_cleanup_command, if any.
9475  */
9476  if (archiveCleanupCommand && strcmp(archiveCleanupCommand, "") != 0)
9478  "archive_cleanup_command",
9479  false);
9480 
9481  return true;
9482 }
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:8639
bool log_checkpoints
Definition: xlog.c:104
void ExecuteRecoveryCommand(const char *command, const char *commandName, bool failOnSignal)
Definition: xlogarchive.c:286
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TimeLineID
Definition: xlogdefs.h:52
int64 pg_time_t
Definition: pgtime.h:23
static void WALInsertLockRelease(void)
Definition: xlog.c:1726
int wal_segment_size
Definition: xlog.c:116
pg_time_t time
Definition: pg_control.h:128
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:167
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
int64 TimestampTz
Definition: timestamp.h:39
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
Definition: xlog.c:2757
TimestampTz ckpt_start_t
Definition: xlog.h:246
slock_t info_lck
Definition: xlog.c:725
#define MemSet(start, val, len)
Definition: c.h:949
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
Definition: xlog.c:9172
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6173
CheckPoint checkPointCopy
Definition: pg_control.h:131
XLogCtlInsert Insert
Definition: xlog.c:599
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:8069
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:356
XLogRecPtr lastCheckPointRecPtr
Definition: xlog.c:694
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define SpinLockAcquire(lock)
Definition: spin.h:62
void UpdateControlFile(void)
Definition: xlog.c:4901
static void LogCheckpointEnd(bool restartpoint)
Definition: xlog.c:8554
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11475
#define DEBUG2
Definition: elog.h:24
static XLogRecPtr RedoRecPtr
Definition: xlog.c:373
static void PreallocXlogFiles(XLogRecPtr endptr)
Definition: xlog.c:3892
uint64 XLogSegNo
Definition: xlogdefs.h:41
int errdetail(const char *fmt,...)
Definition: elog.c:957
unsigned int uint32
Definition: c.h:374
XLogRecPtr RedoRecPtr
Definition: xlog.c:603
CheckPoint lastCheckPoint
Definition: xlog.c:696
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
static TimeLineID minRecoveryPointTLI
Definition: xlog.c:861
#define PROCARRAY_FLAGS_DEFAULT
Definition: procarray.h:47
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:9585
static ControlFileData * ControlFile
Definition: xlog.c:736
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define ereport(elevel,...)
Definition: elog.h:144
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1359
uint64 XLogRecPtr
Definition: xlogdefs.h:21
CheckpointStatsData CheckpointStats
Definition: xlog.c:185
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1697
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool EnableHotStandby
Definition: xlog.c:96
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:824
void InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno)
Definition: slot.c:1133
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr)
Definition: xlog.c:4023
XLogRecPtr RedoRecPtr
Definition: xlog.c:572
XLogRecPtr lastCheckPointEndPtr
Definition: xlog.c:695
XLogRecPtr checkPoint
Definition: pg_control.h:129
XLogRecPtr redo
Definition: pg_control.h:37
static void LogCheckpointStart(int flags, bool restartpoint)
Definition: xlog.c:8536
char * archiveCleanupCommand
Definition: xlog.c:281
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:221
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:166
static XLogRecPtr minRecoveryPoint
Definition: xlog.c:860
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1736
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ DataChecksumsEnabled()

bool DataChecksumsEnabled ( void  )

Definition at line 4930 of file xlog.c.

References Assert, and ControlFileData::data_checksum_version.

Referenced by PageIsVerified(), PageSetChecksumCopy(), PageSetChecksumInplace(), pg_stat_get_db_checksum_failures(), pg_stat_get_db_checksum_last_failure(), ReadControlFile(), and sendFile().

4931 {
4932  Assert(ControlFile != NULL);
4933  return (ControlFile->data_checksum_version > 0);
4934 }
uint32 data_checksum_version
Definition: pg_control.h:220
static ControlFileData * ControlFile
Definition: xlog.c:736
#define Assert(condition)
Definition: c.h:745

◆ do_pg_abort_backup()

void do_pg_abort_backup ( int  code,
Datum  arg 
)

Definition at line 11427 of file xlog.c.

References Assert, DatumGetBool, ereport, errmsg(), EXCLUSIVE_BACKUP_NONE, XLogCtlInsert::exclusiveBackupState, XLogCtlInsert::forcePageWrites, XLogCtlData::Insert, XLogCtlInsert::nonExclusiveBackups, SESSION_BACKUP_NON_EXCLUSIVE, sessionBackupState, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), and WARNING.

Referenced by perform_base_backup(), and register_persistent_abort_backup_handler().

11428 {
11429  bool emit_warning = DatumGetBool(arg);
11430 
11431  /*
11432  * Quick exit if session is not keeping around a non-exclusive backup
11433  * already started.
11434  */
11436  return;
11437 
11441 
11444  {
11445  XLogCtl->Insert.forcePageWrites = false;
11446  }
11448 
11449  if (emit_warning)
11450  ereport(WARNING,
11451  (errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
11452 }
static void WALInsertLockRelease(void)
Definition: xlog.c:1726
static SessionBackupState sessionBackupState
Definition: xlog.c:533
XLogCtlInsert Insert
Definition: xlog.c:599
bool forcePageWrites
Definition: xlog.c:573
#define DatumGetBool(X)
Definition: postgres.h:393
#define WARNING
Definition: elog.h:40
int nonExclusiveBackups
Definition: xlog.c:585
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:584
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1697
static XLogCtlData * XLogCtl
Definition: xlog.c:728
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * arg

◆ do_pg_start_backup()

XLogRecPtr do_pg_start_backup ( const char *  backupidstr,
bool  fast,
TimeLineID starttli_p,
StringInfo  labelfile,
List **  tablespaces,
StringInfo  tblspcmapfile,
bool  needtblspcmapfile 
)

Definition at line 10496 of file xlog.c.

References AllocateDir(), AllocateFile(), appendStringInfo(), appendStringInfoChar(), BACKUP_LABEL_FILE, backup_started_in_recovery, BoolGetDatum, ControlFileData::checkPoint, CHECKPOINT_FORCE, CHECKPOINT_IMMEDIATE, CHECKPOINT_WAIT, ControlFileData::checkPointCopy, dirent::d_name, StringInfoData::data, DataDir, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, EXCLUSIVE_BACKUP_IN_PROGRESS, EXCLUSIVE_BACKUP_NONE, EXCLUSIVE_BACKUP_STARTING, XLogCtlInsert::exclusiveBackupState, XLogCtlInsert::forcePageWrites, FreeDir(), FreeFile(), CheckPoint::fullPageWrites, XLogCtlData::info_lck, initStringInfo(), XLogCtlData::Insert, IS_DIR_SEP, lappend(), XLogCtlInsert::lastBackupStart, XLogCtlData::lastFpwDisableRecPtr, StringInfoData::len, log_timezone, LW_SHARED, LWLockAcquire(), LWLockRelease(), makeStringInfo(), MAXFNAMELEN, MAXPGPATH, XLogCtlInsert::nonExclusiveBackups, tablespaceinfo::oid, palloc(), tablespaceinfo::path, pfree(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, pg_fsync(), pg_localtime(), pg_start_backup_callback(), pg_strftime(), pstrdup(), ReadDir(), readlink, RecoveryInProgress(), CheckPoint::redo, relpath, RequestCheckpoint(), RequestXLogSwitch(), tablespaceinfo::rpath, SESSION_BACKUP_EXCLUSIVE, SESSION_BACKUP_NON_EXCLUSIVE, sessionBackupState, tablespaceinfo::size, snprintf, SpinLockAcquire, SpinLockRelease, stat, TABLESPACE_MAP, CheckPoint::ThisTimeLineID, wal_segment_size, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WARNING, XLByteToSeg, XLogFileName, and XLogIsNeeded.

Referenced by perform_base_backup(), and pg_start_backup().

10499 {
10500  bool exclusive = (labelfile == NULL);
10501  bool backup_started_in_recovery = false;
10502  XLogRecPtr checkpointloc;
10503  XLogRecPtr startpoint;
10504  TimeLineID starttli;
10505  pg_time_t stamp_time;
10506  char strfbuf[128];
10507  char xlogfilename[MAXFNAMELEN];
10508  XLogSegNo _logSegNo;
10509  struct stat stat_buf;
10510  FILE *fp;
10511 
10512  backup_started_in_recovery = RecoveryInProgress();
10513 
10514  /*
10515  * Currently only non-exclusive backup can be taken during recovery.
10516  */
10517  if (backup_started_in_recovery && exclusive)
10518  ereport(ERROR,
10519  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10520  errmsg("recovery is in progress"),
10521  errhint("WAL control functions cannot be executed during recovery.")));
10522 
10523  /*
10524  * During recovery, we don't need to check WAL level. Because, if WAL
10525  * level is not sufficient, it's impossible to get here during recovery.
10526  */
10527  if (!backup_started_in_recovery && !XLogIsNeeded())
10528  ereport(ERROR,
10529  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10530  errmsg("WAL level not sufficient for making an online backup"),
10531  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
10532 
10533  if (strlen(backupidstr) > MAXPGPATH)
10534  ereport(ERROR,
10535  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
10536  errmsg("backup label too long (max %d bytes)",
10537  MAXPGPATH)));
10538 
10539  /*
10540  * Mark backup active in shared memory. We must do full-page WAL writes
10541  * during an on-line backup even if not doing so at other times, because
10542  * it's quite possible for the backup dump to obtain a "torn" (partially
10543  * written) copy of a database page if it reads the page concurrently with
10544  * our write to the same page. This can be fixed as long as the first
10545  * write to the page in the WAL sequence is a full-page write. Hence, we
10546  * turn on forcePageWrites and then force a CHECKPOINT, to ensure there
10547  * are no dirty pages in shared memory that might get dumped while the
10548  * backup is in progress without having a corresponding WAL record. (Once
10549  * the backup is complete, we need not force full-page writes anymore,
10550  * since we expect that any pages not modified during the backup interval
10551  * must have been correctly captured by the backup.)
10552  *
10553  * Note that forcePageWrites has no effect during an online backup from
10554  * the standby.
10555  *
10556  * We must hold all the insertion locks to change the value of
10557  * forcePageWrites, to ensure adequate interlocking against
10558  * XLogInsertRecord().
10559  */
10561  if (exclusive)
10562  {
10563  /*
10564  * At first, mark that we're now starting an exclusive backup, to
10565  * ensure that there are no other sessions currently running
10566  * pg_start_backup() or pg_stop_backup().
10567  */
10569  {
10571  ereport(ERROR,
10572  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10573  errmsg("a backup is already in progress"),
10574  errhint("Run pg_stop_backup() and try again.")));
10575  }
10577  }
10578  else
10580  XLogCtl->Insert.forcePageWrites = true;
10582 
10583  /* Ensure we release forcePageWrites if fail below */
10585  {
10586  bool gotUniqueStartpoint = false;
10587  DIR *tblspcdir;
10588  struct dirent *de;
10589  tablespaceinfo *ti;
10590  int datadirpathlen;
10591 
10592  /*
10593  * Force an XLOG file switch before the checkpoint, to ensure that the
10594  * WAL segment the checkpoint is written to doesn't contain pages with
10595  * old timeline IDs. That would otherwise happen if you called
10596  * pg_start_backup() right after restoring from a PITR archive: the
10597  * first WAL segment containing the startup checkpoint has pages in
10598  * the beginning with the old timeline ID. That can cause trouble at
10599  * recovery: we won't have a history file covering the old timeline if
10600  * pg_wal directory was not included in the base backup and the WAL
10601  * archive was cleared too before starting the backup.
10602  *
10603  * This also ensures that we have emitted a WAL page header that has
10604  * XLP_BKP_REMOVABLE off before we emit the checkpoint record.
10605  * Therefore, if a WAL archiver (such as pglesslog) is trying to
10606  * compress out removable backup blocks, it won't remove any that
10607  * occur after this point.
10608  *
10609  * During recovery, we skip forcing XLOG file switch, which means that
10610  * the backup taken during recovery is not available for the special
10611  * recovery case described above.
10612  */
10613  if (!backup_started_in_recovery)
10614  RequestXLogSwitch(false);
10615 
10616  do
10617  {
10618  bool checkpointfpw;
10619 
10620  /*
10621  * Force a CHECKPOINT. Aside from being necessary to prevent torn
10622  * page problems, this guarantees that two successive backup runs
10623  * will have different checkpoint positions and hence different
10624  * history file names, even if nothing happened in between.
10625  *
10626  * During recovery, establish a restartpoint if possible. We use
10627  * the last restartpoint as the backup starting checkpoint. This
10628  * means that two successive backup runs can have same checkpoint
10629  * positions.
10630  *
10631  * Since the fact that we are executing do_pg_start_backup()
10632  * during recovery means that checkpointer is running, we can use
10633  * RequestCheckpoint() to establish a restartpoint.
10634  *
10635  * We use CHECKPOINT_IMMEDIATE only if requested by user (via
10636  * passing fast = true). Otherwise this can take awhile.
10637  */
10639  (fast ? CHECKPOINT_IMMEDIATE : 0));
10640 
10641  /*
10642  * Now we need to fetch the checkpoint record location, and also
10643  * its REDO pointer. The oldest point in WAL that would be needed
10644  * to restore starting from the checkpoint is precisely the REDO
10645  * pointer.
10646  */
10647  LWLockAcquire(ControlFileLock, LW_SHARED);
10648  checkpointloc = ControlFile->checkPoint;
10649  startpoint = ControlFile->checkPointCopy.redo;
10651  checkpointfpw = ControlFile->checkPointCopy.fullPageWrites;
10652  LWLockRelease(ControlFileLock);
10653 
10654  if (backup_started_in_recovery)
10655  {
10656  XLogRecPtr recptr;
10657 
10658  /*
10659  * Check to see if all WAL replayed during online backup
10660  * (i.e., since last restartpoint used as backup starting
10661  * checkpoint) contain full-page writes.
10662  */
10664  recptr = XLogCtl->lastFpwDisableRecPtr;
10666 
10667  if (!checkpointfpw || startpoint <= recptr)
10668  ereport(ERROR,
10669  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10670  errmsg("WAL generated with full_page_writes=off was replayed "
10671  "since last restartpoint"),
10672  errhint("This means that the backup being taken on the standby "
10673  "is corrupt and should not be used. "
10674  "Enable full_page_writes and run CHECKPOINT on the primary, "
10675  "and then try an online backup again.")));
10676 
10677  /*
10678  * During recovery, since we don't use the end-of-backup WAL
10679  * record and don't write the backup history file, the
10680  * starting WAL location doesn't need to be unique. This means
10681  * that two base backups started at the same time might use
10682  * the same checkpoint as starting locations.
10683  */
10684  gotUniqueStartpoint = true;
10685  }
10686 
10687  /*
10688  * If two base backups are started at the same time (in WAL sender
10689  * processes), we need to make sure that they use different
10690  * checkpoints as starting locations, because we use the starting
10691  * WAL location as a unique identifier for the base backup in the
10692  * end-of-backup WAL record and when we write the backup history
10693  * file. Perhaps it would be better generate a separate unique ID
10694  * for each backup instead of forcing another checkpoint, but
10695  * taking a checkpoint right after another is not that expensive
10696  * either because only few buffers have been dirtied yet.
10697  */
10699  if (XLogCtl->Insert.lastBackupStart < startpoint)
10700  {
10701  XLogCtl->Insert.lastBackupStart = startpoint;
10702  gotUniqueStartpoint = true;
10703  }
10705  } while (!gotUniqueStartpoint);
10706 
10707  XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
10708  XLogFileName(xlogfilename, starttli, _logSegNo, wal_segment_size);
10709 
10710  /*
10711  * Construct tablespace_map file
10712  */
10713  if (exclusive)
10714  tblspcmapfile = makeStringInfo();
10715 
10716  datadirpathlen = strlen(DataDir);
10717 
10718  /* Collect information about all tablespaces */
10719  tblspcdir = AllocateDir("pg_tblspc");
10720  while ((de = ReadDir(tblspcdir, "pg_tblspc")) != NULL)
10721  {
10722  char fullpath[MAXPGPATH + 10];
10723  char linkpath[MAXPGPATH];
10724  char *relpath = NULL;
10725  int rllen;
10726  StringInfoData buflinkpath;
10727  char *s = linkpath;
10728 
10729  /* Skip special stuff */
10730  if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
10731  continue;
10732 
10733  snprintf(fullpath, sizeof(fullpath), "pg_tblspc/%s", de->d_name);
10734 
10735 #if defined(HAVE_READLINK) || defined(WIN32)
10736  rllen = readlink(fullpath, linkpath, sizeof(linkpath));
10737  if (rllen < 0)
10738  {
10739  ereport(WARNING,
10740  (errmsg("could not read symbolic link \"%s\": %m",
10741  fullpath)));
10742  continue;
10743  }
10744  else if (rllen >= sizeof(linkpath))
10745  {
10746  ereport(WARNING,
10747  (errmsg("symbolic link \"%s\" target is too long",
10748  fullpath)));
10749  continue;
10750  }
10751  linkpath[rllen] = '\0';
10752 
10753  /*
10754  * Add the escape character '\\' before newline in a string to
10755  * ensure that we can distinguish between the newline in the
10756  * tablespace path and end of line while reading tablespace_map
10757  * file during archive recovery.
10758  */
10759  initStringInfo(&buflinkpath);
10760 
10761  while (*s)
10762  {
10763  if ((*s == '\n' || *s == '\r') && needtblspcmapfile)
10764  appendStringInfoChar(&buflinkpath, '\\');
10765  appendStringInfoChar(&buflinkpath, *s++);
10766  }
10767 
10768  /*
10769  * Relpath holds the relative path of the tablespace directory
10770  * when it's located within PGDATA, or NULL if it's located
10771  * elsewhere.
10772  */
10773  if (rllen > datadirpathlen &&
10774  strncmp(linkpath, DataDir, datadirpathlen) == 0 &&
10775  IS_DIR_SEP(linkpath[datadirpathlen]))
10776  relpath = linkpath + datadirpathlen + 1;
10777 
10778  ti = palloc(sizeof(tablespaceinfo));
10779  ti->oid = pstrdup(de->d_name);
10780  ti->path = pstrdup(buflinkpath.data);
10781  ti->rpath = relpath ? pstrdup(relpath) : NULL;
10782  ti->size = -1;
10783 
10784  if (tablespaces)
10785  *tablespaces = lappend(*tablespaces, ti);
10786 
10787  appendStringInfo(tblspcmapfile, "%s %s\n", ti->oid, ti->path);
10788 
10789  pfree(buflinkpath.data);
10790 #else
10791 
10792  /*
10793  * If the platform does not have symbolic links, it should not be
10794  * possible to have tablespaces - clearly somebody else created
10795  * them. Warn about it and ignore.
10796  */
10797  ereport(WARNING,
10798  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10799  errmsg("tablespaces are not supported on this platform")));
10800 #endif
10801  }
10802  FreeDir(tblspcdir);
10803 
10804  /*
10805  * Construct backup label file
10806  */
10807  if (exclusive)
10808  labelfile = makeStringInfo();
10809 
10810  /* Use the log timezone here, not the session timezone */
10811  stamp_time = (pg_time_t) time(NULL);
10812  pg_strftime(strfbuf, sizeof(strfbuf),
10813  "%Y-%m-%d %H:%M:%S %Z",
10814  pg_localtime(&stamp_time, log_timezone));
10815  appendStringInfo(labelfile, "START WAL LOCATION: %X/%X (file %s)\n",
10816  (uint32) (startpoint >> 32), (uint32) startpoint, xlogfilename);
10817  appendStringInfo(labelfile, "CHECKPOINT LOCATION: %X/%X\n",
10818  (uint32) (checkpointloc >> 32), (uint32) checkpointloc);
10819  appendStringInfo(labelfile, "BACKUP METHOD: %s\n",
10820  exclusive ? "pg_start_backup" : "streamed");
10821  appendStringInfo(labelfile, "BACKUP FROM: %s\n",
10822  backup_started_in_recovery ? "standby" : "primary");
10823  appendStringInfo(labelfile, "START TIME: %s\n", strfbuf);
10824  appendStringInfo(labelfile, "LABEL: %s\n", backupidstr);
10825  appendStringInfo(labelfile, "START TIMELINE: %u\n", starttli);
10826 
10827  /*
10828  * Okay, write the file, or return its contents to caller.
10829  */
10830  if (exclusive)
10831  {
10832  /*
10833  * Check for existing backup label --- implies a backup is already
10834  * running. (XXX given that we checked exclusiveBackupState
10835  * above, maybe it would be OK to just unlink any such label
10836  * file?)
10837  */
10838  if (stat(BACKUP_LABEL_FILE, &stat_buf) != 0)
10839  {
10840  if (errno != ENOENT)
10841  ereport(ERROR,
10843  errmsg("could not stat file \"%s\": %m",
10844  BACKUP_LABEL_FILE)));
10845  }
10846  else
10847  ereport(ERROR,
10848  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10849  errmsg("a backup is already in progress"),
10850  errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
10851  BACKUP_LABEL_FILE)));
10852 
10853  fp = AllocateFile(BACKUP_LABEL_FILE, "w");
10854 
10855  if (!fp)
10856  ereport(ERROR,
10858  errmsg("could not create file \"%s\": %m",
10859  BACKUP_LABEL_FILE)));
10860  if (fwrite(labelfile->data, labelfile->len, 1, fp) != 1 ||
10861  fflush(fp) != 0 ||
10862  pg_fsync(fileno(fp)) != 0 ||
10863  ferror(fp) ||
10864  FreeFile(fp))
10865  ereport(ERROR,
10867  errmsg("could not write file \"%s\": %m",
10868  BACKUP_LABEL_FILE)));
10869  /* Allocated locally for exclusive backups, so free separately */
10870  pfree(labelfile->data);
10871  pfree(labelfile);
10872 
10873  /* Write backup tablespace_map file. */
10874  if (tblspcmapfile->len > 0)
10875  {
10876  if (stat(TABLESPACE_MAP, &stat_buf) != 0)
10877  {
10878  if (errno != ENOENT)
10879  ereport(ERROR,
10881  errmsg("could not stat file \"%s\": %m",
10882  TABLESPACE_MAP)));
10883  }
10884  else
10885  ereport(ERROR,
10886  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10887  errmsg("a backup is already in progress"),
10888  errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
10889  TABLESPACE_MAP)));
10890 
10891  fp = AllocateFile(TABLESPACE_MAP, "w");
10892 
10893  if (!fp)
10894  ereport(ERROR,
10896  errmsg("could not create file \"%s\": %m",
10897  TABLESPACE_MAP)));
10898  if (fwrite(tblspcmapfile->data, tblspcmapfile->len, 1, fp) != 1 ||
10899  fflush(fp) != 0 ||
10900  pg_fsync(fileno(fp)) != 0 ||
10901  ferror(fp) ||
10902  FreeFile(fp))
10903  ereport(ERROR,
10905  errmsg("could not write file \"%s\": %m",
10906  TABLESPACE_MAP)));
10907  }
10908 
10909  /* Allocated locally for exclusive backups, so free separately */
10910  pfree(tblspcmapfile->data);
10911  pfree(tblspcmapfile);
10912  }
10913  }
10915 
10916  /*
10917  * Mark that start phase has correctly finished for an exclusive backup.
10918  * Session-level locks are updated as well to reflect that state.
10919  *
10920  * Note that CHECK_FOR_INTERRUPTS() must not occur while updating backup
10921  * counters and session-level lock. Otherwise they can be updated
10922  * inconsistently, and which might cause do_pg_abort_backup() to fail.
10923  */
10924  if (exclusive)
10925  {
10928 
10929  /* Set session-level lock */
10932  }
10933  else
10935 
10936  /*
10937  * We're done. As a convenience, return the starting WAL location.
10938  */
10939  if (starttli_p)
10940  *starttli_p = starttli;
10941  return startpoint;
10942 }
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:128
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9678
int errhint(const char *fmt,...)
Definition: elog.c:1071
uint32 TimeLineID
Definition: xlogdefs.h:52
int64 pg_time_t
Definition: pgtime.h:23
static void WALInsertLockRelease(void)
Definition: xlog.c:1726
int wal_segment_size
Definition: xlog.c:116
XLogRecPtr lastFpwDisableRecPtr
Definition: xlog.c:723
static SessionBackupState sessionBackupState
Definition: xlog.c:533
XLogRecPtr lastBackupStart
Definition: xlog.c:586
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define XLogIsNeeded()
Definition: xlog.h:191
char * rpath
Definition: basebackup.h:27
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
slock_t info_lck
Definition: xlog.c:725
int errcode(int sqlerrcode)
Definition: elog.c:610
CheckPoint checkPointCopy
Definition: pg_control.h:131
XLogCtlInsert Insert
Definition: xlog.c:599
bool RecoveryInProgress(void)
Definition: xlog.c:8069
static bool backup_started_in_recovery
Definition: basebackup.c:88
Definition: dirent.h:9
#define IS_DIR_SEP(ch)
Definition: port.h:84
pg_tz * log_timezone
Definition: pgtz.c:31
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define TABLESPACE_MAP
Definition: xlog.h:392
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
bool forcePageWrites
Definition: xlog.c:573
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:100
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:41
#define readlink(path, buf, size)
Definition: win32_port.h:222
int errcode_for_file_access(void)
Definition: elog.c:633
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2322
unsigned int uint32
Definition: c.h:374
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2583
#define CHECKPOINT_FORCE
Definition: xlog.h:225
List * lappend(List *list, void *datum)
Definition: list.c:321
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define WARNING
Definition: elog.h:40
int nonExclusiveBackups
Definition: xlog.c:585
#define stat(a, b)
Definition: win32_port.h:255
#define MAXFNAMELEN
#define SpinLockRelease(lock)
Definition: spin.h:64
static void pg_start_backup_callback(int code, Datum arg)
Definition: xlog.c:10946
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:584
uintptr_t Datum
Definition: postgres.h:367
static ControlFileData * ControlFile
Definition: xlog.c:736
#define BoolGetDatum(X)
Definition: postgres.h:402
#define ereport(elevel,...)
Definition: elog.h:144
bool fullPageWrites
Definition: pg_control.h:42
#define CHECKPOINT_WAIT
Definition: xlog.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1697
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1342
int FreeFile(FILE *file)
Definition: fd.c:2521
void * palloc(Size size)
Definition: mcxt.c:949
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:224
#define relpath(rnode, forknum)
Definition: relpath.h:87
char * DataDir
Definition: globals.c:62
#define BACKUP_LABEL_FILE
Definition: xlog.h:389
int pg_fsync(int fd)
Definition: fd.c:345
char d_name[MAX_PATH]
Definition: dirent.h:14
#define snprintf
Definition: port.h:193
XLogRecPtr checkPoint
Definition: pg_control.h:129
XLogRecPtr redo
Definition: pg_control.h:37
int FreeDir(DIR *dir)
Definition: fd.c:2701
void RequestCheckpoint(int flags)
Definition: checkpointer.c:903
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ do_pg_stop_backup()

XLogRecPtr do_pg_stop_backup ( char *  labelfile,
bool  waitforarchive,
TimeLineID stoptli_p 
)

Definition at line 11014 of file xlog.c.

References AllocateFile(), Assert, BACKUP_LABEL_FILE, backup_started_in_recovery, BackupHistoryFileName, BackupHistoryFilePath, BoolGetDatum, CHECK_FOR_INTERRUPTS, CleanupBackupHistory(), DEBUG1, durable_unlink(), ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, EXCLUSIVE_BACKUP_IN_PROGRESS, EXCLUSIVE_BACKUP_NONE, EXCLUSIVE_BACKUP_STOPPING, XLogCtlInsert::exclusiveBackupState, XLogCtlInsert::forcePageWrites, fprintf, FreeFile(), XLogCtlData::info_lck, XLogCtlData::Insert, XLogCtlData::lastFpwDisableRecPtr, log_timezone, LW_SHARED, LWLockAcquire(), LWLockRelease(), MAXFNAMELEN, MAXPGPATH, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, XLogCtlInsert::nonExclusiveBackups, NOTICE, palloc(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, pg_localtime(), pg_stop_backup_callback(), pg_strftime(), pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), RecoveryInProgress(), remaining, RequestXLogSwitch(), SESSION_BACKUP_NONE, sessionBackupState, SpinLockAcquire, SpinLockRelease, stat, TABLESPACE_MAP, ThisTimeLineID, WAIT_EVENT_BACKUP_WAIT_WAL_ARCHIVE, wal_segment_size, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WARNING, XLByteToPrevSeg, XLByteToSeg, XLOG_BACKUP_END, XLogArchiveIsBusy(), XLogArchivingActive, XLogArchivingAlways, XLogBeginInsert(), XLogFileName, XLogInsert(), XLogIsNeeded, and XLogRegisterData().

Referenced by perform_base_backup(), pg_stop_backup(), and pg_stop_backup_v2().

11015 {
11016  bool exclusive = (labelfile == NULL);
11017  bool backup_started_in_recovery = false;
11018  XLogRecPtr startpoint;
11019  XLogRecPtr stoppoint;
11020  TimeLineID stoptli;
11021  pg_time_t stamp_time;
11022  char strfbuf[128];
11023  char histfilepath[MAXPGPATH];
11024  char startxlogfilename[MAXFNAMELEN];
11025  char stopxlogfilename[MAXFNAMELEN];
11026  char lastxlogfilename[MAXFNAMELEN];
11027  char histfilename[MAXFNAMELEN];
11028  char backupfrom[20];
11029  XLogSegNo _logSegNo;
11030  FILE *lfp;
11031  FILE *fp;
11032  char ch;
11033  int seconds_before_warning;
11034  int waits = 0;
11035  bool reported_waiting = false;
11036  char *remaining;
11037  char *ptr;
11038  uint32 hi,
11039  lo;
11040 
11041  backup_started_in_recovery = RecoveryInProgress();
11042 
11043  /*
11044  * Currently only non-exclusive backup can be taken during recovery.
11045  */
11046  if (backup_started_in_recovery && exclusive)
11047  ereport(ERROR,
11048  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11049  errmsg("recovery is in progress"),
11050  errhint("WAL control functions cannot be executed during recovery.")));
11051 
11052  /*
11053  * During recovery, we don't need to check WAL level. Because, if WAL
11054  * level is not sufficient, it's impossible to get here during recovery.
11055  */
11056  if (!backup_started_in_recovery && !XLogIsNeeded())
11057  ereport(ERROR,
11058  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11059  errmsg("WAL level not sufficient for making an online backup"),
11060  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
11061 
11062  if (exclusive)
11063  {
11064  /*
11065  * At first, mark that we're now stopping an exclusive backup, to
11066  * ensure that there are no other sessions currently running
11067  * pg_start_backup() or pg_stop_backup().
11068  */
11071  {
11073  ereport(ERROR,
11074  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11075  errmsg("exclusive backup not in progress")));
11076  }
11079 
11080  /*
11081  * Remove backup_label. In case of failure, the state for an exclusive
11082  * backup is switched back to in-progress.
11083  */
11085  {
11086  /*
11087  * Read the existing label file into memory.
11088  */
11089  struct stat statbuf;
11090  int r;
11091 
11092  if (stat(BACKUP_LABEL_FILE, &statbuf))
11093  {
11094  /* should not happen per the upper checks */
11095  if (errno != ENOENT)
11096  ereport(ERROR,
11098  errmsg("could not stat file \"%s\": %m",
11099  BACKUP_LABEL_FILE)));
11100  ereport(ERROR,
11101  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11102  errmsg("a backup is not in progress")));
11103  }
11104 
11105  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
11106  if (!lfp)
11107  {
11108  ereport(ERROR,
11110  errmsg("could not read file \"%s\": %m",
11111  BACKUP_LABEL_FILE)));
11112  }
11113  labelfile = palloc(statbuf.st_size + 1);
11114  r = fread(labelfile, statbuf.st_size, 1, lfp);
11115  labelfile[statbuf.st_size] = '\0';
11116 
11117  /*
11118  * Close and remove the backup label file
11119  */
11120  if (r != 1 || ferror(lfp) || FreeFile(lfp))
11121  ereport(ERROR,
11123  errmsg("could not read file \"%s\": %m",
11124  BACKUP_LABEL_FILE)));
11126 
11127  /*
11128  * Remove tablespace_map file if present, it is created only if
11129  * there are tablespaces.
11130  */
11132  }
11134  }
11135 
11136  /*
11137  * OK to update backup counters, forcePageWrites and session-level lock.
11138  *
11139  * Note that CHECK_FOR_INTERRUPTS() must not occur while updating them.
11140  * Otherwise they can be updated inconsistently, and which might cause
11141  * do_pg_abort_backup() to fail.
11142  */
11144  if (exclusive)
11145  {
11147  }
11148  else
11149  {
11150  /*
11151  * The user-visible pg_start/stop_backup() functions that operate on
11152  * exclusive backups can be called at any time, but for non-exclusive
11153  * backups, it is expected that each do_pg_start_backup() call is
11154  * matched by exactly one do_pg_stop_backup() call.
11155  */
11158  }
11159 
11162  {
11163  XLogCtl->Insert.forcePageWrites = false;
11164  }
11165 
11166  /*
11167  * Clean up session-level lock.
11168  *
11169  * You might think that WALInsertLockRelease() can be called before
11170  * cleaning up session-level lock because session-level lock doesn't need
11171  * to be protected with WAL insertion lock. But since
11172  * CHECK_FOR_INTERRUPTS() can occur in it, session-level lock must be
11173  * cleaned up before it.
11174  */
11176 
11178 
11179  /*
11180  * Read and parse the START WAL LOCATION line (this code is pretty crude,
11181  * but we are not expecting any variability in the file format).
11182  */
11183  if (sscanf(labelfile, "START WAL LOCATION: %X/%X (file %24s)%c",
11184  &hi, &lo, startxlogfilename,
11185  &ch) != 4 || ch != '\n')
11186  ereport(ERROR,
11187  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11188  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
11189  startpoint = ((uint64) hi) << 32 | lo;
11190  remaining = strchr(labelfile, '\n') + 1; /* %n is not portable enough */
11191 
11192  /*
11193  * Parse the BACKUP FROM line. If we are taking an online backup from the
11194  * standby, we confirm that the standby has not been promoted during the
11195  * backup.
11196  */
11197  ptr = strstr(remaining, "BACKUP FROM:");
11198  if (!ptr || sscanf(ptr, "BACKUP FROM: %19s\n", backupfrom) != 1)
11199  ereport(ERROR,
11200  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11201  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
11202  if (strcmp(backupfrom, "standby") == 0 && !backup_started_in_recovery)
11203  ereport(ERROR,
11204  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11205  errmsg("the standby was promoted during online backup"),
11206  errhint("This means that the backup being taken is corrupt "
11207  "and should not be used. "
11208  "Try taking another online backup.")));
11209 
11210  /*
11211  * During recovery, we don't write an end-of-backup record. We assume that
11212  * pg_control was backed up last and its minimum recovery point can be
11213  * available as the backup end location. Since we don't have an
11214  * end-of-backup record, we use the pg_control value to check whether
11215  * we've reached the end of backup when starting recovery from this
11216  * backup. We have no way of checking if pg_control wasn't backed up last
11217  * however.
11218  *
11219  * We don't force a switch to new WAL file but it is still possible to
11220  * wait for all the required files to be archived if waitforarchive is
11221  * true. This is okay if we use the backup to start a standby and fetch
11222  * the missing WAL using streaming replication. But in the case of an
11223  * archive recovery, a user should set waitforarchive to true and wait for
11224  * them to be archived to ensure that all the required files are
11225  * available.
11226  *
11227  * We return the current minimum recovery point as the backup end
11228  * location. Note that it can be greater than the exact backup end
11229  * location if the minimum recovery point is updated after the backup of
11230  * pg_control. This is harmless for current uses.
11231  *
11232  * XXX currently a backup history file is for informational and debug
11233  * purposes only. It's not essential for an online backup. Furthermore,
11234  * even if it's created, it will not be archived during recovery because
11235  * an archiver is not invoked. So it doesn't seem worthwhile to write a
11236  * backup history file during recovery.
11237  */
11238  if (backup_started_in_recovery)
11239  {
11240  XLogRecPtr recptr;
11241 
11242  /*
11243  * Check to see if all WAL replayed during online backup contain
11244  * full-page writes.
11245  */
11247  recptr = XLogCtl->lastFpwDisableRecPtr;
11249 
11250  if (startpoint <= recptr)
11251  ereport(ERROR,
11252  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
11253  errmsg("WAL generated with full_page_writes=off was replayed "
11254  "during online backup"),
11255  errhint("This means that the backup being taken on the standby "
11256  "is corrupt and should not be used. "
11257  "Enable full_page_writes and run CHECKPOINT on the primary, "
11258  "and then try an online backup again.")));
11259 
11260 
11261  LWLockAcquire(ControlFileLock, LW_SHARED);
11262  stoppoint = ControlFile->minRecoveryPoint;
11263  stoptli = ControlFile->minRecoveryPointTLI;
11264  LWLockRelease(ControlFileLock);
11265  }
11266  else
11267  {
11268  /*
11269  * Write the backup-end xlog record
11270  */
11271  XLogBeginInsert();
11272  XLogRegisterData((char *) (&startpoint), sizeof(startpoint));
11273  stoppoint = XLogInsert(RM_XLOG_ID, XLOG_BACKUP_END);
11274  stoptli = ThisTimeLineID;
11275 
11276  /*
11277  * Force a switch to a new xlog segment file, so that the backup is
11278  * valid as soon as archiver moves out the current segment file.
11279  */
11280  RequestXLogSwitch(false);
11281 
11282  XLByteToPrevSeg(stoppoint, _logSegNo, wal_segment_size);
11283  XLogFileName(stopxlogfilename, stoptli, _logSegNo, wal_segment_size);
11284 
11285  /* Use the log timezone here, not the session timezone */
11286  stamp_time = (pg_time_t) time(NULL);
11287  pg_strftime(strfbuf, sizeof(strfbuf),
11288  "%Y-%m-%d %H:%M:%S %Z",
11289  pg_localtime(&stamp_time, log_timezone));
11290 
11291  /*
11292  * Write the backup history file
11293  */
11294  XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
11295  BackupHistoryFilePath(histfilepath, stoptli, _logSegNo,
11296  startpoint, wal_segment_size);
11297  fp = AllocateFile(histfilepath, "w");
11298  if (!fp)
11299  ereport(ERROR,
11301  errmsg("could not create file \"%s\": %m",
11302  histfilepath)));
11303  fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
11304  (uint32) (startpoint >> 32), (uint32) startpoint, startxlogfilename);
11305  fprintf(fp, "STOP WAL LOCATION: %X/%X (file %s)\n",
11306  (uint32) (stoppoint >> 32), (uint32) stoppoint, stopxlogfilename);
11307 
11308  /*
11309  * Transfer remaining lines including label and start timeline to
11310  * history file.
11311  */
11312  fprintf(fp, "%s", remaining);
11313  fprintf(fp, "STOP TIME: %s\n", strfbuf);
11314  fprintf(fp, "STOP TIMELINE: %u\n", stoptli);
11315  if (fflush(fp) || ferror(fp) || FreeFile(fp))
11316  ereport(ERROR,
11318  errmsg("could not write file \"%s\": %m",
11319  histfilepath)));
11320 
11321  /*
11322  * Clean out any no-longer-needed history files. As a side effect,
11323  * this will post a .ready file for the newly created history file,
11324  * notifying the archiver that history file may be archived
11325  * immediately.
11326  */
11328  }
11329 
11330  /*
11331  * If archiving is enabled, wait for all the required WAL files to be
11332  * archived before returning. If archiving isn't enabled, the required WAL
11333  * needs to be transported via streaming replication (hopefully with
11334  * wal_keep_size set high enough), or some more exotic mechanism like
11335  * polling and copying files from pg_wal with script. We have no knowledge
11336  * of those mechanisms, so it's up to the user to ensure that he gets all
11337  * the required WAL.
11338  *
11339  * We wait until both the last WAL file filled during backup and the
11340  * history file have been archived, and assume that the alphabetic sorting
11341  * property of the WAL files ensures any earlier WAL files are safely
11342  * archived as well.
11343  *
11344  * We wait forever, since archive_command is supposed to work and we
11345  * assume the admin wanted his backup to work completely. If you don't
11346  * wish to wait, then either waitforarchive should be passed in as false,
11347  * or you can set statement_timeout. Also, some notices are issued to
11348  * clue in anyone who might be doing this interactively.
11349  */
11350 
11351  if (waitforarchive &&
11352  ((!backup_started_in_recovery && XLogArchivingActive()) ||
11353  (backup_started_in_recovery && XLogArchivingAlways())))
11354  {
11355  XLByteToPrevSeg(stoppoint, _logSegNo, wal_segment_size);
11356  XLogFileName(lastxlogfilename, stoptli, _logSegNo, wal_segment_size);
11357 
11358  XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
11359  BackupHistoryFileName(histfilename, stoptli, _logSegNo,
11360  startpoint, wal_segment_size);
11361 
11362  seconds_before_warning = 60;
11363  waits = 0;
11364 
11365  while (XLogArchiveIsBusy(lastxlogfilename) ||
11366  XLogArchiveIsBusy(histfilename))
11367  {
11369 
11370  if (!reported_waiting && waits > 5)
11371  {
11372  ereport(NOTICE,
11373  (errmsg("base backup done, waiting for required WAL segments to be archived")));
11374  reported_waiting = true;
11375  }
11376 
11378  pg_usleep(1000000L);
11380 
11381  if (++waits >= seconds_before_warning)
11382  {
11383  seconds_before_warning *= 2; /* This wraps in >10 years... */
11384  ereport(WARNING,
11385  (errmsg("still waiting for all required WAL segments to be archived (%d seconds elapsed)",
11386  waits),
11387  errhint("Check that your archive_command is executing properly. "
11388  "You can safely cancel this backup, "
11389  "but the database backup will not be usable without all the WAL segments.")));
11390  }
11391  }
11392 
11393  ereport(NOTICE,
11394  (errmsg("all required WAL segments have been archived")));
11395  }
11396  else if (waitforarchive)
11397  ereport(NOTICE,
11398  (errmsg("WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup")));
11399 
11400  /*
11401  * We're done. As a convenience, return the ending WAL location.
11402  */
11403  if (stoptli_p)
11404  *stoptli_p = stoptli;
11405  return stoppoint;
11406 }
int remaining
Definition: informix.c:667
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:128
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9678
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:1071
uint32 TimeLineID
Definition: xlogdefs.h:52
int64 pg_time_t
Definition: pgtime.h:23
static void WALInsertLockRelease(void)
Definition: xlog.c:1726
int wal_segment_size
Definition: xlog.c:116
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:167
XLogRecPtr lastFpwDisableRecPtr
Definition: xlog.c:723
static SessionBackupState sessionBackupState
Definition: xlog.c:533
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
#define XLogIsNeeded()
Definition: xlog.h:191
slock_t info_lck
Definition: xlog.c:725
int errcode(int sqlerrcode)
Definition: elog.c:610
XLogCtlInsert Insert
Definition: xlog.c:599
#define BackupHistoryFileName(fname, tli, logSegNo, startpoint, wal_segsz_bytes)
bool RecoveryInProgress(void)
Definition: xlog.c:8069
static bool backup_started_in_recovery
Definition: basebackup.c:88
#define fprintf
Definition: port.h:197
pg_tz * log_timezone
Definition: pgtz.c:31
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define TABLESPACE_MAP
Definition: xlog.h:392
#define SpinLockAcquire(lock)
Definition: spin.h:62
void pg_usleep(long microsec)
Definition: signal.c:53
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
#define XLogArchivingAlways()
Definition: xlog.h:183
bool forcePageWrites
Definition: xlog.c:573
#define ERROR
Definition: elog.h:43
static void CleanupBackupHistory(void)
Definition: xlog.c:4289
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:41
int errcode_for_file_access(void)
Definition: elog.c:633
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2322
unsigned int uint32
Definition: c.h:374
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1381
#define XLOG_BACKUP_END
Definition: pg_control.h:72
#define WARNING
Definition: elog.h:40
int nonExclusiveBackups
Definition: xlog.c:585
#define stat(a, b)
Definition: win32_port.h:255
#define MAXFNAMELEN
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:330
#define SpinLockRelease(lock)
Definition: spin.h:64
#define BackupHistoryFilePath(path, tli, logSegNo, startpoint, wal_segsz_bytes)
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:584
uintptr_t Datum
Definition: postgres.h:367
static ControlFileData * ControlFile
Definition: xlog.c:736
#define BoolGetDatum(X)
Definition: postgres.h:402
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define ereport(elevel,...)
Definition: elog.h:144
#define NOTICE
Definition: elog.h:37
bool XLogArchiveIsBusy(const char *xlog)
Definition: xlogarchive.c:625
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define XLogArchivingActive()
Definition: xlog.h:180
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1357
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1697
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
static void pg_stop_backup_callback(int code, Datum arg)
Definition: xlog.c:10975
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1342
int durable_unlink(const char *fname, int elevel)
Definition: fd.c:748
int FreeFile(FILE *file)
Definition: fd.c:2521
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define BACKUP_LABEL_FILE
Definition: xlog.h:389
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void XLogBeginInsert(void)
Definition: xloginsert.c:123
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:166
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ emode_for_corrupt_record()

static int emode_for_corrupt_record ( int  emode,
XLogRecPtr  RecPtr 
)
static

Definition at line 12532 of file xlog.c.

References DEBUG1, LOG, readSource, and XLOG_FROM_PG_WAL.

Referenced by ReadRecord(), and XLogPageRead().

12533 {
12534  static XLogRecPtr lastComplaint = 0;
12535 
12536  if (readSource == XLOG_FROM_PG_WAL && emode == LOG)
12537  {
12538  if (RecPtr == lastComplaint)
12539  emode = DEBUG1;
12540  else
12541  lastComplaint = RecPtr;
12542  }
12543  return emode;
12544 }
#define DEBUG1
Definition: elog.h:25
static XLogSource readSource
Definition: xlog.c:815
#define LOG
Definition: elog.h:26
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ exitArchiveRecovery()

static void exitArchiveRecovery ( TimeLineID  endTLI,
XLogRecPtr  endOfLog 
)
static

Definition at line 5543 of file xlog.c.

References Assert, close, durable_unlink(), ereport, errcode_for_file_access(), errmsg(), ERROR, FATAL, fd(), InArchiveRecovery, InvalidXLogRecPtr, LOG, MAXFNAMELEN, readFile, RECOVERY_SIGNAL_FILE, recovery_signal_file_found, STANDBY_SIGNAL_FILE, standby_signal_file_found, ThisTimeLineID, UpdateMinRecoveryPoint(), wal_segment_size, XLByteToPrevSeg, XLByteToSeg, XLogArchiveCleanup(), XLogFileCopy(), XLogFileInit(), XLogFileName, and XLogSegmentOffset.

Referenced by StartupXLOG().

5544 {
5545  char xlogfname[MAXFNAMELEN];
5546  XLogSegNo endLogSegNo;
5547  XLogSegNo startLogSegNo;
5548 
5549  /* we always switch to a new timeline after archive recovery */
5550  Assert(endTLI != ThisTimeLineID);
5551 
5552  /*
5553  * We are no longer in archive recovery state.
5554  */
5555  InArchiveRecovery = false;
5556 
5557  /*
5558  * Update min recovery point one last time.
5559  */
5561 
5562  /*
5563  * If the ending log segment is still open, close it (to avoid problems on
5564  * Windows with trying to rename or delete an open file).
5565  */
5566  if (readFile >= 0)
5567  {
5568  close(readFile);
5569  readFile = -1;
5570  }
5571 
5572  /*
5573  * Calculate the last segment on the old timeline, and the first segment
5574  * on the new timeline. If the switch happens in the middle of a segment,
5575  * they are the same, but if the switch happens exactly at a segment
5576  * boundary, startLogSegNo will be endLogSegNo + 1.
5577  */
5578  XLByteToPrevSeg(endOfLog, endLogSegNo, wal_segment_size);
5579  XLByteToSeg(endOfLog, startLogSegNo, wal_segment_size);
5580 
5581  /*
5582  * Initialize the starting WAL segment for the new timeline. If the switch
5583  * happens in the middle of a segment, copy data from the last WAL segment
5584  * of the old timeline up to the switch point, to the starting WAL segment
5585  * on the new timeline.
5586  */
5587  if (endLogSegNo == startLogSegNo)
5588  {
5589  /*
5590  * Make a copy of the file on the new timeline.
5591  *
5592  * Writing WAL isn't allowed yet, so there are no locking
5593  * considerations. But we should be just as tense as XLogFileInit to
5594  * avoid emplacing a bogus file.
5595  */
5596  XLogFileCopy(endLogSegNo, endTLI, endLogSegNo,
5597  XLogSegmentOffset(endOfLog, wal_segment_size));
5598  }
5599  else
5600  {
5601  /*
5602  * The switch happened at a segment boundary, so just create the next
5603  * segment on the new timeline.
5604  */
5605  bool use_existent = true;
5606  int fd;
5607 
5608  fd = XLogFileInit(startLogSegNo, &use_existent, true);
5609 
5610  if (close(fd) != 0)
5611  {
5612  char xlogfname[MAXFNAMELEN];
5613  int save_errno = errno;
5614 
5615  XLogFileName(xlogfname, ThisTimeLineID, startLogSegNo,
5617  errno = save_errno;
5618  ereport(ERROR,
5620  errmsg("could not close file \"%s\": %m", xlogfname)));
5621  }
5622  }
5623 
5624  /*
5625  * Let's just make real sure there are not .ready or .done flags posted
5626  * for the new segment.
5627  */
5628  XLogFileName(xlogfname, ThisTimeLineID, startLogSegNo, wal_segment_size);
5629  XLogArchiveCleanup(xlogfname);
5630 
5631  /*
5632  * Remove the signal files out of the way, so that we don't accidentally
5633  * re-enter archive recovery mode in a subsequent crash.
5634  */
5637 
5640 
5641  ereport(LOG,
5642  (errmsg("archive recovery complete")));
5643 }
#define RECOVERY_SIGNAL_FILE
Definition: xlog.h:387
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int wal_segment_size
Definition: xlog.c:116
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
Definition: xlog.c:2757
int XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
Definition: xlog.c:3251
static void XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno, int upto)
Definition: xlog.c:3438
#define LOG
Definition: elog.h:26
static bool standby_signal_file_found
Definition: xlog.c:268
static int fd(const char *x,