PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
xlog.h File Reference
#include "access/xlogbackup.h"
#include "access/xlogdefs.h"
#include "datatype/timestamp.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.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 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 XLogIsNeeded()   (wal_level >= WAL_LEVEL_REPLICA)
 
#define XLogHintBitIsNeeded()   (DataChecksumsEnabled() || wal_log_hints)
 
#define XLogStandbyInfoActive()   (wal_level >= WAL_LEVEL_REPLICA)
 
#define XLogLogicalInfoActive()   (wal_level >= WAL_LEVEL_LOGICAL)
 
#define CHECKPOINT_IS_SHUTDOWN   0x0001 /* Checkpoint is for shutdown */
 
#define CHECKPOINT_END_OF_RECOVERY
 
#define CHECKPOINT_IMMEDIATE   0x0004 /* Do it without delays */
 
#define CHECKPOINT_FORCE   0x0008 /* Force even if no activity */
 
#define CHECKPOINT_FLUSH_ALL
 
#define CHECKPOINT_WAIT   0x0020 /* Wait for completion */
 
#define CHECKPOINT_REQUESTED   0x0040 /* Checkpoint request has been made */
 
#define CHECKPOINT_CAUSE_XLOG   0x0080 /* XLOG consumption */
 
#define CHECKPOINT_CAUSE_TIME   0x0100 /* Elapsed time */
 
#define XLOG_INCLUDE_ORIGIN   0x01 /* include the replication origin */
 
#define XLOG_MARK_UNIMPORTANT   0x02 /* record not important for durability */
 
#define RECOVERY_SIGNAL_FILE   "recovery.signal"
 
#define STANDBY_SIGNAL_FILE   "standby.signal"
 
#define BACKUP_LABEL_FILE   "backup_label"
 
#define BACKUP_LABEL_OLD   "backup_label.old"
 
#define TABLESPACE_MAP   "tablespace_map"
 
#define TABLESPACE_MAP_OLD   "tablespace_map.old"
 
#define PROMOTE_SIGNAL_FILE   "promote"
 

Typedefs

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

Enumerations

enum  WalSyncMethod {
  WAL_SYNC_METHOD_FSYNC = 0 , WAL_SYNC_METHOD_FDATASYNC , WAL_SYNC_METHOD_OPEN , WAL_SYNC_METHOD_FSYNC_WRITETHROUGH ,
  WAL_SYNC_METHOD_OPEN_DSYNC
}
 
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  WalCompression { WAL_COMPRESSION_NONE = 0 , WAL_COMPRESSION_PGLZ , WAL_COMPRESSION_LZ4 , WAL_COMPRESSION_ZSTD }
 
enum  RecoveryState { RECOVERY_STATE_CRASH = 0 , RECOVERY_STATE_ARCHIVE , RECOVERY_STATE_DONE }
 
enum  WALAvailability {
  WALAVAIL_INVALID_LSN , WALAVAIL_RESERVED , WALAVAIL_EXTENDED , WALAVAIL_UNRESERVED ,
  WALAVAIL_REMOVED
}
 
enum  SessionBackupState { SESSION_BACKUP_NONE , SESSION_BACKUP_RUNNING }
 

Functions

XLogRecPtr XLogInsertRecord (struct XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi, bool topxid_included)
 
void XLogFlush (XLogRecPtr record)
 
bool XLogBackgroundFlush (void)
 
bool XLogNeedsFlush (XLogRecPtr record)
 
int XLogFileInit (XLogSegNo logsegno, TimeLineID logtli)
 
int XLogFileOpen (XLogSegNo segno, TimeLineID tli)
 
void CheckXLogRemoved (XLogSegNo segno, TimeLineID tli)
 
XLogSegNo XLogGetLastRemovedSegno (void)
 
XLogSegNo XLogGetOldestSegno (TimeLineID tli)
 
void XLogSetAsyncXactLSN (XLogRecPtr asyncXactLSN)
 
void XLogSetReplicationSlotMinimumLSN (XLogRecPtr lsn)
 
void xlog_redo (struct XLogReaderState *record)
 
void xlog_desc (StringInfo buf, struct XLogReaderState *record)
 
const char * xlog_identify (uint8 info)
 
void issue_xlog_fsync (int fd, XLogSegNo segno, TimeLineID tli)
 
bool RecoveryInProgress (void)
 
RecoveryState GetRecoveryState (void)
 
bool XLogInsertAllowed (void)
 
XLogRecPtr GetXLogInsertRecPtr (void)
 
XLogRecPtr GetXLogWriteRecPtr (void)
 
uint64 GetSystemIdentifier (void)
 
char * GetMockAuthenticationNonce (void)
 
bool DataChecksumsEnabled (void)
 
bool GetDefaultCharSignedness (void)
 
XLogRecPtr GetFakeLSNForUnloggedRel (void)
 
Size XLOGShmemSize (void)
 
void XLOGShmemInit (void)
 
void BootStrapXLOG (uint32 data_checksum_version)
 
void InitializeWalConsistencyChecking (void)
 
void LocalProcessControlFile (bool reset)
 
WalLevel GetActiveWalLevelOnStandby (void)
 
void StartupXLOG (void)
 
void ShutdownXLOG (int code, Datum arg)
 
bool CreateCheckPoint (int flags)
 
bool CreateRestartPoint (int flags)
 
WALAvailability GetWALAvailability (XLogRecPtr targetLSN)
 
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 (TimeLineID *insertTLI)
 
TimeLineID GetWALInsertionTimeLine (void)
 
TimeLineID GetWALInsertionTimeLineIfSet (void)
 
XLogRecPtr GetLastImportantRecPtr (void)
 
void SetWalWriterSleeping (bool sleeping)
 
Size WALReadFromBuffers (char *dstbuf, XLogRecPtr startptr, Size count, TimeLineID tli)
 
void RemoveNonParentXlogFiles (XLogRecPtr switchpoint, TimeLineID newTLI)
 
bool XLogCheckpointNeeded (XLogSegNo new_segno)
 
void SwitchIntoArchiveRecovery (XLogRecPtr EndRecPtr, TimeLineID replayTLI)
 
void ReachedEndOfBackup (XLogRecPtr EndRecPtr, TimeLineID tli)
 
void SetInstallXLogFileSegmentActive (void)
 
bool IsInstallXLogFileSegmentActive (void)
 
void XLogShutdownWalRcv (void)
 
void do_pg_backup_start (const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
 
void do_pg_backup_stop (BackupState *state, bool waitforarchive)
 
void do_pg_abort_backup (int code, Datum arg)
 
void register_persistent_abort_backup_handler (void)
 
SessionBackupState get_backup_status (void)
 

Variables

PGDLLIMPORT int wal_sync_method
 
PGDLLIMPORT XLogRecPtr ProcLastRecPtr
 
PGDLLIMPORT XLogRecPtr XactLastRecEnd
 
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
 
PGDLLIMPORT int wal_segment_size
 
PGDLLIMPORT int min_wal_size_mb
 
PGDLLIMPORT int max_wal_size_mb
 
PGDLLIMPORT int wal_keep_size_mb
 
PGDLLIMPORT int max_slot_wal_keep_size_mb
 
PGDLLIMPORT int XLOGbuffers
 
PGDLLIMPORT int XLogArchiveTimeout
 
PGDLLIMPORT int wal_retrieve_retry_interval
 
PGDLLIMPORT char * XLogArchiveCommand
 
PGDLLIMPORT bool EnableHotStandby
 
PGDLLIMPORT bool fullPageWrites
 
PGDLLIMPORT bool wal_log_hints
 
PGDLLIMPORT int wal_compression
 
PGDLLIMPORT bool wal_init_zero
 
PGDLLIMPORT bool wal_recycle
 
PGDLLIMPORT bool * wal_consistency_checking
 
PGDLLIMPORT char * wal_consistency_checking_string
 
PGDLLIMPORT bool log_checkpoints
 
PGDLLIMPORT int CommitDelay
 
PGDLLIMPORT int CommitSiblings
 
PGDLLIMPORT bool track_wal_io_timing
 
PGDLLIMPORT int wal_decode_buffer_size
 
PGDLLIMPORT int CheckPointSegments
 
PGDLLIMPORT int XLogArchiveMode
 
PGDLLIMPORT int wal_level
 
PGDLLIMPORT CheckpointStatsData CheckpointStats
 

Macro Definition Documentation

◆ BACKUP_LABEL_FILE

#define BACKUP_LABEL_FILE   "backup_label"

Definition at line 303 of file xlog.h.

◆ BACKUP_LABEL_OLD

#define BACKUP_LABEL_OLD   "backup_label.old"

Definition at line 304 of file xlog.h.

◆ CHECKPOINT_CAUSE_TIME

#define CHECKPOINT_CAUSE_TIME   0x0100 /* Elapsed time */

Definition at line 149 of file xlog.h.

◆ CHECKPOINT_CAUSE_XLOG

#define CHECKPOINT_CAUSE_XLOG   0x0080 /* XLOG consumption */

Definition at line 148 of file xlog.h.

◆ CHECKPOINT_END_OF_RECOVERY

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

Definition at line 140 of file xlog.h.

◆ CHECKPOINT_FLUSH_ALL

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

Definition at line 143 of file xlog.h.

◆ CHECKPOINT_FORCE

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

Definition at line 142 of file xlog.h.

◆ CHECKPOINT_IMMEDIATE

#define CHECKPOINT_IMMEDIATE   0x0004 /* Do it without delays */

Definition at line 141 of file xlog.h.

◆ CHECKPOINT_IS_SHUTDOWN

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

Definition at line 139 of file xlog.h.

◆ CHECKPOINT_REQUESTED

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

Definition at line 146 of file xlog.h.

◆ CHECKPOINT_WAIT

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

Definition at line 145 of file xlog.h.

◆ PROMOTE_SIGNAL_FILE

#define PROMOTE_SIGNAL_FILE   "promote"

Definition at line 310 of file xlog.h.

◆ RECOVERY_SIGNAL_FILE

#define RECOVERY_SIGNAL_FILE   "recovery.signal"

Definition at line 301 of file xlog.h.

◆ STANDBY_SIGNAL_FILE

#define STANDBY_SIGNAL_FILE   "standby.signal"

Definition at line 302 of file xlog.h.

◆ TABLESPACE_MAP

#define TABLESPACE_MAP   "tablespace_map"

Definition at line 306 of file xlog.h.

◆ TABLESPACE_MAP_OLD

#define TABLESPACE_MAP_OLD   "tablespace_map.old"

Definition at line 307 of file xlog.h.

◆ XLOG_INCLUDE_ORIGIN

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

Definition at line 154 of file xlog.h.

◆ XLOG_MARK_UNIMPORTANT

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

Definition at line 155 of file xlog.h.

◆ XLogArchivingActive

Definition at line 99 of file xlog.h.

◆ XLogArchivingAlways

Definition at line 102 of file xlog.h.

◆ XLogHintBitIsNeeded

#define XLogHintBitIsNeeded ( )    (DataChecksumsEnabled() || wal_log_hints)

Definition at line 120 of file xlog.h.

◆ XLogIsNeeded

#define XLogIsNeeded ( )    (wal_level >= WAL_LEVEL_REPLICA)

Definition at line 109 of file xlog.h.

◆ XLogLogicalInfoActive

#define XLogLogicalInfoActive ( )    (wal_level >= WAL_LEVEL_LOGICAL)

Definition at line 126 of file xlog.h.

◆ XLogStandbyInfoActive

#define XLogStandbyInfoActive ( )    (wal_level >= WAL_LEVEL_REPLICA)

Definition at line 123 of file xlog.h.

Typedef Documentation

◆ ArchiveMode

typedef enum ArchiveMode ArchiveMode

◆ CheckpointStatsData

◆ RecoveryState

◆ SessionBackupState

◆ WALAvailability

◆ WalCompression

◆ WalLevel

typedef enum WalLevel WalLevel

Enumeration Type Documentation

◆ ArchiveMode

Enumerator
ARCHIVE_MODE_OFF 
ARCHIVE_MODE_ON 
ARCHIVE_MODE_ALWAYS 

Definition at line 63 of file xlog.h.

64{
65 ARCHIVE_MODE_OFF = 0, /* disabled */
66 ARCHIVE_MODE_ON, /* enabled while server is running normally */
67 ARCHIVE_MODE_ALWAYS, /* enabled always (even during recovery) */
ArchiveMode
Definition: xlog.h:64
@ ARCHIVE_MODE_ALWAYS
Definition: xlog.h:67
@ ARCHIVE_MODE_OFF
Definition: xlog.h:65
@ ARCHIVE_MODE_ON
Definition: xlog.h:66

◆ RecoveryState

Enumerator
RECOVERY_STATE_CRASH 
RECOVERY_STATE_ARCHIVE 
RECOVERY_STATE_DONE 

Definition at line 89 of file xlog.h.

90{
91 RECOVERY_STATE_CRASH = 0, /* crash recovery */
92 RECOVERY_STATE_ARCHIVE, /* archive recovery */
93 RECOVERY_STATE_DONE, /* currently in production */
RecoveryState
Definition: xlog.h:90
@ RECOVERY_STATE_CRASH
Definition: xlog.h:91
@ RECOVERY_STATE_DONE
Definition: xlog.h:93
@ RECOVERY_STATE_ARCHIVE
Definition: xlog.h:92

◆ SessionBackupState

Enumerator
SESSION_BACKUP_NONE 
SESSION_BACKUP_RUNNING 

Definition at line 286 of file xlog.h.

289{
@ SESSION_BACKUP_NONE
Definition: xlog.h:288

◆ WALAvailability

Enumerator
WALAVAIL_INVALID_LSN 
WALAVAIL_RESERVED 
WALAVAIL_EXTENDED 
WALAVAIL_UNRESERVED 
WALAVAIL_REMOVED 

Definition at line 187 of file xlog.h.

190{
191 WALAVAIL_INVALID_LSN, /* parameter error */
192 WALAVAIL_RESERVED, /* WAL segment is within max_wal_size */
193 WALAVAIL_EXTENDED, /* WAL segment is reserved by a slot or
194 * wal_keep_size */
195 WALAVAIL_UNRESERVED, /* no longer reserved, but not removed yet */
@ WALAVAIL_RESERVED
Definition: xlog.h:190
@ WALAVAIL_UNRESERVED
Definition: xlog.h:193
@ WALAVAIL_EXTENDED
Definition: xlog.h:191
@ WALAVAIL_INVALID_LSN
Definition: xlog.h:189

◆ WalCompression

Enumerator
WAL_COMPRESSION_NONE 
WAL_COMPRESSION_PGLZ 
WAL_COMPRESSION_LZ4 
WAL_COMPRESSION_ZSTD 

Definition at line 80 of file xlog.h.

81{
WalCompression
Definition: xlog.h:81
@ WAL_COMPRESSION_NONE
Definition: xlog.h:82
@ WAL_COMPRESSION_LZ4
Definition: xlog.h:84
@ WAL_COMPRESSION_PGLZ
Definition: xlog.h:83
@ WAL_COMPRESSION_ZSTD
Definition: xlog.h:85

◆ WalLevel

enum WalLevel
Enumerator
WAL_LEVEL_MINIMAL 
WAL_LEVEL_REPLICA 
WAL_LEVEL_LOGICAL 

Definition at line 72 of file xlog.h.

73{
77} WalLevel;
WalLevel
Definition: xlog.h:73
@ WAL_LEVEL_REPLICA
Definition: xlog.h:75
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76
@ WAL_LEVEL_MINIMAL
Definition: xlog.h:74

◆ WalSyncMethod

Enumerator
WAL_SYNC_METHOD_FSYNC 
WAL_SYNC_METHOD_FDATASYNC 
WAL_SYNC_METHOD_OPEN 
WAL_SYNC_METHOD_FSYNC_WRITETHROUGH 
WAL_SYNC_METHOD_OPEN_DSYNC 

Definition at line 22 of file xlog.h.

23{
26 WAL_SYNC_METHOD_OPEN, /* for O_SYNC */
28 WAL_SYNC_METHOD_OPEN_DSYNC /* for O_DSYNC */
29};
@ WAL_SYNC_METHOD_OPEN
Definition: xlog.h:26
@ WAL_SYNC_METHOD_FDATASYNC
Definition: xlog.h:25
@ WAL_SYNC_METHOD_FSYNC_WRITETHROUGH
Definition: xlog.h:27
@ WAL_SYNC_METHOD_OPEN_DSYNC
Definition: xlog.h:28
@ WAL_SYNC_METHOD_FSYNC
Definition: xlog.h:24

Function Documentation

◆ BootStrapXLOG()

void BootStrapXLOG ( uint32  data_checksum_version)

Definition at line 5218 of file xlog.c.

5219{
5220 CheckPoint checkPoint;
5221 char *buffer;
5222 XLogPageHeader page;
5223 XLogLongPageHeader longpage;
5224 XLogRecord *record;
5225 char *recptr;
5226 uint64 sysidentifier;
5227 struct timeval tv;
5228 pg_crc32c crc;
5229
5230 /* allow ordinary WAL segment creation, like StartupXLOG() would */
5232
5233 /*
5234 * Select a hopefully-unique system identifier code for this installation.
5235 * We use the result of gettimeofday(), including the fractional seconds
5236 * field, as being about as unique as we can easily get. (Think not to
5237 * use random(), since it hasn't been seeded and there's no portable way
5238 * to seed it other than the system clock value...) The upper half of the
5239 * uint64 value is just the tv_sec part, while the lower half contains the
5240 * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
5241 * PID for a little extra uniqueness. A person knowing this encoding can
5242 * determine the initialization time of the installation, which could
5243 * perhaps be useful sometimes.
5244 */
5245 gettimeofday(&tv, NULL);
5246 sysidentifier = ((uint64) tv.tv_sec) << 32;
5247 sysidentifier |= ((uint64) tv.tv_usec) << 12;
5248 sysidentifier |= getpid() & 0xFFF;
5249
5250 /* page buffer must be aligned suitably for O_DIRECT */
5251 buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
5252 page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
5253 memset(page, 0, XLOG_BLCKSZ);
5254
5255 /*
5256 * Set up information for the initial checkpoint record
5257 *
5258 * The initial checkpoint record is written to the beginning of the WAL
5259 * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not
5260 * used, so that we can use 0/0 to mean "before any valid WAL segment".
5261 */
5265 checkPoint.fullPageWrites = fullPageWrites;
5266 checkPoint.wal_level = wal_level;
5267 checkPoint.nextXid =
5269 checkPoint.nextOid = FirstGenbkiObjectId;
5270 checkPoint.nextMulti = FirstMultiXactId;
5271 checkPoint.nextMultiOffset = 0;
5273 checkPoint.oldestXidDB = Template1DbOid;
5274 checkPoint.oldestMulti = FirstMultiXactId;
5275 checkPoint.oldestMultiDB = Template1DbOid;
5278 checkPoint.time = (pg_time_t) time(NULL);
5280
5281 TransamVariables->nextXid = checkPoint.nextXid;
5282 TransamVariables->nextOid = checkPoint.nextOid;
5284 MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5285 AdvanceOldestClogXid(checkPoint.oldestXid);
5286 SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5287 SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5289
5290 /* Set up the XLOG page header */
5291 page->xlp_magic = XLOG_PAGE_MAGIC;
5292 page->xlp_info = XLP_LONG_HEADER;
5295 longpage = (XLogLongPageHeader) page;
5296 longpage->xlp_sysid = sysidentifier;
5297 longpage->xlp_seg_size = wal_segment_size;
5298 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
5299
5300 /* Insert the initial checkpoint record */
5301 recptr = ((char *) page + SizeOfXLogLongPHD);
5302 record = (XLogRecord *) recptr;
5303 record->xl_prev = 0;
5304 record->xl_xid = InvalidTransactionId;
5305 record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(checkPoint);
5307 record->xl_rmid = RM_XLOG_ID;
5308 recptr += SizeOfXLogRecord;
5309 /* fill the XLogRecordDataHeaderShort struct */
5310 *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
5311 *(recptr++) = sizeof(checkPoint);
5312 memcpy(recptr, &checkPoint, sizeof(checkPoint));
5313 recptr += sizeof(checkPoint);
5314 Assert(recptr - (char *) record == record->xl_tot_len);
5315
5317 COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
5318 COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
5319 FIN_CRC32C(crc);
5320 record->xl_crc = crc;
5321
5322 /* Create first XLOG segment file */
5325
5326 /*
5327 * We needn't bother with Reserve/ReleaseExternalFD here, since we'll
5328 * close the file again in a moment.
5329 */
5330
5331 /* Write the first page with the initial record */
5332 errno = 0;
5333 pgstat_report_wait_start(WAIT_EVENT_WAL_BOOTSTRAP_WRITE);
5334 if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
5335 {
5336 /* if write didn't set errno, assume problem is no disk space */
5337 if (errno == 0)
5338 errno = ENOSPC;
5339 ereport(PANIC,
5341 errmsg("could not write bootstrap write-ahead log file: %m")));
5342 }
5344
5345 pgstat_report_wait_start(WAIT_EVENT_WAL_BOOTSTRAP_SYNC);
5346 if (pg_fsync(openLogFile) != 0)
5347 ereport(PANIC,
5349 errmsg("could not fsync bootstrap write-ahead log file: %m")));
5351
5352 if (close(openLogFile) != 0)
5353 ereport(PANIC,
5355 errmsg("could not close bootstrap write-ahead log file: %m")));
5356
5357 openLogFile = -1;
5358
5359 /* Now create pg_control */
5360 InitControlFile(sysidentifier, data_checksum_version);
5361 ControlFile->time = checkPoint.time;
5362 ControlFile->checkPoint = checkPoint.redo;
5363 ControlFile->checkPointCopy = checkPoint;
5364
5365 /* some additional ControlFile fields are set in WriteControlFile() */
5367
5368 /* Bootstrap the commit log, too */
5369 BootStrapCLOG();
5373
5374 pfree(buffer);
5375
5376 /*
5377 * Force control file to be read - in contrast to normal processing we'd
5378 * otherwise never run the checks and GUC related initializations therein.
5379 */
5381}
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:775
uint64_t uint64
Definition: c.h:503
void BootStrapCLOG(void)
Definition: clog.c:833
void BootStrapCommitTs(void)
Definition: commit_ts.c:596
void SetCommitTsLimit(TransactionId oldestXact, TransactionId newestXact)
Definition: commit_ts.c:909
int errcode_for_file_access(void)
Definition: elog.c:877
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define PANIC
Definition: elog.h:42
#define ereport(elevel,...)
Definition: elog.h:149
int pg_fsync(int fd)
Definition: fd.c:386
Assert(PointerIsAligned(start, uint64))
#define close(a)
Definition: win32.h:12
#define write(a, b, c)
Definition: win32.h:14
void pfree(void *pointer)
Definition: mcxt.c:2147
void * palloc(Size size)
Definition: mcxt.c:1940
void MultiXactSetNextMXact(MultiXactId nextMulti, MultiXactOffset nextMultiOffset)
Definition: multixact.c:2328
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2362
void BootStrapMultiXact(void)
Definition: multixact.c:2034
#define FirstMultiXactId
Definition: multixact.h:25
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:68
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:153
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:158
return crc
int64 pg_time_t
Definition: pgtime.h:23
Oid oldestMultiDB
Definition: pg_control.h:51
MultiXactId oldestMulti
Definition: pg_control.h:50
MultiXactOffset nextMultiOffset
Definition: pg_control.h:47
TransactionId newestCommitTsXid
Definition: pg_control.h:55
TransactionId oldestXid
Definition: pg_control.h:48
TimeLineID PrevTimeLineID
Definition: pg_control.h:40
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
Oid nextOid
Definition: pg_control.h:45
TransactionId oldestActiveXid
Definition: pg_control.h:64
bool fullPageWrites
Definition: pg_control.h:42
MultiXactId nextMulti
Definition: pg_control.h:46
FullTransactionId nextXid
Definition: pg_control.h:44
TransactionId oldestCommitTsXid
Definition: pg_control.h:53
pg_time_t time
Definition: pg_control.h:52
int wal_level
Definition: pg_control.h:43
XLogRecPtr redo
Definition: pg_control.h:37
Oid oldestXidDB
Definition: pg_control.h:49
CheckPoint checkPointCopy
Definition: pg_control.h:135
pg_time_t time
Definition: pg_control.h:132
XLogRecPtr checkPoint
Definition: pg_control.h:133
FullTransactionId nextXid
Definition: transam.h:220
TimeLineID xlp_tli
Definition: xlog_internal.h:40
XLogRecPtr xlp_pageaddr
Definition: xlog_internal.h:41
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
uint8 xl_info
Definition: xlogrecord.h:46
uint32 xl_tot_len
Definition: xlogrecord.h:43
TransactionId xl_xid
Definition: xlogrecord.h:44
RmgrId xl_rmid
Definition: xlogrecord.h:47
void BootStrapSUBTRANS(void)
Definition: subtrans.c:270
#define InvalidTransactionId
Definition: transam.h:31
#define FirstGenbkiObjectId
Definition: transam.h:195
#define FirstNormalTransactionId
Definition: transam.h:34
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:372
void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid)
Definition: varsup.c:355
TransamVariablesData * TransamVariables
Definition: varsup.c:34
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
int gettimeofday(struct timeval *tp, void *tzp)
int XLogFileInit(XLogSegNo logsegno, TimeLineID logtli)
Definition: xlog.c:3519
bool fullPageWrites
Definition: xlog.c:122
static void InitControlFile(uint64 sysidentifier, uint32 data_checksum_version)
Definition: xlog.c:4343
void SetInstallXLogFileSegmentActive(void)
Definition: xlog.c:9661
static int openLogFile
Definition: xlog.c:646
int wal_level
Definition: xlog.c:131
static void WriteControlFile(void)
Definition: xlog.c:4378
int wal_segment_size
Definition: xlog.c:143
static TimeLineID openLogTLI
Definition: xlog.c:648
static ControlFileData * ControlFile
Definition: xlog.c:585
#define BootstrapTimeLineID
Definition: xlog.c:111
static void ReadControlFile(void)
Definition: xlog.c:4487
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:71
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:54
#define XLP_LONG_HEADER
Definition: xlog_internal.h:76
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:69
#define SizeOfXLogRecordDataHeaderShort
Definition: xlogrecord.h:217
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:241
#define SizeOfXLogRecord
Definition: xlogrecord.h:55

References AdvanceOldestClogXid(), Assert(), BootStrapCLOG(), BootStrapCommitTs(), BootStrapMultiXact(), BootStrapSUBTRANS(), BootstrapTimeLineID, ControlFileData::checkPoint, ControlFileData::checkPointCopy, close, COMP_CRC32C, ControlFile, crc, ereport, errcode_for_file_access(), errmsg(), FIN_CRC32C, FirstGenbkiObjectId, FirstMultiXactId, FirstNormalTransactionId, fullPageWrites, CheckPoint::fullPageWrites, FullTransactionIdFromEpochAndXid(), gettimeofday(), INIT_CRC32C, InitControlFile(), InvalidTransactionId, MultiXactSetNextMXact(), CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, TransamVariablesData::nextOid, CheckPoint::nextOid, TransamVariablesData::nextXid, CheckPoint::nextXid, TransamVariablesData::oidCount, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, CheckPoint::oldestXid, CheckPoint::oldestXidDB, openLogFile, openLogTLI, palloc(), PANIC, pfree(), pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), CheckPoint::PrevTimeLineID, ReadControlFile(), CheckPoint::redo, SetCommitTsLimit(), SetInstallXLogFileSegmentActive(), SetMultiXactIdLimit(), SetTransactionIdLimit(), SizeOfXLogLongPHD, SizeOfXLogRecord, SizeOfXLogRecordDataHeaderShort, CheckPoint::ThisTimeLineID, CheckPoint::time, ControlFileData::time, TransamVariables, TYPEALIGN, wal_level, CheckPoint::wal_level, wal_segment_size, 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(), 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 BootstrapModeMain().

◆ CheckXLogRemoved()

void CheckXLogRemoved ( XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 3866 of file xlog.c.

3867{
3868 int save_errno = errno;
3869 XLogSegNo lastRemovedSegNo;
3870
3872 lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3874
3875 if (segno <= lastRemovedSegNo)
3876 {
3877 char filename[MAXFNAMELEN];
3878
3880 errno = save_errno;
3881 ereport(ERROR,
3883 errmsg("requested WAL segment %s has already been removed",
3884 filename)));
3885 }
3886 errno = save_errno;
3887}
#define ERROR
Definition: elog.h:39
static char * filename
Definition: pg_dumpall.c:123
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
slock_t info_lck
Definition: xlog.c:564
XLogSegNo lastRemovedSegNo
Definition: xlog.c:456
static XLogCtlData * XLogCtl
Definition: xlog.c:577
#define MAXFNAMELEN
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
uint64 XLogSegNo
Definition: xlogdefs.h:48

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

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

◆ CreateCheckPoint()

bool CreateCheckPoint ( int  flags)

Definition at line 7077 of file xlog.c.

7078{
7079 bool shutdown;
7080 CheckPoint checkPoint;
7081 XLogRecPtr recptr;
7082 XLogSegNo _logSegNo;
7084 uint32 freespace;
7085 XLogRecPtr PriorRedoPtr;
7086 XLogRecPtr last_important_lsn;
7087 VirtualTransactionId *vxids;
7088 int nvxids;
7089 int oldXLogAllowed = 0;
7090
7091 /*
7092 * An end-of-recovery checkpoint is really a shutdown checkpoint, just
7093 * issued at a different time.
7094 */
7096 shutdown = true;
7097 else
7098 shutdown = false;
7099
7100 /* sanity check */
7101 if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
7102 elog(ERROR, "can't create a checkpoint during recovery");
7103
7104 /*
7105 * Prepare to accumulate statistics.
7106 *
7107 * Note: because it is possible for log_checkpoints to change while a
7108 * checkpoint proceeds, we always accumulate stats, even if
7109 * log_checkpoints is currently off.
7110 */
7113
7114 /*
7115 * Let smgr prepare for checkpoint; this has to happen outside the
7116 * critical section and before we determine the REDO pointer. Note that
7117 * smgr must not do anything that'd have to be undone if we decide no
7118 * checkpoint is needed.
7119 */
7121
7122 /*
7123 * Use a critical section to force system panic if we have trouble.
7124 */
7126
7127 if (shutdown)
7128 {
7129 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7132 LWLockRelease(ControlFileLock);
7133 }
7134
7135 /* Begin filling in the checkpoint WAL record */
7136 MemSet(&checkPoint, 0, sizeof(checkPoint));
7137 checkPoint.time = (pg_time_t) time(NULL);
7138
7139 /*
7140 * For Hot Standby, derive the oldestActiveXid before we fix the redo
7141 * pointer. This allows us to begin accumulating changes to assemble our
7142 * starting snapshot of locks and transactions.
7143 */
7144 if (!shutdown && XLogStandbyInfoActive())
7146 else
7148
7149 /*
7150 * Get location of last important record before acquiring insert locks (as
7151 * GetLastImportantRecPtr() also locks WAL locks).
7152 */
7153 last_important_lsn = GetLastImportantRecPtr();
7154
7155 /*
7156 * If this isn't a shutdown or forced checkpoint, and if there has been no
7157 * WAL activity requiring a checkpoint, skip it. The idea here is to
7158 * avoid inserting duplicate checkpoints when the system is idle.
7159 */
7161 CHECKPOINT_FORCE)) == 0)
7162 {
7163 if (last_important_lsn == ControlFile->checkPoint)
7164 {
7167 (errmsg_internal("checkpoint skipped because system is idle")));
7168 return false;
7169 }
7170 }
7171
7172 /*
7173 * An end-of-recovery checkpoint is created before anyone is allowed to
7174 * write WAL. To allow us to write the checkpoint record, temporarily
7175 * enable XLogInsertAllowed.
7176 */
7177 if (flags & CHECKPOINT_END_OF_RECOVERY)
7178 oldXLogAllowed = LocalSetXLogInsertAllowed();
7179
7181 if (flags & CHECKPOINT_END_OF_RECOVERY)
7183 else
7184 checkPoint.PrevTimeLineID = checkPoint.ThisTimeLineID;
7185
7186 /*
7187 * We must block concurrent insertions while examining insert state.
7188 */
7190
7191 checkPoint.fullPageWrites = Insert->fullPageWrites;
7192 checkPoint.wal_level = wal_level;
7193
7194 if (shutdown)
7195 {
7196 XLogRecPtr curInsert = XLogBytePosToRecPtr(Insert->CurrBytePos);
7197
7198 /*
7199 * Compute new REDO record ptr = location of next XLOG record.
7200 *
7201 * Since this is a shutdown checkpoint, there can't be any concurrent
7202 * WAL insertion.
7203 */
7204 freespace = INSERT_FREESPACE(curInsert);
7205 if (freespace == 0)
7206 {
7207 if (XLogSegmentOffset(curInsert, wal_segment_size) == 0)
7208 curInsert += SizeOfXLogLongPHD;
7209 else
7210 curInsert += SizeOfXLogShortPHD;
7211 }
7212 checkPoint.redo = curInsert;
7213
7214 /*
7215 * Here we update the shared RedoRecPtr for future XLogInsert calls;
7216 * this must be done while holding all the insertion locks.
7217 *
7218 * Note: if we fail to complete the checkpoint, RedoRecPtr will be
7219 * left pointing past where it really needs to point. This is okay;
7220 * the only consequence is that XLogInsert might back up whole buffers
7221 * that it didn't really need to. We can't postpone advancing
7222 * RedoRecPtr because XLogInserts that happen while we are dumping
7223 * buffers must assume that their buffer changes are not included in
7224 * the checkpoint.
7225 */
7226 RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
7227 }
7228
7229 /*
7230 * Now we can release the WAL insertion locks, allowing other xacts to
7231 * proceed while we are flushing disk buffers.
7232 */
7234
7235 /*
7236 * If this is an online checkpoint, we have not yet determined the redo
7237 * point. We do so now by inserting the special XLOG_CHECKPOINT_REDO
7238 * record; the LSN at which it starts becomes the new redo pointer. We
7239 * don't do this for a shutdown checkpoint, because in that case no WAL
7240 * can be written between the redo point and the insertion of the
7241 * checkpoint record itself, so the checkpoint record itself serves to
7242 * mark the redo point.
7243 */
7244 if (!shutdown)
7245 {
7246 /* Include WAL level in record for WAL summarizer's benefit. */
7249 (void) XLogInsert(RM_XLOG_ID, XLOG_CHECKPOINT_REDO);
7250
7251 /*
7252 * XLogInsertRecord will have updated XLogCtl->Insert.RedoRecPtr in
7253 * shared memory and RedoRecPtr in backend-local memory, but we need
7254 * to copy that into the record that will be inserted when the
7255 * checkpoint is complete.
7256 */
7257 checkPoint.redo = RedoRecPtr;
7258 }
7259
7260 /* Update the info_lck-protected copy of RedoRecPtr as well */
7262 XLogCtl->RedoRecPtr = checkPoint.redo;
7264
7265 /*
7266 * If enabled, log checkpoint start. We postpone this until now so as not
7267 * to log anything if we decided to skip the checkpoint.
7268 */
7269 if (log_checkpoints)
7270 LogCheckpointStart(flags, false);
7271
7272 /* Update the process title */
7273 update_checkpoint_display(flags, false, false);
7274
7275 TRACE_POSTGRESQL_CHECKPOINT_START(flags);
7276
7277 /*
7278 * Get the other info we need for the checkpoint record.
7279 *
7280 * We don't need to save oldestClogXid in the checkpoint, it only matters
7281 * for the short period in which clog is being truncated, and if we crash
7282 * during that we'll redo the clog truncation and fix up oldestClogXid
7283 * there.
7284 */
7285 LWLockAcquire(XidGenLock, LW_SHARED);
7286 checkPoint.nextXid = TransamVariables->nextXid;
7287 checkPoint.oldestXid = TransamVariables->oldestXid;
7289 LWLockRelease(XidGenLock);
7290
7291 LWLockAcquire(CommitTsLock, LW_SHARED);
7294 LWLockRelease(CommitTsLock);
7295
7296 LWLockAcquire(OidGenLock, LW_SHARED);
7297 checkPoint.nextOid = TransamVariables->nextOid;
7298 if (!shutdown)
7299 checkPoint.nextOid += TransamVariables->oidCount;
7300 LWLockRelease(OidGenLock);
7301
7302 MultiXactGetCheckptMulti(shutdown,
7303 &checkPoint.nextMulti,
7304 &checkPoint.nextMultiOffset,
7305 &checkPoint.oldestMulti,
7306 &checkPoint.oldestMultiDB);
7307
7308 /*
7309 * Having constructed the checkpoint record, ensure all shmem disk buffers
7310 * and commit-log buffers are flushed to disk.
7311 *
7312 * This I/O could fail for various reasons. If so, we will fail to
7313 * complete the checkpoint, but there is no reason to force a system
7314 * panic. Accordingly, exit critical section while doing it.
7315 */
7317
7318 /*
7319 * In some cases there are groups of actions that must all occur on one
7320 * side or the other of a checkpoint record. Before flushing the
7321 * checkpoint record we must explicitly wait for any backend currently
7322 * performing those groups of actions.
7323 *
7324 * One example is end of transaction, so we must wait for any transactions
7325 * that are currently in commit critical sections. If an xact inserted
7326 * its commit record into XLOG just before the REDO point, then a crash
7327 * restart from the REDO point would not replay that record, which means
7328 * that our flushing had better include the xact's update of pg_xact. So
7329 * we wait till he's out of his commit critical section before proceeding.
7330 * See notes in RecordTransactionCommit().
7331 *
7332 * Because we've already released the insertion locks, this test is a bit
7333 * fuzzy: it is possible that we will wait for xacts we didn't really need
7334 * to wait for. But the delay should be short and it seems better to make
7335 * checkpoint take a bit longer than to hold off insertions longer than
7336 * necessary. (In fact, the whole reason we have this issue is that xact.c
7337 * does commit record XLOG insertion and clog update as two separate steps
7338 * protected by different locks, but again that seems best on grounds of
7339 * minimizing lock contention.)
7340 *
7341 * A transaction that has not yet set delayChkptFlags when we look cannot
7342 * be at risk, since it has not inserted its commit record yet; and one
7343 * that's already cleared it is not at risk either, since it's done fixing
7344 * clog and we will correctly flush the update below. So we cannot miss
7345 * any xacts we need to wait for.
7346 */
7348 if (nvxids > 0)
7349 {
7350 do
7351 {
7352 /*
7353 * Keep absorbing fsync requests while we wait. There could even
7354 * be a deadlock if we don't, if the process that prevents the
7355 * checkpoint is trying to add a request to the queue.
7356 */
7358
7359 pgstat_report_wait_start(WAIT_EVENT_CHECKPOINT_DELAY_START);
7360 pg_usleep(10000L); /* wait for 10 msec */
7362 } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
7364 }
7365 pfree(vxids);
7366
7367 CheckPointGuts(checkPoint.redo, flags);
7368
7370 if (nvxids > 0)
7371 {
7372 do
7373 {
7375
7376 pgstat_report_wait_start(WAIT_EVENT_CHECKPOINT_DELAY_COMPLETE);
7377 pg_usleep(10000L); /* wait for 10 msec */
7379 } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
7381 }
7382 pfree(vxids);
7383
7384 /*
7385 * Take a snapshot of running transactions and write this to WAL. This
7386 * allows us to reconstruct the state of running transactions during
7387 * archive recovery, if required. Skip, if this info disabled.
7388 *
7389 * If we are shutting down, or Startup process is completing crash
7390 * recovery we don't need to write running xact data.
7391 */
7392 if (!shutdown && XLogStandbyInfoActive())
7394
7396
7397 /*
7398 * Now insert the checkpoint record into XLOG.
7399 */
7401 XLogRegisterData(&checkPoint, sizeof(checkPoint));
7402 recptr = XLogInsert(RM_XLOG_ID,
7403 shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
7405
7406 XLogFlush(recptr);
7407
7408 /*
7409 * We mustn't write any new WAL after a shutdown checkpoint, or it will be
7410 * overwritten at next startup. No-one should even try, this just allows
7411 * sanity-checking. In the case of an end-of-recovery checkpoint, we want
7412 * to just temporarily disable writing until the system has exited
7413 * recovery.
7414 */
7415 if (shutdown)
7416 {
7417 if (flags & CHECKPOINT_END_OF_RECOVERY)
7418 LocalXLogInsertAllowed = oldXLogAllowed;
7419 else
7420 LocalXLogInsertAllowed = 0; /* never again write WAL */
7421 }
7422
7423 /*
7424 * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
7425 * = end of actual checkpoint record.
7426 */
7427 if (shutdown && checkPoint.redo != ProcLastRecPtr)
7428 ereport(PANIC,
7429 (errmsg("concurrent write-ahead log activity while database system is shutting down")));
7430
7431 /*
7432 * Remember the prior checkpoint's redo ptr for
7433 * UpdateCheckPointDistanceEstimate()
7434 */
7435 PriorRedoPtr = ControlFile->checkPointCopy.redo;
7436
7437 /*
7438 * Update the control file.
7439 */
7440 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7441 if (shutdown)
7444 ControlFile->checkPointCopy = checkPoint;
7445 /* crash recovery should always recover to the end of WAL */
7448
7449 /*
7450 * Persist unloggedLSN value. It's reset on crash recovery, so this goes
7451 * unused on non-shutdown checkpoints, but seems useful to store it always
7452 * for debugging purposes.
7453 */
7455
7457 LWLockRelease(ControlFileLock);
7458
7459 /* Update shared-memory copy of checkpoint XID/epoch */
7461 XLogCtl->ckptFullXid = checkPoint.nextXid;
7463
7464 /*
7465 * We are now done with critical updates; no need for system panic if we
7466 * have trouble while fooling with old log segments.
7467 */
7469
7470 /*
7471 * WAL summaries end when the next XLOG_CHECKPOINT_REDO or
7472 * XLOG_CHECKPOINT_SHUTDOWN record is reached. This is the first point
7473 * where (a) we're not inside of a critical section and (b) we can be
7474 * certain that the relevant record has been flushed to disk, which must
7475 * happen before it can be summarized.
7476 *
7477 * If this is a shutdown checkpoint, then this happens reasonably
7478 * promptly: we've only just inserted and flushed the
7479 * XLOG_CHECKPOINT_SHUTDOWN record. If this is not a shutdown checkpoint,
7480 * then this might not be very prompt at all: the XLOG_CHECKPOINT_REDO
7481 * record was written before we began flushing data to disk, and that
7482 * could be many minutes ago at this point. However, we don't XLogFlush()
7483 * after inserting that record, so we're not guaranteed that it's on disk
7484 * until after the above call that flushes the XLOG_CHECKPOINT_ONLINE
7485 * record.
7486 */
7488
7489 /*
7490 * Let smgr do post-checkpoint cleanup (eg, deleting old files).
7491 */
7493
7494 /*
7495 * Update the average distance between checkpoints if the prior checkpoint
7496 * exists.
7497 */
7498 if (PriorRedoPtr != InvalidXLogRecPtr)
7500
7501 /*
7502 * Delete old log files, those no longer needed for last checkpoint to
7503 * prevent the disk holding the xlog from growing full.
7504 */
7506 KeepLogSeg(recptr, &_logSegNo);
7508 _logSegNo, InvalidOid,
7510 {
7511 /*
7512 * Some slots have been invalidated; recalculate the old-segment
7513 * horizon, starting again from RedoRecPtr.
7514 */
7516 KeepLogSeg(recptr, &_logSegNo);
7517 }
7518 _logSegNo--;
7519 RemoveOldXlogFiles(_logSegNo, RedoRecPtr, recptr,
7520 checkPoint.ThisTimeLineID);
7521
7522 /*
7523 * Make more log segments if needed. (Do this after recycling old log
7524 * segments, since that may supply some of the needed files.)
7525 */
7526 if (!shutdown)
7527 PreallocXlogFiles(recptr, checkPoint.ThisTimeLineID);
7528
7529 /*
7530 * Truncate pg_subtrans if possible. We can throw away all data before
7531 * the oldest XMIN of any running transaction. No future transaction will
7532 * attempt to reference any pg_subtrans entry older than that (see Asserts
7533 * in subtrans.c). During recovery, though, we mustn't do this because
7534 * StartupSUBTRANS hasn't been called yet.
7535 */
7536 if (!RecoveryInProgress())
7538
7539 /* Real work is done; log and update stats. */
7540 LogCheckpointEnd(false);
7541
7542 /* Reset the process title */
7543 update_checkpoint_display(flags, false, true);
7544
7545 TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats.ckpt_bufs_written,
7546 NBuffers,
7550
7551 return true;
7552}
static uint64 pg_atomic_read_membarrier_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:476
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
uint32_t uint32
Definition: c.h:502
#define MemSet(start, val, len)
Definition: c.h:991
void AbsorbSyncRequests(void)
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:226
static void Insert(File file)
Definition: fd.c:1317
int NBuffers
Definition: globals.c:143
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
#define START_CRIT_SECTION()
Definition: miscadmin.h:150
#define END_CRIT_SECTION()
Definition: miscadmin.h:152
void MultiXactGetCheckptMulti(bool is_shutdown, MultiXactId *nextMulti, MultiXactOffset *nextMultiOffset, MultiXactId *oldestMulti, Oid *oldestMultiDB)
Definition: multixact.c:2282
#define XLOG_CHECKPOINT_REDO
Definition: pg_control.h:82
@ DB_SHUTDOWNING
Definition: pg_control.h:94
@ DB_SHUTDOWNED
Definition: pg_control.h:92
#define XLOG_CHECKPOINT_ONLINE
Definition: pg_control.h:69
#define InvalidOid
Definition: postgres_ext.h:35
#define DELAY_CHKPT_START
Definition: proc.h:120
#define DELAY_CHKPT_COMPLETE
Definition: proc.h:121
TransactionId GetOldestActiveTransactionId(void)
Definition: procarray.c:2880
TransactionId GetOldestTransactionIdConsideredRunning(void)
Definition: procarray.c:2034
bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
Definition: procarray.c:3089
VirtualTransactionId * GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
Definition: procarray.c:3043
void pg_usleep(long microsec)
Definition: signal.c:53
bool InvalidateObsoleteReplicationSlots(uint32 possible_causes, XLogSegNo oldestSegno, Oid dboid, TransactionId snapshotConflictHorizon)
Definition: slot.c:1976
@ RS_INVAL_WAL_REMOVED
Definition: slot.h:55
@ RS_INVAL_IDLE_TIMEOUT
Definition: slot.h:61
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1282
TimestampTz ckpt_start_t
Definition: xlog.h:161
int ckpt_segs_removed
Definition: xlog.h:171
int ckpt_segs_added
Definition: xlog.h:170
int ckpt_bufs_written
Definition: xlog.h:167
int ckpt_segs_recycled
Definition: xlog.h:172
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:168
XLogRecPtr unloggedLSN
Definition: pg_control.h:137
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:169
TransactionId oldestCommitTsXid
Definition: transam.h:232
TransactionId newestCommitTsXid
Definition: transam.h:233
TransactionId oldestXid
Definition: transam.h:222
FullTransactionId ckptFullXid
Definition: xlog.c:452
TimeLineID InsertTimeLineID
Definition: xlog.c:520
XLogRecPtr RedoRecPtr
Definition: xlog.c:451
XLogCtlInsert Insert
Definition: xlog.c:447
TimeLineID PrevTimeLineID
Definition: xlog.c:521
pg_atomic_uint64 unloggedLSN
Definition: xlog.c:459
XLogRecPtr RedoRecPtr
Definition: xlog.c:425
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:411
void SyncPreCheckpoint(void)
Definition: sync.c:177
void SyncPostCheckpoint(void)
Definition: sync.c:202
void WakeupWalSummarizer(void)
XLogRecPtr ProcLastRecPtr
Definition: xlog.c:253
bool RecoveryInProgress(void)
Definition: xlog.c:6522
static void WALInsertLockRelease(void)
Definition: xlog.c:1456
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1869
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1427
static void UpdateControlFile(void)
Definition: xlog.c:4725
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr, TimeLineID insertTLI)
Definition: xlog.c:4004
static void LogCheckpointStart(int flags, bool restartpoint)
Definition: xlog.c:6837
static XLogRecPtr RedoRecPtr
Definition: xlog.c:273
static void LogCheckpointEnd(bool restartpoint)
Definition: xlog.c:6869
static void PreallocXlogFiles(XLogRecPtr endptr, TimeLineID tli)
Definition: xlog.c:3829
bool log_checkpoints
Definition: xlog.c:129
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:8141
static int LocalSetXLogInsertAllowed(void)
Definition: xlog.c:6610
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:6744
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:6974
#define INSERT_FREESPACE(endptr)
Definition: xlog.c:591
static int LocalXLogInsertAllowed
Definition: xlog.c:236
CheckpointStatsData CheckpointStats
Definition: xlog.c:209
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2923
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
Definition: xlog.c:7698
static void update_checkpoint_display(int flags, bool restartpoint, bool reset)
Definition: xlog.c:7012
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:140
#define CHECKPOINT_FORCE
Definition: xlog.h:142
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:139
#define XLogStandbyInfoActive()
Definition: xlog.h:123
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:52
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:474
void XLogRegisterData(const void *data, uint32 len)
Definition: xloginsert.c:364
void XLogBeginInsert(void)
Definition: xloginsert.c:149

