PostgreSQL Source Code  git master
xlog.h File Reference
#include "access/rmgr.h"
#include "access/xlogdefs.h"
#include "access/xloginsert.h"
#include "access/xlogreader.h"
#include "datatype/timestamp.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "storage/fd.h"
Include dependency graph for xlog.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  CheckpointStatsData
 

Macros

#define SYNC_METHOD_FSYNC   0
 
#define SYNC_METHOD_FDATASYNC   1
 
#define SYNC_METHOD_OPEN   2 /* for O_SYNC */
 
#define SYNC_METHOD_FSYNC_WRITETHROUGH   3
 
#define SYNC_METHOD_OPEN_DSYNC   4 /* for O_DSYNC */
 
#define InHotStandby   (standbyState >= STANDBY_SNAPSHOT_PENDING)
 
#define XLogArchivingActive()   (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)
 
#define XLogArchivingAlways()   (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
 
#define XLogArchiveCommandSet()   (XLogArchiveCommand[0] != '\0')
 
#define XLogIsNeeded()   (wal_level >= WAL_LEVEL_REPLICA)
 
#define XLogHintBitIsNeeded()   (DataChecksumsEnabled() || wal_log_hints)
 
#define XLogStandbyInfoActive()   (wal_level >= WAL_LEVEL_REPLICA)
 
#define XLogLogicalInfoActive()   (wal_level >= WAL_LEVEL_LOGICAL)
 
#define CHECKPOINT_IS_SHUTDOWN   0x0001 /* Checkpoint is for shutdown */
 
#define CHECKPOINT_END_OF_RECOVERY
 
#define CHECKPOINT_IMMEDIATE   0x0004 /* Do it without delays */
 
#define CHECKPOINT_FORCE   0x0008 /* Force even if no activity */
 
#define CHECKPOINT_FLUSH_ALL
 
#define CHECKPOINT_WAIT   0x0020 /* Wait for completion */
 
#define CHECKPOINT_REQUESTED   0x0040 /* Checkpoint request has been made */
 
#define CHECKPOINT_CAUSE_XLOG   0x0080 /* XLOG consumption */
 
#define CHECKPOINT_CAUSE_TIME   0x0100 /* Elapsed time */
 
#define XLOG_INCLUDE_ORIGIN   0x01 /* include the replication origin */
 
#define XLOG_MARK_UNIMPORTANT   0x02 /* record not important for durability */
 
#define XLOG_INCLUDE_XID   0x04 /* include XID of top-level xact */
 
#define RECOVERY_SIGNAL_FILE   "recovery.signal"
 
#define STANDBY_SIGNAL_FILE   "standby.signal"
 
#define BACKUP_LABEL_FILE   "backup_label"
 
#define BACKUP_LABEL_OLD   "backup_label.old"
 
#define TABLESPACE_MAP   "tablespace_map"
 
#define TABLESPACE_MAP_OLD   "tablespace_map.old"
 
#define PROMOTE_SIGNAL_FILE   "promote"
 

Typedefs

typedef enum ArchiveMode ArchiveMode
 
typedef enum WalLevel WalLevel
 
typedef enum RecoveryState RecoveryState
 
typedef struct CheckpointStatsData CheckpointStatsData
 
typedef enum WALAvailability WALAvailability
 
typedef enum SessionBackupState SessionBackupState
 

Enumerations

enum  HotStandbyState { STANDBY_DISABLED, STANDBY_INITIALIZED, STANDBY_SNAPSHOT_PENDING, STANDBY_SNAPSHOT_READY }
 
enum  RecoveryTargetType {
  RECOVERY_TARGET_UNSET, RECOVERY_TARGET_XID, RECOVERY_TARGET_TIME, RECOVERY_TARGET_NAME,
  RECOVERY_TARGET_LSN, RECOVERY_TARGET_IMMEDIATE
}
 
enum  RecoveryTargetTimeLineGoal { RECOVERY_TARGET_TIMELINE_CONTROLFILE, RECOVERY_TARGET_TIMELINE_LATEST, RECOVERY_TARGET_TIMELINE_NUMERIC }
 
enum  ArchiveMode { ARCHIVE_MODE_OFF = 0, ARCHIVE_MODE_ON, ARCHIVE_MODE_ALWAYS }
 
enum  WalLevel { WAL_LEVEL_MINIMAL = 0, WAL_LEVEL_REPLICA, WAL_LEVEL_LOGICAL }
 
enum  RecoveryState { RECOVERY_STATE_CRASH = 0, RECOVERY_STATE_ARCHIVE, RECOVERY_STATE_DONE }
 
enum  WALAvailability {
  WALAVAIL_INVALID_LSN, WALAVAIL_RESERVED, WALAVAIL_EXTENDED, WALAVAIL_UNRESERVED,
  WALAVAIL_REMOVED
}
 
enum  SessionBackupState { SESSION_BACKUP_NONE, SESSION_BACKUP_EXCLUSIVE, SESSION_BACKUP_NON_EXCLUSIVE }
 

Functions

XLogRecPtr XLogInsertRecord (struct XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi)
 
void XLogFlush (XLogRecPtr RecPtr)
 
bool XLogBackgroundFlush (void)
 
bool XLogNeedsFlush (XLogRecPtr RecPtr)
 
int XLogFileInit (XLogSegNo segno, bool *use_existent, bool use_lock)
 
int XLogFileOpen (XLogSegNo segno)
 
void CheckXLogRemoved (XLogSegNo segno, TimeLineID tli)
 
XLogSegNo XLogGetLastRemovedSegno (void)
 
void XLogSetAsyncXactLSN (XLogRecPtr record)
 
void XLogSetReplicationSlotMinimumLSN (XLogRecPtr lsn)
 
void xlog_redo (XLogReaderState *record)
 
void xlog_desc (StringInfo buf, XLogReaderState *record)
 
const char * xlog_identify (uint8 info)
 
void issue_xlog_fsync (int fd, XLogSegNo segno)
 
bool RecoveryInProgress (void)
 
RecoveryState GetRecoveryState (void)
 
bool HotStandbyActive (void)
 
bool HotStandbyActiveInReplay (void)
 
bool XLogInsertAllowed (void)
 
void GetXLogReceiptTime (TimestampTz *rtime, bool *fromStream)
 
XLogRecPtr GetXLogReplayRecPtr (TimeLineID *replayTLI)
 
XLogRecPtr GetXLogInsertRecPtr (void)
 
XLogRecPtr GetXLogWriteRecPtr (void)
 
bool RecoveryIsPaused (void)
 
void SetRecoveryPause (bool recoveryPause)
 
TimestampTz GetLatestXTime (void)
 
TimestampTz GetCurrentChunkReplayStartTime (void)
 
void UpdateControlFile (void)
 
uint64 GetSystemIdentifier (void)
 
char * GetMockAuthenticationNonce (void)
 
bool DataChecksumsEnabled (void)
 
XLogRecPtr GetFakeLSNForUnloggedRel (void)
 
Size XLOGShmemSize (void)
 
void XLOGShmemInit (void)
 
void BootStrapXLOG (void)
 
void LocalProcessControlFile (bool reset)
 
void StartupXLOG (void)
 
void ShutdownXLOG (int code, Datum arg)
 
void InitXLOGAccess (void)
 
void CreateCheckPoint (int flags)
 
bool CreateRestartPoint (int flags)
 
WALAvailability GetWALAvailability (XLogRecPtr targetLSN)
 
XLogRecPtr CalculateMaxmumSafeLSN (void)
 
void XLogPutNextOid (Oid nextOid)
 
XLogRecPtr XLogRestorePoint (const char *rpName)
 
void UpdateFullPageWrites (void)
 
void GetFullPageWriteInfo (XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
 
XLogRecPtr GetRedoRecPtr (void)
 
XLogRecPtr GetInsertRecPtr (void)
 
XLogRecPtr GetFlushRecPtr (void)
 
XLogRecPtr GetLastImportantRecPtr (void)
 
void RemovePromoteSignalFiles (void)
 
bool PromoteIsTriggered (void)
 
bool CheckPromoteSignal (void)
 
void WakeupRecovery (void)
 
void SetWalWriterSleeping (bool sleeping)
 
void StartupRequestWalReceiverRestart (void)
 
void XLogRequestWalReceiverReply (void)
 
void assign_max_wal_size (int newval, void *extra)
 
void assign_checkpoint_completion_target (double newval, void *extra)
 
XLogRecPtr do_pg_start_backup (const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, List **tablespaces, StringInfo tblspcmapfile, bool needtblspcmapfile)
 
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)
 
SessionBackupState get_backup_status (void)
 

Variables

int sync_method
 
PGDLLIMPORT TimeLineID ThisTimeLineID
 
bool InRecovery
 
HotStandbyState standbyState
 
XLogRecPtr ProcLastRecPtr
 
XLogRecPtr XactLastRecEnd
 
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
 
bool reachedConsistency
 
int wal_segment_size
 
int min_wal_size_mb
 
int max_wal_size_mb
 
int wal_keep_size_mb
 
int max_slot_wal_keep_size_mb
 
int XLOGbuffers
 
int XLogArchiveTimeout
 
int wal_retrieve_retry_interval
 
char * XLogArchiveCommand
 
bool EnableHotStandby
 
bool fullPageWrites
 
bool wal_log_hints
 
bool wal_compression
 
bool wal_init_zero
 
bool wal_recycle
 
boolwal_consistency_checking
 
char * wal_consistency_checking_string
 
bool log_checkpoints
 
char * recoveryRestoreCommand
 
char * recoveryEndCommand
 
char * archiveCleanupCommand
 
bool recoveryTargetInclusive
 
int recoveryTargetAction
 
int recovery_min_apply_delay
 
char * PrimaryConnInfo
 
char * PrimarySlotName
 
bool wal_receiver_create_temp_slot
 
TransactionId recoveryTargetXid
 
char * recovery_target_time_string
 
const char * recoveryTargetName
 
XLogRecPtr recoveryTargetLSN
 
RecoveryTargetType recoveryTarget
 
char * PromoteTriggerFile
 
RecoveryTargetTimeLineGoal recoveryTargetTimeLineGoal
 
TimeLineID recoveryTargetTLIRequested
 
TimeLineID recoveryTargetTLI
 
int CheckPointSegments
 
bool StandbyModeRequested
 
bool StandbyMode
 
int XLogArchiveMode
 
