PostgreSQL Source Code  git master
xlog.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/heaptoast.h"
#include "access/multixact.h"
#include "access/rewriteheap.h"
#include "access/subtrans.h"
#include "access/timeline.h"
#include "access/transam.h"
#include "access/twophase.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "access/xlogarchive.h"
#include "access/xloginsert.h"
#include "access/xlogreader.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "backup/basebackup.h"
#include "catalog/catversion.h"
#include "catalog/pg_control.h"
#include "catalog/pg_database.h"
#include "common/controldata_utils.h"
#include "common/file_utils.h"
#include "executor/instrument.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "pgstat.h"
#include "port/atomics.h"
#include "port/pg_iovec.h"
#include "postmaster/bgwriter.h"
#include "postmaster/startup.h"
#include "postmaster/walsummarizer.h"
#include "postmaster/walwriter.h"
#include "replication/origin.h"
#include "replication/slot.h"
#include "replication/snapbuild.h"
#include "replication/walreceiver.h"
#include "replication/walsender.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/large_object.h"
#include "storage/latch.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/reinit.h"
#include "storage/spin.h"
#include "storage/sync.h"
#include "utils/guc_hooks.h"
#include "utils/guc_tables.h"
#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/relmapper.h"
#include "utils/snapmgr.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"
#include "utils/varlena.h"
Include dependency graph for xlog.c:

Go to the source code of this file.

Data Structures

struct  XLogwrtRqst
 
struct  XLogwrtResult
 
struct  WALInsertLock
 
union  WALInsertLockPadded
 
struct  XLogCtlInsert
 
struct  XLogCtlData
 

Macros

#define BootstrapTimeLineID   1
 
#define NUM_XLOGINSERT_LOCKS   8
 
#define INSERT_FREESPACE(endptr)    (((endptr) % XLOG_BLCKSZ == 0) ? 0 : (XLOG_BLCKSZ - (endptr) % XLOG_BLCKSZ))
 
#define NextBufIdx(idx)    (((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))
 
#define XLogRecPtrToBufIdx(recptr)    (((recptr) / XLOG_BLCKSZ) % (XLogCtl->XLogCacheBlck + 1))
 
#define UsableBytesInPage   (XLOG_BLCKSZ - SizeOfXLogShortPHD)
 
#define ConvertToXSegs(x, segsize)   XLogMBVarToSegs((x), (segsize))
 
#define RefreshXLogWriteResult(_target)
 

Typedefs

typedef struct XLogwrtRqst XLogwrtRqst
 
typedef struct XLogwrtResult XLogwrtResult
 
typedef union WALInsertLockPadded WALInsertLockPadded
 
typedef struct XLogCtlInsert XLogCtlInsert
 
typedef struct XLogCtlData XLogCtlData
 

Enumerations

enum  WalInsertClass { WALINSERT_NORMAL , WALINSERT_SPECIAL_SWITCH , WALINSERT_SPECIAL_CHECKPOINT }
 

Functions

static void CleanupAfterArchiveRecovery (TimeLineID EndOfLogTLI, XLogRecPtr EndOfLog, TimeLineID newTLI)
 
static void CheckRequiredParameterValues (void)
 
static void XLogReportParameters (void)
 
static int LocalSetXLogInsertAllowed (void)
 
static void CreateEndOfRecoveryRecord (void)
 
static XLogRecPtr CreateOverwriteContrecordRecord (XLogRecPtr aborted_lsn, XLogRecPtr pagePtr, TimeLineID newTLI)
 
static void CheckPointGuts (XLogRecPtr checkPointRedo, int flags)
 
static void KeepLogSeg (XLogRecPtr recptr, XLogSegNo *logSegNo)
 
static XLogRecPtr XLogGetReplicationSlotMinimumLSN (void)
 
static void AdvanceXLInsertBuffer (XLogRecPtr upto, TimeLineID tli, bool opportunistic)
 
static void XLogWrite (XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 
static bool InstallXLogFileSegment (XLogSegNo *segno, char *tmppath, bool find_free, XLogSegNo max_segno, TimeLineID tli)
 
static void XLogFileClose (void)
 
static void PreallocXlogFiles (XLogRecPtr endptr, TimeLineID tli)
 
static void RemoveTempXlogFiles (void)
 
static void RemoveOldXlogFiles (XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr, TimeLineID insertTLI)
 
static void RemoveXlogFile (const struct dirent *segment_de, XLogSegNo recycleSegNo, XLogSegNo *endlogSegNo, TimeLineID insertTLI)
 
static void UpdateLastRemovedPtr (char *filename)
 
static void ValidateXLOGDirectoryStructure (void)
 
static void CleanupBackupHistory (void)
 
static void UpdateMinRecoveryPoint (XLogRecPtr lsn, bool force)
 
static bool PerformRecoveryXLogAction (void)
 
static void InitControlFile (uint64 sysidentifier)
 
static void WriteControlFile (void)
 
static void ReadControlFile (void)
 
static void UpdateControlFile (void)
 
static char * str_time (pg_time_t tnow)
 
static int get_sync_bit (int method)
 
static void CopyXLogRecordToWAL (int write_len, bool isLogSwitch, XLogRecData *rdata, XLogRecPtr StartPos, XLogRecPtr EndPos, TimeLineID tli)
 
static void ReserveXLogInsertLocation (int size, XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
 
static bool ReserveXLogSwitch (XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
 
static XLogRecPtr WaitXLogInsertionsToFinish (XLogRecPtr upto)
 
static char * GetXLogBuffer (XLogRecPtr ptr, TimeLineID tli)
 
static XLogRecPtr XLogBytePosToRecPtr (uint64 bytepos)
 
static XLogRecPtr XLogBytePosToEndRecPtr (uint64 bytepos)
 
static uint64 XLogRecPtrToBytePos (XLogRecPtr ptr)
 
static void WALInsertLockAcquire (void)
 
static void WALInsertLockAcquireExclusive (void)
 
static void WALInsertLockRelease (void)
 
static void WALInsertLockUpdateInsertingAt (XLogRecPtr insertingAt)
 
XLogRecPtr XLogInsertRecord (XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi, bool topxid_included)
 
Size WALReadFromBuffers (char *dstbuf, XLogRecPtr startptr, Size count, TimeLineID tli)
 
static void CalculateCheckpointSegments (void)
 
void assign_max_wal_size (int newval, void *extra)
 
void assign_checkpoint_completion_target (double newval, void *extra)
 
bool check_wal_segment_size (int *newval, void **extra, GucSource source)
 
bool check_max_slot_wal_keep_size (int *newval, void **extra, GucSource source)
 
static XLogSegNo XLOGfileslop (XLogRecPtr lastredoptr)
 
bool XLogCheckpointNeeded (XLogSegNo new_segno)
 
void XLogSetAsyncXactLSN (XLogRecPtr asyncXactLSN)
 
void XLogSetReplicationSlotMinimumLSN (XLogRecPtr lsn)
 
void XLogFlush (XLogRecPtr record)
 
bool XLogBackgroundFlush (void)
 
bool XLogNeedsFlush (XLogRecPtr record)
 
static int XLogFileInitInternal (XLogSegNo logsegno, TimeLineID logtli, bool *added, char *path)
 
int XLogFileInit (XLogSegNo logsegno, TimeLineID logtli)
 
static void XLogFileCopy (TimeLineID destTLI, XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno, int upto)
 
int XLogFileOpen (XLogSegNo segno, TimeLineID tli)
 
void CheckXLogRemoved (XLogSegNo segno, TimeLineID tli)
 
XLogSegNo XLogGetLastRemovedSegno (void)
 
XLogSegNo XLogGetOldestSegno (TimeLineID tli)
 
void RemoveNonParentXlogFiles (XLogRecPtr switchpoint, TimeLineID newTLI)
 
uint64 GetSystemIdentifier (void)
 
char * GetMockAuthenticationNonce (void)
 
bool DataChecksumsEnabled (void)
 
XLogRecPtr GetFakeLSNForUnloggedRel (void)
 
static int XLOGChooseNumBuffers (void)
 
bool check_wal_buffers (int *newval, void **extra, GucSource source)
 
bool check_wal_consistency_checking (char **newval, void **extra, GucSource source)
 
void assign_wal_consistency_checking (const char *newval, void *extra)
 
void InitializeWalConsistencyChecking (void)
 
const char * show_archive_command (void)
 
const char * show_in_hot_standby (void)
 
void LocalProcessControlFile (bool reset)
 
WalLevel GetActiveWalLevelOnStandby (void)
 
Size XLOGShmemSize (void)
 
void XLOGShmemInit (void)
 
void BootStrapXLOG (void)
 
static void XLogInitNewTimeline (TimeLineID endTLI, XLogRecPtr endOfLog, TimeLineID newTLI)
 
void StartupXLOG (void)
 
void SwitchIntoArchiveRecovery (XLogRecPtr EndRecPtr, TimeLineID replayTLI)
 
void ReachedEndOfBackup (XLogRecPtr EndRecPtr, TimeLineID tli)
 
bool RecoveryInProgress (void)
 
RecoveryState GetRecoveryState (void)
 
bool XLogInsertAllowed (void)
 
XLogRecPtr GetRedoRecPtr (void)
 
void GetFullPageWriteInfo (XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
 
XLogRecPtr GetInsertRecPtr (void)
 
XLogRecPtr GetFlushRecPtr (TimeLineID *insertTLI)
 
TimeLineID GetWALInsertionTimeLine (void)
 
XLogRecPtr GetLastImportantRecPtr (void)
 
pg_time_t GetLastSegSwitchData (XLogRecPtr *lastSwitchLSN)
 
void ShutdownXLOG (int code, Datum arg)
 
static void LogCheckpointStart (int flags, bool restartpoint)
 
static void LogCheckpointEnd (bool restartpoint)
 
static void UpdateCheckPointDistanceEstimate (uint64 nbytes)
 
static void update_checkpoint_display (int flags, bool restartpoint, bool reset)
 
void CreateCheckPoint (int flags)
 
static void RecoveryRestartPoint (const CheckPoint *checkPoint, XLogReaderState *record)
 
bool CreateRestartPoint (int flags)
 
WALAvailability GetWALAvailability (XLogRecPtr targetLSN)
 
void XLogPutNextOid (Oid nextOid)
 
XLogRecPtr RequestXLogSwitch (bool mark_unimportant)
 
XLogRecPtr XLogRestorePoint (const char *rpName)
 
void UpdateFullPageWrites (void)
 
void xlog_redo (XLogReaderState *record)
 
void assign_wal_sync_method (int new_wal_sync_method, void *extra)
 
void issue_xlog_fsync (int fd, XLogSegNo segno, TimeLineID tli)
 
void do_pg_backup_start (const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
 
SessionBackupState get_backup_status (void)
 
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)
 
XLogRecPtr GetXLogInsertRecPtr (void)
 
XLogRecPtr GetXLogWriteRecPtr (void)
 
void GetOldestRestartPoint (XLogRecPtr *oldrecptr, TimeLineID *oldtli)
 
void XLogShutdownWalRcv (void)
 
void SetInstallXLogFileSegmentActive (void)
 
bool IsInstallXLogFileSegmentActive (void)
 
void SetWalWriterSleeping (bool sleeping)
 

Variables

uint32 bootstrap_data_checksum_version
 
int max_wal_size_mb = 1024
 
int min_wal_size_mb = 80
 
int wal_keep_size_mb = 0
 
int XLOGbuffers = -1
 
int XLogArchiveTimeout = 0
 
int XLogArchiveMode = ARCHIVE_MODE_OFF
 
char * XLogArchiveCommand = NULL
 
bool EnableHotStandby = false
 
bool fullPageWrites = true
 
bool wal_log_hints = false
 
int wal_compression = WAL_COMPRESSION_NONE
 
char * wal_consistency_checking_string = NULL
 
boolwal_consistency_checking = NULL
 
bool wal_init_zero = true
 
bool wal_recycle = true
 
bool log_checkpoints = true
 
int wal_sync_method = DEFAULT_WAL_SYNC_METHOD
 
int wal_level = WAL_LEVEL_REPLICA
 
int CommitDelay = 0
 
int CommitSiblings = 5
 
int wal_retrieve_retry_interval = 5000
 
int max_slot_wal_keep_size_mb = -1
 
int wal_decode_buffer_size = 512 * 1024
 
bool track_wal_io_timing = false
 
int wal_segment_size = DEFAULT_XLOG_SEG_SIZE
 
int CheckPointSegments
 
static double CheckPointDistanceEstimate = 0
 
static double PrevCheckPointDistance = 0
 
static bool check_wal_consistency_checking_deferred = false
 
const struct config_enum_entry wal_sync_method_options []
 
const struct config_enum_entry archive_mode_options []
 
CheckpointStatsData CheckpointStats
 
static bool lastFullPageWrites
 
static bool LocalRecoveryInProgress = true
 
static int LocalXLogInsertAllowed = -1
 
XLogRecPtr ProcLastRecPtr = InvalidXLogRecPtr
 
XLogRecPtr XactLastRecEnd = InvalidXLogRecPtr
 
XLogRecPtr XactLastCommitEnd = InvalidXLogRecPtr
 
static XLogRecPtr RedoRecPtr
 
static bool doPageWrites
 
static SessionBackupState sessionBackupState = SESSION_BACKUP_NONE
 
static XLogCtlDataXLogCtl = NULL
 
static WALInsertLockPaddedWALInsertLocks = NULL
 
static ControlFileDataControlFile = NULL
 
static int UsableBytesInSegment
 
static XLogwrtResult LogwrtResult = {0, 0}
 
static int openLogFile = -1
 
static XLogSegNo openLogSegNo = 0
 
static TimeLineID openLogTLI = 0
 
static XLogRecPtr LocalMinRecoveryPoint
 
static TimeLineID LocalMinRecoveryPointTLI
 
static bool updateMinRecoveryPoint = true
 
static int MyLockNo = 0
 
static bool holdingAllLocks = false
 

Macro Definition Documentation

◆ BootstrapTimeLineID

#define BootstrapTimeLineID   1

Definition at line 111 of file xlog.c.

◆ ConvertToXSegs

#define ConvertToXSegs (   x,
  segsize 
)    XLogMBVarToSegs((x), (segsize))

Definition at line 598 of file xlog.c.

◆ INSERT_FREESPACE

#define INSERT_FREESPACE (   endptr)     (((endptr) % XLOG_BLCKSZ == 0) ? 0 : (XLOG_BLCKSZ - (endptr) % XLOG_BLCKSZ))

Definition at line 575 of file xlog.c.

◆ NextBufIdx

#define NextBufIdx (   idx)     (((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))

Definition at line 579 of file xlog.c.

◆ NUM_XLOGINSERT_LOCKS

#define NUM_XLOGINSERT_LOCKS   8

Definition at line 150 of file xlog.c.

◆ RefreshXLogWriteResult

#define RefreshXLogWriteResult (   _target)
Value:
do { \
pg_read_barrier(); \
} while (0)
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:462
pg_atomic_uint64 logWriteResult
Definition: xlog.c:472
pg_atomic_uint64 logFlushResult
Definition: xlog.c:473
static XLogCtlData * XLogCtl
Definition: xlog.c:561

Definition at line 615 of file xlog.c.

◆ UsableBytesInPage

#define UsableBytesInPage   (XLOG_BLCKSZ - SizeOfXLogShortPHD)

Definition at line 592 of file xlog.c.

◆ XLogRecPtrToBufIdx

#define XLogRecPtrToBufIdx (   recptr)     (((recptr) / XLOG_BLCKSZ) % (XLogCtl->XLogCacheBlck + 1))

Definition at line 586 of file xlog.c.

Typedef Documentation

◆ WALInsertLockPadded

◆ XLogCtlData

typedef struct XLogCtlData XLogCtlData

◆ XLogCtlInsert

typedef struct XLogCtlInsert XLogCtlInsert

◆ XLogwrtResult

typedef struct XLogwrtResult XLogwrtResult

◆ XLogwrtRqst

typedef struct XLogwrtRqst XLogwrtRqst

Enumeration Type Documentation

◆ WalInsertClass

Enumerator
WALINSERT_NORMAL 
WALINSERT_SPECIAL_SWITCH 
WALINSERT_SPECIAL_CHECKPOINT 

Definition at line 554 of file xlog.c.

555 {
WalInsertClass
Definition: xlog.c:555
@ WALINSERT_SPECIAL_SWITCH
Definition: xlog.c:557
@ WALINSERT_NORMAL
Definition: xlog.c:556
@ WALINSERT_SPECIAL_CHECKPOINT
Definition: xlog.c:558

Function Documentation

◆ AdvanceXLInsertBuffer()

static void AdvanceXLInsertBuffer ( XLogRecPtr  upto,
TimeLineID  tli,
bool  opportunistic 
)
static

Definition at line 1980 of file xlog.c.

1981 {
1983  int nextidx;
1984  XLogRecPtr OldPageRqstPtr;
1985  XLogwrtRqst WriteRqst;
1986  XLogRecPtr NewPageEndPtr = InvalidXLogRecPtr;
1987  XLogRecPtr NewPageBeginPtr;
1988  XLogPageHeader NewPage;
1989  int npages pg_attribute_unused() = 0;
1990 
1991  LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE);
1992 
1993  /*
1994  * Now that we have the lock, check if someone initialized the page
1995  * already.
1996  */
1997  while (upto >= XLogCtl->InitializedUpTo || opportunistic)
1998  {
2000 
2001  /*
2002  * Get ending-offset of the buffer page we need to replace (this may
2003  * be zero if the buffer hasn't been used yet). Fall through if it's
2004  * already written out.
2005  */
2006  OldPageRqstPtr = pg_atomic_read_u64(&XLogCtl->xlblocks[nextidx]);
2007  if (LogwrtResult.Write < OldPageRqstPtr)
2008  {
2009  /*
2010  * Nope, got work to do. If we just want to pre-initialize as much
2011  * as we can without flushing, give up now.
2012  */
2013  if (opportunistic)
2014  break;
2015 
2016  /* Advance shared memory write request position */
2018  if (XLogCtl->LogwrtRqst.Write < OldPageRqstPtr)
2019  XLogCtl->LogwrtRqst.Write = OldPageRqstPtr;
2021 
2022  /*
2023  * Acquire an up-to-date LogwrtResult value and see if we still
2024  * need to write it or if someone else already did.
2025  */
2027  if (LogwrtResult.Write < OldPageRqstPtr)
2028  {
2029  /*
2030  * Must acquire write lock. Release WALBufMappingLock first,
2031  * to make sure that all insertions that we need to wait for
2032  * can finish (up to this same position). Otherwise we risk
2033  * deadlock.
2034  */
2035  LWLockRelease(WALBufMappingLock);
2036 
2037  WaitXLogInsertionsToFinish(OldPageRqstPtr);
2038 
2039  LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
2040 
2042  if (LogwrtResult.Write >= OldPageRqstPtr)
2043  {
2044  /* OK, someone wrote it already */
2045  LWLockRelease(WALWriteLock);
2046  }
2047  else
2048  {
2049  /* Have to write it ourselves */
2050  TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_START();
2051  WriteRqst.Write = OldPageRqstPtr;
2052  WriteRqst.Flush = 0;
2053  XLogWrite(WriteRqst, tli, false);
2054  LWLockRelease(WALWriteLock);
2056  TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_DONE();
2057  }
2058  /* Re-acquire WALBufMappingLock and retry */
2059  LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE);
2060  continue;
2061  }
2062  }
2063 
2064  /*
2065  * Now the next buffer slot is free and we can set it up to be the
2066  * next output page.
2067  */
2068  NewPageBeginPtr = XLogCtl->InitializedUpTo;
2069  NewPageEndPtr = NewPageBeginPtr + XLOG_BLCKSZ;
2070 
2071  Assert(XLogRecPtrToBufIdx(NewPageBeginPtr) == nextidx);
2072 
2073  NewPage = (XLogPageHeader) (XLogCtl->pages + nextidx * (Size) XLOG_BLCKSZ);
2074 
2075  /*
2076  * Mark the xlblock with InvalidXLogRecPtr and issue a write barrier
2077  * before initializing. Otherwise, the old page may be partially
2078  * zeroed but look valid.
2079  */
2081  pg_write_barrier();
2082 
2083  /*
2084  * Be sure to re-zero the buffer so that bytes beyond what we've
2085  * written will look like zeroes and not valid XLOG records...
2086  */
2087  MemSet((char *) NewPage, 0, XLOG_BLCKSZ);
2088 
2089  /*
2090  * Fill the new page's header
2091  */
2092  NewPage->xlp_magic = XLOG_PAGE_MAGIC;
2093 
2094  /* NewPage->xlp_info = 0; */ /* done by memset */
2095  NewPage->xlp_tli = tli;
2096  NewPage->xlp_pageaddr = NewPageBeginPtr;
2097 
2098  /* NewPage->xlp_rem_len = 0; */ /* done by memset */
2099 
2100  /*
2101  * If online backup is not in progress, mark the header to indicate
2102  * that WAL records beginning in this page have removable backup
2103  * blocks. This allows the WAL archiver to know whether it is safe to
2104  * compress archived WAL data by transforming full-block records into
2105  * the non-full-block format. It is sufficient to record this at the
2106  * page level because we force a page switch (in fact a segment
2107  * switch) when starting a backup, so the flag will be off before any
2108  * records can be written during the backup. At the end of a backup,
2109  * the last page will be marked as all unsafe when perhaps only part
2110  * is unsafe, but at worst the archiver would miss the opportunity to
2111  * compress a few records.
2112  */
2113  if (Insert->runningBackups == 0)
2114  NewPage->xlp_info |= XLP_BKP_REMOVABLE;
2115 
2116  /*
2117  * If first page of an XLOG segment file, make it a long header.
2118  */
2119  if ((XLogSegmentOffset(NewPage->xlp_pageaddr, wal_segment_size)) == 0)
2120  {
2121  XLogLongPageHeader NewLongPage = (XLogLongPageHeader) NewPage;
2122 
2123  NewLongPage->xlp_sysid = ControlFile->system_identifier;
2124  NewLongPage->xlp_seg_size = wal_segment_size;
2125  NewLongPage->xlp_xlog_blcksz = XLOG_BLCKSZ;
2126  NewPage->xlp_info |= XLP_LONG_HEADER;
2127  }
2128 
2129  /*
2130  * Make sure the initialization of the page becomes visible to others
2131  * before the xlblocks update. GetXLogBuffer() reads xlblocks without
2132  * holding a lock.
2133  */
2134  pg_write_barrier();
2135 
2136  pg_atomic_write_u64(&XLogCtl->xlblocks[nextidx], NewPageEndPtr);
2137  XLogCtl->InitializedUpTo = NewPageEndPtr;
2138 
2139  npages++;
2140  }
2141  LWLockRelease(WALBufMappingLock);
2142 
2143 #ifdef WAL_DEBUG
2144  if (XLOG_DEBUG && npages > 0)
2145  {
2146  elog(DEBUG1, "initialized %d pages, up to %X/%X",
2147  npages, LSN_FORMAT_ARGS(NewPageEndPtr));
2148  }
2149 #endif
2150 }
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:480
#define pg_write_barrier()
Definition: atomics.h:152
#define pg_attribute_unused()
Definition: c.h:123
#define Assert(condition)
Definition: c.h:858
#define MemSet(start, val, len)
Definition: c.h:1020
size_t Size
Definition: c.h:605
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:224
static void Insert(File file)
Definition: fd.c:1313
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
@ LW_EXCLUSIVE
Definition: lwlock.h:114
PgStat_PendingWalStats PendingWalStats
Definition: pgstat_wal.c:24
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
uint64 system_identifier
Definition: pg_control.h:109
PgStat_Counter wal_buffers_full
Definition: pgstat.h:452
XLogwrtRqst LogwrtRqst
Definition: xlog.c:455
slock_t info_lck
Definition: xlog.c:548
XLogRecPtr InitializedUpTo
Definition: xlog.c:485
char * pages
Definition: xlog.c:492
pg_atomic_uint64 * xlblocks
Definition: xlog.c:493
XLogCtlInsert Insert
Definition: xlog.c:452
TimeLineID xlp_tli
Definition: xlog_internal.h:40
XLogRecPtr xlp_pageaddr
Definition: xlog_internal.h:41
XLogRecPtr Write
Definition: xlog.c:327
XLogRecPtr Flush
Definition: xlog.c:322
XLogRecPtr Write
Definition: xlog.c:321
static XLogRecPtr WaitXLogInsertionsToFinish(XLogRecPtr upto)
Definition: xlog.c:1499
#define RefreshXLogWriteResult(_target)
Definition: xlog.c:615
int wal_segment_size
Definition: xlog.c:143
static XLogwrtResult LogwrtResult
Definition: xlog.c:607
#define XLogRecPtrToBufIdx(recptr)
Definition: xlog.c:586
static void XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
Definition: xlog.c:2309
static ControlFileData * ControlFile
Definition: xlog.c:569
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:71
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:54
#define XLP_LONG_HEADER
Definition: xlog_internal.h:76
#define XLP_BKP_REMOVABLE
Definition: xlog_internal.h:78
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert, ControlFile, DEBUG1, elog, XLogwrtRqst::Flush, XLogCtlData::info_lck, XLogCtlData::InitializedUpTo, XLogCtlData::Insert, Insert(), InvalidXLogRecPtr, LogwrtResult, XLogCtlData::LogwrtRqst, LSN_FORMAT_ARGS, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemSet, XLogCtlData::pages, PendingWalStats, pg_atomic_read_u64(), pg_atomic_write_u64(), pg_attribute_unused, pg_write_barrier, RefreshXLogWriteResult, SpinLockAcquire, SpinLockRelease, ControlFileData::system_identifier, WaitXLogInsertionsToFinish(), PgStat_PendingWalStats::wal_buffers_full, wal_segment_size, XLogwrtRqst::Write, XLogwrtResult::Write, XLogCtlData::xlblocks, XLOG_PAGE_MAGIC, XLogCtl, XLogRecPtrToBufIdx, XLogSegmentOffset, XLogWrite(), XLP_BKP_REMOVABLE, XLP_LONG_HEADER, XLogLongPageHeaderData::xlp_seg_size, XLogLongPageHeaderData::xlp_sysid, and XLogLongPageHeaderData::xlp_xlog_blcksz.

Referenced by GetXLogBuffer(), and XLogBackgroundFlush().

◆ assign_checkpoint_completion_target()

void assign_checkpoint_completion_target ( double  newval,
void *  extra 
)

Definition at line 2193 of file xlog.c.

2194 {
2197 }
double CheckPointCompletionTarget
Definition: checkpointer.c:138
#define newval
static void CalculateCheckpointSegments(void)
Definition: xlog.c:2157

References CalculateCheckpointSegments(), CheckPointCompletionTarget, and newval.

◆ assign_max_wal_size()

void assign_max_wal_size ( int  newval,
void *  extra 
)

Definition at line 2186 of file xlog.c.

2187 {
2190 }
int max_wal_size_mb
Definition: xlog.c:114

References CalculateCheckpointSegments(), max_wal_size_mb, and newval.

◆ assign_wal_consistency_checking()

void assign_wal_consistency_checking ( const char *  newval,
void *  extra 
)

Definition at line 4724 of file xlog.c.

4725 {
4726  /*
4727  * If some checks were deferred, it's possible that the checks will fail
4728  * later during InitializeWalConsistencyChecking(). But in that case, the
4729  * postmaster will exit anyway, so it's safe to proceed with the
4730  * assignment.
4731  *
4732  * Any built-in resource managers specified are assigned immediately,
4733  * which affects WAL created before shared_preload_libraries are
4734  * processed. Any custom resource managers specified won't be assigned
4735  * until after shared_preload_libraries are processed, but that's OK
4736  * because WAL for a custom resource manager can't be written before the
4737  * module is loaded anyway.
4738  */
4739  wal_consistency_checking = extra;
4740 }
bool * wal_consistency_checking
Definition: xlog.c:126

References wal_consistency_checking.

◆ assign_wal_sync_method()

void assign_wal_sync_method ( int  new_wal_sync_method,
void *  extra 
)

Definition at line 8561 of file xlog.c.

8562 {
8563  if (wal_sync_method != new_wal_sync_method)
8564  {
8565  /*
8566  * To ensure that no blocks escape unsynced, force an fsync on the
8567  * currently open log segment (if any). Also, if the open flag is
8568  * changing, close the log file so it will be reopened (with new flag
8569  * bit) at next use.
8570  */
8571  if (openLogFile >= 0)
8572  {
8573  pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN);
8574  if (pg_fsync(openLogFile) != 0)
8575  {
8576  char xlogfname[MAXFNAMELEN];
8577  int save_errno;
8578 
8579  save_errno = errno;
8580  XLogFileName(xlogfname, openLogTLI, openLogSegNo,
8582  errno = save_errno;
8583  ereport(PANIC,
8585  errmsg("could not fsync file \"%s\": %m", xlogfname)));
8586  }
8587 
8589  if (get_sync_bit(wal_sync_method) != get_sync_bit(new_wal_sync_method))
8590  XLogFileClose();
8591  }
8592  }
8593 }
int errcode_for_file_access(void)
Definition: elog.c:880
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define PANIC
Definition: elog.h:42
#define ereport(elevel,...)
Definition: elog.h:149
int pg_fsync(int fd)
Definition: fd.c:386
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:82
static void pgstat_report_wait_end(void)
Definition: wait_event.h:98
static int openLogFile
Definition: xlog.c:630
static int get_sync_bit(int method)
Definition: xlog.c:8513
int wal_sync_method
Definition: xlog.c:130
static TimeLineID openLogTLI
Definition: xlog.c:632
static void XLogFileClose(void)
Definition: xlog.c:3628
static XLogSegNo openLogSegNo
Definition: xlog.c:631
#define MAXFNAMELEN
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)