References AbsorbSyncRequests(), ControlFileData::checkPoint, CHECKPOINT_END_OF_RECOVERY, CHECKPOINT_FORCE, CHECKPOINT_IS_SHUTDOWN, ControlFileData::checkPointCopy, CheckPointGuts(), CheckpointStats, CheckpointStatsData::ckpt_bufs_written, CheckpointStatsData::ckpt_segs_added, CheckpointStatsData::ckpt_segs_recycled, CheckpointStatsData::ckpt_segs_removed, CheckpointStatsData::ckpt_start_t, XLogCtlData::ckptFullXid, ControlFile, DB_SHUTDOWNED, DB_SHUTDOWNING, DEBUG1, DELAY_CHKPT_COMPLETE, DELAY_CHKPT_START, elog, END_CRIT_SECTION, ereport, errmsg(), errmsg_internal(), ERROR, CheckPoint::fullPageWrites, GetCurrentTimestamp(), GetLastImportantRecPtr(), GetOldestActiveTransactionId(), GetOldestTransactionIdConsideredRunning(), GetVirtualXIDsDelayingChkpt(), HaveVirtualXIDsDelayingChkpt(), XLogCtlData::info_lck, XLogCtlData::Insert, Insert(), INSERT_FREESPACE, XLogCtlData::InsertTimeLineID, InvalidateObsoleteReplicationSlots(), InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, KeepLogSeg(), LocalSetXLogInsertAllowed(), LocalXLogInsertAllowed, log_checkpoints, LogCheckpointEnd(), LogCheckpointStart(), LogStandbySnapshot(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemSet, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, MultiXactGetCheckptMulti(), NBuffers, TransamVariablesData::newestCommitTsXid, CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, TransamVariablesData::nextOid, CheckPoint::nextOid, TransamVariablesData::nextXid, CheckPoint::nextXid, TransamVariablesData::oidCount, CheckPoint::oldestActiveXid, TransamVariablesData::oldestCommitTsXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, TransamVariablesData::oldestXid, CheckPoint::oldestXid, TransamVariablesData::oldestXidDB, CheckPoint::oldestXidDB, PANIC, pfree(), pg_atomic_read_membarrier_u64(), pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), PreallocXlogFiles(), XLogCtlData::PrevTimeLineID, CheckPoint::PrevTimeLineID, ProcLastRecPtr, RecoveryInProgress(), CheckPoint::redo, RedoRecPtr, XLogCtlInsert::RedoRecPtr, XLogCtlData::RedoRecPtr, RemoveOldXlogFiles(), RS_INVAL_IDLE_TIMEOUT, RS_INVAL_WAL_REMOVED, SizeOfXLogLongPHD, SizeOfXLogShortPHD, SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, ControlFileData::state, SyncPostCheckpoint(), SyncPreCheckpoint(), CheckPoint::ThisTimeLineID, CheckPoint::time, TransamVariables, TruncateSUBTRANS(), XLogCtlData::unloggedLSN, ControlFileData::unloggedLSN, update_checkpoint_display(), UpdateCheckPointDistanceEstimate(), UpdateControlFile(), WakeupWalSummarizer(), wal_level, CheckPoint::wal_level, wal_segment_size, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLByteToSeg, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_REDO, XLOG_CHECKPOINT_SHUTDOWN, XLogBeginInsert(), XLogBytePosToRecPtr(), XLogCtl, XLogFlush(), XLogInsert(), XLogRegisterData(), XLogSegmentOffset, and XLogStandbyInfoActive.

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

◆ CreateRestartPoint()

bool CreateRestartPoint ( int  flags)

Definition at line 7779 of file xlog.c.

7780{
7781 XLogRecPtr lastCheckPointRecPtr;
7782 XLogRecPtr lastCheckPointEndPtr;
7783 CheckPoint lastCheckPoint;
7784 XLogRecPtr PriorRedoPtr;
7785 XLogRecPtr receivePtr;
7786 XLogRecPtr replayPtr;
7787 TimeLineID replayTLI;
7788 XLogRecPtr endptr;
7789 XLogSegNo _logSegNo;
7790 TimestampTz xtime;
7791
7792 /* Concurrent checkpoint/restartpoint cannot happen */
7794
7795 /* Get a local copy of the last safe checkpoint record. */
7797 lastCheckPointRecPtr = XLogCtl->lastCheckPointRecPtr;
7798 lastCheckPointEndPtr = XLogCtl->lastCheckPointEndPtr;
7799 lastCheckPoint = XLogCtl->lastCheckPoint;
7801
7802 /*
7803 * Check that we're still in recovery mode. It's ok if we exit recovery
7804 * mode after this check, the restart point is valid anyway.
7805 */
7806 if (!RecoveryInProgress())
7807 {
7809 (errmsg_internal("skipping restartpoint, recovery has already ended")));
7810 return false;
7811 }
7812
7813 /*
7814 * If the last checkpoint record we've replayed is already our last
7815 * restartpoint, we can't perform a new restart point. We still update
7816 * minRecoveryPoint in that case, so that if this is a shutdown restart
7817 * point, we won't start up earlier than before. That's not strictly
7818 * necessary, but when hot standby is enabled, it would be rather weird if
7819 * the database opened up for read-only connections at a point-in-time
7820 * before the last shutdown. Such time travel is still possible in case of
7821 * immediate shutdown, though.
7822 *
7823 * We don't explicitly advance minRecoveryPoint when we do create a
7824 * restartpoint. It's assumed that flushing the buffers will do that as a
7825 * side-effect.
7826 */
7827 if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||
7828 lastCheckPoint.redo <= ControlFile->checkPointCopy.redo)
7829 {
7831 (errmsg_internal("skipping restartpoint, already performed at %X/%X",
7832 LSN_FORMAT_ARGS(lastCheckPoint.redo))));
7833
7835 if (flags & CHECKPOINT_IS_SHUTDOWN)
7836 {
7837 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7840 LWLockRelease(ControlFileLock);
7841 }
7842 return false;
7843 }
7844
7845 /*
7846 * Update the shared RedoRecPtr so that the startup process can calculate
7847 * the number of segments replayed since last restartpoint, and request a
7848 * restartpoint if it exceeds CheckPointSegments.
7849 *
7850 * Like in CreateCheckPoint(), hold off insertions to update it, although
7851 * during recovery this is just pro forma, because no WAL insertions are
7852 * happening.
7853 */
7855 RedoRecPtr = XLogCtl->Insert.RedoRecPtr = lastCheckPoint.redo;
7857
7858 /* Also update the info_lck-protected copy */
7860 XLogCtl->RedoRecPtr = lastCheckPoint.redo;
7862
7863 /*
7864 * Prepare to accumulate statistics.
7865 *
7866 * Note: because it is possible for log_checkpoints to change while a
7867 * checkpoint proceeds, we always accumulate stats, even if
7868 * log_checkpoints is currently off.
7869 */
7872
7873 if (log_checkpoints)
7874 LogCheckpointStart(flags, true);
7875
7876 /* Update the process title */
7877 update_checkpoint_display(flags, true, false);
7878
7879 CheckPointGuts(lastCheckPoint.redo, flags);
7880
7881 /*
7882 * This location needs to be after CheckPointGuts() to ensure that some
7883 * work has already happened during this checkpoint.
7884 */
7885 INJECTION_POINT("create-restart-point");
7886
7887 /*
7888 * Remember the prior checkpoint's redo ptr for
7889 * UpdateCheckPointDistanceEstimate()
7890 */
7891 PriorRedoPtr = ControlFile->checkPointCopy.redo;
7892
7893 /*
7894 * Update pg_control, using current time. Check that it still shows an
7895 * older checkpoint, else do nothing; this is a quick hack to make sure
7896 * nothing really bad happens if somehow we get here after the
7897 * end-of-recovery checkpoint.
7898 */
7899 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7900 if (ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
7901 {
7902 /*
7903 * Update the checkpoint information. We do this even if the cluster
7904 * does not show DB_IN_ARCHIVE_RECOVERY to match with the set of WAL
7905 * segments recycled below.
7906 */
7907 ControlFile->checkPoint = lastCheckPointRecPtr;
7908 ControlFile->checkPointCopy = lastCheckPoint;
7909
7910 /*
7911 * Ensure minRecoveryPoint is past the checkpoint record and update it
7912 * if the control file still shows DB_IN_ARCHIVE_RECOVERY. Normally,
7913 * this will have happened already while writing out dirty buffers,
7914 * but not necessarily - e.g. because no buffers were dirtied. We do
7915 * this because a backup performed in recovery uses minRecoveryPoint
7916 * to determine which WAL files must be included in the backup, and
7917 * the file (or files) containing the checkpoint record must be
7918 * included, at a minimum. Note that for an ordinary restart of
7919 * recovery there's no value in having the minimum recovery point any
7920 * earlier than this anyway, because redo will begin just after the
7921 * checkpoint record.
7922 */
7924 {
7925 if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
7926 {
7927 ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
7929
7930 /* update local copy */
7933 }
7934 if (flags & CHECKPOINT_IS_SHUTDOWN)
7936 }
7938 }
7939 LWLockRelease(ControlFileLock);
7940
7941 /*
7942 * Update the average distance between checkpoints/restartpoints if the
7943 * prior checkpoint exists.
7944 */
7945 if (PriorRedoPtr != InvalidXLogRecPtr)
7947
7948 /*
7949 * Delete old log files, those no longer needed for last restartpoint to
7950 * prevent the disk holding the xlog from growing full.
7951 */
7953
7954 /*
7955 * Retreat _logSegNo using the current end of xlog replayed or received,
7956 * whichever is later.
7957 */
7958 receivePtr = GetWalRcvFlushRecPtr(NULL, NULL);
7959 replayPtr = GetXLogReplayRecPtr(&replayTLI);
7960 endptr = (receivePtr < replayPtr) ? replayPtr : receivePtr;
7961 KeepLogSeg(endptr, &_logSegNo);
7963 _logSegNo, InvalidOid,
7965 {
7966 /*
7967 * Some slots have been invalidated; recalculate the old-segment
7968 * horizon, starting again from RedoRecPtr.
7969 */
7971 KeepLogSeg(endptr, &_logSegNo);
7972 }
7973 _logSegNo--;
7974
7975 /*
7976 * Try to recycle segments on a useful timeline. If we've been promoted
7977 * since the beginning of this restartpoint, use the new timeline chosen
7978 * at end of recovery. If we're still in recovery, use the timeline we're
7979 * currently replaying.
7980 *
7981 * There is no guarantee that the WAL segments will be useful on the
7982 * current timeline; if recovery proceeds to a new timeline right after
7983 * this, the pre-allocated WAL segments on this timeline will not be used,
7984 * and will go wasted until recycled on the next restartpoint. We'll live
7985 * with that.
7986 */
7987 if (!RecoveryInProgress())
7988 replayTLI = XLogCtl->InsertTimeLineID;
7989
7990 RemoveOldXlogFiles(_logSegNo, RedoRecPtr, endptr, replayTLI);
7991
7992 /*
7993 * Make more log segments if needed. (Do this after recycling old log
7994 * segments, since that may supply some of the needed files.)
7995 */
7996 PreallocXlogFiles(endptr, replayTLI);
7997
7998 /*
7999 * Truncate pg_subtrans if possible. We can throw away all data before
8000 * the oldest XMIN of any running transaction. No future transaction will
8001 * attempt to reference any pg_subtrans entry older than that (see Asserts
8002 * in subtrans.c). When hot standby is disabled, though, we mustn't do
8003 * this because StartupSUBTRANS hasn't been called yet.
8004 */
8005 if (EnableHotStandby)
8007
8008 /* Real work is done; log and update stats. */
8009 LogCheckpointEnd(true);
8010
8011 /* Reset the process title */
8012 update_checkpoint_display(flags, true, true);
8013
8014 xtime = GetLatestXTime();
8016 (errmsg("recovery restart point at %X/%X",
8017 LSN_FORMAT_ARGS(lastCheckPoint.redo)),
8018 xtime ? errdetail("Last completed transaction was at log time %s.",
8019 timestamptz_to_str(xtime)) : 0));
8020
8021 /*
8022 * Finally, execute archive_cleanup_command, if any.
8023 */
8024 if (archiveCleanupCommand && strcmp(archiveCleanupCommand, "") != 0)
8026 "archive_cleanup_command",
8027 false,
8028 WAIT_EVENT_ARCHIVE_CLEANUP_COMMAND);
8029
8030 return true;
8031}
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1862
int64 TimestampTz
Definition: timestamp.h:39
int errdetail(const char *fmt,...)
Definition: elog.c:1204
#define LOG
Definition: elog.h:31
#define DEBUG2
Definition: elog.h:29
bool IsUnderPostmaster
Definition: globals.c:121
#define INJECTION_POINT(name)
@ B_CHECKPOINTER
Definition: miscadmin.h:363
BackendType MyBackendType
Definition: miscinit.c:64
@ DB_IN_ARCHIVE_RECOVERY
Definition: pg_control.h:96
@ DB_SHUTDOWNED_IN_RECOVERY
Definition: pg_control.h:93
CheckPoint lastCheckPoint
Definition: xlog.c:556
XLogRecPtr lastCheckPointRecPtr
Definition: xlog.c:554
XLogRecPtr lastCheckPointEndPtr
Definition: xlog.c:555
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool EnableHotStandby
Definition: xlog.c:121
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
Definition: xlog.c:2843
static XLogRecPtr LocalMinRecoveryPoint
Definition: xlog.c:657
static TimeLineID LocalMinRecoveryPointTLI
Definition: xlog.c:658
void ExecuteRecoveryCommand(const char *command, const char *commandName, bool failOnSignal, uint32 wait_event_info)
Definition: xlogarchive.c:295
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint32 TimeLineID
Definition: xlogdefs.h:59
char * archiveCleanupCommand
Definition: xlogrecovery.c:85
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
TimestampTz GetLatestXTime(void)

