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

Referenced by CancelBackup(), and StartupXLOG().

#define CHECKPOINT_CAUSE_TIME   0x0080 /* Elapsed time */

Definition at line 186 of file xlog.h.

Referenced by CheckpointerMain(), and LogCheckpointStart().

#define CHECKPOINT_CAUSE_XLOG   0x0040 /* XLOG consumption */

Definition at line 185 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 180 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 324 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 303 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 2240 of file xlog.c.

References CalculateCheckpointSegments(), CheckPointCompletionTarget, and newval.

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

Definition at line 2233 of file xlog.c.

References CalculateCheckpointSegments(), max_wal_size_mb, and newval.

2234 {
2237 }
static void CalculateCheckpointSegments(void)
Definition: xlog.c:2210
int max_wal_size_mb
Definition: xlog.c:89
#define newval
void BootStrapXLOG ( void  )

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

4946 {
4947  CheckPoint checkPoint;
4948  char *buffer;
4949  XLogPageHeader page;
4950  XLogLongPageHeader longpage;
4951  XLogRecord *record;
4952  char *recptr;
4953  bool use_existent;
4954  uint64 sysidentifier;
4955  char mock_auth_nonce[MOCK_AUTH_NONCE_LEN];
4956  struct timeval tv;
4957  pg_crc32c crc;
4958 
4959  /*
4960  * Select a hopefully-unique system identifier code for this installation.
4961  * We use the result of gettimeofday(), including the fractional seconds
4962  * field, as being about as unique as we can easily get. (Think not to
4963  * use random(), since it hasn't been seeded and there's no portable way
4964  * to seed it other than the system clock value...) The upper half of the
4965  * uint64 value is just the tv_sec part, while the lower half contains the
4966  * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
4967  * PID for a little extra uniqueness. A person knowing this encoding can
4968  * determine the initialization time of the installation, which could
4969  * perhaps be useful sometimes.
4970  */
4971  gettimeofday(&tv, NULL);
4972  sysidentifier = ((uint64) tv.tv_sec) << 32;
4973  sysidentifier |= ((uint64) tv.tv_usec) << 12;
4974  sysidentifier |= getpid() & 0xFFF;
4975 
4976  /*
4977  * Generate a random nonce. This is used for authentication requests that
4978  * will fail because the user does not exist. The nonce is used to create
4979  * a genuine-looking password challenge for the non-existent user, in lieu
4980  * of an actual stored password.
4981  */
4982  if (!pg_backend_random(mock_auth_nonce, MOCK_AUTH_NONCE_LEN))
4983  ereport(PANIC,
4984  (errcode(ERRCODE_INTERNAL_ERROR),
4985  errmsg("could not generate secret authorization token")));
4986 
4987  /* First timeline ID is always 1 */
4988  ThisTimeLineID = 1;
4989 
4990  /* page buffer must be aligned suitably for O_DIRECT */
4991  buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
4992  page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
4993  memset(page, 0, XLOG_BLCKSZ);
4994 
4995  /*
4996  * Set up information for the initial checkpoint record
4997  *
4998  * The initial checkpoint record is written to the beginning of the WAL
4999  * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not
5000  * used, so that we can use 0/0 to mean "before any valid WAL segment".
5001  */
5002  checkPoint.redo = XLogSegSize + SizeOfXLogLongPHD;
5003  checkPoint.ThisTimeLineID = ThisTimeLineID;
5004  checkPoint.PrevTimeLineID = ThisTimeLineID;
5005  checkPoint.fullPageWrites = fullPageWrites;
5006  checkPoint.nextXidEpoch = 0;
5007  checkPoint.nextXid = FirstNormalTransactionId;
5008  checkPoint.nextOid = FirstBootstrapObjectId;
5009  checkPoint.nextMulti = FirstMultiXactId;
5010  checkPoint.nextMultiOffset = 0;
5011  checkPoint.oldestXid = FirstNormalTransactionId;
5012  checkPoint.oldestXidDB = TemplateDbOid;
5013  checkPoint.oldestMulti = FirstMultiXactId;
5014  checkPoint.oldestMultiDB = TemplateDbOid;
5017  checkPoint.time = (pg_time_t) time(NULL);
5019 
5020  ShmemVariableCache->nextXid = checkPoint.nextXid;
5021  ShmemVariableCache->nextOid = checkPoint.nextOid;
5023  MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5024  AdvanceOldestClogXid(checkPoint.oldestXid);
5025  SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5026  SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5028 
5029  /* Set up the XLOG page header */
5030  page->xlp_magic = XLOG_PAGE_MAGIC;
5031  page->xlp_info = XLP_LONG_HEADER;
5032  page->xlp_tli = ThisTimeLineID;
5033  page->xlp_pageaddr = XLogSegSize;
5034  longpage = (XLogLongPageHeader) page;
5035  longpage->xlp_sysid = sysidentifier;
5036  longpage->xlp_seg_size = XLogSegSize;
5037  longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
5038 
5039  /* Insert the initial checkpoint record */
5040  recptr = ((char *) page + SizeOfXLogLongPHD);
5041  record = (XLogRecord *) recptr;
5042  record->xl_prev = 0;
5043  record->xl_xid = InvalidTransactionId;
5044  record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(checkPoint);
5046  record->xl_rmid = RM_XLOG_ID;
5047  recptr += SizeOfXLogRecord;
5048  /* fill the XLogRecordDataHeaderShort struct */
5049  *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
5050  *(recptr++) = sizeof(checkPoint);
5051  memcpy(recptr, &checkPoint, sizeof(checkPoint));
5052  recptr += sizeof(checkPoint);
5053  Assert(recptr - (char *) record == record->xl_tot_len);
5054 
5055  INIT_CRC32C(crc);
5056  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
5057  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
5058  FIN_CRC32C(crc);
5059  record->xl_crc = crc;
5060 
5061  /* Create first XLOG segment file */
5062  use_existent = false;
5063  openLogFile = XLogFileInit(1, &use_existent, false);
5064 
5065  /* Write the first page with the initial record */
5066  errno = 0;
5068  if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
5069  {
5070  /* if write didn't set errno, assume problem is no disk space */
5071  if (errno == 0)
5072  errno = ENOSPC;
5073  ereport(PANIC,
5075  errmsg("could not write bootstrap write-ahead log file: %m")));
5076  }
5078 
5080  if (pg_fsync(openLogFile) != 0)
5081  ereport(PANIC,
5083  errmsg("could not fsync bootstrap write-ahead log file: %m")));
5085 
5086  if (close(openLogFile))
5087  ereport(PANIC,
5089  errmsg("could not close bootstrap write-ahead log file: %m")));
5090 
5091  openLogFile = -1;
5092 
5093  /* Now create pg_control */
5094 
5095  memset(ControlFile, 0, sizeof(ControlFileData));
5096  /* Initialize pg_control status fields */
5097  ControlFile->system_identifier = sysidentifier;
5098  memcpy(ControlFile->mock_authentication_nonce, mock_auth_nonce, MOCK_AUTH_NONCE_LEN);
5100  ControlFile->time = checkPoint.time;
5101  ControlFile->checkPoint = checkPoint.redo;
5102  ControlFile->checkPointCopy = checkPoint;
5103  ControlFile->unloggedLSN = 1;
5104 
5105  /* Set important parameter values for use when replaying WAL */
5114 
5115  /* some additional ControlFile fields are set in WriteControlFile() */
5116 
5117  WriteControlFile();
5118 
5119  /* Bootstrap the commit log, too */
5120  BootStrapCLOG();
5124 
5125  pfree(buffer);
5126 }
static void WriteControlFile(void)
Definition: xlog.c:4376
#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:201
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:3153
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:212
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:124
Oid oldestMultiDB
Definition: pg_control.h:49
static int openLogFile
Definition: xlog.c:770
static ControlFileData * ControlFile
Definition: xlog.c:713
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:214
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:224
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:125
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 12080 of file xlog.c.

