PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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_CAUSE_XLOG   0x0040 /* XLOG consumption */
 
#define CHECKPOINT_CAUSE_TIME   0x0080 /* Elapsed time */
 
#define XLOG_INCLUDE_ORIGIN   0x01 /* include the replication origin */
 
#define XLOG_MARK_UNIMPORTANT   0x02 /* record not important for durability */
 
#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"
 

Typedefs

typedef enum ArchiveMode ArchiveMode
 
typedef enum WalLevel WalLevel
 
typedef struct CheckpointStatsData CheckpointStatsData
 
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  ArchiveMode { ARCHIVE_MODE_OFF = 0, ARCHIVE_MODE_ON, ARCHIVE_MODE_ALWAYS }
 
enum  WalLevel { WAL_LEVEL_MINIMAL = 0, WAL_LEVEL_REPLICA, WAL_LEVEL_LOGICAL }
 
enum  SessionBackupState { SESSION_BACKUP_NONE, SESSION_BACKUP_EXCLUSIVE, SESSION_BACKUP_NON_EXCLUSIVE }
 

Functions

XLogRecPtr XLogInsertRecord (struct XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags)
 
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)
 
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)
 
char * XLogFileNameP (TimeLineID tli, XLogSegNo segno)
 
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 StartupXLOG (void)
 
void ShutdownXLOG (int code, Datum arg)
 
void InitXLOGAccess (void)
 
void CreateCheckPoint (int flags)
 
bool CreateRestartPoint (int flags)
 
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 GetNextXidAndEpoch (TransactionId *xid, uint32 *epoch)
 
void RemovePromoteSignalFiles (void)
 
bool CheckPromoteSignal (void)
 
void WakeupRecovery (void)
 
void SetWalWriterSleeping (bool sleeping)
 
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, DIR *tblspcdir, 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 (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 min_wal_size_mb
 
int max_wal_size_mb
 
int wal_keep_segments
 
int XLOGbuffers
 
int XLogArchiveTimeout
 
int wal_retrieve_retry_interval
 
char * XLogArchiveCommand
 
bool EnableHotStandby
 
bool fullPageWrites
 
bool wal_log_hints
 
bool wal_compression
 
boolwal_consistency_checking
 
char * wal_consistency_checking_string
 
bool log_checkpoints
 
int CheckPointSegments
 
int XLogArchiveMode
 
PGDLLIMPORT int wal_level
 
CheckpointStatsData CheckpointStats
 

Macro Definition Documentation

#define BACKUP_LABEL_FILE   "backup_label"
#define BACKUP_LABEL_OLD   "backup_label.old"

Definition at line 322 of file xlog.h.

Referenced by CancelBackup(), and StartupXLOG().

#define CHECKPOINT_CAUSE_TIME   0x0080 /* Elapsed time */

Definition at line 187 of file xlog.h.

Referenced by CheckpointerMain(), and LogCheckpointStart().

#define CHECKPOINT_CAUSE_XLOG   0x0040 /* XLOG consumption */

Definition at line 186 of file xlog.h.

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

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

Definition at line 176 of file xlog.h.

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

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

Definition at line 181 of file xlog.h.

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

#define CHECKPOINT_FORCE   0x0008 /* Force even if no activity */
#define CHECKPOINT_IS_SHUTDOWN   0x0001 /* Checkpoint is for shutdown */
#define CHECKPOINT_WAIT   0x0020 /* Wait for completion */
#define SYNC_METHOD_FDATASYNC   1

Definition at line 26 of file xlog.h.

Referenced by get_sync_bit(), and issue_xlog_fsync().

#define SYNC_METHOD_FSYNC   0

Definition at line 25 of file xlog.h.

Referenced by get_sync_bit(), and issue_xlog_fsync().

#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().

#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().

#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().

#define TABLESPACE_MAP   "tablespace_map"
#define TABLESPACE_MAP_OLD   "tablespace_map.old"

Definition at line 325 of file xlog.h.

Referenced by CancelBackup(), and StartupXLOG().

#define XLOG_INCLUDE_ORIGIN   0x01 /* include the replication origin */
#define XLOG_MARK_UNIMPORTANT   0x02 /* record not important for durability */
#define XLogArchiveCommandSet ( )    (XLogArchiveCommand[0] != '\0')

Definition at line 139 of file xlog.h.

Referenced by pgarch_ArchiverCopyLoop(), and ShutdownXLOG().

Definition at line 137 of file xlog.h.

Referenced by sigusr1_handler().

#define XLogLogicalInfoActive ( )    (wal_level >= WAL_LEVEL_LOGICAL)

Definition at line 162 of file xlog.h.

Referenced by AssignTransactionId(), and XactLogCommitRecord().

Typedef Documentation

Enumeration Type Documentation

Enumerator
ARCHIVE_MODE_OFF 
ARCHIVE_MODE_ON 
ARCHIVE_MODE_ALWAYS 

Definition at line 115 of file xlog.h.

116 {
117  ARCHIVE_MODE_OFF = 0, /* disabled */
118  ARCHIVE_MODE_ON, /* enabled while server is running normally */
119  ARCHIVE_MODE_ALWAYS /* enabled always (even during recovery) */
120 } ArchiveMode;
ArchiveMode
Definition: xlog.h:115
Enumerator
STANDBY_DISABLED 
STANDBY_INITIALIZED 
STANDBY_SNAPSHOT_PENDING 
STANDBY_SNAPSHOT_READY 

Definition at line 64 of file xlog.h.

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.

Enumerator
SESSION_BACKUP_NONE 
SESSION_BACKUP_EXCLUSIVE 
SESSION_BACKUP_NON_EXCLUSIVE 

Definition at line 304 of file xlog.h.

enum WalLevel
Enumerator
WAL_LEVEL_MINIMAL 
WAL_LEVEL_REPLICA 
WAL_LEVEL_LOGICAL 

Definition at line 124 of file xlog.h.

Function Documentation

void assign_checkpoint_completion_target ( double  newval,
void *  extra 
)

Definition at line 2241 of file xlog.c.

References CalculateCheckpointSegments(), CheckPointCompletionTarget, and newval.

2242 {
2245 }
static void CalculateCheckpointSegments(void)
Definition: xlog.c:2211
#define newval
double CheckPointCompletionTarget
Definition: checkpointer.c:147
void assign_max_wal_size ( int  newval,
void *  extra 
)

Definition at line 2234 of file xlog.c.

References CalculateCheckpointSegments(), max_wal_size_mb, and newval.

2235 {
2238 }
static void CalculateCheckpointSegments(void)
Definition: xlog.c:2211
int max_wal_size_mb
Definition: xlog.c:89
#define newval
void BootStrapXLOG ( void  )

Definition at line 4946 of file xlog.c.

References AdvanceOldestClogXid(), Assert, bootstrap_data_checksum_version, BootStrapCLOG(), BootStrapCommitTs(), BootStrapMultiXact(), BootStrapSUBTRANS(), buffer, ControlFileData::checkPoint, ControlFileData::checkPointCopy, close, COMP_CRC32C, ControlFileData::data_checksum_version, DB_SHUTDOWNED, ereport, errcode(), errcode_for_file_access(), errmsg(), FIN_CRC32C, FirstBootstrapObjectId, FirstMultiXactId, FirstNormalTransactionId, CheckPoint::fullPageWrites, fullPageWrites, gettimeofday(), INIT_CRC32C, InvalidTransactionId, max_locks_per_xact, ControlFileData::max_locks_per_xact, max_prepared_xacts, ControlFileData::max_prepared_xacts, max_worker_processes, ControlFileData::max_worker_processes, MaxConnections, ControlFileData::MaxConnections, MOCK_AUTH_NONCE_LEN, ControlFileData::mock_authentication_nonce, MultiXactSetNextMXact(), CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, VariableCacheData::nextOid, CheckPoint::nextXid, VariableCacheData::nextXid, CheckPoint::nextXidEpoch, NULL, offsetof, VariableCacheData::oidCount, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, CheckPoint::oldestXid, CheckPoint::oldestXidDB, openLogFile, palloc(), PANIC, pfree(), pg_backend_random(), pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), CheckPoint::PrevTimeLineID, CheckPoint::redo, SetCommitTsLimit(), SetMultiXactIdLimit(), SetTransactionIdLimit(), ShmemVariableCache, SizeOfXLogLongPHD, SizeOfXLogRecord, SizeOfXLogRecordDataHeaderShort, ControlFileData::state, ControlFileData::system_identifier, TemplateDbOid, CheckPoint::ThisTimeLineID, ThisTimeLineID, CheckPoint::time, ControlFileData::time, track_commit_timestamp, ControlFileData::track_commit_timestamp, TYPEALIGN, ControlFileData::unloggedLSN, WAIT_EVENT_WAL_BOOTSTRAP_SYNC, WAIT_EVENT_WAL_BOOTSTRAP_WRITE, wal_level, ControlFileData::wal_level, wal_log_hints, ControlFileData::wal_log_hints, write, WriteControlFile(), XLogRecord::xl_crc, XLogRecord::xl_info, XLogRecord::xl_prev, XLogRecord::xl_rmid, XLogRecord::xl_tot_len, XLogRecord::xl_xid, XLOG_CHECKPOINT_SHUTDOWN, XLOG_PAGE_MAGIC, XLogFileInit(), XLogSegSize, 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().

4947 {
4948  CheckPoint checkPoint;
4949  char *buffer;
4950  XLogPageHeader page;
4951  XLogLongPageHeader longpage;
4952  XLogRecord *record;
4953  char *recptr;
4954  bool use_existent;
4955  uint64 sysidentifier;
4956  char mock_auth_nonce[MOCK_AUTH_NONCE_LEN];
4957  struct timeval tv;
4958  pg_crc32c crc;
4959 
4960  /*
4961  * Select a hopefully-unique system identifier code for this installation.
4962  * We use the result of gettimeofday(), including the fractional seconds
4963  * field, as being about as unique as we can easily get. (Think not to
4964  * use random(), since it hasn't been seeded and there's no portable way
4965  * to seed it other than the system clock value...) The upper half of the
4966  * uint64 value is just the tv_sec part, while the lower half contains the
4967  * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
4968  * PID for a little extra uniqueness. A person knowing this encoding can
4969  * determine the initialization time of the installation, which could
4970  * perhaps be useful sometimes.
4971  */
4972  gettimeofday(&tv, NULL);
4973  sysidentifier = ((uint64) tv.tv_sec) << 32;
4974  sysidentifier |= ((uint64) tv.tv_usec) << 12;
4975  sysidentifier |= getpid() & 0xFFF;
4976 
4977  /*
4978  * Generate a random nonce. This is used for authentication requests that
4979  * will fail because the user does not exist. The nonce is used to create
4980  * a genuine-looking password challenge for the non-existent user, in lieu
4981  * of an actual stored password.
4982  */
4983  if (!pg_backend_random(mock_auth_nonce, MOCK_AUTH_NONCE_LEN))
4984  ereport(PANIC,
4985  (errcode(ERRCODE_INTERNAL_ERROR),
4986  errmsg("could not generate secret authorization token")));
4987 
4988  /* First timeline ID is always 1 */
4989  ThisTimeLineID = 1;
4990 
4991  /* page buffer must be aligned suitably for O_DIRECT */
4992  buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
4993  page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
4994  memset(page, 0, XLOG_BLCKSZ);
4995 
4996  /*
4997  * Set up information for the initial checkpoint record
4998  *
4999  * The initial checkpoint record is written to the beginning of the WAL
5000  * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not
5001  * used, so that we can use 0/0 to mean "before any valid WAL segment".
5002  */
5003  checkPoint.redo = XLogSegSize + SizeOfXLogLongPHD;
5004  checkPoint.ThisTimeLineID = ThisTimeLineID;
5005  checkPoint.PrevTimeLineID = ThisTimeLineID;
5006  checkPoint.fullPageWrites = fullPageWrites;
5007  checkPoint.nextXidEpoch = 0;
5008  checkPoint.nextXid = FirstNormalTransactionId;
5009  checkPoint.nextOid = FirstBootstrapObjectId;
5010  checkPoint.nextMulti = FirstMultiXactId;
5011  checkPoint.nextMultiOffset = 0;
5012  checkPoint.oldestXid = FirstNormalTransactionId;
5013  checkPoint.oldestXidDB = TemplateDbOid;
5014  checkPoint.oldestMulti = FirstMultiXactId;
5015  checkPoint.oldestMultiDB = TemplateDbOid;
5018  checkPoint.time = (pg_time_t) time(NULL);
5020 
5021  ShmemVariableCache->nextXid = checkPoint.nextXid;
5022  ShmemVariableCache->nextOid = checkPoint.nextOid;
5024  MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5025  AdvanceOldestClogXid(checkPoint.oldestXid);
5026  SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5027  SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5029 
5030  /* Set up the XLOG page header */
5031  page->xlp_magic = XLOG_PAGE_MAGIC;
5032  page->xlp_info = XLP_LONG_HEADER;
5033  page->xlp_tli = ThisTimeLineID;
5034  page->xlp_pageaddr = XLogSegSize;
5035  longpage = (XLogLongPageHeader) page;
5036  longpage->xlp_sysid = sysidentifier;
5037  longpage->xlp_seg_size = XLogSegSize;
5038  longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
5039 
5040  /* Insert the initial checkpoint record */
5041  recptr = ((char *) page + SizeOfXLogLongPHD);
5042  record = (XLogRecord *) recptr;
5043  record->xl_prev = 0;
5044  record->xl_xid = InvalidTransactionId;
5045  record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(checkPoint);
5047  record->xl_rmid = RM_XLOG_ID;
5048  recptr += SizeOfXLogRecord;
5049  /* fill the XLogRecordDataHeaderShort struct */
5050  *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
5051  *(recptr++) = sizeof(checkPoint);
5052  memcpy(recptr, &checkPoint, sizeof(checkPoint));
5053  recptr += sizeof(checkPoint);
5054  Assert(recptr - (char *) record == record->xl_tot_len);
5055 
5056  INIT_CRC32C(crc);
5057  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
5058  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
5059  FIN_CRC32C(crc);
5060  record->xl_crc = crc;
5061 
5062  /* Create first XLOG segment file */
5063  use_existent = false;
5064  openLogFile = XLogFileInit(1, &use_existent, false);
5065 
5066  /* Write the first page with the initial record */
5067  errno = 0;
5069  if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
5070  {
5071  /* if write didn't set errno, assume problem is no disk space */
5072  if (errno == 0)
5073  errno = ENOSPC;
5074  ereport(PANIC,
5076  errmsg("could not write bootstrap write-ahead log file: %m")));
5077  }
5079 
5081  if (pg_fsync(openLogFile) != 0)
5082  ereport(PANIC,
5084  errmsg("could not fsync bootstrap write-ahead log file: %m")));
5086 
5087  if (close(openLogFile))
5088  ereport(PANIC,
5090  errmsg("could not close bootstrap write-ahead log file: %m")));
5091 
5092  openLogFile = -1;
5093 
5094  /* Now create pg_control */
5095 
5096  memset(ControlFile, 0, sizeof(ControlFileData));
5097  /* Initialize pg_control status fields */
5098  ControlFile->system_identifier = sysidentifier;
5099  memcpy(ControlFile->mock_authentication_nonce, mock_auth_nonce, MOCK_AUTH_NONCE_LEN);
5101  ControlFile->time = checkPoint.time;
5102  ControlFile->checkPoint = checkPoint.redo;
5103  ControlFile->checkPointCopy = checkPoint;
5104  ControlFile->unloggedLSN = 1;
5105 
5106  /* Set important parameter values for use when replaying WAL */
5115 
5116  /* some additional ControlFile fields are set in WriteControlFile() */
5117 
5118  WriteControlFile();
5119 
5120  /* Bootstrap the commit log, too */
5121  BootStrapCLOG();
5125 
5126  pfree(buffer);
5127 }
static void WriteControlFile(void)
Definition: xlog.c:4377
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define XLogSegSize
Definition: xlog_internal.h:92
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
int max_locks_per_xact
Definition: pg_control.h:185
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
int max_prepared_xacts
Definition: pg_control.h:184
int64 pg_time_t
Definition: pgtime.h:23
pg_time_t time
Definition: pg_control.h:131
void SetCommitTsLimit(TransactionId oldestXact, TransactionId newestXact)
Definition: commit_ts.c:849
uint32 oidCount
Definition: transam.h:112
#define write(a, b, c)
Definition: win32.h:14
#define SizeOfXLogRecordDataHeaderShort
Definition: xlogrecord.h:200
int max_worker_processes
Definition: pg_control.h:183
uint32 pg_crc32c
Definition: pg_crc32c.h:38
TransactionId oldestActiveXid
Definition: pg_control.h:62
int wal_level
Definition: xlog.c:104
int XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
Definition: xlog.c:3154
void BootStrapMultiXact(void)
Definition: multixact.c:1866
MultiXactId oldestMulti
Definition: pg_control.h:48
TimeLineID PrevTimeLineID
Definition: pg_control.h:38
int errcode(int sqlerrcode)
Definition: elog.c:575
RmgrId xl_rmid
Definition: xlogrecord.h:47
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
CheckPoint checkPointCopy
Definition: pg_control.h:135
TransactionId oldestXid
Definition: pg_control.h:46
TransactionId nextXid
Definition: pg_control.h:42
pg_time_t time
Definition: pg_control.h:50
#define PANIC
Definition: elog.h:53
uint32 bootstrap_data_checksum_version
Definition: bootstrap.c:49
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
#define MOCK_AUTH_NONCE_LEN
Definition: pg_control.h:23
bool fullPageWrites
Definition: xlog.c:97
void BootStrapSUBTRANS(void)
Definition: subtrans.c:211
MultiXactOffset nextMultiOffset
Definition: pg_control.h:45
void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid)
Definition: varsup.c:271
TransactionId oldestCommitTsXid
Definition: pg_control.h:51
void pfree(void *pointer)
Definition: mcxt.c:950
#define FirstNormalTransactionId
Definition: transam.h:34
int max_prepared_xacts
Definition: twophase.c:117
uint64 system_identifier
Definition: pg_control.h:109
uint32 xl_tot_len
Definition: xlogrecord.h:43
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
TransactionId nextXid
Definition: transam.h:117
uint32 nextXidEpoch
Definition: pg_control.h:41
bool track_commit_timestamp
Definition: commit_ts.c:103
#define TemplateDbOid
Definition: pg_database.h:80
uint32 data_checksum_version
Definition: pg_control.h:225
bool pg_backend_random(char *dst, int len)
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:66
XLogRecPtr unloggedLSN
Definition: pg_control.h:137
int errcode_for_file_access(void)
Definition: elog.c:598
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
#define FirstBootstrapObjectId
Definition: transam.h:93
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1232
#define FirstMultiXactId
Definition: multixact.h:24
#define ereport(elevel, rest)
Definition: elog.h:122
int max_locks_per_xact
Definition: lock.c:54
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:53
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:232
int MaxConnections
Definition: globals.c:123
Oid oldestMultiDB
Definition: pg_control.h:49
static int openLogFile
Definition: xlog.c:771
static ControlFileData * ControlFile
Definition: xlog.c:714
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2194
TimeLineID ThisTimeLineID
Definition: xlog.c:179
Oid nextOid
Definition: pg_control.h:43
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:581
bool fullPageWrites
Definition: pg_control.h:40
bool wal_log_hints
Definition: xlog.c:98
void BootStrapCLOG(void)
Definition: clog.c:464
#define NULL
Definition: c.h:229
bool track_commit_timestamp
Definition: pg_control.h:186
#define Assert(condition)
Definition: c.h:675
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
Oid oldestXidDB
Definition: pg_control.h:47
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:211
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1208
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:288
uint8 xl_info
Definition: xlogrecord.h:46
MultiXactId nextMulti
Definition: pg_control.h:44
pg_crc32c xl_crc
Definition: xlogrecord.h:49
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:223
TransactionId xl_xid
Definition: xlogrecord.h:44
TimeLineID ThisTimeLineID
Definition: pg_control.h:37
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int max_worker_processes
Definition: globals.c:124
int pg_fsync(int fd)
Definition: fd.c:333
#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:73
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:78
XLogRecPtr checkPoint
Definition: pg_control.h:132
XLogRecPtr redo
Definition: pg_control.h:35
#define offsetof(type, field)
Definition: c.h:555
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72
void MultiXactSetNextMXact(MultiXactId nextMulti, MultiXactOffset nextMultiOffset)
Definition: multixact.c:2160
bool CheckPromoteSignal ( void  )