References ereport, errcode_for_file_access(), errmsg(), get_sync_bit(), MAXFNAMELEN, openLogFile, openLogSegNo, openLogTLI, PANIC, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), wal_segment_size, wal_sync_method, XLogFileClose(), and XLogFileName().

◆ BootStrapXLOG()

void BootStrapXLOG ( void  )

Definition at line 5000 of file xlog.c.

5001 {
5002  CheckPoint checkPoint;
5003  char *buffer;
5004  XLogPageHeader page;
5005  XLogLongPageHeader longpage;
5006  XLogRecord *record;
5007  char *recptr;
5008  uint64 sysidentifier;
5009  struct timeval tv;
5010  pg_crc32c crc;
5011 
5012  /* allow ordinary WAL segment creation, like StartupXLOG() would */
5014 
5015  /*
5016  * Select a hopefully-unique system identifier code for this installation.
5017  * We use the result of gettimeofday(), including the fractional seconds
5018  * field, as being about as unique as we can easily get. (Think not to
5019  * use random(), since it hasn't been seeded and there's no portable way
5020  * to seed it other than the system clock value...) The upper half of the
5021  * uint64 value is just the tv_sec part, while the lower half contains the
5022  * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
5023  * PID for a little extra uniqueness. A person knowing this encoding can
5024  * determine the initialization time of the installation, which could
5025  * perhaps be useful sometimes.
5026  */
5027  gettimeofday(&tv, NULL);
5028  sysidentifier = ((uint64) tv.tv_sec) << 32;
5029  sysidentifier |= ((uint64) tv.tv_usec) << 12;
5030  sysidentifier |= getpid() & 0xFFF;
5031 
5032  /* page buffer must be aligned suitably for O_DIRECT */
5033  buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
5034  page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
5035  memset(page, 0, XLOG_BLCKSZ);
5036 
5037  /*
5038  * Set up information for the initial checkpoint record
5039  *
5040  * The initial checkpoint record is written to the beginning of the WAL
5041  * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not
5042  * used, so that we can use 0/0 to mean "before any valid WAL segment".
5043  */
5044  checkPoint.redo = wal_segment_size + SizeOfXLogLongPHD;
5045  checkPoint.ThisTimeLineID = BootstrapTimeLineID;
5046  checkPoint.PrevTimeLineID = BootstrapTimeLineID;
5047  checkPoint.fullPageWrites = fullPageWrites;
5048  checkPoint.nextXid =
5050  checkPoint.nextOid = FirstGenbkiObjectId;
5051  checkPoint.nextMulti = FirstMultiXactId;
5052  checkPoint.nextMultiOffset = 0;
5053  checkPoint.oldestXid = FirstNormalTransactionId;
5054  checkPoint.oldestXidDB = Template1DbOid;
5055  checkPoint.oldestMulti = FirstMultiXactId;
5056  checkPoint.oldestMultiDB = Template1DbOid;
5059  checkPoint.time = (pg_time_t) time(NULL);
5061 
5062  TransamVariables->nextXid = checkPoint.nextXid;
5063  TransamVariables->nextOid = checkPoint.nextOid;
5065  MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5066  AdvanceOldestClogXid(checkPoint.oldestXid);
5067  SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5068  SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5070 
5071  /* Set up the XLOG page header */
5072  page->xlp_magic = XLOG_PAGE_MAGIC;
5073  page->xlp_info = XLP_LONG_HEADER;
5074  page->xlp_tli = BootstrapTimeLineID;
5076  longpage = (XLogLongPageHeader) page;
5077  longpage->xlp_sysid = sysidentifier;
5078  longpage->xlp_seg_size = wal_segment_size;
5079  longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
5080 
5081  /* Insert the initial checkpoint record */
5082  recptr = ((char *) page + SizeOfXLogLongPHD);
5083  record = (XLogRecord *) recptr;
5084  record->xl_prev = 0;
5085  record->xl_xid = InvalidTransactionId;
5086  record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(checkPoint);
5088  record->xl_rmid = RM_XLOG_ID;
5089  recptr += SizeOfXLogRecord;
5090  /* fill the XLogRecordDataHeaderShort struct */
5091  *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
5092  *(recptr++) = sizeof(checkPoint);
5093  memcpy(recptr, &checkPoint, sizeof(checkPoint));
5094  recptr += sizeof(checkPoint);
5095  Assert(recptr - (char *) record == record->xl_tot_len);
5096 
5097  INIT_CRC32C(crc);
5098  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
5099  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
5100  FIN_CRC32C(crc);
5101  record->xl_crc = crc;
5102 
5103  /* Create first XLOG segment file */
5106 
5107  /*
5108  * We needn't bother with Reserve/ReleaseExternalFD here, since we'll
5109  * close the file again in a moment.
5110  */
5111 
5112  /* Write the first page with the initial record */
5113  errno = 0;
5114  pgstat_report_wait_start(WAIT_EVENT_WAL_BOOTSTRAP_WRITE);
5115  if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
5116  {
5117  /* if write didn't set errno, assume problem is no disk space */
5118  if (errno == 0)
5119  errno = ENOSPC;
5120  ereport(PANIC,
5122  errmsg("could not write bootstrap write-ahead log file: %m")));
5123  }
5125 
5126  pgstat_report_wait_start(WAIT_EVENT_WAL_BOOTSTRAP_SYNC);
5127  if (pg_fsync(openLogFile) != 0)
5128  ereport(PANIC,
5130  errmsg("could not fsync bootstrap write-ahead log file: %m")));
5132 
5133  if (close(openLogFile) != 0)
5134  ereport(PANIC,
5136  errmsg("could not close bootstrap write-ahead log file: %m")));
5137 
5138  openLogFile = -1;
5139 
5140  /* Now create pg_control */
5141  InitControlFile(sysidentifier);
5142  ControlFile->time = checkPoint.time;
5143  ControlFile->checkPoint = checkPoint.redo;
5144  ControlFile->checkPointCopy = checkPoint;
5145 
5146  /* some additional ControlFile fields are set in WriteControlFile() */
5147  WriteControlFile();
5148 
5149  /* Bootstrap the commit log, too */
5150  BootStrapCLOG();
5154 
5155  pfree(buffer);
5156 
5157  /*
5158  * Force control file to be read - in contrast to normal processing we'd
5159  * otherwise never run the checks and GUC related initializations therein.
5160  */
5161  ReadControlFile();
5162 }
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:804
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
#define close(a)
Definition: win32.h:12
#define write(a, b, c)
Definition: win32.h:14
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc(Size size)
Definition: mcxt.c:1316
void MultiXactSetNextMXact(MultiXactId nextMulti, MultiXactOffset nextMultiOffset)
Definition: multixact.c:2279
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2313
void BootStrapMultiXact(void)
Definition: multixact.c:1985
#define FirstMultiXactId
Definition: multixact.h:25
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:67
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:98
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:103
return crc
int64 pg_time_t
Definition: pgtime.h:23
Oid oldestMultiDB
Definition: pg_control.h:50
MultiXactId oldestMulti
Definition: pg_control.h:49
MultiXactOffset nextMultiOffset
Definition: pg_control.h:46
TransactionId newestCommitTsXid
Definition: pg_control.h:54
TransactionId oldestXid
Definition: pg_control.h:47
TimeLineID PrevTimeLineID
Definition: pg_control.h:40
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
Oid nextOid
Definition: pg_control.h:44
TransactionId oldestActiveXid
Definition: pg_control.h:63
bool fullPageWrites
Definition: pg_control.h:42
MultiXactId nextMulti
Definition: pg_control.h:45
FullTransactionId nextXid
Definition: pg_control.h:43
TransactionId oldestCommitTsXid
Definition: pg_control.h:52
pg_time_t time
Definition: pg_control.h:51
XLogRecPtr redo
Definition: pg_control.h:37
Oid oldestXidDB
Definition: pg_control.h:48
CheckPoint checkPointCopy
Definition: pg_control.h:134
pg_time_t time
Definition: pg_control.h:131
XLogRecPtr checkPoint
Definition: pg_control.h:132
FullTransactionId nextXid
Definition: transam.h:220
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
int gettimeofday(struct timeval *tp, void *tzp)
int XLogFileInit(XLogSegNo logsegno, TimeLineID logtli)
Definition: xlog.c:3369
bool fullPageWrites
Definition: xlog.c:122
static void InitControlFile(uint64 sysidentifier)
Definition: xlog.c:4193
void SetInstallXLogFileSegmentActive(void)
Definition: xlog.c:9404
static void WriteControlFile(void)
Definition: xlog.c:4228
#define BootstrapTimeLineID
Definition: xlog.c:111
static void ReadControlFile(void)
Definition: xlog.c:4310
#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_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().

◆ CalculateCheckpointSegments()

static void CalculateCheckpointSegments ( void  )
static

Definition at line 2157 of file xlog.c.

2158 {
2159  double target;
2160 
2161  /*-------
2162  * Calculate the distance at which to trigger a checkpoint, to avoid
2163  * exceeding max_wal_size_mb. This is based on two assumptions:
2164  *
2165  * a) we keep WAL for only one checkpoint cycle (prior to PG11 we kept
2166  * WAL for two checkpoint cycles to allow us to recover from the
2167  * secondary checkpoint if the first checkpoint failed, though we
2168  * only did this on the primary anyway, not on standby. Keeping just
2169  * one checkpoint simplifies processing and reduces disk space in
2170  * many smaller databases.)
2171  * b) during checkpoint, we consume checkpoint_completion_target *
2172  * number of segments consumed between checkpoints.
2173  *-------
2174  */
2175  target = (double) ConvertToXSegs(max_wal_size_mb, wal_segment_size) /
2177 
2178  /* round down */
2179  CheckPointSegments = (int) target;
2180 
2181  if (CheckPointSegments < 1)
2182  CheckPointSegments = 1;
2183 }
#define ConvertToXSegs(x, segsize)
Definition: xlog.c:598
int CheckPointSegments
Definition: xlog.c:156

References CheckPointCompletionTarget, CheckPointSegments, ConvertToXSegs, max_wal_size_mb, and wal_segment_size.

Referenced by assign_checkpoint_completion_target(), assign_max_wal_size(), and ReadControlFile().

◆ check_max_slot_wal_keep_size()

bool check_max_slot_wal_keep_size ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 2218 of file xlog.c.

2219 {
2220  if (IsBinaryUpgrade && *newval != -1)
2221  {
2222  GUC_check_errdetail("\"%s\" must be set to -1 during binary upgrade mode.",
2223  "max_slot_wal_keep_size");
2224  return false;
2225  }
2226 
2227  return true;
2228 }
bool IsBinaryUpgrade
Definition: globals.c:118
#define GUC_check_errdetail
Definition: guc.h:447

References GUC_check_errdetail, IsBinaryUpgrade, and newval.

◆ check_wal_buffers()

bool check_wal_buffers ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 4604 of file xlog.c.

4605 {
4606  /*
4607  * -1 indicates a request for auto-tune.
4608  */
4609  if (*newval == -1)
4610  {
4611  /*
4612  * If we haven't yet changed the boot_val default of -1, just let it
4613  * be. We'll fix it when XLOGShmemSize is called.
4614  */
4615  if (XLOGbuffers == -1)
4616  return true;
4617 
4618  /* Otherwise, substitute the auto-tune value */
4620  }
4621 
4622  /*
4623  * We clamp manually-set values to at least 4 blocks. Prior to PostgreSQL
4624  * 9.1, a minimum of 4 was enforced by guc.c, but since that is no longer
4625  * the case, we just silently treat such values as a request for the
4626  * minimum. (We could throw an error instead, but that doesn't seem very
4627  * helpful.)
4628  */
4629  if (*newval < 4)
4630  *newval = 4;
4631 
4632  return true;
4633 }
static int XLOGChooseNumBuffers(void)
Definition: xlog.c:4588
int XLOGbuffers
Definition: xlog.c:117

References newval, XLOGbuffers, and XLOGChooseNumBuffers().

◆ check_wal_consistency_checking()

bool check_wal_consistency_checking ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 4639 of file xlog.c.