References archiveCleanupCommand, Assert(), B_CHECKPOINTER, ControlFileData::checkPoint, CHECKPOINT_IS_SHUTDOWN, ControlFileData::checkPointCopy, CheckPointGuts(), CheckpointStats, CheckpointStatsData::ckpt_start_t, ControlFile, DB_IN_ARCHIVE_RECOVERY, DB_SHUTDOWNED_IN_RECOVERY, DEBUG2, EnableHotStandby, ereport, errdetail(), errmsg(), errmsg_internal(), ExecuteRecoveryCommand(), GetCurrentTimestamp(), GetLatestXTime(), GetOldestTransactionIdConsideredRunning(), GetWalRcvFlushRecPtr(), GetXLogReplayRecPtr(), XLogCtlData::info_lck, INJECTION_POINT, XLogCtlData::Insert, XLogCtlData::InsertTimeLineID, InvalidateObsoleteReplicationSlots(), InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, IsUnderPostmaster, KeepLogSeg(), XLogCtlData::lastCheckPoint, XLogCtlData::lastCheckPointEndPtr, XLogCtlData::lastCheckPointRecPtr, LocalMinRecoveryPoint, LocalMinRecoveryPointTLI, LOG, log_checkpoints, LogCheckpointEnd(), LogCheckpointStart(), LSN_FORMAT_ARGS, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemSet, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, MyBackendType, PreallocXlogFiles(), RecoveryInProgress(), CheckPoint::redo, RedoRecPtr, XLogCtlInsert::RedoRecPtr, XLogCtlData::RedoRecPtr, RemoveOldXlogFiles(), RS_INVAL_IDLE_TIMEOUT, RS_INVAL_WAL_REMOVED, SpinLockAcquire, SpinLockRelease, ControlFileData::state, CheckPoint::ThisTimeLineID, timestamptz_to_str(), TruncateSUBTRANS(), update_checkpoint_display(), UpdateCheckPointDistanceEstimate(), UpdateControlFile(), UpdateMinRecoveryPoint(), wal_segment_size, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLByteToSeg, XLogCtl, and XLogRecPtrIsInvalid.

Referenced by CheckpointerMain(), and ShutdownXLOG().

◆ DataChecksumsEnabled()

◆ do_pg_abort_backup()

void do_pg_abort_backup ( int  code,
Datum  arg 
)

Definition at line 9571 of file xlog.c.

9572{
9573 bool during_backup_start = DatumGetBool(arg);
9574
9575 /* If called during backup start, there shouldn't be one already running */
9576 Assert(!during_backup_start || sessionBackupState == SESSION_BACKUP_NONE);
9577
9578 if (during_backup_start || sessionBackupState != SESSION_BACKUP_NONE)
9579 {
9583
9586
9587 if (!during_backup_start)
9589 errmsg("aborting backup due to backend exiting before pg_backup_stop was called"));
9590 }
9591}
#define WARNING
Definition: elog.h:36
void * arg
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
int runningBackups
Definition: xlog.c:433
static SessionBackupState sessionBackupState
Definition: xlog.c:386

References arg, Assert(), DatumGetBool(), ereport, errmsg(), XLogCtlData::Insert, XLogCtlInsert::runningBackups, SESSION_BACKUP_NONE, sessionBackupState, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WARNING, and XLogCtl.

Referenced by do_pg_backup_start(), perform_base_backup(), and register_persistent_abort_backup_handler().

◆ do_pg_backup_start()

void do_pg_backup_start ( const char *  backupidstr,
bool  fast,
List **  tablespaces,
BackupState state,
StringInfo  tblspcmapfile 
)

Definition at line 8969 of file xlog.c.

8971{
8973
8974 Assert(state != NULL);
8976
8977 /*
8978 * During recovery, we don't need to check WAL level. Because, if WAL
8979 * level is not sufficient, it's impossible to get here during recovery.
8980 */
8982 ereport(ERROR,
8983 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8984 errmsg("WAL level not sufficient for making an online backup"),
8985 errhint("\"wal_level\" must be set to \"replica\" or \"logical\" at server start.")));
8986
8987 if (strlen(backupidstr) > MAXPGPATH)
8988 ereport(ERROR,
8989 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8990 errmsg("backup label too long (max %d bytes)",
8991 MAXPGPATH)));
8992
8993 strlcpy(state->name, backupidstr, sizeof(state->name));
8994
8995 /*
8996 * Mark backup active in shared memory. We must do full-page WAL writes
8997 * during an on-line backup even if not doing so at other times, because
8998 * it's quite possible for the backup dump to obtain a "torn" (partially
8999 * written) copy of a database page if it reads the page concurrently with
9000 * our write to the same page. This can be fixed as long as the first
9001 * write to the page in the WAL sequence is a full-page write. Hence, we
9002 * increment runningBackups then force a CHECKPOINT, to ensure there are
9003 * no dirty pages in shared memory that might get dumped while the backup
9004 * is in progress without having a corresponding WAL record. (Once the
9005 * backup is complete, we need not force full-page writes anymore, since
9006 * we expect that any pages not modified during the backup interval must
9007 * have been correctly captured by the backup.)
9008 *
9009 * Note that forcing full-page writes has no effect during an online
9010 * backup from the standby.
9011 *
9012 * We must hold all the insertion locks to change the value of
9013 * runningBackups, to ensure adequate interlocking against
9014 * XLogInsertRecord().
9015 */
9019
9020 /*
9021 * Ensure we decrement runningBackups if we fail below. NB -- for this to
9022 * work correctly, it is critical that sessionBackupState is only updated
9023 * after this block is over.
9024 */
9026 {
9027 bool gotUniqueStartpoint = false;
9028 DIR *tblspcdir;
9029 struct dirent *de;
9030 tablespaceinfo *ti;
9031 int datadirpathlen;
9032
9033 /*
9034 * Force an XLOG file switch before the checkpoint, to ensure that the
9035 * WAL segment the checkpoint is written to doesn't contain pages with
9036 * old timeline IDs. That would otherwise happen if you called
9037 * pg_backup_start() right after restoring from a PITR archive: the
9038 * first WAL segment containing the startup checkpoint has pages in
9039 * the beginning with the old timeline ID. That can cause trouble at
9040 * recovery: we won't have a history file covering the old timeline if
9041 * pg_wal directory was not included in the base backup and the WAL
9042 * archive was cleared too before starting the backup.
9043 *
9044 * This also ensures that we have emitted a WAL page header that has
9045 * XLP_BKP_REMOVABLE off before we emit the checkpoint record.
9046 * Therefore, if a WAL archiver (such as pglesslog) is trying to
9047 * compress out removable backup blocks, it won't remove any that
9048 * occur after this point.
9049 *
9050 * During recovery, we skip forcing XLOG file switch, which means that
9051 * the backup taken during recovery is not available for the special
9052 * recovery case described above.
9053 */
9055 RequestXLogSwitch(false);
9056
9057 do
9058 {
9059 bool checkpointfpw;
9060
9061 /*
9062 * Force a CHECKPOINT. Aside from being necessary to prevent torn
9063 * page problems, this guarantees that two successive backup runs
9064 * will have different checkpoint positions and hence different
9065 * history file names, even if nothing happened in between.
9066 *
9067 * During recovery, establish a restartpoint if possible. We use
9068 * the last restartpoint as the backup starting checkpoint. This
9069 * means that two successive backup runs can have same checkpoint
9070 * positions.
9071 *
9072 * Since the fact that we are executing do_pg_backup_start()
9073 * during recovery means that checkpointer is running, we can use
9074 * RequestCheckpoint() to establish a restartpoint.
9075 *
9076 * We use CHECKPOINT_IMMEDIATE only if requested by user (via
9077 * passing fast = true). Otherwise this can take awhile.
9078 */
9080 (fast ? CHECKPOINT_IMMEDIATE : 0));
9081
9082 /*
9083 * Now we need to fetch the checkpoint record location, and also
9084 * its REDO pointer. The oldest point in WAL that would be needed
9085 * to restore starting from the checkpoint is precisely the REDO
9086 * pointer.
9087 */
9088 LWLockAcquire(ControlFileLock, LW_SHARED);
9089 state->checkpointloc = ControlFile->checkPoint;
9090 state->startpoint = ControlFile->checkPointCopy.redo;
9092 checkpointfpw = ControlFile->checkPointCopy.fullPageWrites;
9093 LWLockRelease(ControlFileLock);
9094
9096 {
9097 XLogRecPtr recptr;
9098
9099 /*
9100 * Check to see if all WAL replayed during online backup
9101 * (i.e., since last restartpoint used as backup starting
9102 * checkpoint) contain full-page writes.
9103 */
9105 recptr = XLogCtl->lastFpwDisableRecPtr;
9107
9108 if (!checkpointfpw || state->startpoint <= recptr)
9109 ereport(ERROR,
9110 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
9111 errmsg("WAL generated with \"full_page_writes=off\" was replayed "
9112 "since last restartpoint"),
9113 errhint("This means that the backup being taken on the standby "
9114 "is corrupt and should not be used. "
9115 "Enable \"full_page_writes\" and run CHECKPOINT on the primary, "
9116 "and then try an online backup again.")));
9117
9118 /*
9119 * During recovery, since we don't use the end-of-backup WAL
9120 * record and don't write the backup history file, the
9121 * starting WAL location doesn't need to be unique. This means
9122 * that two base backups started at the same time might use
9123 * the same checkpoint as starting locations.
9124 */
9125 gotUniqueStartpoint = true;
9126 }
9127
9128 /*
9129 * If two base backups are started at the same time (in WAL sender
9130 * processes), we need to make sure that they use different
9131 * checkpoints as starting locations, because we use the starting
9132 * WAL location as a unique identifier for the base backup in the
9133 * end-of-backup WAL record and when we write the backup history
9134 * file. Perhaps it would be better generate a separate unique ID
9135 * for each backup instead of forcing another checkpoint, but
9136 * taking a checkpoint right after another is not that expensive
9137 * either because only few buffers have been dirtied yet.
9138 */
9140 if (XLogCtl->Insert.lastBackupStart < state->startpoint)
9141 {
9142 XLogCtl->Insert.lastBackupStart = state->startpoint;
9143 gotUniqueStartpoint = true;
9144 }
9146 } while (!gotUniqueStartpoint);
9147
9148 /*
9149 * Construct tablespace_map file.
9150 */
9151 datadirpathlen = strlen(DataDir);
9152
9153 /* Collect information about all tablespaces */
9154 tblspcdir = AllocateDir(PG_TBLSPC_DIR);
9155 while ((de = ReadDir(tblspcdir, PG_TBLSPC_DIR)) != NULL)
9156 {
9157 char fullpath[MAXPGPATH + sizeof(PG_TBLSPC_DIR)];
9158 char linkpath[MAXPGPATH];
9159 char *relpath = NULL;
9160 char *s;
9161 PGFileType de_type;
9162 char *badp;
9163 Oid tsoid;
9164
9165 /*
9166 * Try to parse the directory name as an unsigned integer.
9167 *
9168 * Tablespace directories should be positive integers that can be
9169 * represented in 32 bits, with no leading zeroes or trailing
9170 * garbage. If we come across a name that doesn't meet those
9171 * criteria, skip it.
9172 */
9173 if (de->d_name[0] < '1' || de->d_name[1] > '9')
9174 continue;
9175 errno = 0;
9176 tsoid = strtoul(de->d_name, &badp, 10);
9177 if (*badp != '\0' || errno == EINVAL || errno == ERANGE)
9178 continue;
9179
9180 snprintf(fullpath, sizeof(fullpath), "%s/%s", PG_TBLSPC_DIR, de->d_name);
9181
9182 de_type = get_dirent_type(fullpath, de, false, ERROR);
9183
9184 if (de_type == PGFILETYPE_LNK)
9185 {
9186 StringInfoData escapedpath;
9187 int rllen;
9188
9189 rllen = readlink(fullpath, linkpath, sizeof(linkpath));
9190 if (rllen < 0)
9191 {
9193 (errmsg("could not read symbolic link \"%s\": %m",
9194 fullpath)));
9195 continue;
9196 }
9197 else if (rllen >= sizeof(linkpath))
9198 {
9200 (errmsg("symbolic link \"%s\" target is too long",
9201 fullpath)));
9202 continue;
9203 }
9204 linkpath[rllen] = '\0';
9205
9206 /*
9207 * Relpath holds the relative path of the tablespace directory
9208 * when it's located within PGDATA, or NULL if it's located
9209 * elsewhere.
9210 */
9211 if (rllen > datadirpathlen &&
9212 strncmp(linkpath, DataDir, datadirpathlen) == 0 &&
9213 IS_DIR_SEP(linkpath[datadirpathlen]))
9214 relpath = pstrdup(linkpath + datadirpathlen + 1);
9215
9216 /*
9217 * Add a backslash-escaped version of the link path to the
9218 * tablespace map file.
9219 */
9220 initStringInfo(&escapedpath);
9221 for (s = linkpath; *s; s++)
9222 {
9223 if (*s == '\n' || *s == '\r' || *s == '\\')
9224 appendStringInfoChar(&escapedpath, '\\');
9225 appendStringInfoChar(&escapedpath, *s);
9226 }
9227 appendStringInfo(tblspcmapfile, "%s %s\n",
9228 de->d_name, escapedpath.data);
9229 pfree(escapedpath.data);
9230 }
9231 else if (de_type == PGFILETYPE_DIR)
9232 {
9233 /*
9234 * It's possible to use allow_in_place_tablespaces to create
9235 * directories directly under pg_tblspc, for testing purposes
9236 * only.
9237 *
9238 * In this case, we store a relative path rather than an
9239 * absolute path into the tablespaceinfo.
9240 */
9241 snprintf(linkpath, sizeof(linkpath), "%s/%s",
9242 PG_TBLSPC_DIR, de->d_name);
9243 relpath = pstrdup(linkpath);
9244 }
9245 else
9246 {
9247 /* Skip any other file type that appears here. */
9248 continue;
9249 }
9250
9251 ti = palloc(sizeof(tablespaceinfo));
9252 ti->oid = tsoid;
9253 ti->path = pstrdup(linkpath);
9254 ti->rpath = relpath;
9255 ti->size = -1;
9256
9257 if (tablespaces)
9258 *tablespaces = lappend(*tablespaces, ti);
9259 }
9260 FreeDir(tblspcdir);
9261
9262 state->starttime = (pg_time_t) time(NULL);
9263 }
9265
9266 state->started_in_recovery = backup_started_in_recovery;
9267
9268 /*
9269 * Mark that the start phase has correctly finished for the backup.
9270 */
9272}
static bool backup_started_in_recovery
Definition: basebackup.c:123
void RequestCheckpoint(int flags)
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int FreeDir(DIR *dir)
Definition: fd.c:3025
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2907
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2973
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition: file_utils.c:547
PGFileType
Definition: file_utils.h:19
@ PGFILETYPE_LNK
Definition: file_utils.h:24
@ PGFILETYPE_DIR
Definition: file_utils.h:23
char * DataDir
Definition: globals.c:72
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
List * lappend(List *list, void *datum)
Definition: list.c:339
char * pstrdup(const char *in)
Definition: mcxt.c:2322
#define MAXPGPATH
#define snprintf
Definition: port.h:239
#define IS_DIR_SEP(ch)
Definition: port.h:103
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
unsigned int Oid
Definition: postgres_ext.h:30
#define relpath(rlocator, forknum)
Definition: relpath.h:150
#define PG_TBLSPC_DIR
Definition: relpath.h:41
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
Definition: dirent.c:26
XLogRecPtr lastFpwDisableRecPtr
Definition: xlog.c:562
XLogRecPtr lastBackupStart
Definition: xlog.c:434
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
Definition: regguts.h:323
char * rpath
Definition: basebackup.h:32
#define readlink(path, buf, size)
Definition: win32_port.h:226
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:8248
void do_pg_abort_backup(int code, Datum arg)
Definition: xlog.c:9571
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:289
#define CHECKPOINT_WAIT
Definition: xlog.h:145
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:141
#define XLogIsNeeded()
Definition: xlog.h:109

References AllocateDir(), appendStringInfo(), appendStringInfoChar(), Assert(), backup_started_in_recovery, ControlFileData::checkPoint, CHECKPOINT_FORCE, CHECKPOINT_IMMEDIATE, CHECKPOINT_WAIT, ControlFileData::checkPointCopy, ControlFile, dirent::d_name, StringInfoData::data, DataDir, DatumGetBool(), do_pg_abort_backup(), ereport, errcode(), errhint(), errmsg(), ERROR, FreeDir(), CheckPoint::fullPageWrites, get_dirent_type(), XLogCtlData::info_lck, initStringInfo(), XLogCtlData::Insert, IS_DIR_SEP, lappend(), XLogCtlInsert::lastBackupStart, XLogCtlData::lastFpwDisableRecPtr, LW_SHARED, LWLockAcquire(), LWLockRelease(), MAXPGPATH, tablespaceinfo::oid, palloc(), tablespaceinfo::path, pfree(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, PG_TBLSPC_DIR, PGFILETYPE_DIR, PGFILETYPE_LNK, pstrdup(), ReadDir(), readlink, RecoveryInProgress(), CheckPoint::redo, relpath, RequestCheckpoint(), RequestXLogSwitch(), tablespaceinfo::rpath, XLogCtlInsert::runningBackups, SESSION_BACKUP_RUNNING, sessionBackupState, tablespaceinfo::size, snprintf, SpinLockAcquire, SpinLockRelease, strlcpy(), CheckPoint::ThisTimeLineID, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WARNING, XLogCtl, and XLogIsNeeded.

Referenced by perform_base_backup(), and pg_backup_start().

◆ do_pg_backup_stop()

void do_pg_backup_stop ( BackupState state,
bool  waitforarchive 
)

Definition at line 9297 of file xlog.c.

9298{
9299 bool backup_stopped_in_recovery = false;
9300 char histfilepath[MAXPGPATH];
9301 char lastxlogfilename[MAXFNAMELEN];
9302 char histfilename[MAXFNAMELEN];
9303 XLogSegNo _logSegNo;
9304 FILE *fp;
9305 int seconds_before_warning;
9306 int waits = 0;
9307 bool reported_waiting = false;
9308
9309 Assert(state != NULL);
9310
9311 backup_stopped_in_recovery = RecoveryInProgress();
9312
9313 /*
9314 * During recovery, we don't need to check WAL level. Because, if WAL
9315 * level is not sufficient, it's impossible to get here during recovery.
9316 */
9317 if (!backup_stopped_in_recovery && !XLogIsNeeded())
9318 ereport(ERROR,
9319 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
9320 errmsg("WAL level not sufficient for making an online backup"),
9321 errhint("\"wal_level\" must be set to \"replica\" or \"logical\" at server start.")));
9322
9323 /*
9324 * OK to update backup counter and session-level lock.
9325 *
9326 * Note that CHECK_FOR_INTERRUPTS() must not occur while updating them,
9327 * otherwise they can be updated inconsistently, which might cause
9328 * do_pg_abort_backup() to fail.
9329 */
9331
9332 /*
9333 * It is expected that each do_pg_backup_start() call is matched by
9334 * exactly one do_pg_backup_stop() call.
9335 */
9338
9339 /*
9340 * Clean up session-level lock.
9341 *
9342 * You might think that WALInsertLockRelease() can be called before
9343 * cleaning up session-level lock because session-level lock doesn't need
9344 * to be protected with WAL insertion lock. But since
9345 * CHECK_FOR_INTERRUPTS() can occur in it, session-level lock must be
9346 * cleaned up before it.
9347 */
9349
9351
9352 /*
9353 * If we are taking an online backup from the standby, we confirm that the
9354 * standby has not been promoted during the backup.
9355 */
9356 if (state->started_in_recovery && !backup_stopped_in_recovery)
9357 ereport(ERROR,
9358 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
9359 errmsg("the standby was promoted during online backup"),
9360 errhint("This means that the backup being taken is corrupt "
9361 "and should not be used. "
9362 "Try taking another online backup.")));
9363
9364 /*
9365 * During recovery, we don't write an end-of-backup record. We assume that
9366 * pg_control was backed up last and its minimum recovery point can be
9367 * available as the backup end location. Since we don't have an
9368 * end-of-backup record, we use the pg_control value to check whether
9369 * we've reached the end of backup when starting recovery from this
9370 * backup. We have no way of checking if pg_control wasn't backed up last
9371 * however.
9372 *
9373 * We don't force a switch to new WAL file but it is still possible to
9374 * wait for all the required files to be archived if waitforarchive is
9375 * true. This is okay if we use the backup to start a standby and fetch
9376 * the missing WAL using streaming replication. But in the case of an
9377 * archive recovery, a user should set waitforarchive to true and wait for
9378 * them to be archived to ensure that all the required files are
9379 * available.
9380 *
9381 * We return the current minimum recovery point as the backup end
9382 * location. Note that it can be greater than the exact backup end
9383 * location if the minimum recovery point is updated after the backup of
9384 * pg_control. This is harmless for current uses.
9385 *
9386 * XXX currently a backup history file is for informational and debug
9387 * purposes only. It's not essential for an online backup. Furthermore,
9388 * even if it's created, it will not be archived during recovery because
9389 * an archiver is not invoked. So it doesn't seem worthwhile to write a
9390 * backup history file during recovery.
9391 */
9392 if (backup_stopped_in_recovery)
9393 {
9394 XLogRecPtr recptr;
9395
9396 /*
9397 * Check to see if all WAL replayed during online backup contain
9398 * full-page writes.
9399 */
9401 recptr = XLogCtl->lastFpwDisableRecPtr;
9403
9404 if (state->startpoint <= recptr)
9405 ereport(ERROR,
9406 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
9407 errmsg("WAL generated with \"full_page_writes=off\" was replayed "
9408 "during online backup"),
9409 errhint("This means that the backup being taken on the standby "
9410 "is corrupt and should not be used. "
9411 "Enable \"full_page_writes\" and run CHECKPOINT on the primary, "
9412 "and then try an online backup again.")));
9413
9414
9415 LWLockAcquire(ControlFileLock, LW_SHARED);
9416 state->stoppoint = ControlFile->minRecoveryPoint;
9418 LWLockRelease(ControlFileLock);
9419 }
9420 else
9421 {
9422 char *history_file;
9423
9424 /*
9425 * Write the backup-end xlog record
9426 */
9428 XLogRegisterData(&state->startpoint,
9429 sizeof(state->startpoint));
9430 state->stoppoint = XLogInsert(RM_XLOG_ID, XLOG_BACKUP_END);
9431
9432 /*
9433 * Given that we're not in recovery, InsertTimeLineID is set and can't
9434 * change, so we can read it without a lock.
9435 */
9436 state->stoptli = XLogCtl->InsertTimeLineID;
9437
9438 /*
9439 * Force a switch to a new xlog segment file, so that the backup is
9440 * valid as soon as archiver moves out the current segment file.
9441 */
9442 RequestXLogSwitch(false);
9443
9444 state->stoptime = (pg_time_t) time(NULL);
9445
9446 /*
9447 * Write the backup history file
9448 */
9449 XLByteToSeg(state->startpoint, _logSegNo, wal_segment_size);
9450 BackupHistoryFilePath(histfilepath, state->stoptli, _logSegNo,
9451 state->startpoint, wal_segment_size);
9452 fp = AllocateFile(histfilepath, "w");
9453 if (!fp)
9454 ereport(ERROR,
9456 errmsg("could not create file \"%s\": %m",
9457 histfilepath)));
9458
9459 /* Build and save the contents of the backup history file */
9460 history_file = build_backup_content(state, true);
9461 fprintf(fp, "%s", history_file);
9462 pfree(history_file);
9463
9464 if (fflush(fp) || ferror(fp) || FreeFile(fp))
9465 ereport(ERROR,
9467 errmsg("could not write file \"%s\": %m",
9468 histfilepath)));
9469
9470 /*
9471 * Clean out any no-longer-needed history files. As a side effect,
9472 * this will post a .ready file for the newly created history file,
9473 * notifying the archiver that history file may be archived
9474 * immediately.
9475 */
9477 }
9478
9479 /*
9480 * If archiving is enabled, wait for all the required WAL files to be
9481 * archived before returning. If archiving isn't enabled, the required WAL
9482 * needs to be transported via streaming replication (hopefully with
9483 * wal_keep_size set high enough), or some more exotic mechanism like
9484 * polling and copying files from pg_wal with script. We have no knowledge
9485 * of those mechanisms, so it's up to the user to ensure that he gets all
9486 * the required WAL.
9487 *
9488 * We wait until both the last WAL file filled during backup and the
9489 * history file have been archived, and assume that the alphabetic sorting
9490 * property of the WAL files ensures any earlier WAL files are safely
9491 * archived as well.
9492 *
9493 * We wait forever, since archive_command is supposed to work and we
9494 * assume the admin wanted his backup to work completely. If you don't
9495 * wish to wait, then either waitforarchive should be passed in as false,
9496 * or you can set statement_timeout. Also, some notices are issued to
9497 * clue in anyone who might be doing this interactively.
9498 */
9499
9500 if (waitforarchive &&
9501 ((!backup_stopped_in_recovery && XLogArchivingActive()) ||
9502 (backup_stopped_in_recovery && XLogArchivingAlways())))
9503 {
9504 XLByteToPrevSeg(state->stoppoint, _logSegNo, wal_segment_size);
9505 XLogFileName(lastxlogfilename, state->stoptli, _logSegNo,
9507
9508 XLByteToSeg(state->startpoint, _logSegNo, wal_segment_size);
9509 BackupHistoryFileName(histfilename, state->stoptli, _logSegNo,
9510 state->startpoint, wal_segment_size);
9511
9512 seconds_before_warning = 60;
9513 waits = 0;
9514
9515 while (XLogArchiveIsBusy(lastxlogfilename) ||
9516 XLogArchiveIsBusy(histfilename))
9517 {
9519
9520 if (!reported_waiting && waits > 5)
9521 {
9523 (errmsg("base backup done, waiting for required WAL segments to be archived")));
9524 reported_waiting = true;
9525 }
9526
9527 (void) WaitLatch(MyLatch,
9529 1000L,
9530 WAIT_EVENT_BACKUP_WAIT_WAL_ARCHIVE);
9532
9533 if (++waits >= seconds_before_warning)
9534 {
9535 seconds_before_warning *= 2; /* This wraps in >10 years... */
9537 (errmsg("still waiting for all required WAL segments to be archived (%d seconds elapsed)",
9538 waits),
9539 errhint("Check that your \"archive_command\" is executing properly. "
9540 "You can safely cancel this backup, "
9541 "but the database backup will not be usable without all the WAL segments.")));
9542 }
9543 }
9544
9546 (errmsg("all required WAL segments have been archived")));
9547 }
9548 else if (waitforarchive)
9550 (errmsg("WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup")));
9551}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define NOTICE
Definition: elog.h:35
int FreeFile(FILE *file)
Definition: fd.c:2843
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2644
struct Latch * MyLatch
Definition: globals.c:64
void ResetLatch(Latch *latch)
Definition: latch.c:372
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:172
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define XLOG_BACKUP_END
Definition: pg_control.h:73
#define WL_TIMEOUT
Definition: waiteventset.h:37
#define WL_EXIT_ON_PM_DEATH
Definition: waiteventset.h:39
#define WL_LATCH_SET
Definition: waiteventset.h:34
static void CleanupBackupHistory(void)
Definition: xlog.c:4300
#define XLogArchivingActive()
Definition: xlog.h:99
#define XLogArchivingAlways()
Definition: xlog.h:102
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
static void BackupHistoryFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, XLogRecPtr startpoint, int wal_segsz_bytes)
static void BackupHistoryFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, XLogRecPtr startpoint, int wal_segsz_bytes)
bool XLogArchiveIsBusy(const char *xlog)
Definition: xlogarchive.c:619
char * build_backup_content(BackupState *state, bool ishistoryfile)
Definition: xlogbackup.c:29

References AllocateFile(), Assert(), BackupHistoryFileName(), BackupHistoryFilePath(), build_backup_content(), CHECK_FOR_INTERRUPTS, CleanupBackupHistory(), ControlFile, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, fprintf, FreeFile(), XLogCtlData::info_lck, XLogCtlData::Insert, XLogCtlData::InsertTimeLineID, XLogCtlData::lastFpwDisableRecPtr, LW_SHARED, LWLockAcquire(), LWLockRelease(), MAXFNAMELEN, MAXPGPATH, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, MyLatch, NOTICE, pfree(), RecoveryInProgress(), RequestXLogSwitch(), ResetLatch(), XLogCtlInsert::runningBackups, SESSION_BACKUP_NONE, sessionBackupState, SpinLockAcquire, SpinLockRelease, WaitLatch(), wal_segment_size, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WARNING, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, XLByteToPrevSeg, XLByteToSeg, XLOG_BACKUP_END, XLogArchiveIsBusy(), XLogArchivingActive, XLogArchivingAlways, XLogBeginInsert(), XLogCtl, XLogFileName(), XLogInsert(), XLogIsNeeded, and XLogRegisterData().

Referenced by perform_base_backup(), and pg_backup_stop().

◆ get_backup_status()

SessionBackupState get_backup_status ( void  )

Definition at line 9278 of file xlog.c.

9279{
9280 return sessionBackupState;
9281}

References sessionBackupState.

Referenced by pg_backup_start(), pg_backup_stop(), and SendBaseBackup().

◆ GetActiveWalLevelOnStandby()

WalLevel GetActiveWalLevelOnStandby ( void  )

Definition at line 5040 of file xlog.c.

5041{
5042 return ControlFile->wal_level;
5043}

References ControlFile, and ControlFileData::wal_level.

Referenced by CheckLogicalDecodingRequirements().

◆ GetDefaultCharSignedness()

bool GetDefaultCharSignedness ( void  )

Definition at line 4768 of file xlog.c.

4769{
4771}
bool default_char_signedness
Definition: pg_control.h:228

References ControlFile, and ControlFileData::default_char_signedness.