Definition at line 12076 of file xlog.c.

References FALLBACK_PROMOTE_SIGNAL_FILE, and PROMOTE_SIGNAL_FILE.

Referenced by sigusr1_handler().

12077 {
12078  struct stat stat_buf;
12079 
12080  if (stat(PROMOTE_SIGNAL_FILE, &stat_buf) == 0 ||
12082  return true;
12083 
12084  return false;
12085 }
#define FALLBACK_PROMOTE_SIGNAL_FILE
Definition: xlog.c:85
struct stat stat_buf
Definition: pg_standby.c:101
#define PROMOTE_SIGNAL_FILE
Definition: xlog.c:84
void CheckXLogRemoved ( XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 3766 of file xlog.c.

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

Referenced by perform_base_backup(), and XLogRead().

3767 {
3768  XLogSegNo lastRemovedSegNo;
3769 
3771  lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3773 
3774  if (segno <= lastRemovedSegNo)
3775  {
3776  char filename[MAXFNAMELEN];
3777 
3778  XLogFileName(filename, tli, segno);
3779  ereport(ERROR,
3781  errmsg("requested WAL segment %s has already been removed",
3782  filename)));
3783  }
3784 }
slock_t info_lck
Definition: xlog.c:703
#define XLogFileName(fname, tli, logSegNo)
XLogSegNo lastRemovedSegNo
Definition: xlog.c:585
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
uint64 XLogSegNo
Definition: xlogdefs.h:34
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define MAXFNAMELEN
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
static char * filename
Definition: pg_dumpall.c:89
int errmsg(const char *fmt,...)
Definition: elog.c:797
void CreateCheckPoint ( int  flags)

Definition at line 8523 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::ckptXid, XLogCtlData::ckptXidEpoch, 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, InvalidTransactionId, InvalidXLogRecPtr, KeepLogSeg(), LocalSetXLogInsertAllowed(), LocalXLogInsertAllowed, log_checkpoints, LogCheckpointEnd(), LogCheckpointStart(), LogStandbySnapshot(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemSet, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, MultiXactGetCheckptMulti(), NBuffers, CheckPoint::newestCommitTsXid, VariableCacheData::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, VariableCacheData::nextOid, CheckPoint::nextXid, VariableCacheData::nextXid, CheckPoint::nextXidEpoch, NULL, 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(), ControlFileData::prevCheckPoint, CheckPoint::PrevTimeLineID, XLogCtlData::PrevTimeLineID, PROCARRAY_FLAGS_DEFAULT, ProcLastRecPtr, RecoveryInProgress(), CheckPoint::redo, RedoRecPtr, XLogCtlInsert::RedoRecPtr, XLogCtlData::RedoRecPtr, RemoveOldXlogFiles(), ShmemVariableCache, SizeOfXLogLongPHD, SizeOfXLogShortPHD, smgrpostckpt(), smgrpreckpt(), SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, ControlFileData::state, CheckPoint::ThisTimeLineID, ThisTimeLineID, CheckPoint::time, ControlFileData::time, TruncateSUBTRANS(), XLogCtlData::ulsn_lck, ControlFileData::unloggedLSN, XLogCtlData::unloggedLSN, UpdateCheckPointDistanceEstimate(), UpdateControlFile(), WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLByteToSeg, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_SHUTDOWN, XLogBeginInsert(), XLogBytePosToRecPtr(), XLogFlush(), XLogInsert(), XLogRegisterData(), XLogSegSize, and XLogStandbyInfoActive.

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

8524 {
8525  bool shutdown;
8526  CheckPoint checkPoint;
8527  XLogRecPtr recptr;
8529  uint32 freespace;
8530  XLogRecPtr PriorRedoPtr;
8531  XLogRecPtr curInsert;
8532  XLogRecPtr last_important_lsn;
8533  VirtualTransactionId *vxids;
8534  int nvxids;
8535 
8536  /*
8537  * An end-of-recovery checkpoint is really a shutdown checkpoint, just
8538  * issued at a different time.
8539  */
8541  shutdown = true;
8542  else
8543  shutdown = false;
8544 
8545  /* sanity check */
8546  if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
8547  elog(ERROR, "can't create a checkpoint during recovery");
8548 
8549  /*
8550  * Initialize InitXLogInsert working areas before entering the critical
8551  * section. Normally, this is done by the first call to
8552  * RecoveryInProgress() or LocalSetXLogInsertAllowed(), but when creating
8553  * an end-of-recovery checkpoint, the LocalSetXLogInsertAllowed call is
8554  * done below in a critical section, and InitXLogInsert cannot be called
8555  * in a critical section.
8556  */
8557  InitXLogInsert();
8558 
8559  /*
8560  * Acquire CheckpointLock to ensure only one checkpoint happens at a time.
8561  * (This is just pro forma, since in the present system structure there is
8562  * only one process that is allowed to issue checkpoints at any given
8563  * time.)
8564  */
8565  LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
8566 
8567  /*
8568  * Prepare to accumulate statistics.
8569  *
8570  * Note: because it is possible for log_checkpoints to change while a
8571  * checkpoint proceeds, we always accumulate stats, even if
8572  * log_checkpoints is currently off.
8573  */
8574  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
8576 
8577  /*
8578  * Use a critical section to force system panic if we have trouble.
8579  */
8581 
8582  if (shutdown)
8583  {
8584  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8586  ControlFile->time = (pg_time_t) time(NULL);
8588  LWLockRelease(ControlFileLock);
8589  }
8590 
8591  /*
8592  * Let smgr prepare for checkpoint; this has to happen before we determine
8593  * the REDO pointer. Note that smgr must not do anything that'd have to
8594  * be undone if we decide no checkpoint is needed.
8595  */
8596  smgrpreckpt();
8597 
8598  /* Begin filling in the checkpoint WAL record */
8599  MemSet(&checkPoint, 0, sizeof(checkPoint));
8600  checkPoint.time = (pg_time_t) time(NULL);
8601 
8602  /*
8603  * For Hot Standby, derive the oldestActiveXid before we fix the redo
8604  * pointer. This allows us to begin accumulating changes to assemble our
8605  * starting snapshot of locks and transactions.
8606  */
8607  if (!shutdown && XLogStandbyInfoActive())
8609  else
8611 
8612  /*
8613  * Get location of last important record before acquiring insert locks (as
8614  * GetLastImportantRecPtr() also locks WAL locks).
8615  */
8616  last_important_lsn = GetLastImportantRecPtr();
8617 
8618  /*
8619  * We must block concurrent insertions while examining insert state to
8620  * determine the checkpoint REDO pointer.
8621  */
8623  curInsert = XLogBytePosToRecPtr(Insert->CurrBytePos);
8624 
8625  /*
8626  * If this isn't a shutdown or forced checkpoint, and if there has been no
8627  * WAL activity requiring a checkpoint, skip it. The idea here is to
8628  * avoid inserting duplicate checkpoints when the system is idle.
8629  */
8630  if ((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
8631  CHECKPOINT_FORCE)) == 0)
8632  {
8633  if (last_important_lsn == ControlFile->checkPoint)
8634  {
8636  LWLockRelease(CheckpointLock);
8637  END_CRIT_SECTION();
8638  ereport(DEBUG1,
8639  (errmsg("checkpoint skipped due to an idle system")));
8640  return;
8641  }
8642  }
8643 
8644  /*
8645  * An end-of-recovery checkpoint is created before anyone is allowed to
8646  * write WAL. To allow us to write the checkpoint record, temporarily
8647  * enable XLogInsertAllowed. (This also ensures ThisTimeLineID is
8648  * initialized, which we need here and in AdvanceXLInsertBuffer.)
8649  */
8650  if (flags & CHECKPOINT_END_OF_RECOVERY)
8652 
8653  checkPoint.ThisTimeLineID = ThisTimeLineID;
8654  if (flags & CHECKPOINT_END_OF_RECOVERY)
8655  checkPoint.PrevTimeLineID = XLogCtl->PrevTimeLineID;
8656  else
8657  checkPoint.PrevTimeLineID = ThisTimeLineID;
8658 
8659  checkPoint.fullPageWrites = Insert->fullPageWrites;
8660 
8661  /*
8662  * Compute new REDO record ptr = location of next XLOG record.
8663  *
8664  * NB: this is NOT necessarily where the checkpoint record itself will be,
8665  * since other backends may insert more XLOG records while we're off doing
8666  * the buffer flush work. Those XLOG records are logically after the
8667  * checkpoint, even though physically before it. Got that?
8668  */
8669  freespace = INSERT_FREESPACE(curInsert);
8670  if (freespace == 0)
8671  {
8672  if (curInsert % XLogSegSize == 0)
8673  curInsert += SizeOfXLogLongPHD;
8674  else
8675  curInsert += SizeOfXLogShortPHD;
8676  }
8677  checkPoint.redo = curInsert;
8678 
8679  /*
8680  * Here we update the shared RedoRecPtr for future XLogInsert calls; this
8681  * must be done while holding all the insertion locks.
8682  *
8683  * Note: if we fail to complete the checkpoint, RedoRecPtr will be left
8684  * pointing past where it really needs to point. This is okay; the only
8685  * consequence is that XLogInsert might back up whole buffers that it
8686  * didn't really need to. We can't postpone advancing RedoRecPtr because
8687  * XLogInserts that happen while we are dumping buffers must assume that
8688  * their buffer changes are not included in the checkpoint.
8689  */
8690  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
8691 
8692  /*
8693  * Now we can release the WAL insertion locks, allowing other xacts to
8694  * proceed while we are flushing disk buffers.
8695  */
8697 
8698  /* Update the info_lck-protected copy of RedoRecPtr as well */
8700  XLogCtl->RedoRecPtr = checkPoint.redo;
8702 
8703  /*
8704  * If enabled, log checkpoint start. We postpone this until now so as not
8705  * to log anything if we decided to skip the checkpoint.
8706  */
8707  if (log_checkpoints)
8708  LogCheckpointStart(flags, false);
8709 
8710  TRACE_POSTGRESQL_CHECKPOINT_START(flags);
8711 
8712  /*
8713  * Get the other info we need for the checkpoint record.
8714  *
8715  * We don't need to save oldestClogXid in the checkpoint, it only matters
8716  * for the short period in which clog is being truncated, and if we crash
8717  * during that we'll redo the clog truncation and fix up oldestClogXid
8718  * there.
8719  */
8720  LWLockAcquire(XidGenLock, LW_SHARED);
8721  checkPoint.nextXid = ShmemVariableCache->nextXid;
8722  checkPoint.oldestXid = ShmemVariableCache->oldestXid;
8724  LWLockRelease(XidGenLock);
8725 
8726  LWLockAcquire(CommitTsLock, LW_SHARED);
8729  LWLockRelease(CommitTsLock);
8730 
8731  /* Increase XID epoch if we've wrapped around since last checkpoint */
8733  if (checkPoint.nextXid < ControlFile->checkPointCopy.nextXid)
8734  checkPoint.nextXidEpoch++;
8735 
8736  LWLockAcquire(OidGenLock, LW_SHARED);
8737  checkPoint.nextOid = ShmemVariableCache->nextOid;
8738  if (!shutdown)
8739  checkPoint.nextOid += ShmemVariableCache->oidCount;
8740  LWLockRelease(OidGenLock);
8741 
8742  MultiXactGetCheckptMulti(shutdown,
8743  &checkPoint.nextMulti,
8744  &checkPoint.nextMultiOffset,
8745  &checkPoint.oldestMulti,
8746  &checkPoint.oldestMultiDB);
8747 
8748  /*
8749  * Having constructed the checkpoint record, ensure all shmem disk buffers
8750  * and commit-log buffers are flushed to disk.
8751  *
8752  * This I/O could fail for various reasons. If so, we will fail to
8753  * complete the checkpoint, but there is no reason to force a system
8754  * panic. Accordingly, exit critical section while doing it.
8755  */
8756  END_CRIT_SECTION();
8757 
8758  /*
8759  * In some cases there are groups of actions that must all occur on one
8760  * side or the other of a checkpoint record. Before flushing the
8761  * checkpoint record we must explicitly wait for any backend currently
8762  * performing those groups of actions.
8763  *
8764  * One example is end of transaction, so we must wait for any transactions
8765  * that are currently in commit critical sections. If an xact inserted
8766  * its commit record into XLOG just before the REDO point, then a crash
8767  * restart from the REDO point would not replay that record, which means
8768  * that our flushing had better include the xact's update of pg_xact. So
8769  * we wait till he's out of his commit critical section before proceeding.
8770  * See notes in RecordTransactionCommit().
8771  *
8772  * Because we've already released the insertion locks, this test is a bit
8773  * fuzzy: it is possible that we will wait for xacts we didn't really need
8774  * to wait for. But the delay should be short and it seems better to make
8775  * checkpoint take a bit longer than to hold off insertions longer than
8776  * necessary. (In fact, the whole reason we have this issue is that xact.c
8777  * does commit record XLOG insertion and clog update as two separate steps
8778  * protected by different locks, but again that seems best on grounds of
8779  * minimizing lock contention.)
8780  *
8781  * A transaction that has not yet set delayChkpt when we look cannot be at
8782  * risk, since he's not inserted his commit record yet; and one that's
8783  * already cleared it is not at risk either, since he's done fixing clog
8784  * and we will correctly flush the update below. So we cannot miss any
8785  * xacts we need to wait for.
8786  */
8787  vxids = GetVirtualXIDsDelayingChkpt(&nvxids);
8788  if (nvxids > 0)
8789  {
8790  do
8791  {
8792  pg_usleep(10000L); /* wait for 10 msec */
8793  } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids));
8794  }
8795  pfree(vxids);
8796 
8797  CheckPointGuts(checkPoint.redo, flags);
8798 
8799  /*
8800  * Take a snapshot of running transactions and write this to WAL. This
8801  * allows us to reconstruct the state of running transactions during
8802  * archive recovery, if required. Skip, if this info disabled.
8803  *
8804  * If we are shutting down, or Startup process is completing crash
8805  * recovery we don't need to write running xact data.
8806  */
8807  if (!shutdown && XLogStandbyInfoActive())
8809 
8811 
8812  /*
8813  * Now insert the checkpoint record into XLOG.
8814  */
8815  XLogBeginInsert();
8816  XLogRegisterData((char *) (&checkPoint), sizeof(checkPoint));
8817  recptr = XLogInsert(RM_XLOG_ID,
8818  shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
8820 
8821  XLogFlush(recptr);
8822 
8823  /*
8824  * We mustn't write any new WAL after a shutdown checkpoint, or it will be
8825  * overwritten at next startup. No-one should even try, this just allows
8826  * sanity-checking. In the case of an end-of-recovery checkpoint, we want
8827  * to just temporarily disable writing until the system has exited
8828  * recovery.
8829  */
8830  if (shutdown)
8831  {
8832  if (flags & CHECKPOINT_END_OF_RECOVERY)
8833  LocalXLogInsertAllowed = -1; /* return to "check" state */
8834  else
8835  LocalXLogInsertAllowed = 0; /* never again write WAL */
8836  }
8837 
8838  /*
8839  * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
8840  * = end of actual checkpoint record.
8841  */
8842  if (shutdown && checkPoint.redo != ProcLastRecPtr)
8843  ereport(PANIC,
8844  (errmsg("concurrent write-ahead log activity while database system is shutting down")));
8845 
8846  /*
8847  * Remember the prior checkpoint's redo pointer, used later to determine
8848  * the point where the log can be truncated.
8849  */
8850  PriorRedoPtr = ControlFile->checkPointCopy.redo;
8851 
8852  /*
8853  * Update the control file.
8854  */
8855  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8856  if (shutdown)
8860  ControlFile->checkPointCopy = checkPoint;
8861  ControlFile->time = (pg_time_t) time(NULL);
8862  /* crash recovery should always recover to the end of WAL */
8865 
8866  /*
8867  * Persist unloggedLSN value. It's reset on crash recovery, so this goes
8868  * unused on non-shutdown checkpoints, but seems useful to store it always
8869  * for debugging purposes.
8870  */
8874 
8876  LWLockRelease(ControlFileLock);
8877 
8878  /* Update shared-memory copy of checkpoint XID/epoch */
8880  XLogCtl->ckptXidEpoch = checkPoint.nextXidEpoch;
8881  XLogCtl->ckptXid = checkPoint.nextXid;
8883 
8884  /*
8885  * We are now done with critical updates; no need for system panic if we
8886  * have trouble while fooling with old log segments.
8887  */
8888  END_CRIT_SECTION();
8889 
8890  /*
8891  * Let smgr do post-checkpoint cleanup (eg, deleting old files).
8892  */
8893  smgrpostckpt();
8894 
8895  /*
8896  * Delete old log files (those no longer needed even for previous
8897  * checkpoint or the standbys in XLOG streaming).
8898  */
8899  if (PriorRedoPtr != InvalidXLogRecPtr)
8900  {
8901  XLogSegNo _logSegNo;
8902 
8903  /* Update the average distance between checkpoints. */
8905 
8906  XLByteToSeg(PriorRedoPtr, _logSegNo);
8907  KeepLogSeg(recptr, &_logSegNo);
8908  _logSegNo--;
8909  RemoveOldXlogFiles(_logSegNo, PriorRedoPtr, recptr);
8910  }
8911 
8912  /*
8913  * Make more log segments if needed. (Do this after recycling old log
8914  * segments, since that may supply some of the needed files.)
8915  */
8916  if (!shutdown)
8917  PreallocXlogFiles(recptr);
8918 
8919  /*
8920  * Truncate pg_subtrans if possible. We can throw away all data before
8921  * the oldest XMIN of any running transaction. No future transaction will
8922  * attempt to reference any pg_subtrans entry older than that (see Asserts
8923  * in subtrans.c). During recovery, though, we mustn't do this because
8924  * StartupSUBTRANS hasn't been called yet.
8925  */
8926  if (!RecoveryInProgress())
8928 
8929  /* Real work is done, but log and update stats before releasing lock. */
8930  LogCheckpointEnd(false);
8931 
8932  TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats.ckpt_bufs_written,
8933  NBuffers,
8937 
8938  LWLockRelease(CheckpointLock);
8939 }
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:8240
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:8461
#define XLogSegSize
Definition: xlog_internal.h:92
static int LocalXLogInsertAllowed
Definition: xlog.c:233
bool log_checkpoints
Definition: xlog.c:102
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
int64 pg_time_t
Definition: pgtime.h:23
TransactionId ckptXid
Definition: xlog.c:581
static void WALInsertLockRelease(void)
Definition: xlog.c:1646
pg_time_t time
Definition: pg_control.h:131
#define XLOG_CHECKPOINT_ONLINE
Definition: pg_control.h:67
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:171
uint32 oidCount
Definition: transam.h:112
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1915
XLogRecPtr unloggedLSN
Definition: xlog.c:589
XLogRecPtr ProcLastRecPtr
Definition: xlog.c:336
TransactionId oldestActiveXid
Definition: pg_control.h:62
void InitXLogInsert(void)
Definition: xloginsert.c:1028
TimestampTz ckpt_start_t
Definition: xlog.h:199
slock_t info_lck
Definition: xlog.c:703
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
VirtualTransactionId * GetVirtualXIDsDelayingChkpt(int *nvxids)
Definition: procarray.c:2250
MultiXactId oldestMulti
Definition: pg_control.h:48
TimeLineID PrevTimeLineID
Definition: xlog.c:630
TimeLineID PrevTimeLineID
Definition: pg_control.h:38
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
int ckpt_segs_recycled
Definition: xlog.h:209
TransactionId oldestXid
Definition: transam.h:119
#define MemSet(start, val, len)
Definition: c.h:857
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr PriorRedoPtr, XLogRecPtr endptr)
Definition: xlog.c:3831
void MultiXactGetCheckptMulti(bool is_shutdown, MultiXactId *nextMulti, MultiXactOffset *nextMultiOffset, MultiXactId *oldestMulti, Oid *oldestMultiDB)
Definition: multixact.c:2118
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
Definition: xlog.c:9000
CheckPoint checkPointCopy
Definition: pg_control.h:135
XLogCtlInsert Insert
Definition: xlog.c:575
TransactionId oldestXid
Definition: pg_control.h:46
bool RecoveryInProgress(void)
Definition: xlog.c:7873
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:353
uint32 ckptXidEpoch
Definition: xlog.c:580
TransactionId nextXid
Definition: pg_control.h:42
pg_time_t time
Definition: pg_control.h:50
#define PANIC
Definition: elog.h:53
bool fullPageWrites
Definition: xlog.c:550
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2758
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define SpinLockAcquire(lock)
Definition: spin.h:62
void pg_usleep(long microsec)
Definition: signal.c:53
MultiXactOffset nextMultiOffset
Definition: pg_control.h:45
void UpdateControlFile(void)
Definition: xlog.c:4641
TransactionId oldestCommitTsXid
Definition: pg_control.h:51
void pfree(void *pointer)
Definition: mcxt.c:950
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:909
#define ERROR
Definition: elog.h:43
static void LogCheckpointEnd(bool restartpoint)
Definition: xlog.c:8376
TransactionId nextXid
Definition: transam.h:117
uint32 nextXidEpoch
Definition: pg_control.h:41
static XLogRecPtr RedoRecPtr
Definition: xlog.c:350
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:66
XLogRecPtr unloggedLSN
Definition: pg_control.h:137
static void PreallocXlogFiles(XLogRecPtr endptr)
Definition: xlog.c:3740
uint64 XLogSegNo
Definition: xlogdefs.h:34
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:176
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
uint64 CurrBytePos
Definition: xlog.c:526
unsigned int uint32
Definition: c.h:268
XLogRecPtr RedoRecPtr
Definition: xlog.c:579
int ckpt_segs_removed
Definition: xlog.h:208
#define CHECKPOINT_FORCE
Definition: xlog.h:180
#define INSERT_FREESPACE(endptr)
Definition: xlog.c:720
#define ereport(elevel, rest)
Definition: elog.h:122
TransactionId oldestCommitTsXid
Definition: transam.h:129
static void Insert(File file)
Definition: fd.c:1044
int ckpt_bufs_written
Definition: xlog.h:205
static void LocalSetXLogInsertAllowed(void)
Definition: xlog.c:7999
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
TransactionId newestCommitTsXid
Definition: pg_control.h:53
#define PROCARRAY_FLAGS_DEFAULT
Definition: procarray.h:51
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:9324
Oid oldestMultiDB
Definition: pg_control.h:49
#define XLogStandbyInfoActive()
Definition: xlog.h:159
XLogRecPtr prevCheckPoint
Definition: pg_control.h:133
static ControlFileData * ControlFile
Definition: xlog.c:714
TimeLineID ThisTimeLineID
Definition: xlog.c:179
Oid nextOid
Definition: pg_control.h:43
bool fullPageWrites
Definition: pg_control.h:40
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1314
void smgrpreckpt(void)
Definition: smgr.c:744
#define XLByteToSeg(xlrp, logSegNo)
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
Oid oldestXidDB
Definition: pg_control.h:47
TransactionId newestCommitTsXid
Definition: transam.h:130
CheckpointStatsData CheckpointStats
Definition: xlog.c:173
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
MultiXactId nextMulti
Definition: pg_control.h:44
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1617
static XLogCtlData * XLogCtl
Definition: xlog.c:706
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int ckpt_segs_added
Definition: xlog.h:207
slock_t ulsn_lck
Definition: xlog.c:590
TimeLineID ThisTimeLineID
Definition: pg_control.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:797
TransactionId GetOldestActiveTransactionId(void)
Definition: procarray.c:2088
int NBuffers
Definition: globals.c:122
#define elog
Definition: elog.h:219
bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids)
Definition: procarray.c:2295
void XLogBeginInsert(void)
Definition: xloginsert.c:120
XLogRecPtr RedoRecPtr
Definition: xlog.c:548
void smgrpostckpt(void)
Definition: smgr.c:774
XLogRecPtr checkPoint
Definition: pg_control.h:132
XLogRecPtr redo
Definition: pg_control.h:35
static void LogCheckpointStart(int flags, bool restartpoint)
Definition: xlog.c:8358
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:175
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:170
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72
bool CreateRestartPoint ( int  flags)