PGDLLIMPORT int wal_level
 
CheckpointStatsData CheckpointStats
 

Macro Definition Documentation

◆ BACKUP_LABEL_FILE

#define BACKUP_LABEL_FILE   "backup_label"

◆ BACKUP_LABEL_OLD

#define BACKUP_LABEL_OLD   "backup_label.old"

Definition at line 390 of file xlog.h.

Referenced by CancelBackup(), and StartupXLOG().

◆ CHECKPOINT_CAUSE_TIME

#define CHECKPOINT_CAUSE_TIME   0x0100 /* Elapsed time */

Definition at line 233 of file xlog.h.

Referenced by CheckpointerMain(), and LogCheckpointStart().

◆ CHECKPOINT_CAUSE_XLOG

#define CHECKPOINT_CAUSE_XLOG   0x0080 /* XLOG consumption */

Definition at line 232 of file xlog.h.

Referenced by CheckpointerMain(), LogCheckpointStart(), XLogPageRead(), and XLogWrite().

◆ CHECKPOINT_END_OF_RECOVERY

#define CHECKPOINT_END_OF_RECOVERY
Value:
0x0002 /* Like shutdown checkpoint, but
* issued at end of WAL recovery */

Definition at line 222 of file xlog.h.

Referenced by BufferSync(), CheckpointerMain(), CreateCheckPoint(), LogCheckpointStart(), and StartupXLOG().

◆ CHECKPOINT_FLUSH_ALL

#define CHECKPOINT_FLUSH_ALL
Value:
0x0010 /* Flush all pages, including those
* belonging to unlogged tables */

Definition at line 226 of file xlog.h.

Referenced by BufferSync(), createdb(), LogCheckpointStart(), and movedb().

◆ CHECKPOINT_FORCE

#define CHECKPOINT_FORCE   0x0008 /* Force even if no activity */

◆ CHECKPOINT_IMMEDIATE

◆ CHECKPOINT_IS_SHUTDOWN

#define CHECKPOINT_IS_SHUTDOWN   0x0001 /* Checkpoint is for shutdown */

◆ CHECKPOINT_REQUESTED

#define CHECKPOINT_REQUESTED   0x0040 /* Checkpoint request has been made */

Definition at line 230 of file xlog.h.

Referenced by RequestCheckpoint().

◆ CHECKPOINT_WAIT

#define CHECKPOINT_WAIT   0x0020 /* Wait for completion */

◆ InHotStandby

◆ PROMOTE_SIGNAL_FILE

#define PROMOTE_SIGNAL_FILE   "promote"

Definition at line 396 of file xlog.h.

Referenced by CheckPromoteSignal(), pg_promote(), and RemovePromoteSignalFiles().

◆ RECOVERY_SIGNAL_FILE

#define RECOVERY_SIGNAL_FILE   "recovery.signal"

Definition at line 387 of file xlog.h.

Referenced by exitArchiveRecovery(), and readRecoverySignalFile().

◆ STANDBY_SIGNAL_FILE

#define STANDBY_SIGNAL_FILE   "standby.signal"

Definition at line 388 of file xlog.h.

Referenced by exitArchiveRecovery(), and readRecoverySignalFile().

◆ SYNC_METHOD_FDATASYNC

#define SYNC_METHOD_FDATASYNC   1

Definition at line 26 of file xlog.h.

Referenced by get_sync_bit(), and issue_xlog_fsync().

◆ SYNC_METHOD_FSYNC

#define SYNC_METHOD_FSYNC   0

Definition at line 25 of file xlog.h.

Referenced by get_sync_bit(), and issue_xlog_fsync().

◆ SYNC_METHOD_FSYNC_WRITETHROUGH

#define SYNC_METHOD_FSYNC_WRITETHROUGH   3

Definition at line 28 of file xlog.h.

Referenced by get_sync_bit(), issue_xlog_fsync(), and pg_fsync().

◆ SYNC_METHOD_OPEN

#define SYNC_METHOD_OPEN   2 /* for O_SYNC */

Definition at line 27 of file xlog.h.

Referenced by get_sync_bit(), issue_xlog_fsync(), and XLogWrite().

◆ SYNC_METHOD_OPEN_DSYNC

#define SYNC_METHOD_OPEN_DSYNC   4 /* for O_DSYNC */

Definition at line 29 of file xlog.h.

Referenced by get_sync_bit(), issue_xlog_fsync(), and XLogWrite().

◆ TABLESPACE_MAP

#define TABLESPACE_MAP   "tablespace_map"

◆ TABLESPACE_MAP_OLD

#define TABLESPACE_MAP_OLD   "tablespace_map.old"

Definition at line 393 of file xlog.h.

Referenced by CancelBackup(), and StartupXLOG().

◆ XLOG_INCLUDE_ORIGIN

#define XLOG_INCLUDE_ORIGIN   0x01 /* include the replication origin */

◆ XLOG_INCLUDE_XID

#define XLOG_INCLUDE_XID   0x04 /* include XID of top-level xact */

Definition at line 240 of file xlog.h.

Referenced by XLogRecordAssemble(), and XLogResetInsertion().

◆ XLOG_MARK_UNIMPORTANT

#define XLOG_MARK_UNIMPORTANT   0x02 /* record not important for durability */

◆ XLogArchiveCommandSet

#define XLogArchiveCommandSet ( )    (XLogArchiveCommand[0] != '\0')

Definition at line 185 of file xlog.h.

Referenced by pgarch_ArchiverCopyLoop(), and ShutdownXLOG().

◆ XLogArchivingActive

◆ XLogArchivingAlways

Definition at line 183 of file xlog.h.

Referenced by do_pg_stop_backup(), sigusr1_handler(), and XLogArchiveCheckDone().

◆ XLogHintBitIsNeeded

◆ XLogIsNeeded

◆ XLogLogicalInfoActive

◆ XLogStandbyInfoActive

Typedef Documentation

◆ ArchiveMode

typedef enum ArchiveMode ArchiveMode

◆ CheckpointStatsData

◆ RecoveryState

◆ SessionBackupState

◆ WALAvailability

◆ WalLevel

typedef enum WalLevel WalLevel

Enumeration Type Documentation

◆ ArchiveMode

Enumerator
ARCHIVE_MODE_OFF 
ARCHIVE_MODE_ON 
ARCHIVE_MODE_ALWAYS 

Definition at line 153 of file xlog.h.

154 {
155  ARCHIVE_MODE_OFF = 0, /* disabled */
156  ARCHIVE_MODE_ON, /* enabled while server is running normally */
157  ARCHIVE_MODE_ALWAYS /* enabled always (even during recovery) */
158 } ArchiveMode;
ArchiveMode
Definition: xlog.h:153

◆ HotStandbyState

Enumerator
STANDBY_DISABLED 
STANDBY_INITIALIZED 
STANDBY_SNAPSHOT_PENDING 
STANDBY_SNAPSHOT_READY 

Definition at line 64 of file xlog.h.

◆ RecoveryState

Enumerator
RECOVERY_STATE_CRASH 
RECOVERY_STATE_ARCHIVE 
RECOVERY_STATE_DONE 

Definition at line 170 of file xlog.h.

171 {
172  RECOVERY_STATE_CRASH = 0, /* crash recovery */
173  RECOVERY_STATE_ARCHIVE, /* archive recovery */
174  RECOVERY_STATE_DONE /* currently in production */
175 } RecoveryState;
RecoveryState
Definition: xlog.h:170

◆ RecoveryTargetTimeLineGoal

Enumerator
RECOVERY_TARGET_TIMELINE_CONTROLFILE 
RECOVERY_TARGET_TIMELINE_LATEST 
RECOVERY_TARGET_TIMELINE_NUMERIC 

Definition at line 93 of file xlog.h.

◆ RecoveryTargetType

Enumerator
RECOVERY_TARGET_UNSET 
RECOVERY_TARGET_XID 
RECOVERY_TARGET_TIME 
RECOVERY_TARGET_NAME 
RECOVERY_TARGET_LSN 
RECOVERY_TARGET_IMMEDIATE 

Definition at line 80 of file xlog.h.

◆ SessionBackupState

Enumerator
SESSION_BACKUP_NONE 
SESSION_BACKUP_EXCLUSIVE 
SESSION_BACKUP_NON_EXCLUSIVE 

Definition at line 369 of file xlog.h.

◆ WALAvailability

Enumerator
WALAVAIL_INVALID_LSN 
WALAVAIL_RESERVED 
WALAVAIL_EXTENDED 
WALAVAIL_UNRESERVED 
WALAVAIL_REMOVED 

Definition at line 271 of file xlog.h.