Referenced by CMPTRGM_CHOOSE().

◆ GetFakeLSNForUnloggedRel()

XLogRecPtr GetFakeLSNForUnloggedRel ( void  )

Definition at line 4783 of file xlog.c.

4784{
4786}
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:522

References pg_atomic_fetch_add_u64(), XLogCtlData::unloggedLSN, and XLogCtl.

Referenced by gistGetFakeLSN().

◆ GetFlushRecPtr()

XLogRecPtr GetFlushRecPtr ( TimeLineID insertTLI)

Definition at line 6687 of file xlog.c.

6688{
6690
6692
6693 /*
6694 * If we're writing and flushing WAL, the time line can't be changing, so
6695 * no lock is required.
6696 */
6697 if (insertTLI)
6698 *insertTLI = XLogCtl->InsertTimeLineID;
6699
6700 return LogwrtResult.Flush;
6701}
RecoveryState SharedRecoveryState
Definition: xlog.c:527
XLogRecPtr Flush
Definition: xlog.c:323
#define RefreshXLogWriteResult(_target)
Definition: xlog.c:631
static XLogwrtResult LogwrtResult
Definition: xlog.c:623

References Assert(), XLogwrtResult::Flush, XLogCtlData::InsertTimeLineID, LogwrtResult, RECOVERY_STATE_DONE, RefreshXLogWriteResult, XLogCtlData::SharedRecoveryState, and XLogCtl.

Referenced by binary_upgrade_logical_slot_has_caught_up(), get_flush_position(), GetCurrentLSN(), GetLatestLSN(), IdentifySystem(), pg_current_wal_flush_lsn(), pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), read_local_xlog_page_guts(), StartReplication(), WalSndWaitForWal(), XLogSendLogical(), and XLogSendPhysical().

◆ GetFullPageWriteInfo()

void GetFullPageWriteInfo ( XLogRecPtr RedoRecPtr_p,
bool *  doPageWrites_p 
)

Definition at line 6655 of file xlog.c.

6656{
6657 *RedoRecPtr_p = RedoRecPtr;
6658 *doPageWrites_p = doPageWrites;
6659}
static bool doPageWrites
Definition: xlog.c:286

References doPageWrites, and RedoRecPtr.

Referenced by XLogCheckBufferNeedsBackup(), and XLogInsert().

◆ GetInsertRecPtr()

XLogRecPtr GetInsertRecPtr ( void  )

Definition at line 6670 of file xlog.c.

6671{
6672 XLogRecPtr recptr;
6673
6675 recptr = XLogCtl->LogwrtRqst.Write;
6677
6678 return recptr;
6679}
XLogwrtRqst LogwrtRqst
Definition: xlog.c:450
XLogRecPtr Write
Definition: xlog.c:316

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

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

◆ GetLastImportantRecPtr()

XLogRecPtr GetLastImportantRecPtr ( void  )

Definition at line 6744 of file xlog.c.

6745{
6747 int i;
6748
6749 for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
6750 {
6751 XLogRecPtr last_important;
6752
6753 /*
6754 * Need to take a lock to prevent torn reads of the LSN, which are
6755 * possible on some of the supported platforms. WAL insert locks only
6756 * support exclusive mode, so we have to use that.
6757 */
6759 last_important = WALInsertLocks[i].l.lastImportantAt;
6760 LWLockRelease(&WALInsertLocks[i].l.lock);
6761
6762 if (res < last_important)
6763 res = last_important;
6764 }
6765
6766 return res;
6767}
int i
Definition: isn.c:77
XLogRecPtr lastImportantAt
Definition: xlog.c:366
WALInsertLock l
Definition: xlog.c:378
static WALInsertLockPadded * WALInsertLocks
Definition: xlog.c:580
#define NUM_XLOGINSERT_LOCKS
Definition: xlog.c:150

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

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

◆ GetMockAuthenticationNonce()

char * GetMockAuthenticationNonce ( void  )

Definition at line 4744 of file xlog.c.

4745{
4746 Assert(ControlFile != NULL);
4748}
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:235

References Assert(), ControlFile, and ControlFileData::mock_authentication_nonce.

Referenced by scram_mock_salt().

◆ GetRecoveryState()

RecoveryState GetRecoveryState ( void  )

Definition at line 6558 of file xlog.c.

6559{
6560 RecoveryState retval;
6561
6563 retval = XLogCtl->SharedRecoveryState;
6565
6566 return retval;
6567}

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

Referenced by XLogArchiveCheckDone().

◆ GetRedoRecPtr()

XLogRecPtr GetRedoRecPtr ( void  )

Definition at line 6625 of file xlog.c.

6626{
6627 XLogRecPtr ptr;
6628
6629 /*
6630 * The possibly not up-to-date copy in XlogCtl is enough. Even if we
6631 * grabbed a WAL insertion lock to read the authoritative value in
6632 * Insert->RedoRecPtr, someone might update it just after we've released
6633 * the lock.
6634 */
6636 ptr = XLogCtl->RedoRecPtr;
6638
6639 if (RedoRecPtr < ptr)
6640 RedoRecPtr = ptr;
6641
6642 return RedoRecPtr;
6643}

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

Referenced by CheckPointLogicalRewriteHeap(), CheckPointSnapBuild(), MaybeRemoveOldWalSummaries(), nextval_internal(), ReplicationSlotReserveWal(), smgr_bulk_finish(), smgr_bulk_start_smgr(), XLogPageRead(), XLogSaveBufferForHint(), and XLogWrite().

◆ GetSystemIdentifier()

◆ GetWALAvailability()

WALAvailability GetWALAvailability ( XLogRecPtr  targetLSN)

Definition at line 8057 of file xlog.c.

8058{
8059 XLogRecPtr currpos; /* current write LSN */
8060 XLogSegNo currSeg; /* segid of currpos */
8061 XLogSegNo targetSeg; /* segid of targetLSN */
8062 XLogSegNo oldestSeg; /* actual oldest segid */
8063 XLogSegNo oldestSegMaxWalSize; /* oldest segid kept by max_wal_size */
8064 XLogSegNo oldestSlotSeg; /* oldest segid kept by slot */
8065 uint64 keepSegs;
8066
8067 /*
8068 * slot does not reserve WAL. Either deactivated, or has never been active
8069 */
8070 if (XLogRecPtrIsInvalid(targetLSN))
8071 return WALAVAIL_INVALID_LSN;
8072
8073 /*
8074 * Calculate the oldest segment currently reserved by all slots,
8075 * considering wal_keep_size and max_slot_wal_keep_size. Initialize
8076 * oldestSlotSeg to the current segment.
8077 */
8078 currpos = GetXLogWriteRecPtr();
8079 XLByteToSeg(currpos, oldestSlotSeg, wal_segment_size);
8080 KeepLogSeg(currpos, &oldestSlotSeg);
8081
8082 /*
8083 * Find the oldest extant segment file. We get 1 until checkpoint removes
8084 * the first WAL segment file since startup, which causes the status being
8085 * wrong under certain abnormal conditions but that doesn't actually harm.
8086 */
8087 oldestSeg = XLogGetLastRemovedSegno() + 1;
8088
8089 /* calculate oldest segment by max_wal_size */
8090 XLByteToSeg(currpos, currSeg, wal_segment_size);
8092
8093 if (currSeg > keepSegs)
8094 oldestSegMaxWalSize = currSeg - keepSegs;
8095 else
8096 oldestSegMaxWalSize = 1;
8097
8098 /* the segment we care about */
8099 XLByteToSeg(targetLSN, targetSeg, wal_segment_size);
8100
8101 /*
8102 * No point in returning reserved or extended status values if the
8103 * targetSeg is known to be lost.
8104 */
8105 if (targetSeg >= oldestSlotSeg)
8106 {
8107 /* show "reserved" when targetSeg is within max_wal_size */
8108 if (targetSeg >= oldestSegMaxWalSize)
8109 return WALAVAIL_RESERVED;
8110
8111 /* being retained by slots exceeding max_wal_size */
8112 return WALAVAIL_EXTENDED;
8113 }
8114
8115 /* WAL segments are no longer retained but haven't been removed yet */
8116 if (targetSeg >= oldestSeg)
8117 return WALAVAIL_UNRESERVED;
8118
8119 /* Definitely lost */
8120 return WALAVAIL_REMOVED;
8121}
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3897
int max_wal_size_mb
Definition: xlog.c:114
#define ConvertToXSegs(x, segsize)
Definition: xlog.c:614
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:9628
@ WALAVAIL_REMOVED
Definition: xlog.h:194

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

Referenced by pg_get_replication_slots().

◆ GetWALInsertionTimeLine()

TimeLineID GetWALInsertionTimeLine ( void  )

◆ GetWALInsertionTimeLineIfSet()

TimeLineID GetWALInsertionTimeLineIfSet ( void  )

Definition at line 6724 of file xlog.c.

6725{
6726 TimeLineID insertTLI;
6727
6729 insertTLI = XLogCtl->InsertTimeLineID;
6731
6732 return insertTLI;
6733}

References XLogCtlData::info_lck, XLogCtlData::InsertTimeLineID, SpinLockAcquire, SpinLockRelease, and XLogCtl.

Referenced by GetLatestLSN().

◆ GetXLogInsertRecPtr()

XLogRecPtr GetXLogInsertRecPtr ( void  )

Definition at line 9612 of file xlog.c.

9613{
9615 uint64 current_bytepos;
9616
9617 SpinLockAcquire(&Insert->insertpos_lck);
9618 current_bytepos = Insert->CurrBytePos;
9619 SpinLockRelease(&Insert->insertpos_lck);
9620
9621 return XLogBytePosToRecPtr(current_bytepos);
9622}

References XLogCtlData::Insert, Insert(), SpinLockAcquire, SpinLockRelease, XLogBytePosToRecPtr(), and XLogCtl.

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

◆ GetXLogWriteRecPtr()

XLogRecPtr GetXLogWriteRecPtr ( void  )

◆ InitializeWalConsistencyChecking()

void InitializeWalConsistencyChecking ( void  )

Definition at line 4965 of file xlog.c.

4966{
4968
4970 {
4971 struct config_generic *guc;
4972
4973 guc = find_option("wal_consistency_checking", false, false, ERROR);
4974
4976
4977 set_config_option_ext("wal_consistency_checking",
4979 guc->scontext, guc->source, guc->srole,
4980 GUC_ACTION_SET, true, ERROR, false);
4981
4982 /* checking should not be deferred again */
4984 }
4985}
int set_config_option_ext(const char *name, const char *value, GucContext context, GucSource source, Oid srole, GucAction action, bool changeVal, int elevel, bool is_reload)
Definition: guc.c:3382
struct config_generic * find_option(const char *name, bool create_placeholders, bool skip_errors, int elevel)
Definition: guc.c:1235
@ GUC_ACTION_SET
Definition: guc.h:203
bool process_shared_preload_libraries_done
Definition: miscinit.c:1838
GucContext scontext
Definition: guc_tables.h:185
GucSource source
Definition: guc_tables.h:183
char * wal_consistency_checking_string
Definition: xlog.c:125
static bool check_wal_consistency_checking_deferred
Definition: xlog.c:166

References Assert(), check_wal_consistency_checking_deferred, ERROR, find_option(), GUC_ACTION_SET, process_shared_preload_libraries_done, config_generic::scontext, set_config_option_ext(), config_generic::source, config_generic::srole, and wal_consistency_checking_string.

Referenced by PostgresSingleUserMain(), and PostmasterMain().

◆ IsInstallXLogFileSegmentActive()

bool IsInstallXLogFileSegmentActive ( void  )

Definition at line 9669 of file xlog.c.

9670{
9671 bool result;
9672
9673 LWLockAcquire(ControlFileLock, LW_SHARED);
9675 LWLockRelease(ControlFileLock);
9676
9677 return result;
9678}
bool InstallXLogFileSegmentActive
Definition: xlog.c:537

References XLogCtlData::InstallXLogFileSegmentActive, LW_SHARED, LWLockAcquire(), LWLockRelease(), and XLogCtl.

Referenced by XLogFileRead().

◆ issue_xlog_fsync()

void issue_xlog_fsync ( int  fd,
XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 8871 of file xlog.c.

8872{
8873 char *msg = NULL;
8875
8876 Assert(tli != 0);
8877
8878 /*
8879 * Quick exit if fsync is disabled or write() has already synced the WAL
8880 * file.
8881 */
8882 if (!enableFsync ||
8885 return;
8886
8887 /*
8888 * Measure I/O timing to sync the WAL file for pg_stat_io.
8889 */
8891
8892 pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
8893 switch (wal_sync_method)
8894 {
8896 if (pg_fsync_no_writethrough(fd) != 0)
8897 msg = _("could not fsync file \"%s\": %m");
8898 break;
8899#ifdef HAVE_FSYNC_WRITETHROUGH
8901 if (pg_fsync_writethrough(fd) != 0)
8902 msg = _("could not fsync write-through file \"%s\": %m");
8903 break;
8904#endif
8906 if (pg_fdatasync(fd) != 0)
8907 msg = _("could not fdatasync file \"%s\": %m");
8908 break;
8911 /* not reachable */
8912 Assert(false);
8913 break;
8914 default:
8915 ereport(PANIC,
8916 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8917 errmsg_internal("unrecognized \"wal_sync_method\": %d", wal_sync_method));
8918 break;
8919 }
8920
8921 /* PANIC if failed to fsync */
8922 if (msg)
8923 {
8924 char xlogfname[MAXFNAMELEN];
8925 int save_errno = errno;
8926
8927 XLogFileName(xlogfname, tli, segno, wal_segment_size);
8928 errno = save_errno;
8929 ereport(PANIC,
8931 errmsg(msg, xlogfname)));
8932 }
8933
8935
8937 start, 1, 0);
8938}
#define _(x)
Definition: elog.c:91
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:441
int pg_fdatasync(int fd)
Definition: fd.c:480
int pg_fsync_writethrough(int fd)
Definition: fd.c:461
bool enableFsync
Definition: globals.c:130
return str start
@ IOOBJECT_WAL
Definition: pgstat.h:276
@ IOCONTEXT_NORMAL
Definition: pgstat.h:286
@ IOOP_FSYNC
Definition: pgstat.h:305
instr_time pgstat_prepare_io_time(bool track_io_guc)
Definition: pgstat_io.c:90
void pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt, uint64 bytes)
Definition: pgstat_io.c:121
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int wal_sync_method
Definition: xlog.c:130
bool track_wal_io_timing
Definition: xlog.c:137

References _, Assert(), enableFsync, ereport, errcode(), errcode_for_file_access(), errmsg(), errmsg_internal(), fd(), IOCONTEXT_NORMAL, IOOBJECT_WAL, IOOP_FSYNC, MAXFNAMELEN, PANIC, pg_fdatasync(), pg_fsync_no_writethrough(), pg_fsync_writethrough(), pgstat_count_io_op_time(), pgstat_prepare_io_time(), pgstat_report_wait_end(), pgstat_report_wait_start(), start, track_wal_io_timing, wal_segment_size, wal_sync_method, WAL_SYNC_METHOD_FDATASYNC, WAL_SYNC_METHOD_FSYNC, WAL_SYNC_METHOD_FSYNC_WRITETHROUGH, WAL_SYNC_METHOD_OPEN, WAL_SYNC_METHOD_OPEN_DSYNC, and XLogFileName().

Referenced by XLogWalRcvFlush(), and XLogWrite().

◆ LocalProcessControlFile()

void LocalProcessControlFile ( bool  reset)

Definition at line 5027 of file xlog.c.

5028{
5029 Assert(reset || ControlFile == NULL);
5032}
void reset(void)
Definition: sql-declare.c:600

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

Referenced by PostgresSingleUserMain(), PostmasterMain(), and PostmasterStateMachine().

◆ ReachedEndOfBackup()

void ReachedEndOfBackup ( XLogRecPtr  EndRecPtr,
TimeLineID  tli 
)

Definition at line 6435 of file xlog.c.

6436{
6437 /*
6438 * We have reached the end of base backup, as indicated by pg_control. The
6439 * data on disk is now consistent (unless minRecoveryPoint is further
6440 * ahead, which can happen if we crashed during previous recovery). Reset
6441 * backupStartPoint and backupEndPoint, and update minRecoveryPoint to
6442 * make sure we don't allow starting up at an earlier point even if
6443 * recovery is stopped and restarted soon after this.
6444 */
6445 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6446
6447 if (ControlFile->minRecoveryPoint < EndRecPtr)
6448 {
6449 ControlFile->minRecoveryPoint = EndRecPtr;
6451 }
6452
6457
6458 LWLockRelease(ControlFileLock);
6459}
XLogRecPtr backupStartPoint
Definition: pg_control.h:170
bool backupEndRequired
Definition: pg_control.h:172
XLogRecPtr backupEndPoint
Definition: pg_control.h:171

References ControlFileData::backupEndPoint, ControlFileData::backupEndRequired, ControlFileData::backupStartPoint, ControlFile, InvalidXLogRecPtr, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, and UpdateControlFile().

Referenced by CheckRecoveryConsistency().

◆ RecoveryInProgress()

bool RecoveryInProgress ( void  )

Definition at line 6522 of file xlog.c.

6523{
6524 /*
6525 * We check shared state each time only until we leave recovery mode. We
6526 * can't re-enter recovery, so there's no need to keep checking after the
6527 * shared variable has once been seen false.
6528 */
6530 return false;
6531 else
6532 {
6533 /*
6534 * use volatile pointer to make sure we make a fresh read of the
6535 * shared variable.
6536 */
6537 volatile XLogCtlData *xlogctl = XLogCtl;
6538
6540
6541 /*
6542 * Note: We don't need a memory barrier when we're still in recovery.
6543 * We might exit recovery immediately after return, so the caller
6544 * can't rely on 'true' meaning that we're still in recovery anyway.
6545 */
6546
6548 }
6549}
static bool LocalRecoveryInProgress
Definition: xlog.c:224

References LocalRecoveryInProgress, RECOVERY_STATE_DONE, XLogCtlData::SharedRecoveryState, and XLogCtl.

Referenced by amcheck_index_mainfork_expected(), attribute_statistics_update(), BackgroundWriterMain(), BeginReportingGUCOptions(), brin_desummarize_range(), brin_summarize_range(), CanInvalidateIdleSlot(), check_transaction_isolation(), check_transaction_read_only(), CheckArchiveTimeout(), CheckLogicalDecodingRequirements(), CheckpointerMain(), ComputeXidHorizons(), CreateCheckPoint(), CreateDecodingContext(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), do_pg_backup_start(), do_pg_backup_stop(), error_commit_ts_disabled(), get_relation_info(), GetCurrentLSN(), GetLatestLSN(), GetNewMultiXactId(), GetNewObjectId(), GetNewTransactionId(), GetOldestActiveTransactionId(), GetOldestSafeDecodingTransactionId(), GetRunningTransactionData(), GetSerializableTransactionSnapshot(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), GetStrictOldestNonRemovableTransactionId(), gin_clean_pending_list(), GlobalVisHorizonKindForRel(), heap_force_common(), heap_page_prune_opt(), IdentifySystem(), InitTempTableNamespace(), InitWalSender(), IsCheckpointOnSchedule(), LockAcquireExtended(), logical_read_xlog_page(), MaintainLatestCompletedXid(), MarkBufferDirtyHint(), perform_base_backup(), pg_clear_attribute_stats(), pg_create_restore_point(), pg_current_wal_flush_lsn(), pg_current_wal_insert_lsn(), pg_current_wal_lsn(), pg_get_sequence_data(), pg_get_wal_replay_pause_state(), pg_is_in_recovery(), pg_is_wal_replay_paused(), pg_log_standby_snapshot(), pg_logical_slot_get_changes_guts(), pg_promote(), pg_replication_slot_advance(), pg_sequence_last_value(), pg_switch_wal(), pg_sync_replication_slots(), pg_wal_replay_pause(), pg_wal_replay_resume(), pg_walfile_name(), pg_walfile_name_offset(), PhysicalWakeupLogicalWalSnd(), PrepareRedoAdd(), PrepareRedoRemove(), PreventCommandDuringRecovery(), ProcSleep(), read_local_xlog_page_guts(), ReadReplicationSlot(), recovery_create_dbdir(), relation_statistics_update(), ReplicationSlotAlter(), ReplicationSlotCreate(), ReplicationSlotDrop(), ReplicationSlotReserveWal(), replorigin_check_prerequisites(), ReportChangedGUCOptions(), sendDir(), SerialSetActiveSerXmin(), show_in_hot_standby(), ShutdownXLOG(), SnapBuildWaitSnapshot(), standard_ProcessUtility(), StandbySlotsHaveCaughtup(), StartLogicalReplication(), StartReplication(), StartTransaction(), TransactionIdIsInProgress(), TruncateMultiXact(), UpdateFullPageWrites(), verify_heapam(), WALReadFromBuffers(), WalReceiverMain(), WalSndWaitForWal(), xlog_decode(), XLogBackgroundFlush(), XLogFlush(), XLogInsertAllowed(), XLogNeedsFlush(), and XLogSendPhysical().

◆ register_persistent_abort_backup_handler()

void register_persistent_abort_backup_handler ( void  )

Definition at line 9598 of file xlog.c.

9599{
9600 static bool already_done = false;
9601
9602 if (already_done)
9603 return;
9605 already_done = true;
9606}
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337

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

Referenced by pg_backup_start().

◆ RemoveNonParentXlogFiles()

void RemoveNonParentXlogFiles ( XLogRecPtr  switchpoint,
TimeLineID  newTLI 
)

Definition at line 4079 of file xlog.c.

4080{
4081 DIR *xldir;
4082 struct dirent *xlde;
4083 char switchseg[MAXFNAMELEN];
4084 XLogSegNo endLogSegNo;
4085 XLogSegNo switchLogSegNo;
4086 XLogSegNo recycleSegNo;
4087
4088 /*
4089 * Initialize info about where to begin the work. This will recycle,
4090 * somewhat arbitrarily, 10 future segments.
4091 */
4092 XLByteToPrevSeg(switchpoint, switchLogSegNo, wal_segment_size);
4093 XLByteToSeg(switchpoint, endLogSegNo, wal_segment_size);
4094 recycleSegNo = endLogSegNo + 10;
4095
4096 /*
4097 * Construct a filename of the last segment to be kept.
4098 */
4099 XLogFileName(switchseg, newTLI, switchLogSegNo, wal_segment_size);
4100
4101 elog(DEBUG2, "attempting to remove WAL segments newer than log file %s",
4102 switchseg);
4103
4104 xldir = AllocateDir(XLOGDIR);
4105
4106 while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
4107 {
4108 /* Ignore files that are not XLOG segments */
4109 if (!IsXLogFileName(xlde->d_name))
4110 continue;
4111
4112 /*
4113 * Remove files that are on a timeline older than the new one we're
4114 * switching to, but with a segment number >= the first segment on the
4115 * new timeline.
4116 */
4117 if (strncmp(xlde->d_name, switchseg, 8) < 0 &&
4118 strcmp(xlde->d_name + 8, switchseg + 8) > 0)
4119 {
4120 /*
4121 * If the file has already been marked as .ready, however, don't
4122 * remove it yet. It should be OK to remove it - files that are
4123 * not part of our timeline history are not required for recovery
4124 * - but seems safer to let them be archived and removed later.
4125 */
4126 if (!XLogArchiveIsReady(xlde->d_name))
4127 RemoveXlogFile(xlde, recycleSegNo, &endLogSegNo, newTLI);
4128 }
4129 }
4130
4131 FreeDir(xldir);
4132}
static void RemoveXlogFile(const struct dirent *segment_de, XLogSegNo recycleSegNo, XLogSegNo *endlogSegNo, TimeLineID insertTLI)
Definition: xlog.c:4148
static bool IsXLogFileName(const char *fname)
#define XLOGDIR
bool XLogArchiveIsReady(const char *xlog)
Definition: xlogarchive.c:694

References AllocateDir(), dirent::d_name, DEBUG2, elog, FreeDir(), IsXLogFileName(), MAXFNAMELEN, ReadDir(), RemoveXlogFile(), wal_segment_size, XLByteToPrevSeg, XLByteToSeg, XLogArchiveIsReady(), XLOGDIR, and XLogFileName().

Referenced by ApplyWalRecord(), and CleanupAfterArchiveRecovery().

◆ SetInstallXLogFileSegmentActive()

void SetInstallXLogFileSegmentActive ( void  )

Definition at line 9661 of file xlog.c.

9662{
9663 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9665 LWLockRelease(ControlFileLock);
9666}

References XLogCtlData::InstallXLogFileSegmentActive, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and XLogCtl.

Referenced by BootStrapXLOG(), StartupXLOG(), and WaitForWALToBecomeAvailable().

◆ SetWalWriterSleeping()

void SetWalWriterSleeping ( bool  sleeping)

Definition at line 9684 of file xlog.c.

9685{
9687 XLogCtl->WalWriterSleeping = sleeping;
9689}
bool WalWriterSleeping
Definition: xlog.c:544

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

Referenced by WalWriterMain().

◆ ShutdownXLOG()

void ShutdownXLOG ( int  code,
Datum  arg 
)

Definition at line 6790 of file xlog.c.

6791{
6792 /*
6793 * We should have an aux process resource owner to use, and we should not
6794 * be in a transaction that's installed some other resowner.
6795 */
6797 Assert(CurrentResourceOwner == NULL ||
6800
6801 /* Don't be chatty in standalone mode */
6803 (errmsg("shutting down")));
6804
6805 /*
6806 * Signal walsenders to move to stopping state.
6807 */
6809
6810 /*
6811 * Wait for WAL senders to be in stopping state. This prevents commands
6812 * from writing new WAL.
6813 */
6815
6816 if (RecoveryInProgress())
6818 else
6819 {
6820 /*
6821 * If archiving is enabled, rotate the last XLOG file so that all the
6822 * remaining records are archived (postmaster wakes up the archiver
6823 * process one more time at the end of shutdown). The checkpoint
6824 * record will go to the next XLOG file and won't be archived (yet).
6825 */
6826 if (XLogArchivingActive())
6827 RequestXLogSwitch(false);
6828
6830 }
6831}
bool IsPostmasterEnvironment
Definition: globals.c:120
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:176
void WalSndInitStopping(void)
Definition: walsender.c:3750
void WalSndWaitStopping(void)
Definition: walsender.c:3776
bool CreateRestartPoint(int flags)
Definition: xlog.c:7779
bool CreateCheckPoint(int flags)
Definition: xlog.c:7077

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

Referenced by CheckpointerMain(), and InitPostgres().

◆ StartupXLOG()

void StartupXLOG ( void  )

Definition at line 5614 of file xlog.c.