Definition at line 9070 of file xlog.c.

References XLogCtlData::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(), GetWalRcvWriteRecPtr(), GetXLogReplayRecPtr(), XLogCtlData::info_lck, XLogCtlData::Insert, InvalidXLogRecPtr, KeepLogSeg(), XLogCtlData::lastCheckPoint, XLogCtlData::lastCheckPointEndPtr, XLogCtlData::lastCheckPointRecPtr, LOG, log_checkpoints, LogCheckpointEnd(), LogCheckpointStart(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemSet, ControlFileData::minRecoveryPoint, minRecoveryPoint, ControlFileData::minRecoveryPointTLI, minRecoveryPointTLI, NULL, PreallocXlogFiles(), ControlFileData::prevCheckPoint, 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(), WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLByteToSeg, and XLogRecPtrIsInvalid.

Referenced by CheckpointerMain(), and ShutdownXLOG().

9071 {
9072  XLogRecPtr lastCheckPointRecPtr;
9073  XLogRecPtr lastCheckPointEndPtr;
9074  CheckPoint lastCheckPoint;
9075  XLogRecPtr PriorRedoPtr;
9076  TimestampTz xtime;
9077 
9078  /*
9079  * Acquire CheckpointLock to ensure only one restartpoint or checkpoint
9080  * happens at a time.
9081  */
9082  LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
9083 
9084  /* Get a local copy of the last safe checkpoint record. */
9086  lastCheckPointRecPtr = XLogCtl->lastCheckPointRecPtr;
9087  lastCheckPointEndPtr = XLogCtl->lastCheckPointEndPtr;
9088  lastCheckPoint = XLogCtl->lastCheckPoint;
9090 
9091  /*
9092  * Check that we're still in recovery mode. It's ok if we exit recovery
9093  * mode after this check, the restart point is valid anyway.
9094  */
9095  if (!RecoveryInProgress())
9096  {
9097  ereport(DEBUG2,
9098  (errmsg("skipping restartpoint, recovery has already ended")));
9099  LWLockRelease(CheckpointLock);
9100  return false;
9101  }
9102 
9103  /*
9104  * If the last checkpoint record we've replayed is already our last
9105  * restartpoint, we can't perform a new restart point. We still update
9106  * minRecoveryPoint in that case, so that if this is a shutdown restart
9107  * point, we won't start up earlier than before. That's not strictly
9108  * necessary, but when hot standby is enabled, it would be rather weird if
9109  * the database opened up for read-only connections at a point-in-time
9110  * before the last shutdown. Such time travel is still possible in case of
9111  * immediate shutdown, though.
9112  *
9113  * We don't explicitly advance minRecoveryPoint when we do create a
9114  * restartpoint. It's assumed that flushing the buffers will do that as a
9115  * side-effect.
9116  */
9117  if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||
9118  lastCheckPoint.redo <= ControlFile->checkPointCopy.redo)
9119  {
9120  ereport(DEBUG2,
9121  (errmsg("skipping restartpoint, already performed at %X/%X",
9122  (uint32) (lastCheckPoint.redo >> 32),
9123  (uint32) lastCheckPoint.redo)));
9124 
9126  if (flags & CHECKPOINT_IS_SHUTDOWN)
9127  {
9128  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9130  ControlFile->time = (pg_time_t) time(NULL);
9132  LWLockRelease(ControlFileLock);
9133  }
9134  LWLockRelease(CheckpointLock);
9135  return false;
9136  }
9137 
9138  /*
9139  * Update the shared RedoRecPtr so that the startup process can calculate
9140  * the number of segments replayed since last restartpoint, and request a
9141  * restartpoint if it exceeds CheckPointSegments.
9142  *
9143  * Like in CreateCheckPoint(), hold off insertions to update it, although
9144  * during recovery this is just pro forma, because no WAL insertions are
9145  * happening.
9146  */
9148  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = lastCheckPoint.redo;
9150 
9151  /* Also update the info_lck-protected copy */
9153  XLogCtl->RedoRecPtr = lastCheckPoint.redo;
9155 
9156  /*
9157  * Prepare to accumulate statistics.
9158  *
9159  * Note: because it is possible for log_checkpoints to change while a
9160  * checkpoint proceeds, we always accumulate stats, even if
9161  * log_checkpoints is currently off.
9162  */
9163  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
9165 
9166  if (log_checkpoints)
9167  LogCheckpointStart(flags, true);
9168 
9169  CheckPointGuts(lastCheckPoint.redo, flags);
9170 
9171  /*
9172  * Remember the prior checkpoint's redo pointer, used later to determine
9173  * the point at which we can truncate the log.
9174  */
9175  PriorRedoPtr = ControlFile->checkPointCopy.redo;
9176 
9177  /*
9178  * Update pg_control, using current time. Check that it still shows
9179  * IN_ARCHIVE_RECOVERY state and an older checkpoint, else do nothing;
9180  * this is a quick hack to make sure nothing really bad happens if somehow
9181  * we get here after the end-of-recovery checkpoint.
9182  */
9183  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9185  ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
9186  {
9188  ControlFile->checkPoint = lastCheckPointRecPtr;
9189  ControlFile->checkPointCopy = lastCheckPoint;
9190  ControlFile->time = (pg_time_t) time(NULL);
9191 
9192  /*
9193  * Ensure minRecoveryPoint is past the checkpoint record. Normally,
9194  * this will have happened already while writing out dirty buffers,
9195  * but not necessarily - e.g. because no buffers were dirtied. We do
9196  * this because a non-exclusive base backup uses minRecoveryPoint to
9197  * determine which WAL files must be included in the backup, and the
9198  * file (or files) containing the checkpoint record must be included,
9199  * at a minimum. Note that for an ordinary restart of recovery there's
9200  * no value in having the minimum recovery point any earlier than this
9201  * anyway, because redo will begin just after the checkpoint record.
9202  */
9203  if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
9204  {
9205  ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
9207 
9208  /* update local copy */
9211  }
9212  if (flags & CHECKPOINT_IS_SHUTDOWN)
9215  }
9216  LWLockRelease(ControlFileLock);
9217 
9218  /*
9219  * Delete old log files (those no longer needed even for previous
9220  * checkpoint/restartpoint) to prevent the disk holding the xlog from
9221  * growing full.
9222  */
9223  if (PriorRedoPtr != InvalidXLogRecPtr)
9224  {
9225  XLogRecPtr receivePtr;
9226  XLogRecPtr replayPtr;
9227  TimeLineID replayTLI;
9228  XLogRecPtr endptr;
9229  XLogSegNo _logSegNo;
9230 
9231  /* Update the average distance between checkpoints/restartpoints. */
9233 
9234  XLByteToSeg(PriorRedoPtr, _logSegNo);
9235 
9236  /*
9237  * Get the current end of xlog replayed or received, whichever is
9238  * later.
9239  */
9240  receivePtr = GetWalRcvWriteRecPtr(NULL, NULL);
9241  replayPtr = GetXLogReplayRecPtr(&replayTLI);
9242  endptr = (receivePtr < replayPtr) ? replayPtr : receivePtr;
9243 
9244  KeepLogSeg(endptr, &_logSegNo);
9245  _logSegNo--;
9246 
9247  /*
9248  * Try to recycle segments on a useful timeline. If we've been
9249  * promoted since the beginning of this restartpoint, use the new
9250  * timeline chosen at end of recovery (RecoveryInProgress() sets
9251  * ThisTimeLineID in that case). If we're still in recovery, use the
9252  * timeline we're currently replaying.
9253  *
9254  * There is no guarantee that the WAL segments will be useful on the
9255  * current timeline; if recovery proceeds to a new timeline right
9256  * after this, the pre-allocated WAL segments on this timeline will
9257  * not be used, and will go wasted until recycled on the next
9258  * restartpoint. We'll live with that.
9259  */
9260  if (RecoveryInProgress())
9261  ThisTimeLineID = replayTLI;
9262 
9263  RemoveOldXlogFiles(_logSegNo, PriorRedoPtr, endptr);
9264 
9265  /*
9266  * Make more log segments if needed. (Do this after recycling old log
9267  * segments, since that may supply some of the needed files.)
9268  */
9269  PreallocXlogFiles(endptr);
9270 
9271  /*
9272  * ThisTimeLineID is normally not set when we're still in recovery.
9273  * However, recycling/preallocating segments above needed
9274  * ThisTimeLineID to determine which timeline to install the segments
9275  * on. Reset it now, to restore the normal state of affairs for
9276  * debugging purposes.
9277  */
9278  if (RecoveryInProgress())
9279  ThisTimeLineID = 0;
9280  }
9281 
9282  /*
9283  * Truncate pg_subtrans if possible. We can throw away all data before
9284  * the oldest XMIN of any running transaction. No future transaction will
9285  * attempt to reference any pg_subtrans entry older than that (see Asserts
9286  * in subtrans.c). When hot standby is disabled, though, we mustn't do
9287  * this because StartupSUBTRANS hasn't been called yet.
9288  */
9289  if (EnableHotStandby)
9291 
9292  /* Real work is done, but log and update before releasing lock. */
9293  LogCheckpointEnd(true);
9294 
9295  xtime = GetLatestXTime();
9297  (errmsg("recovery restart point at %X/%X",
9298  (uint32) (lastCheckPoint.redo >> 32), (uint32) lastCheckPoint.redo),
9299  xtime ? errdetail("last completed transaction was at log time %s",
9300  timestamptz_to_str(xtime)) : 0));
9301 
9302  LWLockRelease(CheckpointLock);
9303 
9304  /*
9305  * Finally, execute archive_cleanup_command, if any.
9306  */
9309  "archive_cleanup_command",
9310  false);
9311 
9312  return true;
9313 }
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:8461
bool log_checkpoints
Definition: xlog.c:102
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TimeLineID
Definition: xlogdefs.h:45
int64 pg_time_t
Definition: pgtime.h:23
XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
static void WALInsertLockRelease(void)
Definition: xlog.c:1646
pg_time_t time
Definition: pg_control.h:131
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:171
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
int64 TimestampTz
Definition: timestamp.h:39
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
Definition: xlog.c:2683
TimestampTz ckpt_start_t
Definition: xlog.h:199
slock_t info_lck
Definition: xlog.c:703
#define MemSet(start, val, len)
Definition: c.h:857
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr PriorRedoPtr, XLogRecPtr endptr)
Definition: xlog.c:3831
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
Definition: xlog.c:9000
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6061
CheckPoint checkPointCopy
Definition: pg_control.h:135
XLogCtlInsert Insert
Definition: xlog.c:575
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7873
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:353
XLogRecPtr lastCheckPointRecPtr
Definition: xlog.c:672
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define SpinLockAcquire(lock)
Definition: spin.h:62
void UpdateControlFile(void)
Definition: xlog.c:4641
static void LogCheckpointEnd(bool restartpoint)
Definition: xlog.c:8376
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11084
#define DEBUG2
Definition: elog.h:24
static XLogRecPtr RedoRecPtr
Definition: xlog.c:350
static void PreallocXlogFiles(XLogRecPtr endptr)
Definition: xlog.c:3740
uint64 XLogSegNo
Definition: xlogdefs.h:34
int errdetail(const char *fmt,...)
Definition: elog.c:873
unsigned int uint32
Definition: c.h:268
XLogRecPtr RedoRecPtr
Definition: xlog.c:579
#define ereport(elevel, rest)
Definition: elog.h:122
CheckPoint lastCheckPoint
Definition: xlog.c:674
void ExecuteRecoveryCommand(char *command, char *commandName, bool failOnSignal)
Definition: xlogarchive.c:329
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
static TimeLineID minRecoveryPointTLI
Definition: xlog.c:823
#define PROCARRAY_FLAGS_DEFAULT
Definition: procarray.h:51
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:9324
XLogRecPtr prevCheckPoint
Definition: pg_control.h:133
static ControlFileData * ControlFile
Definition: xlog.c:714
TimeLineID ThisTimeLineID
Definition: xlog.c:179
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1314
#define XLByteToSeg(xlrp, logSegNo)
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
CheckpointStatsData CheckpointStats
Definition: xlog.c:173
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1617
static XLogCtlData * XLogCtl
Definition: xlog.c:706
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
char archiveCleanupCommand[MAXPGPATH]
Definition: xlog.c:636
bool EnableHotStandby
Definition: xlog.c:96
TimeLineID ThisTimeLineID
Definition: pg_control.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:797
XLogRecPtr RedoRecPtr
Definition: xlog.c:548
XLogRecPtr lastCheckPointEndPtr
Definition: xlog.c:673
XLogRecPtr checkPoint
Definition: pg_control.h:132
XLogRecPtr redo
Definition: pg_control.h:35
static void LogCheckpointStart(int flags, bool restartpoint)
Definition: xlog.c:8358
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:175
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:170
static XLogRecPtr minRecoveryPoint
Definition: xlog.c:821
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1710
bool DataChecksumsEnabled ( void  )