References FALLBACK_PROMOTE_SIGNAL_FILE, and PROMOTE_SIGNAL_FILE.

Referenced by sigusr1_handler().

12081 {
12082  struct stat stat_buf;
12083 
12084  if (stat(PROMOTE_SIGNAL_FILE, &stat_buf) == 0 ||
12086  return true;
12087 
12088  return false;
12089 }
#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 3765 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().

3766 {
3767  XLogSegNo lastRemovedSegNo;
3768 
3770  lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3772 
3773  if (segno <= lastRemovedSegNo)
3774  {
3775  char filename[MAXFNAMELEN];
3776 
3777  XLogFileName(filename, tli, segno);
3778  ereport(ERROR,
3780  errmsg("requested WAL segment %s has already been removed",
3781  filename)));
3782  }
3783 }
slock_t info_lck
Definition: xlog.c:702
#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:705
static char * filename
Definition: pg_dumpall.c:89
int errmsg(const char *fmt,...)
Definition: elog.c:797
void CreateCheckPoint ( int  flags)

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

8528 {
8529  bool shutdown;
8530  CheckPoint checkPoint;
8531  XLogRecPtr recptr;
8533  uint32 freespace;
8534  XLogRecPtr PriorRedoPtr;
8535  XLogRecPtr curInsert;
8536  XLogRecPtr last_important_lsn;
8537  VirtualTransactionId *vxids;
8538  int nvxids;
8539 
8540  /*
8541  * An end-of-recovery checkpoint is really a shutdown checkpoint, just
8542  * issued at a different time.
8543  */
8545  shutdown = true;
8546  else
8547  shutdown = false;
8548 
8549  /* sanity check */
8550  if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
8551  elog(ERROR, "can't create a checkpoint during recovery");
8552 
8553  /*
8554  * Initialize InitXLogInsert working areas before entering the critical
8555  * section. Normally, this is done by the first call to
8556  * RecoveryInProgress() or LocalSetXLogInsertAllowed(), but when creating
8557  * an end-of-recovery checkpoint, the LocalSetXLogInsertAllowed call is
8558  * done below in a critical section, and InitXLogInsert cannot be called
8559  * in a critical section.
8560  */
8561  InitXLogInsert();
8562 
8563  /*
8564  * Acquire CheckpointLock to ensure only one checkpoint happens at a time.
8565  * (This is just pro forma, since in the present system structure there is
8566  * only one process that is allowed to issue checkpoints at any given
8567  * time.)
8568  */
8569  LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
8570 
8571  /*
8572  * Prepare to accumulate statistics.
8573  *
8574  * Note: because it is possible for log_checkpoints to change while a
8575  * checkpoint proceeds, we always accumulate stats, even if
8576  * log_checkpoints is currently off.
8577  */
8578  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
8580 
8581  /*
8582  * Use a critical section to force system panic if we have trouble.
8583  */
8585 
8586  if (shutdown)
8587  {
8588  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8590  ControlFile->time = (pg_time_t) time(NULL);
8592  LWLockRelease(ControlFileLock);
8593  }
8594 
8595  /*
8596  * Let smgr prepare for checkpoint; this has to happen before we determine
8597  * the REDO pointer. Note that smgr must not do anything that'd have to
8598  * be undone if we decide no checkpoint is needed.
8599  */
8600  smgrpreckpt();
8601 
8602  /* Begin filling in the checkpoint WAL record */
8603  MemSet(&checkPoint, 0, sizeof(checkPoint));
8604  checkPoint.time = (pg_time_t) time(NULL);
8605 
8606  /*
8607  * For Hot Standby, derive the oldestActiveXid before we fix the redo
8608  * pointer. This allows us to begin accumulating changes to assemble our
8609  * starting snapshot of locks and transactions.
8610  */
8611  if (!shutdown && XLogStandbyInfoActive())
8613  else
8615 
8616  /*
8617  * Get location of last important record before acquiring insert locks (as
8618  * GetLastImportantRecPtr() also locks WAL locks).
8619  */
8620  last_important_lsn = GetLastImportantRecPtr();
8621 
8622  /*
8623  * We must block concurrent insertions while examining insert state to
8624  * determine the checkpoint REDO pointer.
8625  */
8627  curInsert = XLogBytePosToRecPtr(Insert->CurrBytePos);
8628 
8629  /*
8630  * If this isn't a shutdown or forced checkpoint, and if there has been no
8631  * WAL activity requiring a checkpoint, skip it. The idea here is to
8632  * avoid inserting duplicate checkpoints when the system is idle.
8633  */
8634  if ((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
8635  CHECKPOINT_FORCE)) == 0)
8636  {
8637  if (last_important_lsn == ControlFile->checkPoint)
8638  {
8640  LWLockRelease(CheckpointLock);
8641  END_CRIT_SECTION();
8642  ereport(DEBUG1,
8643  (errmsg("checkpoint skipped due to an idle system")));
8644  return;
8645  }
8646  }
8647 
8648  /*
8649  * An end-of-recovery checkpoint is created before anyone is allowed to
8650  * write WAL. To allow us to write the checkpoint record, temporarily
8651  * enable XLogInsertAllowed. (This also ensures ThisTimeLineID is
8652  * initialized, which we need here and in AdvanceXLInsertBuffer.)
8653  */
8654  if (flags & CHECKPOINT_END_OF_RECOVERY)
8656 
8657  checkPoint.ThisTimeLineID = ThisTimeLineID;
8658  if (flags & CHECKPOINT_END_OF_RECOVERY)
8659  checkPoint.PrevTimeLineID = XLogCtl->PrevTimeLineID;
8660  else
8661  checkPoint.PrevTimeLineID = ThisTimeLineID;
8662 
8663  checkPoint.fullPageWrites = Insert->fullPageWrites;
8664 
8665  /*
8666  * Compute new REDO record ptr = location of next XLOG record.
8667  *
8668  * NB: this is NOT necessarily where the checkpoint record itself will be,
8669  * since other backends may insert more XLOG records while we're off doing
8670  * the buffer flush work. Those XLOG records are logically after the
8671  * checkpoint, even though physically before it. Got that?
8672  */
8673  freespace = INSERT_FREESPACE(curInsert);
8674  if (freespace == 0)
8675  {
8676  if (curInsert % XLogSegSize == 0)
8677  curInsert += SizeOfXLogLongPHD;
8678  else
8679  curInsert += SizeOfXLogShortPHD;
8680  }
8681  checkPoint.redo = curInsert;
8682 
8683  /*
8684  * Here we update the shared RedoRecPtr for future XLogInsert calls; this
8685  * must be done while holding all the insertion locks.
8686  *
8687  * Note: if we fail to complete the checkpoint, RedoRecPtr will be left
8688  * pointing past where it really needs to point. This is okay; the only
8689  * consequence is that XLogInsert might back up whole buffers that it
8690  * didn't really need to. We can't postpone advancing RedoRecPtr because
8691  * XLogInserts that happen while we are dumping buffers must assume that
8692  * their buffer changes are not included in the checkpoint.
8693  */
8694  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
8695 
8696  /*
8697  * Now we can release the WAL insertion locks, allowing other xacts to
8698  * proceed while we are flushing disk buffers.
8699  */
8701 
8702  /* Update the info_lck-protected copy of RedoRecPtr as well */
8704  XLogCtl->RedoRecPtr = checkPoint.redo;
8706 
8707  /*
8708  * If enabled, log checkpoint start. We postpone this until now so as not
8709  * to log anything if we decided to skip the checkpoint.
8710  */
8711  if (log_checkpoints)
8712  LogCheckpointStart(flags, false);
8713 
8714  TRACE_POSTGRESQL_CHECKPOINT_START(flags);
8715 
8716  /*
8717  * Get the other info we need for the checkpoint record.
8718  *
8719  * We don't need to save oldestClogXid in the checkpoint, it only matters
8720  * for the short period in which clog is being truncated, and if we crash
8721  * during that we'll redo the clog truncation and fix up oldestClogXid
8722  * there.
8723  */
8724  LWLockAcquire(XidGenLock, LW_SHARED);
8725  checkPoint.nextXid = ShmemVariableCache->nextXid;
8726  checkPoint.oldestXid = ShmemVariableCache->oldestXid;
8728  LWLockRelease(XidGenLock);
8729 
8730  LWLockAcquire(CommitTsLock, LW_SHARED);
8733  LWLockRelease(CommitTsLock);
8734 
8735  /* Increase XID epoch if we've wrapped around since last checkpoint */
8737  if (checkPoint.nextXid < ControlFile->checkPointCopy.nextXid)
8738  checkPoint.nextXidEpoch++;
8739 
8740  LWLockAcquire(OidGenLock, LW_SHARED);
8741  checkPoint.nextOid = ShmemVariableCache->nextOid;
8742  if (!shutdown)
8743  checkPoint.nextOid += ShmemVariableCache->oidCount;
8744  LWLockRelease(OidGenLock);
8745 
8746  MultiXactGetCheckptMulti(shutdown,
8747  &checkPoint.nextMulti,
8748  &checkPoint.nextMultiOffset,
8749  &checkPoint.oldestMulti,
8750  &checkPoint.oldestMultiDB);
8751 
8752  /*
8753  * Having constructed the checkpoint record, ensure all shmem disk buffers
8754  * and commit-log buffers are flushed to disk.
8755  *
8756  * This I/O could fail for various reasons. If so, we will fail to
8757  * complete the checkpoint, but there is no reason to force a system
8758  * panic. Accordingly, exit critical section while doing it.
8759  */
8760  END_CRIT_SECTION();
8761 
8762  /*
8763  * In some cases there are groups of actions that must all occur on one
8764  * side or the other of a checkpoint record. Before flushing the
8765  * checkpoint record we must explicitly wait for any backend currently
8766  * performing those groups of actions.
8767  *
8768  * One example is end of transaction, so we must wait for any transactions
8769  * that are currently in commit critical sections. If an xact inserted
8770  * its commit record into XLOG just before the REDO point, then a crash
8771  * restart from the REDO point would not replay that record, which means
8772  * that our flushing had better include the xact's update of pg_xact. So
8773  * we wait till he's out of his commit critical section before proceeding.
8774  * See notes in RecordTransactionCommit().
8775  *
8776  * Because we've already released the insertion locks, this test is a bit
8777  * fuzzy: it is possible that we will wait for xacts we didn't really need
8778  * to wait for. But the delay should be short and it seems better to make
8779  * checkpoint take a bit longer than to hold off insertions longer than
8780  * necessary. (In fact, the whole reason we have this issue is that xact.c
8781  * does commit record XLOG insertion and clog update as two separate steps
8782  * protected by different locks, but again that seems best on grounds of
8783  * minimizing lock contention.)
8784  *
8785  * A transaction that has not yet set delayChkpt when we look cannot be at
8786  * risk, since he's not inserted his commit record yet; and one that's
8787  * already cleared it is not at risk either, since he's done fixing clog
8788  * and we will correctly flush the update below. So we cannot miss any
8789  * xacts we need to wait for.
8790  */
8791  vxids = GetVirtualXIDsDelayingChkpt(&nvxids);
8792  if (nvxids > 0)
8793  {
8794  do
8795  {
8796  pg_usleep(10000L); /* wait for 10 msec */
8797  } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids));
8798  }
8799  pfree(vxids);
8800 
8801  CheckPointGuts(checkPoint.redo, flags);
8802 
8803  /*
8804  * Take a snapshot of running transactions and write this to WAL. This
8805  * allows us to reconstruct the state of running transactions during
8806  * archive recovery, if required. Skip, if this info disabled.
8807  *
8808  * If we are shutting down, or Startup process is completing crash
8809  * recovery we don't need to write running xact data.
8810  */
8811  if (!shutdown && XLogStandbyInfoActive())
8813 
8815 
8816  /*
8817  * Now insert the checkpoint record into XLOG.
8818  */
8819  XLogBeginInsert();
8820  XLogRegisterData((char *) (&checkPoint), sizeof(checkPoint));
8821  recptr = XLogInsert(RM_XLOG_ID,
8822  shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
8824 
8825  XLogFlush(recptr);
8826 
8827  /*
8828  * We mustn't write any new WAL after a shutdown checkpoint, or it will be
8829  * overwritten at next startup. No-one should even try, this just allows
8830  * sanity-checking. In the case of an end-of-recovery checkpoint, we want
8831  * to just temporarily disable writing until the system has exited
8832  * recovery.
8833  */
8834  if (shutdown)
8835  {
8836  if (flags & CHECKPOINT_END_OF_RECOVERY)
8837  LocalXLogInsertAllowed = -1; /* return to "check" state */
8838  else
8839  LocalXLogInsertAllowed = 0; /* never again write WAL */
8840  }
8841 
8842  /*
8843  * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
8844  * = end of actual checkpoint record.
8845  */
8846  if (shutdown && checkPoint.redo != ProcLastRecPtr)
8847  ereport(PANIC,
8848  (errmsg("concurrent write-ahead log activity while database system is shutting down")));
8849 
8850  /*
8851  * Remember the prior checkpoint's redo pointer, used later to determine
8852  * the point where the log can be truncated.
8853  */
8854  PriorRedoPtr = ControlFile->checkPointCopy.redo;
8855 
8856  /*
8857  * Update the control file.
8858  */
8859  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8860  if (shutdown)
8864  ControlFile->checkPointCopy = checkPoint;
8865  ControlFile->time = (pg_time_t) time(NULL);
8866  /* crash recovery should always recover to the end of WAL */
8869 
8870  /*
8871  * Persist unloggedLSN value. It's reset on crash recovery, so this goes
8872  * unused on non-shutdown checkpoints, but seems useful to store it always
8873  * for debugging purposes.
8874  */
8878 
8880  LWLockRelease(ControlFileLock);
8881 
8882  /* Update shared-memory copy of checkpoint XID/epoch */
8884  XLogCtl->ckptXidEpoch = checkPoint.nextXidEpoch;
8885  XLogCtl->ckptXid = checkPoint.nextXid;
8887 
8888  /*
8889  * We are now done with critical updates; no need for system panic if we
8890  * have trouble while fooling with old log segments.
8891  */
8892  END_CRIT_SECTION();
8893 
8894  /*
8895  * Let smgr do post-checkpoint cleanup (eg, deleting old files).
8896  */
8897  smgrpostckpt();
8898 
8899  /*
8900  * Delete old log files (those no longer needed even for previous
8901  * checkpoint or the standbys in XLOG streaming).
8902  */
8903  if (PriorRedoPtr != InvalidXLogRecPtr)
8904  {
8905  XLogSegNo _logSegNo;
8906 
8907  /* Update the average distance between checkpoints. */
8909 
8910  XLByteToSeg(PriorRedoPtr, _logSegNo);
8911  KeepLogSeg(recptr, &_logSegNo);
8912  _logSegNo--;
8913  RemoveOldXlogFiles(_logSegNo, PriorRedoPtr, recptr);
8914  }
8915 
8916  /*
8917  * Make more log segments if needed. (Do this after recycling old log
8918  * segments, since that may supply some of the needed files.)
8919  */
8920  if (!shutdown)
8921  PreallocXlogFiles(recptr);
8922 
8923  /*
8924  * Truncate pg_subtrans if possible. We can throw away all data before
8925  * the oldest XMIN of any running transaction. No future transaction will
8926  * attempt to reference any pg_subtrans entry older than that (see Asserts
8927  * in subtrans.c). During recovery, though, we mustn't do this because
8928  * StartupSUBTRANS hasn't been called yet.
8929  */
8930  if (!RecoveryInProgress())
8932 
8933  /* Real work is done, but log and update stats before releasing lock. */
8934  LogCheckpointEnd(false);
8935 
8936  TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats.ckpt_bufs_written,
8937  NBuffers,
8941 
8942  LWLockRelease(CheckpointLock);
8943 }
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:8239
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:8465
#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:1645
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:1914
XLogRecPtr unloggedLSN
Definition: xlog.c:588
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:198
slock_t info_lck
Definition: xlog.c:702
#define END_CRIT_SECTION()
Definition: miscadmin.h:135
VirtualTransactionId * GetVirtualXIDsDelayingChkpt(int *nvxids)
Definition: procarray.c:2253
MultiXactId oldestMulti
Definition: pg_control.h:48
TimeLineID PrevTimeLineID
Definition: xlog.c:629
TimeLineID PrevTimeLineID
Definition: pg_control.h:38
#define START_CRIT_SECTION()
Definition: miscadmin.h:133
int ckpt_segs_recycled
Definition: xlog.h:208
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:3830
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:9004
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:7872
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:354
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:2757
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:4640
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:8380
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:3739
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:207
#define CHECKPOINT_FORCE
Definition: xlog.h:179
#define INSERT_FREESPACE(endptr)
Definition: xlog.c:719
#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:204
static void LocalSetXLogInsertAllowed(void)
Definition: xlog.c:7998
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:50
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:9328
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:713
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:1616
static XLogCtlData * XLogCtl
Definition: xlog.c:705
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int ckpt_segs_added
Definition: xlog.h:206
slock_t ulsn_lck
Definition: xlog.c:589
TimeLineID ThisTimeLineID
Definition: pg_control.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:797
TransactionId GetOldestActiveTransactionId(void)
Definition: procarray.c:2091
int NBuffers
Definition: globals.c:123
#define elog
Definition: elog.h:219
bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids)
Definition: procarray.c:2298
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:8362
#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 9074 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().

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

