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 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"
 
#define FALLBACK_PROMOTE_SIGNAL_FILE   "fallback_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_NORMAL, WALAVAIL_RESERVED, 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 restart_lsn)
 
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 infotbssize, 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_segments
 
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 387 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 */

◆ FALLBACK_PROMOTE_SIGNAL_FILE

#define FALLBACK_PROMOTE_SIGNAL_FILE   "fallback_promote"

Definition at line 394 of file xlog.h.

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

◆ InHotStandby

◆ PROMOTE_SIGNAL_FILE

#define PROMOTE_SIGNAL_FILE   "promote"

◆ RECOVERY_SIGNAL_FILE

#define RECOVERY_SIGNAL_FILE   "recovery.signal"

Definition at line 384 of file xlog.h.

Referenced by exitArchiveRecovery(), and readRecoverySignalFile().

◆ STANDBY_SIGNAL_FILE

#define STANDBY_SIGNAL_FILE   "standby.signal"

Definition at line 385 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 390 of file xlog.h.

Referenced by CancelBackup(), and StartupXLOG().

◆ XLOG_INCLUDE_ORIGIN

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

◆ 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

#define XLogLogicalInfoActive ( )    (wal_level >= WAL_LEVEL_LOGICAL)

◆ 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 366 of file xlog.h.

◆ WALAvailability

Enumerator
WALAVAIL_INVALID_LSN 
WALAVAIL_NORMAL 
WALAVAIL_RESERVED 
WALAVAIL_REMOVED 

Definition at line 270 of file xlog.h.