272 {
273  WALAVAIL_INVALID_LSN, /* parameter error */
274  WALAVAIL_RESERVED, /* WAL segment is within max_wal_size */
275  WALAVAIL_EXTENDED, /* WAL segment is reserved by a slot or
276  * wal_keep_size */
277  WALAVAIL_UNRESERVED, /* no longer reserved, but not removed yet */
278  WALAVAIL_REMOVED /* WAL segment has been removed */
WALAvailability
Definition: xlog.h:271

◆ WalLevel

enum WalLevel
Enumerator
WAL_LEVEL_MINIMAL 
WAL_LEVEL_REPLICA 
WAL_LEVEL_LOGICAL 

Definition at line 162 of file xlog.h.

Function Documentation

◆ assign_checkpoint_completion_target()

void assign_checkpoint_completion_target ( double  newval,
void *  extra 
)

Definition at line 2330 of file xlog.c.

References CalculateCheckpointSegments(), CheckPointCompletionTarget, and newval.

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

◆ assign_max_wal_size()

void assign_max_wal_size ( int  newval,
void *  extra 
)

Definition at line 2323 of file xlog.c.

References CalculateCheckpointSegments(), max_wal_size_mb, and newval.

2324 {
2327 }
static void CalculateCheckpointSegments(void)
Definition: xlog.c:2294
int max_wal_size_mb
Definition: xlog.c:90
#define newval

◆ BootStrapXLOG()

void BootStrapXLOG ( void  )

Definition at line 5196 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().

5197 {
5198  CheckPoint checkPoint;
5199  char *buffer;
5200  XLogPageHeader page;
5201  XLogLongPageHeader longpage;
5202  XLogRecord *record;
5203  char *recptr;
5204  bool use_existent;
5205  uint64 sysidentifier;
5206  struct timeval tv;
5207  pg_crc32c crc;
5208 
5209  /*
5210  * Select a hopefully-unique system identifier code for this installation.
5211  * We use the result of gettimeofday(), including the fractional seconds
5212  * field, as being about as unique as we can easily get. (Think not to
5213  * use random(), since it hasn't been seeded and there's no portable way
5214  * to seed it other than the system clock value...) The upper half of the
5215  * uint64 value is just the tv_sec part, while the lower half contains the
5216  * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
5217  * PID for a little extra uniqueness. A person knowing this encoding can
5218  * determine the initialization time of the installation, which could
5219  * perhaps be useful sometimes.
5220  */
5221  gettimeofday(&tv, NULL);
5222  sysidentifier = ((uint64) tv.tv_sec) << 32;
5223  sysidentifier |= ((uint64) tv.tv_usec) << 12;
5224  sysidentifier |= getpid() & 0xFFF;
5225 
5226  /* First timeline ID is always 1 */
5227  ThisTimeLineID = 1;
5228 
5229  /* page buffer must be aligned suitably for O_DIRECT */
5230  buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
5231  page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
5232  memset(page, 0, XLOG_BLCKSZ);
5233 
5234  /*
5235  * Set up information for the initial checkpoint record
5236  *
5237  * The initial checkpoint record is written to the beginning of the WAL
5238  * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not
5239  * used, so that we can use 0/0 to mean "before any valid WAL segment".
5240  */
5241  checkPoint.redo = wal_segment_size + SizeOfXLogLongPHD;
5242  checkPoint.ThisTimeLineID = ThisTimeLineID;
5243  checkPoint.PrevTimeLineID = ThisTimeLineID;
5244  checkPoint.fullPageWrites = fullPageWrites;
5245  checkPoint.nextXid =
5247  checkPoint.nextOid = FirstBootstrapObjectId;
5248  checkPoint.nextMulti = FirstMultiXactId;
5249  checkPoint.nextMultiOffset = 0;
5250  checkPoint.oldestXid = FirstNormalTransactionId;
5251  checkPoint.oldestXidDB = TemplateDbOid;
5252  checkPoint.oldestMulti = FirstMultiXactId;
5253  checkPoint.oldestMultiDB = TemplateDbOid;
5256  checkPoint.time = (pg_time_t) time(NULL);
5258 
5259  ShmemVariableCache->nextXid = checkPoint.nextXid;
5260  ShmemVariableCache->nextOid = checkPoint.nextOid;
5262  MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5263  AdvanceOldestClogXid(checkPoint.oldestXid);
5264  SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5265  SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5267 
5268  /* Set up the XLOG page header */
5269  page->xlp_magic = XLOG_PAGE_MAGIC;
5270  page->xlp_info = XLP_LONG_HEADER;
5271  page->xlp_tli = ThisTimeLineID;
5273  longpage = (XLogLongPageHeader) page;
5274  longpage->xlp_sysid = sysidentifier;
5275  longpage->xlp_seg_size = wal_segment_size;
5276  longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
5277 
5278  /* Insert the initial checkpoint record */
5279  recptr = ((char *) page + SizeOfXLogLongPHD);
5280  record = (XLogRecord *) recptr;
5281  record->xl_prev = 0;
5282  record->xl_xid = InvalidTransactionId;
5283  record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(checkPoint);
5285  record->xl_rmid = RM_XLOG_ID;
5286  recptr += SizeOfXLogRecord;
5287  /* fill the XLogRecordDataHeaderShort struct */
5288  *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
5289  *(recptr++) = sizeof(checkPoint);
5290  memcpy(recptr, &checkPoint, sizeof(checkPoint));
5291  recptr += sizeof(checkPoint);
5292  Assert(recptr - (char *) record == record->xl_tot_len);
5293 
5294  INIT_CRC32C(crc);
5295  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
5296  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
5297  FIN_CRC32C(crc);
5298  record->xl_crc = crc;
5299 
5300  /* Create first XLOG segment file */
5301  use_existent = false;
5302  openLogFile = XLogFileInit(1, &use_existent, false);
5303 
5304  /*
5305  * We needn't bother with Reserve/ReleaseExternalFD here, since we'll
5306  * close the file again in a moment.
5307  */
5308 
5309  /* Write the first page with the initial record */
5310  errno = 0;
5312  if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
5313  {
5314  /* if write didn't set errno, assume problem is no disk space */
5315  if (errno == 0)
5316  errno = ENOSPC;
5317  ereport(PANIC,
5319  errmsg("could not write bootstrap write-ahead log file: %m")));
5320  }
5322 
5324  if (pg_fsync(openLogFile) != 0)
5325  ereport(PANIC,
5327  errmsg("could not fsync bootstrap write-ahead log file: %m")));
5329 
5330  if (close(openLogFile) != 0)
5331  ereport(PANIC,
5333  errmsg("could not close bootstrap write-ahead log file: %m")));
5334 
5335  openLogFile = -1;
5336 
5337  /* Now create pg_control */
5338  InitControlFile(sysidentifier);
5339  ControlFile->time = checkPoint.time;
5340  ControlFile->checkPoint = checkPoint.redo;
5341  ControlFile->checkPointCopy = checkPoint;
5342 
5343  /* some additional ControlFile fields are set in WriteControlFile() */
5344  WriteControlFile();
5345 
5346  /* Bootstrap the commit log, too */
5347  BootStrapCLOG();
5351 
5352  pfree(buffer);
5353 
5354  /*
5355  * Force control file to be read - in contrast to normal processing we'd
5356  * otherwise never run the checks and GUC related initializations therein.
5357  */
5358  ReadControlFile();
5359 }
static void WriteControlFile(void)
Definition: xlog.c:4613
#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:117
pg_time_t time
Definition: pg_control.h:128
void SetCommitTsLimit(TransactionId oldestXact, TransactionId newestXact)
Definition: commit_ts.c:892
uint32 oidCount
Definition: transam.h:208
#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:3254
void BootStrapMultiXact(void)
Definition: multixact.c:1889
MultiXactId oldestMulti
Definition: pg_control.h:49
TimeLineID PrevTimeLineID
Definition: pg_control.h:40
static void InitControlFile(uint64 sysidentifier)
Definition: xlog.c:4578
RmgrId xl_rmid
Definition: xlogrecord.h:47
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:54
CheckPoint checkPointCopy
Definition: pg_control.h:131
TransactionId oldestXid
Definition: pg_control.h:47
FullTransactionId nextXid
Definition: transam.h:213
pg_time_t time
Definition: pg_control.h:51
#define PANIC
Definition: elog.h:53
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:71
bool fullPageWrites
Definition: xlog.c:98
void BootStrapSUBTRANS(void)
Definition: subtrans.c:210
MultiXactOffset nextMultiOffset
Definition: pg_control.h:46
void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid)
Definition: varsup.c:328
TransactionId oldestCommitTsXid
Definition: pg_control.h:52
void pfree(void *pointer)
Definition: mcxt.c:1057
#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:4704
#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:189
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1460
#define FirstMultiXactId
Definition: multixact.h:25
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:798
static ControlFileData * ControlFile
Definition: xlog.c:737
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2208
TimeLineID ThisTimeLineID
Definition: xlog.c:192
Oid nextOid
Definition: pg_control.h:44
#define ereport(elevel,...)
Definition: elog.h:144
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:692
bool fullPageWrites
Definition: pg_control.h:42
void BootStrapCLOG(void)
Definition: clog.c:706
#define Assert(condition)
Definition: c.h:746
#define XLP_LONG_HEADER
Definition: xlog_internal.h:76
Oid oldestXidDB
Definition: pg_control.h:48
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1436
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:345
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:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
int pg_fsync(int fd)
Definition: fd.c:346
#define close(a)
Definition: win32.h:12
void BootStrapCommitTs(void)
Definition: commit_ts.c:585
#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:669
FullTransactionId nextXid
Definition: pg_control.h:43
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:69
void MultiXactSetNextMXact(MultiXactId nextMulti, MultiXactOffset nextMultiOffset)
Definition: multixact.c:2174

◆ CalculateMaxmumSafeLSN()

XLogRecPtr CalculateMaxmumSafeLSN ( void  )

◆ CheckPromoteSignal()

bool CheckPromoteSignal ( void  )

Definition at line 12660 of file xlog.c.

References PROMOTE_SIGNAL_FILE, and stat.

Referenced by CheckForStandbyTrigger(), and sigusr1_handler().

12661 {
12662  struct stat stat_buf;
12663 
12664  if (stat(PROMOTE_SIGNAL_FILE, &stat_buf) == 0)
12665  return true;
12666 
12667  return false;
12668 }
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:396
struct stat stat_buf
Definition: pg_standby.c:100
#define stat
Definition: win32_port.h:275

◆ CheckXLogRemoved()

void CheckXLogRemoved ( XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 3928 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().

3929 {
3930  int save_errno = errno;
3931  XLogSegNo lastRemovedSegNo;
3932 
3934  lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3936 
3937  if (segno <= lastRemovedSegNo)
3938  {
3939  char filename[MAXFNAMELEN];
3940 
3941  XLogFileName(filename, tli, segno, wal_segment_size);
3942  errno = save_errno;
3943  ereport(ERROR,
3945  errmsg("requested WAL segment %s has already been removed",
3946  filename)));
3947  }
3948  errno = save_errno;
3949 }
int wal_segment_size
Definition: xlog.c:117
slock_t info_lck
Definition: xlog.c:726
XLogSegNo lastRemovedSegNo
Definition: xlog.c:609
#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:729
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ CreateCheckPoint()

void CreateCheckPoint ( int  flags)

Definition at line 8704 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(), GetOldestTransactionIdConsideredRunning(), 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, 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().

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

◆ CreateRestartPoint()

bool CreateRestartPoint ( int  flags)

Definition at line 9257 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(), GetOldestTransactionIdConsideredRunning(), 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(), 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().

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

◆ DataChecksumsEnabled()

bool DataChecksumsEnabled ( void  )

Definition at line 4933 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().

4934 {
4935  Assert(ControlFile != NULL);
4936  return (ControlFile->data_checksum_version > 0);
4937 }
uint32 data_checksum_version
Definition: pg_control.h:220
static ControlFileData * ControlFile
Definition: xlog.c:737
#define Assert(condition)
Definition: c.h:746

◆ do_pg_abort_backup()

void do_pg_abort_backup ( int  code,
Datum  arg 
)

Definition at line 11452 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().