Definition at line 4709 of file xlog.c.

References Assert, ControlFileData::data_checksum_version, and NULL.

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

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

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

11069 {
11073 
11076  {
11077  XLogCtl->Insert.forcePageWrites = false;
11078  }
11080 }
static void WALInsertLockRelease(void)
Definition: xlog.c:1645
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:1616
static XLogCtlData * XLogCtl
Definition: xlog.c:705
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 10177 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().

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

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

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

References sessionBackupState.

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

10667 {
10668  return sessionBackupState;
10669 }
static SessionBackupState sessionBackupState
Definition: xlog.c:510
TimestampTz GetCurrentChunkReplayStartTime ( void  )

Definition at line 6090 of file xlog.c.

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

Referenced by GetReplicationApplyDelay().

6091 {
6092  TimestampTz xtime;
6093 
6095  xtime = XLogCtl->currentChunkStartTime;
6097 
6098  return xtime;
6099 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:702
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:705
TimestampTz currentChunkStartTime
Definition: xlog.c:692
XLogRecPtr GetFakeLSNForUnloggedRel ( void  )

Definition at line 4725 of file xlog.c.

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

Referenced by gistGetFakeLSN().

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

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

8222 {
8226 
8227  return LogwrtResult.Flush;
8228 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:747
slock_t info_lck
Definition: xlog.c:702
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:599
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:705
XLogRecPtr Flush
Definition: xlog.c:424
void GetFullPageWriteInfo ( XLogRecPtr RedoRecPtr_p,
bool doPageWrites_p 
)

Definition at line 8190 of file xlog.c.

References doPageWrites, and RedoRecPtr.

Referenced by XLogCheckBufferNeedsBackup(), and XLogInsert().

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

Definition at line 8205 of file xlog.c.

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

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

8206 {
8207  XLogRecPtr recptr;
8208 
8210  recptr = XLogCtl->LogwrtRqst.Write;
8212 
8213  return recptr;
8214 }
slock_t info_lck
Definition: xlog.c:702
#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:705
XLogRecPtr GetLastImportantRecPtr ( void  )

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

8240 {
8242  int i;
8243 
8244  for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
8245  {
8246  XLogRecPtr last_important;
8247 
8248  /*
8249  * Need to take a lock to prevent torn reads of the LSN, which are
8250  * possible on some of the supported platforms. WAL insert locks only
8251  * support exclusive mode, so we have to use that.
8252  */
8254  last_important = WALInsertLocks[i].l.lastImportantAt;
8255  LWLockRelease(&WALInsertLocks[i].l.lock);
8256 
8257  if (res < last_important)
8258  res = last_important;
8259  }
8260 
8261  return res;
8262 }
#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:708
TimestampTz GetLatestXTime ( void  )

Definition at line 6060 of file xlog.c.

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

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

6061 {
6062  TimestampTz xtime;
6063 
6065  xtime = XLogCtl->recoveryLastXTime;
6067 
6068  return xtime;
6069 }
int64 TimestampTz
Definition: timestamp.h:39
slock_t info_lck
Definition: xlog.c:702
#define SpinLockAcquire(lock)
Definition: spin.h:62
TimestampTz recoveryLastXTime
Definition: xlog.c:686
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:705
char* GetMockAuthenticationNonce ( void  )

Definition at line 4699 of file xlog.c.

References Assert, ControlFileData::mock_authentication_nonce, and NULL.

Referenced by scram_mock_salt().

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

Definition at line 8290 of file xlog.c.

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

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

8291 {
8292  uint32 ckptXidEpoch;
8293  TransactionId ckptXid;
8294  TransactionId nextXid;
8295 
8296  /* Must read checkpoint info first, else have race condition */
8298  ckptXidEpoch = XLogCtl->ckptXidEpoch;
8299  ckptXid = XLogCtl->ckptXid;
8301 
8302  /* Now fetch current nextXid */
8303  nextXid = ReadNewTransactionId();
8304 
8305  /*
8306  * nextXid is certainly logically later than ckptXid. So if it's
8307  * numerically less, it must have wrapped into the next epoch.
8308  */
8309  if (nextXid < ckptXid)
8310  ckptXidEpoch++;
8311 
8312  *xid = nextXid;
8313  *epoch = ckptXidEpoch;
8314 }
TransactionId ckptXid
Definition: xlog.c:581
uint32 TransactionId
Definition: c.h:397
slock_t info_lck
Definition: xlog.c:702
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:705
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
XLogRecPtr GetRedoRecPtr ( void  )

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

8163 {
8164  XLogRecPtr ptr;
8165 
8166  /*
8167  * The possibly not up-to-date copy in XlogCtl is enough. Even if we
8168  * grabbed a WAL insertion lock to read the master copy, someone might
8169  * update it just after we've released the lock.
8170  */
8172  ptr = XLogCtl->RedoRecPtr;
8174 
8175  if (RedoRecPtr < ptr)
8176  RedoRecPtr = ptr;
8177 
8178  return RedoRecPtr;
8179 }
slock_t info_lck
Definition: xlog.c:702
#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:705
uint64 GetSystemIdentifier ( void  )

Definition at line 4689 of file xlog.c.

References Assert, NULL, and ControlFileData::system_identifier.

Referenced by IdentifySystem(), and WalReceiverMain().

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

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

11108 {
11110  uint64 current_bytepos;
11111 
11112  SpinLockAcquire(&Insert->insertpos_lck);
11113  current_bytepos = Insert->CurrBytePos;
11114  SpinLockRelease(&Insert->insertpos_lck);
11115 
11116  return XLogBytePosToRecPtr(current_bytepos);
11117 }
slock_t insertpos_lck
Definition: xlog.c:517
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1914
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:705
void GetXLogReceiptTime ( TimestampTz rtime,
bool fromStream 
)

Definition at line 6106 of file xlog.c.

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

Referenced by GetStandbyLimitTime().

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

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

11089 {
11090  XLogRecPtr recptr;
11091  TimeLineID tli;
11092 
11094  recptr = XLogCtl->lastReplayedEndRecPtr;
11095  tli = XLogCtl->lastReplayedTLI;
11097 
11098  if (replayTLI)
11099  *replayTLI = tli;
11100  return recptr;
11101 }
uint32 TimeLineID
Definition: xlogdefs.h:45
slock_t info_lck
Definition: xlog.c:702
#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:705
TimeLineID lastReplayedTLI
Definition: xlog.c:682
XLogRecPtr lastReplayedEndRecPtr
Definition: xlog.c:681
XLogRecPtr GetXLogWriteRecPtr ( void  )

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

11124 {
11128 
11129  return LogwrtResult.Write;
11130 }
static XLogwrtResult LogwrtResult
Definition: xlog.c:747
slock_t info_lck
Definition: xlog.c:702
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogwrtResult LogwrtResult
Definition: xlog.c:599
#define SpinLockRelease(lock)
Definition: spin.h:64
static XLogCtlData * XLogCtl
Definition: xlog.c:705
XLogRecPtr Write
Definition: xlog.c:423
bool HotStandbyActive ( void  )

Definition at line 7928 of file xlog.c.

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

Referenced by XLogWalRcvSendHSFeedback().

7929 {
7930  /*
7931  * We check shared state each time only until Hot Standby is active. We
7932  * can't de-activate Hot Standby, so there's no need to keep checking
7933  * after the shared variable has once been seen true.
7934  */
7936  return true;
7937  else
7938  {
7939  /* spinlock is essential on machines with weak memory ordering! */
7943 
7944  return LocalHotStandbyActive;
7945  }
7946 }
bool SharedHotStandbyActive
Definition: xlog.c:647
slock_t info_lck
Definition: xlog.c:702
#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:705
bool HotStandbyActiveInReplay ( void  )

Definition at line 7953 of file xlog.c.

References AmStartupProcess, Assert, IsPostmasterEnvironment, and LocalHotStandbyActive.

Referenced by btree_xlog_vacuum().

7954 {
7956  return LocalHotStandbyActive;
7957 }
#define AmStartupProcess()
Definition: miscadmin.h:408
bool IsPostmasterEnvironment
Definition: globals.c:100
static bool LocalHotStandbyActive
Definition: xlog.c:221
#define Assert(condition)
Definition: c.h:675
void InitXLOGAccess ( void  )

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

8140 {
8142 
8143  /* ThisTimeLineID doesn't change so we need no lock to copy it */
8146 
8147  /* Use GetRedoRecPtr to copy the RedoRecPtr safely */
8148  (void) GetRedoRecPtr();
8149  /* Also update our copy of doPageWrites. */
8150  doPageWrites = (Insert->fullPageWrites || Insert->forcePageWrites);
8151 
8152  /* Also initialize the working areas for constructing WAL records */
8153  InitXLogInsert();
8154 }
void InitXLogInsert(void)
Definition: xloginsert.c:1028
TimeLineID ThisTimeLineID
Definition: xlog.c:628
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:705
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8162
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:370
void issue_xlog_fsync ( int  fd,
XLogSegNo  segno 
)

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

10088 {
10089  switch (sync_method)
10090  {
10091  case SYNC_METHOD_FSYNC:
10092  if (pg_fsync_no_writethrough(fd) != 0)
10093  ereport(PANIC,
10095  errmsg("could not fsync log file %s: %m",
10096  XLogFileNameP(ThisTimeLineID, segno))));
10097  break;
10098 #ifdef HAVE_FSYNC_WRITETHROUGH
10100  if (pg_fsync_writethrough(fd) != 0)
10101  ereport(PANIC,
10103  errmsg("could not fsync write-through log file %s: %m",
10104  XLogFileNameP(ThisTimeLineID, segno))));
10105  break;
10106 #endif
10107 #ifdef HAVE_FDATASYNC
10108  case SYNC_METHOD_FDATASYNC:
10109  if (pg_fdatasync(fd) != 0)
10110  ereport(PANIC,
10112  errmsg("could not fdatasync log file %s: %m",
10113  XLogFileNameP(ThisTimeLineID, segno))));
10114  break;
10115 #endif
10116  case SYNC_METHOD_OPEN:
10118  /* write synced it already */
10119  break;
10120  default:
10121  elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
10122  break;
10123  }
10124 }
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:10130
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 7872 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().

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