271 {
272  WALAVAIL_INVALID_LSN, /* parameter error */
273  WALAVAIL_NORMAL, /* WAL segment is within max_wal_size */
274  WALAVAIL_RESERVED, /* WAL segment is reserved by a slot */
275  WALAVAIL_REMOVED /* WAL segment has been removed */
WALAvailability
Definition: xlog.h:270

◆ 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:848
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:1380
#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:738
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:684
bool fullPageWrites
Definition: pg_control.h:42
void BootStrapCLOG(void)
Definition: clog.c:705
#define Assert(condition)
Definition: c.h:738
#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:1356
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:343
#define close(a)
Definition: win32.h:12
void BootStrapCommitTs(void)
Definition: commit_ts.c:523
#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:661
#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 12644 of file xlog.c.

References FALLBACK_PROMOTE_SIGNAL_FILE, PROMOTE_SIGNAL_FILE, and stat.

Referenced by sigusr1_handler().

12645 {
12646  struct stat stat_buf;
12647 
12648  if (stat(PROMOTE_SIGNAL_FILE, &stat_buf) == 0 ||
12650  return true;
12651 
12652  return false;
12653 }
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:393
struct stat stat_buf
Definition: pg_standby.c:100
#define FALLBACK_PROMOTE_SIGNAL_FILE
Definition: xlog.h:394
#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:727
XLogSegNo lastRemovedSegNo
Definition: xlog.c:610
#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:730
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 8700 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().

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

9242 {
9243  XLogRecPtr lastCheckPointRecPtr;
9244  XLogRecPtr lastCheckPointEndPtr;
9245  CheckPoint lastCheckPoint;
9246  XLogRecPtr PriorRedoPtr;
9247  XLogRecPtr receivePtr;
9248  XLogRecPtr replayPtr;
9249  TimeLineID replayTLI;
9250  XLogRecPtr endptr;
9251  XLogSegNo _logSegNo;
9252  TimestampTz xtime;
9253 
9254  /*
9255  * Acquire CheckpointLock to ensure only one restartpoint or checkpoint
9256  * happens at a time.
9257  */
9258  LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
9259 
9260  /* Get a local copy of the last safe checkpoint record. */
9262  lastCheckPointRecPtr = XLogCtl->lastCheckPointRecPtr;
9263  lastCheckPointEndPtr = XLogCtl->lastCheckPointEndPtr;
9264  lastCheckPoint = XLogCtl->lastCheckPoint;
9266 
9267  /*
9268  * Check that we're still in recovery mode. It's ok if we exit recovery
9269  * mode after this check, the restart point is valid anyway.
9270  */
9271  if (!RecoveryInProgress())
9272  {
9273  ereport(DEBUG2,
9274  (errmsg("skipping restartpoint, recovery has already ended")));
9275  LWLockRelease(CheckpointLock);
9276  return false;
9277  }
9278 
9279  /*
9280  * If the last checkpoint record we've replayed is already our last
9281  * restartpoint, we can't perform a new restart point. We still update
9282  * minRecoveryPoint in that case, so that if this is a shutdown restart
9283  * point, we won't start up earlier than before. That's not strictly
9284  * necessary, but when hot standby is enabled, it would be rather weird if
9285  * the database opened up for read-only connections at a point-in-time
9286  * before the last shutdown. Such time travel is still possible in case of
9287  * immediate shutdown, though.
9288  *
9289  * We don't explicitly advance minRecoveryPoint when we do create a
9290  * restartpoint. It's assumed that flushing the buffers will do that as a
9291  * side-effect.
9292  */
9293  if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||
9294  lastCheckPoint.redo <= ControlFile->checkPointCopy.redo)
9295  {
9296  ereport(DEBUG2,
9297  (errmsg("skipping restartpoint, already performed at %X/%X",
9298  (uint32) (lastCheckPoint.redo >> 32),
9299  (uint32) lastCheckPoint.redo)));
9300 
9302  if (flags & CHECKPOINT_IS_SHUTDOWN)
9303  {
9304  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9306  ControlFile->time = (pg_time_t) time(NULL);
9308  LWLockRelease(ControlFileLock);
9309  }
9310  LWLockRelease(CheckpointLock);
9311  return false;
9312  }
9313 
9314  /*
9315  * Update the shared RedoRecPtr so that the startup process can calculate
9316  * the number of segments replayed since last restartpoint, and request a
9317  * restartpoint if it exceeds CheckPointSegments.
9318  *
9319  * Like in CreateCheckPoint(), hold off insertions to update it, although
9320  * during recovery this is just pro forma, because no WAL insertions are
9321  * happening.
9322  */
9324  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = lastCheckPoint.redo;
9326 
9327  /* Also update the info_lck-protected copy */
9329  XLogCtl->RedoRecPtr = lastCheckPoint.redo;
9331 
9332  /*
9333  * Prepare to accumulate statistics.
9334  *
9335  * Note: because it is possible for log_checkpoints to change while a
9336  * checkpoint proceeds, we always accumulate stats, even if
9337  * log_checkpoints is currently off.
9338  */
9339  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
9341 
9342  if (log_checkpoints)
9343  LogCheckpointStart(flags, true);
9344 
9345  CheckPointGuts(lastCheckPoint.redo, flags);
9346 
9347  /*
9348  * Remember the prior checkpoint's redo ptr for
9349  * UpdateCheckPointDistanceEstimate()
9350  */
9351  PriorRedoPtr = ControlFile->checkPointCopy.redo;
9352 
9353  /*
9354  * Update pg_control, using current time. Check that it still shows
9355  * DB_IN_ARCHIVE_RECOVERY state and an older checkpoint, else do nothing;
9356  * this is a quick hack to make sure nothing really bad happens if somehow
9357  * we get here after the end-of-recovery checkpoint.
9358  */
9359  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9361  ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
9362  {
9363  ControlFile->checkPoint = lastCheckPointRecPtr;
9364  ControlFile->checkPointCopy = lastCheckPoint;
9365  ControlFile->time = (pg_time_t) time(NULL);
9366 
9367  /*
9368  * Ensure minRecoveryPoint is past the checkpoint record. Normally,
9369  * this will have happened already while writing out dirty buffers,
9370  * but not necessarily - e.g. because no buffers were dirtied. We do
9371  * this because a non-exclusive base backup uses minRecoveryPoint to
9372  * determine which WAL files must be included in the backup, and the
9373  * file (or files) containing the checkpoint record must be included,
9374  * at a minimum. Note that for an ordinary restart of recovery there's
9375  * no value in having the minimum recovery point any earlier than this
9376  * anyway, because redo will begin just after the checkpoint record.
9377  */
9378  if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
9379  {
9380  ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
9382 
9383  /* update local copy */
9386  }
9387  if (flags & CHECKPOINT_IS_SHUTDOWN)
9390  }
9391  LWLockRelease(ControlFileLock);
9392 
9393  /*
9394  * Update the average distance between checkpoints/restartpoints if the
9395  * prior checkpoint exists.
9396  */
9397  if (PriorRedoPtr != InvalidXLogRecPtr)
9399 
9400  /*
9401  * Delete old log files, those no longer needed for last restartpoint to
9402  * prevent the disk holding the xlog from growing full.
9403  */
9405 
9406  /*
9407  * Retreat _logSegNo using the current end of xlog replayed or received,
9408  * whichever is later.
9409  */
9410  receivePtr = GetWalRcvFlushRecPtr(NULL, NULL);
9411  replayPtr = GetXLogReplayRecPtr(&replayTLI);
9412  endptr = (receivePtr < replayPtr) ? replayPtr : receivePtr;
9413  KeepLogSeg(endptr, &_logSegNo);
9415  _logSegNo--;
9416 
9417  /*
9418  * Try to recycle segments on a useful timeline. If we've been promoted
9419  * since the beginning of this restartpoint, use the new timeline chosen
9420  * at end of recovery (RecoveryInProgress() sets ThisTimeLineID in that
9421  * case). If we're still in recovery, use the timeline we're currently
9422  * replaying.
9423  *
9424  * There is no guarantee that the WAL segments will be useful on the
9425  * current timeline; if recovery proceeds to a new timeline right after
9426  * this, the pre-allocated WAL segments on this timeline will not be used,
9427  * and will go wasted until recycled on the next restartpoint. We'll live
9428  * with that.
9429  */
9430  if (RecoveryInProgress())
9431  ThisTimeLineID = replayTLI;
9432 
9433  RemoveOldXlogFiles(_logSegNo, RedoRecPtr, endptr);
9434 
9435  /*
9436  * Make more log segments if needed. (Do this after recycling old log
9437  * segments, since that may supply some of the needed files.)
9438  */
9439  PreallocXlogFiles(endptr);
9440 
9441  /*
9442  * ThisTimeLineID is normally not set when we're still in recovery.
9443  * However, recycling/preallocating segments above needed ThisTimeLineID
9444  * to determine which timeline to install the segments on. Reset it now,
9445  * to restore the normal state of affairs for debugging purposes.
9446  */
9447  if (RecoveryInProgress())
9448  ThisTimeLineID = 0;
9449 
9450  /*
9451  * Truncate pg_subtrans if possible. We can throw away all data before
9452  * the oldest XMIN of any running transaction. No future transaction will
9453  * attempt to reference any pg_subtrans entry older than that (see Asserts
9454  * in subtrans.c). When hot standby is disabled, though, we mustn't do
9455  * this because StartupSUBTRANS hasn't been called yet.
9456  */
9457  if (EnableHotStandby)
9459 
9460  /* Real work is done, but log and update before releasing lock. */
9461  LogCheckpointEnd(true);
9462 
9463  xtime = GetLatestXTime();
9465  (errmsg("recovery restart point at %X/%X",
9466  (uint32) (lastCheckPoint.redo >> 32), (uint32) lastCheckPoint.redo),
9467  xtime ? errdetail("Last completed transaction was at log time %s.",
9468  timestamptz_to_str(xtime)) : 0));
9469 
9470  LWLockRelease(CheckpointLock);
9471 
9472  /*
9473  * Finally, execute archive_cleanup_command, if any.
9474  */
9475  if (archiveCleanupCommand && strcmp(archiveCleanupCommand, "") != 0)
9477  "archive_cleanup_command",
9478  false);
9479 
9480  return true;
9481 }
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:8638
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:1583
int64 TimestampTz
Definition: timestamp.h:39
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
Definition: xlog.c:2757
TimestampTz ckpt_start_t
Definition: xlog.h:245
slock_t info_lck
Definition: xlog.c:727
#define MemSet(start, val, len)
Definition: c.h:971
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
Definition: xlog.c:9171
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6173
CheckPoint checkPointCopy
Definition: pg_control.h:131
XLogCtlInsert Insert
Definition: xlog.c:601
#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:696
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:8553
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11463
#define DEBUG2
Definition: elog.h:24
static XLogRecPtr RedoRecPtr
Definition: xlog.c:376
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:367
XLogRecPtr RedoRecPtr
Definition: xlog.c:605
CheckPoint lastCheckPoint
Definition: xlog.c:698
#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:50
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:9577
static ControlFileData * ControlFile
Definition: xlog.c:738
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:730
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:1090
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr)
Definition: xlog.c:4023
XLogRecPtr RedoRecPtr
Definition: xlog.c:574
XLogRecPtr lastCheckPointEndPtr
Definition: xlog.c:697
XLogRecPtr checkPoint
Definition: pg_control.h:129
XLogRecPtr redo
Definition: pg_control.h:37
static void LogCheckpointStart(int flags, bool restartpoint)
Definition: xlog.c:8535
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:1745
#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:738
#define Assert(condition)
Definition: c.h:738