Definition at line 4710 of file xlog.c.

References Assert, ControlFileData::data_checksum_version, and NULL.

Referenced by PageIsVerified(), PageSetChecksumCopy(), PageSetChecksumInplace(), and ReadControlFile().

4711 {
4712  Assert(ControlFile != NULL);
4713  return (ControlFile->data_checksum_version > 0);
4714 }
uint32 data_checksum_version
Definition: pg_control.h:225
static ControlFileData * ControlFile
Definition: xlog.c:714
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void do_pg_abort_backup ( void  )

Definition at line 11064 of file xlog.c.

References Assert, EXCLUSIVE_BACKUP_NONE, XLogCtlInsert::exclusiveBackupState, XLogCtlInsert::forcePageWrites, XLogCtlData::Insert, XLogCtlInsert::nonExclusiveBackups, WALInsertLockAcquireExclusive(), and WALInsertLockRelease().

Referenced by base_backup_cleanup(), and nonexclusive_base_backup_cleanup().

11065 {
11069 
11072  {
11073  XLogCtl->Insert.forcePageWrites = false;
11074  }
11076 }
static void WALInsertLockRelease(void)
Definition: xlog.c:1646
XLogCtlInsert Insert
Definition: xlog.c:575
bool forcePageWrites
Definition: xlog.c:549
int nonExclusiveBackups
Definition: xlog.c:561
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:560
#define Assert(condition)
Definition: c.h:675
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1617
static XLogCtlData * XLogCtl
Definition: xlog.c:706
XLogRecPtr do_pg_start_backup ( const char *  backupidstr,
bool  fast,
TimeLineID starttli_p,
StringInfo  labelfile,
DIR tblspcdir,
List **  tablespaces,
StringInfo  tblspcmapfile,
bool  infotbssize,
bool  needtblspcmapfile 
)

Definition at line 10173 of file xlog.c.

References 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, 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, NULL, tablespaceinfo::oid, palloc(), tablespaceinfo::path, pfree(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, pg_fsync(), pg_localtime(), pg_start_backup_callback(), pg_strftime(), pstrdup(), ReadDir(), RecoveryInProgress(), CheckPoint::redo, relpath, RequestCheckpoint(), RequestXLogSwitch(), tablespaceinfo::rpath, sendTablespace(), SESSION_BACKUP_EXCLUSIVE, SESSION_BACKUP_NON_EXCLUSIVE, sessionBackupState, tablespaceinfo::size, snprintf(), SpinLockAcquire, SpinLockRelease, TABLESPACE_MAP, CheckPoint::ThisTimeLineID, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WARNING, XLByteToSeg, XLogFileName, and XLogIsNeeded.

Referenced by perform_base_backup(), and pg_start_backup().

10177 {
10178  bool exclusive = (labelfile == NULL);
10179  bool backup_started_in_recovery = false;
10180  XLogRecPtr checkpointloc;
10181  XLogRecPtr startpoint;
10182  TimeLineID starttli;
10183  pg_time_t stamp_time;
10184  char strfbuf[128];
10185  char xlogfilename[MAXFNAMELEN];
10186  XLogSegNo _logSegNo;
10187  struct stat stat_buf;
10188  FILE *fp;
10189 
10190  backup_started_in_recovery = RecoveryInProgress();
10191 
10192  /*
10193  * Currently only non-exclusive backup can be taken during recovery.
10194  */
10195  if (backup_started_in_recovery && exclusive)
10196  ereport(ERROR,
10197  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10198  errmsg("recovery is in progress"),
10199  errhint("WAL control functions cannot be executed during recovery.")));
10200 
10201  /*
10202  * During recovery, we don't need to check WAL level. Because, if WAL
10203  * level is not sufficient, it's impossible to get here during recovery.
10204  */
10205  if (!backup_started_in_recovery && !XLogIsNeeded())
10206  ereport(ERROR,
10207  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10208  errmsg("WAL level not sufficient for making an online backup"),
10209  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
10210 
10211  if (strlen(backupidstr) > MAXPGPATH)
10212  ereport(ERROR,
10213  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
10214  errmsg("backup label too long (max %d bytes)",
10215  MAXPGPATH)));
10216 
10217  /*
10218  * Mark backup active in shared memory. We must do full-page WAL writes
10219  * during an on-line backup even if not doing so at other times, because
10220  * it's quite possible for the backup dump to obtain a "torn" (partially
10221  * written) copy of a database page if it reads the page concurrently with
10222  * our write to the same page. This can be fixed as long as the first
10223  * write to the page in the WAL sequence is a full-page write. Hence, we
10224  * turn on forcePageWrites and then force a CHECKPOINT, to ensure there
10225  * are no dirty pages in shared memory that might get dumped while the
10226  * backup is in progress without having a corresponding WAL record. (Once
10227  * the backup is complete, we need not force full-page writes anymore,
10228  * since we expect that any pages not modified during the backup interval
10229  * must have been correctly captured by the backup.)
10230  *
10231  * Note that forcePageWrites has no effect during an online backup from
10232  * the standby.
10233  *
10234  * We must hold all the insertion locks to change the value of
10235  * forcePageWrites, to ensure adequate interlocking against
10236  * XLogInsertRecord().
10237  */
10239  if (exclusive)
10240  {
10241  /*
10242  * At first, mark that we're now starting an exclusive backup, to
10243  * ensure that there are no other sessions currently running
10244  * pg_start_backup() or pg_stop_backup().
10245  */
10247  {
10249  ereport(ERROR,
10250  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10251  errmsg("a backup is already in progress"),
10252  errhint("Run pg_stop_backup() and try again.")));
10253  }
10255  }
10256  else
10258  XLogCtl->Insert.forcePageWrites = true;
10260 
10261  /* Ensure we release forcePageWrites if fail below */
10263  {
10264  bool gotUniqueStartpoint = false;
10265  struct dirent *de;
10266  tablespaceinfo *ti;
10267  int datadirpathlen;
10268 
10269  /*
10270  * Force an XLOG file switch before the checkpoint, to ensure that the
10271  * WAL segment the checkpoint is written to doesn't contain pages with
10272  * old timeline IDs. That would otherwise happen if you called
10273  * pg_start_backup() right after restoring from a PITR archive: the
10274  * first WAL segment containing the startup checkpoint has pages in
10275  * the beginning with the old timeline ID. That can cause trouble at
10276  * recovery: we won't have a history file covering the old timeline if
10277  * pg_wal directory was not included in the base backup and the WAL
10278  * archive was cleared too before starting the backup.
10279  *
10280  * This also ensures that we have emitted a WAL page header that has
10281  * XLP_BKP_REMOVABLE off before we emit the checkpoint record.
10282  * Therefore, if a WAL archiver (such as pglesslog) is trying to
10283  * compress out removable backup blocks, it won't remove any that
10284  * occur after this point.
10285  *
10286  * During recovery, we skip forcing XLOG file switch, which means that
10287  * the backup taken during recovery is not available for the special
10288  * recovery case described above.
10289  */
10290  if (!backup_started_in_recovery)
10291  RequestXLogSwitch(false);
10292 
10293  do
10294  {
10295  bool checkpointfpw;
10296 
10297  /*
10298  * Force a CHECKPOINT. Aside from being necessary to prevent torn
10299  * page problems, this guarantees that two successive backup runs
10300  * will have different checkpoint positions and hence different
10301  * history file names, even if nothing happened in between.
10302  *
10303  * During recovery, establish a restartpoint if possible. We use
10304  * the last restartpoint as the backup starting checkpoint. This
10305  * means that two successive backup runs can have same checkpoint
10306  * positions.
10307  *
10308  * Since the fact that we are executing do_pg_start_backup()
10309  * during recovery means that checkpointer is running, we can use
10310  * RequestCheckpoint() to establish a restartpoint.
10311  *
10312  * We use CHECKPOINT_IMMEDIATE only if requested by user (via
10313  * passing fast = true). Otherwise this can take awhile.
10314  */
10316  (fast ? CHECKPOINT_IMMEDIATE : 0));
10317 
10318  /*
10319  * Now we need to fetch the checkpoint record location, and also
10320  * its REDO pointer. The oldest point in WAL that would be needed
10321  * to restore starting from the checkpoint is precisely the REDO
10322  * pointer.
10323  */
10324  LWLockAcquire(ControlFileLock, LW_SHARED);
10325  checkpointloc = ControlFile->checkPoint;
10326  startpoint = ControlFile->checkPointCopy.redo;
10328  checkpointfpw = ControlFile->checkPointCopy.fullPageWrites;
10329  LWLockRelease(ControlFileLock);
10330 
10331  if (backup_started_in_recovery)
10332  {
10333  XLogRecPtr recptr;
10334 
10335  /*
10336  * Check to see if all WAL replayed during online backup
10337  * (i.e., since last restartpoint used as backup starting
10338  * checkpoint) contain full-page writes.
10339  */
10341  recptr = XLogCtl->lastFpwDisableRecPtr;
10343 
10344  if (!checkpointfpw || startpoint <= recptr)
10345  ereport(ERROR,
10346  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10347  errmsg("WAL generated with full_page_writes=off was replayed "
10348  "since last restartpoint"),
10349  errhint("This means that the backup being taken on the standby "
10350  "is corrupt and should not be used. "
10351  "Enable full_page_writes and run CHECKPOINT on the master, "
10352  "and then try an online backup again.")));
10353 
10354  /*
10355  * During recovery, since we don't use the end-of-backup WAL
10356  * record and don't write the backup history file, the
10357  * starting WAL location doesn't need to be unique. This means
10358  * that two base backups started at the same time might use
10359  * the same checkpoint as starting locations.
10360  */
10361  gotUniqueStartpoint = true;
10362  }
10363 
10364  /*
10365  * If two base backups are started at the same time (in WAL sender
10366  * processes), we need to make sure that they use different
10367  * checkpoints as starting locations, because we use the starting
10368  * WAL location as a unique identifier for the base backup in the
10369  * end-of-backup WAL record and when we write the backup history
10370  * file. Perhaps it would be better generate a separate unique ID
10371  * for each backup instead of forcing another checkpoint, but
10372  * taking a checkpoint right after another is not that expensive
10373  * either because only few buffers have been dirtied yet.
10374  */
10376  if (XLogCtl->Insert.lastBackupStart < startpoint)
10377  {
10378  XLogCtl->Insert.lastBackupStart = startpoint;
10379  gotUniqueStartpoint = true;
10380  }
10382  } while (!gotUniqueStartpoint);
10383 
10384  XLByteToSeg(startpoint, _logSegNo);
10385  XLogFileName(xlogfilename, starttli, _logSegNo);
10386 
10387  /*
10388  * Construct tablespace_map file
10389  */
10390  if (exclusive)
10391  tblspcmapfile = makeStringInfo();
10392 
10393  datadirpathlen = strlen(DataDir);
10394 
10395  /* Collect information about all tablespaces */
10396  while ((de = ReadDir(tblspcdir, "pg_tblspc")) != NULL)
10397  {
10398  char fullpath[MAXPGPATH + 10];
10399  char linkpath[MAXPGPATH];
10400  char *relpath = NULL;
10401  int rllen;
10402  StringInfoData buflinkpath;
10403  char *s = linkpath;
10404 
10405  /* Skip special stuff */
10406  if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
10407  continue;
10408 
10409  snprintf(fullpath, sizeof(fullpath), "pg_tblspc/%s", de->d_name);
10410 
10411 #if defined(HAVE_READLINK) || defined(WIN32)
10412  rllen = readlink(fullpath, linkpath, sizeof(linkpath));
10413  if (rllen < 0)
10414  {
10415  ereport(WARNING,
10416  (errmsg("could not read symbolic link \"%s\": %m",
10417  fullpath)));
10418  continue;
10419  }
10420  else if (rllen >= sizeof(linkpath))
10421  {
10422  ereport(WARNING,
10423  (errmsg("symbolic link \"%s\" target is too long",
10424  fullpath)));
10425  continue;
10426  }
10427  linkpath[rllen] = '\0';
10428 
10429  /*
10430  * Add the escape character '\\' before newline in a string to
10431  * ensure that we can distinguish between the newline in the
10432  * tablespace path and end of line while reading tablespace_map
10433  * file during archive recovery.
10434  */
10435  initStringInfo(&buflinkpath);
10436 
10437  while (*s)
10438  {
10439  if ((*s == '\n' || *s == '\r') && needtblspcmapfile)
10440  appendStringInfoChar(&buflinkpath, '\\');
10441  appendStringInfoChar(&buflinkpath, *s++);
10442  }
10443 
10444 
10445  /*
10446  * Relpath holds the relative path of the tablespace directory
10447  * when it's located within PGDATA, or NULL if it's located
10448  * elsewhere.
10449  */
10450  if (rllen > datadirpathlen &&
10451  strncmp(linkpath, DataDir, datadirpathlen) == 0 &&
10452  IS_DIR_SEP(linkpath[datadirpathlen]))
10453  relpath = linkpath + datadirpathlen + 1;
10454 
10455  ti = palloc(sizeof(tablespaceinfo));
10456  ti->oid = pstrdup(de->d_name);
10457  ti->path = pstrdup(buflinkpath.data);
10458  ti->rpath = relpath ? pstrdup(relpath) : NULL;
10459  ti->size = infotbssize ? sendTablespace(fullpath, true) : -1;
10460 
10461  if (tablespaces)
10462  *tablespaces = lappend(*tablespaces, ti);
10463 
10464  appendStringInfo(tblspcmapfile, "%s %s\n", ti->oid, ti->path);
10465 
10466  pfree(buflinkpath.data);
10467 #else
10468 
10469  /*
10470  * If the platform does not have symbolic links, it should not be
10471  * possible to have tablespaces - clearly somebody else created
10472  * them. Warn about it and ignore.
10473  */
10474  ereport(WARNING,
10475  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10476  errmsg("tablespaces are not supported on this platform")));
10477 #endif
10478  }
10479 
10480  /*
10481  * Construct backup label file
10482  */
10483  if (exclusive)
10484  labelfile = makeStringInfo();
10485 
10486  /* Use the log timezone here, not the session timezone */
10487  stamp_time = (pg_time_t) time(NULL);
10488  pg_strftime(strfbuf, sizeof(strfbuf),
10489  "%Y-%m-%d %H:%M:%S %Z",
10490  pg_localtime(&stamp_time, log_timezone));
10491  appendStringInfo(labelfile, "START WAL LOCATION: %X/%X (file %s)\n",
10492  (uint32) (startpoint >> 32), (uint32) startpoint, xlogfilename);
10493  appendStringInfo(labelfile, "CHECKPOINT LOCATION: %X/%X\n",
10494  (uint32) (checkpointloc >> 32), (uint32) checkpointloc);
10495  appendStringInfo(labelfile, "BACKUP METHOD: %s\n",
10496  exclusive ? "pg_start_backup" : "streamed");
10497  appendStringInfo(labelfile, "BACKUP FROM: %s\n",
10498  backup_started_in_recovery ? "standby" : "master");
10499  appendStringInfo(labelfile, "START TIME: %s\n", strfbuf);
10500  appendStringInfo(labelfile, "LABEL: %s\n", backupidstr);
10501 
10502  /*
10503  * Okay, write the file, or return its contents to caller.
10504  */
10505  if (exclusive)
10506  {
10507  /*
10508  * Check for existing backup label --- implies a backup is already
10509  * running. (XXX given that we checked exclusiveBackupState
10510  * above, maybe it would be OK to just unlink any such label
10511  * file?)
10512  */
10513  if (stat(BACKUP_LABEL_FILE, &stat_buf) != 0)
10514  {
10515  if (errno != ENOENT)
10516  ereport(ERROR,
10518  errmsg("could not stat file \"%s\": %m",
10519  BACKUP_LABEL_FILE)));
10520  }
10521  else
10522  ereport(ERROR,
10523  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10524  errmsg("a backup is already in progress"),
10525  errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
10526  BACKUP_LABEL_FILE)));
10527 
10528  fp = AllocateFile(BACKUP_LABEL_FILE, "w");
10529 
10530  if (!fp)
10531  ereport(ERROR,
10533  errmsg("could not create file \"%s\": %m",
10534  BACKUP_LABEL_FILE)));
10535  if (fwrite(labelfile->data, labelfile->len, 1, fp) != 1 ||
10536  fflush(fp) != 0 ||
10537  pg_fsync(fileno(fp)) != 0 ||
10538  ferror(fp) ||
10539  FreeFile(fp))
10540  ereport(ERROR,
10542  errmsg("could not write file \"%s\": %m",
10543  BACKUP_LABEL_FILE)));
10544  /* Allocated locally for exclusive backups, so free separately */
10545  pfree(labelfile->data);
10546  pfree(labelfile);
10547 
10548  /* Write backup tablespace_map file. */
10549  if (tblspcmapfile->len > 0)
10550  {
10551  if (stat(TABLESPACE_MAP, &stat_buf) != 0)
10552  {
10553  if (errno != ENOENT)
10554  ereport(ERROR,
10556  errmsg("could not stat file \"%s\": %m",
10557  TABLESPACE_MAP)));
10558  }
10559  else
10560  ereport(ERROR,
10561  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10562  errmsg("a backup is already in progress"),
10563  errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
10564  TABLESPACE_MAP)));
10565 
10566  fp = AllocateFile(TABLESPACE_MAP, "w");
10567 
10568  if (!fp)
10569  ereport(ERROR,
10571  errmsg("could not create file \"%s\": %m",
10572  TABLESPACE_MAP)));
10573  if (fwrite(tblspcmapfile->data, tblspcmapfile->len, 1, fp) != 1 ||
10574  fflush(fp) != 0 ||
10575  pg_fsync(fileno(fp)) != 0 ||
10576  ferror(fp) ||
10577  FreeFile(fp))
10578  ereport(ERROR,
10580  errmsg("could not write file \"%s\": %m",
10581  TABLESPACE_MAP)));
10582  }
10583 
10584  /* Allocated locally for exclusive backups, so free separately */
10585  pfree(tblspcmapfile->data);
10586  pfree(tblspcmapfile);
10587  }
10588  }
10590 
10591  /*
10592  * Mark that start phase has correctly finished for an exclusive backup.
10593  * Session-level locks are updated as well to reflect that state.
10594  */
10595  if (exclusive)
10596  {
10601  }
10602  else
10604 
10605  /*
10606  * We're done. As a convenience, return the starting WAL location.
10607  */
10608  if (starttli_p)
10609  *starttli_p = starttli;
10610  return startpoint;
10611 }
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:122
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9401
int errhint(const char *fmt,...)
Definition: elog.c:987
uint32 TimeLineID
Definition: xlogdefs.h:45
int64 pg_time_t
Definition: pgtime.h:23
static void WALInsertLockRelease(void)
Definition: xlog.c:1646
XLogRecPtr lastFpwDisableRecPtr
Definition: xlog.c:701
static SessionBackupState sessionBackupState
Definition: xlog.c:510
XLogRecPtr lastBackupStart
Definition: xlog.c:562
char * pstrdup(const char *in)
Definition: mcxt.c:1077
#define XLogIsNeeded()
Definition: xlog.h:145
char * rpath
Definition: basebackup.h:28
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
slock_t info_lck
Definition: xlog.c:703
#define XLogFileName(fname, tli, logSegNo)
int errcode(int sqlerrcode)
Definition: elog.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
CheckPoint checkPointCopy
Definition: pg_control.h:135
XLogCtlInsert Insert
Definition: xlog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7873
static bool backup_started_in_recovery
Definition: basebackup.c:73
Definition: dirent.h:9
#define IS_DIR_SEP(ch)
Definition: port.h:75
pg_tz * log_timezone
Definition: pgtz.c:31
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define TABLESPACE_MAP
Definition: xlog.h:324
#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:950
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
bool forcePageWrites
Definition: xlog.c:549
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:101
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:34
int errcode_for_file_access(void)
Definition: elog.c:598
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2094
unsigned int uint32
Definition: c.h:268
int64 sendTablespace(char *path, bool sizeonly)
Definition: basebackup.c:906
#define CHECKPOINT_FORCE
Definition: xlog.h:180
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define WARNING
Definition: elog.h:40
int nonExclusiveBackups
Definition: xlog.c:561
#define MAXFNAMELEN
#define SpinLockRelease(lock)
Definition: spin.h:64
static void pg_start_backup_callback(int code, Datum arg)
Definition: xlog.c:10615
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:560
uintptr_t Datum
Definition: postgres.h:372
static ControlFileData * ControlFile
Definition: xlog.c:714
#define BoolGetDatum(X)
Definition: postgres.h:408
bool fullPageWrites
Definition: pg_control.h:40
#define CHECKPOINT_WAIT
Definition: xlog.h:184
#define XLByteToSeg(xlrp, logSegNo)
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2401
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1617
static XLogCtlData * XLogCtl
Definition: xlog.c:706
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#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:1314
int FreeFile(FILE *file)
Definition: fd.c:2277
void * palloc(Size size)
Definition: mcxt.c:849
TimeLineID ThisTimeLineID
Definition: pg_control.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:179
#define relpath(rnode, forknum)
Definition: relpath.h:71
char * DataDir
Definition: globals.c:59
#define BACKUP_LABEL_FILE
Definition: xlog.h:321
int pg_fsync(int fd)
Definition: fd.c:333
char d_name[MAX_PATH]
Definition: dirent.h:14
XLogRecPtr checkPoint
Definition: pg_control.h:132
XLogRecPtr redo
Definition: pg_control.h:35
void RequestCheckpoint(int flags)
Definition: checkpointer.c:967
XLogRecPtr do_pg_stop_backup ( char *  labelfile,
bool  waitforarchive,
TimeLineID stoptli_p 
)