4640 {
4641  char *rawstring;
4642  List *elemlist;
4643  ListCell *l;
4644  bool newwalconsistency[RM_MAX_ID + 1];
4645 
4646  /* Initialize the array */
4647  MemSet(newwalconsistency, 0, (RM_MAX_ID + 1) * sizeof(bool));
4648 
4649  /* Need a modifiable copy of string */
4650  rawstring = pstrdup(*newval);
4651 
4652  /* Parse string into list of identifiers */
4653  if (!SplitIdentifierString(rawstring, ',', &elemlist))
4654  {
4655  /* syntax error in list */
4656  GUC_check_errdetail("List syntax is invalid.");
4657  pfree(rawstring);
4658  list_free(elemlist);
4659  return false;
4660  }
4661 
4662  foreach(l, elemlist)
4663  {
4664  char *tok = (char *) lfirst(l);
4665  int rmid;
4666 
4667  /* Check for 'all'. */
4668  if (pg_strcasecmp(tok, "all") == 0)
4669  {
4670  for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
4671  if (RmgrIdExists(rmid) && GetRmgr(rmid).rm_mask != NULL)
4672  newwalconsistency[rmid] = true;
4673  }
4674  else
4675  {
4676  /* Check if the token matches any known resource manager. */
4677  bool found = false;
4678 
4679  for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
4680  {
4681  if (RmgrIdExists(rmid) && GetRmgr(rmid).rm_mask != NULL &&
4682  pg_strcasecmp(tok, GetRmgr(rmid).rm_name) == 0)
4683  {
4684  newwalconsistency[rmid] = true;
4685  found = true;
4686  break;
4687  }
4688  }
4689  if (!found)
4690  {
4691  /*
4692  * During startup, it might be a not-yet-loaded custom
4693  * resource manager. Defer checking until
4694  * InitializeWalConsistencyChecking().
4695  */
4697  {
4699  }
4700  else
4701  {
4702  GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
4703  pfree(rawstring);
4704  list_free(elemlist);
4705  return false;
4706  }
4707  }
4708  }
4709  }
4710 
4711  pfree(rawstring);
4712  list_free(elemlist);
4713 
4714  /* assign new value */
4715  *extra = guc_malloc(ERROR, (RM_MAX_ID + 1) * sizeof(bool));
4716  memcpy(*extra, newwalconsistency, (RM_MAX_ID + 1) * sizeof(bool));
4717  return true;
4718 }
#define ERROR
Definition: elog.h:39
void * guc_malloc(int elevel, size_t size)
Definition: guc.c:640
void list_free(List *list)
Definition: list.c:1546
char * pstrdup(const char *in)
Definition: mcxt.c:1695
bool process_shared_preload_libraries_done
Definition: miscinit.c:1779
#define lfirst(lc)
Definition: pg_list.h:172
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define RM_MAX_ID
Definition: rmgr.h:33
Definition: pg_list.h:54
void(* rm_mask)(char *pagedata, BlockNumber blkno)
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3457
static bool check_wal_consistency_checking_deferred
Definition: xlog.c:166
static RmgrData GetRmgr(RmgrId rmid)
static bool RmgrIdExists(RmgrId rmid)

References check_wal_consistency_checking_deferred, ERROR, GetRmgr(), GUC_check_errdetail, guc_malloc(), lfirst, list_free(), MemSet, newval, pfree(), pg_strcasecmp(), process_shared_preload_libraries_done, pstrdup(), RmgrData::rm_mask, RM_MAX_ID, RmgrIdExists(), and SplitIdentifierString().

◆ check_wal_segment_size()

bool check_wal_segment_size ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 2200 of file xlog.c.

2201 {
2202  if (!IsValidWalSegSize(*newval))
2203  {
2204  GUC_check_errdetail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
2205  return false;
2206  }
2207 
2208  return true;
2209 }
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96

References GUC_check_errdetail, IsValidWalSegSize, and newval.

◆ CheckPointGuts()

static void CheckPointGuts ( XLogRecPtr  checkPointRedo,
int  flags 
)
static

Definition at line 7431 of file xlog.c.

