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 2327 of file xlog.c.

References CalculateCheckpointSegments(), CheckPointCompletionTarget, and newval.

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

◆ assign_max_wal_size()

void assign_max_wal_size ( int  newval,
void *  extra 
)

Definition at line 2320 of file xlog.c.

References CalculateCheckpointSegments(), max_wal_size_mb, and newval.

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

◆ BootStrapXLOG()

void BootStrapXLOG ( void  )

Definition at line 5193 of file xlog.c.

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

Referenced by AuxiliaryProcessMain().

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

◆ CalculateMaxmumSafeLSN()

XLogRecPtr CalculateMaxmumSafeLSN ( void  )

◆ CheckPromoteSignal()

bool CheckPromoteSignal ( void  )

Definition at line 12634 of file xlog.c.

References PROMOTE_SIGNAL_FILE, and stat.

Referenced by CheckForStandbyTrigger(), and sigusr1_handler().

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

◆ CheckXLogRemoved()

void CheckXLogRemoved ( XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 3925 of file xlog.c.

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

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

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

◆ CreateCheckPoint()

void CreateCheckPoint ( int  flags)

Definition at line 8701 of file xlog.c.

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

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

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

◆ CreateRestartPoint()

bool CreateRestartPoint ( int  flags)

Definition at line 9242 of file xlog.c.

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

Referenced by CheckpointerMain(), and ShutdownXLOG().

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

◆ DataChecksumsEnabled()

bool DataChecksumsEnabled ( void  )

Definition at line 4930 of file xlog.c.

References Assert, and ControlFileData::data_checksum_version.

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

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

◆ do_pg_abort_backup()

void do_pg_abort_backup ( int  code,
Datum  arg 
)

Definition at line 11427 of file xlog.c.

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

Referenced by perform_base_backup(), and register_persistent_abort_backup_handler().

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

◆ do_pg_start_backup()

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

Definition at line 10496 of file xlog.c.

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

Referenced by perform_base_backup(), and pg_start_backup().

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

◆ do_pg_stop_backup()

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

Definition at line 11014 of file xlog.c.

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

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

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

◆ get_backup_status()

SessionBackupState get_backup_status ( void  )

Definition at line 10993 of file xlog.c.

References sessionBackupState.

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

10994 {
10995  return sessionBackupState;
10996 }
static SessionBackupState sessionBackupState
Definition: xlog.c:533

◆ GetCurrentChunkReplayStartTime()

TimestampTz GetCurrentChunkReplayStartTime ( void  )

Definition at line 6203 of file xlog.c.

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

Referenced by GetReplicationApplyDelay().

6204 {
6205  TimestampTz xtime;
6206 
6208  xtime = XLogCtl->currentChunkStartTime;
6210 
6211  return xtime;
6212 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:728
TimestampTz currentChunkStartTime
Definition: xlog.c:715

◆ GetFakeLSNForUnloggedRel()

XLogRecPtr GetFakeLSNForUnloggedRel ( void  )

Definition at line 4946 of file xlog.c.

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

Referenced by gistGetFakeLSN().

4947 {
4948  XLogRecPtr nextUnloggedLSN;
4949 
4950  /* increment the unloggedLSN counter, need SpinLock */
4952  nextUnloggedLSN = XLogCtl->unloggedLSN++;
4954 
4955  return nextUnloggedLSN;
4956 }
XLogRecPtr unloggedLSN
Definition: xlog.c:611
#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:728
slock_t ulsn_lck
Definition: xlog.c:612

◆ GetFlushRecPtr()

XLogRecPtr GetFlushRecPtr ( void  )

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

8422 {
8426 
8427  return LogwrtResult.Flush;
8428 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:774
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:622
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:728
XLogRecPtr Flush
Definition: xlog.c:447

◆ GetFullPageWriteInfo()

void GetFullPageWriteInfo ( XLogRecPtr RedoRecPtr_p,
bool doPageWrites_p 
)

Definition at line 8390 of file xlog.c.

References doPageWrites, and RedoRecPtr.

Referenced by XLogCheckBufferNeedsBackup(), and XLogInsert().

8391 {
8392  *RedoRecPtr_p = RedoRecPtr;
8393  *doPageWrites_p = doPageWrites;
8394 }
static bool doPageWrites
Definition: xlog.c:380
static XLogRecPtr RedoRecPtr
Definition: xlog.c:373

◆ GetInsertRecPtr()

XLogRecPtr GetInsertRecPtr ( void  )

Definition at line 8405 of file xlog.c.

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

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

8406 {
8407  XLogRecPtr recptr;
8408 
8410  recptr = XLogCtl->LogwrtRqst.Write;
8412 
8413  return recptr;
8414 }
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr Write
Definition: xlog.c:440
XLogwrtRqst LogwrtRqst
Definition: xlog.c:602
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:728

◆ GetLastImportantRecPtr()

XLogRecPtr GetLastImportantRecPtr ( void  )

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

8440 {
8442  int i;
8443 
8444  for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
8445  {
8446  XLogRecPtr last_important;
8447 
8448  /*
8449  * Need to take a lock to prevent torn reads of the LSN, which are
8450  * possible on some of the supported platforms. WAL insert locks only
8451  * support exclusive mode, so we have to use that.
8452  */
8454  last_important = WALInsertLocks[i].l.lastImportantAt;
8455  LWLockRelease(&WALInsertLocks[i].l.lock);
8456 
8457  if (res < last_important)
8458  res = last_important;
8459  }
8460 
8461  return res;
8462 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr lastImportantAt
Definition: xlog.c:490
#define NUM_XLOGINSERT_LOCKS
Definition: xlog.c:123
WALInsertLock l
Definition: xlog.c:502
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:731

◆ GetLatestXTime()

TimestampTz GetLatestXTime ( void  )

Definition at line 6173 of file xlog.c.

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

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

6174 {
6175  TimestampTz xtime;
6176 
6178  xtime = XLogCtl->recoveryLastXTime;
6180 
6181  return xtime;
6182 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
TimestampTz recoveryLastXTime
Definition: xlog.c:709
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:728

◆ GetMockAuthenticationNonce()

char* GetMockAuthenticationNonce ( void  )

Definition at line 4920 of file xlog.c.

References Assert, and ControlFileData::mock_authentication_nonce.

Referenced by scram_mock_salt().

4921 {
4922  Assert(ControlFile != NULL);
4924 }
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:227
static ControlFileData * ControlFile
Definition: xlog.c:736
#define Assert(condition)
Definition: c.h:745

◆ GetRecoveryState()

RecoveryState GetRecoveryState ( void  )

Definition at line 8122 of file xlog.c.

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

Referenced by XLogArchiveCheckDone().

8123 {
8124  RecoveryState retval;
8125 
8127  retval = XLogCtl->SharedRecoveryState;
8129 
8130  return retval;
8131 }
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
RecoveryState SharedRecoveryState
Definition: xlog.c:658
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:728
RecoveryState
Definition: xlog.h:170

◆ GetRedoRecPtr()

XLogRecPtr GetRedoRecPtr ( void  )

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

8362 {
8363  XLogRecPtr ptr;
8364 
8365  /*
8366  * The possibly not up-to-date copy in XlogCtl is enough. Even if we
8367  * grabbed a WAL insertion lock to read the authoritative value in
8368  * Insert->RedoRecPtr, someone might update it just after we've released
8369  * the lock.
8370  */
8372  ptr = XLogCtl->RedoRecPtr;
8374 
8375  if (RedoRecPtr < ptr)
8376  RedoRecPtr = ptr;
8377 
8378  return RedoRecPtr;
8379 }
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
static XLogRecPtr RedoRecPtr
Definition: xlog.c:373
XLogRecPtr RedoRecPtr
Definition: xlog.c:603
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:728

◆ GetSystemIdentifier()

uint64 GetSystemIdentifier ( void  )

Definition at line 4910 of file xlog.c.

References Assert, and ControlFileData::system_identifier.

Referenced by IdentifySystem(), and WalReceiverMain().

4911 {
4912  Assert(ControlFile != NULL);
4914 }
uint64 system_identifier
Definition: pg_control.h:106
static ControlFileData * ControlFile
Definition: xlog.c:736
#define Assert(condition)
Definition: c.h:745

◆ GetWALAvailability()

WALAvailability GetWALAvailability ( XLogRecPtr  targetLSN)

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

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

◆ GetXLogInsertRecPtr()

XLogRecPtr GetXLogInsertRecPtr ( void  )

Definition at line 11494 of file xlog.c.

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

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

11495 {
11497  uint64 current_bytepos;
11498 
11499  SpinLockAcquire(&Insert->insertpos_lck);
11500  current_bytepos = Insert->CurrBytePos;
11501  SpinLockRelease(&Insert->insertpos_lck);
11502 
11503  return XLogBytePosToRecPtr(current_bytepos);
11504 }
slock_t insertpos_lck
Definition: xlog.c:540
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1995
XLogCtlInsert Insert
Definition: xlog.c:599
#define SpinLockAcquire(lock)
Definition: spin.h:62
uint64 CurrBytePos
Definition: xlog.c:549
static void Insert(File file)
Definition: fd.c:1174
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:728

◆ GetXLogReceiptTime()

void GetXLogReceiptTime ( TimestampTz rtime,
bool fromStream 
)

Definition at line 6219 of file xlog.c.

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

Referenced by GetStandbyLimitTime().

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

◆ GetXLogReplayRecPtr()

XLogRecPtr GetXLogReplayRecPtr ( TimeLineID replayTLI)

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

11476 {
11477  XLogRecPtr recptr;
11478  TimeLineID tli;
11479 
11481  recptr = XLogCtl->lastReplayedEndRecPtr;
11482  tli = XLogCtl->lastReplayedTLI;
11484 
11485  if (replayTLI)
11486  *replayTLI = tli;
11487  return recptr;
11488 }
uint32 TimeLineID
Definition: xlogdefs.h:52
slock_t info_lck
Definition: xlog.c:725
#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:728
TimeLineID lastReplayedTLI
Definition: xlog.c:705
XLogRecPtr lastReplayedEndRecPtr
Definition: xlog.c:704

◆ GetXLogWriteRecPtr()

XLogRecPtr GetXLogWriteRecPtr ( void  )

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

11511 {
11515 
11516  return LogwrtResult.Write;
11517 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:774
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:622
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:728
XLogRecPtr Write
Definition: xlog.c:446

◆ HotStandbyActive()

bool HotStandbyActive ( void  )

Definition at line 8143 of file xlog.c.

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

Referenced by XLogWalRcvSendHSFeedback().

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

◆ HotStandbyActiveInReplay()

bool HotStandbyActiveInReplay ( void  )

Definition at line 8168 of file xlog.c.

References AmStartupProcess, Assert, IsPostmasterEnvironment, and LocalHotStandbyActive.

8169 {
8171  return LocalHotStandbyActive;
8172 }
#define AmStartupProcess()
Definition: miscadmin.h:431
bool IsPostmasterEnvironment
Definition: globals.c:108
static bool LocalHotStandbyActive
Definition: xlog.c:234
#define Assert(condition)
Definition: c.h:745

◆ InitXLOGAccess()

void InitXLOGAccess ( void  )

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

8336 {
8338 
8339  /* ThisTimeLineID doesn't change so we need no lock to copy it */
8342 
8343  /* set wal_segment_size */
8345 
8346  /* Use GetRedoRecPtr to copy the RedoRecPtr safely */
8347  (void) GetRedoRecPtr();
8348  /* Also update our copy of doPageWrites. */
8349  doPageWrites = (Insert->fullPageWrites || Insert->forcePageWrites);
8350 
8351  /* Also initialize the working areas for constructing WAL records */
8352  InitXLogInsert();
8353 }
int wal_segment_size
Definition: xlog.c:116
void InitXLogInsert(void)
Definition: xloginsert.c:1140
TimeLineID ThisTimeLineID
Definition: xlog.c:651
XLogCtlInsert Insert
Definition: xlog.c:599
bool fullPageWrites
Definition: xlog.c:574
uint32 xlog_seg_size
Definition: pg_control.h:209
static bool doPageWrites
Definition: xlog.c:380
bool forcePageWrites
Definition: xlog.c:573
static void Insert(File file)
Definition: fd.c:1174
static ControlFileData * ControlFile
Definition: xlog.c:736
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define Assert(condition)
Definition: c.h:745
static XLogCtlData * XLogCtl
Definition: xlog.c:728
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8361
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:393

◆ issue_xlog_fsync()

void issue_xlog_fsync ( int  fd,
XLogSegNo  segno 
)

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

10404 {
10405  char *msg = NULL;
10406 
10408  switch (sync_method)
10409  {
10410  case SYNC_METHOD_FSYNC:
10411  if (pg_fsync_no_writethrough(fd) != 0)
10412  msg = _("could not fsync file \"%s\": %m");
10413  break;
10414 #ifdef HAVE_FSYNC_WRITETHROUGH
10416  if (pg_fsync_writethrough(fd) != 0)
10417  msg = _("could not fsync write-through file \"%s\": %m");
10418  break;
10419 #endif
10420 #ifdef HAVE_FDATASYNC
10421  case SYNC_METHOD_FDATASYNC:
10422  if (pg_fdatasync(fd) != 0)
10423  msg = _("could not fdatasync file \"%s\": %m");
10424  break;
10425 #endif
10426  case SYNC_METHOD_OPEN:
10428  /* write synced it already */
10429  break;
10430  default:
10431  elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
10432  break;
10433  }
10434 
10435  /* PANIC if failed to fsync */
10436  if (msg)
10437  {
10438  char xlogfname[MAXFNAMELEN];
10439  int save_errno = errno;
10440 
10441  XLogFileName(xlogfname, ThisTimeLineID, segno,
10443  errno = save_errno;
10444  ereport(PANIC,
10446  errmsg(msg, xlogfname)));
10447  }
10448 
10450 }
int pg_fdatasync(int fd)
Definition: fd.c:435
int wal_segment_size
Definition: xlog.c:116
#define SYNC_METHOD_FSYNC_WRITETHROUGH
Definition: xlog.h:28
int pg_fsync_writethrough(int fd)
Definition: fd.c:412
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:400
#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:1381
#define SYNC_METHOD_OPEN
Definition: xlog.h:27
#define MAXFNAMELEN
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define ereport(elevel,...)
Definition: elog.h:144
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1357
int sync_method
Definition: xlog.c:105
#define SYNC_METHOD_FDATASYNC
Definition: xlog.h:26
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
#define _(x)
Definition: elog.c:88

◆ LocalProcessControlFile()

void LocalProcessControlFile ( bool  reset)

Definition at line 5030 of file xlog.c.

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

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

5031 {
5032  Assert(reset || ControlFile == NULL);
5033  ControlFile = palloc(sizeof(ControlFileData));
5034  ReadControlFile();
5035 }
static void ReadControlFile(void)
Definition: xlog.c:4701
static ControlFileData * ControlFile
Definition: xlog.c:736
#define Assert(condition)
Definition: c.h:745
void * palloc(Size size)
Definition: mcxt.c:949

◆ PromoteIsTriggered()

bool PromoteIsTriggered ( void  )

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

12554 {
12555  /*
12556  * We check shared state each time only until a standby promotion is
12557  * triggered. We can't trigger a promotion again, so there's no need to
12558  * keep checking after the shared variable has once been seen true.
12559  */
12561  return true;
12562 
12566 
12567  return LocalPromoteIsTriggered;
12568 }
slock_t info_lck
Definition: xlog.c:725
static bool LocalPromoteIsTriggered
Definition: xlog.c:240
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:728
bool SharedPromoteIsTriggered
Definition: xlog.c:670

◆ RecoveryInProgress()

bool RecoveryInProgress ( void  )

Definition at line 8069 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(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateRestartPoint(), do_pg_start_backup(), do_pg_stop_backup(), error_commit_ts_disabled(), get_relation_info(), GetNewMultiXactId(), GetNewObjectId(), GetNewTransactionId(), GetOldestActiveTransactionId(), GetOldestSafeDecodingTransactionId(), GetOldestXmin(), GetRunningTransactionData(), GetSerializableTransactionSnapshot(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), gin_clean_pending_list(), heap_page_prune_opt(), IdentifySystem(), InitPostgres(), InitTempTableNamespace(), IsCheckpointOnSchedule(), LockAcquireExtended(), 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().

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

◆ RecoveryIsPaused()

bool RecoveryIsPaused ( void  )

Definition at line 6037 of file xlog.c.

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

Referenced by pg_is_wal_replay_paused(), and recoveryPausesHere().

6038 {
6039  bool recoveryPause;
6040 
6042  recoveryPause = XLogCtl->recoveryPause;
6044 
6045  return recoveryPause;
6046 }
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:717
static XLogCtlData * XLogCtl
Definition: xlog.c:728

◆ register_persistent_abort_backup_handler()

void register_persistent_abort_backup_handler ( void  )

Definition at line 11459 of file xlog.c.

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

Referenced by pg_start_backup().

11460 {
11461  static bool already_done = false;
11462 
11463  if (already_done)
11464  return;
11466  already_done = true;
11467 }
void do_pg_abort_backup(int code, Datum arg)
Definition: xlog.c:11427
#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 12625 of file xlog.c.

References PROMOTE_SIGNAL_FILE.

Referenced by CheckForStandbyTrigger(), and PostmasterMain().

12626 {
12627  unlink(PROMOTE_SIGNAL_FILE);
12628 }
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:396

◆ SetRecoveryPause()

void SetRecoveryPause ( bool  recoveryPause)

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

6050 {
6052  XLogCtl->recoveryPause = recoveryPause;
6054 }
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:717
static XLogCtlData * XLogCtl
Definition: xlog.c:728

◆ SetWalWriterSleeping()

void SetWalWriterSleeping ( bool  sleeping)

Definition at line 12658 of file xlog.c.

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

Referenced by WalWriterMain().

12659 {
12661  XLogCtl->WalWriterSleeping = sleeping;
12663 }
slock_t info_lck
Definition: xlog.c:725
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool WalWriterSleeping
Definition: xlog.c:677
static XLogCtlData * XLogCtl
Definition: xlog.c:728

◆ ShutdownXLOG()

void ShutdownXLOG ( int  code,
Datum  arg 
)

Definition at line 8485 of file xlog.c.

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

Referenced by HandleCheckpointerInterrupts(), and InitPostgres().

8486 {
8487  /*
8488  * We should have an aux process resource owner to use, and we should not
8489  * be in a transaction that's installed some other resowner.
8490  */
8492  Assert(CurrentResourceOwner == NULL ||
8495 
8496  /* Don't be chatty in standalone mode */
8498  (errmsg("shutting down")));
8499 
8500  /*
8501  * Signal walsenders to move to stopping state.
8502  */
8504 
8505  /*
8506  * Wait for WAL senders to be in stopping state. This prevents commands
8507  * from writing new WAL.
8508  */
8510 
8511  if (RecoveryInProgress())
8513  else
8514  {
8515  /*
8516  * If archiving is enabled, rotate the last XLOG file so that all the
8517  * remaining records are archived (postmaster wakes up the archiver
8518  * process one more time at the end of shutdown). The checkpoint
8519  * record will go to the next XLOG file and won't be archived (yet).
8520  */
8522  RequestXLogSwitch(false);
8523 
8525  }
8526  ShutdownCLOG();
8527  ShutdownCommitTs();
8528  ShutdownSUBTRANS();
8530 }
bool IsPostmasterEnvironment
Definition: globals.c:108
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9678
void ShutdownSUBTRANS(void)
Definition: subtrans.c:285
void CreateCheckPoint(int flags)
Definition: xlog.c:8701
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ShutdownCLOG(void)
Definition: clog.c:816
bool CreateRestartPoint(int flags)
Definition: xlog.c:9242
#define XLogArchiveCommandSet()
Definition: xlog.h:185
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:8069
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:145
void WalSndWaitStopping(void)
Definition: walsender.c:3149
void ShutdownMultiXact(void)
Definition: multixact.c:2107
#define ereport(elevel,...)
Definition: elog.h:144
#define NOTICE
Definition: elog.h:37
void WalSndInitStopping(void)
Definition: walsender.c:3123
void ShutdownCommitTs(void)
Definition: commit_ts.c:805
#define Assert(condition)
Definition: c.h:745
#define XLogArchivingActive()
Definition: xlog.h:180
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:224
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:221

◆ StartupRequestWalReceiverRestart()

void StartupRequestWalReceiverRestart ( void  )

Definition at line 12502 of file xlog.c.

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

Referenced by StartupRereadConfig().

12503 {
12505  {
12506  ereport(LOG,
12507  (errmsg("wal receiver process shutdown requested")));
12508 
12509  pendingWalRcvRestart = true;
12510  }
12511 }
#define LOG
Definition: elog.h:26
static bool pendingWalRcvRestart
Definition: xlog.c:829
#define ereport(elevel,...)
Definition: elog.h:144
static XLogSource currentSource
Definition: xlog.c:827
int errmsg(const char *fmt,...)
Definition: elog.c:824
bool WalRcvRunning(void)

◆ StartupXLOG()

void StartupXLOG ( void  )

Definition at line 6302 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, 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::nextFullXid, VariableCacheData::nextFullXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, VariableCacheData::nextOid, RunningTransactionsData::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_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().

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