5615{
5617 CheckPoint checkPoint;
5618 bool wasShutdown;
5619 bool didCrash;
5620 bool haveTblspcMap;
5621 bool haveBackupLabel;
5622 XLogRecPtr EndOfLog;
5623 TimeLineID EndOfLogTLI;
5624 TimeLineID newTLI;
5625 bool performedWalRecovery;
5626 EndOfWalRecoveryInfo *endOfRecoveryInfo;
5629 TransactionId oldestActiveXID;
5630 bool promoted = false;
5631
5632 /*
5633 * We should have an aux process resource owner to use, and we should not
5634 * be in a transaction that's installed some other resowner.
5635 */
5637 Assert(CurrentResourceOwner == NULL ||
5640
5641 /*
5642 * Check that contents look valid.
5643 */
5645 ereport(FATAL,
5647 errmsg("control file contains invalid checkpoint location")));
5648
5649 switch (ControlFile->state)
5650 {
5651 case DB_SHUTDOWNED:
5652
5653 /*
5654 * This is the expected case, so don't be chatty in standalone
5655 * mode
5656 */
5658 (errmsg("database system was shut down at %s",
5660 break;
5661
5663 ereport(LOG,
5664 (errmsg("database system was shut down in recovery at %s",
5666 break;
5667
5668 case DB_SHUTDOWNING:
5669 ereport(LOG,
5670 (errmsg("database system shutdown was interrupted; last known up at %s",
5672 break;
5673
5675 ereport(LOG,
5676 (errmsg("database system was interrupted while in recovery at %s",
5678 errhint("This probably means that some data is corrupted and"
5679 " you will have to use the last backup for recovery.")));
5680 break;
5681
5683 ereport(LOG,
5684 (errmsg("database system was interrupted while in recovery at log time %s",
5686 errhint("If this has occurred more than once some data might be corrupted"
5687 " and you might need to choose an earlier recovery target.")));
5688 break;
5689
5690 case DB_IN_PRODUCTION:
5691 ereport(LOG,
5692 (errmsg("database system was interrupted; last known up at %s",
5694 break;
5695
5696 default:
5697 ereport(FATAL,
5699 errmsg("control file contains invalid database cluster state")));
5700 }
5701
5702 /* This is just to allow attaching to startup process with a debugger */
5703#ifdef XLOG_REPLAY_DELAY
5705 pg_usleep(60000000L);
5706#endif
5707
5708 /*
5709 * Verify that pg_wal, pg_wal/archive_status, and pg_wal/summaries exist.
5710 * In cases where someone has performed a copy for PITR, these directories
5711 * may have been excluded and need to be re-created.
5712 */
5714
5715 /* Set up timeout handler needed to report startup progress. */
5719
5720 /*----------
5721 * If we previously crashed, perform a couple of actions:
5722 *
5723 * - The pg_wal directory may still include some temporary WAL segments
5724 * used when creating a new segment, so perform some clean up to not
5725 * bloat this path. This is done first as there is no point to sync
5726 * this temporary data.
5727 *
5728 * - There might be data which we had written, intending to fsync it, but
5729 * which we had not actually fsync'd yet. Therefore, a power failure in
5730 * the near future might cause earlier unflushed writes to be lost, even
5731 * though more recent data written to disk from here on would be
5732 * persisted. To avoid that, fsync the entire data directory.
5733 */
5736 {
5739 didCrash = true;
5740 }
5741 else
5742 didCrash = false;
5743
5744 /*
5745 * Prepare for WAL recovery if needed.
5746 *
5747 * InitWalRecovery analyzes the control file and the backup label file, if
5748 * any. It updates the in-memory ControlFile buffer according to the
5749 * starting checkpoint, and sets InRecovery and ArchiveRecoveryRequested.
5750 * It also applies the tablespace map file, if any.
5751 */
5752 InitWalRecovery(ControlFile, &wasShutdown,
5753 &haveBackupLabel, &haveTblspcMap);
5754 checkPoint = ControlFile->checkPointCopy;
5755
5756 /* initialize shared memory variables from the checkpoint record */
5757 TransamVariables->nextXid = checkPoint.nextXid;
5758 TransamVariables->nextOid = checkPoint.nextOid;
5760 MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5761 AdvanceOldestClogXid(checkPoint.oldestXid);
5762 SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5763 SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5765 checkPoint.newestCommitTsXid);
5766 XLogCtl->ckptFullXid = checkPoint.nextXid;
5767
5768 /*
5769 * Clear out any old relcache cache files. This is *necessary* if we do
5770 * any WAL replay, since that would probably result in the cache files
5771 * being out of sync with database reality. In theory we could leave them
5772 * in place if the database had been cleanly shut down, but it seems
5773 * safest to just remove them always and let them be rebuilt during the
5774 * first backend startup. These files needs to be removed from all
5775 * directories including pg_tblspc, however the symlinks are created only
5776 * after reading tablespace_map file in case of archive recovery from
5777 * backup, so needs to clear old relcache files here after creating
5778 * symlinks.
5779 */
5781
5782 /*
5783 * Initialize replication slots, before there's a chance to remove
5784 * required resources.
5785 */
5787
5788 /*
5789 * Startup logical state, needs to be setup now so we have proper data
5790 * during crash recovery.
5791 */
5793
5794 /*
5795 * Startup CLOG. This must be done after TransamVariables->nextXid has
5796 * been initialized and before we accept connections or begin WAL replay.
5797 */
5798 StartupCLOG();
5799
5800 /*
5801 * Startup MultiXact. We need to do this early to be able to replay
5802 * truncations.
5803 */
5805
5806 /*
5807 * Ditto for commit timestamps. Activate the facility if the setting is
5808 * enabled in the control file, as there should be no tracking of commit
5809 * timestamps done when the setting was disabled. This facility can be
5810 * started or stopped when replaying a XLOG_PARAMETER_CHANGE record.
5811 */
5814
5815 /*
5816 * Recover knowledge about replay progress of known replication partners.
5817 */
5819
5820 /*
5821 * Initialize unlogged LSN. On a clean shutdown, it's restored from the
5822 * control file. On recovery, all unlogged relations are blown away, so
5823 * the unlogged LSN counter can be reset too.
5824 */
5828 else
5831
5832 /*
5833 * Copy any missing timeline history files between 'now' and the recovery
5834 * target timeline from archive to pg_wal. While we don't need those files
5835 * ourselves - the history file of the recovery target timeline covers all
5836 * the previous timelines in the history too - a cascading standby server
5837 * might be interested in them. Or, if you archive the WAL from this
5838 * server to a different archive than the primary, it'd be good for all
5839 * the history files to get archived there after failover, so that you can
5840 * use one of the old timelines as a PITR target. Timeline history files
5841 * are small, so it's better to copy them unnecessarily than not copy them
5842 * and regret later.
5843 */
5845
5846 /*
5847 * Before running in recovery, scan pg_twophase and fill in its status to
5848 * be able to work on entries generated by redo. Doing a scan before
5849 * taking any recovery action has the merit to discard any 2PC files that
5850 * are newer than the first record to replay, saving from any conflicts at
5851 * replay. This avoids as well any subsequent scans when doing recovery
5852 * of the on-disk two-phase data.
5853 */
5855
5856 /*
5857 * When starting with crash recovery, reset pgstat data - it might not be
5858 * valid. Otherwise restore pgstat data. It's safe to do this here,
5859 * because postmaster will not yet have started any other processes.
5860 *
5861 * NB: Restoring replication slot stats relies on slot state to have
5862 * already been restored from disk.
5863 *
5864 * TODO: With a bit of extra work we could just start with a pgstat file
5865 * associated with the checkpoint redo location we're starting from.
5866 */
5867 if (didCrash)
5869 else
5871
5873
5876
5877 /* REDO */
5878 if (InRecovery)
5879 {
5880 /* Initialize state for RecoveryInProgress() */
5884 else
5887
5888 /*
5889 * Update pg_control to show that we are recovering and to show the
5890 * selected checkpoint as the place we are starting from. We also mark
5891 * pg_control with any minimum recovery stop point obtained from a
5892 * backup history file.
5893 *
5894 * No need to hold ControlFileLock yet, we aren't up far enough.
5895 */
5897
5898 /*
5899 * If there was a backup label file, it's done its job and the info
5900 * has now been propagated into pg_control. We must get rid of the
5901 * label file so that if we crash during recovery, we'll pick up at
5902 * the latest recovery restartpoint instead of going all the way back
5903 * to the backup start point. It seems prudent though to just rename
5904 * the file out of the way rather than delete it completely.
5905 */
5906 if (haveBackupLabel)
5907 {
5908 unlink(BACKUP_LABEL_OLD);
5910 }
5911
5912 /*
5913 * If there was a tablespace_map file, it's done its job and the
5914 * symlinks have been created. We must get rid of the map file so
5915 * that if we crash during recovery, we don't create symlinks again.
5916 * It seems prudent though to just rename the file out of the way
5917 * rather than delete it completely.
5918 */
5919 if (haveTblspcMap)
5920 {
5921 unlink(TABLESPACE_MAP_OLD);
5923 }
5924
5925 /*
5926 * Initialize our local copy of minRecoveryPoint. When doing crash
5927 * recovery we want to replay up to the end of WAL. Particularly, in
5928 * the case of a promoted standby minRecoveryPoint value in the
5929 * control file is only updated after the first checkpoint. However,
5930 * if the instance crashes before the first post-recovery checkpoint
5931 * is completed then recovery will use a stale location causing the
5932 * startup process to think that there are still invalid page
5933 * references when checking for data consistency.
5934 */
5936 {
5939 }
5940 else
5941 {
5944 }
5945
5946 /* Check that the GUCs used to generate the WAL allow recovery */
5948
5949 /*
5950 * We're in recovery, so unlogged relations may be trashed and must be
5951 * reset. This should be done BEFORE allowing Hot Standby
5952 * connections, so that read-only backends don't try to read whatever
5953 * garbage is left over from before.
5954 */
5956
5957 /*
5958 * Likewise, delete any saved transaction snapshot files that got left
5959 * behind by crashed backends.
5960 */
5962
5963 /*
5964 * Initialize for Hot Standby, if enabled. We won't let backends in
5965 * yet, not until we've reached the min recovery point specified in
5966 * control file and we've established a recovery snapshot from a
5967 * running-xacts WAL record.
5968 */
5970 {
5971 TransactionId *xids;
5972 int nxids;
5973
5975 (errmsg_internal("initializing for hot standby")));
5976
5978
5979 if (wasShutdown)
5980 oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
5981 else
5982 oldestActiveXID = checkPoint.oldestActiveXid;
5983 Assert(TransactionIdIsValid(oldestActiveXID));
5984
5985 /* Tell procarray about the range of xids it has to deal with */
5987
5988 /*
5989 * Startup subtrans only. CLOG, MultiXact and commit timestamp
5990 * have already been started up and other SLRUs are not maintained
5991 * during recovery and need not be started yet.
5992 */
5993 StartupSUBTRANS(oldestActiveXID);
5994
5995 /*
5996 * If we're beginning at a shutdown checkpoint, we know that
5997 * nothing was running on the primary at this point. So fake-up an
5998 * empty running-xacts record and use that here and now. Recover
5999 * additional standby state for prepared transactions.
6000 */
6001 if (wasShutdown)
6002 {
6004 TransactionId latestCompletedXid;
6005
6006 /* Update pg_subtrans entries for any prepared transactions */
6008
6009 /*
6010 * Construct a RunningTransactions snapshot representing a
6011 * shut down server, with only prepared transactions still
6012 * alive. We're never overflowed at this point because all
6013 * subxids are listed with their parent prepared transactions.
6014 */
6015 running.xcnt = nxids;
6016 running.subxcnt = 0;
6018 running.nextXid = XidFromFullTransactionId(checkPoint.nextXid);
6019 running.oldestRunningXid = oldestActiveXID;
6020 latestCompletedXid = XidFromFullTransactionId(checkPoint.nextXid);
6021 TransactionIdRetreat(latestCompletedXid);
6022 Assert(TransactionIdIsNormal(latestCompletedXid));
6023 running.latestCompletedXid = latestCompletedXid;
6024 running.xids = xids;
6025
6027 }
6028 }
6029
6030 /*
6031 * We're all set for replaying the WAL now. Do it.
6032 */
6034 performedWalRecovery = true;
6035 }
6036 else
6037 performedWalRecovery = false;
6038
6039 /*
6040 * Finish WAL recovery.
6041 */
6042 endOfRecoveryInfo = FinishWalRecovery();
6043 EndOfLog = endOfRecoveryInfo->endOfLog;
6044 EndOfLogTLI = endOfRecoveryInfo->endOfLogTLI;
6045 abortedRecPtr = endOfRecoveryInfo->abortedRecPtr;
6046 missingContrecPtr = endOfRecoveryInfo->missingContrecPtr;
6047
6048 /*
6049 * Reset ps status display, so as no information related to recovery shows
6050 * up.
6051 */
6052 set_ps_display("");
6053
6054 /*
6055 * When recovering from a backup (we are in recovery, and archive recovery
6056 * was requested), complain if we did not roll forward far enough to reach
6057 * the point where the database is consistent. For regular online
6058 * backup-from-primary, that means reaching the end-of-backup WAL record
6059 * (at which point we reset backupStartPoint to be Invalid), for
6060 * backup-from-replica (which can't inject records into the WAL stream),
6061 * that point is when we reach the minRecoveryPoint in pg_control (which
6062 * we purposefully copy last when backing up from a replica). For
6063 * pg_rewind (which creates a backup_label with a method of "pg_rewind")
6064 * or snapshot-style backups (which don't), backupEndRequired will be set
6065 * to false.
6066 *
6067 * Note: it is indeed okay to look at the local variable
6068 * LocalMinRecoveryPoint here, even though ControlFile->minRecoveryPoint
6069 * might be further ahead --- ControlFile->minRecoveryPoint cannot have
6070 * been advanced beyond the WAL we processed.
6071 */
6072 if (InRecovery &&
6073 (EndOfLog < LocalMinRecoveryPoint ||
6075 {
6076 /*
6077 * Ran off end of WAL before reaching end-of-backup WAL record, or
6078 * minRecoveryPoint. That's a bad sign, indicating that you tried to
6079 * recover from an online backup but never called pg_backup_stop(), or
6080 * you didn't archive all the WAL needed.
6081 */
6083 {
6085 ereport(FATAL,
6086 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
6087 errmsg("WAL ends before end of online backup"),
6088 errhint("All WAL generated while online backup was taken must be available at recovery.")));
6089 else
6090 ereport(FATAL,
6091 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
6092 errmsg("WAL ends before consistent recovery point")));
6093 }
6094 }
6095
6096 /*
6097 * Reset unlogged relations to the contents of their INIT fork. This is
6098 * done AFTER recovery is complete so as to include any unlogged relations
6099 * created during recovery, but BEFORE recovery is marked as having
6100 * completed successfully. Otherwise we'd not retry if any of the post
6101 * end-of-recovery steps fail.
6102 */
6103 if (InRecovery)
6105
6106 /*
6107 * Pre-scan prepared transactions to find out the range of XIDs present.
6108 * This information is not quite needed yet, but it is positioned here so
6109 * as potential problems are detected before any on-disk change is done.
6110 */
6111 oldestActiveXID = PrescanPreparedTransactions(NULL, NULL);
6112
6113 /*
6114 * Allow ordinary WAL segment creation before possibly switching to a new
6115 * timeline, which creates a new segment, and after the last ReadRecord().
6116 */
6118
6119 /*
6120 * Consider whether we need to assign a new timeline ID.
6121 *
6122 * If we did archive recovery, we always assign a new ID. This handles a
6123 * couple of issues. If we stopped short of the end of WAL during
6124 * recovery, then we are clearly generating a new timeline and must assign
6125 * it a unique new ID. Even if we ran to the end, modifying the current
6126 * last segment is problematic because it may result in trying to
6127 * overwrite an already-archived copy of that segment, and we encourage
6128 * DBAs to make their archive_commands reject that. We can dodge the
6129 * problem by making the new active segment have a new timeline ID.
6130 *
6131 * In a normal crash recovery, we can just extend the timeline we were in.
6132 */
6133 newTLI = endOfRecoveryInfo->lastRecTLI;
6135 {
6137 ereport(LOG,
6138 (errmsg("selected new timeline ID: %u", newTLI)));
6139
6140 /*
6141 * Make a writable copy of the last WAL segment. (Note that we also
6142 * have a copy of the last block of the old WAL in
6143 * endOfRecovery->lastPage; we will use that below.)
6144 */
6145 XLogInitNewTimeline(EndOfLogTLI, EndOfLog, newTLI);
6146
6147 /*
6148 * Remove the signal files out of the way, so that we don't
6149 * accidentally re-enter archive recovery mode in a subsequent crash.
6150 */
6151 if (endOfRecoveryInfo->standby_signal_file_found)
6153
6154 if (endOfRecoveryInfo->recovery_signal_file_found)
6156
6157 /*
6158 * Write the timeline history file, and have it archived. After this
6159 * point (or rather, as soon as the file is archived), the timeline
6160 * will appear as "taken" in the WAL archive and to any standby
6161 * servers. If we crash before actually switching to the new
6162 * timeline, standby servers will nevertheless think that we switched
6163 * to the new timeline, and will try to connect to the new timeline.
6164 * To minimize the window for that, try to do as little as possible
6165 * between here and writing the end-of-recovery record.
6166 */
6168 EndOfLog, endOfRecoveryInfo->recoveryStopReason);
6169
6170 ereport(LOG,
6171 (errmsg("archive recovery complete")));
6172 }
6173
6174 /* Save the selected TimeLineID in shared memory, too */
6176 XLogCtl->InsertTimeLineID = newTLI;
6177 XLogCtl->PrevTimeLineID = endOfRecoveryInfo->lastRecTLI;
6179
6180 /*
6181 * Actually, if WAL ended in an incomplete record, skip the parts that
6182 * made it through and start writing after the portion that persisted.
6183 * (It's critical to first write an OVERWRITE_CONTRECORD message, which
6184 * we'll do as soon as we're open for writing new WAL.)
6185 */
6187 {
6188 /*
6189 * We should only have a missingContrecPtr if we're not switching to a
6190 * new timeline. When a timeline switch occurs, WAL is copied from the
6191 * old timeline to the new only up to the end of the last complete
6192 * record, so there can't be an incomplete WAL record that we need to
6193 * disregard.
6194 */
6195 Assert(newTLI == endOfRecoveryInfo->lastRecTLI);
6197 EndOfLog = missingContrecPtr;
6198 }
6199
6200 /*
6201 * Prepare to write WAL starting at EndOfLog location, and init xlog
6202 * buffer cache using the block containing the last record from the
6203 * previous incarnation.
6204 */
6205 Insert = &XLogCtl->Insert;
6206 Insert->PrevBytePos = XLogRecPtrToBytePos(endOfRecoveryInfo->lastRec);
6207 Insert->CurrBytePos = XLogRecPtrToBytePos(EndOfLog);
6208
6209 /*
6210 * Tricky point here: lastPage contains the *last* block that the LastRec
6211 * record spans, not the one it starts in. The last block is indeed the
6212 * one we want to use.
6213 */
6214 if (EndOfLog % XLOG_BLCKSZ != 0)
6215 {
6216 char *page;
6217 int len;
6218 int firstIdx;
6219
6220 firstIdx = XLogRecPtrToBufIdx(EndOfLog);
6221 len = EndOfLog - endOfRecoveryInfo->lastPageBeginPtr;
6222 Assert(len < XLOG_BLCKSZ);
6223
6224 /* Copy the valid part of the last block, and zero the rest */
6225 page = &XLogCtl->pages[firstIdx * XLOG_BLCKSZ];
6226 memcpy(page, endOfRecoveryInfo->lastPage, len);
6227 memset(page + len, 0, XLOG_BLCKSZ - len);
6228
6229 pg_atomic_write_u64(&XLogCtl->xlblocks[firstIdx], endOfRecoveryInfo->lastPageBeginPtr + XLOG_BLCKSZ);
6230 pg_atomic_write_u64(&XLogCtl->InitializedUpTo, endOfRecoveryInfo->lastPageBeginPtr + XLOG_BLCKSZ);
6231 XLogCtl->InitializedFrom = endOfRecoveryInfo->lastPageBeginPtr;
6232 }
6233 else
6234 {
6235 /*
6236 * There is no partial block to copy. Just set InitializedUpTo, and
6237 * let the first attempt to insert a log record to initialize the next
6238 * buffer.
6239 */
6241 XLogCtl->InitializedFrom = EndOfLog;
6242 }
6244
6245 /*
6246 * Update local and shared status. This is OK to do without any locks
6247 * because no other process can be reading or writing WAL yet.
6248 */
6249 LogwrtResult.Write = LogwrtResult.Flush = EndOfLog;
6253 XLogCtl->LogwrtRqst.Write = EndOfLog;
6254 XLogCtl->LogwrtRqst.Flush = EndOfLog;
6255
6256 /*
6257 * Preallocate additional log files, if wanted.
6258 */
6259 PreallocXlogFiles(EndOfLog, newTLI);
6260
6261 /*
6262 * Okay, we're officially UP.
6263 */
6264 InRecovery = false;
6265
6266 /* start the archive_timeout timer and LSN running */
6267 XLogCtl->lastSegSwitchTime = (pg_time_t) time(NULL);
6268 XLogCtl->lastSegSwitchLSN = EndOfLog;
6269
6270 /* also initialize latestCompletedXid, to nextXid - 1 */
6271 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
6274 LWLockRelease(ProcArrayLock);
6275
6276 /*
6277 * Start up subtrans, if not already done for hot standby. (commit
6278 * timestamps are started below, if necessary.)
6279 */
6281 StartupSUBTRANS(oldestActiveXID);
6282
6283 /*
6284 * Perform end of recovery actions for any SLRUs that need it.
6285 */
6286 TrimCLOG();
6287 TrimMultiXact();
6288
6289 /*
6290 * Reload shared-memory state for prepared transactions. This needs to
6291 * happen before renaming the last partial segment of the old timeline as
6292 * it may be possible that we have to recover some transactions from it.
6293 */
6295
6296 /* Shut down xlogreader */
6298
6299 /* Enable WAL writes for this backend only. */
6301
6302 /* If necessary, write overwrite-contrecord before doing anything else */
6304 {
6307 }
6308
6309 /*
6310 * Update full_page_writes in shared memory and write an XLOG_FPW_CHANGE
6311 * record before resource manager writes cleanup WAL records or checkpoint
6312 * record is written.
6313 */
6314 Insert->fullPageWrites = lastFullPageWrites;
6316
6317 /*
6318 * Emit checkpoint or end-of-recovery record in XLOG, if required.
6319 */
6320 if (performedWalRecovery)
6321 promoted = PerformRecoveryXLogAction();
6322
6323 /*
6324 * If any of the critical GUCs have changed, log them before we allow
6325 * backends to write WAL.
6326 */
6328
6329 /* If this is archive recovery, perform post-recovery cleanup actions. */
6331 CleanupAfterArchiveRecovery(EndOfLogTLI, EndOfLog, newTLI);
6332
6333 /*
6334 * Local WAL inserts enabled, so it's time to finish initialization of
6335 * commit timestamp.
6336 */
6338
6339 /*
6340 * All done with end-of-recovery actions.
6341 *
6342 * Now allow backends to write WAL and update the control file status in
6343 * consequence. SharedRecoveryState, that controls if backends can write
6344 * WAL, is updated while holding ControlFileLock to prevent other backends
6345 * to look at an inconsistent state of the control file in shared memory.
6346 * There is still a small window during which backends can write WAL and
6347 * the control file is still referring to a system not in DB_IN_PRODUCTION
6348 * state while looking at the on-disk control file.
6349 *
6350 * Also, we use info_lck to update SharedRecoveryState to ensure that
6351 * there are no race conditions concerning visibility of other recent
6352 * updates to shared memory.
6353 */
6354 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6356
6360
6362 LWLockRelease(ControlFileLock);
6363
6364 /*
6365 * Shutdown the recovery environment. This must occur after
6366 * RecoverPreparedTransactions() (see notes in lock_twophase_recover())
6367 * and after switching SharedRecoveryState to RECOVERY_STATE_DONE so as
6368 * any session building a snapshot will not rely on KnownAssignedXids as
6369 * RecoveryInProgress() would return false at this stage. This is
6370 * particularly critical for prepared 2PC transactions, that would still
6371 * need to be included in snapshots once recovery has ended.
6372 */
6375
6376 /*
6377 * If there were cascading standby servers connected to us, nudge any wal
6378 * sender processes to notice that we've been promoted.
6379 */
6380 WalSndWakeup(true, true);
6381
6382 /*
6383 * If this was a promotion, request an (online) checkpoint now. This isn't
6384 * required for consistency, but the last restartpoint might be far back,
6385 * and in case of a crash, recovering from it might take a longer than is
6386 * appropriate now that we're not in standby mode anymore.
6387 */
6388 if (promoted)
6390}
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:485
static void pg_atomic_write_membarrier_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:494
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:467
TimeLineID findNewestTimeLine(TimeLineID startTLI)
Definition: timeline.c:264
void restoreTimeLineHistoryFiles(TimeLineID begin, TimeLineID end)
Definition: timeline.c:50
void writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI, XLogRecPtr switchpoint, char *reason)
Definition: timeline.c:304
void startup_progress_timeout_handler(void)
Definition: startup.c:307
uint32 TransactionId
Definition: c.h:623
void StartupCLOG(void)
Definition: clog.c:877
void TrimCLOG(void)
Definition: clog.c:892
void StartupCommitTs(void)
Definition: commit_ts.c:632
void CompleteCommitTsInitialization(void)
Definition: commit_ts.c:642
#define FATAL
Definition: elog.h:41
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:782
int durable_unlink(const char *fname, int elevel)
Definition: fd.c:872
void SyncDataDirectory(void)
Definition: fd.c:3609
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:477
void TrimMultiXact(void)
Definition: multixact.c:2178
void StartupMultiXact(void)
Definition: multixact.c:2153
void StartupReplicationOrigin(void)
Definition: origin.c:699
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
@ DB_IN_PRODUCTION
Definition: pg_control.h:97
@ DB_IN_CRASH_RECOVERY
Definition: pg_control.h:95
const void size_t len
void pgstat_restore_stats(void)
Definition: pgstat.c:504
void pgstat_discard_stats(void)
Definition: pgstat.c:516
void ProcArrayApplyRecoveryInfo(RunningTransactions running)
Definition: procarray.c:1054
void ProcArrayInitRecovery(TransactionId initializedUptoXID)
Definition: procarray.c:1023
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
void ResetUnloggedRelations(int op)
Definition: reinit.c:47
#define UNLOGGED_RELATION_INIT
Definition: reinit.h:28
#define UNLOGGED_RELATION_CLEANUP
Definition: reinit.h:27
void RelationCacheInitFileRemove(void)
Definition: relcache.c:6883
void StartupReorderBuffer(void)
void StartupReplicationSlots(void)
Definition: slot.c:2089
void DeleteAllExportedSnapshotFiles(void)
Definition: snapmgr.c:1574
void InitRecoveryTransactionEnvironment(void)
Definition: standby.c:95
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:161
@ SUBXIDS_IN_SUBTRANS
Definition: standby.h:82
bool track_commit_timestamp
Definition: pg_control.h:185
XLogRecPtr lastPageBeginPtr
Definition: xlogrecovery.h:111
XLogRecPtr abortedRecPtr
Definition: xlogrecovery.h:120
XLogRecPtr missingContrecPtr
Definition: xlogrecovery.h:121
TimeLineID endOfLogTLI
Definition: xlogrecovery.h:109
TransactionId oldestRunningXid
Definition: standby.h:92
TransactionId nextXid
Definition: standby.h:91
TransactionId latestCompletedXid
Definition: standby.h:95
subxids_array_status subxid_status
Definition: standby.h:90
TransactionId * xids
Definition: standby.h:97
FullTransactionId latestCompletedXid
Definition: transam.h:238
char * pages
Definition: xlog.c:503
pg_time_t lastSegSwitchTime
Definition: xlog.c:462
pg_atomic_uint64 InitializeReserved
Definition: xlog.c:486
pg_atomic_uint64 InitializedUpTo
Definition: xlog.c:494
XLogRecPtr lastSegSwitchLSN
Definition: xlog.c:463
pg_atomic_uint64 * xlblocks
Definition: xlog.c:504
pg_atomic_uint64 logWriteResult
Definition: xlog.c:467
pg_atomic_uint64 logFlushResult
Definition: xlog.c:468
pg_atomic_uint64 logInsertResult
Definition: xlog.c:466
XLogRecPtr InitializedFrom
Definition: xlog.c:473
XLogRecPtr Flush
Definition: xlog.c:317
void StartupSUBTRANS(TransactionId oldestActiveXID)
Definition: subtrans.c:309
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:505
@ STARTUP_PROGRESS_TIMEOUT
Definition: timeout.h:38
#define TransactionIdRetreat(dest)
Definition: transam.h:141
static void FullTransactionIdRetreat(FullTransactionId *dest)
Definition: transam.h:103
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void RecoverPreparedTransactions(void)
Definition: twophase.c:2073
void restoreTwoPhaseData(void)
Definition: twophase.c:1888
TransactionId PrescanPreparedTransactions(TransactionId **xids_p, int *nxids_p)
Definition: twophase.c:1952
void StandbyRecoverPreparedTransactions(void)
Definition: twophase.c:2032
void WalSndWakeup(bool physical, bool logical)
Definition: walsender.c:3671
void UpdateFullPageWrites(void)
Definition: xlog.c:8354
static char * str_time(pg_time_t tnow)
Definition: xlog.c:5384
static void ValidateXLOGDirectoryStructure(void)
Definition: xlog.c:4238
static XLogRecPtr CreateOverwriteContrecordRecord(XLogRecPtr aborted_lsn, XLogRecPtr pagePtr, TimeLineID newTLI)
Definition: xlog.c:7628
static void XLogReportParameters(void)
Definition: xlog.c:8291
static bool PerformRecoveryXLogAction(void)
Definition: xlog.c:6472
static void CleanupAfterArchiveRecovery(TimeLineID EndOfLogTLI, XLogRecPtr EndOfLog, TimeLineID newTLI)
Definition: xlog.c:5474
static bool lastFullPageWrites
Definition: xlog.c:217
static uint64 XLogRecPtrToBytePos(XLogRecPtr ptr)
Definition: xlog.c:1952
static void XLogInitNewTimeline(TimeLineID endTLI, XLogRecPtr endOfLog, TimeLineID newTLI)
Definition: xlog.c:5399
static void CheckRequiredParameterValues(void)
Definition: xlog.c:5570
#define XLogRecPtrToBufIdx(recptr)
Definition: xlog.c:602
static void RemoveTempXlogFiles(void)
Definition: xlog.c:3971
#define TABLESPACE_MAP_OLD
Definition: xlog.h:307
#define TABLESPACE_MAP
Definition: xlog.h:306
#define STANDBY_SIGNAL_FILE
Definition: xlog.h:302
#define BACKUP_LABEL_OLD
Definition: xlog.h:304
#define BACKUP_LABEL_FILE
Definition: xlog.h:303
#define RECOVERY_SIGNAL_FILE
Definition: xlog.h:301
#define XRecOffIsValid(xlrp)
#define FirstNormalUnloggedLSN
Definition: xlogdefs.h:36
void ShutdownWalRecovery(void)
bool ArchiveRecoveryRequested
Definition: xlogrecovery.c:138
bool InArchiveRecovery
Definition: xlogrecovery.c:139
void PerformWalRecovery(void)
static XLogRecPtr missingContrecPtr
Definition: xlogrecovery.c:379
static XLogRecPtr abortedRecPtr
Definition: xlogrecovery.c:378
EndOfWalRecoveryInfo * FinishWalRecovery(void)
void InitWalRecovery(ControlFileData *ControlFile, bool *wasShutdown_ptr, bool *haveBackupLabel_ptr, bool *haveTblspcMap_ptr)
Definition: xlogrecovery.c:518
TimeLineID recoveryTargetTLI
Definition: xlogrecovery.c:123
HotStandbyState standbyState
Definition: xlogutils.c:53
bool InRecovery
Definition: xlogutils.c:50
@ STANDBY_DISABLED
Definition: xlogutils.h:52

References abortedRecPtr, EndOfWalRecoveryInfo::abortedRecPtr, AdvanceOldestClogXid(), ArchiveRecoveryRequested, Assert(), AuxProcessResourceOwner, BACKUP_LABEL_FILE, BACKUP_LABEL_OLD, ControlFileData::backupEndRequired, ControlFileData::backupStartPoint, ControlFileData::checkPoint, CHECKPOINT_FORCE, ControlFileData::checkPointCopy, CheckRequiredParameterValues(), XLogCtlData::ckptFullXid, CleanupAfterArchiveRecovery(), CompleteCommitTsInitialization(), ControlFile, CreateOverwriteContrecordRecord(), CurrentResourceOwner, DB_IN_ARCHIVE_RECOVERY, DB_IN_CRASH_RECOVERY, DB_IN_PRODUCTION, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY, DB_SHUTDOWNING, DEBUG1, DeleteAllExportedSnapshotFiles(), doPageWrites, durable_rename(), durable_unlink(), EnableHotStandby, EndOfWalRecoveryInfo::endOfLog, EndOfWalRecoveryInfo::endOfLogTLI, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errhint(), errmsg(), errmsg_internal(), FATAL, findNewestTimeLine(), FinishWalRecovery(), FirstNormalUnloggedLSN, XLogwrtRqst::Flush, XLogwrtResult::Flush, CheckPoint::fullPageWrites, FullTransactionIdRetreat(), InArchiveRecovery, XLogCtlData::info_lck, XLogCtlData::InitializedFrom, XLogCtlData::InitializedUpTo, XLogCtlData::InitializeReserved, InitRecoveryTransactionEnvironment(), InitWalRecovery(), InRecovery, XLogCtlData::Insert, Insert(), XLogCtlData::InsertTimeLineID, InvalidXLogRecPtr, IsBootstrapProcessingMode, IsPostmasterEnvironment, lastFullPageWrites, EndOfWalRecoveryInfo::lastPage, EndOfWalRecoveryInfo::lastPageBeginPtr, EndOfWalRecoveryInfo::lastRec, EndOfWalRecoveryInfo::lastRecTLI, XLogCtlData::lastSegSwitchLSN, XLogCtlData::lastSegSwitchTime, TransamVariablesData::latestCompletedXid, RunningTransactionsData::latestCompletedXid, len, LocalMinRecoveryPoint, LocalMinRecoveryPointTLI, LocalSetXLogInsertAllowed(), LOG, XLogCtlData::logFlushResult, XLogCtlData::logInsertResult, XLogCtlData::logWriteResult, LogwrtResult, XLogCtlData::LogwrtRqst, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, missingContrecPtr, EndOfWalRecoveryInfo::missingContrecPtr, MultiXactSetNextMXact(), CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, TransamVariablesData::nextOid, CheckPoint::nextOid, TransamVariablesData::nextXid, CheckPoint::nextXid, RunningTransactionsData::nextXid, NOTICE, TransamVariablesData::oidCount, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, RunningTransactionsData::oldestRunningXid, CheckPoint::oldestXid, CheckPoint::oldestXidDB, XLogCtlData::pages, PerformRecoveryXLogAction(), PerformWalRecovery(), pg_atomic_read_u64(), pg_atomic_write_membarrier_u64(), pg_atomic_write_u64(), pg_usleep(), pgstat_discard_stats(), pgstat_restore_stats(), PreallocXlogFiles(), PrescanPreparedTransactions(), XLogCtlData::PrevTimeLineID, ProcArrayApplyRecoveryInfo(), ProcArrayInitRecovery(), RecoverPreparedTransactions(), RECOVERY_SIGNAL_FILE, EndOfWalRecoveryInfo::recovery_signal_file_found, RECOVERY_STATE_ARCHIVE, RECOVERY_STATE_CRASH, RECOVERY_STATE_DONE, EndOfWalRecoveryInfo::recoveryStopReason, recoveryTargetTLI, CheckPoint::redo, RedoRecPtr, XLogCtlInsert::RedoRecPtr, XLogCtlData::RedoRecPtr, RegisterTimeout(), RelationCacheInitFileRemove(), RemoveTempXlogFiles(), RequestCheckpoint(), ResetUnloggedRelations(), restoreTimeLineHistoryFiles(), restoreTwoPhaseData(), set_ps_display(), SetCommitTsLimit(), SetInstallXLogFileSegmentActive(), SetMultiXactIdLimit(), SetTransactionIdLimit(), XLogCtlData::SharedRecoveryState, ShutdownRecoveryTransactionEnvironment(), ShutdownWalRecovery(), SpinLockAcquire, SpinLockRelease, STANDBY_DISABLED, STANDBY_SIGNAL_FILE, EndOfWalRecoveryInfo::standby_signal_file_found, StandbyRecoverPreparedTransactions(), standbyState, STARTUP_PROGRESS_TIMEOUT, startup_progress_timeout_handler(), StartupCLOG(), StartupCommitTs(), StartupMultiXact(), StartupReorderBuffer(), StartupReplicationOrigin(), StartupReplicationSlots(), StartupSUBTRANS(), ControlFileData::state, str_time(), RunningTransactionsData::subxcnt, RunningTransactionsData::subxid_status, SUBXIDS_IN_SUBTRANS, SyncDataDirectory(), TABLESPACE_MAP, TABLESPACE_MAP_OLD, CheckPoint::ThisTimeLineID, CheckPoint::time, ControlFileData::time, ControlFileData::track_commit_timestamp, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdRetreat, TransamVariables, TrimCLOG(), TrimMultiXact(), UNLOGGED_RELATION_CLEANUP, UNLOGGED_RELATION_INIT, XLogCtlData::unloggedLSN, ControlFileData::unloggedLSN, UpdateControlFile(), UpdateFullPageWrites(), ValidateXLOGDirectoryStructure(), WalSndWakeup(), XLogwrtRqst::Write, XLogwrtResult::Write, writeTimeLineHistory(), RunningTransactionsData::xcnt, XidFromFullTransactionId, RunningTransactionsData::xids, XLogCtlData::xlblocks, XLogCtl, XLogInitNewTimeline(), XLogRecPtrIsInvalid, XLogRecPtrToBufIdx, XLogRecPtrToBytePos(), XLogReportParameters(), and XRecOffIsValid.

Referenced by InitPostgres(), and StartupProcessMain().

◆ SwitchIntoArchiveRecovery()

void SwitchIntoArchiveRecovery ( XLogRecPtr  EndRecPtr,
TimeLineID  replayTLI 
)

Definition at line 6397 of file xlog.c.

6398{
6399 /* initialize minRecoveryPoint to this record */
6400 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6402 if (ControlFile->minRecoveryPoint < EndRecPtr)
6403 {
6404 ControlFile->minRecoveryPoint = EndRecPtr;
6405 ControlFile->minRecoveryPointTLI = replayTLI;
6406 }
6407 /* update local copy */
6410
6411 /*
6412 * The startup process can update its local copy of minRecoveryPoint from
6413 * this point.
6414 */
6416
6418
6419 /*
6420 * We update SharedRecoveryState while holding the lock on ControlFileLock
6421 * so both states are consistent in shared memory.
6422 */
6426
6427 LWLockRelease(ControlFileLock);
6428}
static bool updateMinRecoveryPoint
Definition: xlog.c:659

References ControlFile, DB_IN_ARCHIVE_RECOVERY, XLogCtlData::info_lck, LocalMinRecoveryPoint, LocalMinRecoveryPointTLI, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, RECOVERY_STATE_ARCHIVE, XLogCtlData::SharedRecoveryState, SpinLockAcquire, SpinLockRelease, ControlFileData::state, UpdateControlFile(), updateMinRecoveryPoint, and XLogCtl.

Referenced by ReadRecord().

◆ UpdateFullPageWrites()

void UpdateFullPageWrites ( void  )

Definition at line 8354 of file xlog.c.

8355{
8357 bool recoveryInProgress;
8358
8359 /*
8360 * Do nothing if full_page_writes has not been changed.
8361 *
8362 * It's safe to check the shared full_page_writes without the lock,
8363 * because we assume that there is no concurrently running process which
8364 * can update it.
8365 */
8366 if (fullPageWrites == Insert->fullPageWrites)
8367 return;
8368
8369 /*
8370 * Perform this outside critical section so that the WAL insert
8371 * initialization done by RecoveryInProgress() doesn't trigger an
8372 * assertion failure.
8373 */
8374 recoveryInProgress = RecoveryInProgress();
8375
8377
8378 /*
8379 * It's always safe to take full page images, even when not strictly
8380 * required, but not the other round. So if we're setting full_page_writes
8381 * to true, first set it true and then write the WAL record. If we're
8382 * setting it to false, first write the WAL record and then set the global
8383 * flag.
8384 */
8385 if (fullPageWrites)
8386 {
8388 Insert->fullPageWrites = true;
8390 }
8391
8392 /*
8393 * Write an XLOG_FPW_CHANGE record. This allows us to keep track of
8394 * full_page_writes during archive recovery, if required.
8395 */
8396 if (XLogStandbyInfoActive() && !recoveryInProgress)
8397 {
8399 XLogRegisterData(&fullPageWrites, sizeof(bool));
8400
8401 XLogInsert(RM_XLOG_ID, XLOG_FPW_CHANGE);
8402 }
8403
8404 if (!fullPageWrites)
8405 {
8407 Insert->fullPageWrites = false;
8409 }
8411}
#define XLOG_FPW_CHANGE
Definition: pg_control.h:76

References END_CRIT_SECTION, fullPageWrites, XLogCtlData::Insert, Insert(), RecoveryInProgress(), START_CRIT_SECTION, WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLOG_FPW_CHANGE, XLogBeginInsert(), XLogCtl, XLogInsert(), XLogRegisterData(), and XLogStandbyInfoActive.

Referenced by StartupXLOG(), and UpdateSharedMemoryConfig().

◆ WALReadFromBuffers()

Size WALReadFromBuffers ( char *  dstbuf,
XLogRecPtr  startptr,
Size  count,
TimeLineID  tli 
)

Definition at line 1759 of file xlog.c.

1761{
1762 char *pdst = dstbuf;
1763 XLogRecPtr recptr = startptr;
1764 XLogRecPtr inserted;
1765 Size nbytes = count;
1766
1768 return 0;
1769
1770 Assert(!XLogRecPtrIsInvalid(startptr));
1771
1772 /*
1773 * Caller should ensure that the requested data has been inserted into WAL
1774 * buffers before we try to read it.
1775 */
1777 if (startptr + count > inserted)
1778 ereport(ERROR,
1779 errmsg("cannot read past end of generated WAL: requested %X/%X, current position %X/%X",
1780 LSN_FORMAT_ARGS(startptr + count),
1781 LSN_FORMAT_ARGS(inserted)));
1782
1783 /*
1784 * Loop through the buffers without a lock. For each buffer, atomically
1785 * read and verify the end pointer, then copy the data out, and finally
1786 * re-read and re-verify the end pointer.
1787 *
1788 * Once a page is evicted, it never returns to the WAL buffers, so if the
1789 * end pointer matches the expected end pointer before and after we copy
1790 * the data, then the right page must have been present during the data
1791 * copy. Read barriers are necessary to ensure that the data copy actually
1792 * happens between the two verification steps.
1793 *
1794 * If either verification fails, we simply terminate the loop and return
1795 * with the data that had been already copied out successfully.
1796 */
1797 while (nbytes > 0)
1798 {
1799 uint32 offset = recptr % XLOG_BLCKSZ;
1800 int idx = XLogRecPtrToBufIdx(recptr);
1801 XLogRecPtr expectedEndPtr;
1802 XLogRecPtr endptr;
1803 const char *page;
1804 const char *psrc;
1805 Size npagebytes;
1806
1807 /*
1808 * Calculate the end pointer we expect in the xlblocks array if the
1809 * correct page is present.
1810 */
1811 expectedEndPtr = recptr + (XLOG_BLCKSZ - offset);
1812
1813 /*
1814 * First verification step: check that the correct page is present in
1815 * the WAL buffers.
1816 */
1818 if (expectedEndPtr != endptr)
1819 break;
1820
1821 /*
1822 * The correct page is present (or was at the time the endptr was
1823 * read; must re-verify later). Calculate pointer to source data and
1824 * determine how much data to read from this page.
1825 */
1826 page = XLogCtl->pages + idx * (Size) XLOG_BLCKSZ;
1827 psrc = page + offset;
1828 npagebytes = Min(nbytes, XLOG_BLCKSZ - offset);
1829
1830 /*
1831 * Ensure that the data copy and the first verification step are not
1832 * reordered.
1833 */
1835
1836 /* data copy */
1837 memcpy(pdst, psrc, npagebytes);
1838
1839 /*
1840 * Ensure that the data copy and the second verification step are not
1841 * reordered.
1842 */
1844
1845 /*
1846 * Second verification step: check that the page we read from wasn't
1847 * evicted while we were copying the data.
1848 */
1850 if (expectedEndPtr != endptr)
1851 break;
1852
1853 pdst += npagebytes;
1854 recptr += npagebytes;
1855 nbytes -= npagebytes;
1856 }
1857
1858 Assert(pdst - dstbuf <= count);
1859
1860 return pdst - dstbuf;
1861}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
#define pg_read_barrier()
Definition: atomics.h:156
#define Min(x, y)
Definition: c.h:975
size_t Size
Definition: c.h:576
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6708

References Assert(), ereport, errmsg(), ERROR, GetWALInsertionTimeLine(), idx(), XLogCtlData::logInsertResult, LSN_FORMAT_ARGS, Min, XLogCtlData::pages, pg_atomic_read_u64(), pg_read_barrier, RecoveryInProgress(), XLogCtlData::xlblocks, XLogCtl, XLogRecPtrIsInvalid, and XLogRecPtrToBufIdx.

Referenced by XLogSendPhysical().

◆ xlog_desc()

void xlog_desc ( StringInfo  buf,
struct XLogReaderState record 
)

Definition at line 58 of file xlogdesc.c.

59{
60 char *rec = XLogRecGetData(record);
61 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
62
63 if (info == XLOG_CHECKPOINT_SHUTDOWN ||
65 {
66 CheckPoint *checkpoint = (CheckPoint *) rec;
67
68 appendStringInfo(buf, "redo %X/%X; "
69 "tli %u; prev tli %u; fpw %s; wal_level %s; xid %u:%u; oid %u; multi %u; offset %u; "
70 "oldest xid %u in DB %u; oldest multi %u in DB %u; "
71 "oldest/newest commit timestamp xid: %u/%u; "
72 "oldest running xid %u; %s",
73 LSN_FORMAT_ARGS(checkpoint->redo),
74 checkpoint->ThisTimeLineID,
75 checkpoint->PrevTimeLineID,
76 checkpoint->fullPageWrites ? "true" : "false",
80 checkpoint->nextOid,
81 checkpoint->nextMulti,
82 checkpoint->nextMultiOffset,
83 checkpoint->oldestXid,
84 checkpoint->oldestXidDB,
85 checkpoint->oldestMulti,
86 checkpoint->oldestMultiDB,
87 checkpoint->oldestCommitTsXid,
88 checkpoint->newestCommitTsXid,
89 checkpoint->oldestActiveXid,
90 (info == XLOG_CHECKPOINT_SHUTDOWN) ? "shutdown" : "online");
91 }
92 else if (info == XLOG_NEXTOID)
93 {
94 Oid nextOid;
95
96 memcpy(&nextOid, rec, sizeof(Oid));
97 appendStringInfo(buf, "%u", nextOid);
98 }
99 else if (info == XLOG_RESTORE_POINT)
100 {
101 xl_restore_point *xlrec = (xl_restore_point *) rec;
102
104 }
105 else if (info == XLOG_FPI || info == XLOG_FPI_FOR_HINT)
106 {
107 /* no further information to print */
108 }
109 else if (info == XLOG_BACKUP_END)
110 {
111 XLogRecPtr startpoint;
112
113 memcpy(&startpoint, rec, sizeof(XLogRecPtr));
114 appendStringInfo(buf, "%X/%X", LSN_FORMAT_ARGS(startpoint));
115 }
116 else if (info == XLOG_PARAMETER_CHANGE)
117 {
119 const char *wal_level_str;
120
121 memcpy(&xlrec, rec, sizeof(xl_parameter_change));
123
124 appendStringInfo(buf, "max_connections=%d max_worker_processes=%d "
125 "max_wal_senders=%d max_prepared_xacts=%d "
126 "max_locks_per_xact=%d wal_level=%s "
127 "wal_log_hints=%s track_commit_timestamp=%s",
128 xlrec.MaxConnections,
130 xlrec.max_wal_senders,
131 xlrec.max_prepared_xacts,
132 xlrec.max_locks_per_xact,
134 xlrec.wal_log_hints ? "on" : "off",
135 xlrec.track_commit_timestamp ? "on" : "off");
136 }
137 else if (info == XLOG_FPW_CHANGE)
138 {
139 bool fpw;
140
141 memcpy(&fpw, rec, sizeof(bool));
142 appendStringInfoString(buf, fpw ? "true" : "false");
143 }
144 else if (info == XLOG_END_OF_RECOVERY)
145 {
146 xl_end_of_recovery xlrec;
147
148 memcpy(&xlrec, rec, sizeof(xl_end_of_recovery));
149 appendStringInfo(buf, "tli %u; prev tli %u; time %s; wal_level %s",
150 xlrec.ThisTimeLineID, xlrec.PrevTimeLineID,
153 }
154 else if (info == XLOG_OVERWRITE_CONTRECORD)
155 {
157
158 memcpy(&xlrec, rec, sizeof(xl_overwrite_contrecord));
159 appendStringInfo(buf, "lsn %X/%X; time %s",
162 }
163 else if (info == XLOG_CHECKPOINT_REDO)
164 {
165 int wal_level;
166
167 memcpy(&wal_level, rec, sizeof(int));
169 }
170}
static const char * wal_level_str(WalLevel wal_level)
uint8_t uint8
Definition: c.h:500
#define XLOG_RESTORE_POINT
Definition: pg_control.h:75
#define XLOG_OVERWRITE_CONTRECORD
Definition: pg_control.h:81
#define XLOG_FPI
Definition: pg_control.h:79
#define XLOG_FPI_FOR_HINT
Definition: pg_control.h:78
#define XLOG_NEXTOID
Definition: pg_control.h:71
#define XLOG_PARAMETER_CHANGE
Definition: pg_control.h:74
#define XLOG_END_OF_RECOVERY
Definition: pg_control.h:77
static char * buf
Definition: pg_test_fsync.c:72
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
TimeLineID PrevTimeLineID
TimestampTz end_time
TimeLineID ThisTimeLineID
char rp_name[MAXFNAMELEN]
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
static const char * get_wal_level_string(int wal_level)
Definition: xlogdesc.c:40
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415

References appendStringInfo(), appendStringInfoString(), buf, xl_end_of_recovery::end_time, EpochFromFullTransactionId, CheckPoint::fullPageWrites, get_wal_level_string(), LSN_FORMAT_ARGS, xl_parameter_change::max_locks_per_xact, xl_parameter_change::max_prepared_xacts, xl_parameter_change::max_wal_senders, xl_parameter_change::max_worker_processes, xl_parameter_change::MaxConnections, CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, CheckPoint::nextXid, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, CheckPoint::oldestXid, CheckPoint::oldestXidDB, xl_overwrite_contrecord::overwrite_time, xl_overwrite_contrecord::overwritten_lsn, xl_end_of_recovery::PrevTimeLineID, CheckPoint::PrevTimeLineID, CheckPoint::redo, xl_restore_point::rp_name, xl_end_of_recovery::ThisTimeLineID, CheckPoint::ThisTimeLineID, timestamptz_to_str(), xl_parameter_change::track_commit_timestamp, wal_level, xl_parameter_change::wal_level, xl_end_of_recovery::wal_level, CheckPoint::wal_level, wal_level_str(), xl_parameter_change::wal_log_hints, XidFromFullTransactionId, XLOG_BACKUP_END, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_REDO, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, XLOG_FPI, XLOG_FPI_FOR_HINT, XLOG_FPW_CHANGE, XLOG_NEXTOID, XLOG_OVERWRITE_CONTRECORD, XLOG_PARAMETER_CHANGE, XLOG_RESTORE_POINT, XLogRecGetData, and XLogRecGetInfo.

◆ xlog_identify()

const char * xlog_identify ( uint8  info)

Definition at line 173 of file xlogdesc.c.

174{
175 const char *id = NULL;
176
177 switch (info & ~XLR_INFO_MASK)
178 {
180 id = "CHECKPOINT_SHUTDOWN";
181 break;
183 id = "CHECKPOINT_ONLINE";
184 break;
185 case XLOG_NOOP:
186 id = "NOOP";
187 break;
188 case XLOG_NEXTOID:
189 id = "NEXTOID";
190 break;
191 case XLOG_SWITCH:
192 id = "SWITCH";
193 break;
194 case XLOG_BACKUP_END:
195 id = "BACKUP_END";
196 break;
198 id = "PARAMETER_CHANGE";
199 break;
201 id = "RESTORE_POINT";
202 break;
203 case XLOG_FPW_CHANGE:
204 id = "FPW_CHANGE";
205 break;
207 id = "END_OF_RECOVERY";
208 break;
210 id = "OVERWRITE_CONTRECORD";
211 break;
212 case XLOG_FPI:
213 id = "FPI";
214 break;
216 id = "FPI_FOR_HINT";
217 break;
219 id = "CHECKPOINT_REDO";
220 break;
221 }
222
223 return id;
224}
#define XLOG_NOOP
Definition: pg_control.h:70
#define XLOG_SWITCH
Definition: pg_control.h:72
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

References XLOG_BACKUP_END, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_REDO, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, XLOG_FPI, XLOG_FPI_FOR_HINT, XLOG_FPW_CHANGE, XLOG_NEXTOID, XLOG_NOOP, XLOG_OVERWRITE_CONTRECORD, XLOG_PARAMETER_CHANGE, XLOG_RESTORE_POINT, XLOG_SWITCH, and XLR_INFO_MASK.

◆ xlog_redo()

void xlog_redo ( struct XLogReaderState record)

Definition at line 8423 of file xlog.c.

8424{
8425 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
8426 XLogRecPtr lsn = record->EndRecPtr;
8427
8428 /*
8429 * In XLOG rmgr, backup blocks are only used by XLOG_FPI and
8430 * XLOG_FPI_FOR_HINT records.
8431 */
8432 Assert(info == XLOG_FPI || info == XLOG_FPI_FOR_HINT ||
8433 !XLogRecHasAnyBlockRefs(record));
8434
8435 if (info == XLOG_NEXTOID)
8436 {
8437 Oid nextOid;
8438
8439 /*
8440 * We used to try to take the maximum of TransamVariables->nextOid and
8441 * the recorded nextOid, but that fails if the OID counter wraps
8442 * around. Since no OID allocation should be happening during replay
8443 * anyway, better to just believe the record exactly. We still take
8444 * OidGenLock while setting the variable, just in case.
8445 */
8446 memcpy(&nextOid, XLogRecGetData(record), sizeof(Oid));
8447 LWLockAcquire(OidGenLock, LW_EXCLUSIVE);
8448 TransamVariables->nextOid = nextOid;
8450 LWLockRelease(OidGenLock);
8451 }
8452 else if (info == XLOG_CHECKPOINT_SHUTDOWN)
8453 {
8454 CheckPoint checkPoint;
8455 TimeLineID replayTLI;
8456
8457 memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
8458 /* In a SHUTDOWN checkpoint, believe the counters exactly */
8459 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
8460 TransamVariables->nextXid = checkPoint.nextXid;
8461 LWLockRelease(XidGenLock);
8462 LWLockAcquire(OidGenLock, LW_EXCLUSIVE);
8463 TransamVariables->nextOid = checkPoint.nextOid;
8465 LWLockRelease(OidGenLock);
8467 checkPoint.nextMultiOffset);
8468
8470 checkPoint.oldestMultiDB);
8471
8472 /*
8473 * No need to set oldestClogXid here as well; it'll be set when we
8474 * redo an xl_clog_truncate if it changed since initialization.
8475 */
8476 SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
8477
8478 /*
8479 * If we see a shutdown checkpoint while waiting for an end-of-backup
8480 * record, the backup was canceled and the end-of-backup record will
8481 * never arrive.
8482 */
8486 ereport(PANIC,
8487 (errmsg("online backup was canceled, recovery cannot continue")));
8488
8489 /*
8490 * If we see a shutdown checkpoint, we know that nothing was running
8491 * on the primary at this point. So fake-up an empty running-xacts
8492 * record and use that here and now. Recover additional standby state
8493 * for prepared transactions.
8494 */
8496 {
8497 TransactionId *xids;
8498 int nxids;
8499 TransactionId oldestActiveXID;
8500 TransactionId latestCompletedXid;
8502
8503 oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
8504
8505 /* Update pg_subtrans entries for any prepared transactions */
8507
8508 /*
8509 * Construct a RunningTransactions snapshot representing a shut
8510 * down server, with only prepared transactions still alive. We're
8511 * never overflowed at this point because all subxids are listed
8512 * with their parent prepared transactions.
8513 */
8514 running.xcnt = nxids;
8515 running.subxcnt = 0;
8517 running.nextXid = XidFromFullTransactionId(checkPoint.nextXid);
8518 running.oldestRunningXid = oldestActiveXID;
8519 latestCompletedXid = XidFromFullTransactionId(checkPoint.nextXid);
8520 TransactionIdRetreat(latestCompletedXid);
8521 Assert(TransactionIdIsNormal(latestCompletedXid));
8522 running.latestCompletedXid = latestCompletedXid;
8523 running.xids = xids;
8524
8526 }
8527
8528 /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
8529 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8531 LWLockRelease(ControlFileLock);
8532
8533 /* Update shared-memory copy of checkpoint XID/epoch */
8535 XLogCtl->ckptFullXid = checkPoint.nextXid;
8537
8538 /*
8539 * We should've already switched to the new TLI before replaying this
8540 * record.
8541 */
8542 (void) GetCurrentReplayRecPtr(&replayTLI);
8543 if (checkPoint.ThisTimeLineID != replayTLI)
8544 ereport(PANIC,
8545 (errmsg("unexpected timeline ID %u (should be %u) in shutdown checkpoint record",
8546 checkPoint.ThisTimeLineID, replayTLI)));
8547
8548 RecoveryRestartPoint(&checkPoint, record);
8549 }
8550 else if (info == XLOG_CHECKPOINT_ONLINE)
8551 {
8552 CheckPoint checkPoint;
8553 TimeLineID replayTLI;
8554
8555 memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
8556 /* In an ONLINE checkpoint, treat the XID counter as a minimum */
8557 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
8559 checkPoint.nextXid))
8560 TransamVariables->nextXid = checkPoint.nextXid;
8561 LWLockRelease(XidGenLock);
8562
8563 /*
8564 * We ignore the nextOid counter in an ONLINE checkpoint, preferring
8565 * to track OID assignment through XLOG_NEXTOID records. The nextOid
8566 * counter is from the start of the checkpoint and might well be stale
8567 * compared to later XLOG_NEXTOID records. We could try to take the
8568 * maximum of the nextOid counter and our latest value, but since
8569 * there's no particular guarantee about the speed with which the OID
8570 * counter wraps around, that's a risky thing to do. In any case,
8571 * users of the nextOid counter are required to avoid assignment of
8572 * duplicates, so that a somewhat out-of-date value should be safe.
8573 */
8574
8575 /* Handle multixact */
8577 checkPoint.nextMultiOffset);
8578
8579 /*
8580 * NB: This may perform multixact truncation when replaying WAL
8581 * generated by an older primary.
8582 */
8584 checkPoint.oldestMultiDB);
8586 checkPoint.oldestXid))
8588 checkPoint.oldestXidDB);
8589 /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
8590 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8592 LWLockRelease(ControlFileLock);
8593
8594 /* Update shared-memory copy of checkpoint XID/epoch */
8596 XLogCtl->ckptFullXid = checkPoint.nextXid;
8598
8599 /* TLI should not change in an on-line checkpoint */
8600 (void) GetCurrentReplayRecPtr(&replayTLI);
8601 if (checkPoint.ThisTimeLineID != replayTLI)
8602 ereport(PANIC,
8603 (errmsg("unexpected timeline ID %u (should be %u) in online checkpoint record",
8604 checkPoint.ThisTimeLineID, replayTLI)));
8605
8606 RecoveryRestartPoint(&checkPoint, record);
8607 }
8608 else if (info == XLOG_OVERWRITE_CONTRECORD)
8609 {
8610 /* nothing to do here, handled in xlogrecovery_redo() */
8611 }
8612 else if (info == XLOG_END_OF_RECOVERY)
8613 {
8614 xl_end_of_recovery xlrec;
8615 TimeLineID replayTLI;
8616
8617 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_end_of_recovery));
8618
8619 /*
8620 * For Hot Standby, we could treat this like a Shutdown Checkpoint,
8621 * but this case is rarer and harder to test, so the benefit doesn't
8622 * outweigh the potential extra cost of maintenance.
8623 */
8624
8625 /*
8626 * We should've already switched to the new TLI before replaying this
8627 * record.
8628 */
8629 (void) GetCurrentReplayRecPtr(&replayTLI);
8630 if (xlrec.ThisTimeLineID != replayTLI)
8631 ereport(PANIC,
8632 (errmsg("unexpected timeline ID %u (should be %u) in end-of-recovery record",
8633 xlrec.ThisTimeLineID, replayTLI)));
8634 }
8635 else if (info == XLOG_NOOP)
8636 {
8637 /* nothing to do here */
8638 }
8639 else if (info == XLOG_SWITCH)
8640 {
8641 /* nothing to do here */
8642 }
8643 else if (info == XLOG_RESTORE_POINT)
8644 {
8645 /* nothing to do here, handled in xlogrecovery.c */
8646 }
8647 else if (info == XLOG_FPI || info == XLOG_FPI_FOR_HINT)
8648 {
8649 /*
8650 * XLOG_FPI records contain nothing else but one or more block
8651 * references. Every block reference must include a full-page image
8652 * even if full_page_writes was disabled when the record was generated
8653 * - otherwise there would be no point in this record.
8654 *
8655 * XLOG_FPI_FOR_HINT records are generated when a page needs to be
8656 * WAL-logged because of a hint bit update. They are only generated
8657 * when checksums and/or wal_log_hints are enabled. They may include
8658 * no full-page images if full_page_writes was disabled when they were
8659 * generated. In this case there is nothing to do here.
8660 *
8661 * No recovery conflicts are generated by these generic records - if a
8662 * resource manager needs to generate conflicts, it has to define a
8663 * separate WAL record type and redo routine.
8664 */
8665 for (uint8 block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
8666 {
8667 Buffer buffer;
8668
8669 if (!XLogRecHasBlockImage(record, block_id))
8670 {
8671 if (info == XLOG_FPI)
8672 elog(ERROR, "XLOG_FPI record did not contain a full-page image");
8673 continue;
8674 }
8675
8676 if (XLogReadBufferForRedo(record, block_id, &buffer) != BLK_RESTORED)
8677 elog(ERROR, "unexpected XLogReadBufferForRedo result when restoring backup block");
8678 UnlockReleaseBuffer(buffer);
8679 }
8680 }
8681 else if (info == XLOG_BACKUP_END)
8682 {
8683 /* nothing to do here, handled in xlogrecovery_redo() */
8684 }
8685 else if (info == XLOG_PARAMETER_CHANGE)
8686 {
8687 xl_parameter_change xlrec;
8688
8689 /* Update our copy of the parameters in pg_control */
8690 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_parameter_change));
8691
8692 /*
8693 * Invalidate logical slots if we are in hot standby and the primary
8694 * does not have a WAL level sufficient for logical decoding. No need
8695 * to search for potentially conflicting logically slots if standby is
8696 * running with wal_level lower than logical, because in that case, we
8697 * would have either disallowed creation of logical slots or
8698 * invalidated existing ones.
8699 */
8700 if (InRecovery && InHotStandby &&
8701 xlrec.wal_level < WAL_LEVEL_LOGICAL &&
8704 0, InvalidOid,
8706
8707 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
8715
8716 /*
8717 * Update minRecoveryPoint to ensure that if recovery is aborted, we
8718 * recover back up to this point before allowing hot standby again.
8719 * This is important if the max_* settings are decreased, to ensure
8720 * you don't run queries against the WAL preceding the change. The
8721 * local copies cannot be updated as long as crash recovery is
8722 * happening and we expect all the WAL to be replayed.
8723 */
8725 {
8728 }
8730 {
8731 TimeLineID replayTLI;
8732
8733 (void) GetCurrentReplayRecPtr(&replayTLI);
8735 ControlFile->minRecoveryPointTLI = replayTLI;
8736 }
8737
8741
8743 LWLockRelease(ControlFileLock);
8744
8745 /* Check to see if any parameter change gives a problem on recovery */
8747 }
8748 else if (info == XLOG_FPW_CHANGE)
8749 {
8750 bool fpw;
8751
8752 memcpy(&fpw, XLogRecGetData(record), sizeof(bool));
8753
8754 /*
8755 * Update the LSN of the last replayed XLOG_FPW_CHANGE record so that
8756 * do_pg_backup_start() and do_pg_backup_stop() can check whether
8757 * full_page_writes has been disabled during online backup.
8758 */
8759 if (!fpw)
8760 {
8765 }
8766
8767 /* Keep track of full_page_writes */
8768 lastFullPageWrites = fpw;
8769 }
8770 else if (info == XLOG_CHECKPOINT_REDO)
8771 {
8772 /* nothing to do here, just for informational purposes */
8773 }
8774}
int Buffer
Definition: buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5320
void CommitTsParameterChange(bool newvalue, bool oldvalue)
Definition: commit_ts.c:664
void MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB)
Definition: multixact.c:2536
void MultiXactAdvanceNextMXact(MultiXactId minMulti, MultiXactOffset minMultiOffset)
Definition: multixact.c:2511
@ RS_INVAL_WAL_LEVEL
Definition: slot.h:59
int max_worker_processes
Definition: pg_control.h:181
int max_locks_per_xact
Definition: pg_control.h:184
int max_prepared_xacts
Definition: pg_control.h:183
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:206
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51
static void RecoveryRestartPoint(const CheckPoint *checkPoint, XLogReaderState *record)
Definition: xlog.c:7738
#define XLogRecMaxBlockId(decoder)
Definition: xlogreader.h:418
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:423
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:417
XLogRecPtr GetCurrentReplayRecPtr(TimeLineID *replayEndTLI)
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:303
@ STANDBY_INITIALIZED
Definition: xlogutils.h:53
#define InHotStandby
Definition: xlogutils.h:60
@ BLK_RESTORED
Definition: xlogutils.h:76