7432 {
7438 
7439  /* Write out all dirty data in SLRUs and the main buffer pool */
7440  TRACE_POSTGRESQL_BUFFER_CHECKPOINT_START(flags);
7442  CheckPointCLOG();
7447  CheckPointBuffers(flags);
7448 
7449  /* Perform all queued up fsyncs */
7450  TRACE_POSTGRESQL_BUFFER_CHECKPOINT_SYNC_START();
7454  TRACE_POSTGRESQL_BUFFER_CHECKPOINT_DONE();
7455 
7456  /* We deliberately delay 2PC checkpointing as long as possible */
7457  CheckPointTwoPhase(checkPointRedo);
7458 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
void CheckPointBuffers(int flags)
Definition: bufmgr.c:3699
void CheckPointCLOG(void)
Definition: clog.c:937
void CheckPointCommitTs(void)
Definition: commit_ts.c:820
void CheckPointMultiXact(void)
Definition: multixact.c:2255
void CheckPointReplicationOrigin(void)
Definition: origin.c:573
void CheckPointPredicate(void)
Definition: predicate.c:1036
void CheckPointRelationMap(void)
Definition: relmapper.c:611
void CheckPointLogicalRewriteHeap(void)
Definition: rewriteheap.c:1155
void CheckPointReplicationSlots(bool is_shutdown)
Definition: slot.c:1839
void CheckPointSnapBuild(void)
Definition: snapbuild.c:2054
TimestampTz ckpt_write_t
Definition: xlog.h:160
TimestampTz ckpt_sync_end_t
Definition: xlog.h:162
TimestampTz ckpt_sync_t
Definition: xlog.h:161
void CheckPointSUBTRANS(void)
Definition: subtrans.c:355
void ProcessSyncRequests(void)
Definition: sync.c:286
void CheckPointTwoPhase(XLogRecPtr redo_horizon)
Definition: twophase.c:1816
CheckpointStatsData CheckpointStats
Definition: xlog.c:209
#define CHECKPOINT_IS_SHUTDOWN
Definition: xlog.h:137

References CHECKPOINT_IS_SHUTDOWN, CheckPointBuffers(), CheckPointCLOG(), CheckPointCommitTs(), CheckPointLogicalRewriteHeap(), CheckPointMultiXact(), CheckPointPredicate(), CheckPointRelationMap(), CheckPointReplicationOrigin(), CheckPointReplicationSlots(), CheckPointSnapBuild(), CheckpointStats, CheckPointSUBTRANS(), CheckPointTwoPhase(), CheckpointStatsData::ckpt_sync_end_t, CheckpointStatsData::ckpt_sync_t, CheckpointStatsData::ckpt_write_t, GetCurrentTimestamp(), and ProcessSyncRequests().

Referenced by CreateCheckPoint(), and CreateRestartPoint().

◆ CheckRequiredParameterValues()

static void CheckRequiredParameterValues ( void  )
static

Definition at line 5344 of file xlog.c.

5345 {
5346  /*
5347  * For archive recovery, the WAL must be generated with at least 'replica'
5348  * wal_level.
5349  */
5351  {
5352  ereport(FATAL,
5353  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
5354  errmsg("WAL was generated with \"wal_level=minimal\", cannot continue recovering"),
5355  errdetail("This happens if you temporarily set \"wal_level=minimal\" on the server."),
5356  errhint("Use a backup taken after setting \"wal_level\" to higher than \"minimal\".")));
5357  }
5358 
5359  /*
5360  * For Hot Standby, the WAL must be generated with 'replica' mode, and we
5361  * must have at least as many backend slots as the primary.
5362  */
5364  {
5365  /* We ignore autovacuum_max_workers when we make this test. */
5366  RecoveryRequiresIntParameter("max_connections",
5369  RecoveryRequiresIntParameter("max_worker_processes",
5372  RecoveryRequiresIntParameter("max_wal_senders",
5375  RecoveryRequiresIntParameter("max_prepared_transactions",
5378  RecoveryRequiresIntParameter("max_locks_per_transaction",
5381  }
5382 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:857
#define FATAL
Definition: elog.h:41
int MaxConnections
Definition: globals.c:140
int max_worker_processes
Definition: globals.c:141
int max_locks_per_xact
Definition: lock.c:53
int max_worker_processes
Definition: pg_control.h:180
int max_locks_per_xact
Definition: pg_control.h:183
int max_prepared_xacts
Definition: pg_control.h:182
int max_prepared_xacts
Definition: twophase.c:115
int max_wal_senders
Definition: walsender.c:121
bool EnableHotStandby
Definition: xlog.c:121
@ WAL_LEVEL_MINIMAL
Definition: xlog.h:72
bool ArchiveRecoveryRequested
Definition: xlogrecovery.c:137
void RecoveryRequiresIntParameter(const char *param_name, int currValue, int minValue)

References ArchiveRecoveryRequested, ControlFile, EnableHotStandby, ereport, errcode(), errdetail(), errhint(), errmsg(), FATAL, max_locks_per_xact, ControlFileData::max_locks_per_xact, max_prepared_xacts, ControlFileData::max_prepared_xacts, max_wal_senders, ControlFileData::max_wal_senders, max_worker_processes, ControlFileData::max_worker_processes, MaxConnections, ControlFileData::MaxConnections, RecoveryRequiresIntParameter(), ControlFileData::wal_level, and WAL_LEVEL_MINIMAL.

Referenced by StartupXLOG(), and xlog_redo().

◆ CheckXLogRemoved()

void CheckXLogRemoved ( XLogSegNo  segno,
TimeLineID  tli 
)

Definition at line 3716 of file xlog.c.

3717 {
3718  int save_errno = errno;
3719  XLogSegNo lastRemovedSegNo;
3720 
3722  lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3724 
3725  if (segno <= lastRemovedSegNo)
3726  {
3727  char filename[MAXFNAMELEN];
3728 
3729  XLogFileName(filename, tli, segno, wal_segment_size);
3730  errno = save_errno;
3731  ereport(ERROR,
3733  errmsg("requested WAL segment %s has already been removed",
3734  filename)));
3735  }
3736  errno = save_errno;
3737 }
static char * filename
Definition: pg_dumpall.c:119
XLogSegNo lastRemovedSegNo
Definition: xlog.c:461
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().

◆ CleanupAfterArchiveRecovery()

static void CleanupAfterArchiveRecovery ( TimeLineID  EndOfLogTLI,
XLogRecPtr  EndOfLog,
TimeLineID  newTLI 
)
static

Definition at line 5255 of file xlog.c.

5257 {
5258  /*
5259  * Execute the recovery_end_command, if any.
5260  */
5261  if (recoveryEndCommand && strcmp(recoveryEndCommand, "") != 0)
5263  "recovery_end_command",
5264  true,
5265  WAIT_EVENT_RECOVERY_END_COMMAND);
5266 
5267  /*
5268  * We switched to a new timeline. Clean up segments on the old timeline.
5269  *
5270  * If there are any higher-numbered segments on the old timeline, remove
5271  * them. They might contain valid WAL, but they might also be
5272  * pre-allocated files containing garbage. In any case, they are not part
5273  * of the new timeline's history so we don't need them.
5274  */
5275  RemoveNonParentXlogFiles(EndOfLog, newTLI);
5276 
5277  /*
5278  * If the switch happened in the middle of a segment, what to do with the
5279  * last, partial segment on the old timeline? If we don't archive it, and
5280  * the server that created the WAL never archives it either (e.g. because
5281  * it was hit by a meteor), it will never make it to the archive. That's
5282  * OK from our point of view, because the new segment that we created with
5283  * the new TLI contains all the WAL from the old timeline up to the switch
5284  * point. But if you later try to do PITR to the "missing" WAL on the old
5285  * timeline, recovery won't find it in the archive. It's physically
5286  * present in the new file with new TLI, but recovery won't look there
5287  * when it's recovering to the older timeline. On the other hand, if we
5288  * archive the partial segment, and the original server on that timeline
5289  * is still running and archives the completed version of the same segment
5290  * later, it will fail. (We used to do that in 9.4 and below, and it
5291  * caused such problems).
5292  *
5293  * As a compromise, we rename the last segment with the .partial suffix,
5294  * and archive it. Archive recovery will never try to read .partial
5295  * segments, so they will normally go unused. But in the odd PITR case,
5296  * the administrator can copy them manually to the pg_wal directory
5297  * (removing the suffix). They can be useful in debugging, too.
5298  *
5299  * If a .done or .ready file already exists for the old timeline, however,
5300  * we had already determined that the segment is complete, so we can let
5301  * it be archived normally. (In particular, if it was restored from the
5302  * archive to begin with, it's expected to have a .done file).
5303  */
5304  if (XLogSegmentOffset(EndOfLog, wal_segment_size) != 0 &&
5306  {
5307  char origfname[MAXFNAMELEN];
5308  XLogSegNo endLogSegNo;
5309 
5310  XLByteToPrevSeg(EndOfLog, endLogSegNo, wal_segment_size);
5311  XLogFileName(origfname, EndOfLogTLI, endLogSegNo, wal_segment_size);
5312 
5313  if (!XLogArchiveIsReadyOrDone(origfname))
5314  {
5315  char origpath[MAXPGPATH];
5316  char partialfname[MAXFNAMELEN];
5317  char partialpath[MAXPGPATH];
5318 
5319  XLogFilePath(origpath, EndOfLogTLI, endLogSegNo, wal_segment_size);
5320  snprintf(partialfname, MAXFNAMELEN, "%s.partial", origfname);
5321  snprintf(partialpath, MAXPGPATH, "%s.partial", origpath);
5322 
5323  /*
5324  * Make sure there's no .done or .ready file for the .partial
5325  * file.
5326  */
5327  XLogArchiveCleanup(partialfname);
5328 
5329  durable_rename(origpath, partialpath, ERROR);
5330  XLogArchiveNotify(partialfname);
5331  }
5332  }
5333 }
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:782
#define MAXPGPATH
#define snprintf
Definition: port.h:238
void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
Definition: xlog.c:3929
#define XLogArchivingActive()
Definition: xlog.h:97
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
bool XLogArchiveIsReadyOrDone(const char *xlog)
Definition: xlogarchive.c:664
void ExecuteRecoveryCommand(const char *command, const char *commandName, bool failOnSignal, uint32 wait_event_info)
Definition: xlogarchive.c:295
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:444
void XLogArchiveCleanup(const char *xlog)
Definition: xlogarchive.c:712
char * recoveryEndCommand
Definition: xlogrecovery.c:83

References durable_rename(), ERROR, ExecuteRecoveryCommand(), MAXFNAMELEN, MAXPGPATH, recoveryEndCommand, RemoveNonParentXlogFiles(), snprintf, wal_segment_size, XLByteToPrevSeg, XLogArchiveCleanup(), XLogArchiveIsReadyOrDone(), XLogArchiveNotify(), XLogArchivingActive, XLogFileName(), XLogFilePath(), and XLogSegmentOffset.

Referenced by StartupXLOG().

◆ CleanupBackupHistory()

static void CleanupBackupHistory ( void  )
static

Definition at line 4150 of file xlog.c.

4151 {
4152  DIR *xldir;
4153  struct dirent *xlde;
4154  char path[MAXPGPATH + sizeof(XLOGDIR)];
4155 
4156  xldir = AllocateDir(XLOGDIR);
4157 
4158  while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
4159  {
4160  if (IsBackupHistoryFileName(xlde->d_name))
4161  {
4162  if (XLogArchiveCheckDone(xlde->d_name))
4163  {
4164  elog(DEBUG2, "removing WAL backup history file \"%s\"",
4165  xlde->d_name);
4166  snprintf(path, sizeof(path), XLOGDIR "/%s", xlde->d_name);
4167  unlink(path);
4168  XLogArchiveCleanup(xlde->d_name);
4169  }
4170  }
4171  }
4172 
4173  FreeDir(xldir);
4174 }
#define DEBUG2
Definition: elog.h:29
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2909
int FreeDir(DIR *dir)
Definition: fd.c:2961
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2843
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
#define XLOGDIR
static bool IsBackupHistoryFileName(const char *fname)
bool XLogArchiveCheckDone(const char *xlog)
Definition: xlogarchive.c:565

References AllocateDir(), dirent::d_name, DEBUG2, elog, FreeDir(), IsBackupHistoryFileName(), MAXPGPATH, ReadDir(), snprintf, XLogArchiveCheckDone(), XLogArchiveCleanup(), and XLOGDIR.

Referenced by do_pg_backup_stop().

◆ CopyXLogRecordToWAL()

static void CopyXLogRecordToWAL ( int  write_len,
bool  isLogSwitch,
XLogRecData rdata,
XLogRecPtr  StartPos,
XLogRecPtr  EndPos,
TimeLineID  tli 
)
static

Definition at line 1220 of file xlog.c.

1222 {
1223  char *currpos;
1224  int freespace;
1225  int written;
1226  XLogRecPtr CurrPos;
1227  XLogPageHeader pagehdr;
1228 
1229  /*
1230  * Get a pointer to the right place in the right WAL buffer to start
1231  * inserting to.
1232  */
1233  CurrPos = StartPos;
1234  currpos = GetXLogBuffer(CurrPos, tli);
1235  freespace = INSERT_FREESPACE(CurrPos);
1236 
1237  /*
1238  * there should be enough space for at least the first field (xl_tot_len)
1239  * on this page.
1240  */
1241  Assert(freespace >= sizeof(uint32));
1242 
1243  /* Copy record data */
1244  written = 0;
1245  while (rdata != NULL)
1246  {
1247  char *rdata_data = rdata->data;
1248  int rdata_len = rdata->len;
1249 
1250  while (rdata_len > freespace)
1251  {
1252  /*
1253  * Write what fits on this page, and continue on the next page.
1254  */
1255  Assert(CurrPos % XLOG_BLCKSZ >= SizeOfXLogShortPHD || freespace == 0);
1256  memcpy(currpos, rdata_data, freespace);
1257  rdata_data += freespace;
1258  rdata_len -= freespace;
1259  written += freespace;
1260  CurrPos += freespace;
1261 
1262  /*
1263  * Get pointer to beginning of next page, and set the xlp_rem_len
1264  * in the page header. Set XLP_FIRST_IS_CONTRECORD.
1265  *
1266  * It's safe to set the contrecord flag and xlp_rem_len without a
1267  * lock on the page. All the other flags were already set when the
1268  * page was initialized, in AdvanceXLInsertBuffer, and we're the
1269  * only backend that needs to set the contrecord flag.
1270  */
1271  currpos = GetXLogBuffer(CurrPos, tli);
1272  pagehdr = (XLogPageHeader) currpos;
1273  pagehdr->xlp_rem_len = write_len - written;
1274  pagehdr->xlp_info |= XLP_FIRST_IS_CONTRECORD;
1275 
1276  /* skip over the page header */
1277  if (XLogSegmentOffset(CurrPos, wal_segment_size) == 0)
1278  {
1279  CurrPos += SizeOfXLogLongPHD;
1280  currpos += SizeOfXLogLongPHD;
1281  }
1282  else
1283  {
1284  CurrPos += SizeOfXLogShortPHD;
1285  currpos += SizeOfXLogShortPHD;
1286  }
1287  freespace = INSERT_FREESPACE(CurrPos);
1288  }
1289 
1290  Assert(CurrPos % XLOG_BLCKSZ >= SizeOfXLogShortPHD || rdata_len == 0);
1291  memcpy(currpos, rdata_data, rdata_len);
1292  currpos += rdata_len;
1293  CurrPos += rdata_len;
1294  freespace -= rdata_len;
1295  written += rdata_len;
1296 
1297  rdata = rdata->next;
1298  }
1299  Assert(written == write_len);
1300 
1301  /*
1302  * If this was an xlog-switch, it's not enough to write the switch record,
1303  * we also have to consume all the remaining space in the WAL segment. We
1304  * have already reserved that space, but we need to actually fill it.
1305  */
1306  if (isLogSwitch && XLogSegmentOffset(CurrPos, wal_segment_size) != 0)
1307  {
1308  /* An xlog-switch record doesn't contain any data besides the header */
1309  Assert(write_len == SizeOfXLogRecord);
1310 
1311  /* Assert that we did reserve the right amount of space */
1312  Assert(XLogSegmentOffset(EndPos, wal_segment_size) == 0);
1313 
1314  /* Use up all the remaining space on the current page */
1315  CurrPos += freespace;
1316 
1317  /*
1318  * Cause all remaining pages in the segment to be flushed, leaving the
1319  * XLog position where it should be, at the start of the next segment.
1320  * We do this one page at a time, to make sure we don't deadlock
1321  * against ourselves if wal_buffers < wal_segment_size.
1322  */
1323  while (CurrPos < EndPos)
1324  {
1325  /*
1326  * The minimal action to flush the page would be to call
1327  * WALInsertLockUpdateInsertingAt(CurrPos) followed by
1328  * AdvanceXLInsertBuffer(...). The page would be left initialized
1329  * mostly to zeros, except for the page header (always the short
1330  * variant, as this is never a segment's first page).
1331  *
1332  * The large vistas of zeros are good for compressibility, but the
1333  * headers interrupting them every XLOG_BLCKSZ (with values that
1334  * differ from page to page) are not. The effect varies with
1335  * compression tool, but bzip2 for instance compresses about an
1336  * order of magnitude worse if those headers are left in place.
1337  *
1338  * Rather than complicating AdvanceXLInsertBuffer itself (which is
1339  * called in heavily-loaded circumstances as well as this lightly-
1340  * loaded one) with variant behavior, we just use GetXLogBuffer
1341  * (which itself calls the two methods we need) to get the pointer
1342  * and zero most of the page. Then we just zero the page header.
1343  */
1344  currpos = GetXLogBuffer(CurrPos, tli);
1345  MemSet(currpos, 0, SizeOfXLogShortPHD);
1346 
1347  CurrPos += XLOG_BLCKSZ;
1348  }
1349  }
1350  else
1351  {
1352  /* Align the end position, so that the next record starts aligned */
1353  CurrPos = MAXALIGN64(CurrPos);
1354  }
1355 
1356  if (CurrPos != EndPos)
1357  ereport(PANIC,
1359  errmsg_internal("space reserved for WAL record does not match what was written"));
1360 }
unsigned int uint32
Definition: c.h:506
#define MAXALIGN64(LEN)
Definition: c.h:836
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
struct XLogRecData * next
#define INSERT_FREESPACE(endptr)
Definition: xlog.c:575
static char * GetXLogBuffer(XLogRecPtr ptr, TimeLineID tli)
Definition: xlog.c:1627
#define XLP_FIRST_IS_CONTRECORD
Definition: xlog_internal.h:74
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:52

References Assert, XLogRecData::data, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), GetXLogBuffer(), INSERT_FREESPACE, XLogRecData::len, MAXALIGN64, MemSet, XLogRecData::next, PANIC, SizeOfXLogLongPHD, SizeOfXLogRecord, SizeOfXLogShortPHD, wal_segment_size, XLogSegmentOffset, XLP_FIRST_IS_CONTRECORD, XLogPageHeaderData::xlp_info, and XLogPageHeaderData::xlp_rem_len.

Referenced by XLogInsertRecord().

◆ CreateCheckPoint()

void CreateCheckPoint ( int  flags)

Definition at line 6821 of file xlog.c.

6822 {
6823  bool shutdown;
6824  CheckPoint checkPoint;
6825  XLogRecPtr recptr;
6826  XLogSegNo _logSegNo;
6828  uint32 freespace;
6829  XLogRecPtr PriorRedoPtr;
6830  XLogRecPtr last_important_lsn;
6831  VirtualTransactionId *vxids;
6832  int nvxids;
6833  int oldXLogAllowed = 0;
6834 
6835  /*
6836  * An end-of-recovery checkpoint is really a shutdown checkpoint, just
6837  * issued at a different time.
6838  */
6840  shutdown = true;
6841  else
6842  shutdown = false;
6843 
6844  /* sanity check */
6845  if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
6846  elog(ERROR, "can't create a checkpoint during recovery");
6847 
6848  /*
6849  * Prepare to accumulate statistics.
6850  *
6851  * Note: because it is possible for log_checkpoints to change while a
6852  * checkpoint proceeds, we always accumulate stats, even if
6853  * log_checkpoints is currently off.
6854  */
6855  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
6857 
6858  /*
6859  * Let smgr prepare for checkpoint; this has to happen outside the
6860  * critical section and before we determine the REDO pointer. Note that
6861  * smgr must not do anything that'd have to be undone if we decide no
6862  * checkpoint is needed.
6863  */
6865 
6866  /*
6867  * Use a critical section to force system panic if we have trouble.
6868  */
6870 
6871  if (shutdown)
6872  {
6873  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6876  LWLockRelease(ControlFileLock);
6877  }
6878 
6879  /* Begin filling in the checkpoint WAL record */
6880  MemSet(&checkPoint, 0, sizeof(checkPoint));
6881  checkPoint.time = (pg_time_t) time(NULL);
6882 
6883  /*
6884  * For Hot Standby, derive the oldestActiveXid before we fix the redo
6885  * pointer. This allows us to begin accumulating changes to assemble our
6886  * starting snapshot of locks and transactions.
6887  */
6888  if (!shutdown && XLogStandbyInfoActive())
6890  else
6892 
6893  /*
6894  * Get location of last important record before acquiring insert locks (as
6895  * GetLastImportantRecPtr() also locks WAL locks).
6896  */
6897  last_important_lsn = GetLastImportantRecPtr();
6898 
6899  /*
6900  * If this isn't a shutdown or forced checkpoint, and if there has been no
6901  * WAL activity requiring a checkpoint, skip it. The idea here is to
6902  * avoid inserting duplicate checkpoints when the system is idle.
6903  */
6905  CHECKPOINT_FORCE)) == 0)
6906  {
6907  if (last_important_lsn == ControlFile->checkPoint)
6908  {
6909  END_CRIT_SECTION();
6910  ereport(DEBUG1,
6911  (errmsg_internal("checkpoint skipped because system is idle")));
6912  return;
6913  }
6914  }
6915 
6916  /*
6917  * An end-of-recovery checkpoint is created before anyone is allowed to
6918  * write WAL. To allow us to write the checkpoint record, temporarily
6919  * enable XLogInsertAllowed.
6920  */
6921  if (flags & CHECKPOINT_END_OF_RECOVERY)
6922  oldXLogAllowed = LocalSetXLogInsertAllowed();
6923 
6924  checkPoint.ThisTimeLineID = XLogCtl->InsertTimeLineID;
6925  if (flags & CHECKPOINT_END_OF_RECOVERY)
6926  checkPoint.PrevTimeLineID = XLogCtl->PrevTimeLineID;
6927  else
6928  checkPoint.PrevTimeLineID = checkPoint.ThisTimeLineID;
6929 
6930  /*
6931  * We must block concurrent insertions while examining insert state.
6932  */
6934 
6935  checkPoint.fullPageWrites = Insert->fullPageWrites;
6936 
6937  if (shutdown)
6938  {
6939  XLogRecPtr curInsert = XLogBytePosToRecPtr(Insert->CurrBytePos);
6940 
6941  /*
6942  * Compute new REDO record ptr = location of next XLOG record.
6943  *
6944  * Since this is a shutdown checkpoint, there can't be any concurrent
6945  * WAL insertion.
6946  */
6947  freespace = INSERT_FREESPACE(curInsert);
6948  if (freespace == 0)
6949  {
6950  if (XLogSegmentOffset(curInsert, wal_segment_size) == 0)
6951  curInsert += SizeOfXLogLongPHD;
6952  else
6953  curInsert += SizeOfXLogShortPHD;
6954  }
6955  checkPoint.redo = curInsert;
6956 
6957  /*
6958  * Here we update the shared RedoRecPtr for future XLogInsert calls;
6959  * this must be done while holding all the insertion locks.
6960  *
6961  * Note: if we fail to complete the checkpoint, RedoRecPtr will be
6962  * left pointing past where it really needs to point. This is okay;
6963  * the only consequence is that XLogInsert might back up whole buffers
6964  * that it didn't really need to. We can't postpone advancing
6965  * RedoRecPtr because XLogInserts that happen while we are dumping
6966  * buffers must assume that their buffer changes are not included in
6967  * the checkpoint.
6968  */
6969  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
6970  }
6971 
6972  /*
6973  * Now we can release the WAL insertion locks, allowing other xacts to
6974  * proceed while we are flushing disk buffers.
6975  */
6977 
6978  /*
6979  * If this is an online checkpoint, we have not yet determined the redo
6980  * point. We do so now by inserting the special XLOG_CHECKPOINT_REDO
6981  * record; the LSN at which it starts becomes the new redo pointer. We
6982  * don't do this for a shutdown checkpoint, because in that case no WAL
6983  * can be written between the redo point and the insertion of the
6984  * checkpoint record itself, so the checkpoint record itself serves to
6985  * mark the redo point.
6986  */
6987  if (!shutdown)
6988  {
6989  int dummy = 0;
6990 
6991  /* Record must have payload to avoid assertion failure. */
6992  XLogBeginInsert();
6993  XLogRegisterData((char *) &dummy, sizeof(dummy));
6994  (void) XLogInsert(RM_XLOG_ID, XLOG_CHECKPOINT_REDO);
6995 
6996  /*
6997  * XLogInsertRecord will have updated XLogCtl->Insert.RedoRecPtr in
6998  * shared memory and RedoRecPtr in backend-local memory, but we need
6999  * to copy that into the record that will be inserted when the
7000  * checkpoint is complete.
7001  */
7002  checkPoint.redo = RedoRecPtr;
7003  }
7004 
7005  /* Update the info_lck-protected copy of RedoRecPtr as well */
7007  XLogCtl->RedoRecPtr = checkPoint.redo;
7009 
7010  /*
7011  * If enabled, log checkpoint start. We postpone this until now so as not
7012  * to log anything if we decided to skip the checkpoint.
7013  */
7014  if (log_checkpoints)
7015  LogCheckpointStart(flags, false);
7016 
7017  /* Update the process title */
7018  update_checkpoint_display(flags, false, false);
7019 
7020  TRACE_POSTGRESQL_CHECKPOINT_START(flags);
7021 
7022  /*
7023  * Get the other info we need for the checkpoint record.
7024  *
7025  * We don't need to save oldestClogXid in the checkpoint, it only matters
7026  * for the short period in which clog is being truncated, and if we crash
7027  * during that we'll redo the clog truncation and fix up oldestClogXid
7028  * there.
7029  */
7030  LWLockAcquire(XidGenLock, LW_SHARED);
7031  checkPoint.nextXid = TransamVariables->nextXid;
7032  checkPoint.oldestXid = TransamVariables->oldestXid;
7034  LWLockRelease(XidGenLock);
7035 
7036  LWLockAcquire(CommitTsLock, LW_SHARED);
7039  LWLockRelease(CommitTsLock);
7040 
7041  LWLockAcquire(OidGenLock, LW_SHARED);
7042  checkPoint.nextOid = TransamVariables->nextOid;
7043  if (!shutdown)
7044  checkPoint.nextOid += TransamVariables->oidCount;
7045  LWLockRelease(OidGenLock);
7046 
7047  MultiXactGetCheckptMulti(shutdown,
7048  &checkPoint.nextMulti,
7049  &checkPoint.nextMultiOffset,
7050  &checkPoint.oldestMulti,
7051  &checkPoint.oldestMultiDB);
7052 
7053  /*
7054  * Having constructed the checkpoint record, ensure all shmem disk buffers
7055  * and commit-log buffers are flushed to disk.
7056  *
7057  * This I/O could fail for various reasons. If so, we will fail to
7058  * complete the checkpoint, but there is no reason to force a system
7059  * panic. Accordingly, exit critical section while doing it.
7060  */
7061  END_CRIT_SECTION();
7062 
7063  /*
7064  * In some cases there are groups of actions that must all occur on one
7065  * side or the other of a checkpoint record. Before flushing the
7066  * checkpoint record we must explicitly wait for any backend currently
7067  * performing those groups of actions.
7068  *
7069  * One example is end of transaction, so we must wait for any transactions
7070  * that are currently in commit critical sections. If an xact inserted
7071  * its commit record into XLOG just before the REDO point, then a crash
7072  * restart from the REDO point would not replay that record, which means
7073  * that our flushing had better include the xact's update of pg_xact. So
7074  * we wait till he's out of his commit critical section before proceeding.
7075  * See notes in RecordTransactionCommit().
7076  *
7077  * Because we've already released the insertion locks, this test is a bit
7078  * fuzzy: it is possible that we will wait for xacts we didn't really need
7079  * to wait for. But the delay should be short and it seems better to make
7080  * checkpoint take a bit longer than to hold off insertions longer than
7081  * necessary. (In fact, the whole reason we have this issue is that xact.c
7082  * does commit record XLOG insertion and clog update as two separate steps
7083  * protected by different locks, but again that seems best on grounds of
7084  * minimizing lock contention.)
7085  *
7086  * A transaction that has not yet set delayChkptFlags when we look cannot
7087  * be at risk, since it has not inserted its commit record yet; and one
7088  * that's already cleared it is not at risk either, since it's done fixing
7089  * clog and we will correctly flush the update below. So we cannot miss
7090  * any xacts we need to wait for.
7091  */
7093  if (nvxids > 0)
7094  {
7095  do
7096  {
7097  pgstat_report_wait_start(WAIT_EVENT_CHECKPOINT_DELAY_START);
7098  pg_usleep(10000L); /* wait for 10 msec */
7100  } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
7102  }
7103  pfree(vxids);
7104 
7105  CheckPointGuts(checkPoint.redo, flags);
7106 
7108  if (nvxids > 0)
7109  {
7110  do
7111  {
7112  pgstat_report_wait_start(WAIT_EVENT_CHECKPOINT_DELAY_COMPLETE);
7113  pg_usleep(10000L); /* wait for 10 msec */
7115  } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
7117  }
7118  pfree(vxids);
7119 
7120  /*
7121  * Take a snapshot of running transactions and write this to WAL. This
7122  * allows us to reconstruct the state of running transactions during
7123  * archive recovery, if required. Skip, if this info disabled.
7124  *
7125  * If we are shutting down, or Startup process is completing crash
7126  * recovery we don't need to write running xact data.
7127  */
7128  if (!shutdown && XLogStandbyInfoActive())
7130 
7132 
7133  /*
7134  * Now insert the checkpoint record into XLOG.
7135  */
7136  XLogBeginInsert();
7137  XLogRegisterData((char *) (&checkPoint), sizeof(checkPoint));
7138  recptr = XLogInsert(RM_XLOG_ID,
7139  shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
7141 
7142  XLogFlush(recptr);
7143 
7144  /*
7145  * We mustn't write any new WAL after a shutdown checkpoint, or it will be
7146  * overwritten at next startup. No-one should even try, this just allows
7147  * sanity-checking. In the case of an end-of-recovery checkpoint, we want
7148  * to just temporarily disable writing until the system has exited
7149  * recovery.
7150  */
7151  if (shutdown)
7152  {
7153  if (flags & CHECKPOINT_END_OF_RECOVERY)
7154  LocalXLogInsertAllowed = oldXLogAllowed;
7155  else
7156  LocalXLogInsertAllowed = 0; /* never again write WAL */
7157  }
7158 
7159  /*
7160  * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
7161  * = end of actual checkpoint record.
7162  */
7163  if (shutdown && checkPoint.redo != ProcLastRecPtr)
7164  ereport(PANIC,
7165  (errmsg("concurrent write-ahead log activity while database system is shutting down")));
7166 
7167  /*
7168  * Remember the prior checkpoint's redo ptr for
7169  * UpdateCheckPointDistanceEstimate()
7170  */
7171  PriorRedoPtr = ControlFile->checkPointCopy.redo;
7172 
7173  /*
7174  * Update the control file.
7175  */
7176  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7177  if (shutdown)
7180  ControlFile->checkPointCopy = checkPoint;
7181  /* crash recovery should always recover to the end of WAL */
7184 
7185  /*
7186  * Persist unloggedLSN value. It's reset on crash recovery, so this goes
7187  * unused on non-shutdown checkpoints, but seems useful to store it always
7188  * for debugging purposes.
7189  */
7191 
7193  LWLockRelease(ControlFileLock);
7194 
7195  /* Update shared-memory copy of checkpoint XID/epoch */
7197  XLogCtl->ckptFullXid = checkPoint.nextXid;
7199 
7200  /*
7201  * We are now done with critical updates; no need for system panic if we
7202  * have trouble while fooling with old log segments.
7203  */
7204  END_CRIT_SECTION();
7205 
7206  /*
7207  * WAL summaries end when the next XLOG_CHECKPOINT_REDO or
7208  * XLOG_CHECKPOINT_SHUTDOWN record is reached. This is the first point
7209  * where (a) we're not inside of a critical section and (b) we can be
7210  * certain that the relevant record has been flushed to disk, which must
7211  * happen before it can be summarized.
7212  *
7213  * If this is a shutdown checkpoint, then this happens reasonably
7214  * promptly: we've only just inserted and flushed the
7215  * XLOG_CHECKPOINT_SHUTDOWN record. If this is not a shutdown checkpoint,
7216  * then this might not be very prompt at all: the XLOG_CHECKPOINT_REDO
7217  * record was written before we began flushing data to disk, and that
7218  * could be many minutes ago at this point. However, we don't XLogFlush()
7219  * after inserting that record, so we're not guaranteed that it's on disk
7220  * until after the above call that flushes the XLOG_CHECKPOINT_ONLINE
7221  * record.
7222  */
7224 
7225  /*
7226  * Let smgr do post-checkpoint cleanup (eg, deleting old files).
7227  */
7229 
7230  /*
7231  * Update the average distance between checkpoints if the prior checkpoint
7232  * exists.
7233  */
7234  if (PriorRedoPtr != InvalidXLogRecPtr)
7236 
7237  /*
7238  * Delete old log files, those no longer needed for last checkpoint to
7239  * prevent the disk holding the xlog from growing full.
7240  */
7242  KeepLogSeg(recptr, &_logSegNo);
7244  _logSegNo, InvalidOid,
7246  {
7247  /*
7248  * Some slots have been invalidated; recalculate the old-segment
7249  * horizon, starting again from RedoRecPtr.
7250  */
7252  KeepLogSeg(recptr, &_logSegNo);
7253  }
7254  _logSegNo--;
7255  RemoveOldXlogFiles(_logSegNo, RedoRecPtr, recptr,
7256  checkPoint.ThisTimeLineID);
7257 
7258  /*
7259  * Make more log segments if needed. (Do this after recycling old log
7260  * segments, since that may supply some of the needed files.)
7261  */
7262  if (!shutdown)
7263  PreallocXlogFiles(recptr, checkPoint.ThisTimeLineID);
7264 
7265  /*
7266  * Truncate pg_subtrans if possible. We can throw away all data before
7267  * the oldest XMIN of any running transaction. No future transaction will
7268  * attempt to reference any pg_subtrans entry older than that (see Asserts
7269  * in subtrans.c). During recovery, though, we mustn't do this because
7270  * StartupSUBTRANS hasn't been called yet.
7271  */
7272  if (!RecoveryInProgress())
7274 
7275  /* Real work is done; log and update stats. */
7276  LogCheckpointEnd(false);
7277 
7278  /* Reset the process title */
7279  update_checkpoint_display(flags, false, true);
7280 
7281  TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats.ckpt_bufs_written,
7282  NBuffers,
7286 }
static uint64 pg_atomic_read_membarrier_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:471
int NBuffers
Definition: globals.c:139
@ LW_SHARED
Definition: lwlock.h:115
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
void MultiXactGetCheckptMulti(bool is_shutdown, MultiXactId *nextMulti, MultiXactOffset *nextMultiOffset, MultiXactId *oldestMulti, Oid *oldestMultiDB)
Definition: multixact.c:2233
#define XLOG_CHECKPOINT_REDO
Definition: pg_control.h:81
@ DB_SHUTDOWNING
Definition: pg_control.h:93
@ DB_SHUTDOWNED
Definition: pg_control.h:91
#define XLOG_CHECKPOINT_ONLINE
Definition: pg_control.h:68
#define InvalidOid
Definition: postgres_ext.h:36
#define DELAY_CHKPT_START
Definition: proc.h:114
#define DELAY_CHKPT_COMPLETE
Definition: proc.h:115
VirtualTransactionId * GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
Definition: procarray.c:3030
TransactionId GetOldestActiveTransactionId(void)
Definition: procarray.c:2867
TransactionId GetOldestTransactionIdConsideredRunning(void)
Definition: procarray.c:2022
bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
Definition: procarray.c:3076
void pg_usleep(long microsec)
Definition: signal.c:53
bool InvalidateObsoleteReplicationSlots(ReplicationSlotInvalidationCause cause, XLogSegNo oldestSegno, Oid dboid, TransactionId snapshotConflictHorizon)
Definition: slot.c:1783
@ RS_INVAL_WAL_REMOVED
Definition: slot.h:51
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1285
TimestampTz ckpt_start_t
Definition: xlog.h:159
int ckpt_segs_removed
Definition: xlog.h:168
int ckpt_segs_added
Definition: xlog.h:167
int ckpt_bufs_written
Definition: xlog.h:165
int ckpt_segs_recycled
Definition: xlog.h:169
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:167
XLogRecPtr unloggedLSN
Definition: pg_control.h:136
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:168
TransactionId oldestCommitTsXid
Definition: transam.h:232
TransactionId newestCommitTsXid
Definition: transam.h:233
TransactionId oldestXid
Definition: transam.h:222
FullTransactionId ckptFullXid
Definition: xlog.c:457
TimeLineID InsertTimeLineID
Definition: xlog.c:504
XLogRecPtr RedoRecPtr
Definition: xlog.c:456
TimeLineID PrevTimeLineID
Definition: xlog.c:505
pg_atomic_uint64 unloggedLSN
Definition: xlog.c:464
XLogRecPtr RedoRecPtr
Definition: xlog.c:430
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:411
void SyncPreCheckpoint(void)
Definition: sync.c:177
void SyncPostCheckpoint(void)
Definition: sync.c:202
void SetWalSummarizerLatch(void)
XLogRecPtr ProcLastRecPtr
Definition: xlog.c:253
bool RecoveryInProgress(void)
Definition: xlog.c:6290
static void WALInsertLockRelease(void)
Definition: xlog.c:1440
static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos)
Definition: xlog.c:1853
static void WALInsertLockAcquireExclusive(void)
Definition: xlog.c:1411
static void UpdateControlFile(void)
Definition: xlog.c:4526
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr, TimeLineID insertTLI)
Definition: xlog.c:3854
static void LogCheckpointStart(int flags, bool restartpoint)
Definition: xlog.c:6586
static XLogRecPtr RedoRecPtr
Definition: xlog.c:273
static void LogCheckpointEnd(bool restartpoint)
Definition: xlog.c:6618
static void PreallocXlogFiles(XLogRecPtr endptr, TimeLineID tli)
Definition: xlog.c:3679
bool log_checkpoints
Definition: xlog.c:129
static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
Definition: xlog.c:7874
static int LocalSetXLogInsertAllowed(void)
Definition: xlog.c:6378
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:6493
static void UpdateCheckPointDistanceEstimate(uint64 nbytes)
Definition: xlog.c:6721
static int LocalXLogInsertAllowed
Definition: xlog.c:236
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2791
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
Definition: xlog.c:7431
static void update_checkpoint_display(int flags, bool restartpoint, bool reset)
Definition: xlog.c:6759
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:138
#define CHECKPOINT_FORCE
Definition: xlog.h:140
#define XLogStandbyInfoActive()
Definition: xlog.h:121
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:364
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:474
void XLogBeginInsert(void)
Definition: xloginsert.c:149