Definition at line 10681 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, FreeFile(), XLogCtlData::info_lck, XLogCtlData::Insert, XLogCtlData::lastFpwDisableRecPtr, log_timezone, LW_SHARED, LWLockAcquire(), LWLockRelease(), MAXFNAMELEN, MAXPGPATH, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, XLogCtlInsert::nonExclusiveBackups, NOTICE, NULL, palloc(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, pg_localtime(), pg_stop_backup_callback(), pg_strftime(), pg_usleep(), RecoveryInProgress(), remaining, RequestXLogSwitch(), SESSION_BACKUP_NONE, sessionBackupState, SpinLockAcquire, SpinLockRelease, TABLESPACE_MAP, ThisTimeLineID, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WARNING, XLByteToPrevSeg, XLByteToSeg, XLOG_BACKUP_END, XLogArchiveIsBusy(), XLogArchivingActive, XLogBeginInsert(), XLogFileName, XLogInsert(), XLogIsNeeded, XLogRegisterData(), and XLogSegSize.

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

10682 {
10683  bool exclusive = (labelfile == NULL);
10684  bool backup_started_in_recovery = false;
10685  XLogRecPtr startpoint;
10686  XLogRecPtr stoppoint;
10687  TimeLineID stoptli;
10688  pg_time_t stamp_time;
10689  char strfbuf[128];
10690  char histfilepath[MAXPGPATH];
10691  char startxlogfilename[MAXFNAMELEN];
10692  char stopxlogfilename[MAXFNAMELEN];
10693  char lastxlogfilename[MAXFNAMELEN];
10694  char histfilename[MAXFNAMELEN];
10695  char backupfrom[20];
10696  XLogSegNo _logSegNo;
10697  FILE *lfp;
10698  FILE *fp;
10699  char ch;
10700  int seconds_before_warning;
10701  int waits = 0;
10702  bool reported_waiting = false;
10703  char *remaining;
10704  char *ptr;
10705  uint32 hi,
10706  lo;
10707 
10708  backup_started_in_recovery = RecoveryInProgress();
10709 
10710  /*
10711  * Currently only non-exclusive backup can be taken during recovery.
10712  */
10713  if (backup_started_in_recovery && exclusive)
10714  ereport(ERROR,
10715  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10716  errmsg("recovery is in progress"),
10717  errhint("WAL control functions cannot be executed during recovery.")));
10718 
10719  /*
10720  * During recovery, we don't need to check WAL level. Because, if WAL
10721  * level is not sufficient, it's impossible to get here during recovery.
10722  */
10723  if (!backup_started_in_recovery && !XLogIsNeeded())
10724  ereport(ERROR,
10725  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10726  errmsg("WAL level not sufficient for making an online backup"),
10727  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
10728 
10729  if (exclusive)
10730  {
10731  /*
10732  * At first, mark that we're now stopping an exclusive backup, to
10733  * ensure that there are no other sessions currently running
10734  * pg_start_backup() or pg_stop_backup().
10735  */
10738  {
10740  ereport(ERROR,
10741  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10742  errmsg("exclusive backup not in progress")));
10743  }
10746 
10747  /*
10748  * Remove backup_label. In case of failure, the state for an exclusive
10749  * backup is switched back to in-progress.
10750  */
10752  {
10753  /*
10754  * Read the existing label file into memory.
10755  */
10756  struct stat statbuf;
10757  int r;
10758 
10759  if (stat(BACKUP_LABEL_FILE, &statbuf))
10760  {
10761  /* should not happen per the upper checks */
10762  if (errno != ENOENT)
10763  ereport(ERROR,
10765  errmsg("could not stat file \"%s\": %m",
10766  BACKUP_LABEL_FILE)));
10767  ereport(ERROR,
10768  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10769  errmsg("a backup is not in progress")));
10770  }
10771 
10772  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
10773  if (!lfp)
10774  {
10775  ereport(ERROR,
10777  errmsg("could not read file \"%s\": %m",
10778  BACKUP_LABEL_FILE)));
10779  }
10780  labelfile = palloc(statbuf.st_size + 1);
10781  r = fread(labelfile, statbuf.st_size, 1, lfp);
10782  labelfile[statbuf.st_size] = '\0';
10783 
10784  /*
10785  * Close and remove the backup label file
10786  */
10787  if (r != 1 || ferror(lfp) || FreeFile(lfp))
10788  ereport(ERROR,
10790  errmsg("could not read file \"%s\": %m",
10791  BACKUP_LABEL_FILE)));
10793 
10794  /*
10795  * Remove tablespace_map file if present, it is created only if
10796  * there are tablespaces.
10797  */
10799  }
10801  }
10802 
10803  /*
10804  * OK to update backup counters and forcePageWrites
10805  */
10807  if (exclusive)
10808  {
10810  }
10811  else
10812  {
10813  /*
10814  * The user-visible pg_start/stop_backup() functions that operate on
10815  * exclusive backups can be called at any time, but for non-exclusive
10816  * backups, it is expected that each do_pg_start_backup() call is
10817  * matched by exactly one do_pg_stop_backup() call.
10818  */
10821  }
10822 
10825  {
10826  XLogCtl->Insert.forcePageWrites = false;
10827  }
10829 
10830  /* Clean up session-level lock */
10832 
10833  /*
10834  * Read and parse the START WAL LOCATION line (this code is pretty crude,
10835  * but we are not expecting any variability in the file format).
10836  */
10837  if (sscanf(labelfile, "START WAL LOCATION: %X/%X (file %24s)%c",
10838  &hi, &lo, startxlogfilename,
10839  &ch) != 4 || ch != '\n')
10840  ereport(ERROR,
10841  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10842  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
10843  startpoint = ((uint64) hi) << 32 | lo;
10844  remaining = strchr(labelfile, '\n') + 1; /* %n is not portable enough */
10845 
10846  /*
10847  * Parse the BACKUP FROM line. If we are taking an online backup from the
10848  * standby, we confirm that the standby has not been promoted during the
10849  * backup.
10850  */
10851  ptr = strstr(remaining, "BACKUP FROM:");
10852  if (!ptr || sscanf(ptr, "BACKUP FROM: %19s\n", backupfrom) != 1)
10853  ereport(ERROR,
10854  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10855  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
10856  if (strcmp(backupfrom, "standby") == 0 && !backup_started_in_recovery)
10857  ereport(ERROR,
10858  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10859  errmsg("the standby was promoted during online backup"),
10860  errhint("This means that the backup being taken is corrupt "
10861  "and should not be used. "
10862  "Try taking another online backup.")));
10863 
10864  /*
10865  * During recovery, we don't write an end-of-backup record. We assume that
10866  * pg_control was backed up last and its minimum recovery point can be
10867  * available as the backup end location. Since we don't have an
10868  * end-of-backup record, we use the pg_control value to check whether
10869  * we've reached the end of backup when starting recovery from this
10870  * backup. We have no way of checking if pg_control wasn't backed up last
10871  * however.
10872  *
10873  * We don't force a switch to new WAL file and wait for all the required
10874  * files to be archived. This is okay if we use the backup to start the
10875  * standby. But, if it's for an archive recovery, to ensure all the
10876  * required files are available, a user should wait for them to be
10877  * archived, or include them into the backup.
10878  *
10879  * We return the current minimum recovery point as the backup end
10880  * location. Note that it can be greater than the exact backup end
10881  * location if the minimum recovery point is updated after the backup of
10882  * pg_control. This is harmless for current uses.
10883  *
10884  * XXX currently a backup history file is for informational and debug
10885  * purposes only. It's not essential for an online backup. Furthermore,
10886  * even if it's created, it will not be archived during recovery because
10887  * an archiver is not invoked. So it doesn't seem worthwhile to write a
10888  * backup history file during recovery.
10889  */
10890  if (backup_started_in_recovery)
10891  {
10892  XLogRecPtr recptr;
10893 
10894  /*
10895  * Check to see if all WAL replayed during online backup contain
10896  * full-page writes.
10897  */
10899  recptr = XLogCtl->lastFpwDisableRecPtr;
10901 
10902  if (startpoint <= recptr)
10903  ereport(ERROR,
10904  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
10905  errmsg("WAL generated with full_page_writes=off was replayed "
10906  "during online backup"),
10907  errhint("This means that the backup being taken on the standby "
10908  "is corrupt and should not be used. "
10909  "Enable full_page_writes and run CHECKPOINT on the master, "
10910  "and then try an online backup again.")));
10911 
10912 
10913  LWLockAcquire(ControlFileLock, LW_SHARED);
10914  stoppoint = ControlFile->minRecoveryPoint;
10915  stoptli = ControlFile->minRecoveryPointTLI;
10916  LWLockRelease(ControlFileLock);
10917 
10918  if (stoptli_p)
10919  *stoptli_p = stoptli;
10920  return stoppoint;
10921  }
10922 
10923  /*
10924  * Write the backup-end xlog record
10925  */
10926  XLogBeginInsert();
10927  XLogRegisterData((char *) (&startpoint), sizeof(startpoint));
10928  stoppoint = XLogInsert(RM_XLOG_ID, XLOG_BACKUP_END);
10929  stoptli = ThisTimeLineID;
10930 
10931  /*
10932  * Force a switch to a new xlog segment file, so that the backup is valid
10933  * as soon as archiver moves out the current segment file.
10934  */
10935  RequestXLogSwitch(false);
10936 
10937  XLByteToPrevSeg(stoppoint, _logSegNo);
10938  XLogFileName(stopxlogfilename, ThisTimeLineID, _logSegNo);
10939 
10940  /* Use the log timezone here, not the session timezone */
10941  stamp_time = (pg_time_t) time(NULL);
10942  pg_strftime(strfbuf, sizeof(strfbuf),
10943  "%Y-%m-%d %H:%M:%S %Z",
10944  pg_localtime(&stamp_time, log_timezone));
10945 
10946  /*
10947  * Write the backup history file
10948  */
10949  XLByteToSeg(startpoint, _logSegNo);
10950  BackupHistoryFilePath(histfilepath, ThisTimeLineID, _logSegNo,
10951  (uint32) (startpoint % XLogSegSize));
10952  fp = AllocateFile(histfilepath, "w");
10953  if (!fp)
10954  ereport(ERROR,
10956  errmsg("could not create file \"%s\": %m",
10957  histfilepath)));
10958  fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
10959  (uint32) (startpoint >> 32), (uint32) startpoint, startxlogfilename);
10960  fprintf(fp, "STOP WAL LOCATION: %X/%X (file %s)\n",
10961  (uint32) (stoppoint >> 32), (uint32) stoppoint, stopxlogfilename);
10962  /* transfer remaining lines from label to history file */
10963  fprintf(fp, "%s", remaining);
10964  fprintf(fp, "STOP TIME: %s\n", strfbuf);
10965  if (fflush(fp) || ferror(fp) || FreeFile(fp))
10966  ereport(ERROR,
10968  errmsg("could not write file \"%s\": %m",
10969  histfilepath)));
10970 
10971  /*
10972  * Clean out any no-longer-needed history files. As a side effect, this
10973  * will post a .ready file for the newly created history file, notifying
10974  * the archiver that history file may be archived immediately.
10975  */
10977 
10978  /*
10979  * If archiving is enabled, wait for all the required WAL files to be
10980  * archived before returning. If archiving isn't enabled, the required WAL
10981  * needs to be transported via streaming replication (hopefully with
10982  * wal_keep_segments set high enough), or some more exotic mechanism like
10983  * polling and copying files from pg_wal with script. We have no knowledge
10984  * of those mechanisms, so it's up to the user to ensure that he gets all
10985  * the required WAL.
10986  *
10987  * We wait until both the last WAL file filled during backup and the
10988  * history file have been archived, and assume that the alphabetic sorting
10989  * property of the WAL files ensures any earlier WAL files are safely
10990  * archived as well.
10991  *
10992  * We wait forever, since archive_command is supposed to work and we
10993  * assume the admin wanted his backup to work completely. If you don't
10994  * wish to wait, then either waitforarchive should be passed in as false,
10995  * or you can set statement_timeout. Also, some notices are issued to
10996  * clue in anyone who might be doing this interactively.
10997  */
10998  if (waitforarchive && XLogArchivingActive())
10999  {
11000  XLByteToPrevSeg(stoppoint, _logSegNo);
11001  XLogFileName(lastxlogfilename, ThisTimeLineID, _logSegNo);
11002 
11003  XLByteToSeg(startpoint, _logSegNo);
11004  BackupHistoryFileName(histfilename, ThisTimeLineID, _logSegNo,
11005  (uint32) (startpoint % XLogSegSize));
11006 
11007  seconds_before_warning = 60;
11008  waits = 0;
11009 
11010  while (XLogArchiveIsBusy(lastxlogfilename) ||
11011  XLogArchiveIsBusy(histfilename))
11012  {
11014 
11015  if (!reported_waiting && waits > 5)
11016  {
11017  ereport(NOTICE,
11018  (errmsg("pg_stop_backup cleanup done, waiting for required WAL segments to be archived")));
11019  reported_waiting = true;
11020  }
11021 
11022  pg_usleep(1000000L);
11023 
11024  if (++waits >= seconds_before_warning)
11025  {
11026  seconds_before_warning *= 2; /* This wraps in >10 years... */
11027  ereport(WARNING,
11028  (errmsg("pg_stop_backup still waiting for all required WAL segments to be archived (%d seconds elapsed)",
11029  waits),
11030  errhint("Check that your archive_command is executing properly. "
11031  "pg_stop_backup can be canceled safely, "
11032  "but the database backup will not be usable without all the WAL segments.")));
11033  }
11034  }
11035 
11036  ereport(NOTICE,
11037  (errmsg("pg_stop_backup complete, all required WAL segments have been archived")));
11038  }
11039  else if (waitforarchive)
11040  ereport(NOTICE,
11041  (errmsg("WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup")));
11042 
11043  /*
11044  * We're done. As a convenience, return the ending WAL location.
11045  */
11046  if (stoptli_p)
11047  *stoptli_p = stoptli;
11048  return stoppoint;
11049 }
int remaining
Definition: informix.c:692
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:122
#define XLogSegSize
Definition: xlog_internal.h:92
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9401
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:987
uint32 TimeLineID
Definition: xlogdefs.h:45
int64 pg_time_t
Definition: pgtime.h:23
static void WALInsertLockRelease(void)
Definition: xlog.c:1646
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:171
XLogRecPtr lastFpwDisableRecPtr
Definition: xlog.c:701
static SessionBackupState sessionBackupState
Definition: xlog.c:510
#define XLogIsNeeded()
Definition: xlog.h:145
slock_t info_lck
Definition: xlog.c:703
#define XLogFileName(fname, tli, logSegNo)
int errcode(int sqlerrcode)
Definition: elog.c:575
XLogCtlInsert Insert
Definition: xlog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7873
static bool backup_started_in_recovery
Definition: basebackup.c:73
pg_tz * log_timezone
Definition: pgtz.c:31
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define TABLESPACE_MAP
Definition: xlog.h:324
#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
bool forcePageWrites
Definition: xlog.c:549
#define ERROR
Definition: elog.h:43
static void CleanupBackupHistory(void)
Definition: xlog.c:4101
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:34
int errcode_for_file_access(void)
Definition: elog.c:598
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2094
unsigned int uint32
Definition: c.h:268
#define XLByteToPrevSeg(xlrp, logSegNo)
#define ereport(elevel, rest)
Definition: elog.h:122
#define XLOG_BACKUP_END
Definition: pg_control.h:71
#define WARNING
Definition: elog.h:40
int nonExclusiveBackups
Definition: xlog.c:561
#define MAXFNAMELEN
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
ExclusiveBackupState exclusiveBackupState
Definition: xlog.c:560
uintptr_t Datum
Definition: postgres.h:372
static ControlFileData * ControlFile
Definition: xlog.c:714
#define BoolGetDatum(X)
Definition: postgres.h:408
TimeLineID ThisTimeLineID
Definition: xlog.c:179
#define NOTICE
Definition: elog.h:37
#define XLByteToSeg(xlrp, logSegNo)
bool XLogArchiveIsBusy(const char *xlog)
Definition: xlogarchive.c:657
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define XLogArchivingActive()
Definition: xlog.h:134
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1617
static XLogCtlData * XLogCtl
Definition: xlog.c:706
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
static void pg_stop_backup_callback(int code, Datum arg)
Definition: xlog.c:10644
#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:1314
int durable_unlink(const char *fname, int elevel)
Definition: fd.c:676
int FreeFile(FILE *file)
Definition: fd.c:2277
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define BackupHistoryFilePath(path, tli, logSegNo, offset)
#define BACKUP_LABEL_FILE
Definition: xlog.h:321
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define BackupHistoryFileName(fname, tli, logSegNo, offset)
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:170
SessionBackupState get_backup_status ( void  )