◆ do_pg_abort_backup()

void do_pg_abort_backup ( int  code,
Datum  arg 
)

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

11416 {
11417  bool emit_warning = DatumGetBool(arg);
11418 
11419  /*
11420  * Quick exit if session is not keeping around a non-exclusive backup
11421  * already started.
11422  */
11424  return;
11425 
11429 
11432  {
11433  XLogCtl->Insert.forcePageWrites = false;
11434  }
11436 
11437  if (emit_warning)
11438  ereport(WARNING,
11439  (errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
11440 }
static void WALInsertLockRelease(void)
Definition: xlog.c:1726
static SessionBackupState sessionBackupState
Definition: xlog.c:536
XLogCtlInsert Insert
Definition: xlog.c:601
bool forcePageWrites
Definition: xlog.c:575
#define DatumGetBool(X)
Definition: postgres.h:393
#define WARNING
Definition: elog.h:40
int nonExclusiveBackups
Definition: xlog.c:587
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:586
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1697
static XLogCtlData * XLogCtl
Definition: xlog.c:730
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  infotbssize,
bool  needtblspcmapfile 
)

Definition at line 10474 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(), pgstat_progress_update_param(), PROGRESS_BASEBACKUP_PHASE, PROGRESS_BASEBACKUP_PHASE_ESTIMATE_BACKUP_SIZE, pstrdup(), ReadDir(), readlink, RecoveryInProgress(), CheckPoint::redo, relpath, RequestCheckpoint(), RequestXLogSwitch(), tablespaceinfo::rpath, sendTablespace(), 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().

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

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

References sessionBackupState.

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

10982 {
10983  return sessionBackupState;
10984 }
static SessionBackupState sessionBackupState
Definition: xlog.c:536

◆ 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:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:730
TimestampTz currentChunkStartTime
Definition: xlog.c:717

◆ 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:613
#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:730
slock_t ulsn_lck
Definition: xlog.c:614

◆ GetFlushRecPtr()

XLogRecPtr GetFlushRecPtr ( void  )

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

8421 {
8425 
8426  return LogwrtResult.Flush;
8427 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:774
slock_t info_lck
Definition: xlog.c:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:624
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:730
XLogRecPtr Flush
Definition: xlog.c:450

◆ GetFullPageWriteInfo()

void GetFullPageWriteInfo ( XLogRecPtr RedoRecPtr_p,
bool doPageWrites_p 
)

Definition at line 8389 of file xlog.c.

References doPageWrites, and RedoRecPtr.

Referenced by XLogCheckBufferNeedsBackup(), and XLogInsert().

8390 {
8391  *RedoRecPtr_p = RedoRecPtr;
8392  *doPageWrites_p = doPageWrites;
8393 }
static bool doPageWrites
Definition: xlog.c:383
static XLogRecPtr RedoRecPtr
Definition: xlog.c:376

◆ GetInsertRecPtr()

XLogRecPtr GetInsertRecPtr ( void  )

Definition at line 8404 of file xlog.c.

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

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

8405 {
8406  XLogRecPtr recptr;
8407 
8409  recptr = XLogCtl->LogwrtRqst.Write;
8411 
8412  return recptr;
8413 }
slock_t info_lck
Definition: xlog.c:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr Write
Definition: xlog.c:443
XLogwrtRqst LogwrtRqst
Definition: xlog.c:604
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:730

◆ GetLastImportantRecPtr()

XLogRecPtr GetLastImportantRecPtr ( void  )

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

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

◆ 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:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
TimestampTz recoveryLastXTime
Definition: xlog.c:711
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:730

◆ 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:738
#define Assert(condition)
Definition: c.h:738

◆ 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:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
RecoveryState SharedRecoveryState
Definition: xlog.c:660
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:730
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 master copy, someone might
8368  * update it just after we've released the lock.
8369  */
8371  ptr = XLogCtl->RedoRecPtr;
8373 
8374  if (RedoRecPtr < ptr)
8375  RedoRecPtr = ptr;
8376 
8377  return RedoRecPtr;
8378 }
slock_t info_lck
Definition: xlog.c:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
static XLogRecPtr RedoRecPtr
Definition: xlog.c:376
XLogRecPtr RedoRecPtr
Definition: xlog.c:605
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:730

◆ 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:738
#define Assert(condition)
Definition: c.h:738

◆ GetWALAvailability()

WALAvailability GetWALAvailability ( XLogRecPtr  restart_lsn)

Definition at line 9501 of file xlog.c.

References ConvertToXSegs, GetXLogWriteRecPtr(), InvalidXLogRecPtr, KeepLogSeg(), Max, max_slot_wal_keep_size_mb, max_wal_size_mb, wal_keep_segments, wal_segment_size, WALAVAIL_INVALID_LSN, WALAVAIL_NORMAL, WALAVAIL_REMOVED, WALAVAIL_RESERVED, XLByteToSeg, XLogGetLastRemovedSegno(), and XLogRecPtrIsInvalid.

Referenced by pg_get_replication_slots().

9502 {
9503  XLogRecPtr currpos; /* current write LSN */
9504  XLogSegNo currSeg; /* segid of currpos */
9505  XLogSegNo targetSeg; /* segid of targetLSN */
9506  XLogSegNo oldestSeg; /* actual oldest segid */
9507  XLogSegNo oldestSegMaxWalSize; /* oldest segid kept by max_wal_size */
9508  XLogSegNo oldestSlotSeg = InvalidXLogRecPtr; /* oldest segid kept by
9509  * slot */
9510  uint64 keepSegs;
9511 
9512  /* slot does not reserve WAL. Either deactivated, or has never been active */
9513  if (XLogRecPtrIsInvalid(targetLSN))
9514  return WALAVAIL_INVALID_LSN;
9515 
9516  currpos = GetXLogWriteRecPtr();
9517 
9518  /* calculate oldest segment currently needed by slots */
9519  XLByteToSeg(targetLSN, targetSeg, wal_segment_size);
9520  KeepLogSeg(currpos, &oldestSlotSeg);
9521 
9522  /*
9523  * Find the oldest extant segment file. We get 1 until checkpoint removes
9524  * the first WAL segment file since startup, which causes the status being
9525  * wrong under certain abnormal conditions but that doesn't actually harm.
9526  */
9527  oldestSeg = XLogGetLastRemovedSegno() + 1;
9528 
9529  /* calculate oldest segment by max_wal_size and wal_keep_segments */
9530  XLByteToSeg(currpos, currSeg, wal_segment_size);
9532  wal_segment_size) + 1;
9533 
9534  if (currSeg > keepSegs)
9535  oldestSegMaxWalSize = currSeg - keepSegs;
9536  else
9537  oldestSegMaxWalSize = 1;
9538 
9539  /*
9540  * If max_slot_wal_keep_size has changed after the last call, the segment
9541  * that would been kept by the current setting might have been lost by the
9542  * previous setting. No point in showing normal or keeping status values
9543  * if the targetSeg is known to be lost.
9544  */
9545  if (targetSeg >= oldestSeg)
9546  {
9547  /*
9548  * show "normal" when targetSeg is within max_wal_size, even if
9549  * max_slot_wal_keep_size is smaller than max_wal_size.
9550  */
9551  if ((max_slot_wal_keep_size_mb <= 0 ||
9553  oldestSegMaxWalSize <= targetSeg)
9554  return WALAVAIL_NORMAL;
9555 
9556  /* being retained by slots */
9557  if (oldestSlotSeg <= targetSeg)
9558  return WALAVAIL_RESERVED;
9559  }
9560 
9561  /* Definitely lost */
9562  return WALAVAIL_REMOVED;
9563 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define ConvertToXSegs(x, segsize)
Definition: xlog.c:764
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:9577
int max_slot_wal_keep_size_mb
Definition: xlog.c:110
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11498
#define Max(x, y)
Definition: c.h:914
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int wal_keep_segments
Definition: xlog.c:91
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ GetXLogInsertRecPtr()

XLogRecPtr GetXLogInsertRecPtr ( void  )

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

11483 {
11485  uint64 current_bytepos;
11486 
11487  SpinLockAcquire(&Insert->insertpos_lck);
11488  current_bytepos = Insert->CurrBytePos;
11489  SpinLockRelease(&Insert->insertpos_lck);
11490 
11491  return XLogBytePosToRecPtr(current_bytepos);
11492 }
slock_t insertpos_lck
Definition: xlog.c:543
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1995
XLogCtlInsert Insert
Definition: xlog.c:601
#define SpinLockAcquire(lock)
Definition: spin.h:62
uint64 CurrBytePos
Definition: xlog.c:552
static void Insert(File file)
Definition: fd.c:1172
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:730

◆ 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:738

◆ GetXLogReplayRecPtr()

XLogRecPtr GetXLogReplayRecPtr ( TimeLineID replayTLI)

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

11464 {
11465  XLogRecPtr recptr;
11466  TimeLineID tli;
11467 
11469  recptr = XLogCtl->lastReplayedEndRecPtr;
11470  tli = XLogCtl->lastReplayedTLI;
11472 
11473  if (replayTLI)
11474  *replayTLI = tli;
11475  return recptr;
11476 }
uint32 TimeLineID
Definition: xlogdefs.h:52
slock_t info_lck
Definition: xlog.c:727
#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:730
TimeLineID lastReplayedTLI
Definition: xlog.c:707
XLogRecPtr lastReplayedEndRecPtr
Definition: xlog.c:706

◆ GetXLogWriteRecPtr()

XLogRecPtr GetXLogWriteRecPtr ( void  )

Definition at line 11498 of file xlog.c.

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

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

11499 {
11503 
11504  return LogwrtResult.Write;
11505 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:774
slock_t info_lck
Definition: xlog.c:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:624
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:730
XLogRecPtr Write
Definition: xlog.c:449

◆ 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:666
slock_t info_lck
Definition: xlog.c:727
#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:730

◆ 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:430
bool IsPostmasterEnvironment
Definition: globals.c:108
static bool LocalHotStandbyActive
Definition: xlog.c:234
#define Assert(condition)
Definition: c.h:738

◆ 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:1121
TimeLineID ThisTimeLineID
Definition: xlog.c:653
XLogCtlInsert Insert
Definition: xlog.c:601
bool fullPageWrites
Definition: xlog.c:576
uint32 xlog_seg_size
Definition: pg_control.h:209
static bool doPageWrites
Definition: xlog.c:383
bool forcePageWrites
Definition: xlog.c:575
static void Insert(File file)
Definition: fd.c:1172
static ControlFileData * ControlFile
Definition: xlog.c:738
TimeLineID ThisTimeLineID
Definition: xlog.c:191
#define Assert(condition)
Definition: c.h:738
static XLogCtlData * XLogCtl
Definition: xlog.c:730
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8361
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:392

◆ issue_xlog_fsync()

void issue_xlog_fsync ( int  fd,
XLogSegNo  segno 
)

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

10382 {
10383  char *msg = NULL;
10384 
10386  switch (sync_method)
10387  {
10388  case SYNC_METHOD_FSYNC:
10389  if (pg_fsync_no_writethrough(fd) != 0)
10390  msg = _("could not fsync file \"%s\": %m");
10391  break;
10392 #ifdef HAVE_FSYNC_WRITETHROUGH
10394  if (pg_fsync_writethrough(fd) != 0)
10395  msg = _("could not fsync write-through file \"%s\": %m");
10396  break;
10397 #endif
10398 #ifdef HAVE_FDATASYNC
10399  case SYNC_METHOD_FDATASYNC:
10400  if (pg_fdatasync(fd) != 0)
10401  msg = _("could not fdatasync file \"%s\": %m");
10402  break;
10403 #endif
10404  case SYNC_METHOD_OPEN:
10406  /* write synced it already */
10407  break;
10408  default:
10409  elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
10410  break;
10411  }
10412 
10413  /* PANIC if failed to fsync */
10414  if (msg)
10415  {
10416  char xlogfname[MAXFNAMELEN];
10417  int save_errno = errno;
10418 
10419  XLogFileName(xlogfname, ThisTimeLineID, segno,
10421  errno = save_errno;
10422  ereport(PANIC,
10424  errmsg(msg, xlogfname)));
10425  }
10426 
10428 }
int pg_fdatasync(int fd)
Definition: fd.c:433
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:410
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:398
#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:1380
#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:1356
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:738
#define Assert(condition)
Definition: c.h:738
void * palloc(Size size)
Definition: mcxt.c:949

◆ PromoteIsTriggered()

bool PromoteIsTriggered ( void  )

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

12542 {
12543  /*
12544  * We check shared state each time only until a standby promotion is
12545  * triggered. We can't trigger a promotion again, so there's no need to
12546  * keep checking after the shared variable has once been seen true.
12547  */
12549  return true;
12550 
12554 
12555  return LocalPromoteIsTriggered;
12556 }
slock_t info_lck
Definition: xlog.c:727
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:730
bool SharedPromoteIsTriggered
Definition: xlog.c:672

◆ 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:660
#define pg_memory_barrier()
Definition: atomics.h:145
static XLogCtlData * XLogCtl
Definition: xlog.c:730
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:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:719
static XLogCtlData * XLogCtl
Definition: xlog.c:730

◆ register_persistent_abort_backup_handler()

void register_persistent_abort_backup_handler ( void  )

Definition at line 11447 of file xlog.c.

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

Referenced by pg_start_backup().

11448 {
11449  static bool already_done = false;
11450 
11451  if (already_done)
11452  return;
11454  already_done = true;
11455 }
void do_pg_abort_backup(int code, Datum arg)
Definition: xlog.c:11415
#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 12633 of file xlog.c.

References FALLBACK_PROMOTE_SIGNAL_FILE, and PROMOTE_SIGNAL_FILE.

Referenced by PostmasterMain().

12634 {
12635  unlink(PROMOTE_SIGNAL_FILE);
12637 }
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:393
#define FALLBACK_PROMOTE_SIGNAL_FILE
Definition: xlog.h:394

◆ 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:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:719
static XLogCtlData * XLogCtl
Definition: xlog.c:730

◆ SetWalWriterSleeping()

void SetWalWriterSleeping ( bool  sleeping)

Definition at line 12669 of file xlog.c.

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

Referenced by WalWriterMain().

12670 {
12672  XLogCtl->WalWriterSleeping = sleeping;
12674 }
slock_t info_lck
Definition: xlog.c:727
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool WalWriterSleeping
Definition: xlog.c:679
static XLogCtlData * XLogCtl
Definition: xlog.c:730

◆ ShutdownXLOG()

void ShutdownXLOG ( int  code,
Datum  arg 
)

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

8485 {
8486  /*
8487  * We should have an aux process resource owner to use, and we should not
8488  * be in a transaction that's installed some other resowner.
8489  */
8491  Assert(CurrentResourceOwner == NULL ||
8494 
8495  /* Don't be chatty in standalone mode */
8497  (errmsg("shutting down")));
8498 
8499  /*
8500  * Signal walsenders to move to stopping state.
8501  */
8503 
8504  /*
8505  * Wait for WAL senders to be in stopping state. This prevents commands
8506  * from writing new WAL.
8507  */
8509 
8510  if (RecoveryInProgress())
8512  else
8513  {
8514  /*
8515  * If archiving is enabled, rotate the last XLOG file so that all the
8516  * remaining records are archived (postmaster wakes up the archiver
8517  * process one more time at the end of shutdown). The checkpoint
8518  * record will go to the next XLOG file and won't be archived (yet).
8519  */
8521  RequestXLogSwitch(false);
8522 
8524  }
8525  ShutdownCLOG();
8526  ShutdownCommitTs();
8527  ShutdownSUBTRANS();
8529 }
bool IsPostmasterEnvironment
Definition: globals.c:108
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9664
void ShutdownSUBTRANS(void)
Definition: subtrans.c:285
void CreateCheckPoint(int flags)
Definition: xlog.c:8700
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ShutdownCLOG(void)
Definition: clog.c:816
bool CreateRestartPoint(int flags)
Definition: xlog.c:9241
#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:3167
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:3141
void ShutdownCommitTs(void)
Definition: commit_ts.c:744
#define Assert(condition)
Definition: c.h:738
#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 12490 of file xlog.c.

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

Referenced by StartupRereadConfig().

12491 {
12493  {
12494  ereport(LOG,
12495  (errmsg("wal receiver process shutdown requested")));
12496 
12497  pendingWalRcvRestart = true;
12498  }
12499 }
#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(), fast_promote, 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, LocalSetXLogInsertAllowed(), LocalXLogInsertAllowed, LOG, XLogCtlData::LogwrtResult, LogwrtResult, XLogCtlData::LogwrtRqst, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), master_image_masked, 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, 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 fast_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  master_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 rec