References 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_WAL_REMOVED, SetWalSummarizerLatch(), 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(), 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().

◆ CreateEndOfRecoveryRecord()

static void CreateEndOfRecoveryRecord ( void  )
static

Definition at line 7297 of file xlog.c.

7298 {
7299  xl_end_of_recovery xlrec;
7300  XLogRecPtr recptr;
7301 
7302  /* sanity check */
7303  if (!RecoveryInProgress())
7304  elog(ERROR, "can only be used to end recovery");
7305 
7306  xlrec.end_time = GetCurrentTimestamp();
7307 
7312 
7314 
7315  XLogBeginInsert();
7316  XLogRegisterData((char *) &xlrec, sizeof(xl_end_of_recovery));
7317  recptr = XLogInsert(RM_XLOG_ID, XLOG_END_OF_RECOVERY);
7318 
7319  XLogFlush(recptr);
7320 
7321  /*
7322  * Update the control file so that crash recovery can follow the timeline
7323  * changes to this point.
7324  */
7325  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7326  ControlFile->minRecoveryPoint = recptr;
7327  ControlFile->minRecoveryPointTLI = xlrec.ThisTimeLineID;
7329  LWLockRelease(ControlFileLock);
7330 
7331  END_CRIT_SECTION();
7332 }
#define XLOG_END_OF_RECOVERY
Definition: pg_control.h:76
TimeLineID PrevTimeLineID
TimestampTz end_time
TimeLineID ThisTimeLineID

References ControlFile, elog, END_CRIT_SECTION, xl_end_of_recovery::end_time, ERROR, GetCurrentTimestamp(), XLogCtlData::InsertTimeLineID, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, XLogCtlData::PrevTimeLineID, xl_end_of_recovery::PrevTimeLineID, RecoveryInProgress(), START_CRIT_SECTION, xl_end_of_recovery::ThisTimeLineID, UpdateControlFile(), WALInsertLockAcquireExclusive(), WALInsertLockRelease(), XLOG_END_OF_RECOVERY, XLogBeginInsert(), XLogCtl, XLogFlush(), XLogInsert(), and XLogRegisterData().

Referenced by PerformRecoveryXLogAction().

◆ CreateOverwriteContrecordRecord()

static XLogRecPtr CreateOverwriteContrecordRecord ( XLogRecPtr  aborted_lsn,
XLogRecPtr  pagePtr,
TimeLineID  newTLI 
)
static

Definition at line 7361 of file xlog.c.

7363 {
7365  XLogRecPtr recptr;
7366  XLogPageHeader pagehdr;
7367  XLogRecPtr startPos;
7368 
7369  /* sanity checks */
7370  if (!RecoveryInProgress())
7371  elog(ERROR, "can only be used at end of recovery");
7372  if (pagePtr % XLOG_BLCKSZ != 0)
7373  elog(ERROR, "invalid position for missing continuation record %X/%X",
7374  LSN_FORMAT_ARGS(pagePtr));
7375 
7376  /* The current WAL insert position should be right after the page header */
7377  startPos = pagePtr;
7378  if (XLogSegmentOffset(startPos, wal_segment_size) == 0)
7379  startPos += SizeOfXLogLongPHD;
7380  else
7381  startPos += SizeOfXLogShortPHD;
7382  recptr = GetXLogInsertRecPtr();
7383  if (recptr != startPos)
7384  elog(ERROR, "invalid WAL insert position %X/%X for OVERWRITE_CONTRECORD",
7385  LSN_FORMAT_ARGS(recptr));
7386 
7388 
7389  /*
7390  * Initialize the XLOG page header (by GetXLogBuffer), and set the
7391  * XLP_FIRST_IS_OVERWRITE_CONTRECORD flag.
7392  *
7393  * No other backend is allowed to write WAL yet, so acquiring the WAL
7394  * insertion lock is just pro forma.
7395  */
7397  pagehdr = (XLogPageHeader) GetXLogBuffer(pagePtr, newTLI);
7400 
7401  /*
7402  * Insert the XLOG_OVERWRITE_CONTRECORD record as the first record on the
7403  * page. We know it becomes the first record, because no other backend is
7404  * allowed to write WAL yet.
7405  */
7406  XLogBeginInsert();
7407  xlrec.overwritten_lsn = aborted_lsn;
7409  XLogRegisterData((char *) &xlrec, sizeof(xl_overwrite_contrecord));
7410  recptr = XLogInsert(RM_XLOG_ID, XLOG_OVERWRITE_CONTRECORD);
7411 
7412  /* check that the record was inserted to the right place */
7413  if (ProcLastRecPtr != startPos)
7414  elog(ERROR, "OVERWRITE_CONTRECORD was inserted to unexpected position %X/%X",
7416 
7417  XLogFlush(recptr);
7418 
7419  END_CRIT_SECTION();
7420 
7421  return recptr;
7422 }
#define XLOG_OVERWRITE_CONTRECORD
Definition: pg_control.h:80
static void WALInsertLockAcquire(void)
Definition: xlog.c:1366
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:9355
#define XLP_FIRST_IS_OVERWRITE_CONTRECORD
Definition: xlog_internal.h:80

References elog, END_CRIT_SECTION, ERROR, GetCurrentTimestamp(), GetXLogBuffer(), GetXLogInsertRecPtr(), LSN_FORMAT_ARGS, xl_overwrite_contrecord::overwrite_time, xl_overwrite_contrecord::overwritten_lsn, ProcLastRecPtr, RecoveryInProgress(), SizeOfXLogLongPHD, SizeOfXLogShortPHD, START_CRIT_SECTION, wal_segment_size, WALInsertLockAcquire(), WALInsertLockRelease(), XLOG_OVERWRITE_CONTRECORD, XLogBeginInsert(), XLogFlush(), XLogInsert(), XLogRegisterData(), XLogSegmentOffset, XLP_FIRST_IS_OVERWRITE_CONTRECORD, and XLogPageHeaderData::xlp_info.

Referenced by StartupXLOG().

◆ CreateRestartPoint()

bool CreateRestartPoint ( int  flags)

Definition at line 7512 of file xlog.c.

7513 {
7514  XLogRecPtr lastCheckPointRecPtr;
7515  XLogRecPtr lastCheckPointEndPtr;
7516  CheckPoint lastCheckPoint;
7517  XLogRecPtr PriorRedoPtr;
7518  XLogRecPtr receivePtr;
7519  XLogRecPtr replayPtr;
7520  TimeLineID replayTLI;
7521  XLogRecPtr endptr;
7522  XLogSegNo _logSegNo;
7523  TimestampTz xtime;
7524 
7525  /* Concurrent checkpoint/restartpoint cannot happen */
7527 
7528  /* Get a local copy of the last safe checkpoint record. */
7530  lastCheckPointRecPtr = XLogCtl->lastCheckPointRecPtr;
7531  lastCheckPointEndPtr = XLogCtl->lastCheckPointEndPtr;
7532  lastCheckPoint = XLogCtl->lastCheckPoint;
7534 
7535  /*
7536  * Check that we're still in recovery mode. It's ok if we exit recovery
7537  * mode after this check, the restart point is valid anyway.
7538  */
7539  if (!RecoveryInProgress())
7540  {
7541  ereport(DEBUG2,
7542  (errmsg_internal("skipping restartpoint, recovery has already ended")));
7543  return false;
7544  }
7545 
7546  /*
7547  * If the last checkpoint record we've replayed is already our last
7548  * restartpoint, we can't perform a new restart point. We still update
7549  * minRecoveryPoint in that case, so that if this is a shutdown restart
7550  * point, we won't start up earlier than before. That's not strictly
7551  * necessary, but when hot standby is enabled, it would be rather weird if
7552  * the database opened up for read-only connections at a point-in-time
7553  * before the last shutdown. Such time travel is still possible in case of
7554  * immediate shutdown, though.
7555  *
7556  * We don't explicitly advance minRecoveryPoint when we do create a
7557  * restartpoint. It's assumed that flushing the buffers will do that as a
7558  * side-effect.
7559  */
7560  if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||
7561  lastCheckPoint.redo <= ControlFile->checkPointCopy.redo)
7562  {
7563  ereport(DEBUG2,
7564  (errmsg_internal("skipping restartpoint, already performed at %X/%X",
7565  LSN_FORMAT_ARGS(lastCheckPoint.redo))));
7566 
7568  if (flags & CHECKPOINT_IS_SHUTDOWN)
7569  {
7570  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7573  LWLockRelease(ControlFileLock);
7574  }
7575  return false;
7576  }
7577 
7578  /*
7579  * Update the shared RedoRecPtr so that the startup process can calculate
7580  * the number of segments replayed since last restartpoint, and request a
7581  * restartpoint if it exceeds CheckPointSegments.
7582  *
7583  * Like in CreateCheckPoint(), hold off insertions to update it, although
7584  * during recovery this is just pro forma, because no WAL insertions are
7585  * happening.
7586  */
7588  RedoRecPtr = XLogCtl->Insert.RedoRecPtr = lastCheckPoint.redo;
7590 
7591  /* Also update the info_lck-protected copy */
7593  XLogCtl->RedoRecPtr = lastCheckPoint.redo;
7595 
7596  /*
7597  * Prepare to accumulate statistics.
7598  *
7599  * Note: because it is possible for log_checkpoints to change while a
7600  * checkpoint proceeds, we always accumulate stats, even if
7601  * log_checkpoints is currently off.
7602  */
7603  MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
7605 
7606  if (log_checkpoints)
7607  LogCheckpointStart(flags, true);
7608 
7609  /* Update the process title */
7610  update_checkpoint_display(flags, true, false);
7611 
7612  CheckPointGuts(lastCheckPoint.redo, flags);
7613 
7614  /*
7615  * This location needs to be after CheckPointGuts() to ensure that some
7616  * work has already happened during this checkpoint.
7617  */
7618  INJECTION_POINT("create-restart-point");
7619 
7620  /*
7621  * Remember the prior checkpoint's redo ptr for
7622  * UpdateCheckPointDistanceEstimate()
7623  */
7624  PriorRedoPtr = ControlFile->checkPointCopy.redo;
7625 
7626  /*
7627  * Update pg_control, using current time. Check that it still shows an
7628  * older checkpoint, else do nothing; this is a quick hack to make sure
7629  * nothing really bad happens if somehow we get here after the
7630  * end-of-recovery checkpoint.
7631  */
7632  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7633  if (ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
7634  {
7635  /*
7636  * Update the checkpoint information. We do this even if the cluster
7637  * does not show DB_IN_ARCHIVE_RECOVERY to match with the set of WAL
7638  * segments recycled below.
7639  */
7640  ControlFile->checkPoint = lastCheckPointRecPtr;
7641  ControlFile->checkPointCopy = lastCheckPoint;
7642 
7643  /*
7644  * Ensure minRecoveryPoint is past the checkpoint record and update it
7645  * if the control file still shows DB_IN_ARCHIVE_RECOVERY. Normally,
7646  * this will have happened already while writing out dirty buffers,
7647  * but not necessarily - e.g. because no buffers were dirtied. We do
7648  * this because a backup performed in recovery uses minRecoveryPoint
7649  * to determine which WAL files must be included in the backup, and
7650  * the file (or files) containing the checkpoint record must be
7651  * included, at a minimum. Note that for an ordinary restart of
7652  * recovery there's no value in having the minimum recovery point any
7653  * earlier than this anyway, because redo will begin just after the
7654  * checkpoint record.
7655  */
7657  {
7658  if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
7659  {
7660  ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
7662 
7663  /* update local copy */
7666  }
7667  if (flags & CHECKPOINT_IS_SHUTDOWN)
7669  }
7671  }
7672  LWLockRelease(ControlFileLock);
7673 
7674  /*
7675  * Update the average distance between checkpoints/restartpoints if the
7676  * prior checkpoint exists.
7677  */
7678  if (PriorRedoPtr != InvalidXLogRecPtr)
7680 
7681  /*
7682  * Delete old log files, those no longer needed for last restartpoint to
7683  * prevent the disk holding the xlog from growing full.
7684  */
7686 
7687  /*
7688  * Retreat _logSegNo using the current end of xlog replayed or received,
7689  * whichever is later.
7690  */
7691  receivePtr = GetWalRcvFlushRecPtr(NULL, NULL);
7692  replayPtr = GetXLogReplayRecPtr(&replayTLI);
7693  endptr = (receivePtr < replayPtr) ? replayPtr : receivePtr;
7694  KeepLogSeg(endptr, &_logSegNo);
7696  _logSegNo, InvalidOid,
7698  {
7699  /*
7700  * Some slots have been invalidated; recalculate the old-segment
7701  * horizon, starting again from RedoRecPtr.
7702  */
7704  KeepLogSeg(endptr, &_logSegNo);
7705  }
7706  _logSegNo--;
7707 
7708  /*
7709  * Try to recycle segments on a useful timeline. If we've been promoted
7710  * since the beginning of this restartpoint, use the new timeline chosen
7711  * at end of recovery. If we're still in recovery, use the timeline we're
7712  * currently replaying.
7713  *
7714  * There is no guarantee that the WAL segments will be useful on the
7715  * current timeline; if recovery proceeds to a new timeline right after
7716  * this, the pre-allocated WAL segments on this timeline will not be used,
7717  * and will go wasted until recycled on the next restartpoint. We'll live
7718  * with that.
7719  */
7720  if (!RecoveryInProgress())
7721  replayTLI = XLogCtl->InsertTimeLineID;
7722 
7723  RemoveOldXlogFiles(_logSegNo, RedoRecPtr, endptr, replayTLI);
7724 
7725  /*
7726  * Make more log segments if needed. (Do this after recycling old log
7727  * segments, since that may supply some of the needed files.)
7728  */
7729  PreallocXlogFiles(endptr, replayTLI);
7730 
7731  /*
7732  * Truncate pg_subtrans if possible. We can throw away all data before
7733  * the oldest XMIN of any running transaction. No future transaction will
7734  * attempt to reference any pg_subtrans entry older than that (see Asserts
7735  * in subtrans.c). When hot standby is disabled, though, we mustn't do
7736  * this because StartupSUBTRANS hasn't been called yet.
7737  */
7738  if (EnableHotStandby)
7740 
7741  /* Real work is done; log and update stats. */
7742  LogCheckpointEnd(true);
7743 
7744  /* Reset the process title */
7745  update_checkpoint_display(flags, true, true);
7746 
7747  xtime = GetLatestXTime();
7749  (errmsg("recovery restart point at %X/%X",
7750  LSN_FORMAT_ARGS(lastCheckPoint.redo)),
7751  xtime ? errdetail("Last completed transaction was at log time %s.",
7752  timestamptz_to_str(xtime)) : 0));
7753 
7754  /*
7755  * Finally, execute archive_cleanup_command, if any.
7756  */
7757  if (archiveCleanupCommand && strcmp(archiveCleanupCommand, "") != 0)
7759  "archive_cleanup_command",
7760  false,
7761  WAIT_EVENT_ARCHIVE_CLEANUP_COMMAND);
7762 
7763  return true;
7764 }
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1853
int64 TimestampTz
Definition: timestamp.h:39
#define LOG
Definition: elog.h:31
bool IsUnderPostmaster
Definition: globals.c:117
#define INJECTION_POINT(name)
@ B_CHECKPOINTER
Definition: miscadmin.h:357
BackendType MyBackendType
Definition: miscinit.c:63
@ DB_IN_ARCHIVE_RECOVERY
Definition: pg_control.h:95
@ DB_SHUTDOWNED_IN_RECOVERY
Definition: pg_control.h:92
CheckPoint lastCheckPoint
Definition: xlog.c:540
XLogRecPtr lastCheckPointRecPtr
Definition: xlog.c:538
XLogRecPtr lastCheckPointEndPtr
Definition: xlog.c:539
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
Definition: xlog.c:2711
static XLogRecPtr LocalMinRecoveryPoint
Definition: xlog.c:641
static TimeLineID LocalMinRecoveryPointTLI
Definition: xlog.c:642
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint32 TimeLineID
Definition: xlogdefs.h:59
char * archiveCleanupCommand
Definition: xlogrecovery.c:84
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_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 9314 of file xlog.c.

9315 {
9316  bool during_backup_start = DatumGetBool(arg);
9317 
9318  /* If called during backup start, there shouldn't be one already running */
9319  Assert(!during_backup_start || sessionBackupState == SESSION_BACKUP_NONE);
9320 
9321  if (during_backup_start || sessionBackupState != SESSION_BACKUP_NONE)
9322  {
9326 
9329 
9330  if (!during_backup_start)
9331  ereport(WARNING,
9332  errmsg("aborting backup due to backend exiting before pg_backup_stop was called"));
9333  }
9334 }
#define WARNING
Definition: elog.h:36
void * arg
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
int runningBackups
Definition: xlog.c:438
static SessionBackupState sessionBackupState
Definition: xlog.c:391
@ SESSION_BACKUP_NONE
Definition: xlog.h:283

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