Definition at line 10662 of file xlog.c.

References sessionBackupState.

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

10663 {
10664  return sessionBackupState;
10665 }
static SessionBackupState sessionBackupState
Definition: xlog.c:510
TimestampTz GetCurrentChunkReplayStartTime ( void  )

Definition at line 6091 of file xlog.c.

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

Referenced by GetReplicationApplyDelay().

6092 {
6093  TimestampTz xtime;
6094 
6096  xtime = XLogCtl->currentChunkStartTime;
6098 
6099  return xtime;
6100 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
TimestampTz currentChunkStartTime
Definition: xlog.c:693
XLogRecPtr GetFakeLSNForUnloggedRel ( void  )

Definition at line 4726 of file xlog.c.

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

Referenced by gistGetFakeLSN().

4727 {
4728  XLogRecPtr nextUnloggedLSN;
4729 
4730  /* increment the unloggedLSN counter, need SpinLock */
4732  nextUnloggedLSN = XLogCtl->unloggedLSN++;
4734 
4735  return nextUnloggedLSN;
4736 }
XLogRecPtr unloggedLSN
Definition: xlog.c:589
#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:706
slock_t ulsn_lck
Definition: xlog.c:590
XLogRecPtr GetFlushRecPtr ( void  )

Definition at line 8222 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(), read_local_xlog_page(), StartReplication(), WalSndWaitForWal(), XLogSendLogical(), and XLogSendPhysical().

8223 {
8227 
8228  return LogwrtResult.Flush;
8229 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:748
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:600
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
XLogRecPtr Flush
Definition: xlog.c:424
void GetFullPageWriteInfo ( XLogRecPtr RedoRecPtr_p,
bool doPageWrites_p 
)

Definition at line 8191 of file xlog.c.

References doPageWrites, and RedoRecPtr.

Referenced by XLogCheckBufferNeedsBackup(), and XLogInsert().

8192 {
8193  *RedoRecPtr_p = RedoRecPtr;
8194  *doPageWrites_p = doPageWrites;
8195 }
static bool doPageWrites
Definition: xlog.c:357
static XLogRecPtr RedoRecPtr
Definition: xlog.c:350
XLogRecPtr GetInsertRecPtr ( void  )

Definition at line 8206 of file xlog.c.

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

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

8207 {
8208  XLogRecPtr recptr;
8209 
8211  recptr = XLogCtl->LogwrtRqst.Write;
8213 
8214  return recptr;
8215 }
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr Write
Definition: xlog.c:417
XLogwrtRqst LogwrtRqst
Definition: xlog.c:578
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:706
XLogRecPtr GetLastImportantRecPtr ( void  )

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

8241 {
8243  int i;
8244 
8245  for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
8246  {
8247  XLogRecPtr last_important;
8248 
8249  /*
8250  * Need to take a lock to prevent torn reads of the LSN, which are
8251  * possible on some of the supported platforms. WAL insert locks only
8252  * support exclusive mode, so we have to use that.
8253  */
8255  last_important = WALInsertLocks[i].l.lastImportantAt;
8256  LWLockRelease(&WALInsertLocks[i].l.lock);
8257 
8258  if (res < last_important)
8259  res = last_important;
8260  }
8261 
8262  return res;
8263 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr lastImportantAt
Definition: xlog.c:467
#define NUM_XLOGINSERT_LOCKS
Definition: xlog.c:118
WALInsertLock l
Definition: xlog.c:479
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int i
static WALInsertLockPadded * WALInsertLocks
Definition: xlog.c:709
TimestampTz GetLatestXTime ( void  )

Definition at line 6061 of file xlog.c.

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

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

6062 {
6063  TimestampTz xtime;
6064 
6066  xtime = XLogCtl->recoveryLastXTime;
6068 
6069  return xtime;
6070 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
TimestampTz recoveryLastXTime
Definition: xlog.c:687
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
char* GetMockAuthenticationNonce ( void  )

Definition at line 4700 of file xlog.c.

References Assert, ControlFileData::mock_authentication_nonce, and NULL.

Referenced by scram_mock_salt().

4701 {
4702  Assert(ControlFile != NULL);
4704 }
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:232
static ControlFileData * ControlFile
Definition: xlog.c:714
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void GetNextXidAndEpoch ( TransactionId xid,
uint32 epoch 
)

Definition at line 8291 of file xlog.c.

References XLogCtlData::ckptXid, XLogCtlData::ckptXidEpoch, XLogCtlData::info_lck, ReadNewTransactionId(), SpinLockAcquire, and SpinLockRelease.

Referenced by load_xid_epoch(), TransactionIdInRecentPast(), and XLogWalRcvSendHSFeedback().

8292 {
8293  uint32 ckptXidEpoch;
8294  TransactionId ckptXid;
8295  TransactionId nextXid;
8296 
8297  /* Must read checkpoint info first, else have race condition */
8299  ckptXidEpoch = XLogCtl->ckptXidEpoch;
8300  ckptXid = XLogCtl->ckptXid;
8302 
8303  /* Now fetch current nextXid */
8304  nextXid = ReadNewTransactionId();
8305 
8306  /*
8307  * nextXid is certainly logically later than ckptXid. So if it's
8308  * numerically less, it must have wrapped into the next epoch.
8309  */
8310  if (nextXid < ckptXid)
8311  ckptXidEpoch++;
8312 
8313  *xid = nextXid;
8314  *epoch = ckptXidEpoch;
8315 }
TransactionId ckptXid
Definition: xlog.c:581
uint32 TransactionId
Definition: c.h:397
slock_t info_lck
Definition: xlog.c:703
uint32 ckptXidEpoch
Definition: xlog.c:580
#define SpinLockAcquire(lock)
Definition: spin.h:62
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
unsigned int uint32
Definition: c.h:268
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
XLogRecPtr GetRedoRecPtr ( void  )

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

8164 {
8165  XLogRecPtr ptr;
8166 
8167  /*
8168  * The possibly not up-to-date copy in XlogCtl is enough. Even if we
8169  * grabbed a WAL insertion lock to read the master copy, someone might
8170  * update it just after we've released the lock.
8171  */
8173  ptr = XLogCtl->RedoRecPtr;
8175 
8176  if (RedoRecPtr < ptr)
8177  RedoRecPtr = ptr;
8178 
8179  return RedoRecPtr;
8180 }
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
static XLogRecPtr RedoRecPtr
Definition: xlog.c:350
XLogRecPtr RedoRecPtr
Definition: xlog.c:579
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogCtlData * XLogCtl
Definition: xlog.c:706
uint64 GetSystemIdentifier ( void  )

Definition at line 4690 of file xlog.c.

References Assert, NULL, and ControlFileData::system_identifier.

Referenced by IdentifySystem(), and WalReceiverMain().

4691 {
4692  Assert(ControlFile != NULL);
4694 }
uint64 system_identifier
Definition: pg_control.h:109
static ControlFileData * ControlFile
Definition: xlog.c:714
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
XLogRecPtr GetXLogInsertRecPtr ( void  )

Definition at line 11103 of file xlog.c.

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

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

11104 {
11106  uint64 current_bytepos;
11107 
11108  SpinLockAcquire(&Insert->insertpos_lck);
11109  current_bytepos = Insert->CurrBytePos;
11110  SpinLockRelease(&Insert->insertpos_lck);
11111 
11112  return XLogBytePosToRecPtr(current_bytepos);
11113 }
slock_t insertpos_lck
Definition: xlog.c:517
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1915
XLogCtlInsert Insert
Definition: xlog.c:575
#define SpinLockAcquire(lock)
Definition: spin.h:62
uint64 CurrBytePos
Definition: xlog.c:526
static void Insert(File file)
Definition: fd.c:1044
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
void GetXLogReceiptTime ( TimestampTz rtime,
bool fromStream 
)

Definition at line 6107 of file xlog.c.

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

Referenced by GetStandbyLimitTime().

6108 {
6109  /*
6110  * This must be executed in the startup process, since we don't export the
6111  * relevant state to shared memory.
6112  */
6113  Assert(InRecovery);
6114 
6115  *rtime = XLogReceiptTime;
6116  *fromStream = (XLogReceiptSource == XLOG_FROM_STREAM);
6117 }
static XLogSource XLogReceiptSource
Definition: xlog.c:815
bool InRecovery
Definition: xlog.c:192
static TimestampTz XLogReceiptTime
Definition: xlog.c:814
#define Assert(condition)
Definition: c.h:675
XLogRecPtr GetXLogReplayRecPtr ( TimeLineID replayTLI)

Definition at line 11084 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(), read_local_xlog_page(), WalReceiverMain(), WalSndWaitForWal(), and XLogWalRcvSendReply().

11085 {
11086  XLogRecPtr recptr;
11087  TimeLineID tli;
11088 
11090  recptr = XLogCtl->lastReplayedEndRecPtr;
11091  tli = XLogCtl->lastReplayedTLI;
11093 
11094  if (replayTLI)
11095  *replayTLI = tli;
11096  return recptr;
11097 }
uint32 TimeLineID
Definition: xlogdefs.h:45
slock_t info_lck
Definition: xlog.c:703
#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:706
TimeLineID lastReplayedTLI
Definition: xlog.c:683
XLogRecPtr lastReplayedEndRecPtr
Definition: xlog.c:682
XLogRecPtr GetXLogWriteRecPtr ( void  )

Definition at line 11119 of file xlog.c.

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

Referenced by pg_attribute_noreturn(), and pg_current_wal_lsn().

11120 {
11124 
11125  return LogwrtResult.Write;
11126 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:748
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:600
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
XLogRecPtr Write
Definition: xlog.c:423
bool HotStandbyActive ( void  )

Definition at line 7929 of file xlog.c.

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

Referenced by XLogWalRcvSendHSFeedback().

7930 {
7931  /*
7932  * We check shared state each time only until Hot Standby is active. We
7933  * can't de-activate Hot Standby, so there's no need to keep checking
7934  * after the shared variable has once been seen true.
7935  */
7937  return true;
7938  else
7939  {
7940  /* spinlock is essential on machines with weak memory ordering! */
7944 
7945  return LocalHotStandbyActive;
7946  }
7947 }
bool SharedHotStandbyActive
Definition: xlog.c:648
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
static bool LocalHotStandbyActive
Definition: xlog.c:221
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:706
bool HotStandbyActiveInReplay ( void  )

Definition at line 7954 of file xlog.c.

References AmStartupProcess, Assert, IsPostmasterEnvironment, and LocalHotStandbyActive.

Referenced by btree_xlog_vacuum().

7955 {
7957  return LocalHotStandbyActive;
7958 }
#define AmStartupProcess()
Definition: miscadmin.h:403
bool IsPostmasterEnvironment
Definition: globals.c:99
static bool LocalHotStandbyActive
Definition: xlog.c:221
#define Assert(condition)
Definition: c.h:675
void InitXLOGAccess ( void  )

Definition at line 8140 of file xlog.c.

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

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

8141 {
8143 
8144  /* ThisTimeLineID doesn't change so we need no lock to copy it */
8147 
8148  /* Use GetRedoRecPtr to copy the RedoRecPtr safely */
8149  (void) GetRedoRecPtr();
8150  /* Also update our copy of doPageWrites. */
8151  doPageWrites = (Insert->fullPageWrites || Insert->forcePageWrites);
8152 
8153  /* Also initialize the working areas for constructing WAL records */
8154  InitXLogInsert();
8155 }
void InitXLogInsert(void)
Definition: xloginsert.c:1028
TimeLineID ThisTimeLineID
Definition: xlog.c:629
XLogCtlInsert Insert
Definition: xlog.c:575
bool fullPageWrites
Definition: xlog.c:550
static bool doPageWrites
Definition: xlog.c:357
bool forcePageWrites
Definition: xlog.c:549
static void Insert(File file)
Definition: fd.c:1044
TimeLineID ThisTimeLineID
Definition: xlog.c:179
#define Assert(condition)
Definition: c.h:675
static XLogCtlData * XLogCtl
Definition: xlog.c:706
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8163
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
void issue_xlog_fsync ( int  fd,
XLogSegNo  segno 
)

Definition at line 10083 of file xlog.c.

References elog, ereport, errcode_for_file_access(), errmsg(), PANIC, pg_fdatasync(), pg_fsync_no_writethrough(), pg_fsync_writethrough(), sync_method, SYNC_METHOD_FDATASYNC, SYNC_METHOD_FSYNC, SYNC_METHOD_FSYNC_WRITETHROUGH, SYNC_METHOD_OPEN, SYNC_METHOD_OPEN_DSYNC, ThisTimeLineID, and XLogFileNameP().

Referenced by XLogWalRcvFlush(), and XLogWrite().

10084 {
10085  switch (sync_method)
10086  {
10087  case SYNC_METHOD_FSYNC:
10088  if (pg_fsync_no_writethrough(fd) != 0)
10089  ereport(PANIC,
10091  errmsg("could not fsync log file %s: %m",
10092  XLogFileNameP(ThisTimeLineID, segno))));
10093  break;
10094 #ifdef HAVE_FSYNC_WRITETHROUGH
10096  if (pg_fsync_writethrough(fd) != 0)
10097  ereport(PANIC,
10099  errmsg("could not fsync write-through log file %s: %m",
10100  XLogFileNameP(ThisTimeLineID, segno))));
10101  break;
10102 #endif
10103 #ifdef HAVE_FDATASYNC
10104  case SYNC_METHOD_FDATASYNC:
10105  if (pg_fdatasync(fd) != 0)
10106  ereport(PANIC,
10108  errmsg("could not fdatasync log file %s: %m",
10109  XLogFileNameP(ThisTimeLineID, segno))));
10110  break;
10111 #endif
10112  case SYNC_METHOD_OPEN:
10114  /* write synced it already */
10115  break;
10116  default:
10117  elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
10118  break;
10119  }
10120 }
int pg_fdatasync(int fd)
Definition: fd.c:385
#define SYNC_METHOD_FSYNC_WRITETHROUGH
Definition: xlog.h:28
int pg_fsync_writethrough(int fd)
Definition: fd.c:362
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:350
#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
char * XLogFileNameP(TimeLineID tli, XLogSegNo segno)
Definition: xlog.c:10126
int errcode_for_file_access(void)
Definition: elog.c:598
#define SYNC_METHOD_FSYNC
Definition: xlog.h:25
#define ereport(elevel, rest)
Definition: elog.h:122
#define SYNC_METHOD_OPEN
Definition: xlog.h:27
TimeLineID ThisTimeLineID
Definition: xlog.c:179
int sync_method
Definition: xlog.c:103
#define SYNC_METHOD_FDATASYNC
Definition: xlog.h:26
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
bool RecoveryInProgress ( void  )

Definition at line 7873 of file xlog.c.

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

Referenced by BackgroundWriterMain(), 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(), OldSerXidSetActiveSerXmin(), 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_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(), ShutdownXLOG(), SnapBuildWaitSnapshot(), standard_ProcessUtility(), StartLogicalReplication(), StartTransaction(), TransactionIdIsInProgress(), TruncateMultiXact(), UpdateFullPageWrites(), WalReceiverMain(), WalSndShutdown(), WalSndWaitForWal(), XLogBackgroundFlush(), XLogInsertAllowed(), XLogNeedsFlush(), and XLogSendPhysical().

7874 {
7875  /*
7876  * We check shared state each time only until we leave recovery mode. We
7877  * can't re-enter recovery, so there's no need to keep checking after the
7878  * shared variable has once been seen false.
7879  */
7881  return false;
7882  else
7883  {
7884  /*
7885  * use volatile pointer to make sure we make a fresh read of the
7886  * shared variable.
7887  */
7888  volatile XLogCtlData *xlogctl = XLogCtl;
7889 
7891 
7892  /*
7893  * Initialize TimeLineID and RedoRecPtr when we discover that recovery
7894  * is finished. InitPostgres() relies upon this behaviour to ensure
7895  * that InitXLOGAccess() is called at backend startup. (If you change
7896  * this, see also LocalSetXLogInsertAllowed.)
7897  */
7899  {
7900  /*
7901  * If we just exited recovery, make sure we read TimeLineID and
7902  * RedoRecPtr after SharedRecoveryInProgress (for machines with
7903  * weak memory ordering).
7904  */
7906  InitXLOGAccess();
7907  }
7908 
7909  /*
7910  * Note: We don't need a memory barrier when we're still in recovery.
7911  * We might exit recovery immediately after return, so the caller
7912  * can't rely on 'true' meaning that we're still in recovery anyway.
7913  */
7914 
7915  return LocalRecoveryInProgress;
7916  }
7917 }
void InitXLOGAccess(void)
Definition: xlog.c:8140
bool SharedRecoveryInProgress
Definition: xlog.c:642
#define pg_memory_barrier()
Definition: atomics.h:148
static XLogCtlData * XLogCtl
Definition: xlog.c:706
static bool LocalRecoveryInProgress
Definition: xlog.c:215
bool RecoveryIsPaused ( void  )

Definition at line 5930 of file xlog.c.

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

Referenced by pg_is_wal_replay_paused(), and recoveryPausesHere().

5931 {
5932  bool recoveryPause;
5933 
5935  recoveryPause = XLogCtl->recoveryPause;
5937 
5938  return recoveryPause;
5939 }
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:695
static XLogCtlData * XLogCtl
Definition: xlog.c:706
void RemovePromoteSignalFiles ( void  )

Definition at line 12065 of file xlog.c.

References FALLBACK_PROMOTE_SIGNAL_FILE, PROMOTE_SIGNAL_FILE, and unlink().

Referenced by PostmasterMain().

12066 {
12069 }
#define FALLBACK_PROMOTE_SIGNAL_FILE
Definition: xlog.c:85
#define PROMOTE_SIGNAL_FILE
Definition: xlog.c:84
int unlink(const char *filename)
void SetRecoveryPause ( bool  recoveryPause)

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

5943 {
5945  XLogCtl->recoveryPause = recoveryPause;
5947 }
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:695
static XLogCtlData * XLogCtl
Definition: xlog.c:706
void SetWalWriterSleeping ( bool  sleeping)

Definition at line 12101 of file xlog.c.

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

Referenced by WalWriterMain().

12102 {
12104  XLogCtl->WalWriterSleeping = sleeping;
12106 }
slock_t info_lck
Definition: xlog.c:703
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool WalWriterSleeping
Definition: xlog.c:655
static XLogCtlData * XLogCtl
Definition: xlog.c:706
void ShutdownXLOG ( int  code,
Datum  arg 
)

Definition at line 8321 of file xlog.c.

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

Referenced by CheckpointerMain(), and InitPostgres().

8322 {
8323  /* Don't be chatty in standalone mode */
8325  (errmsg("shutting down")));
8326 
8327  /*
8328  * Wait for WAL senders to be in stopping state. This prevents commands
8329  * from writing new WAL.
8330  */
8332 
8333  if (RecoveryInProgress())
8335  else
8336  {
8337  /*
8338  * If archiving is enabled, rotate the last XLOG file so that all the
8339  * remaining records are archived (postmaster wakes up the archiver
8340  * process one more time at the end of shutdown). The checkpoint
8341  * record will go to the next XLOG file and won't be archived (yet).
8342  */
8344  RequestXLogSwitch(false);
8345 
8347  }
8348  ShutdownCLOG();
8349  ShutdownCommitTs();
8350  ShutdownSUBTRANS();
8352 }
bool IsPostmasterEnvironment
Definition: globals.c:99
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9401
void ShutdownSUBTRANS(void)
Definition: subtrans.c:282
void CreateCheckPoint(int flags)
Definition: xlog.c:8523
void ShutdownCLOG(void)
Definition: clog.c:575
bool CreateRestartPoint(int flags)
Definition: xlog.c:9070
#define XLogArchiveCommandSet()
Definition: xlog.h:139
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7873
void WalSndWaitStopping(void)
Definition: walsender.c:3045
void ShutdownMultiXact(void)
Definition: multixact.c:2105
#define ereport(elevel, rest)
Definition: elog.h:122
#define NOTICE
Definition: elog.h:37
void ShutdownCommitTs(void)
Definition: commit_ts.c:745
#define XLogArchivingActive()
Definition: xlog.h:134
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:179
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:175
void StartupXLOG ( void  )

Definition at line 6189 of file xlog.c.

References AdvanceOldestClogXid(), AllowCascadeReplication, appendStringInfo(), appendStringInfoString(), archiveCleanupCommand, XLogCtlData::archiveCleanupCommand, ArchiveRecoveryRequested, ErrorContextCallback::arg, Assert, 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::ckptXid, XLogCtlData::ckptXidEpoch, close, CompleteCommitTsInitialization(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), XLogCtlInsert::CurrBytePos, XLogCtlData::currentChunkStartTime, 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, EndRecPtr, ereport, errcode(), errcode_for_file_access(), errdetail(), errhint(), errmsg(), errmsg_internal(), ERROR, error_context_stack, ExecuteRecoveryCommand(), exitArchiveRecovery(), fast_promote, FATAL, findNewestTimeLine(), 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::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, VariableCacheData::nextOid, CheckPoint::nextXid, RunningTransactionsData::nextXid, VariableCacheData::nextXid, CheckPoint::nextXidEpoch, NIL, NOTICE, NULL, 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, ControlFileData::prevCheckPoint, 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(), ReadControlFile(), readFile, readOff, XLogReaderState::readPageTLI, ReadRecord(), readRecoveryCommandFile(), ReadRecPtr, RecordKnownAssignedTransactionIds(), RecoverPreparedTransactions(), 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_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(), 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, SendPostmasterSignal(), SetCommitTsLimit(), SetForwardFsyncRequests(), SetMultiXactIdLimit(), SetRecoveryPause(), SetTransactionIdLimit(), XLogCtlData::SharedRecoveryInProgress, ShmemVariableCache, ShutdownRecoveryTransactionEnvironment(), ShutdownWalRcv(), snprintf(), SpinLockAcquire, SpinLockRelease, STANDBY_DISABLED, STANDBY_INITIALIZED, StandbyMode, StandbyModeRequested, StandbyRecoverPreparedTransactions(), standbyState, StartupCLOG(), StartupCommitTs(), StartupMultiXact(), StartupReorderBuffer(), StartupReplicationOrigin(), StartupReplicationSlots(), StartupSUBTRANS(), ControlFileData::state, str_time(), strlcpy(), RunningTransactionsData::subxcnt, RunningTransactionsData::subxid_overflow, SyncDataDirectory(), XLogReaderState::system_identifier, ControlFileData::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, track_commit_timestamp, ControlFileData::track_commit_timestamp, TransactionIdAdvance, TransactionIdFollowsOrEquals(), TransactionIdIsNormal, TransactionIdIsValid, TransactionIdRetreat, TrimCLOG(), TrimMultiXact(), unlink(), UNLOGGED_RELATION_CLEANUP, UNLOGGED_RELATION_INIT, ControlFileData::unloggedLSN, XLogCtlData::unloggedLSN, UpdateControlFile(), UpdateFullPageWrites(), ValidateXLOGDirectoryStructure(), WalRcvForceReply(), WalSndWakeup(), XLogwrtRqst::Write, XLogwrtResult::Write, writeTimeLineHistory(), RunningTransactionsData::xcnt, RunningTransactionsData::xids, XLogRecord::xl_info, XLogRecord::xl_rmid, XLogRecord::xl_xid, XLogCtlData::xlblocks, XLByteToPrevSeg, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, xlog_outdesc(), XLogArchiveCleanup(), XLogArchiveIsReadyOrDone(), XLogArchiveNotify(), XLogArchivingActive, XLogFileName, XLogFilePath, XLogPageRead(), XLogReaderAllocate(), XLogReaderFree(), XLogReceiptTime, XLogRecGetData, XLogRecPtrIsInvalid, XLogRecPtrToBufIdx, XLogRecPtrToBytePos(), XLogReportParameters(), XLogSegSize, XLR_CHECK_CONSISTENCY, XLR_INFO_MASK, and XRecOffIsValid.

Referenced by InitPostgres(), and StartupProcessMain().

6190 {
6192  CheckPoint checkPoint;
6193  bool wasShutdown;
6194  bool reachedStopPoint = false;
6195  bool haveBackupLabel = false;
6196  bool haveTblspcMap = false;
6197  XLogRecPtr RecPtr,
6198  checkPointLoc,
6199  EndOfLog;
6200  TimeLineID EndOfLogTLI;
6201  TimeLineID PrevTimeLineID;
6202  XLogRecord *record;
6203  TransactionId oldestActiveXID;
6204  bool backupEndRequired = false;
6205  bool backupFromStandby = false;
6206  DBState dbstate_at_startup;
6207  XLogReaderState *xlogreader;
6208  XLogPageReadPrivate private;
6209  bool fast_promoted = false;
6210  struct stat st;
6211 
6212  /*
6213  * Read control file and check XLOG status looks valid.
6214  *
6215  * Note: in most control paths, *ControlFile is already valid and we need
6216  * not do ReadControlFile() here, but might as well do it to be sure.
6217  */
6218  ReadControlFile();
6219 
6220  if (ControlFile->state < DB_SHUTDOWNED ||
6223  ereport(FATAL,
6224  (errmsg("control file contains invalid data")));
6225 
6227  {
6228  /* This is the expected case, so don't be chatty in standalone mode */
6230  (errmsg("database system was shut down at %s",
6231  str_time(ControlFile->time))));
6232  }
6234  ereport(LOG,
6235  (errmsg("database system was shut down in recovery at %s",
6236  str_time(ControlFile->time))));
6237  else if (ControlFile->state == DB_SHUTDOWNING)
6238  ereport(LOG,
6239  (errmsg("database system shutdown was interrupted; last known up at %s",
6240  str_time(ControlFile->time))));
6241  else if (ControlFile->state == DB_IN_CRASH_RECOVERY)
6242  ereport(LOG,
6243  (errmsg("database system was interrupted while in recovery at %s",
6245  errhint("This probably means that some data is corrupted and"
6246  " you will have to use the last backup for recovery.")));
6248  ereport(LOG,
6249  (errmsg("database system was interrupted while in recovery at log time %s",
6251  errhint("If this has occurred more than once some data might be corrupted"
6252  " and you might need to choose an earlier recovery target.")));
6253  else if (ControlFile->state == DB_IN_PRODUCTION)
6254  ereport(LOG,
6255  (errmsg("database system was interrupted; last known up at %s",
6256  str_time(ControlFile->time))));
6257 
6258  /* This is just to allow attaching to startup process with a debugger */
6259 #ifdef XLOG_REPLAY_DELAY
6261  pg_usleep(60000000L);
6262 #endif
6263 
6264  /*
6265  * Verify that pg_wal and pg_wal/archive_status exist. In cases where
6266  * someone has performed a copy for PITR, these directories may have been
6267  * excluded and need to be re-created.
6268  */
6270 
6271  /*
6272  * If we previously crashed, there might be data which we had written,
6273  * intending to fsync it, but which we had not actually fsync'd yet.
6274  * Therefore, a power failure in the near future might cause earlier
6275  * unflushed writes to be lost, even though more recent data written to
6276  * disk from here on would be persisted. To avoid that, fsync the entire
6277  * data directory.
6278  */
6279  if (ControlFile->state != DB_SHUTDOWNED &&
6282 
6283  /*
6284  * Initialize on the assumption we want to recover to the latest timeline
6285  * that's active according to pg_control.
6286  */
6290  else
6292 
6293  /*
6294  * Check for recovery control file, and if so set up state for offline
6295  * recovery
6296  */
6298 
6299  /*
6300  * Save archive_cleanup_command in shared memory so that other processes
6301  * can see it.
6302  */
6305  sizeof(XLogCtl->archiveCleanupCommand));
6306 
6308  {
6310  ereport(LOG,
6311  (errmsg("entering standby mode")));
6312  else if (recoveryTarget == RECOVERY_TARGET_XID)
6313  ereport(LOG,
6314  (errmsg("starting point-in-time recovery to XID %u",
6315  recoveryTargetXid)));
6317  ereport(LOG,
6318  (errmsg("starting point-in-time recovery to %s",
6321  ereport(LOG,
6322  (errmsg("starting point-in-time recovery to \"%s\"",
6323  recoveryTargetName)));
6324  else if (recoveryTarget == RECOVERY_TARGET_LSN)
6325  ereport(LOG,
6326  (errmsg("starting point-in-time recovery to WAL location (LSN) \"%X/%X\"",
6327  (uint32) (recoveryTargetLSN >> 32),
6330  ereport(LOG,
6331  (errmsg("starting point-in-time recovery to earliest consistent point")));
6332  else
6333  ereport(LOG,
6334  (errmsg("starting archive recovery")));
6335  }
6336 
6337  /*
6338  * Take ownership of the wakeup latch if we're going to sleep during
6339  * recovery.
6340  */
6343 
6344  /* Set up XLOG reader facility */
6345  MemSet(&private, 0, sizeof(XLogPageReadPrivate));
6346  xlogreader = XLogReaderAllocate(&XLogPageRead, &private);
6347  if (!xlogreader)
6348  ereport(ERROR,
6349  (errcode(ERRCODE_OUT_OF_MEMORY),
6350  errmsg("out of memory"),
6351  errdetail("Failed while allocating a WAL reading processor.")));
6353 
6354  /*
6355  * Allocate pages dedicated to WAL consistency checks, those had better be
6356  * aligned.
6357  */
6358  replay_image_masked = (char *) palloc(BLCKSZ);
6359  master_image_masked = (char *) palloc(BLCKSZ);
6360 
6361  if (read_backup_label(&checkPointLoc, &backupEndRequired,
6362  &backupFromStandby))
6363  {
6364  List *tablespaces = NIL;
6365 
6366  /*
6367  * Archive recovery was requested, and thanks to the backup label
6368  * file, we know how far we need to replay to reach consistency. Enter
6369  * archive recovery directly.
6370  */
6371  InArchiveRecovery = true;
6373  StandbyMode = true;
6374 
6375  /*
6376  * When a backup_label file is present, we want to roll forward from
6377  * the checkpoint it identifies, rather than using pg_control.
6378  */
6379  record = ReadCheckpointRecord(xlogreader, checkPointLoc, 0, true);
6380  if (record != NULL)
6381  {
6382  memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
6383  wasShutdown = ((record->xl_info & ~XLR_INFO_MASK) == XLOG_CHECKPOINT_SHUTDOWN);
6384  ereport(DEBUG1,
6385  (errmsg("checkpoint record is at %X/%X",
6386  (uint32) (checkPointLoc >> 32), (uint32) checkPointLoc)));
6387  InRecovery = true; /* force recovery even if SHUTDOWNED */
6388 
6389  /*
6390  * Make sure that REDO location exists. This may not be the case
6391  * if there was a crash during an online backup, which left a
6392  * backup_label around that references a WAL segment that's
6393  * already been archived.
6394  */
6395  if (checkPoint.redo < checkPointLoc)
6396  {
6397  if (!ReadRecord(xlogreader, checkPoint.redo, LOG, false))
6398  ereport(FATAL,
6399  (errmsg("could not find redo location referenced by checkpoint record"),
6400  errhint("If you are not restoring from a backup, try removing the file \"%s/backup_label\".", DataDir)));
6401  }
6402  }
6403  else
6404  {
6405  ereport(FATAL,
6406  (errmsg("could not locate required checkpoint record"),
6407  errhint("If you are not restoring from a backup, try removing the file \"%s/backup_label\".", DataDir)));
6408  wasShutdown = false; /* keep compiler quiet */
6409  }
6410 
6411  /* read the tablespace_map file if present and create symlinks. */
6412  if (read_tablespace_map(&tablespaces))
6413  {
6414  ListCell *lc;
6415 
6416  foreach(lc, tablespaces)
6417  {
6418  tablespaceinfo *ti = lfirst(lc);
6419  char *linkloc;
6420 
6421  linkloc = psprintf("pg_tblspc/%s", ti->oid);
6422 
6423  /*
6424  * Remove the existing symlink if any and Create the symlink
6425  * under PGDATA.
6426  */
6427  remove_tablespace_symlink(linkloc);
6428 
6429  if (symlink(ti->path, linkloc) < 0)
6430  ereport(ERROR,
6432  errmsg("could not create symbolic link \"%s\": %m",
6433  linkloc)));
6434 
6435  pfree(ti->oid);
6436  pfree(ti->path);
6437  pfree(ti);
6438  }
6439 
6440  /* set flag to delete it later */
6441  haveTblspcMap = true;
6442  }
6443 
6444  /* set flag to delete it later */
6445  haveBackupLabel = true;
6446  }
6447  else
6448  {
6449  /*
6450  * If tablespace_map file is present without backup_label file, there
6451  * is no use of such file. There is no harm in retaining it, but it
6452  * is better to get rid of the map file so that we don't have any
6453  * redundant file in data directory and it will avoid any sort of
6454  * confusion. It seems prudent though to just rename the file out of
6455  * the way rather than delete it completely, also we ignore any error
6456  * that occurs in rename operation as even if map file is present
6457  * without backup_label file, it is harmless.
6458  */
6459  if (stat(TABLESPACE_MAP, &st) == 0)
6460  {
6463  ereport(LOG,
6464  (errmsg("ignoring file \"%s\" because no file \"%s\" exists",
6466  errdetail("File \"%s\" was renamed to \"%s\".",
6468  else
6469  ereport(LOG,
6470  (errmsg("ignoring file \"%s\" because no file \"%s\" exists",
6472  errdetail("Could not rename file \"%s\" to \"%s\": %m.",
6474  }
6475 
6476  /*
6477  * It's possible that archive recovery was requested, but we don't
6478  * know how far we need to replay the WAL before we reach consistency.
6479  * This can happen for example if a base backup is taken from a
6480  * running server using an atomic filesystem snapshot, without calling
6481  * pg_start/stop_backup. Or if you just kill a running master server
6482  * and put it into archive recovery by creating a recovery.conf file.
6483  *
6484  * Our strategy in that case is to perform crash recovery first,
6485  * replaying all the WAL present in pg_wal, and only enter archive
6486  * recovery after that.
6487  *
6488  * But usually we already know how far we need to replay the WAL (up
6489  * to minRecoveryPoint, up to backupEndPoint, or until we see an
6490  * end-of-backup record), and we can enter archive recovery directly.
6491  */
6497  {
6498  InArchiveRecovery = true;
6500  StandbyMode = true;
6501  }
6502 
6503  /*
6504  * Get the last valid checkpoint record. If the latest one according
6505  * to pg_control is broken, try the next-to-last one.
6506  */
6507  checkPointLoc = ControlFile->checkPoint;
6509  record = ReadCheckpointRecord(xlogreader, checkPointLoc, 1, true);
6510  if (record != NULL)
6511  {
6512  ereport(DEBUG1,
6513  (errmsg("checkpoint record is at %X/%X",
6514  (uint32) (checkPointLoc >> 32), (uint32) checkPointLoc)));
6515  }
6516  else if (StandbyMode)
6517  {
6518  /*
6519  * The last valid checkpoint record required for a streaming
6520  * recovery exists in neither standby nor the primary.
6521  */
6522  ereport(PANIC,
6523  (errmsg("could not locate a valid checkpoint record")));
6524  }
6525  else
6526  {
6527  checkPointLoc = ControlFile->prevCheckPoint;
6528  record = ReadCheckpointRecord(xlogreader, checkPointLoc, 2, true);
6529  if (record != NULL)
6530  {
6531  ereport(LOG,
6532  (errmsg("using previous checkpoint record at %X/%X",
6533  (uint32) (checkPointLoc >> 32), (uint32) checkPointLoc)));
6534  InRecovery = true; /* force recovery even if SHUTDOWNED */
6535  }
6536  else
6537  ereport(PANIC,
6538  (errmsg("could not locate a valid checkpoint record")));
6539  }
6540  memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
6541  wasShutdown = ((record->xl_info & ~XLR_INFO_MASK) == XLOG_CHECKPOINT_SHUTDOWN);
6542  }
6543 
6544  /*
6545  * Clear out any old relcache cache files. This is *necessary* if we do
6546  * any WAL replay, since that would probably result in the cache files
6547  * being out of sync with database reality. In theory we could leave them
6548  * in place if the database had been cleanly shut down, but it seems
6549  * safest to just remove them always and let them be rebuilt during the
6550  * first backend startup. These files needs to be removed from all
6551  * directories including pg_tblspc, however the symlinks are created only
6552  * after reading tablespace_map file in case of archive recovery from
6553  * backup, so needs to clear old relcache files here after creating
6554  * symlinks.
6555  */
6557 
6558  /*
6559  * If the location of the checkpoint record is not on the expected
6560  * timeline in the history of the requested timeline, we cannot proceed:
6561  * the backup is not part of the history of the requested timeline.
6562  */
6563  Assert(expectedTLEs); /* was initialized by reading checkpoint
6564  * record */
6565  if (tliOfPointInHistory(checkPointLoc, expectedTLEs) !=
6566  checkPoint.ThisTimeLineID)
6567  {
6568  XLogRecPtr switchpoint;
6569 
6570  /*
6571  * tliSwitchPoint will throw an error if the checkpoint's timeline is
6572  * not in expectedTLEs at all.
6573  */
6575  ereport(FATAL,
6576  (errmsg("requested timeline %u is not a child of this server's history",
6578  errdetail("Latest checkpoint is at %X/%X on timeline %u, but in the history of the requested timeline, the server forked off from that timeline at %X/%X.",
6579  (uint32) (ControlFile->checkPoint >> 32),
6582  (uint32) (switchpoint >> 32),
6583  (uint32) switchpoint)));
6584  }
6585 
6586  /*
6587  * The min recovery point should be part of the requested timeline's
6588  * history, too.
6589  */
6593  ereport(FATAL,
6594  (errmsg("requested timeline %u does not contain minimum recovery point %X/%X on timeline %u",
6596  (uint32) (ControlFile->minRecoveryPoint >> 32),
6599 
6600  LastRec = RecPtr = checkPointLoc;
6601 
6602  ereport(DEBUG1,
6603  (errmsg_internal("redo record is at %X/%X; shutdown %s",
6604  (uint32) (checkPoint.redo >> 32), (uint32) checkPoint.redo,
6605  wasShutdown ? "TRUE" : "FALSE")));
6606  ereport(DEBUG1,
6607  (errmsg_internal("next transaction ID: %u:%u; next OID: %u",
6608  checkPoint.nextXidEpoch, checkPoint.nextXid,
6609  checkPoint.nextOid)));
6610  ereport(DEBUG1,
6611  (errmsg_internal("next MultiXactId: %u; next MultiXactOffset: %u",
6612  checkPoint.nextMulti, checkPoint.nextMultiOffset)));
6613  ereport(DEBUG1,
6614  (errmsg_internal("oldest unfrozen transaction ID: %u, in database %u",
6615  checkPoint.oldestXid, checkPoint.oldestXidDB)));
6616  ereport(DEBUG1,
6617  (errmsg_internal("oldest MultiXactId: %u, in database %u",
6618  checkPoint.oldestMulti, checkPoint.oldestMultiDB)));
6619  ereport(DEBUG1,
6620  (errmsg_internal("commit timestamp Xid oldest/newest: %u/%u",
6621  checkPoint.oldestCommitTsXid,
6622  checkPoint.newestCommitTsXid)));
6623  if (!TransactionIdIsNormal(checkPoint.nextXid))
6624  ereport(PANIC,
6625  (errmsg("invalid next transaction ID")));
6626 
6627  /* initialize shared memory variables from the checkpoint record */
6628  ShmemVariableCache->nextXid = checkPoint.nextXid;
6629  ShmemVariableCache->nextOid = checkPoint.nextOid;
6631  MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
6632  AdvanceOldestClogXid(checkPoint.oldestXid);
6633  SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
6634  SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
6636  checkPoint.newestCommitTsXid);
6637  XLogCtl->ckptXidEpoch = checkPoint.nextXidEpoch;
6638  XLogCtl->ckptXid = checkPoint.nextXid;
6639 
6640  /*
6641  * Initialize replication slots, before there's a chance to remove
6642  * required resources.
6643  */
6645 
6646  /*
6647  * Startup logical state, needs to be setup now so we have proper data
6648  * during crash recovery.
6649  */
6651 
6652  /*
6653  * Startup MultiXact. We need to do this early to be able to replay
6654  * truncations.
6655  */
6656  StartupMultiXact();
6657 
6658  /*
6659  * Ditto commit timestamps. In a standby, we do it if setting is enabled
6660  * in ControlFile; in a master we base the decision on the GUC itself.
6661  */
6664  StartupCommitTs();
6665 
6666  /*
6667  * Recover knowledge about replay progress of known replication partners.
6668  */
6670 
6671  /*
6672  * Initialize unlogged LSN. On a clean shutdown, it's restored from the
6673  * control file. On recovery, all unlogged relations are blown away, so
6674  * the unlogged LSN counter can be reset too.
6675  */
6678  else
6679  XLogCtl->unloggedLSN = 1;
6680 
6681  /*
6682  * We must replay WAL entries using the same TimeLineID they were created
6683  * under, so temporarily adopt the TLI indicated by the checkpoint (see
6684  * also xlog_redo()).
6685  */
6686  ThisTimeLineID = checkPoint.ThisTimeLineID;
6687 
6688  /*
6689  * Copy any missing timeline history files between 'now' and the recovery
6690  * target timeline from archive to pg_wal. While we don't need those files
6691  * ourselves - the history file of the recovery target timeline covers all
6692  * the previous timelines in the history too - a cascading standby server
6693  * might be interested in them. Or, if you archive the WAL from this
6694  * server to a different archive than the master, it'd be good for all the
6695  * history files to get archived there after failover, so that you can use
6696  * one of the old timelines as a PITR target. Timeline history files are
6697  * small, so it's better to copy them unnecessarily than not copy them and
6698  * regret later.
6699  */
6701 
6702  /*
6703  * Before running in recovery, scan pg_twophase and fill in its status to
6704  * be able to work on entries generated by redo. Doing a scan before
6705  * taking any recovery action has the merit to discard any 2PC files that
6706  * are newer than the first record to replay, saving from any conflicts at
6707  * replay. This avoids as well any subsequent scans when doing recovery
6708  * of the on-disk two-phase data.
6709  */
6711 
6712  lastFullPageWrites = checkPoint.fullPageWrites;
6713 
6716 
6717  if (RecPtr < checkPoint.redo)
6718  ereport(PANIC,
6719  (errmsg("invalid redo in checkpoint record")));
6720 
6721  /*
6722  * Check whether we need to force recovery from WAL. If it appears to
6723  * have been a clean shutdown and we did not have a recovery.conf file,
6724  * then assume no recovery needed.
6725  */
6726  if (checkPoint.redo < RecPtr)
6727  {
6728  if (wasShutdown)
6729  ereport(PANIC,
6730  (errmsg("invalid redo record in shutdown checkpoint")));
6731  InRecovery = true;
6732  }
6733  else if (ControlFile->state != DB_SHUTDOWNED)
6734  InRecovery = true;
6735  else if (ArchiveRecoveryRequested)
6736  {
6737  /* force recovery due to presence of recovery.conf */
6738  InRecovery = true;
6739  }
6740 
6741  /* REDO */
6742  if (InRecovery)
6743  {
6744  int rmid;
6745 
6746  /*
6747  * Update pg_control to show that we are recovering and to show the
6748  * selected checkpoint as the place we are starting from. We also mark
6749  * pg_control with any minimum recovery stop point obtained from a
6750  * backup history file.
6751  */
6752  dbstate_at_startup = ControlFile->state;
6753  if (InArchiveRecovery)
6755  else
6756  {
6757  ereport(LOG,
6758  (errmsg("database system was not properly shut down; "
6759  "automatic recovery in progress")));
6761  ereport(LOG,
6762  (errmsg("crash recovery starts in timeline %u "
6763  "and has target timeline %u",
6765  recoveryTargetTLI)));
6767  }
6769  ControlFile->checkPoint = checkPointLoc;
6770  ControlFile->checkPointCopy = checkPoint;
6771  if (InArchiveRecovery)
6772  {
6773  /* initialize minRecoveryPoint if not set yet */
6774  if (ControlFile->minRecoveryPoint < checkPoint.redo)
6775  {
6776  ControlFile->minRecoveryPoint = checkPoint.redo;
6778  }
6779  }
6780 
6781  /*
6782  * Set backupStartPoint if we're starting recovery from a base backup.
6783  *
6784  * Also set backupEndPoint and use minRecoveryPoint as the backup end
6785  * location if we're starting recovery from a base backup which was
6786  * taken from a standby. In this case, the database system status in
6787  * pg_control must indicate that the database was already in recovery.
6788  * Usually that will be DB_IN_ARCHIVE_RECOVERY but also can be
6789  * DB_SHUTDOWNED_IN_RECOVERY if recovery previously was interrupted
6790  * before reaching this point; e.g. because restore_command or
6791  * primary_conninfo were faulty.
6792  *
6793  * Any other state indicates that the backup somehow became corrupted
6794  * and we can't sensibly continue with recovery.
6795  */
6796  if (haveBackupLabel)
6797  {
6798  ControlFile->backupStartPoint = checkPoint.redo;
6799  ControlFile->backupEndRequired = backupEndRequired;
6800 
6801  if (backupFromStandby)
6802  {
6803  if (dbstate_at_startup != DB_IN_ARCHIVE_RECOVERY &&
6804  dbstate_at_startup != DB_SHUTDOWNED_IN_RECOVERY)
6805  ereport(FATAL,
6806  (errmsg("backup_label contains data inconsistent with control file"),
6807  errhint("This means that the backup is corrupted and you will "
6808  "have to use another backup for recovery.")));
6810  }
6811  }
6812  ControlFile->time = (pg_time_t) time(NULL);
6813  /* No need to hold ControlFileLock yet, we aren't up far enough */
6814