Definition at line 5929 of file xlog.c.

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

Referenced by pg_is_wal_replay_paused(), and recoveryPausesHere().

5930 {
5931  bool recoveryPause;
5932 
5934  recoveryPause = XLogCtl->recoveryPause;
5936 
5937  return recoveryPause;
5938 }
slock_t info_lck
Definition: xlog.c:702
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:694
static XLogCtlData * XLogCtl
Definition: xlog.c:705
void RemovePromoteSignalFiles ( void  )

Definition at line 12069 of file xlog.c.

References FALLBACK_PROMOTE_SIGNAL_FILE, PROMOTE_SIGNAL_FILE, and unlink().

Referenced by PostmasterMain().

12070 {
12073 }
#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 5941 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().

5942 {
5944  XLogCtl->recoveryPause = recoveryPause;
5946 }
slock_t info_lck
Definition: xlog.c:702
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool recoveryPause
Definition: xlog.c:694
static XLogCtlData * XLogCtl
Definition: xlog.c:705
void SetWalWriterSleeping ( bool  sleeping)

Definition at line 12105 of file xlog.c.

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

Referenced by WalWriterMain().

12106 {
12108  XLogCtl->WalWriterSleeping = sleeping;
12110 }
slock_t info_lck
Definition: xlog.c:702
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
bool WalWriterSleeping
Definition: xlog.c:654
static XLogCtlData * XLogCtl
Definition: xlog.c:705
void ShutdownXLOG ( int  code,
Datum  arg 
)