8714 {
8716 
8717  Assert(state != NULL);
8719 
8720  /*
8721  * During recovery, we don't need to check WAL level. Because, if WAL
8722  * level is not sufficient, it's impossible to get here during recovery.
8723  */
8725  ereport(ERROR,
8726  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8727  errmsg("WAL level not sufficient for making an online backup"),
8728  errhint("\"wal_level\" must be set to \"replica\" or \"logical\" at server start.")));
8729 
8730  if (strlen(backupidstr) > MAXPGPATH)
8731  ereport(ERROR,
8732  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8733  errmsg("backup label too long (max %d bytes)",
8734  MAXPGPATH)));
8735 
8736  memcpy(state->name, backupidstr, strlen(backupidstr));
8737 
8738  /*
8739  * Mark backup active in shared memory. We must do full-page WAL writes
8740  * during an on-line backup even if not doing so at other times, because
8741  * it's quite possible for the backup dump to obtain a "torn" (partially
8742  * written) copy of a database page if it reads the page concurrently with
8743  * our write to the same page. This can be fixed as long as the first
8744  * write to the page in the WAL sequence is a full-page write. Hence, we
8745  * increment runningBackups then force a CHECKPOINT, to ensure there are
8746  * no dirty pages in shared memory that might get dumped while the backup
8747  * is in progress without having a corresponding WAL record. (Once the
8748  * backup is complete, we need not force full-page writes anymore, since
8749  * we expect that any pages not modified during the backup interval must
8750  * have been correctly captured by the backup.)
8751  *
8752  * Note that forcing full-page writes has no effect during an online
8753  * backup from the standby.
8754  *
8755  * We must hold all the insertion locks to change the value of
8756  * runningBackups, to ensure adequate interlocking against
8757  * XLogInsertRecord().
8758  */
8762 
8763  /*
8764  * Ensure we decrement runningBackups if we fail below. NB -- for this to
8765  * work correctly, it is critical that sessionBackupState is only updated
8766  * after this block is over.
8767  */
8769  {
8770  bool gotUniqueStartpoint = false;
8771  DIR *tblspcdir;
8772  struct dirent *de;
8773  tablespaceinfo *ti;
8774  int datadirpathlen;
8775 
8776  /*
8777  * Force an XLOG file switch before the checkpoint, to ensure that the
8778  * WAL segment the checkpoint is written to doesn't contain pages with
8779  * old timeline IDs. That would otherwise happen if you called
8780  * pg_backup_start() right after restoring from a PITR archive: the
8781  * first WAL segment containing the startup checkpoint has pages in
8782  * the beginning with the old timeline ID. That can cause trouble at
8783  * recovery: we won't have a history file covering the old timeline if
8784  * pg_wal directory was not included in the base backup and the WAL
8785  * archive was cleared too before starting the backup.
8786  *
8787  * This also ensures that we have emitted a WAL page header that has
8788  * XLP_BKP_REMOVABLE off before we emit the checkpoint record.
8789  * Therefore, if a WAL archiver (such as pglesslog) is trying to
8790  * compress out removable backup blocks, it won't remove any that
8791  * occur after this point.
8792  *
8793  * During recovery, we skip forcing XLOG file switch, which means that
8794  * the backup taken during recovery is not available for the special
8795  * recovery case described above.
8796  */
8798  RequestXLogSwitch(false);
8799 
8800  do
8801  {
8802  bool checkpointfpw;
8803 
8804  /*
8805  * Force a CHECKPOINT. Aside from being necessary to prevent torn
8806  * page problems, this guarantees that two successive backup runs
8807  * will have different checkpoint positions and hence different
8808  * history file names, even if nothing happened in between.
8809  *
8810  * During recovery, establish a restartpoint if possible. We use
8811  * the last restartpoint as the backup starting checkpoint. This
8812  * means that two successive backup runs can have same checkpoint
8813  * positions.
8814  *
8815  * Since the fact that we are executing do_pg_backup_start()
8816  * during recovery means that checkpointer is running, we can use
8817  * RequestCheckpoint() to establish a restartpoint.
8818  *
8819  * We use CHECKPOINT_IMMEDIATE only if requested by user (via
8820  * passing fast = true). Otherwise this can take awhile.
8821  */
8823  (fast ? CHECKPOINT_IMMEDIATE : 0));
8824 
8825  /*
8826  * Now we need to fetch the checkpoint record location, and also
8827  * its REDO pointer. The oldest point in WAL that would be needed
8828  * to restore starting from the checkpoint is precisely the REDO
8829  * pointer.
8830  */
8831  LWLockAcquire(ControlFileLock, LW_SHARED);
8832  state->checkpointloc = ControlFile->checkPoint;
8833  state->startpoint = ControlFile->checkPointCopy.redo;
8835  checkpointfpw = ControlFile->checkPointCopy.fullPageWrites;
8836  LWLockRelease(ControlFileLock);
8837 
8839  {
8840  XLogRecPtr recptr;
8841 
8842  /*
8843  * Check to see if all WAL replayed during online backup
8844  * (i.e., since last restartpoint used as backup starting
8845  * checkpoint) contain full-page writes.
8846  */
8848  recptr = XLogCtl->lastFpwDisableRecPtr;
8850 
8851  if (!checkpointfpw || state->startpoint <= recptr)
8852  ereport(ERROR,
8853  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8854  errmsg("WAL generated with \"full_page_writes=off\" was replayed "
8855  "since last restartpoint"),
8856  errhint("This means that the backup being taken on the standby "
8857  "is corrupt and should not be used. "
8858  "Enable \"full_page_writes\" and run CHECKPOINT on the primary, "
8859  "and then try an online backup again.")));
8860 
8861  /*
8862  * During recovery, since we don't use the end-of-backup WAL
8863  * record and don't write the backup history file, the
8864  * starting WAL location doesn't need to be unique. This means
8865  * that two base backups started at the same time might use
8866  * the same checkpoint as starting locations.
8867  */
8868  gotUniqueStartpoint = true;
8869  }
8870 
8871  /*
8872  * If two base backups are started at the same time (in WAL sender
8873  * processes), we need to make sure that they use different
8874  * checkpoints as starting locations, because we use the starting
8875  * WAL location as a unique identifier for the base backup in the
8876  * end-of-backup WAL record and when we write the backup history
8877  * file. Perhaps it would be better generate a separate unique ID
8878  * for each backup instead of forcing another checkpoint, but
8879  * taking a checkpoint right after another is not that expensive
8880  * either because only few buffers have been dirtied yet.
8881  */
8883  if (XLogCtl->Insert.lastBackupStart < state->startpoint)
8884  {
8885  XLogCtl->Insert.lastBackupStart = state->startpoint;
8886  gotUniqueStartpoint = true;
8887  }
8889  } while (!gotUniqueStartpoint);
8890 
8891  /*
8892  * Construct tablespace_map file.
8893  */
8894  datadirpathlen = strlen(DataDir);
8895 
8896  /* Collect information about all tablespaces */
8897  tblspcdir = AllocateDir("pg_tblspc");
8898  while ((de = ReadDir(tblspcdir, "pg_tblspc")) != NULL)
8899  {
8900  char fullpath[MAXPGPATH + 10];
8901  char linkpath[MAXPGPATH];
8902  char *relpath = NULL;
8903  char *s;
8904  PGFileType de_type;
8905  char *badp;
8906  Oid tsoid;
8907 
8908  /*
8909  * Try to parse the directory name as an unsigned integer.
8910  *
8911  * Tablespace directories should be positive integers that can be
8912  * represented in 32 bits, with no leading zeroes or trailing
8913  * garbage. If we come across a name that doesn't meet those
8914  * criteria, skip it.
8915  */
8916  if (de->d_name[0] < '1' || de->d_name[1] > '9')
8917  continue;
8918  errno = 0;
8919  tsoid = strtoul(de->d_name, &badp, 10);
8920  if (*badp != '\0' || errno == EINVAL || errno == ERANGE)
8921  continue;
8922 
8923  snprintf(fullpath, sizeof(fullpath), "pg_tblspc/%s", de->d_name);
8924 
8925  de_type = get_dirent_type(fullpath, de, false, ERROR);
8926 
8927  if (de_type == PGFILETYPE_LNK)
8928  {
8929  StringInfoData escapedpath;
8930  int rllen;
8931 
8932  rllen = readlink(fullpath, linkpath, sizeof(linkpath));
8933  if (rllen < 0)
8934  {
8935  ereport(WARNING,
8936  (errmsg("could not read symbolic link \"%s\": %m",
8937  fullpath)));
8938  continue;
8939  }
8940  else if (rllen >= sizeof(linkpath))
8941  {
8942  ereport(WARNING,
8943  (errmsg("symbolic link \"%s\" target is too long",
8944  fullpath)));
8945  continue;
8946  }
8947  linkpath[rllen] = '\0';
8948 
8949  /*
8950  * Relpath holds the relative path of the tablespace directory
8951  * when it's located within PGDATA, or NULL if it's located
8952  * elsewhere.
8953  */
8954  if (rllen > datadirpathlen &&
8955  strncmp(linkpath, DataDir, datadirpathlen) == 0 &&
8956  IS_DIR_SEP(linkpath[datadirpathlen]))
8957  relpath = pstrdup(linkpath + datadirpathlen + 1);
8958 
8959  /*
8960  * Add a backslash-escaped version of the link path to the
8961  * tablespace map file.
8962  */
8963  initStringInfo(&escapedpath);
8964  for (s = linkpath; *s; s++)
8965  {
8966  if (*s == '\n' || *s == '\r' || *s == '\\')
8967  appendStringInfoChar(&escapedpath, '\\');
8968  appendStringInfoChar(&escapedpath, *s);
8969  }
8970  appendStringInfo(tblspcmapfile, "%s %s\n",
8971  de->d_name, escapedpath.data);
8972  pfree(escapedpath.data);
8973  }
8974  else if (de_type == PGFILETYPE_DIR)
8975  {
8976  /*
8977  * It's possible to use allow_in_place_tablespaces to create
8978  * directories directly under pg_tblspc, for testing purposes
8979  * only.
8980  *
8981  * In this case, we store a relative path rather than an
8982  * absolute path into the tablespaceinfo.
8983  */
8984  snprintf(linkpath, sizeof(linkpath), "pg_tblspc/%s",
8985  de->d_name);
8986  relpath = pstrdup(linkpath);
8987  }
8988  else
8989  {
8990  /* Skip any other file type that appears here. */
8991  continue;
8992  }
8993 
8994  ti = palloc(sizeof(tablespaceinfo));
8995  ti->oid = tsoid;
8996  ti->path = pstrdup(linkpath);
8997  ti->rpath = relpath;
8998  ti->size = -1;
8999 
9000  if (tablespaces)
9001  *tablespaces = lappend(*tablespaces, ti);
9002  }
9003  FreeDir(tblspcdir);
9004 
9005  state->starttime = (pg_time_t) time(NULL);
9006  }
9008 
9009  state->started_in_recovery = backup_started_in_recovery;
9010 
9011  /*
9012  * Mark that the start phase has correctly finished for the backup.
9013  */
9015 }
static bool backup_started_in_recovery
Definition: basebackup.c:123
void RequestCheckpoint(int flags)
Definition: checkpointer.c:941
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition: file_utils.c:525
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:68
#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
#define IS_DIR_SEP(ch)
Definition: port.h:102
unsigned int Oid
Definition: postgres_ext.h:31
#define relpath(rlocator, forknum)
Definition: relpath.h:94
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
XLogRecPtr lastFpwDisableRecPtr
Definition: xlog.c:546
XLogRecPtr lastBackupStart
Definition: xlog.c:439
Definition: regguts.h:323
char * rpath
Definition: basebackup.h:32
#define readlink(path, buf, size)
Definition: win32_port.h:236
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:7981
void do_pg_abort_backup(int code, Datum arg)
Definition: xlog.c:9314
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:284
#define CHECKPOINT_WAIT
Definition: xlog.h:143
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:139
#define XLogIsNeeded()
Definition: xlog.h:107

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

9041 {
9042  bool backup_stopped_in_recovery = false;
9043  char histfilepath[MAXPGPATH];
9044  char lastxlogfilename[MAXFNAMELEN];
9045  char histfilename[MAXFNAMELEN];
9046  XLogSegNo _logSegNo;
9047  FILE *fp;
9048  int seconds_before_warning;
9049  int waits = 0;
9050  bool reported_waiting = false;
9051 
9052  Assert(state != NULL);
9053 
9054  backup_stopped_in_recovery = RecoveryInProgress();
9055 
9056  /*
9057  * During recovery, we don't need to check WAL level. Because, if WAL
9058  * level is not sufficient, it's impossible to get here during recovery.
9059  */
9060  if (!backup_stopped_in_recovery && !XLogIsNeeded())
9061  ereport(ERROR,
9062  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
9063  errmsg("WAL level not sufficient for making an online backup"),
9064  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
9065 
9066  /*
9067  * OK to update backup counter and session-level lock.
9068  *
9069  * Note that CHECK_FOR_INTERRUPTS() must not occur while updating them,
9070  * otherwise they can be updated inconsistently, which might cause
9071  * do_pg_abort_backup() to fail.
9072  */
9074 
9075  /*
9076  * It is expected that each do_pg_backup_start() call is matched by
9077  * exactly one do_pg_backup_stop() call.
9078  */
9081 
9082  /*
9083  * Clean up session-level lock.
9084  *
9085  * You might think that WALInsertLockRelease() can be called before
9086  * cleaning up session-level lock because session-level lock doesn't need
9087  * to be protected with WAL insertion lock. But since
9088  * CHECK_FOR_INTERRUPTS() can occur in it, session-level lock must be
9089  * cleaned up before it.
9090  */
9092 
9094 
9095  /*
9096  * If we are taking an online backup from the standby, we confirm that the
9097  * standby has not been promoted during the backup.
9098  */
9099  if (state->started_in_recovery && !backup_stopped_in_recovery)
9100  ereport(ERROR,
9101  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
9102  errmsg("the standby was promoted during online backup"),
9103  errhint("This means that the backup being taken is corrupt "
9104  "and should not be used. "
9105  "Try taking another online backup.")));
9106 
9107  /*
9108  * During recovery, we don't write an end-of-backup record. We assume that
9109  * pg_control was backed up last and its minimum recovery point can be
9110  * available as the backup end location. Since we don't have an
9111  * end-of-backup record, we use the pg_control value to check whether
9112  * we've reached the end of backup when starting recovery from this
9113  * backup. We have no way of checking if pg_control wasn't backed up last
9114  * however.
9115  *
9116  * We don't force a switch to new WAL file but it is still possible to
9117  * wait for all the required files to be archived if waitforarchive is
9118  * true. This is okay if we use the backup to start a standby and fetch
9119  * the missing WAL using streaming replication. But in the case of an
9120  * archive recovery, a user should set waitforarchive to true and wait for
9121  * them to be archived to ensure that all the required files are
9122  * available.
9123  *
9124  * We return the current minimum recovery point as the backup end
9125  * location. Note that it can be greater than the exact backup end
9126  * location if the minimum recovery point is updated after the backup of
9127  * pg_control. This is harmless for current uses.
9128  *
9129  * XXX currently a backup history file is for informational and debug
9130  * purposes only. It's not essential for an online backup. Furthermore,
9131  * even if it's created, it will not be archived during recovery because
9132  * an archiver is not invoked. So it doesn't seem worthwhile to write a
9133  * backup history file during recovery.
9134  */
9135  if (backup_stopped_in_recovery)
9136  {
9137  XLogRecPtr recptr;
9138 
9139  /*
9140  * Check to see if all WAL replayed during online backup contain
9141  * full-page writes.
9142  */
9144  recptr = XLogCtl->lastFpwDisableRecPtr;
9146 
9147  if (state->startpoint <= recptr)
9148  ereport(ERROR,
9149  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
9150  errmsg("WAL generated with \"full_page_writes=off\" was replayed "
9151  "during online backup"),
9152  errhint("This means that the backup being taken on the standby "
9153  "is corrupt and should not be used. "
9154  "Enable \"full_page_writes\" and run CHECKPOINT on the primary, "
9155  "and then try an online backup again.")));
9156 
9157 
9158  LWLockAcquire(ControlFileLock, LW_SHARED);
9159  state->stoppoint = ControlFile->minRecoveryPoint;
9161  LWLockRelease(ControlFileLock);
9162  }
9163  else
9164  {
9165  char *history_file;
9166 
9167  /*
9168  * Write the backup-end xlog record
9169  */
9170  XLogBeginInsert();
9171  XLogRegisterData((char *) (&state->startpoint),
9172  sizeof(state->startpoint));
9173  state->stoppoint = XLogInsert(RM_XLOG_ID, XLOG_BACKUP_END);
9174 
9175  /*
9176  * Given that we're not in recovery, InsertTimeLineID is set and can't
9177  * change, so we can read it without a lock.
9178  */
9179  state->stoptli = XLogCtl->InsertTimeLineID;
9180 
9181  /*
9182  * Force a switch to a new xlog segment file, so that the backup is
9183  * valid as soon as archiver moves out the current segment file.
9184  */
9185  RequestXLogSwitch(false);
9186 
9187  state->stoptime = (pg_time_t) time(NULL);
9188 
9189  /*
9190  * Write the backup history file
9191  */
9192  XLByteToSeg(state->startpoint, _logSegNo, wal_segment_size);
9193  BackupHistoryFilePath(histfilepath, state->stoptli, _logSegNo,
9194  state->startpoint, wal_segment_size);
9195  fp = AllocateFile(histfilepath, "w");
9196  if (!fp)
9197  ereport(ERROR,
9199  errmsg("could not create file \"%s\": %m",
9200  histfilepath)));
9201 
9202  /* Build and save the contents of the backup history file */
9203  history_file = build_backup_content(state, true);
9204  fprintf(fp, "%s", history_file);
9205  pfree(history_file);
9206 
9207  if (fflush(fp) || ferror(fp) || FreeFile(fp))
9208  ereport(ERROR,
9210  errmsg("could not write file \"%s\": %m",
9211  histfilepath)));
9212 
9213  /*
9214  * Clean out any no-longer-needed history files. As a side effect,
9215  * this will post a .ready file for the newly created history file,
9216  * notifying the archiver that history file may be archived
9217  * immediately.
9218  */
9220  }
9221 
9222  /*
9223  * If archiving is enabled, wait for all the required WAL files to be
9224  * archived before returning. If archiving isn't enabled, the required WAL
9225  * needs to be transported via streaming replication (hopefully with
9226  * wal_keep_size set high enough), or some more exotic mechanism like
9227  * polling and copying files from pg_wal with script. We have no knowledge
9228  * of those mechanisms, so it's up to the user to ensure that he gets all
9229  * the required WAL.
9230  *
9231  * We wait until both the last WAL file filled during backup and the
9232  * history file have been archived, and assume that the alphabetic sorting
9233  * property of the WAL files ensures any earlier WAL files are safely
9234  * archived as well.
9235  *
9236  * We wait forever, since archive_command is supposed to work and we
9237  * assume the admin wanted his backup to work completely. If you don't
9238  * wish to wait, then either waitforarchive should be passed in as false,
9239  * or you can set statement_timeout. Also, some notices are issued to
9240  * clue in anyone who might be doing this interactively.
9241  */
9242 
9243  if (waitforarchive &&
9244  ((!backup_stopped_in_recovery && XLogArchivingActive()) ||
9245  (backup_stopped_in_recovery && XLogArchivingAlways())))
9246  {
9247  XLByteToPrevSeg(state->stoppoint, _logSegNo, wal_segment_size);
9248  XLogFileName(lastxlogfilename, state->stoptli, _logSegNo,
9250 
9251  XLByteToSeg(state->startpoint, _logSegNo, wal_segment_size);
9252  BackupHistoryFileName(histfilename, state->stoptli, _logSegNo,
9253  state->startpoint, wal_segment_size);
9254 
9255  seconds_before_warning = 60;
9256  waits = 0;
9257 
9258  while (XLogArchiveIsBusy(lastxlogfilename) ||
9259  XLogArchiveIsBusy(histfilename))
9260  {
9262 
9263  if (!reported_waiting && waits > 5)
9264  {
9265  ereport(NOTICE,
9266  (errmsg("base backup done, waiting for required WAL segments to be archived")));
9267  reported_waiting = true;
9268  }
9269 
9270  (void) WaitLatch(MyLatch,
9272  1000L,
9273  WAIT_EVENT_BACKUP_WAIT_WAL_ARCHIVE);
9275 
9276  if (++waits >= seconds_before_warning)
9277  {
9278  seconds_before_warning *= 2; /* This wraps in >10 years... */
9279  ereport(WARNING,
9280  (errmsg("still waiting for all required WAL segments to be archived (%d seconds elapsed)",
9281  waits),
9282  errhint("Check that your \"archive_command\" is executing properly. "
9283  "You can safely cancel this backup, "
9284  "but the database backup will not be usable without all the WAL segments.")));
9285  }
9286  }
9287 
9288  ereport(NOTICE,
9289  (errmsg("all required WAL segments have been archived")));
9290  }
9291  else if (waitforarchive)
9292  ereport(NOTICE,
9293  (errmsg("WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup")));
9294 }
#define NOTICE
Definition: elog.h:35
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2583
int FreeFile(FILE *file)
Definition: fd.c:2781
struct Latch * MyLatch
Definition: globals.c:60
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
static void const char fflush(stdout)
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define XLOG_BACKUP_END
Definition: pg_control.h:72
#define fprintf
Definition: port.h:242
static void CleanupBackupHistory(void)
Definition: xlog.c:4150
#define XLogArchivingAlways()
Definition: xlog.h:100
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, fflush(), 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 9021 of file xlog.c.

9022 {
9023  return sessionBackupState;
9024 }

References sessionBackupState.

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

◆ get_sync_bit()

static int get_sync_bit ( int  method)
static

Definition at line 8513 of file xlog.c.

8514 {
8515  int o_direct_flag = 0;
8516 
8517  /*
8518  * Use O_DIRECT if requested, except in walreceiver process. The WAL
8519  * written by walreceiver is normally read by the startup process soon
8520  * after it's written. Also, walreceiver performs unaligned writes, which
8521  * don't work with O_DIRECT, so it is required for correctness too.
8522  */
8524  o_direct_flag = PG_O_DIRECT;
8525 
8526  /* If fsync is disabled, never open in sync mode */
8527  if (!enableFsync)
8528  return o_direct_flag;
8529 
8530  switch (method)
8531  {
8532  /*
8533  * enum values for all sync options are defined even if they are
8534  * not supported on the current platform. But if not, they are
8535  * not included in the enum option array, and therefore will never
8536  * be seen here.
8537  */
8538  case WAL_SYNC_METHOD_FSYNC:
8541  return o_direct_flag;
8542 #ifdef O_SYNC
8543  case WAL_SYNC_METHOD_OPEN:
8544  return O_SYNC | o_direct_flag;
8545 #endif
8546 #ifdef O_DSYNC
8548  return O_DSYNC | o_direct_flag;
8549 #endif
8550  default:
8551  /* can't happen (unless we are out of sync with option array) */
8552  elog(ERROR, "unrecognized \"wal_sync_method\": %d", method);
8553  return 0; /* silence warning */
8554  }
8555 }
int io_direct_flags
Definition: fd.c:168
#define IO_DIRECT_WAL
Definition: fd.h:55
#define PG_O_DIRECT
Definition: fd.h:97
bool enableFsync
Definition: globals.c:126
#define AmWalReceiverProcess()
Definition: miscadmin.h:383
#define O_DSYNC
Definition: win32_port.h:352
@ 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

References AmWalReceiverProcess, elog, enableFsync, ERROR, io_direct_flags, IO_DIRECT_WAL, O_DSYNC, PG_O_DIRECT, WAL_SYNC_METHOD_FDATASYNC, WAL_SYNC_METHOD_FSYNC, WAL_SYNC_METHOD_FSYNC_WRITETHROUGH, WAL_SYNC_METHOD_OPEN, and WAL_SYNC_METHOD_OPEN_DSYNC.

Referenced by assign_wal_sync_method(), XLogFileInit(), XLogFileInitInternal(), and XLogFileOpen().

◆ GetActiveWalLevelOnStandby()

WalLevel GetActiveWalLevelOnStandby ( void  )

Definition at line 4826 of file xlog.c.

4827 {
4828  return ControlFile->wal_level;
4829 }

References ControlFile, and ControlFileData::wal_level.

Referenced by CheckLogicalDecodingRequirements().

◆ GetFakeLSNForUnloggedRel()

XLogRecPtr GetFakeLSNForUnloggedRel ( void  )

Definition at line 4571 of file xlog.c.