References ArchiveRecoveryRequested, Assert(), ControlFileData::backupEndPoint, ControlFileData::backupStartPoint, BLK_RESTORED, ControlFileData::checkPointCopy, CheckRequiredParameterValues(), XLogCtlData::ckptFullXid, CommitTsParameterChange(), ControlFile, elog, XLogReaderState::EndRecPtr, ereport, errmsg(), ERROR, FullTransactionIdPrecedes, GetCurrentReplayRecPtr(), InArchiveRecovery, XLogCtlData::info_lck, InHotStandby, InRecovery, InvalidateObsoleteReplicationSlots(), InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, XLogCtlData::lastFpwDisableRecPtr, lastFullPageWrites, RunningTransactionsData::latestCompletedXid, LocalMinRecoveryPoint, LocalMinRecoveryPointTLI, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), xl_parameter_change::max_locks_per_xact, ControlFileData::max_locks_per_xact, xl_parameter_change::max_prepared_xacts, ControlFileData::max_prepared_xacts, xl_parameter_change::max_wal_senders, ControlFileData::max_wal_senders, xl_parameter_change::max_worker_processes, ControlFileData::max_worker_processes, xl_parameter_change::MaxConnections, ControlFileData::MaxConnections, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, MultiXactAdvanceNextMXact(), MultiXactAdvanceOldest(), MultiXactSetNextMXact(), CheckPoint::nextMulti, CheckPoint::nextMultiOffset, TransamVariablesData::nextOid, CheckPoint::nextOid, TransamVariablesData::nextXid, CheckPoint::nextXid, RunningTransactionsData::nextXid, TransamVariablesData::oidCount, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, RunningTransactionsData::oldestRunningXid, TransamVariablesData::oldestXid, CheckPoint::oldestXid, CheckPoint::oldestXidDB, PANIC, PrescanPreparedTransactions(), ProcArrayApplyRecoveryInfo(), XLogReaderState::ReadRecPtr, RecoveryRestartPoint(), RS_INVAL_WAL_LEVEL, SetTransactionIdLimit(), SpinLockAcquire, SpinLockRelease, STANDBY_INITIALIZED, StandbyRecoverPreparedTransactions(), standbyState, RunningTransactionsData::subxcnt, RunningTransactionsData::subxid_status, SUBXIDS_IN_SUBTRANS, xl_end_of_recovery::ThisTimeLineID, CheckPoint::ThisTimeLineID, xl_parameter_change::track_commit_timestamp, ControlFileData::track_commit_timestamp, TransactionIdIsNormal, TransactionIdPrecedes(), TransactionIdRetreat, TransamVariables, UnlockReleaseBuffer(), UpdateControlFile(), wal_level, xl_parameter_change::wal_level, ControlFileData::wal_level, WAL_LEVEL_LOGICAL, xl_parameter_change::wal_log_hints, ControlFileData::wal_log_hints, RunningTransactionsData::xcnt, XidFromFullTransactionId, RunningTransactionsData::xids, XLOG_BACKUP_END, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_REDO, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, XLOG_FPI, XLOG_FPI_FOR_HINT, XLOG_FPW_CHANGE, XLOG_NEXTOID, XLOG_NOOP, XLOG_OVERWRITE_CONTRECORD, XLOG_PARAMETER_CHANGE, XLOG_RESTORE_POINT, XLOG_SWITCH, XLogCtl, XLogReadBufferForRedo(), XLogRecGetData, XLogRecGetInfo, XLogRecHasAnyBlockRefs, XLogRecHasBlockImage, XLogRecMaxBlockId, and XLogRecPtrIsInvalid.