Definition at line 8320 of file xlog.c.

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

Referenced by CheckpointerMain(), and InitPostgres().

8321 {
8322  /* Don't be chatty in standalone mode */
8324  (errmsg("shutting down")));
8325 
8326  /*
8327  * Signal walsenders to move to stopping state.
8328  */
8330 
8331  /*
8332  * Wait for WAL senders to be in stopping state. This prevents commands
8333  * from writing new WAL.
8334  */
8336 
8337  if (RecoveryInProgress())
8339  else
8340  {
8341  /*
8342  * If archiving is enabled, rotate the last XLOG file so that all the
8343  * remaining records are archived (postmaster wakes up the archiver
8344  * process one more time at the end of shutdown). The checkpoint
8345  * record will go to the next XLOG file and won't be archived (yet).
8346  */
8348  RequestXLogSwitch(false);
8349 
8351  }
8352  ShutdownCLOG();
8353  ShutdownCommitTs();
8354  ShutdownSUBTRANS();
8356 }
bool IsPostmasterEnvironment
Definition: globals.c:100
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9405
void ShutdownSUBTRANS(void)
Definition: subtrans.c:283
void CreateCheckPoint(int flags)
Definition: xlog.c:8527
void ShutdownCLOG(void)
Definition: clog.c:575
bool CreateRestartPoint(int flags)
Definition: xlog.c:9074
#define XLogArchiveCommandSet()
Definition: xlog.h:139
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7872
void WalSndWaitStopping(void)
Definition: walsender.c:3051
void ShutdownMultiXact(void)
Definition: multixact.c:2105
#define ereport(elevel, rest)
Definition: elog.h:122
#define NOTICE
Definition: elog.h:37
void WalSndInitStopping(void)
Definition: walsender.c:3025
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:178
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:175
void StartupXLOG ( void  )

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

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