4572 {
4574 }
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:518

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

Referenced by gistGetFakeLSN().

◆ GetFlushRecPtr()

XLogRecPtr GetFlushRecPtr ( TimeLineID insertTLI)

Definition at line 6455 of file xlog.c.

6456 {
6458 
6460 
6461  /*
6462  * If we're writing and flushing WAL, the time line can't be changing, so
6463  * no lock is required.
6464  */
6465  if (insertTLI)
6466  *insertTLI = XLogCtl->InsertTimeLineID;
6467 
6468  return LogwrtResult.Flush;
6469 }
RecoveryState SharedRecoveryState
Definition: xlog.c:511
XLogRecPtr Flush
Definition: xlog.c:328
@ RECOVERY_STATE_DONE
Definition: xlog.h:91

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

6424 {
6425  *RedoRecPtr_p = RedoRecPtr;
6426  *doPageWrites_p = doPageWrites;
6427 }
static bool doPageWrites
Definition: xlog.c:286

References doPageWrites, and RedoRecPtr.

Referenced by XLogCheckBufferNeedsBackup(), and XLogInsert().

◆ GetInsertRecPtr()

XLogRecPtr GetInsertRecPtr ( void  )

Definition at line 6438 of file xlog.c.

6439 {
6440  XLogRecPtr recptr;
6441 
6443  recptr = XLogCtl->LogwrtRqst.Write;
6445 
6446  return recptr;
6447 }

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

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

◆ GetLastImportantRecPtr()

XLogRecPtr GetLastImportantRecPtr ( void  )

Definition at line 6493 of file xlog.c.

6494 {
6496  int i;
6497 
6498  for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
6499  {
6500  XLogRecPtr last_important;
6501 
6502  /*
6503  * Need to take a lock to prevent torn reads of the LSN, which are
6504  * possible on some of the supported platforms. WAL insert locks only
6505  * support exclusive mode, so we have to use that.
6506  */
6508  last_important = WALInsertLocks[i].l.lastImportantAt;
6509  LWLockRelease(&WALInsertLocks[i].l.lock);
6510 
6511  if (res < last_important)
6512  res = last_important;
6513  }
6514 
6515  return res;
6516 }
int i
Definition: isn.c:73
XLogRecPtr lastImportantAt
Definition: xlog.c:371
WALInsertLock l
Definition: xlog.c:383
static WALInsertLockPadded * WALInsertLocks
Definition: xlog.c:564
#define NUM_XLOGINSERT_LOCKS
Definition: xlog.c:150

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

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

◆ GetLastSegSwitchData()

pg_time_t GetLastSegSwitchData ( XLogRecPtr lastSwitchLSN)

Definition at line 6522 of file xlog.c.

6523 {
6524  pg_time_t result;
6525 
6526  /* Need WALWriteLock, but shared lock is sufficient */
6527  LWLockAcquire(WALWriteLock, LW_SHARED);
6528  result = XLogCtl->lastSegSwitchTime;
6529  *lastSwitchLSN = XLogCtl->lastSegSwitchLSN;
6530  LWLockRelease(WALWriteLock);
6531 
6532  return result;
6533 }
pg_time_t lastSegSwitchTime
Definition: xlog.c:467
XLogRecPtr lastSegSwitchLSN
Definition: xlog.c:468

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

Referenced by CheckArchiveTimeout().

◆ GetMockAuthenticationNonce()

char* GetMockAuthenticationNonce ( void  )

Definition at line 4545 of file xlog.c.

4546 {
4547  Assert(ControlFile != NULL);
4549 }
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:228

References Assert, ControlFile, and ControlFileData::mock_authentication_nonce.

Referenced by scram_mock_salt().

◆ GetOldestRestartPoint()

void GetOldestRestartPoint ( XLogRecPtr oldrecptr,
TimeLineID oldtli 
)

Definition at line 9383 of file xlog.c.

9384 {
9385  LWLockAcquire(ControlFileLock, LW_SHARED);
9386  *oldrecptr = ControlFile->checkPointCopy.redo;
9388  LWLockRelease(ControlFileLock);
9389 }

References ControlFileData::checkPointCopy, ControlFile, LW_SHARED, LWLockAcquire(), LWLockRelease(), CheckPoint::redo, and CheckPoint::ThisTimeLineID.

Referenced by ExecuteRecoveryCommand(), and RestoreArchivedFile().

◆ GetRecoveryState()

RecoveryState GetRecoveryState ( void  )

Definition at line 6326 of file xlog.c.

6327 {
6328  RecoveryState retval;
6329 
6331  retval = XLogCtl->SharedRecoveryState;
6333 
6334  return retval;
6335 }
RecoveryState
Definition: xlog.h:88

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

Referenced by XLogArchiveCheckDone().

◆ GetRedoRecPtr()

XLogRecPtr GetRedoRecPtr ( void  )

Definition at line 6393 of file xlog.c.

6394 {
6395  XLogRecPtr ptr;
6396 
6397  /*
6398  * The possibly not up-to-date copy in XlogCtl is enough. Even if we
6399  * grabbed a WAL insertion lock to read the authoritative value in
6400  * Insert->RedoRecPtr, someone might update it just after we've released
6401  * the lock.
6402  */
6404  ptr = XLogCtl->RedoRecPtr;
6406 
6407  if (RedoRecPtr < ptr)
6408  RedoRecPtr = ptr;
6409 
6410  return RedoRecPtr;
6411 }

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

uint64 GetSystemIdentifier ( void  )

◆ GetWALAvailability()

WALAvailability GetWALAvailability ( XLogRecPtr  targetLSN)

Definition at line 7790 of file xlog.c.

7791 {
7792  XLogRecPtr currpos; /* current write LSN */
7793  XLogSegNo currSeg; /* segid of currpos */
7794  XLogSegNo targetSeg; /* segid of targetLSN */
7795  XLogSegNo oldestSeg; /* actual oldest segid */
7796  XLogSegNo oldestSegMaxWalSize; /* oldest segid kept by max_wal_size */
7797  XLogSegNo oldestSlotSeg; /* oldest segid kept by slot */
7798  uint64 keepSegs;
7799 
7800  /*
7801  * slot does not reserve WAL. Either deactivated, or has never been active
7802  */
7803  if (XLogRecPtrIsInvalid(targetLSN))
7804  return WALAVAIL_INVALID_LSN;
7805 
7806  /*
7807  * Calculate the oldest segment currently reserved by all slots,
7808  * considering wal_keep_size and max_slot_wal_keep_size. Initialize
7809  * oldestSlotSeg to the current segment.
7810  */
7811  currpos = GetXLogWriteRecPtr();
7812  XLByteToSeg(currpos, oldestSlotSeg, wal_segment_size);
7813  KeepLogSeg(currpos, &oldestSlotSeg);
7814 
7815  /*
7816  * Find the oldest extant segment file. We get 1 until checkpoint removes
7817  * the first WAL segment file since startup, which causes the status being
7818  * wrong under certain abnormal conditions but that doesn't actually harm.
7819  */
7820  oldestSeg = XLogGetLastRemovedSegno() + 1;
7821 
7822  /* calculate oldest segment by max_wal_size */
7823  XLByteToSeg(currpos, currSeg, wal_segment_size);
7825 
7826  if (currSeg > keepSegs)
7827  oldestSegMaxWalSize = currSeg - keepSegs;
7828  else
7829  oldestSegMaxWalSize = 1;
7830 
7831  /* the segment we care about */
7832  XLByteToSeg(targetLSN, targetSeg, wal_segment_size);
7833 
7834  /*
7835  * No point in returning reserved or extended status values if the
7836  * targetSeg is known to be lost.
7837  */
7838  if (targetSeg >= oldestSlotSeg)
7839  {
7840  /* show "reserved" when targetSeg is within max_wal_size */
7841  if (targetSeg >= oldestSegMaxWalSize)
7842  return WALAVAIL_RESERVED;
7843 
7844  /* being retained by slots exceeding max_wal_size */
7845  return WALAVAIL_EXTENDED;
7846  }
7847 
7848  /* WAL segments are no longer retained but haven't been removed yet */
7849  if (targetSeg >= oldestSeg)
7850  return WALAVAIL_UNRESERVED;
7851 
7852  /* Definitely lost */
7853  return WALAVAIL_REMOVED;
7854 }
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3747
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:9371
@ WALAVAIL_REMOVED
Definition: xlog.h:191
@ WALAVAIL_RESERVED
Definition: xlog.h:187
@ WALAVAIL_UNRESERVED
Definition: xlog.h:190
@ WALAVAIL_EXTENDED
Definition: xlog.h:188
@ WALAVAIL_INVALID_LSN
Definition: xlog.h:186

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  )

Definition at line 6476 of file xlog.c.

6477 {
6479 
6480  /* Since the value can't be changing, no lock is required. */
6481  return XLogCtl->InsertTimeLineID;
6482 }

References Assert, XLogCtlData::InsertTimeLineID, RECOVERY_STATE_DONE, XLogCtlData::SharedRecoveryState, and XLogCtl.

Referenced by logical_read_xlog_page(), pg_walfile_name(), pg_walfile_name_offset(), ReadReplicationSlot(), WALReadFromBuffers(), and XLogSendPhysical().

◆ GetXLogBuffer()

static char * GetXLogBuffer ( XLogRecPtr  ptr,
TimeLineID  tli 
)
static

Definition at line 1627 of file xlog.c.

1628 {
1629  int idx;
1630  XLogRecPtr endptr;
1631  static uint64 cachedPage = 0;
1632  static char *cachedPos = NULL;
1633  XLogRecPtr expectedEndPtr;
1634 
1635  /*
1636  * Fast path for the common case that we need to access again the same
1637  * page as last time.
1638  */
1639  if (ptr / XLOG_BLCKSZ == cachedPage)
1640  {
1641  Assert(((XLogPageHeader) cachedPos)->xlp_magic == XLOG_PAGE_MAGIC);
1642  Assert(((XLogPageHeader) cachedPos)->xlp_pageaddr == ptr - (ptr % XLOG_BLCKSZ));
1643  return cachedPos + ptr % XLOG_BLCKSZ;
1644  }
1645 
1646  /*
1647  * The XLog buffer cache is organized so that a page is always loaded to a
1648  * particular buffer. That way we can easily calculate the buffer a given
1649  * page must be loaded into, from the XLogRecPtr alone.
1650  */
1651  idx = XLogRecPtrToBufIdx(ptr);
1652 
1653  /*
1654  * See what page is loaded in the buffer at the moment. It could be the
1655  * page we're looking for, or something older. It can't be anything newer
1656  * - that would imply the page we're looking for has already been written
1657  * out to disk and evicted, and the caller is responsible for making sure
1658  * that doesn't happen.
1659  *
1660  * We don't hold a lock while we read the value. If someone is just about
1661  * to initialize or has just initialized the page, it's possible that we
1662  * get InvalidXLogRecPtr. That's ok, we'll grab the mapping lock (in
1663  * AdvanceXLInsertBuffer) and retry if we see anything other than the page
1664  * we're looking for.
1665  */
1666  expectedEndPtr = ptr;
1667  expectedEndPtr += XLOG_BLCKSZ - ptr % XLOG_BLCKSZ;
1668 
1669  endptr = pg_atomic_read_u64(&XLogCtl->xlblocks[idx]);
1670  if (expectedEndPtr != endptr)
1671  {
1672  XLogRecPtr initializedUpto;
1673 
1674  /*
1675  * Before calling AdvanceXLInsertBuffer(), which can block, let others
1676  * know how far we're finished with inserting the record.
1677  *
1678  * NB: If 'ptr' points to just after the page header, advertise a
1679  * position at the beginning of the page rather than 'ptr' itself. If
1680  * there are no other insertions running, someone might try to flush
1681  * up to our advertised location. If we advertised a position after
1682  * the page header, someone might try to flush the page header, even
1683  * though page might actually not be initialized yet. As the first
1684  * inserter on the page, we are effectively responsible for making
1685  * sure that it's initialized, before we let insertingAt to move past
1686  * the page header.
1687  */
1688  if (ptr % XLOG_BLCKSZ == SizeOfXLogShortPHD &&
1689  XLogSegmentOffset(ptr, wal_segment_size) > XLOG_BLCKSZ)
1690  initializedUpto = ptr - SizeOfXLogShortPHD;
1691  else if (ptr % XLOG_BLCKSZ == SizeOfXLogLongPHD &&
1692  XLogSegmentOffset(ptr, wal_segment_size) < XLOG_BLCKSZ)
1693  initializedUpto = ptr - SizeOfXLogLongPHD;
1694  else
1695  initializedUpto = ptr;
1696 
1697  WALInsertLockUpdateInsertingAt(initializedUpto);
1698 
1699  AdvanceXLInsertBuffer(ptr, tli, false);
1700  endptr = pg_atomic_read_u64(&XLogCtl->xlblocks[idx]);
1701 
1702  if (expectedEndPtr != endptr)
1703  elog(PANIC, "could not find WAL buffer for %X/%X",
1704  LSN_FORMAT_ARGS(ptr));
1705  }
1706  else
1707  {
1708  /*
1709  * Make sure the initialization of the page is visible to us, and
1710  * won't arrive later to overwrite the WAL data we write on the page.
1711  */
1713  }
1714 
1715  /*
1716  * Found the buffer holding this page. Return a pointer to the right
1717  * offset within the page.
1718  */
1719  cachedPage = ptr / XLOG_BLCKSZ;
1720  cachedPos = XLogCtl->pages + idx * (Size) XLOG_BLCKSZ;
1721 
1722  Assert(((XLogPageHeader) cachedPos)->xlp_magic == XLOG_PAGE_MAGIC);
1723  Assert(((XLogPageHeader) cachedPos)->xlp_pageaddr == ptr - (ptr % XLOG_BLCKSZ));
1724 
1725  return cachedPos + ptr % XLOG_BLCKSZ;
1726 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define pg_memory_barrier()
Definition: atomics.h:138
static void WALInsertLockUpdateInsertingAt(XLogRecPtr insertingAt)
Definition: xlog.c:1466
static void AdvanceXLInsertBuffer(XLogRecPtr upto, TimeLineID tli, bool opportunistic)
Definition: xlog.c:1980

References AdvanceXLInsertBuffer(), Assert, elog, idx(), LSN_FORMAT_ARGS, XLogCtlData::pages, PANIC, pg_atomic_read_u64(), pg_memory_barrier, SizeOfXLogLongPHD, SizeOfXLogShortPHD, wal_segment_size, WALInsertLockUpdateInsertingAt(), XLogCtlData::xlblocks, XLOG_PAGE_MAGIC, XLogCtl, XLogRecPtrToBufIdx, and XLogSegmentOffset.

Referenced by CopyXLogRecordToWAL(), and CreateOverwriteContrecordRecord().

◆ GetXLogInsertRecPtr()

XLogRecPtr GetXLogInsertRecPtr ( void  )

Definition at line 9355 of file xlog.c.

9356 {
9358  uint64 current_bytepos;
9359 
9360  SpinLockAcquire(&Insert->insertpos_lck);
9361  current_bytepos = Insert->CurrBytePos;
9362  SpinLockRelease(&Insert->insertpos_lck);
9363 
9364  return XLogBytePosToRecPtr(current_bytepos);
9365 }

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  )

◆ InitControlFile()

static void InitControlFile ( uint64  sysidentifier)
static

Definition at line 4193 of file xlog.c.

4194 {
4195  char mock_auth_nonce[MOCK_AUTH_NONCE_LEN];
4196 
4197  /*
4198  * Generate a random nonce. This is used for authentication requests that
4199  * will fail because the user does not exist. The nonce is used to create
4200  * a genuine-looking password challenge for the non-existent user, in lieu
4201  * of an actual stored password.
4202  */
4203  if (!pg_strong_random(mock_auth_nonce, MOCK_AUTH_NONCE_LEN))
4204  ereport(PANIC,
4205  (errcode(ERRCODE_INTERNAL_ERROR),
4206  errmsg("could not generate secret authorization token")));
4207 
4208  memset(ControlFile, 0, sizeof(ControlFileData));
4209  /* Initialize pg_control status fields */
4210  ControlFile->system_identifier = sysidentifier;
4211  memcpy(ControlFile->mock_authentication_nonce, mock_auth_nonce, MOCK_AUTH_NONCE_LEN);
4214 
4215  /* Set important parameter values for use when replaying WAL */
4225 }
bool track_commit_timestamp
Definition: commit_ts.c:109
#define MOCK_AUTH_NONCE_LEN
Definition: pg_control.h:28
bool pg_strong_random(void *buf, size_t len)
bool track_commit_timestamp
Definition: pg_control.h:184
int wal_level
Definition: xlog.c:131
bool wal_log_hints
Definition: xlog.c:123
uint32 bootstrap_data_checksum_version
Definition: bootstrap.c:44
#define FirstNormalUnloggedLSN
Definition: xlogdefs.h:36

References bootstrap_data_checksum_version, ControlFile, ControlFileData::data_checksum_version, DB_SHUTDOWNED, ereport, errcode(), errmsg(), FirstNormalUnloggedLSN, max_locks_per_xact, ControlFileData::max_locks_per_xact, max_prepared_xacts, ControlFileData::max_prepared_xacts, max_wal_senders, ControlFileData::max_wal_senders, max_worker_processes, ControlFileData::max_worker_processes, MaxConnections, ControlFileData::MaxConnections, MOCK_AUTH_NONCE_LEN, ControlFileData::mock_authentication_nonce, PANIC, pg_strong_random(), ControlFileData::state, ControlFileData::system_identifier, track_commit_timestamp, ControlFileData::track_commit_timestamp, ControlFileData::unloggedLSN, wal_level, ControlFileData::wal_level, wal_log_hints, and ControlFileData::wal_log_hints.

Referenced by BootStrapXLOG().

◆ InitializeWalConsistencyChecking()

void InitializeWalConsistencyChecking ( void  )

Definition at line 4751 of file xlog.c.

4752 {
4754 
4756  {
4757  struct config_generic *guc;
4758 
4759  guc = find_option("wal_consistency_checking", false, false, ERROR);
4760 
4762 
4763  set_config_option_ext("wal_consistency_checking",
4765  guc->scontext, guc->source, guc->srole,
4766  GUC_ACTION_SET, true, ERROR, false);
4767 
4768  /* checking should not be deferred again */
4770  }
4771 }
struct config_generic * find_option(const char *name, bool create_placeholders, bool skip_errors, int elevel)
Definition: guc.c:1237
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:3383
@ GUC_ACTION_SET
Definition: guc.h:199
GucContext scontext
Definition: guc_tables.h:167
GucSource source
Definition: guc_tables.h:165
char * wal_consistency_checking_string
Definition: xlog.c:125

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

◆ InstallXLogFileSegment()

static bool InstallXLogFileSegment ( XLogSegNo segno,
char *  tmppath,
bool  find_free,
XLogSegNo  max_segno,
TimeLineID  tli 
)
static

Definition at line 3552 of file xlog.c.

3554 {
3555  char path[MAXPGPATH];
3556  struct stat stat_buf;
3557 
3558  Assert(tli != 0);
3559 
3560  XLogFilePath(path, tli, *segno, wal_segment_size);
3561 
3562  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
3564  {
3565  LWLockRelease(ControlFileLock);
3566  return false;
3567  }
3568 
3569  if (!find_free)
3570  {
3571  /* Force installation: get rid of any pre-existing segment file */
3572  durable_unlink(path, DEBUG1);
3573  }
3574  else
3575  {
3576  /* Find a free slot to put it in */
3577  while (stat(path, &stat_buf) == 0)
3578  {
3579  if ((*segno) >= max_segno)
3580  {
3581  /* Failed to find a free slot within specified range */
3582  LWLockRelease(ControlFileLock);
3583  return false;
3584  }
3585  (*segno)++;
3586  XLogFilePath(path, tli, *segno, wal_segment_size);
3587  }
3588  }
3589 
3590  Assert(access(path, F_OK) != 0 && errno == ENOENT);
3591  if (durable_rename(tmppath, path, LOG) != 0)
3592  {
3593  LWLockRelease(ControlFileLock);
3594  /* durable_rename already emitted log message */
3595  return false;
3596  }
3597 
3598  LWLockRelease(ControlFileLock);
3599 
3600  return true;
3601 }
int durable_unlink(const char *fname, int elevel)
Definition: fd.c:872
short access
Definition: preproc-type.c:36
bool InstallXLogFileSegmentActive
Definition: xlog.c:521
#define stat
Definition: win32_port.h:284

References Assert, DEBUG1, durable_rename(), durable_unlink(), XLogCtlData::InstallXLogFileSegmentActive, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, stat, wal_segment_size, XLogCtl, and XLogFilePath().

Referenced by RemoveXlogFile(), XLogFileCopy(), and XLogFileInitInternal().

◆ IsInstallXLogFileSegmentActive()

bool IsInstallXLogFileSegmentActive ( void  )

Definition at line 9412 of file xlog.c.

9413 {
9414  bool result;
9415 
9416  LWLockAcquire(ControlFileLock, LW_SHARED);
9418  LWLockRelease(ControlFileLock);
9419 
9420  return result;
9421 }

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