11453 {
11454  bool emit_warning = DatumGetBool(arg);
11455 
11456  /*
11457  * Quick exit if session is not keeping around a non-exclusive backup
11458  * already started.
11459  */
11461  return;
11462 
11466 
11469  {
11470  XLogCtl->Insert.forcePageWrites = false;
11471  }
11473 
11474  if (emit_warning)
11475  ereport(WARNING,
11476  (errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
11477 }
static void WALInsertLockRelease(void)
Definition: xlog.c:1728
static SessionBackupState sessionBackupState
Definition: xlog.c:534
XLogCtlInsert Insert
Definition: xlog.c:600
bool forcePageWrites
Definition: xlog.c:574
#define DatumGetBool(X)
Definition: postgres.h:393
#define WARNING
Definition: elog.h:40
int nonExclusiveBackups
Definition: xlog.c:586
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:585
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:746
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1699
static XLogCtlData * XLogCtl
Definition: xlog.c:729
int errmsg(const char *fmt,...)
Definition: elog.c:821
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 10521 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().

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

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

◆ get_backup_status()

SessionBackupState get_backup_status ( void  )

Definition at line 11018 of file xlog.c.

References sessionBackupState.

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

11019 {
11020  return sessionBackupState;
11021 }
static SessionBackupState sessionBackupState
Definition: xlog.c:534

◆ GetCurrentChunkReplayStartTime()

TimestampTz GetCurrentChunkReplayStartTime ( void  )

Definition at line 6206 of file xlog.c.

References XLogCtlData::currentChunkStartTime, XLogCtlData::info_lck, SpinLockAcquire, and SpinLockRelease.

Referenced by GetReplicationApplyDelay().

6207 {
6208  TimestampTz xtime;
6209 
6211  xtime = XLogCtl->currentChunkStartTime;
6213 
6214  return xtime;
6215 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729
TimestampTz currentChunkStartTime
Definition: xlog.c:716

◆ GetFakeLSNForUnloggedRel()

XLogRecPtr GetFakeLSNForUnloggedRel ( void  )

Definition at line 4949 of file xlog.c.

References SpinLockAcquire, SpinLockRelease, XLogCtlData::ulsn_lck, and XLogCtlData::unloggedLSN.

Referenced by gistGetFakeLSN().

4950 {
4951  XLogRecPtr nextUnloggedLSN;
4952 
4953  /* increment the unloggedLSN counter, need SpinLock */
4955  nextUnloggedLSN = XLogCtl->unloggedLSN++;
4957 
4958  return nextUnloggedLSN;
4959 }
XLogRecPtr unloggedLSN
Definition: xlog.c:612
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:729
slock_t ulsn_lck
Definition: xlog.c:613

◆ GetFlushRecPtr()

XLogRecPtr GetFlushRecPtr ( void  )

Definition at line 8428 of file xlog.c.

References XLogwrtResult::Flush, XLogCtlData::info_lck, XLogCtlData::LogwrtResult, SpinLockAcquire, and SpinLockRelease.

Referenced by get_flush_position(), IdentifySystem(), pg_current_wal_flush_lsn(), pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), read_local_xlog_page(), StartReplication(), WalSndWaitForWal(), XLogSendLogical(), and XLogSendPhysical().

8429 {
8433 
8434  return LogwrtResult.Flush;
8435 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:775
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:623
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729
XLogRecPtr Flush
Definition: xlog.c:448

◆ GetFullPageWriteInfo()

void GetFullPageWriteInfo ( XLogRecPtr RedoRecPtr_p,
bool doPageWrites_p 
)

Definition at line 8397 of file xlog.c.

References doPageWrites, and RedoRecPtr.

Referenced by XLogCheckBufferNeedsBackup(), and XLogInsert().

8398 {
8399  *RedoRecPtr_p = RedoRecPtr;
8400  *doPageWrites_p = doPageWrites;
8401 }
static bool doPageWrites
Definition: xlog.c:381
static XLogRecPtr RedoRecPtr
Definition: xlog.c:374

◆ GetInsertRecPtr()

XLogRecPtr GetInsertRecPtr ( void  )

Definition at line 8412 of file xlog.c.

References XLogCtlData::info_lck, XLogCtlData::LogwrtRqst, SpinLockAcquire, SpinLockRelease, and XLogwrtRqst::Write.

Referenced by CheckpointerMain(), gistvacuumscan(), and IsCheckpointOnSchedule().

8413 {
8414  XLogRecPtr recptr;
8415 
8417  recptr = XLogCtl->LogwrtRqst.Write;
8419 
8420  return recptr;
8421 }
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr Write
Definition: xlog.c:441
XLogwrtRqst LogwrtRqst
Definition: xlog.c:603
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ GetLastImportantRecPtr()

XLogRecPtr GetLastImportantRecPtr ( void  )

Definition at line 8446 of file xlog.c.

References i, InvalidXLogRecPtr, WALInsertLockPadded::l, WALInsertLock::lastImportantAt, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and NUM_XLOGINSERT_LOCKS.

Referenced by BackgroundWriterMain(), CheckArchiveTimeout(), and CreateCheckPoint().

8447 {
8449  int i;
8450 
8451  for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
8452  {
8453  XLogRecPtr last_important;
8454 
8455  /*
8456  * Need to take a lock to prevent torn reads of the LSN, which are
8457  * possible on some of the supported platforms. WAL insert locks only
8458  * support exclusive mode, so we have to use that.
8459  */
8461  last_important = WALInsertLocks[i].l.lastImportantAt;
8462  LWLockRelease(&WALInsertLocks[i].l.lock);
8463 
8464  if (res < last_important)
8465  res = last_important;
8466  }
8467 
8468  return res;
8469 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr lastImportantAt
Definition: xlog.c:491
#define NUM_XLOGINSERT_LOCKS
Definition: xlog.c:124
WALInsertLock l
Definition: xlog.c:503
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
int i
static WALInsertLockPadded * WALInsertLocks
Definition: xlog.c:732

◆ GetLatestXTime()

TimestampTz GetLatestXTime ( void  )

Definition at line 6176 of file xlog.c.

References XLogCtlData::info_lck, XLogCtlData::recoveryLastXTime, SpinLockAcquire, and SpinLockRelease.

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

6177 {
6178  TimestampTz xtime;
6179 
6181  xtime = XLogCtl->recoveryLastXTime;
6183 
6184  return xtime;
6185 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
TimestampTz recoveryLastXTime
Definition: xlog.c:710
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ GetMockAuthenticationNonce()

char* GetMockAuthenticationNonce ( void  )

Definition at line 4923 of file xlog.c.

References Assert, and ControlFileData::mock_authentication_nonce.

Referenced by scram_mock_salt().

4924 {
4925  Assert(ControlFile != NULL);
4927 }
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:227
static ControlFileData * ControlFile
Definition: xlog.c:737
#define Assert(condition)
Definition: c.h:746

◆ GetRecoveryState()

RecoveryState GetRecoveryState ( void  )

Definition at line 8129 of file xlog.c.

References XLogCtlData::info_lck, XLogCtlData::SharedRecoveryState, SpinLockAcquire, and SpinLockRelease.

Referenced by XLogArchiveCheckDone().

8130 {
8131  RecoveryState retval;
8132 
8134  retval = XLogCtl->SharedRecoveryState;
8136 
8137  return retval;
8138 }
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
RecoveryState SharedRecoveryState
Definition: xlog.c:659
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729
RecoveryState
Definition: xlog.h:170

◆ GetRedoRecPtr()

XLogRecPtr GetRedoRecPtr ( void  )

Definition at line 8368 of file xlog.c.

References XLogCtlData::info_lck, RedoRecPtr, XLogCtlData::RedoRecPtr, SpinLockAcquire, and SpinLockRelease.

Referenced by CheckPointLogicalRewriteHeap(), CheckPointSnapBuild(), InitXLOGAccess(), nextval_internal(), ReplicationSlotReserveWal(), XLogPageRead(), XLogSaveBufferForHint(), and XLogWrite().

8369 {
8370  XLogRecPtr ptr;
8371 
8372  /*
8373  * The possibly not up-to-date copy in XlogCtl is enough. Even if we
8374  * grabbed a WAL insertion lock to read the authoritative value in
8375  * Insert->RedoRecPtr, someone might update it just after we've released
8376  * the lock.
8377  */
8379  ptr = XLogCtl->RedoRecPtr;
8381 
8382  if (RedoRecPtr < ptr)
8383  RedoRecPtr = ptr;
8384 
8385  return RedoRecPtr;
8386 }
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
static XLogRecPtr RedoRecPtr
Definition: xlog.c:374
XLogRecPtr RedoRecPtr
Definition: xlog.c:604
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ GetSystemIdentifier()

uint64 GetSystemIdentifier ( void  )

Definition at line 4913 of file xlog.c.

References Assert, and ControlFileData::system_identifier.

Referenced by IdentifySystem(), and WalReceiverMain().

4914 {
4915  Assert(ControlFile != NULL);
4917 }
uint64 system_identifier
Definition: pg_control.h:106
static ControlFileData * ControlFile
Definition: xlog.c:737
#define Assert(condition)
Definition: c.h:746

◆ GetWALAvailability()

WALAvailability GetWALAvailability ( XLogRecPtr  targetLSN)

Definition at line 9522 of file xlog.c.

References ConvertToXSegs, GetXLogWriteRecPtr(), KeepLogSeg(), max_wal_size_mb, wal_segment_size, WALAVAIL_EXTENDED, WALAVAIL_INVALID_LSN, WALAVAIL_REMOVED, WALAVAIL_RESERVED, WALAVAIL_UNRESERVED, XLByteToSeg, XLogGetLastRemovedSegno(), and XLogRecPtrIsInvalid.

Referenced by pg_get_replication_slots().

9523 {
9524  XLogRecPtr currpos; /* current write LSN */
9525  XLogSegNo currSeg; /* segid of currpos */
9526  XLogSegNo targetSeg; /* segid of targetLSN */
9527  XLogSegNo oldestSeg; /* actual oldest segid */
9528  XLogSegNo oldestSegMaxWalSize; /* oldest segid kept by max_wal_size */
9529  XLogSegNo oldestSlotSeg; /* oldest segid kept by slot */
9530  uint64 keepSegs;
9531 
9532  /*
9533  * slot does not reserve WAL. Either deactivated, or has never been active
9534  */
9535  if (XLogRecPtrIsInvalid(targetLSN))
9536  return WALAVAIL_INVALID_LSN;
9537 
9538  /*
9539  * Calculate the oldest segment currently reserved by all slots,
9540  * considering wal_keep_size and max_slot_wal_keep_size. Initialize
9541  * oldestSlotSeg to the current segment.
9542  */
9543  currpos = GetXLogWriteRecPtr();
9544  XLByteToSeg(currpos, oldestSlotSeg, wal_segment_size);
9545  KeepLogSeg(currpos, &oldestSlotSeg);
9546 
9547  /*
9548  * Find the oldest extant segment file. We get 1 until checkpoint removes
9549  * the first WAL segment file since startup, which causes the status being
9550  * wrong under certain abnormal conditions but that doesn't actually harm.
9551  */
9552  oldestSeg = XLogGetLastRemovedSegno() + 1;
9553 
9554  /* calculate oldest segment by max_wal_size */
9555  XLByteToSeg(currpos, currSeg, wal_segment_size);
9557 
9558  if (currSeg > keepSegs)
9559  oldestSegMaxWalSize = currSeg - keepSegs;
9560  else
9561  oldestSegMaxWalSize = 1;
9562 
9563  /* the segment we care about */
9564  XLByteToSeg(targetLSN, targetSeg, wal_segment_size);
9565 
9566  /*
9567  * No point in returning reserved or extended status values if the
9568  * targetSeg is known to be lost.
9569  */
9570  if (targetSeg >= oldestSlotSeg)
9571  {
9572  /* show "reserved" when targetSeg is within max_wal_size */
9573  if (targetSeg >= oldestSegMaxWalSize)
9574  return WALAVAIL_RESERVED;
9575 
9576  /* being retained by slots exceeding max_wal_size */
9577  return WALAVAIL_EXTENDED;
9578  }
9579 
9580  /* WAL segments are no longer retained but haven't been removed yet */
9581  if (targetSeg >= oldestSeg)
9582  return WALAVAIL_UNRESERVED;
9583 
9584  /* Definitely lost */
9585  return WALAVAIL_REMOVED;
9586 }
#define ConvertToXSegs(x, segsize)
Definition: xlog.c:766
int wal_segment_size
Definition: xlog.c:117
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3959
int max_wal_size_mb
Definition: xlog.c:90
uint64 XLogSegNo
Definition: xlogdefs.h:41
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:9600
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11535
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ GetXLogInsertRecPtr()

XLogRecPtr GetXLogInsertRecPtr ( void  )

Definition at line 11519 of file xlog.c.

References XLogCtlInsert::CurrBytePos, Insert(), XLogCtlData::Insert, XLogCtlInsert::insertpos_lck, SpinLockAcquire, SpinLockRelease, and XLogBytePosToRecPtr().

Referenced by GetSnapshotDataInitOldSnapshot(), gistGetFakeLSN(), logical_begin_heap_rewrite(), pg_current_wal_insert_lsn(), and ReplicationSlotReserveWal().

11520 {
11522  uint64 current_bytepos;
11523 
11524  SpinLockAcquire(&Insert->insertpos_lck);
11525  current_bytepos = Insert->CurrBytePos;
11526  SpinLockRelease(&Insert->insertpos_lck);
11527 
11528  return XLogBytePosToRecPtr(current_bytepos);
11529 }
slock_t insertpos_lck
Definition: xlog.c:541
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1997
XLogCtlInsert Insert
Definition: xlog.c:600
#define SpinLockAcquire(lock)
Definition: spin.h:62
uint64 CurrBytePos
Definition: xlog.c:550
static void Insert(File file)
Definition: fd.c:1175
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ GetXLogReceiptTime()

void GetXLogReceiptTime ( TimestampTz rtime,
bool fromStream 
)

Definition at line 6222 of file xlog.c.

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

Referenced by GetStandbyLimitTime().

6223 {
6224  /*
6225  * This must be executed in the startup process, since we don't export the
6226  * relevant state to shared memory.
6227  */
6228  Assert(InRecovery);
6229 
6230  *rtime = XLogReceiptTime;
6231  *fromStream = (XLogReceiptSource == XLOG_FROM_STREAM);
6232 }
static XLogSource XLogReceiptSource
Definition: xlog.c:848
bool InRecovery
Definition: xlog.c:205
static TimestampTz XLogReceiptTime
Definition: xlog.c:847
#define Assert(condition)
Definition: c.h:746

◆ GetXLogReplayRecPtr()

XLogRecPtr GetXLogReplayRecPtr ( TimeLineID replayTLI)

Definition at line 11500 of file xlog.c.

References XLogCtlData::info_lck, XLogCtlData::lastReplayedEndRecPtr, XLogCtlData::lastReplayedTLI, SpinLockAcquire, and SpinLockRelease.

Referenced by CheckpointerMain(), CreateRestartPoint(), GetReplicationApplyDelay(), GetStandbyFlushRecPtr(), IsCheckpointOnSchedule(), pg_last_wal_replay_lsn(), pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), read_local_xlog_page(), WalReceiverMain(), WalSndWaitForWal(), and XLogWalRcvSendReply().

11501 {
11502  XLogRecPtr recptr;
11503  TimeLineID tli;
11504 
11506  recptr = XLogCtl->lastReplayedEndRecPtr;
11507  tli = XLogCtl->lastReplayedTLI;
11509 
11510  if (replayTLI)
11511  *replayTLI = tli;
11512  return recptr;
11513 }
uint32 TimeLineID
Definition: xlogdefs.h:52
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:729
TimeLineID lastReplayedTLI
Definition: xlog.c:706
XLogRecPtr lastReplayedEndRecPtr
Definition: xlog.c:705

◆ GetXLogWriteRecPtr()

XLogRecPtr GetXLogWriteRecPtr ( void  )

Definition at line 11535 of file xlog.c.

References XLogCtlData::info_lck, XLogCtlData::LogwrtResult, SpinLockAcquire, SpinLockRelease, and XLogwrtResult::Write.

Referenced by GetWALAvailability(), pg_attribute_noreturn(), pg_current_wal_lsn(), and pg_get_replication_slots().

11536 {
11540 
11541  return LogwrtResult.Write;
11542 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:775
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:623
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729
XLogRecPtr Write
Definition: xlog.c:447

◆ HotStandbyActive()

bool HotStandbyActive ( void  )

Definition at line 8150 of file xlog.c.

References XLogCtlData::info_lck, LocalHotStandbyActive, XLogCtlData::SharedHotStandbyActive, SpinLockAcquire, and SpinLockRelease.

Referenced by XLogWalRcvSendHSFeedback().

8151 {
8152  /*
8153  * We check shared state each time only until Hot Standby is active. We
8154  * can't de-activate Hot Standby, so there's no need to keep checking
8155  * after the shared variable has once been seen true.
8156  */
8158  return true;
8159  else
8160  {
8161  /* spinlock is essential on machines with weak memory ordering! */
8165 
8166  return LocalHotStandbyActive;
8167  }
8168 }
bool SharedHotStandbyActive
Definition: xlog.c:665
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
static bool LocalHotStandbyActive
Definition: xlog.c:235
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ HotStandbyActiveInReplay()

bool HotStandbyActiveInReplay ( void  )

Definition at line 8175 of file xlog.c.

References AmStartupProcess, Assert, IsPostmasterEnvironment, and LocalHotStandbyActive.

8176 {
8178  return LocalHotStandbyActive;
8179 }
#define AmStartupProcess()
Definition: miscadmin.h:431
bool IsPostmasterEnvironment
Definition: globals.c:108
static bool LocalHotStandbyActive
Definition: xlog.c:235
#define Assert(condition)
Definition: c.h:746

◆ InitXLOGAccess()

void InitXLOGAccess ( void  )

Definition at line 8342 of file xlog.c.

References Assert, doPageWrites, XLogCtlInsert::forcePageWrites, XLogCtlInsert::fullPageWrites, GetRedoRecPtr(), InitXLogInsert(), Insert(), XLogCtlData::Insert, IsBootstrapProcessingMode, ThisTimeLineID, XLogCtlData::ThisTimeLineID, wal_segment_size, and ControlFileData::xlog_seg_size.

Referenced by AuxiliaryProcessMain(), LocalSetXLogInsertAllowed(), and RecoveryInProgress().

8343 {
8345 
8346  /* ThisTimeLineID doesn't change so we need no lock to copy it */
8349 
8350  /* set wal_segment_size */
8352 
8353  /* Use GetRedoRecPtr to copy the RedoRecPtr safely */
8354  (void) GetRedoRecPtr();
8355  /* Also update our copy of doPageWrites. */
8356  doPageWrites = (Insert->fullPageWrites || Insert->forcePageWrites);
8357 
8358  /* Also initialize the working areas for constructing WAL records */
8359  InitXLogInsert();
8360 }
int wal_segment_size
Definition: xlog.c:117
void InitXLogInsert(void)
Definition: xloginsert.c:1197
TimeLineID ThisTimeLineID
Definition: xlog.c:652
XLogCtlInsert Insert
Definition: xlog.c:600
bool fullPageWrites
Definition: xlog.c:575
uint32 xlog_seg_size
Definition: pg_control.h:209
static bool doPageWrites
Definition: xlog.c:381
bool forcePageWrites
Definition: xlog.c:574
static void Insert(File file)
Definition: fd.c:1175
static ControlFileData * ControlFile
Definition: xlog.c:737
TimeLineID ThisTimeLineID
Definition: xlog.c:192
#define Assert(condition)
Definition: c.h:746
static XLogCtlData * XLogCtl
Definition: xlog.c:729
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8368
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:393

◆ issue_xlog_fsync()

void issue_xlog_fsync ( int  fd,
XLogSegNo  segno 
)

Definition at line 10428 of file xlog.c.

References _, elog, ereport, errcode_for_file_access(), errmsg(), MAXFNAMELEN, PANIC, pg_fdatasync(), pg_fsync_no_writethrough(), pg_fsync_writethrough(), pgstat_report_wait_end(), pgstat_report_wait_start(), sync_method, SYNC_METHOD_FDATASYNC, SYNC_METHOD_FSYNC, SYNC_METHOD_FSYNC_WRITETHROUGH, SYNC_METHOD_OPEN, SYNC_METHOD_OPEN_DSYNC, ThisTimeLineID, WAIT_EVENT_WAL_SYNC, wal_segment_size, and XLogFileName.

Referenced by XLogWalRcvFlush(), and XLogWrite().

10429 {
10430  char *msg = NULL;
10431 
10433  switch (sync_method)
10434  {
10435  case SYNC_METHOD_FSYNC:
10436  if (pg_fsync_no_writethrough(fd) != 0)
10437  msg = _("could not fsync file \"%s\": %m");
10438  break;
10439 #ifdef HAVE_FSYNC_WRITETHROUGH
10441  if (pg_fsync_writethrough(fd) != 0)
10442  msg = _("could not fsync write-through file \"%s\": %m");
10443  break;
10444 #endif
10445 #ifdef HAVE_FDATASYNC
10446  case SYNC_METHOD_FDATASYNC:
10447  if (pg_fdatasync(fd) != 0)
10448  msg = _("could not fdatasync file \"%s\": %m");
10449  break;
10450 #endif
10451  case SYNC_METHOD_OPEN:
10453  /* write synced it already */
10454  break;
10455  default:
10456  elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
10457  break;
10458  }
10459 
10460  /* PANIC if failed to fsync */
10461  if (msg)
10462  {
10463  char xlogfname[MAXFNAMELEN];
10464  int save_errno = errno;
10465 
10466  XLogFileName(xlogfname, ThisTimeLineID, segno,
10468  errno = save_errno;
10469  ereport(PANIC,
10471  errmsg(msg, xlogfname)));
10472  }
10473 
10475 }
int pg_fdatasync(int fd)
Definition: fd.c:436
int wal_segment_size
Definition: xlog.c:117
#define SYNC_METHOD_FSYNC_WRITETHROUGH
Definition: xlog.h:28
int pg_fsync_writethrough(int fd)
Definition: fd.c:413
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:401
#define PANIC
Definition: elog.h:53
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define SYNC_METHOD_OPEN_DSYNC
Definition: xlog.h:29
int errcode_for_file_access(void)
Definition: elog.c:633
#define SYNC_METHOD_FSYNC
Definition: xlog.h:25
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1460
#define SYNC_METHOD_OPEN
Definition: xlog.h:27
#define MAXFNAMELEN
TimeLineID ThisTimeLineID
Definition: xlog.c:192
#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:1436
int sync_method
Definition: xlog.c:106
#define SYNC_METHOD_FDATASYNC
Definition: xlog.h:26
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
#define _(x)
Definition: elog.c:88

◆ LocalProcessControlFile()

void LocalProcessControlFile ( bool  reset)

Definition at line 5033 of file xlog.c.

References Assert, palloc(), and ReadControlFile().

Referenced by BackendRun(), PostgresMain(), PostmasterMain(), and PostmasterStateMachine().

5034 {
5035  Assert(reset || ControlFile == NULL);
5036  ControlFile = palloc(sizeof(ControlFileData));
5037  ReadControlFile();
5038 }
static void ReadControlFile(void)
Definition: xlog.c:4704
static ControlFileData * ControlFile
Definition: xlog.c:737
#define Assert(condition)
Definition: c.h:746
void * palloc(Size size)
Definition: mcxt.c:950

◆ PromoteIsTriggered()

bool PromoteIsTriggered ( void  )

Definition at line 12579 of file xlog.c.

References XLogCtlData::info_lck, LocalPromoteIsTriggered, XLogCtlData::SharedPromoteIsTriggered, SpinLockAcquire, and SpinLockRelease.

Referenced by pg_wal_replay_pause(), and pg_wal_replay_resume().

12580 {
12581  /*
12582  * We check shared state each time only until a standby promotion is
12583  * triggered. We can't trigger a promotion again, so there's no need to
12584  * keep checking after the shared variable has once been seen true.
12585  */
12587  return true;
12588 
12592 
12593  return LocalPromoteIsTriggered;
12594 }
slock_t info_lck
Definition: xlog.c:726
static bool LocalPromoteIsTriggered
Definition: xlog.c:241
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:729
bool SharedPromoteIsTriggered
Definition: xlog.c:671

◆ RecoveryInProgress()

bool RecoveryInProgress ( void  )

Definition at line 8076 of file xlog.c.

References InitXLOGAccess(), LocalRecoveryInProgress, pg_memory_barrier, RECOVERY_STATE_DONE, XLogCtlData::SharedRecoveryState, and XLogCtl.

Referenced by BackgroundWriterMain(), brin_desummarize_range(), brin_summarize_range(), btree_index_mainfork_expected(), check_transaction_read_only(), check_XactIsoLevel(), CheckArchiveTimeout(), CheckLogicalDecodingRequirements(), CheckpointerMain(), ComputeXidHorizons(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateRestartPoint(), do_pg_start_backup(), do_pg_stop_backup(), error_commit_ts_disabled(), get_relation_info(), GetNewMultiXactId(), GetNewObjectId(), GetNewTransactionId(), GetOldestActiveTransactionId(), GetOldestSafeDecodingTransactionId(), GetRunningTransactionData(), GetSerializableTransactionSnapshot(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), gin_clean_pending_list(), GlobalVisTestFor(), heap_force_common(), heap_page_prune_opt(), IdentifySystem(), InitPostgres(), InitTempTableNamespace(), IsCheckpointOnSchedule(), LockAcquireExtended(), MaintainLatestCompletedXid(), MarkBufferDirtyHint(), perform_base_backup(), pg_create_restore_point(), pg_current_wal_flush_lsn(), pg_current_wal_insert_lsn(), pg_current_wal_lsn(), pg_is_in_recovery(), pg_is_wal_replay_paused(), pg_logical_slot_get_changes_guts(), pg_promote(), pg_replication_slot_advance(), pg_switch_wal(), pg_wal_replay_pause(), pg_wal_replay_resume(), pg_walfile_name(), pg_walfile_name_offset(), PrepareRedoAdd(), PrepareRedoRemove(), PreventCommandDuringRecovery(), ProcSendSignal(), ProcSleep(), read_local_xlog_page(), ReplicationSlotReserveWal(), replorigin_check_prerequisites(), sendDir(), SerialSetActiveSerXmin(), ShutdownXLOG(), SnapBuildWaitSnapshot(), standard_ProcessUtility(), StartLogicalReplication(), StartTransaction(), TransactionIdIsInProgress(), TruncateMultiXact(), UpdateFullPageWrites(), WalReceiverMain(), WalSndWaitForWal(), XLogBackgroundFlush(), XLogInsertAllowed(), XLogNeedsFlush(), and XLogSendPhysical().

8077 {
8078  /*
8079  * We check shared state each time only until we leave recovery mode. We
8080  * can't re-enter recovery, so there's no need to keep checking after the
8081  * shared variable has once been seen false.
8082  */
8084  return false;
8085  else
8086  {
8087  /*
8088  * use volatile pointer to make sure we make a fresh read of the
8089  * shared variable.
8090  */
8091  volatile XLogCtlData *xlogctl = XLogCtl;
8092 
8094 
8095  /*
8096  * Initialize TimeLineID and RedoRecPtr when we discover that recovery
8097  * is finished. InitPostgres() relies upon this behaviour to ensure
8098  * that InitXLOGAccess() is called at backend startup. (If you change
8099  * this, see also LocalSetXLogInsertAllowed.)
8100  */
8102  {
8103  /*
8104  * If we just exited recovery, make sure we read TimeLineID and
8105  * RedoRecPtr after SharedRecoveryState (for machines with weak
8106  * memory ordering).
8107  */
8109  InitXLOGAccess();
8110  }
8111 
8112  /*
8113  * Note: We don't need a memory barrier when we're still in recovery.
8114  * We might exit recovery immediately after return, so the caller
8115  * can't rely on 'true' meaning that we're still in recovery anyway.
8116  */
8117 
8118  return LocalRecoveryInProgress;
8119  }
8120 }
void InitXLOGAccess(void)
Definition: xlog.c:8342
RecoveryState SharedRecoveryState
Definition: xlog.c:659
#define pg_memory_barrier()
Definition: atomics.h:145
static XLogCtlData * XLogCtl
Definition: xlog.c:729
static bool LocalRecoveryInProgress
Definition: xlog.c:229

◆ RecoveryIsPaused()

bool RecoveryIsPaused ( void  )

Definition at line 6040 of file xlog.c.

References XLogCtlData::info_lck, XLogCtlData::recoveryPause, SpinLockAcquire, and SpinLockRelease.

Referenced by pg_is_wal_replay_paused(), and recoveryPausesHere().

6041 {
6042  bool recoveryPause;
6043 
6045  recoveryPause = XLogCtl->recoveryPause;
6047 
6048  return recoveryPause;
6049 }
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:718
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ register_persistent_abort_backup_handler()

void register_persistent_abort_backup_handler ( void  )

Definition at line 11484 of file xlog.c.

References before_shmem_exit(), DatumGetBool, and do_pg_abort_backup().

Referenced by pg_start_backup().

11485 {
11486  static bool already_done = false;
11487 
11488  if (already_done)
11489  return;
11491  already_done = true;
11492 }
void do_pg_abort_backup(int code, Datum arg)
Definition: xlog.c:11452
#define DatumGetBool(X)
Definition: postgres.h:393
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333

◆ RemovePromoteSignalFiles()

void RemovePromoteSignalFiles ( void  )

Definition at line 12651 of file xlog.c.

References PROMOTE_SIGNAL_FILE.

Referenced by CheckForStandbyTrigger(), and PostmasterMain().

12652 {
12653  unlink(PROMOTE_SIGNAL_FILE);
12654 }
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:396

◆ SetRecoveryPause()

void SetRecoveryPause ( bool  recoveryPause)

Definition at line 6052 of file xlog.c.

References XLogCtlData::info_lck, XLogCtlData::recoveryPause, SpinLockAcquire, and SpinLockRelease.

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

6053 {
6055  XLogCtl->recoveryPause = recoveryPause;
6057 }
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:718
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ SetWalWriterSleeping()

void SetWalWriterSleeping ( bool  sleeping)

Definition at line 12684 of file xlog.c.

References XLogCtlData::info_lck, SpinLockAcquire, SpinLockRelease, and XLogCtlData::WalWriterSleeping.

Referenced by WalWriterMain().

12685 {
12687  XLogCtl->WalWriterSleeping = sleeping;
12689 }
slock_t info_lck
Definition: xlog.c:726
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool WalWriterSleeping
Definition: xlog.c:678
static XLogCtlData * XLogCtl
Definition: xlog.c:729

◆ ShutdownXLOG()

void ShutdownXLOG ( int  code,
Datum  arg 
)

Definition at line 8492 of file xlog.c.

References Assert, AuxProcessResourceOwner, CHECKPOINT_IMMEDIATE, CHECKPOINT_IS_SHUTDOWN, CreateCheckPoint(), CreateRestartPoint(), CurrentResourceOwner, ereport, errmsg(), IsPostmasterEnvironment, LOG, NOTICE, RecoveryInProgress(), RequestXLogSwitch(), WalSndInitStopping(), WalSndWaitStopping(), XLogArchiveCommandSet, and XLogArchivingActive.

Referenced by HandleCheckpointerInterrupts(), and InitPostgres().

8493 {
8494  /*
8495  * We should have an aux process resource owner to use, and we should not
8496  * be in a transaction that's installed some other resowner.
8497  */
8499  Assert(CurrentResourceOwner == NULL ||
8502 
8503  /* Don't be chatty in standalone mode */
8505  (errmsg("shutting down")));
8506 
8507  /*
8508  * Signal walsenders to move to stopping state.
8509  */
8511 
8512  /*
8513  * Wait for WAL senders to be in stopping state. This prevents commands
8514  * from writing new WAL.
8515  */
8517 
8518  if (RecoveryInProgress())
8520  else
8521  {
8522  /*
8523  * If archiving is enabled, rotate the last XLOG file so that all the
8524  * remaining records are archived (postmaster wakes up the archiver
8525  * process one more time at the end of shutdown). The checkpoint
8526  * record will go to the next XLOG file and won't be archived (yet).
8527  */
8529  RequestXLogSwitch(false);
8530 
8532  }
8533 }
bool IsPostmasterEnvironment
Definition: globals.c:108
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9693
void CreateCheckPoint(int flags)
Definition: xlog.c:8704
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
bool CreateRestartPoint(int flags)
Definition: xlog.c:9257
#define XLogArchiveCommandSet()
Definition: xlog.h:185
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:8076
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:145
void WalSndWaitStopping(void)
Definition: walsender.c:3175
#define ereport(elevel,...)
Definition: elog.h:144
#define NOTICE
Definition: elog.h:37
void WalSndInitStopping(void)
Definition: walsender.c:3149
#define Assert(condition)
Definition: c.h:746
#define XLogArchivingActive()
Definition: xlog.h:180
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:224
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:221

◆ StartupRequestWalReceiverRestart()

void StartupRequestWalReceiverRestart ( void  )

Definition at line 12528 of file xlog.c.

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

Referenced by StartupRereadConfig().

12529 {
12531  {
12532  ereport(LOG,
12533  (errmsg("WAL receiver process shutdown requested")));
12534 
12535  pendingWalRcvRestart = true;
12536  }
12537 }
#define LOG
Definition: elog.h:26
static bool pendingWalRcvRestart
Definition: xlog.c:830
#define ereport(elevel,...)
Definition: elog.h:144
static XLogSource currentSource
Definition: xlog.c:828
int errmsg(const char *fmt,...)
Definition: elog.c:821
bool WalRcvRunning(void)

◆ StartupXLOG()

void StartupXLOG ( void  )

Definition at line 6305 of file xlog.c.

References AdvanceNextFullTransactionIdPastXid(), AdvanceOldestClogXid(), AllowCascadeReplication, appendStringInfo(), appendStringInfoString(), ArchiveRecoveryRequested, ErrorContextCallback::arg, Assert, AuxProcessResourceOwner, BACKUP_LABEL_FILE, BACKUP_LABEL_OLD, ControlFileData::backupEndPoint, ControlFileData::backupEndRequired, ControlFileData::backupStartPoint, bgwriterLaunched, buf, ErrorContextCallback::callback, ControlFileData::checkPoint, CHECKPOINT_END_OF_RECOVERY, CHECKPOINT_FORCE, CHECKPOINT_IMMEDIATE, CHECKPOINT_WAIT, ControlFileData::checkPointCopy, CheckRecoveryConsistency(), CheckRequiredParameterValues(), checkTimeLineSwitch(), checkXLogConsistency(), XLogCtlData::ckptFullXid, close, CompleteCommitTsInitialization(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), XLogCtlInsert::CurrBytePos, XLogCtlData::currentChunkStartTime, CurrentResourceOwner, StringInfoData::data, DataDir, DB_IN_ARCHIVE_RECOVERY, DB_IN_CRASH_RECOVERY, DB_IN_PRODUCTION, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY, DB_SHUTDOWNING, DEBUG1, DEBUG2, DEBUG3, DeleteAllExportedSnapshotFiles(), DisownLatch(), doPageWrites, doRequestWalReceiverReply, durable_rename(), elog, EnableHotStandby, EnableSyncRequestForwarding(), EndRecPtr, ereport, errcode(), errcode_for_file_access(), errdetail(), errhint(), errmsg(), errmsg_internal(), ERROR, error_context_stack, ExecuteRecoveryCommand(), exitArchiveRecovery(), FATAL, findNewestTimeLine(), FirstNormalUnloggedLSN, XLogwrtRqst::Flush, XLogwrtResult::Flush, CheckPoint::fullPageWrites, XLogCtlInsert::fullPageWrites, FullTransactionIdRetreat(), GetCurrentTimestamp(), GetLatestXTime(), HandleStartupProcInterrupts(), InArchiveRecovery, XLogCtlData::info_lck, XLogCtlData::InitializedUpTo, InitRecoveryTransactionEnvironment(), initStringInfo(), InRecovery, InRedo, Insert(), XLogCtlData::Insert, InvalidXLogRecPtr, IsPostmasterEnvironment, IsUnderPostmaster, lastFullPageWrites, LastRec, XLogCtlData::lastReplayedEndRecPtr, XLogCtlData::lastReplayedTLI, XLogCtlData::lastSegSwitchLSN, XLogCtlData::lastSegSwitchTime, RunningTransactionsData::latestCompletedXid, VariableCacheData::latestCompletedXid, lfirst, LocalPromoteIsTriggered, LocalSetXLogInsertAllowed(), LocalXLogInsertAllowed, LOG, XLogCtlData::LogwrtResult, LogwrtResult, XLogCtlData::LogwrtRqst, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXFNAMELEN, MAXPGPATH, MemSet, ControlFileData::minRecoveryPoint, minRecoveryPoint, ControlFileData::minRecoveryPointTLI, minRecoveryPointTLI, MultiXactSetNextMXact(), CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, VariableCacheData::nextOid, CheckPoint::nextXid, RunningTransactionsData::nextXid, VariableCacheData::nextXid, NIL, NOTICE, tablespaceinfo::oid, VariableCacheData::oidCount, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, RunningTransactionsData::oldestRunningXid, CheckPoint::oldestXid, CheckPoint::oldestXidDB, OwnLatch(), XLogCtlData::pages, palloc(), PANIC, tablespaceinfo::path, pfree(), pg_rusage_init(), pg_rusage_show(), pg_usleep(), pgstat_reset_all(), PMSIGNAL_RECOVERY_STARTED, PreallocXlogFiles(), PrescanPreparedTransactions(), XLogCtlInsert::PrevBytePos, ErrorContextCallback::previous, CheckPoint::PrevTimeLineID, xl_end_of_recovery::PrevTimeLineID, XLogCtlData::PrevTimeLineID, primary_image_masked, proc_exit(), ProcArrayApplyRecoveryInfo(), ProcArrayInitRecovery(), psprintf(), PublishStartupProcessInformation(), reachedConsistency, read_backup_label(), read_tablespace_map(), XLogReaderState::readBuf, ReadCheckpointRecord(), readFile, readOff, ReadRecord(), readRecoverySignalFile(), ReadRecPtr, RecordKnownAssignedTransactionIds(), RecoverPreparedTransactions(), RECOVERY_STATE_ARCHIVE, RECOVERY_STATE_CRASH, RECOVERY_STATE_DONE, RECOVERY_TARGET_ACTION_PAUSE, RECOVERY_TARGET_ACTION_PROMOTE, RECOVERY_TARGET_ACTION_SHUTDOWN, RECOVERY_TARGET_IMMEDIATE, RECOVERY_TARGET_LSN, RECOVERY_TARGET_NAME, RECOVERY_TARGET_TIME, RECOVERY_TARGET_UNSET, RECOVERY_TARGET_XID, recoveryApplyDelay(), recoveryEndCommand, XLogCtlData::recoveryLastXTime, XLogCtlData::recoveryPause, recoveryPausesHere(), recoveryStopAfter, recoveryStopLSN, recoveryStopName, recoveryStopsAfter(), recoveryStopsBefore(), recoveryStopTime, recoveryStopXid, recoveryTarget, recoveryTargetAction, recoveryTargetLSN, recoveryTargetName, recoveryTargetTime, recoveryTargetTLI, recoveryTargetXid, XLogCtlData::recoveryWakeupLatch, CheckPoint::redo, RedoRecPtr, XLogCtlInsert::RedoRecPtr, XLogCtlData::RedoRecPtr, RedoStartLSN, RelationCacheInitFileRemove(), remove_tablespace_symlink(), RemoveNonParentXlogFiles(), RemoveTempXlogFiles(), replay_image_masked, XLogCtlData::replayEndRecPtr, XLogCtlData::replayEndTLI, RequestCheckpoint(), ResetUnloggedRelations(), restoreTimeLineHistoryFiles(), restoreTwoPhaseData(), RmgrData::rm_cleanup, RM_MAX_ID, RmgrData::rm_redo, rm_redo_error_callback(), RmgrData::rm_startup, RmgrTable, XLogReaderState::seg, SendPostmasterSignal(), SetCommitTsLimit(), SetMultiXactIdLimit(), SetRecoveryPause(), SetTransactionIdLimit(), XLogCtlData::SharedRecoveryState, ShmemVariableCache, ShutdownRecoveryTransactionEnvironment(), ShutdownWalRcv(), snprintf, SpinLockAcquire, SpinLockRelease, STANDBY_DISABLED, STANDBY_INITIALIZED, StandbyMode, StandbyModeRequested, StandbyRecoverPreparedTransactions(), standbyState, StartupCLOG(), StartupCommitTs(), StartupMultiXact(), StartupReorderBuffer(), StartupReplicationOrigin(), StartupReplicationSlots(), StartupSUBTRANS(), stat, ControlFileData::state, str_time(), RunningTransactionsData::subxcnt, RunningTransactionsData::subxid_overflow, symlink, SyncDataDirectory(), ControlFileData::system_identifier, XLogReaderState::system_identifier, TABLESPACE_MAP, TABLESPACE_MAP_OLD, CheckPoint::ThisTimeLineID, ThisTimeLineID, xl_end_of_recovery::ThisTimeLineID, XLogCtlData::ThisTimeLineID, CheckPoint::time, ControlFileData::time, timestamptz_to_str(), tliOfPointInHistory(), tliSwitchPoint(), trace_recovery_messages, ControlFileData::track_commit_timestamp, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdRetreat, TrimCLOG(), TrimMultiXact(), U64FromFullTransactionId, UINT64_FORMAT, UNLOGGED_RELATION_CLEANUP, UNLOGGED_RELATION_INIT, ControlFileData::unloggedLSN, XLogCtlData::unloggedLSN, UpdateControlFile(), UpdateFullPageWrites(), validateRecoveryParameters(), ValidateXLOGDirectoryStructure(), wal_segment_close(), wal_segment_size, WalRcvForceReply(), WalRcvStreaming(), WalSndWakeup(), XLogwrtRqst::Write, XLogwrtResult::Write, writeTimeLineHistory(), WALOpenSegment::ws_tli, RunningTransactionsData::xcnt, XidFromFullTransactionId, RunningTransactionsData::xids, XLogRecord::xl_info, XLogRecord::xl_rmid, XL_ROUTINE, XLogRecord::xl_xid, XLogCtlData::xlblocks, XLByteToPrevSeg, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, xlog_outdesc(), XLogArchiveCleanup(), XLogArchiveIsReadyOrDone(), XLogArchiveNotify(), XLogArchivingActive, XLogBeginRead(), XLOGDIR, XLogFileName, XLogFilePath, XLogPageRead(), xlogreader, XLogReaderAllocate(), XLogReaderFree(), XLogReceiptTime, XLogRecGetData, XLogRecPtrIsInvalid, XLogRecPtrToBufIdx, XLogRecPtrToBytePos(), XLogReportParameters(), XLogSegmentOffset, XLR_CHECK_CONSISTENCY, XLR_INFO_MASK, and XRecOffIsValid.

Referenced by InitPostgres(), and StartupProcessMain().

6306 {
6308  CheckPoint checkPoint;
6309  bool wasShutdown;
6310  bool reachedRecoveryTarget = false;
6311  bool haveBackupLabel = false;
6312  bool haveTblspcMap = false;
6313  XLogRecPtr RecPtr,
6314  checkPointLoc,
6315  EndOfLog;
6316  TimeLineID EndOfLogTLI;
6317  TimeLineID PrevTimeLineID;
6318  XLogRecord *record;
6319  TransactionId oldestActiveXID;
6320  bool backupEndRequired = false;
6321  bool backupFromStandby = false;
6322  DBState dbstate_at_startup;
6324  XLogPageReadPrivate private;
6325  bool promoted = false;
6326  struct stat st;
6327 
6328  /*
6329  * We should have an aux process resource owner to use, and we should not
6330  * be in a transaction that's installed some other resowner.
6331  */
6333  Assert(CurrentResourceOwner == NULL ||
6336 
6337  /*
6338  * Check that contents look valid.
6339  */
6341  ereport(FATAL,
6342  (errmsg("control file contains invalid checkpoint location")));
6343 
6344  switch (ControlFile->state)
6345  {
6346  case DB_SHUTDOWNED:
6347 
6348  /*
6349  * This is the expected case, so don't be chatty in standalone
6350  * mode
6351  */
6353  (errmsg("database system was shut down at %s",
6354  str_time(ControlFile->time))));
6355  break;
6356 
6358  ereport(LOG,
6359  (errmsg("database system was shut down in recovery at %s",
6360  str_time(ControlFile->time))));
6361  break;
6362 
6363  case DB_SHUTDOWNING:
6364  ereport(LOG,
6365  (errmsg("database system shutdown was interrupted; last known up at %s",
6366  str_time(ControlFile->time))));
6367  break;
6368 
6369  case DB_IN_CRASH_RECOVERY:
6370  ereport(LOG,
6371  (errmsg("database system was interrupted while in recovery at %s",
6373  errhint("This probably means that some data is corrupted and"
6374  " you will have to use the last backup for recovery.")));
6375  break;
6376 
6378  ereport(LOG,
6379  (errmsg("database system was interrupted while in recovery at log time %s",
6381  errhint("If this has occurred more than once some data might be corrupted"
6382  " and you might need to choose an earlier recovery target.")));
6383  break;
6384 
6385  case DB_IN_PRODUCTION:
6386  ereport(LOG,
6387  (errmsg("database system was interrupted; last known up at %s",
6388  str_time(ControlFile->time))));
6389  break;
6390 
6391  default:
6392  ereport(FATAL,
6393  (errmsg("control file contains invalid database cluster state")));
6394  }
6395 
6396  /* This is just to allow attaching to startup process with a debugger */
6397 #ifdef XLOG_REPLAY_DELAY
6399  pg_usleep(60000000L);
6400 #endif
6401 
6402  /*
6403  * Verify that pg_wal and pg_wal/archive_status exist. In cases where
6404  * someone has performed a copy for PITR, these directories may have been
6405  * excluded and need to be re-created.
6406  */
6408 
6409  /*----------
6410  * If we previously crashed, perform a couple of actions:
6411  *
6412  * - The pg_wal directory may still include some temporary WAL segments
6413  * used when creating a new segment, so perform some clean up to not
6414  * bloat this path. This is done first as there is no point to sync
6415  * this temporary data.
6416  *
6417  * - There might be data which we had written, intending to fsync it, but
6418  * which we had not actually fsync'd yet. Therefore, a power failure in
6419  * the near future might cause earlier unflushed writes to be lost, even
6420  * though more recent data written to disk from here on would be
6421  * persisted. To avoid that, fsync the entire data directory.
6422  */
6423  if (ControlFile->state != DB_SHUTDOWNED &&
6425  {
6428  }
6429 
6430  /*
6431  * Initialize on the assumption we want to recover to the latest timeline
6432  * that's active according to pg_control.
6433  */
6437  else
6439 
6440  /*
6441  * Check for signal files, and if so set up state for offline recovery
6442  */
6445 
6447  {
6449  ereport(LOG,
6450  (errmsg("entering standby mode")));
6451  else if (recoveryTarget == RECOVERY_TARGET_XID)
6452  ereport(LOG,
6453  (errmsg("starting point-in-time recovery to XID %u",
6454  recoveryTargetXid)));
6456  ereport(LOG,
6457  (errmsg("starting point-in-time recovery to %s",
6460  ereport(LOG,
6461  (errmsg("starting point-in-time recovery to \"%s\"",
6462  recoveryTargetName)));
6463  else if (recoveryTarget == RECOVERY_TARGET_LSN)
6464  ereport(LOG,
6465  (errmsg("starting point-in-time recovery to WAL location (LSN) \"%X/%X\"",
6466  (uint32) (recoveryTargetLSN >> 32),
6469  ereport(LOG,
6470  (errmsg("starting point-in-time recovery to earliest consistent point")));
6471  else
6472  ereport(LOG,
6473  (errmsg("starting archive recovery")));
6474  }
6475 
6476  /*
6477  * Take ownership of the wakeup latch if we're going to sleep during
6478  * recovery.
6479  */
6482 
6483  /* Set up XLOG reader facility */
6484  MemSet(&private, 0, sizeof(XLogPageReadPrivate));
6485  xlogreader =
6487  XL_ROUTINE(.page_read = &XLogPageRead,
6488  .segment_open = NULL,
6489  .segment_close = wal_segment_close),
6490  &private);
6491  if (!xlogreader)
6492  ereport(ERROR,
6493  (errcode(ERRCODE_OUT_OF_MEMORY),
6494  errmsg("out of memory"),
6495  errdetail("Failed while allocating a WAL reading processor.")));
6497 
6498  /*
6499  * Allocate two page buffers dedicated to WAL consistency checks. We do
6500  * it this way, rather than just making static arrays, for two reasons:
6501  * (1) no need to waste the storage in most instantiations of the backend;
6502  * (2) a static char array isn't guaranteed to have any particular
6503  * alignment, whereas palloc() will provide MAXALIGN'd storage.
6504  */
6505  replay_image_masked = (char *) palloc(BLCKSZ);
6506  primary_image_masked = (char *) palloc(BLCKSZ);
6507 
6508  if (read_backup_label(&checkPointLoc, &backupEndRequired,
6509  &backupFromStandby))
6510  {
6511  List *tablespaces = NIL;
6512 
6513  /*
6514  * Archive recovery was requested, and thanks to the backup label
6515  * file, we know how far we need to replay to reach consistency. Enter
6516  * archive recovery directly.
6517  */
6518  InArchiveRecovery = true;
6520  StandbyMode = true;
6521 
6522  /*
6523  * When a backup_label file is present, we want to roll forward from
6524  * the checkpoint it identifies, rather than using pg_control.
6525  */
6526  record = ReadCheckpointRecord(xlogreader, checkPointLoc, 0, true);
6527  if (record != NULL)
6528  {
6529  memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
6530  wasShutdown = ((record->xl_info & ~XLR_INFO_MASK) == XLOG_CHECKPOINT_SHUTDOWN);
6531  ereport(DEBUG1,
6532  (errmsg("checkpoint record is at %X/%X",
6533  (uint32) (checkPointLoc >> 32), (uint32) checkPointLoc)));
6534  InRecovery = true; /* force recovery even if SHUTDOWNED */
6535 
6536  /*
6537  * Make sure that REDO location exists. This may not be the case
6538  * if there was a crash during an online backup, which left a
6539  * backup_label around that references a WAL segment that's
6540  * already been archived.
6541  */
6542  if (checkPoint.redo < checkPointLoc)
6543  {
6544  XLogBeginRead(xlogreader, checkPoint.redo);
6545  if (!ReadRecord(xlogreader, LOG, false))
6546  ereport(FATAL,
6547  (errmsg("could not find redo location referenced by checkpoint record"),
6548  errhint("If you are restoring from a backup, touch \"%s/recovery.signal\" and add required recovery options.\n"
6549  "If you are not restoring from a backup, try removing the fi