◆ XLogBackgroundFlush()

bool XLogBackgroundFlush ( void  )

Definition at line 3111 of file xlog.c.

3112{
3113 XLogwrtRqst WriteRqst;
3114 bool flexible = true;
3115 static TimestampTz lastflush;
3117 int flushblocks;
3118 TimeLineID insertTLI;
3119
3120 /* XLOG doesn't need flushing during recovery */
3121 if (RecoveryInProgress())
3122 return false;
3123
3124 /*
3125 * Since we're not in recovery, InsertTimeLineID is set and can't change,
3126 * so we can read it without a lock.
3127 */
3128 insertTLI = XLogCtl->InsertTimeLineID;
3129
3130 /* read updated LogwrtRqst */
3132 WriteRqst = XLogCtl->LogwrtRqst;
3134
3135 /* back off to last completed page boundary */
3136 WriteRqst.Write -= WriteRqst.Write % XLOG_BLCKSZ;
3137
3138 /* if we have already flushed that far, consider async commit records */
3140 if (WriteRqst.Write <= LogwrtResult.Flush)
3141 {
3143 WriteRqst.Write = XLogCtl->asyncXactLSN;
3145 flexible = false; /* ensure it all gets written */
3146 }
3147
3148 /*
3149 * If already known flushed, we're done. Just need to check if we are
3150 * holding an open file handle to a logfile that's no longer in use,
3151 * preventing the file from being deleted.
3152 */
3153 if (WriteRqst.Write <= LogwrtResult.Flush)
3154 {
3155 if (openLogFile >= 0)
3156 {
3159 {
3160 XLogFileClose();
3161 }
3162 }
3163 return false;
3164 }
3165
3166 /*
3167 * Determine how far to flush WAL, based on the wal_writer_delay and
3168 * wal_writer_flush_after GUCs.
3169 *
3170 * Note that XLogSetAsyncXactLSN() performs similar calculation based on
3171 * wal_writer_flush_after, to decide when to wake us up. Make sure the
3172 * logic is the same in both places if you change this.
3173 */
3175 flushblocks =
3176 WriteRqst.Write / XLOG_BLCKSZ - LogwrtResult.Flush / XLOG_BLCKSZ;
3177
3178 if (WalWriterFlushAfter == 0 || lastflush == 0)
3179 {
3180 /* first call, or block based limits disabled */
3181 WriteRqst.Flush = WriteRqst.Write;
3182 lastflush = now;
3183 }
3184 else if (TimestampDifferenceExceeds(lastflush, now, WalWriterDelay))
3185 {
3186 /*
3187 * Flush the writes at least every WalWriterDelay ms. This is
3188 * important to bound the amount of time it takes for an asynchronous
3189 * commit to hit disk.
3190 */
3191 WriteRqst.Flush = WriteRqst.Write;
3192 lastflush = now;
3193 }
3194 else if (flushblocks >= WalWriterFlushAfter)
3195 {
3196 /* exceeded wal_writer_flush_after blocks, flush */
3197 WriteRqst.Flush = WriteRqst.Write;
3198 lastflush = now;
3199 }
3200 else
3201 {
3202 /* no flushing, this time round */
3203 WriteRqst.Flush = 0;
3204 }
3205
3206#ifdef WAL_DEBUG
3207 if (XLOG_DEBUG)
3208 elog(LOG, "xlog bg flush request write %X/%X; flush: %X/%X, current is write %X/%X; flush %X/%X",
3209 LSN_FORMAT_ARGS(WriteRqst.Write),
3210 LSN_FORMAT_ARGS(WriteRqst.Flush),
3213#endif
3214
3216
3217 /* now wait for any in-progress insertions to finish and get write lock */
3219 LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
3221 if (WriteRqst.Write > LogwrtResult.Write ||
3222 WriteRqst.Flush > LogwrtResult.Flush)
3223 {
3224 XLogWrite(WriteRqst, insertTLI, flexible);
3225 }
3226 LWLockRelease(WALWriteLock);
3227
3229
3230 /* wake up walsenders now that we've released heavily contended locks */
3232
3233 /*
3234 * Great, done. To take some work off the critical path, try to initialize
3235 * as many of the no-longer-needed WAL buffers for future use as we can.
3236 */
3237 AdvanceXLInsertBuffer(InvalidXLogRecPtr, insertTLI, true);
3238
3239 /*
3240 * If we determined that we need to write data, but somebody else
3241 * wrote/flushed already, it should be considered as being active, to
3242 * avoid hibernating too early.
3243 */
3244 return true;
3245}
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1781
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
XLogRecPtr asyncXactLSN
Definition: xlog.c:453
static void WalSndWakeupProcessRequests(bool physical, bool logical)
Definition: walsender.h:65
int WalWriterFlushAfter
Definition: walwriter.c:71
int WalWriterDelay
Definition: walwriter.c:70
static XLogRecPtr WaitXLogInsertionsToFinish(XLogRecPtr upto)
Definition: xlog.c:1515
static void AdvanceXLInsertBuffer(XLogRecPtr upto, TimeLineID tli, bool opportunistic)
Definition: xlog.c:1996
static void XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Definition: xlog.c:2447
static void XLogFileClose(void)
Definition: xlog.c:3778
static XLogSegNo openLogSegNo
Definition: xlog.c:647
#define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes)

References AdvanceXLInsertBuffer(), XLogCtlData::asyncXactLSN, elog, END_CRIT_SECTION, XLogwrtRqst::Flush, XLogwrtResult::Flush, GetCurrentTimestamp(), XLogCtlData::info_lck, XLogCtlData::InsertTimeLineID, InvalidXLogRecPtr, LOG, LogwrtResult, XLogCtlData::LogwrtRqst, LSN_FORMAT_ARGS, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), now(), openLogFile, openLogSegNo, RecoveryInProgress(), RefreshXLogWriteResult, SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, TimestampDifferenceExceeds(), WaitXLogInsertionsToFinish(), wal_segment_size, WalSndWakeupProcessRequests(), WalWriterDelay, WalWriterFlushAfter, XLogwrtRqst::Write, XLogwrtResult::Write, XLByteInPrevSeg, XLogCtl, XLogFileClose(), and XLogWrite().

Referenced by WalSndWaitForWal(), and WalWriterMain().

◆ XLogCheckpointNeeded()

bool XLogCheckpointNeeded ( XLogSegNo  new_segno)

Definition at line 2423 of file xlog.c.

2424{
2425 XLogSegNo old_segno;
2426
2428
2429 if (new_segno >= old_segno + (uint64) (CheckPointSegments - 1))
2430 return true;
2431 return false;
2432}
int CheckPointSegments
Definition: xlog.c:156

References CheckPointSegments, RedoRecPtr, wal_segment_size, and XLByteToSeg.

Referenced by XLogPageRead(), and XLogWrite().

◆ XLogFileInit()

int XLogFileInit ( XLogSegNo  logsegno,
TimeLineID  logtli 
)

Definition at line 3519 of file xlog.c.

3520{
3521 bool ignore_added;
3522 char path[MAXPGPATH];
3523 int fd;
3524
3525 Assert(logtli != 0);
3526
3527 fd = XLogFileInitInternal(logsegno, logtli, &ignore_added, path);
3528 if (fd >= 0)
3529 return fd;
3530
3531 /* Now open original target segment (might not be file I just made) */
3532 fd = BasicOpenFile(path, O_RDWR | PG_BINARY | O_CLOEXEC |
3534 if (fd < 0)
3535 ereport(ERROR,
3537 errmsg("could not open file \"%s\": %m", path)));
3538 return fd;
3539}
#define PG_BINARY
Definition: c.h:1244
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1089
#define O_CLOEXEC
Definition: win32_port.h:349
static int get_sync_bit(int method)
Definition: xlog.c:8781
static int XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli, bool *added, char *path)
Definition: xlog.c:3331

References Assert(), BasicOpenFile(), ereport, errcode_for_file_access(), errmsg(), ERROR, fd(), get_sync_bit(), MAXPGPATH, O_CLOEXEC, PG_BINARY, wal_sync_method, and XLogFileInitInternal().

Referenced by BootStrapXLOG(), XLogInitNewTimeline(), XLogWalRcvWrite(), and XLogWrite().

◆ XLogFileOpen()

int XLogFileOpen ( XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 3757 of file xlog.c.

3758{
3759 char path[MAXPGPATH];
3760 int fd;
3761
3762 XLogFilePath(path, tli, segno, wal_segment_size);
3763
3764 fd = BasicOpenFile(path, O_RDWR | PG_BINARY | O_CLOEXEC |
3766 if (fd < 0)
3767 ereport(PANIC,
3769 errmsg("could not open file \"%s\": %m", path)));
3770
3771 return fd;
3772}
static void XLogFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)

References BasicOpenFile(), ereport, errcode_for_file_access(), errmsg(), fd(), get_sync_bit(), MAXPGPATH, O_CLOEXEC, PANIC, PG_BINARY, wal_segment_size, wal_sync_method, and XLogFilePath().

Referenced by XLogWrite().

◆ XLogFlush()

void XLogFlush ( XLogRecPtr  record)

Definition at line 2923 of file xlog.c.