8604 {
8605  char *msg = NULL;
8606  instr_time start;
8607 
8608  Assert(tli != 0);
8609 
8610  /*
8611  * Quick exit if fsync is disabled or write() has already synced the WAL
8612  * file.
8613  */
8614  if (!enableFsync ||
8617  return;
8618 
8619  /* Measure I/O timing to sync the WAL file */
8620  if (track_wal_io_timing)
8622  else
8624 
8625  pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
8626  switch (wal_sync_method)
8627  {
8628  case WAL_SYNC_METHOD_FSYNC:
8629  if (pg_fsync_no_writethrough(fd) != 0)
8630  msg = _("could not fsync file \"%s\": %m");
8631  break;
8632 #ifdef HAVE_FSYNC_WRITETHROUGH
8634  if (pg_fsync_writethrough(fd) != 0)
8635  msg = _("could not fsync write-through file \"%s\": %m");
8636  break;
8637 #endif
8639  if (pg_fdatasync(fd) != 0)
8640  msg = _("could not fdatasync file \"%s\": %m");
8641  break;
8642  case WAL_SYNC_METHOD_OPEN:
8644  /* not reachable */
8645  Assert(false);
8646  break;
8647  default:
8648  ereport(PANIC,
8649  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8650  errmsg_internal("unrecognized \"wal_sync_method\": %d", wal_sync_method));
8651  break;
8652  }
8653 
8654  /* PANIC if failed to fsync */
8655  if (msg)
8656  {
8657  char xlogfname[MAXFNAMELEN];
8658  int save_errno = errno;
8659 
8660  XLogFileName(xlogfname, tli, segno, wal_segment_size);
8661  errno = save_errno;
8662  ereport(PANIC,
8664  errmsg(msg, xlogfname)));
8665  }
8666 
8668 
8669  /*
8670  * Increment the I/O timing and the number of times WAL files were synced.
8671  */
8672  if (track_wal_io_timing)
8673  {
8674  instr_time end;
8675 
8678  }
8679 
8681 }
#define _(x)
Definition: elog.c:90
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
return str start
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:172
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition: instr_time.h:184
static int fd(const char *x, int i)
Definition: preproc-init.c:105
instr_time wal_sync_time
Definition: pgstat.h:456
PgStat_Counter wal_sync
Definition: pgstat.h:454
bool track_wal_io_timing
Definition: xlog.c:137

References _, Assert, enableFsync, ereport, errcode(), errcode_for_file_access(), errmsg(), errmsg_internal(), fd(), INSTR_TIME_ACCUM_DIFF, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, MAXFNAMELEN, PANIC, PendingWalStats, pg_fdatasync(), pg_fsync_no_writethrough(), pg_fsync_writethrough(), pgstat_report_wait_end(), pgstat_report_wait_start(), start, track_wal_io_timing, wal_segment_size, PgStat_PendingWalStats::wal_sync, 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, PgStat_PendingWalStats::wal_sync_time, and XLogFileName().

Referenced by XLogWalRcvFlush(), and XLogWrite().

◆ KeepLogSeg()

static void KeepLogSeg ( XLogRecPtr  recptr,
XLogSegNo logSegNo 
)
static

Definition at line 7874 of file xlog.c.

7875 {
7876  XLogSegNo currSegNo;
7877  XLogSegNo segno;
7878  XLogRecPtr keep;
7879 
7880  XLByteToSeg(recptr, currSegNo, wal_segment_size);
7881  segno = currSegNo;
7882 
7883  /*
7884  * Calculate how many segments are kept by slots first, adjusting for
7885  * max_slot_wal_keep_size.
7886  */
7888  if (keep != InvalidXLogRecPtr && keep < recptr)
7889  {
7890  XLByteToSeg(keep, segno, wal_segment_size);
7891 
7892  /* Cap by max_slot_wal_keep_size ... */
7893  if (max_slot_wal_keep_size_mb >= 0)
7894  {
7895  uint64 slot_keep_segs;
7896 
7897  slot_keep_segs =
7899 
7900  if (currSegNo - segno > slot_keep_segs)
7901  segno = currSegNo - slot_keep_segs;
7902  }
7903  }
7904 
7905  /*
7906  * If WAL summarization is in use, don't remove WAL that has yet to be
7907  * summarized.
7908  */
7909  keep = GetOldestUnsummarizedLSN(NULL, NULL, false);
7910  if (keep != InvalidXLogRecPtr)
7911  {
7912  XLogSegNo unsummarized_segno;
7913 
7914  XLByteToSeg(keep, unsummarized_segno, wal_segment_size);
7915  if (unsummarized_segno < segno)
7916  segno = unsummarized_segno;
7917  }
7918 
7919  /* but, keep at least wal_keep_size if that's set */
7920  if (wal_keep_size_mb > 0)
7921  {
7922  uint64 keep_segs;
7923 
7925  if (currSegNo - segno < keep_segs)
7926  {
7927  /* avoid underflow, don't go below 1 */
7928  if (currSegNo <= keep_segs)
7929  segno = 1;
7930  else
7931  segno = currSegNo - keep_segs;
7932  }
7933  }
7934 
7935  /* don't delete WAL segments newer than the calculated segment */
7936  if (segno < *logSegNo)
7937  *logSegNo = segno;
7938 }
XLogRecPtr GetOldestUnsummarizedLSN(TimeLineID *tli, bool *lsn_is_exact, bool reset_pending_lsn)
int wal_keep_size_mb
Definition: xlog.c:116
static XLogRecPtr XLogGetReplicationSlotMinimumLSN(void)
Definition: xlog.c:2690
int max_slot_wal_keep_size_mb
Definition: xlog.c:135

References ConvertToXSegs, GetOldestUnsummarizedLSN(), InvalidXLogRecPtr, max_slot_wal_keep_size_mb, wal_keep_size_mb, wal_segment_size, XLByteToSeg, and XLogGetReplicationSlotMinimumLSN().

Referenced by CreateCheckPoint(), CreateRestartPoint(), and GetWALAvailability().

◆ LocalProcessControlFile()

void LocalProcessControlFile ( bool  reset)

Definition at line 4813 of file xlog.c.

4814 {
4815  Assert(reset || ControlFile == NULL);
4816  ControlFile = palloc(sizeof(ControlFileData));
4817  ReadControlFile();
4818 }
void reset(void)
Definition: sql-declare.c:600

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

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

◆ LocalSetXLogInsertAllowed()

static int LocalSetXLogInsertAllowed ( void  )
static

Definition at line 6378 of file xlog.c.

6379 {
6380  int oldXLogAllowed = LocalXLogInsertAllowed;
6381 
6383 
6384  return oldXLogAllowed;
6385 }

References LocalXLogInsertAllowed.

Referenced by CreateCheckPoint(), and StartupXLOG().

◆ LogCheckpointEnd()

static void LogCheckpointEnd ( bool  restartpoint)
static

Definition at line 6618 of file xlog.c.

6619 {
6620  long write_msecs,
6621  sync_msecs,
6622  total_msecs,
6623  longest_msecs,
6624  average_msecs;
6625  uint64 average_sync_time;
6626 
6628 
6631 
6634 
6635  /* Accumulate checkpoint timing summary data, in milliseconds. */
6636  PendingCheckpointerStats.write_time += write_msecs;
6637  PendingCheckpointerStats.sync_time += sync_msecs;
6638 
6639  /*
6640  * All of the published timing statistics are accounted for. Only
6641  * continue if a log message is to be written.
6642  */
6643  if (!log_checkpoints)
6644  return;
6645 
6648 
6649  /*
6650  * Timing values returned from CheckpointStats are in microseconds.
6651  * Convert to milliseconds for consistent printing.
6652  */
6653  longest_msecs = (long) ((CheckpointStats.ckpt_longest_sync + 999) / 1000);
6654 
6655  average_sync_time = 0;
6657  average_sync_time = CheckpointStats.ckpt_agg_sync_time /
6659  average_msecs = (long) ((average_sync_time + 999) / 1000);
6660 
6661  /*
6662  * ControlFileLock is not required to see ControlFile->checkPoint and
6663  * ->checkPointCopy here as we are the only updator of those variables at
6664  * this moment.
6665  */
6666  if (restartpoint)
6667  ereport(LOG,
6668  (errmsg("restartpoint complete: wrote %d buffers (%.1f%%); "
6669  "%d WAL file(s) added, %d removed, %d recycled; "
6670  "write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s; "
6671  "sync files=%d, longest=%ld.%03d s, average=%ld.%03d s; "
6672  "distance=%d kB, estimate=%d kB; "
6673  "lsn=%X/%X, redo lsn=%X/%X",
6675  (double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
6679  write_msecs / 1000, (int) (write_msecs % 1000),
6680  sync_msecs / 1000, (int) (sync_msecs % 1000),
6681  total_msecs / 1000, (int) (total_msecs % 1000),
6683  longest_msecs / 1000, (int) (longest_msecs % 1000),
6684  average_msecs / 1000, (int) (average_msecs % 1000),
6685  (int) (PrevCheckPointDistance / 1024.0),
6686  (int) (CheckPointDistanceEstimate / 1024.0),
6689  else
6690  ereport(LOG,
6691  (errmsg("checkpoint complete: wrote %d buffers (%.1f%%); "
6692  "%d WAL file(s) added, %d removed, %d recycled; "
6693  "write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s; "
6694  "sync files=%d, longest=%ld.%03d s, average=%ld.%03d s; "
6695  "distance=%d kB, estimate=%d kB; "
6696  "lsn=%X/%X, redo lsn=%X/%X",
6698  (double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
6702  write_msecs / 1000, (int) (write_msecs % 1000),
6703  sync_msecs / 1000, (int) (sync_msecs % 1000),
6704  total_msecs / 1000, (int) (total_msecs % 1000),
6706  longest_msecs / 1000, (int) (longest_msecs % 1000),
6707  average_msecs / 1000, (int) (average_msecs % 1000),
6708  (int) (PrevCheckPointDistance / 1024.0),
6709  (int) (CheckPointDistanceEstimate / 1024.0),
6712 }
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1766
PgStat_CheckpointerStats PendingCheckpointerStats
uint64 ckpt_agg_sync_time
Definition: xlog.h:173
uint64 ckpt_longest_sync
Definition: xlog.h:172
TimestampTz ckpt_end_t
Definition: xlog.h:163
int ckpt_sync_rels
Definition: xlog.h:171
PgStat_Counter sync_time
Definition: pgstat.h:269
PgStat_Counter write_time
Definition: pgstat.h:268
static double CheckPointDistanceEstimate
Definition: xlog.c:159
static double PrevCheckPointDistance
Definition: xlog.c:160

References ControlFileData::checkPoint, ControlFileData::checkPointCopy, CheckPointDistanceEstimate, CheckpointStats, CheckpointStatsData::ckpt_agg_sync_time, CheckpointStatsData::ckpt_bufs_written, CheckpointStatsData::ckpt_end_t, CheckpointStatsData::ckpt_longest_sync, CheckpointStatsData::ckpt_segs_added, CheckpointStatsData::ckpt_segs_recycled, CheckpointStatsData::ckpt_segs_removed, CheckpointStatsData::ckpt_start_t, CheckpointStatsData::ckpt_sync_end_t, CheckpointStatsData::ckpt_sync_rels, CheckpointStatsData::ckpt_sync_t, CheckpointStatsData::ckpt_write_t, ControlFile, ereport, errmsg(), GetCurrentTimestamp(), LOG, log_checkpoints, LSN_FORMAT_ARGS, NBuffers, PendingCheckpointerStats, PrevCheckPointDistance, CheckPoint::redo, PgStat_CheckpointerStats::sync_time, TimestampDifferenceMilliseconds(), and PgStat_CheckpointerStats::write_time.

Referenced by CreateCheckPoint(), and CreateRestartPoint().

◆ LogCheckpointStart()

static void LogCheckpointStart ( int  flags,
bool  restartpoint 
)
static

Definition at line 6586 of file xlog.c.

6587 {
6588  if (restartpoint)
6589  ereport(LOG,
6590  /* translator: the placeholders show checkpoint options */
6591  (errmsg("restartpoint starting:%s%s%s%s%s%s%s%s",
6592  (flags & CHECKPOINT_IS_SHUTDOWN) ? " shutdown" : "",
6593  (flags & CHECKPOINT_END_OF_RECOVERY) ? " end-of-recovery" : "",
6594  (flags & CHECKPOINT_IMMEDIATE) ? " immediate" : "",
6595  (flags & CHECKPOINT_FORCE) ? " force" : "",
6596  (flags & CHECKPOINT_WAIT) ? " wait" : "",
6597  (flags & CHECKPOINT_CAUSE_XLOG) ? " wal" : "",
6598  (flags & CHECKPOINT_CAUSE_TIME) ? " time" : "",
6599  (flags & CHECKPOINT_FLUSH_ALL) ? " flush-all" : "")));
6600  else
6601  ereport(LOG,
6602  /* translator: the placeholders show checkpoint options */
6603  (errmsg("checkpoint starting:%s%s%s%s%s%s%s%s",
6604  (flags & CHECKPOINT_IS_SHUTDOWN) ? " shutdown" : "",
6605  (flags & CHECKPOINT_END_OF_RECOVERY) ? " end-of-recovery" : "",
6606  (flags & CHECKPOINT_IMMEDIATE) ? " immediate" : "",
6607  (flags & CHECKPOINT_FORCE) ? " force" : "",
6608  (flags & CHECKPOINT_WAIT) ? " wait" : "",
6609  (flags & CHECKPOINT_CAUSE_XLOG) ? " wal" : "",
6610  (flags & CHECKPOINT_CAUSE_TIME) ? " time" : "",
6611  (flags & CHECKPOINT_FLUSH_ALL) ? " flush-all" : "")));
6612 }
#define CHECKPOINT_CAUSE_XLOG
Definition: xlog.h:146
#define CHECKPOINT_FLUSH_ALL
Definition: xlog.h:141
#define CHECKPOINT_CAUSE_TIME
Definition: xlog.h:147

References CHECKPOINT_CAUSE_TIME, CHECKPOINT_CAUSE_XLOG, CHECKPOINT_END_OF_RECOVERY, CHECKPOINT_FLUSH_ALL, CHECKPOINT_FORCE, CHECKPOINT_IMMEDIATE, CHECKPOINT_IS_SHUTDOWN, CHECKPOINT_WAIT, ereport, errmsg(), and LOG.

Referenced by CreateCheckPoint(), and CreateRestartPoint().

◆ PerformRecoveryXLogAction()

static bool PerformRecoveryXLogAction ( void  )
static

Definition at line 6240 of file xlog.c.

6241 {
6242  bool promoted = false;
6243 
6244  /*
6245  * Perform a checkpoint to update all our recovery activity to disk.
6246  *
6247  * Note that we write a shutdown checkpoint rather than an on-line one.
6248  * This is not particularly critical, but since we may be assigning a new
6249  * TLI, using a shutdown checkpoint allows us to have the rule that TLI
6250  * only changes in shutdown checkpoints, which allows some extra error
6251  * checking in xlog_redo.
6252  *
6253  * In promotion, only create a lightweight end-of-recovery record instead
6254  * of a full checkpoint. A checkpoint is requested later, after we're
6255  * fully out of recovery mode and already accepting queries.
6256  */
6259  {
6260  promoted = true;
6261 
6262  /*
6263  * Insert a special WAL record to mark the end of recovery, since we
6264  * aren't doing a checkpoint. That means that the checkpointer process
6265  * may likely be in the middle of a time-smoothed restartpoint and
6266  * could continue to be for minutes after this. That sounds strange,
6267  * but the effect is roughly the same and it would be stranger to try
6268  * to come out of the restartpoint and then checkpoint. We request a
6269  * checkpoint later anyway, just for safety.
6270  */
6272  }
6273  else
6274  {
6277  CHECKPOINT_WAIT);
6278  }
6279 
6280  return promoted;
6281 }
static void CreateEndOfRecoveryRecord(void)
Definition: xlog.c:7297
bool PromoteIsTriggered(void)

References ArchiveRecoveryRequested, CHECKPOINT_END_OF_RECOVERY, CHECKPOINT_IMMEDIATE, CHECKPOINT_WAIT, CreateEndOfRecoveryRecord(), IsUnderPostmaster, PromoteIsTriggered(), and RequestCheckpoint().

Referenced by StartupXLOG().

◆ PreallocXlogFiles()

static void PreallocXlogFiles ( XLogRecPtr  endptr,
TimeLineID  tli 
)
static

Definition at line 3679 of file xlog.c.

3680 {
3681  XLogSegNo _logSegNo;
3682  int lf;
3683  bool added;
3684  char path[MAXPGPATH];
3685  uint64 offset;
3686 
3688  return; /* unlocked check says no */
3689 
3690  XLByteToPrevSeg(endptr, _logSegNo, wal_segment_size);
3691  offset = XLogSegmentOffset(endptr - 1, wal_segment_size);
3692  if (offset >= (uint32) (0.75 * wal_segment_size))
3693  {
3694  _logSegNo++;
3695  lf = XLogFileInitInternal(_logSegNo, tli, &added, path);
3696  if (lf >= 0)
3697  close(lf);
3698  if (added)
3700  }
3701 }
static int XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli, bool *added, char *path)
Definition: xlog.c:3199

References CheckpointStats, CheckpointStatsData::ckpt_segs_added, close, XLogCtlData::InstallXLogFileSegmentActive, MAXPGPATH, wal_segment_size, XLByteToPrevSeg, XLogCtl, XLogFileInitInternal(), and XLogSegmentOffset.

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

◆ ReachedEndOfBackup()

void ReachedEndOfBackup ( XLogRecPtr  EndRecPtr,
TimeLineID  tli 
)

Definition at line 6203 of file xlog.c.

6204 {
6205  /*
6206  * We have reached the end of base backup, as indicated by pg_control. The
6207  * data on disk is now consistent (unless minRecoveryPoint is further
6208  * ahead, which can happen if we crashed during previous recovery). Reset
6209  * backupStartPoint and backupEndPoint, and update minRecoveryPoint to
6210  * make sure we don't allow starting up at an earlier point even if
6211  * recovery is stopped and restarted soon after this.
6212  */
6213  LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6214 
6215  if (ControlFile->minRecoveryPoint < EndRecPtr)
6216  {
6217  ControlFile->minRecoveryPoint = EndRecPtr;
6219  }
6220 
6223  ControlFile->backupEndRequired = false;
6225 
6226  LWLockRelease(ControlFileLock);
6227 }
XLogRecPtr backupStartPoint
Definition: pg_control.h:169
bool backupEndRequired
Definition: pg_control.h:171
XLogRecPtr backupEndPoint
Definition: pg_control.h:170

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

Referenced by CheckRecoveryConsistency().

◆ ReadControlFile()

static void ReadControlFile ( void  )
static

Definition at line 4310 of file xlog.c.

4311 {
4312  pg_crc32c crc;
4313  int fd;
4314  static char wal_segsz_str[20];
4315  int r;
4316 
4317  /*
4318  * Read data...
4319  */
4321  O_RDWR | PG_BINARY);
4322  if (fd < 0)
4323  ereport(PANIC,
4325  errmsg("could not open file \"%s\": %m",
4326  XLOG_CONTROL_FILE)));
4327 
4328  pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_READ);
4329  r = read(fd, ControlFile, sizeof(ControlFileData));
4330  if (r != sizeof(ControlFileData))
4331  {
4332  if (r < 0)
4333  ereport(PANIC,
4335  errmsg("could not read file \"%s\": %m",
4336  XLOG_CONTROL_FILE)));
4337  else
4338  ereport(PANIC,
4340  errmsg("could not read file \"%s\": read %d of %zu",
4341  XLOG_CONTROL_FILE, r, sizeof(ControlFileData))));
4342  }
4344 
4345  close(fd);
4346 
4347  /*
4348  * Check for expected pg_control format version. If this is wrong, the
4349  * CRC check will likely fail because we'll be checking the wrong number
4350  * of bytes. Complaining about wrong version will probably be more
4351  * enlightening than complaining about wrong CRC.
4352  */
4353 
4355  ereport(FATAL,
4356  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
4357  errmsg("database files are incompatible with server"),
4358  errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d (0x%08x),"
4359  " but the server was compiled with PG_CONTROL_VERSION %d (0x%08x).",
4362  errhint("This could be a problem of mismatched byte ordering. It looks like you need to initdb.")));
4363 
4365  ereport(FATAL,
4366  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
4367  errmsg("database files are incompatible with server"),
4368  errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d,"
4369  " but the server was compiled with PG_CONTROL_VERSION %d.",
4371  errhint("It looks like you need to initdb.")));
4372 
4373  /* Now check the CRC. */
4374  INIT_CRC32C(crc);
4375  COMP_CRC32C(crc,
4376  (char *) ControlFile,
4377  offsetof(ControlFileData, crc));
4378  FIN_CRC32C(crc);
4379 
4380  if (!EQ_CRC32C(crc, ControlFile->crc))
4381  ereport(FATAL,
4382  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
4383  errmsg("incorrect checksum in control file")));
4384 
4385  /*
4386  * Do compatibility checking immediately. If the database isn't
4387  * compatible with the backend executable, we want to abort before we can
4388  * possibly do any damage.
4389  */
4391  ereport(FATAL,
4392  (