2924{
2925 XLogRecPtr WriteRqstPtr;
2926 XLogwrtRqst WriteRqst;
2927 TimeLineID insertTLI = XLogCtl->InsertTimeLineID;
2928
2929 /*
2930 * During REDO, we are reading not writing WAL. Therefore, instead of
2931 * trying to flush the WAL, we should update minRecoveryPoint instead. We
2932 * test XLogInsertAllowed(), not InRecovery, because we need checkpointer
2933 * to act this way too, and because when it tries to write the
2934 * end-of-recovery checkpoint, it should indeed flush.
2935 */
2936 if (!XLogInsertAllowed())
2937 {
2938 UpdateMinRecoveryPoint(record, false);
2939 return;
2940 }
2941
2942 /* Quick exit if already known flushed */
2943 if (record <= LogwrtResult.Flush)
2944 return;
2945
2946#ifdef WAL_DEBUG
2947 if (XLOG_DEBUG)
2948 elog(LOG, "xlog flush request %X/%X; write %X/%X; flush %X/%X",
2949 LSN_FORMAT_ARGS(record),
2952#endif
2953
2955
2956 /*
2957 * Since fsync is usually a horribly expensive operation, we try to
2958 * piggyback as much data as we can on each fsync: if we see any more data
2959 * entered into the xlog buffer, we'll write and fsync that too, so that
2960 * the final value of LogwrtResult.Flush is as large as possible. This
2961 * gives us some chance of avoiding another fsync immediately after.
2962 */
2963
2964 /* initialize to given target; may increase below */
2965 WriteRqstPtr = record;
2966
2967 /*
2968 * Now wait until we get the write lock, or someone else does the flush
2969 * for us.
2970 */
2971 for (;;)
2972 {
2973 XLogRecPtr insertpos;
2974
2975 /* done already? */
2977 if (record <= LogwrtResult.Flush)
2978 break;
2979
2980 /*
2981 * Before actually performing the write, wait for all in-flight
2982 * insertions to the pages we're about to write to finish.
2983 */
2985 if (WriteRqstPtr < XLogCtl->LogwrtRqst.Write)
2986 WriteRqstPtr = XLogCtl->LogwrtRqst.Write;
2988 insertpos = WaitXLogInsertionsToFinish(WriteRqstPtr);
2989
2990 /*
2991 * Try to get the write lock. If we can't get it immediately, wait
2992 * until it's released, and recheck if we still need to do the flush
2993 * or if the backend that held the lock did it for us already. This
2994 * helps to maintain a good rate of group committing when the system
2995 * is bottlenecked by the speed of fsyncing.
2996 */
2997 if (!LWLockAcquireOrWait(WALWriteLock, LW_EXCLUSIVE))
2998 {
2999 /*
3000 * The lock is now free, but we didn't acquire it yet. Before we
3001 * do, loop back to check if someone else flushed the record for
3002 * us already.
3003 */
3004 continue;
3005 }
3006
3007 /* Got the lock; recheck whether request is satisfied */
3009 if (record <= LogwrtResult.Flush)
3010 {
3011 LWLockRelease(WALWriteLock);
3012 break;
3013 }
3014
3015 /*
3016 * Sleep before flush! By adding a delay here, we may give further
3017 * backends the opportunity to join the backlog of group commit
3018 * followers; this can significantly improve transaction throughput,
3019 * at the risk of increasing transaction latency.
3020 *
3021 * We do not sleep if enableFsync is not turned on, nor if there are
3022 * fewer than CommitSiblings other backends with active transactions.
3023 */
3024 if (CommitDelay > 0 && enableFsync &&
3026 {
3028
3029 /*
3030 * Re-check how far we can now flush the WAL. It's generally not
3031 * safe to call WaitXLogInsertionsToFinish while holding
3032 * WALWriteLock, because an in-progress insertion might need to
3033 * also grab WALWriteLock to make progress. But we know that all
3034 * the insertions up to insertpos have already finished, because
3035 * that's what the earlier WaitXLogInsertionsToFinish() returned.
3036 * We're only calling it again to allow insertpos to be moved
3037 * further forward, not to actually wait for anyone.
3038 */
3039 insertpos = WaitXLogInsertionsToFinish(insertpos);
3040 }
3041
3042 /* try to write/flush later additions to XLOG as well */
3043 WriteRqst.Write = insertpos;
3044 WriteRqst.Flush = insertpos;
3045
3046 XLogWrite(WriteRqst, insertTLI, false);
3047
3048 LWLockRelease(WALWriteLock);
3049 /* done */
3050 break;
3051 }
3052
3054
3055 /* wake up walsenders now that we've released heavily contended locks */
3057
3058 /*
3059 * If we still haven't flushed to the request point then we have a
3060 * problem; most likely, the requested flush point is past end of XLOG.
3061 * This has been seen to occur when a disk page has a corrupted LSN.
3062 *
3063 * Formerly we treated this as a PANIC condition, but that hurts the
3064 * system's robustness rather than helping it: we do not want to take down
3065 * the whole system due to corruption on one data page. In particular, if
3066 * the bad page is encountered again during recovery then we would be
3067 * unable to restart the database at all! (This scenario actually
3068 * happened in the field several times with 7.1 releases.) As of 8.4, bad
3069 * LSNs encountered during recovery are UpdateMinRecoveryPoint's problem;
3070 * the only time we can reach here during recovery is while flushing the
3071 * end-of-recovery checkpoint record, and we don't expect that to have a
3072 * bad LSN.
3073 *
3074 * Note that for calls from xact.c, the ERROR will be promoted to PANIC
3075 * since xact.c calls this routine inside a critical section. However,
3076 * calls from bufmgr.c are not within critical sections and so we will not
3077 * force a restart for a bad LSN on a data page.
3078 */
3079 if (LogwrtResult.Flush < record)
3080 elog(ERROR,
3081 "xlog flush request %X/%X is not satisfied --- flushed only to %X/%X",
3082 LSN_FORMAT_ARGS(record),
3084}
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1410
bool MinimumActiveBackends(int min)
Definition: procarray.c:3546
int CommitDelay
Definition: xlog.c:132
int CommitSiblings
Definition: xlog.c:133
bool XLogInsertAllowed(void)
Definition: xlog.c:6577

References CommitDelay, CommitSiblings, elog, enableFsync, END_CRIT_SECTION, ERROR, XLogwrtRqst::Flush, XLogwrtResult::Flush, XLogCtlData::info_lck, XLogCtlData::InsertTimeLineID, LOG, LogwrtResult, XLogCtlData::LogwrtRqst, LSN_FORMAT_ARGS, LW_EXCLUSIVE, LWLockAcquireOrWait(), LWLockRelease(), MinimumActiveBackends(), pg_usleep(), RecoveryInProgress(), RefreshXLogWriteResult, SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, UpdateMinRecoveryPoint(), WaitXLogInsertionsToFinish(), WalSndWakeupProcessRequests(), XLogwrtRqst::Write, XLogwrtResult::Write, XLogCtl, XLogInsertAllowed(), and XLogWrite().

Referenced by CheckPointReplicationOrigin(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), dropdb(), EndPrepare(), finish_sync_worker(), FlushBuffer(), LogLogicalMessage(), pg_truncate_visibility_map(), RecordTransactionAbortPrepared(), RecordTransactionCommit(), RecordTransactionCommitPrepared(), RelationTruncate(), ReplicationSlotReserveWal(), replorigin_get_progress(), replorigin_session_get_progress(), SlruPhysicalWritePage(), smgr_redo(), write_relmap_file(), WriteMTruncateXlogRec(), WriteTruncateXlogRec(), xact_redo_abort(), xact_redo_commit(), XLogInsertRecord(), and XLogReportParameters().

◆ XLogGetLastRemovedSegno()

XLogSegNo XLogGetLastRemovedSegno ( void  )

Definition at line 3897 of file xlog.c.

3898{
3899 XLogSegNo lastRemovedSegNo;
3900
3902 lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3904
3905 return lastRemovedSegNo;
3906}

References XLogCtlData::info_lck, XLogCtlData::lastRemovedSegNo, SpinLockAcquire, SpinLockRelease, and XLogCtl.

Referenced by copy_replication_slot(), GetWALAvailability(), ReplicationSlotReserveWal(), and reserve_wal_for_local_slot().

◆ XLogGetOldestSegno()

XLogSegNo XLogGetOldestSegno ( TimeLineID  tli)

Definition at line 3913 of file xlog.c.

3914{
3915 DIR *xldir;
3916 struct dirent *xlde;
3917 XLogSegNo oldest_segno = 0;
3918
3919 xldir = AllocateDir(XLOGDIR);
3920 while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
3921 {
3922 TimeLineID file_tli;
3923 XLogSegNo file_segno;
3924
3925 /* Ignore files that are not XLOG segments. */
3926 if (!IsXLogFileName(xlde->d_name))
3927 continue;
3928
3929 /* Parse filename to get TLI and segno. */
3930 XLogFromFileName(xlde->d_name, &file_tli, &file_segno,
3932
3933 /* Ignore anything that's not from the TLI of interest. */
3934 if (tli != file_tli)
3935 continue;
3936
3937 /* If it's the oldest so far, update oldest_segno. */
3938 if (oldest_segno == 0 || file_segno < oldest_segno)
3939 oldest_segno = file_segno;
3940 }
3941
3942 FreeDir(xldir);
3943 return oldest_segno;
3944}
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)

References AllocateDir(), dirent::d_name, FreeDir(), IsXLogFileName(), ReadDir(), wal_segment_size, XLOGDIR, and XLogFromFileName().

Referenced by GetOldestUnsummarizedLSN(), MaybeRemoveOldWalSummaries(), and reserve_wal_for_local_slot().

◆ XLogInsertAllowed()

bool XLogInsertAllowed ( void  )

Definition at line 6577 of file xlog.c.

6578{
6579 /*
6580 * If value is "unconditionally true" or "unconditionally false", just
6581 * return it. This provides the normal fast path once recovery is known
6582 * done.
6583 */
6584 if (LocalXLogInsertAllowed >= 0)
6585 return (bool) LocalXLogInsertAllowed;
6586
6587 /*
6588 * Else, must check to see if we're still in recovery.
6589 */
6590 if (RecoveryInProgress())
6591 return false;
6592
6593 /*
6594 * On exit from recovery, reset to "unconditionally true", since there is
6595 * no need to keep checking.
6596 */
6598 return true;
6599}

References LocalXLogInsertAllowed, and RecoveryInProgress().

Referenced by XLogBeginInsert(), XLogFlush(), and XLogInsertRecord().

◆ XLogInsertRecord()

XLogRecPtr XLogInsertRecord ( struct XLogRecData rdata,
XLogRecPtr  fpw_lsn,
uint8  flags,
int  num_fpi,
bool  topxid_included 
)

Definition at line 759 of file xlog.c.

764{
766 pg_crc32c rdata_crc;
767 bool inserted;
768 XLogRecord *rechdr = (XLogRecord *) rdata->data;
769 uint8 info = rechdr->xl_info & ~XLR_INFO_MASK;
771 XLogRecPtr StartPos;
772 XLogRecPtr EndPos;
773 bool prevDoPageWrites = doPageWrites;
774 TimeLineID insertTLI;
775
776 /* Does this record type require special handling? */
777 if (unlikely(rechdr->xl_rmid == RM_XLOG_ID))
778 {
779 if (info == XLOG_SWITCH)
781 else if (info == XLOG_CHECKPOINT_REDO)
783 }
784
785 /* we assume that all of the record header is in the first chunk */
786 Assert(rdata->len >= SizeOfXLogRecord);
787
788 /* cross-check on whether we should be here or not */
789 if (!XLogInsertAllowed())
790 elog(ERROR, "cannot make new WAL entries during recovery");
791
792 /*
793 * Given that we're not in recovery, InsertTimeLineID is set and can't
794 * change, so we can read it without a lock.
795 */
796 insertTLI = XLogCtl->InsertTimeLineID;
797
798 /*----------
799 *
800 * We have now done all the preparatory work we can without holding a
801 * lock or modifying shared state. From here on, inserting the new WAL
802 * record to the shared WAL buffer cache is a two-step process:
803 *
804 * 1. Reserve the right amount of space from the WAL. The current head of
805 * reserved space is kept in Insert->CurrBytePos, and is protected by
806 * insertpos_lck.
807 *
808 * 2. Copy the record to the reserved WAL space. This involves finding the
809 * correct WAL buffer containing the reserved space, and copying the
810 * record in place. This can be done concurrently in multiple processes.
811 *
812 * To keep track of which insertions are still in-progress, each concurrent
813 * inserter acquires an insertion lock. In addition to just indicating that
814 * an insertion is in progress, the lock tells others how far the inserter
815 * has progressed. There is a small fixed number of insertion locks,
816 * determined by NUM_XLOGINSERT_LOCKS. When an inserter crosses a page
817 * boundary, it updates the value stored in the lock to the how far it has
818 * inserted, to allow the previous buffer to be flushed.
819 *
820 * Holding onto an insertion lock also protects RedoRecPtr and
821 * fullPageWrites from changing until the insertion is finished.
822 *
823 * Step 2 can usually be done completely in parallel. If the required WAL
824 * page is not initialized yet, you have to go through AdvanceXLInsertBuffer,
825 * which will ensure it is initialized. But the WAL writer tries to do that
826 * ahead of insertions to avoid that from happening in the critical path.
827 *
828 *----------
829 */
831
832 if (likely(class == WALINSERT_NORMAL))
833 {
835
836 /*
837 * Check to see if my copy of RedoRecPtr is out of date. If so, may
838 * have to go back and have the caller recompute everything. This can
839 * only happen just after a checkpoint, so it's better to be slow in
840 * this case and fast otherwise.
841 *
842 * Also check to see if fullPageWrites was just turned on or there's a
843 * running backup (which forces full-page writes); if we weren't
844 * already doing full-page writes then go back and recompute.
845 *
846 * If we aren't doing full-page writes then RedoRecPtr doesn't
847 * actually affect the contents of the XLOG record, so we'll update
848 * our local copy but not force a recomputation. (If doPageWrites was
849 * just turned off, we could recompute the record without full pages,
850 * but we choose not to bother.)
851 */
852 if (RedoRecPtr != Insert->RedoRecPtr)
853 {
854 Assert(RedoRecPtr < Insert->RedoRecPtr);
855 RedoRecPtr = Insert->RedoRecPtr;
856 }
857 doPageWrites = (Insert->fullPageWrites || Insert->runningBackups > 0);
858
859 if (doPageWrites &&
860 (!prevDoPageWrites ||
861 (fpw_lsn != InvalidXLogRecPtr && fpw_lsn <= RedoRecPtr)))
862 {
863 /*
864 * Oops, some buffer now needs to be backed up that the caller
865 * didn't back up. Start over.
866 */
869 return InvalidXLogRecPtr;
870 }
871
872 /*
873 * Reserve space for the record in the WAL. This also sets the xl_prev
874 * pointer.
875 */
876 ReserveXLogInsertLocation(rechdr->xl_tot_len, &StartPos, &EndPos,
877 &rechdr->xl_prev);
878
879 /* Normal records are always inserted. */
880 inserted = true;
881 }
882 else if (class == WALINSERT_SPECIAL_SWITCH)
883 {
884 /*
885 * In order to insert an XLOG_SWITCH record, we need to hold all of
886 * the WAL insertion locks, not just one, so that no one else can
887 * begin inserting a record until we've figured out how much space
888 * remains in the current WAL segment and claimed all of it.
889 *
890 * Nonetheless, this case is simpler than the normal cases handled
891 * below, which must check for changes in doPageWrites and RedoRecPtr.
892 * Those checks are only needed for records that can contain buffer
893 * references, and an XLOG_SWITCH record never does.
894 */
895 Assert(fpw_lsn == InvalidXLogRecPtr);
897 inserted = ReserveXLogSwitch(&StartPos, &EndPos, &rechdr->xl_prev);
898 }
899 else
900 {
902
903 /*
904 * We need to update both the local and shared copies of RedoRecPtr,
905 * which means that we need to hold all the WAL insertion locks.
906 * However, there can't be any buffer references, so as above, we need
907 * not check RedoRecPtr before inserting the record; we just need to
908 * update it afterwards.
909 */
910 Assert(fpw_lsn == InvalidXLogRecPtr);
912 ReserveXLogInsertLocation(rechdr->xl_tot_len, &StartPos, &EndPos,
913 &rechdr->xl_prev);
914 RedoRecPtr = Insert->RedoRecPtr = StartPos;
915 inserted = true;
916 }
917
918 if (inserted)
919 {
920 /*
921 * Now that xl_prev has been filled in, calculate CRC of the record
922 * header.
923 */
924 rdata_crc = rechdr->xl_crc;
925 COMP_CRC32C(rdata_crc, rechdr, offsetof(XLogRecord, xl_crc));
926 FIN_CRC32C(rdata_crc);
927 rechdr->xl_crc = rdata_crc;
928
929 /*
930 * All the record data, including the header, is now ready to be
931 * inserted. Copy the record in the space reserved.
932 */
934 class == WALINSERT_SPECIAL_SWITCH, rdata,
935 StartPos, EndPos, insertTLI);
936
937 /*
938 * Unless record is flagged as not important, update LSN of last
939 * important record in the current slot. When holding all locks, just
940 * update the first one.
941 */
942 if ((flags & XLOG_MARK_UNIMPORTANT) == 0)
943 {
944 int lockno = holdingAllLocks ? 0 : MyLockNo;
945
946 WALInsertLocks[lockno].l.lastImportantAt = StartPos;
947 }
948 }
949 else
950 {
951 /*
952 * This was an xlog-switch record, but the current insert location was
953 * already exactly at the beginning of a segment, so there was no need
954 * to do anything.
955 */
956 }
957
958 /*
959 * Done! Let others know that we're finished.
960 */
962
964
966
967 /*
968 * Mark top transaction id is logged (if needed) so that we should not try
969 * to log it again with the next WAL record in the current subtransaction.
970 */
971 if (topxid_included)
973
974 /*
975 * Update shared LogwrtRqst.Write, if we crossed page boundary.
976 */
977 if (StartPos / XLOG_BLCKSZ != EndPos / XLOG_BLCKSZ)
978 {
980 /* advance global request to include new block(s) */
981 if (XLogCtl->LogwrtRqst.Write < EndPos)
982 XLogCtl->LogwrtRqst.Write = EndPos;
985 }
986
987 /*
988 * If this was an XLOG_SWITCH record, flush the record and the empty
989 * padding space that fills the rest of the segment, and perform
990 * end-of-segment actions (eg, notifying archiver).
991 */
992 if (class == WALINSERT_SPECIAL_SWITCH)
993 {
994 TRACE_POSTGRESQL_WAL_SWITCH();
995 XLogFlush(EndPos);
996
997 /*
998 * Even though we reserved the rest of the segment for us, which is
999 * reflected in EndPos, we return a pointer to just the end of the
1000 * xlog-switch record.
1001 */
1002 if (inserted)
1003 {
1004 EndPos = StartPos + SizeOfXLogRecord;
1005 if (StartPos / XLOG_BLCKSZ != EndPos / XLOG_BLCKSZ)
1006 {
1007 uint64 offset = XLogSegmentOffset(EndPos, wal_segment_size);
1008
1009 if (offset == EndPos % XLOG_BLCKSZ)
1010 EndPos += SizeOfXLogLongPHD;
1011 else
1012 EndPos += SizeOfXLogShortPHD;
1013 }
1014 }
1015 }
1016
1017#ifdef WAL_DEBUG
1018 if (XLOG_DEBUG)
1019 {
1020 static XLogReaderState *debug_reader = NULL;
1021 XLogRecord *record;
1022 DecodedXLogRecord *decoded;
1024 StringInfoData recordBuf;
1025 char *errormsg = NULL;
1026 MemoryContext oldCxt;
1027
1028 oldCxt = MemoryContextSwitchTo(walDebugCxt);
1029
1031 appendStringInfo(&buf, "INSERT @ %X/%X: ", LSN_FORMAT_ARGS(EndPos));
1032
1033 /*
1034 * We have to piece together the WAL record data from the XLogRecData
1035 * entries, so that we can pass it to the rm_desc function as one
1036 * contiguous chunk.
1037 */
1038 initStringInfo(&recordBuf);
1039 for (; rdata != NULL; rdata = rdata->next)
1040 appendBinaryStringInfo(&recordBuf, rdata->data, rdata->len);
1041
1042 /* We also need temporary space to decode the record. */
1043 record = (XLogRecord *) recordBuf.data;
1044 decoded = (DecodedXLogRecord *)
1046
1047 if (!debug_reader)
1048 debug_reader = XLogReaderAllocate(wal_segment_size, NULL,
1049 XL_ROUTINE(.page_read = NULL,
1050 .segment_open = NULL,
1051 .segment_close = NULL),
1052 NULL);
1053 if (!debug_reader)
1054 {
1055 appendStringInfoString(&buf, "error decoding record: out of memory while allocating a WAL reading processor");
1056 }
1057 else if (!DecodeXLogRecord(debug_reader,
1058 decoded,
1059 record,
1060 EndPos,
1061 &errormsg))
1062 {
1063 appendStringInfo(&buf, "error decoding record: %s",
1064 errormsg ? errormsg : "no error message");
1065 }
1066 else
1067 {
1068 appendStringInfoString(&buf, " - ");
1069
1070 debug_reader->record = decoded;
1071 xlog_outdesc(&buf, debug_reader);
1072 debug_reader->record = NULL;
1073 }
1074 elog(LOG, "%s", buf.data);
1075
1076 pfree(decoded);
1077 pfree(buf.data);
1078 pfree(recordBuf.data);
1079 MemoryContextSwitchTo(oldCxt);
1080 }
1081#endif
1082
1083 /*
1084 * Update our global variables
1085 */
1086 ProcLastRecPtr = StartPos;
1087 XactLastRecEnd = EndPos;
1088
1089 /* Report WAL traffic to the instrumentation. */
1090 if (inserted)
1091 {
1092 pgWalUsage.wal_bytes += rechdr->xl_tot_len;
1094 pgWalUsage.wal_fpi += num_fpi;
1095 }
1096
1097 return EndPos;
1098}
#define likely(x)
Definition: c.h:346
#define unlikely(x)
Definition: c.h:347
WalUsage pgWalUsage
Definition: instrument.c:22
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:281
uint64 wal_bytes
Definition: instrument.h:55
int64 wal_fpi
Definition: instrument.h:54
int64 wal_records
Definition: instrument.h:53
DecodedXLogRecord * record
Definition: xlogreader.h:236
const void * data
struct XLogRecData * next
pg_crc32c xl_crc
Definition: xlogrecord.h:49
void MarkSubxactTopXidLogged(void)
Definition: xact.c:591
void MarkCurrentTransactionIdLoggedIfAny(void)
Definition: xact.c:541
XLogRecPtr XactLastRecEnd
Definition: xlog.c:254
static void WALInsertLockAcquire(void)
Definition: xlog.c:1382
static void CopyXLogRecordToWAL(int write_len, bool isLogSwitch, XLogRecData *rdata, XLogRecPtr StartPos, XLogRecPtr EndPos, TimeLineID tli)
Definition: xlog.c:1236
static bool holdingAllLocks
Definition: xlog.c:663
static int MyLockNo
Definition: xlog.c:662
WalInsertClass
Definition: xlog.c:571
@ WALINSERT_SPECIAL_SWITCH
Definition: xlog.c:573
@ WALINSERT_NORMAL
Definition: xlog.c:572
@ WALINSERT_SPECIAL_CHECKPOINT
Definition: xlog.c:574
static void ReserveXLogInsertLocation(int size, XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
Definition: xlog.c:1119
static bool ReserveXLogSwitch(XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
Definition: xlog.c:1175
#define XLOG_MARK_UNIMPORTANT
Definition: xlog.h:155
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:107
bool DecodeXLogRecord(XLogReaderState *state, DecodedXLogRecord *decoded, XLogRecord *record, XLogRecPtr lsn, char **errormsg)
Definition: xlogreader.c:1672
size_t DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
Definition: xlogreader.c:1639
#define XL_ROUTINE(...)
Definition: xlogreader.h:117
void xlog_outdesc(StringInfo buf, XLogReaderState *record)

References appendBinaryStringInfo(), appendStringInfo(), appendStringInfoString(), Assert(), buf, COMP_CRC32C, CopyXLogRecordToWAL(), XLogRecData::data, StringInfoData::data, DecodeXLogRecord(), DecodeXLogRecordRequiredSpace(), doPageWrites, elog, END_CRIT_SECTION, ERROR, FIN_CRC32C, holdingAllLocks, if(), XLogCtlData::info_lck, initStringInfo(), XLogCtlData::Insert, Insert(), XLogCtlData::InsertTimeLineID, InvalidXLogRecPtr, WALInsertLockPadded::l, WALInsertLock::lastImportantAt, XLogRecData::len, likely, LOG, LogwrtResult, XLogCtlData::LogwrtRqst, LSN_FORMAT_ARGS, MarkCurrentTransactionIdLoggedIfAny(), MarkSubxactTopXidLogged(), MemoryContextSwitchTo(), MyLockNo, XLogRecData::next, palloc(), pfree(), pgWalUsage, ProcLastRecPtr, XLogReaderState::record, RedoRecPtr, RefreshXLogWriteResult, ReserveXLogInsertLocation(), ReserveXLogSwitch(), SizeOfXLogLongPHD, SizeOfXLogRecord, SizeOfXLogShortPHD, SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, unlikely, WalUsage::wal_bytes, WalUsage::wal_fpi, WalUsage::wal_records, wal_segment_size, WALINSERT_NORMAL, WALINSERT_SPECIAL_CHECKPOINT, WALINSERT_SPECIAL_SWITCH, WALInsertLockAcquire(), WALInsertLockAcquireExclusive(), WALInsertLockRelease(), WALInsertLocks, XLogwrtRqst::Write, XactLastRecEnd, XLogRecord::xl_crc, XLogRecord::xl_info, XLogRecord::xl_prev, XLogRecord::xl_rmid, XL_ROUTINE, XLogRecord::xl_tot_len, XLOG_CHECKPOINT_REDO, XLOG_MARK_UNIMPORTANT, xlog_outdesc(), XLOG_SWITCH, XLogCtl, XLogFlush(), XLogInsertAllowed(), XLogReaderAllocate(), XLogSegmentOffset, and XLR_INFO_MASK.

Referenced by XLogInsert().

◆ XLogNeedsFlush()

bool XLogNeedsFlush ( XLogRecPtr  record)

Definition at line 3254 of file xlog.c.

3255{
3256 /*
3257 * During recovery, we don't flush WAL but update minRecoveryPoint
3258 * instead. So "needs flush" is taken to mean whether minRecoveryPoint
3259 * would need to be updated.
3260 */
3261 if (RecoveryInProgress())
3262 {
3263 /*
3264 * An invalid minRecoveryPoint means that we need to recover all the
3265 * WAL, i.e., we're doing crash recovery. We never modify the control
3266 * file's value in that case, so we can short-circuit future checks
3267 * here too. This triggers a quick exit path for the startup process,
3268 * which cannot update its local copy of minRecoveryPoint as long as
3269 * it has not replayed all WAL available when doing crash recovery.
3270 */
3272 updateMinRecoveryPoint = false;
3273
3274 /* Quick exit if already known to be updated or cannot be updated */
3276 return false;
3277
3278 /*
3279 * Update local copy of minRecoveryPoint. But if the lock is busy,
3280 * just return a conservative guess.
3281 */
3282 if (!LWLockConditionalAcquire(ControlFileLock, LW_SHARED))
3283 return true;
3286 LWLockRelease(ControlFileLock);
3287
3288 /*
3289 * Check minRecoveryPoint for any other process than the startup
3290 * process doing crash recovery, which should not update the control
3291 * file value if crash recovery is still running.
3292 */
3294 updateMinRecoveryPoint = false;
3295
3296 /* check again */
3298 return false;
3299 else
3300 return true;
3301 }
3302
3303 /* Quick exit if already known flushed */
3304 if (record <= LogwrtResult.Flush)
3305 return false;
3306
3307 /* read LogwrtResult and update local state */
3309
3310 /* check again */
3311 if (record <= LogwrtResult.Flush)
3312 return false;
3313
3314 return true;
3315}
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1353

References ControlFile, XLogwrtResult::Flush, InRecovery, LocalMinRecoveryPoint, LocalMinRecoveryPointTLI, LogwrtResult, LW_SHARED, LWLockConditionalAcquire(), LWLockRelease(), ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, RecoveryInProgress(), RefreshXLogWriteResult, updateMinRecoveryPoint, and XLogRecPtrIsInvalid.

Referenced by GetVictimBuffer(), and SetHintBits().

◆ XLogPutNextOid()

void XLogPutNextOid ( Oid  nextOid)

Definition at line 8211 of file xlog.c.

8212{
8214 XLogRegisterData(&nextOid, sizeof(Oid));
8215 (void) XLogInsert(RM_XLOG_ID, XLOG_NEXTOID);
8216
8217 /*
8218 * We need not flush the NEXTOID record immediately, because any of the
8219 * just-allocated OIDs could only reach disk as part of a tuple insert or
8220 * update that would have its own XLOG record that must follow the NEXTOID
8221 * record. Therefore, the standard buffer LSN interlock applied to those
8222 * records will ensure no such OID reaches disk before the NEXTOID record
8223 * does.
8224 *
8225 * Note, however, that the above statement only covers state "within" the
8226 * database. When we use a generated OID as a file or directory name, we
8227 * are in a sense violating the basic WAL rule, because that filesystem
8228 * change may reach disk before the NEXTOID WAL record does. The impact
8229 * of this is that if a database crash occurs immediately afterward, we
8230 * might after restart re-generate the same OID and find that it conflicts
8231 * with the leftover file or directory. But since for safety's sake we
8232 * always loop until finding a nonconflicting filename, this poses no real
8233 * problem in practice. See pgsql-hackers discussion 27-Sep-2006.
8234 */
8235}

References XLOG_NEXTOID, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by GetNewObjectId().

◆ XLogRestorePoint()

XLogRecPtr XLogRestorePoint ( const char *  rpName)

Definition at line 8266 of file xlog.c.

8267{
8268 XLogRecPtr RecPtr;
8269 xl_restore_point xlrec;
8270
8271 xlrec.rp_time = GetCurrentTimestamp();
8272 strlcpy(xlrec.rp_name, rpName, MAXFNAMELEN);
8273
8275 XLogRegisterData(&xlrec, sizeof(xl_restore_point));
8276
8277 RecPtr = XLogInsert(RM_XLOG_ID, XLOG_RESTORE_POINT);
8278
8279 ereport(LOG,
8280 (errmsg("restore point \"%s\" created at %X/%X",
8281 rpName, LSN_FORMAT_ARGS(RecPtr))));
8282
8283 return RecPtr;
8284}
TimestampTz rp_time

References ereport, errmsg(), GetCurrentTimestamp(), LOG, LSN_FORMAT_ARGS, MAXFNAMELEN, xl_restore_point::rp_name, xl_restore_point::rp_time, strlcpy(), XLOG_RESTORE_POINT, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by pg_create_restore_point().

◆ XLogSetAsyncXactLSN()

void XLogSetAsyncXactLSN ( XLogRecPtr  asyncXactLSN)

Definition at line 2752 of file xlog.c.

2753{
2754 XLogRecPtr WriteRqstPtr = asyncXactLSN;
2755 bool sleeping;
2756 bool wakeup = false;
2757 XLogRecPtr prevAsyncXactLSN;
2758
2760 sleeping = XLogCtl->WalWriterSleeping;
2761 prevAsyncXactLSN = XLogCtl->asyncXactLSN;
2762 if (XLogCtl->asyncXactLSN < asyncXactLSN)
2763 XLogCtl->asyncXactLSN = asyncXactLSN;
2765
2766 /*
2767 * If somebody else already called this function with a more aggressive
2768 * LSN, they will have done what we needed (and perhaps more).
2769 */
2770 if (asyncXactLSN <= prevAsyncXactLSN)
2771 return;
2772
2773 /*
2774 * If the WALWriter is sleeping, kick it to make it come out of low-power
2775 * mode, so that this async commit will reach disk within the expected
2776 * amount of time. Otherwise, determine whether it has enough WAL
2777 * available to flush, the same way that XLogBackgroundFlush() does.
2778 */
2779 if (sleeping)
2780 wakeup = true;
2781 else
2782 {
2783 int flushblocks;
2784
2786
2787 flushblocks =
2788 WriteRqstPtr / XLOG_BLCKSZ - LogwrtResult.Flush / XLOG_BLCKSZ;
2789
2790 if (WalWriterFlushAfter == 0 || flushblocks >= WalWriterFlushAfter)
2791 wakeup = true;
2792 }
2793
2794 if (wakeup)
2795 {
2796 volatile PROC_HDR *procglobal = ProcGlobal;
2797 ProcNumber walwriterProc = procglobal->walwriterProc;
2798
2799 if (walwriterProc != INVALID_PROC_NUMBER)
2800 SetLatch(&GetPGProcByNumber(walwriterProc)->procLatch);
2801 }
2802}
void SetLatch(Latch *latch)
Definition: latch.c:288
#define GetPGProcByNumber(n)
Definition: proc.h:424
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int ProcNumber
Definition: procnumber.h:24
PROC_HDR * ProcGlobal
Definition: proc.c:79
Definition: proc.h:370
ProcNumber walwriterProc
Definition: proc.h:408
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]
Definition: walreceiver.c:130

References XLogCtlData::asyncXactLSN, XLogwrtResult::Flush, GetPGProcByNumber, XLogCtlData::info_lck, INVALID_PROC_NUMBER, LogwrtResult, ProcGlobal, RefreshXLogWriteResult, SetLatch(), SpinLockAcquire, SpinLockRelease, wakeup, WalWriterFlushAfter, PROC_HDR::walwriterProc, XLogCtlData::WalWriterSleeping, and XLogCtl.

Referenced by AbortTransaction(), LogCurrentRunningXacts(), RecordTransactionAbort(), and RecordTransactionCommit().

◆ XLogSetReplicationSlotMinimumLSN()

void XLogSetReplicationSlotMinimumLSN ( XLogRecPtr  lsn)

◆ XLOGShmemInit()

void XLOGShmemInit ( void  )

Definition at line 5099 of file xlog.c.

5100{
5101 bool foundCFile,
5102 foundXLog;
5103 char *allocptr;
5104 int i;
5105 ControlFileData *localControlFile;
5106
5107#ifdef WAL_DEBUG
5108
5109 /*
5110 * Create a memory context for WAL debugging that's exempt from the normal
5111 * "no pallocs in critical section" rule. Yes, that can lead to a PANIC if
5112 * an allocation fails, but wal_debug is not for production use anyway.
5113 */
5114 if (walDebugCxt == NULL)
5115 {
5117 "WAL Debug",
5119 MemoryContextAllowInCriticalSection(walDebugCxt, true);
5120 }
5121#endif
5122
5123
5124 XLogCtl = (XLogCtlData *)
5125 ShmemInitStruct("XLOG Ctl", XLOGShmemSize(), &foundXLog);
5126
5127 localControlFile = ControlFile;
5129 ShmemInitStruct("Control File", sizeof(ControlFileData), &foundCFile);
5130
5131 if (foundCFile || foundXLog)
5132 {
5133 /* both should be present or neither */
5134 Assert(foundCFile && foundXLog);
5135
5136 /* Initialize local copy of WALInsertLocks */
5138
5139 if (localControlFile)
5140 pfree(localControlFile);
5141 return;
5142 }
5143 memset(XLogCtl, 0, sizeof(XLogCtlData));
5144
5145 /*
5146 * Already have read control file locally, unless in bootstrap mode. Move
5147 * contents into shared memory.
5148 */
5149 if (localControlFile)
5150 {
5151 memcpy(ControlFile, localControlFile, sizeof(ControlFileData));
5152 pfree(localControlFile);
5153 }
5154
5155 /*
5156 * Since XLogCtlData contains XLogRecPtr fields, its sizeof should be a
5157 * multiple of the alignment for same, so no extra alignment padding is
5158 * needed here.
5159 */
5160 allocptr = ((char *) XLogCtl) + sizeof(XLogCtlData);
5161 XLogCtl->xlblocks = (pg_atomic_uint64 *) allocptr;
5162 allocptr += sizeof(pg_atomic_uint64) * XLOGbuffers;
5163
5164 for (i = 0; i < XLOGbuffers; i++)
5165 {
5167 }
5168
5169 /* WAL insertion locks. Ensure they're aligned to the full padded size */
5170 allocptr += sizeof(WALInsertLockPadded) -
5171 ((uintptr_t) allocptr) % sizeof(WALInsertLockPadded);
5173 (WALInsertLockPadded *) allocptr;
5174 allocptr += sizeof(WALInsertLockPadded) * NUM_XLOGINSERT_LOCKS;
5175
5176 for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
5177 {
5181 }
5182
5183 /*
5184 * Align the start of the page buffers to a full xlog block size boundary.
5185 * This simplifies some calculations in XLOG insertion. It is also
5186 * required for O_DIRECT.
5187 */
5188 allocptr = (char *) TYPEALIGN(XLOG_BLCKSZ, allocptr);
5189 XLogCtl->pages = allocptr;
5190 memset(XLogCtl->pages, 0, (Size) XLOG_BLCKSZ * XLOGbuffers);
5191
5192 /*
5193 * Do basic initialization of XLogCtl shared data. (StartupXLOG will fill
5194 * in additional info.)
5195 */
5199 XLogCtl->WalWriterSleeping = false;
5200
5207
5211}
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:453
void ConditionVariableInit(ConditionVariable *cv)
struct pg_atomic_uint64 pg_atomic_uint64
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:721
@ LWTRANCHE_WAL_INSERT
Definition: lwlock.h:188
MemoryContext TopMemoryContext
Definition: mcxt.c:165
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
Definition: mcxt.c:725
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:57
ConditionVariable InitializedUpToCondVar
Definition: xlog.c:495
int XLogCacheBlck
Definition: xlog.c:505
WALInsertLockPadded * WALInsertLocks
Definition: xlog.c:439
slock_t insertpos_lck
Definition: xlog.c:393
union WALInsertLockPadded WALInsertLockPadded
Size XLOGShmemSize(void)
Definition: xlog.c:5049
int XLOGbuffers
Definition: xlog.c:117
struct XLogCtlData XLogCtlData

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), ConditionVariableInit(), ControlFile, i, XLogCtlData::info_lck, XLogCtlData::InitializedUpTo, XLogCtlData::InitializedUpToCondVar, XLogCtlData::InitializeReserved, XLogCtlData::Insert, XLogCtlInsert::insertpos_lck, XLogCtlData::InstallXLogFileSegmentActive, InvalidXLogRecPtr, WALInsertLockPadded::l, WALInsertLock::lastImportantAt, XLogCtlData::logFlushResult, XLogCtlData::logInsertResult, XLogCtlData::logWriteResult, LWLockInitialize(), LWTRANCHE_WAL_INSERT, MemoryContextAllowInCriticalSection(), NUM_XLOGINSERT_LOCKS, XLogCtlData::pages, pfree(), pg_atomic_init_u64(), RECOVERY_STATE_CRASH, XLogCtlData::SharedRecoveryState, ShmemInitStruct(), SpinLockInit, TopMemoryContext, TYPEALIGN, XLogCtlData::unloggedLSN, XLogCtlInsert::WALInsertLocks, WALInsertLocks, XLogCtlData::WalWriterSleeping, XLogCtlData::xlblocks, XLOGbuffers, XLogCtlData::XLogCacheBlck, XLogCtl, and XLOGShmemSize().

Referenced by CreateOrAttachShmemStructs().

◆ XLOGShmemSize()

Size XLOGShmemSize ( void  )

Definition at line 5049 of file xlog.c.

5050{
5051 Size size;
5052
5053 /*
5054 * If the value of wal_buffers is -1, use the preferred auto-tune value.
5055 * This isn't an amazingly clean place to do this, but we must wait till
5056 * NBuffers has received its final value, and must do it before using the
5057 * value of XLOGbuffers to do anything important.
5058 *
5059 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
5060 * However, if the DBA explicitly set wal_buffers = -1 in the config file,
5061 * then PGC_S_DYNAMIC_DEFAULT will fail to override that and we must force
5062 * the matter with PGC_S_OVERRIDE.
5063 */
5064 if (XLOGbuffers == -1)
5065 {
5066 char buf[32];
5067
5068 snprintf(buf, sizeof(buf), "%d", XLOGChooseNumBuffers());
5069 SetConfigOption("wal_buffers", buf, PGC_POSTMASTER,
5071 if (XLOGbuffers == -1) /* failed to apply it? */
5072 SetConfigOption("wal_buffers", buf, PGC_POSTMASTER,
5074 }
5075 Assert(XLOGbuffers > 0);
5076
5077 /* XLogCtl */
5078 size = sizeof(XLogCtlData);
5079
5080 /* WAL insertion locks, plus alignment */
5081 size = add_size(size, mul_size(sizeof(WALInsertLockPadded), NUM_XLOGINSERT_LOCKS + 1));
5082 /* xlblocks array */
5083 size = add_size(size, mul_size(sizeof(pg_atomic_uint64), XLOGbuffers));
5084 /* extra alignment padding for XLOG I/O buffers */
5085 size = add_size(size, Max(XLOG_BLCKSZ, PG_IO_ALIGN_SIZE));
5086 /* and the buffers themselves */
5087 size = add_size(size, mul_size(XLOG_BLCKSZ, XLOGbuffers));
5088
5089 /*
5090 * Note: we don't count ControlFileData, it comes out of the "slop factor"
5091 * added by CreateSharedMemoryAndSemaphores. This lets us use this
5092 * routine again below to compute the actual allocation size.
5093 */
5094
5095 return size;
5096}
#define Max(x, y)
Definition: c.h:969
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:114
@ PGC_S_OVERRIDE
Definition: guc.h:123
@ PGC_POSTMASTER
Definition: guc.h:74
#define PG_IO_ALIGN_SIZE
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
static int XLOGChooseNumBuffers(void)
Definition: xlog.c:4800

References add_size(), Assert(), buf, Max, mul_size(), NUM_XLOGINSERT_LOCKS, PG_IO_ALIGN_SIZE, PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT, PGC_S_OVERRIDE, SetConfigOption(), snprintf, XLOGbuffers, and XLOGChooseNumBuffers().

Referenced by CalculateShmemSize(), and XLOGShmemInit().

◆ XLogShutdownWalRcv()

void XLogShutdownWalRcv ( void  )

Definition at line 9650 of file xlog.c.

9651{
9653
9654 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9656 LWLockRelease(ControlFileLock);
9657}
void ShutdownWalRcv(void)

References XLogCtlData::InstallXLogFileSegmentActive, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ShutdownWalRcv(), and XLogCtl.

Referenced by FinishWalRecovery(), and WaitForWALToBecomeAvailable().

Variable Documentation

◆ CheckPointSegments

PGDLLIMPORT int CheckPointSegments
extern

◆ CheckpointStats

◆ CommitDelay

PGDLLIMPORT int CommitDelay
extern

Definition at line 132 of file xlog.c.

Referenced by XLogFlush().

◆ CommitSiblings

PGDLLIMPORT int CommitSiblings
extern

Definition at line 133 of file xlog.c.

Referenced by XLogFlush().

◆ EnableHotStandby

◆ fullPageWrites

PGDLLIMPORT bool fullPageWrites
extern

Definition at line 122 of file xlog.c.

Referenced by BootStrapXLOG(), and UpdateFullPageWrites().

◆ log_checkpoints

PGDLLIMPORT bool log_checkpoints
extern

◆ max_slot_wal_keep_size_mb

PGDLLIMPORT int max_slot_wal_keep_size_mb
extern

Definition at line 135 of file xlog.c.

Referenced by KeepLogSeg(), and pg_get_replication_slots().

◆ max_wal_size_mb

PGDLLIMPORT int max_wal_size_mb
extern

◆ min_wal_size_mb

PGDLLIMPORT int min_wal_size_mb
extern

Definition at line 115 of file xlog.c.

Referenced by ReadControlFile(), and XLOGfileslop().

◆ ProcLastRecPtr

PGDLLIMPORT XLogRecPtr ProcLastRecPtr
extern

◆ track_wal_io_timing

PGDLLIMPORT bool track_wal_io_timing
extern

◆ wal_compression

PGDLLIMPORT int wal_compression
extern

Definition at line 124 of file xlog.c.

Referenced by XLogCompressBackupBlock(), and XLogRecordAssemble().

◆ wal_consistency_checking

PGDLLIMPORT bool* wal_consistency_checking
extern

Definition at line 126 of file xlog.c.

Referenced by assign_wal_consistency_checking(), and XLogRecordAssemble().

◆ wal_consistency_checking_string

PGDLLIMPORT char* wal_consistency_checking_string
extern

Definition at line 125 of file xlog.c.

Referenced by InitializeWalConsistencyChecking().

◆ wal_decode_buffer_size

PGDLLIMPORT int wal_decode_buffer_size
extern

Definition at line 136 of file xlog.c.

Referenced by InitWalRecovery().

◆ wal_init_zero

PGDLLIMPORT bool wal_init_zero
extern

Definition at line 127 of file xlog.c.

Referenced by XLogFileInitInternal().

◆ wal_keep_size_mb

PGDLLIMPORT int wal_keep_size_mb
extern

Definition at line 116 of file xlog.c.

Referenced by KeepLogSeg(), and pg_get_replication_slots().

◆ wal_level

◆ wal_log_hints

PGDLLIMPORT bool wal_log_hints
extern

Definition at line 123 of file xlog.c.

Referenced by InitControlFile(), and XLogReportParameters().

◆ wal_recycle

PGDLLIMPORT bool wal_recycle
extern

Definition at line 128 of file xlog.c.

Referenced by RemoveXlogFile().

◆ wal_retrieve_retry_interval

PGDLLIMPORT int wal_retrieve_retry_interval
extern

◆ wal_segment_size

PGDLLIMPORT int wal_segment_size
extern

Definition at line 143 of file xlog.c.

Referenced by AdvanceXLInsertBuffer(), assign_wal_sync_method(), BootStrapXLOG(), build_backup_content(), CalculateCheckpointSegments(), CheckArchiveTimeout(), CheckXLogRemoved(), CleanupAfterArchiveRecovery(), copy_replication_slot(), CopyXLogRecordToWAL(), CreateCheckPoint(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), do_pg_backup_stop(), ExecuteRecoveryCommand(), FinishWalRecovery(), GetOldestUnsummarizedLSN(), GetWALAvailability(), GetXLogBuffer(), InitWalRecovery(), InitXLogReaderState(), InstallXLogFileSegment(), InvalidateObsoleteReplicationSlots(), IsCheckpointOnSchedule(), issue_xlog_fsync(), KeepLogSeg(), MaybeRemoveOldWalSummaries(), perform_base_backup(), pg_control_checkpoint(), pg_get_replication_slots(), pg_split_walfile_name(), pg_walfile_name(), pg_walfile_name_offset(), PreallocXlogFiles(), ReadControlFile(), ReadRecord(), RemoveNonParentXlogFiles(), RemoveOldXlogFiles(), ReorderBufferRestoreChanges(), ReorderBufferRestoreCleanup(), ReorderBufferSerializedPath(), ReorderBufferSerializeTXN(), ReplicationSlotReserveWal(), RequestXLogStreaming(), reserve_wal_for_local_slot(), ReserveXLogSwitch(), RestoreArchivedFile(), StartReplication(), StartupDecodingContext(), SummarizeWAL(), UpdateLastRemovedPtr(), WALReadRaiseError(), WalReceiverMain(), WalSndSegmentOpen(), WriteControlFile(), XLogArchiveNotifySeg(), XLogBackgroundFlush(), XLogBytePosToEndRecPtr(), XLogBytePosToRecPtr(), XLogCheckpointNeeded(), XLOGChooseNumBuffers(), XLogFileClose(), XLogFileCopy(), XLogFileInitInternal(), XLogFileOpen(), XLogFileRead(), XLogFileReadAnyTLI(), XLOGfileslop(), XLogGetOldestSegno(), XLogInitNewTimeline(), XLogInsertRecord(), XLogPageRead(), XLogReaderAllocate(), XlogReadTwoPhaseData(), XLogRecPtrToBytePos(), XLogWalRcvClose(), XLogWalRcvWrite(), and XLogWrite().

◆ wal_sync_method

PGDLLIMPORT int wal_sync_method
extern

◆ XactLastCommitEnd

◆ XactLastRecEnd

◆ XLogArchiveCommand

PGDLLIMPORT char* XLogArchiveCommand
extern

◆ XLogArchiveMode

◆ XLogArchiveTimeout

PGDLLIMPORT int XLogArchiveTimeout
extern

Definition at line 118 of file xlog.c.

Referenced by CheckArchiveTimeout(), and CheckpointerMain().

◆ XLOGbuffers

PGDLLIMPORT int XLOGbuffers
extern

Definition at line 117 of file xlog.c.

Referenced by AdvanceXLInsertBuffer(), check_wal_buffers(), XLOGShmemInit(), and XLOGShmemSize().