Split xlog.c into xlog.c and xlogrecovery.c.

This moves the functions related to performing WAL recovery into the new
xlogrecovery.c source file, leaving xlog.c responsible for maintaining
the WAL buffers, coordinating the startup and switch from recovery to
normal operations, and other miscellaneous stuff that have always been in
xlog.c.

Reviewed-by: Andres Freund, Kyotaro Horiguchi, Robert Haas
Discussion: https://www.postgresql.org/message-id/a31f27b4-a31d-f976-6217-2b03be646ffa%40iki.fi
This commit is contained in:
Heikki Linnakangas 2022-02-16 09:30:38 +02:00
parent be1c00ab13
commit 70e81861fa
22 changed files with 4912 additions and 4379 deletions

View File

@ -38,6 +38,7 @@
#include "postmaster/interrupt.h"
#include "storage/buf_internals.h"
#include "storage/dsm.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"

View File

@ -32,6 +32,7 @@ OBJS = \
xlogfuncs.o \
xloginsert.o \
xlogreader.o \
xlogrecovery.o \
xlogutils.o
include $(top_srcdir)/src/backend/common.mk

View File

@ -29,6 +29,7 @@
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "catalog/index.h"
#include "catalog/namespace.h"

File diff suppressed because it is too large Load Diff

View File

@ -19,8 +19,8 @@
#include <unistd.h>
#include "access/htup_details.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "catalog/pg_type.h"
#include "funcapi.h"

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@
#include <unistd.h>
#include "access/timeline.h"
#include "access/xlog.h"
#include "access/xlogrecovery.h"
#include "access/xlog_internal.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
@ -46,8 +46,8 @@ bool ignore_invalid_pages = false;
* process you're running in, use RecoveryInProgress() but only after shared
* memory startup and lock initialization.
*
* This is updated from xlog.c, but lives here because it's mostly read by
* WAL redo functions.
* This is updated from xlog.c and xlogrecovery.c, but lives here because
* it's mostly read by WAL redo functions.
*/
bool InRecovery = false;

View File

@ -38,6 +38,7 @@
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"

View File

@ -95,6 +95,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xlogrecovery.h"
#include "catalog/pg_control.h"
#include "common/file_perm.h"
#include "common/ip.h"

View File

@ -20,6 +20,7 @@
#include "postgres.h"
#include "access/xlog.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"

View File

@ -19,6 +19,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "catalog/pg_type.h"
#include "fmgr.h"

View File

@ -14,6 +14,7 @@
#include "access/htup_details.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "funcapi.h"
#include "miscadmin.h"

View File

@ -56,6 +56,7 @@
#include "access/transam.h"
#include "access/xlog_internal.h"
#include "access/xlogarchive.h"
#include "access/xlogrecovery.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_type.h"
#include "common/ip.h"

View File

@ -23,6 +23,7 @@
#include <signal.h>
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "pgstat.h"
#include "postmaster/startup.h"
#include "replication/walreceiver.h"

View File

@ -55,6 +55,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "access/xlogreader.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_type.h"

View File

@ -22,6 +22,7 @@
#include "access/subtrans.h"
#include "access/syncscan.h"
#include "access/twophase.h"
#include "access/xlogrecovery.h"
#include "commands/async.h"
#include "miscadmin.h"
#include "pgstat.h"
@ -119,6 +120,7 @@ CalculateShmemSize(int *num_semaphores)
size = add_size(size, PredicateLockShmemSize());
size = add_size(size, ProcGlobalShmemSize());
size = add_size(size, XLOGShmemSize());
size = add_size(size, XLogRecoveryShmemSize());
size = add_size(size, CLOGShmemSize());
size = add_size(size, CommitTsShmemSize());
size = add_size(size, SUBTRANSShmemSize());
@ -241,6 +243,7 @@ CreateSharedMemoryAndSemaphores(void)
* Set up xlog, clog, and buffers
*/
XLOGShmemInit();
XLogRecoveryShmemInit();
CLOGShmemInit();
CommitTsShmemInit();
SUBTRANSShmemInit();

View File

@ -20,6 +20,7 @@
#include "access/twophase.h"
#include "access/xact.h"
#include "access/xloginsert.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
#include "pgstat.h"

View File

@ -29,6 +29,7 @@
#include "portability/instr_time.h"
#include "postmaster/bgwriter.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/md.h"
#include "utils/hsearch.h"

View File

@ -41,6 +41,7 @@
#include "access/twophase.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "catalog/namespace.h"
#include "catalog/pg_authid.h"
#include "catalog/storage.h"

View File

@ -11,13 +11,11 @@
#ifndef XLOG_H
#define XLOG_H
#include "access/rmgr.h"
#include "access/xlogdefs.h"
#include "access/xlogreader.h"
#include "datatype/timestamp.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "storage/fd.h"
/* Sync methods */
@ -28,36 +26,10 @@
#define SYNC_METHOD_OPEN_DSYNC 4 /* for O_DSYNC */
extern int sync_method;
/*
* Recovery target type.
* Only set during a Point in Time recovery, not when in standby mode.
*/
typedef enum
{
RECOVERY_TARGET_UNSET,
RECOVERY_TARGET_XID,
RECOVERY_TARGET_TIME,
RECOVERY_TARGET_NAME,
RECOVERY_TARGET_LSN,
RECOVERY_TARGET_IMMEDIATE
} RecoveryTargetType;
/*
* Recovery target TimeLine goal
*/
typedef enum
{
RECOVERY_TARGET_TIMELINE_CONTROLFILE,
RECOVERY_TARGET_TIMELINE_LATEST,
RECOVERY_TARGET_TIMELINE_NUMERIC
} RecoveryTargetTimeLineGoal;
extern XLogRecPtr ProcLastRecPtr;
extern XLogRecPtr XactLastRecEnd;
extern PGDLLIMPORT XLogRecPtr XactLastCommitEnd;
extern bool reachedConsistency;
/* these variables are GUC parameters related to XLOG */
extern int wal_segment_size;
extern int min_wal_size_mb;
@ -77,34 +49,10 @@ extern bool wal_recycle;
extern bool *wal_consistency_checking;
extern char *wal_consistency_checking_string;
extern bool log_checkpoints;
extern char *recoveryRestoreCommand;
extern char *recoveryEndCommand;
extern char *archiveCleanupCommand;
extern bool recoveryTargetInclusive;
extern int recoveryTargetAction;
extern int recovery_min_apply_delay;
extern char *PrimaryConnInfo;
extern char *PrimarySlotName;
extern bool wal_receiver_create_temp_slot;
extern bool track_wal_io_timing;
/* indirectly set via GUC system */
extern TransactionId recoveryTargetXid;
extern char *recovery_target_time_string;
extern const char *recoveryTargetName;
extern XLogRecPtr recoveryTargetLSN;
extern RecoveryTargetType recoveryTarget;
extern char *PromoteTriggerFile;
extern RecoveryTargetTimeLineGoal recoveryTargetTimeLineGoal;
extern TimeLineID recoveryTargetTLIRequested;
extern TimeLineID recoveryTargetTLI;
extern int CheckPointSegments;
/* option set locally in startup process only when signal files exist */
extern bool StandbyModeRequested;
extern bool StandbyMode;
/* Archive modes */
typedef enum ArchiveMode
{
@ -138,14 +86,6 @@ typedef enum RecoveryState
RECOVERY_STATE_DONE /* currently in production */
} RecoveryState;
/* Recovery pause states */
typedef enum RecoveryPauseState
{
RECOVERY_NOT_PAUSED, /* pause not requested */
RECOVERY_PAUSE_REQUESTED, /* pause requested, but not yet paused */
RECOVERY_PAUSED /* recovery is paused */
} RecoveryPauseState;
extern PGDLLIMPORT int wal_level;
/* Is WAL archiving enabled (always or only while server is running normally)? */
@ -274,19 +214,10 @@ extern void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli);
extern bool RecoveryInProgress(void);
extern RecoveryState GetRecoveryState(void);
extern bool HotStandbyActive(void);
extern bool HotStandbyActiveInReplay(void);
extern bool XLogInsertAllowed(void);
extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
extern XLogRecPtr GetXLogInsertRecPtr(void);
extern XLogRecPtr GetXLogWriteRecPtr(void);
extern RecoveryPauseState GetRecoveryPauseState(void);
extern void SetRecoveryPause(bool recoveryPause);
extern TimestampTz GetLatestXTime(void);
extern TimestampTz GetCurrentChunkReplayStartTime(void);
extern void UpdateControlFile(void);
extern uint64 GetSystemIdentifier(void);
extern char *GetMockAuthenticationNonce(void);
extern bool DataChecksumsEnabled(void);
@ -310,19 +241,23 @@ extern XLogRecPtr GetInsertRecPtr(void);
extern XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI);
extern TimeLineID GetWALInsertionTimeLine(void);
extern XLogRecPtr GetLastImportantRecPtr(void);
extern void RemovePromoteSignalFiles(void);
extern bool PromoteIsTriggered(void);
extern bool CheckPromoteSignal(void);
extern void WakeupRecovery(void);
extern void SetWalWriterSleeping(bool sleeping);
extern void StartupRequestWalReceiverRestart(void);
extern void XLogRequestWalReceiverReply(void);
extern void assign_max_wal_size(int newval, void *extra);
extern void assign_checkpoint_completion_target(double newval, void *extra);
/*
* Routines used by xlogrecovery.c to call back into xlog.c during recovery.
*/
extern void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI);
extern bool XLogCheckpointNeeded(XLogSegNo new_segno);
extern void SwitchIntoArchiveRecovery(XLogRecPtr EndRecPtr, TimeLineID replayTLI);
extern void ReachedEndOfBackup(XLogRecPtr EndRecPtr, TimeLineID tli);
extern void SetInstallXLogFileSegmentActive(void);
extern bool IsInstallXLogFileSegmentActive(void);
extern void XLogShutdownWalRcv(void);
/*
* Routines to start, stop, and get status of a base backup.
*/

View File

@ -0,0 +1,157 @@
/*
* xlogrecovery.h
*
* Functions for WAL recovery and standby mode
*
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/include/access/xlogrecovery.h
*/
#ifndef XLOGRECOVERY_H
#define XLOGRECOVERY_H
#include "access/xlogreader.h"
#include "catalog/pg_control.h"
#include "lib/stringinfo.h"
#include "utils/timestamp.h"
/*
* Recovery target type.
* Only set during a Point in Time recovery, not when in standby mode.
*/
typedef enum
{
RECOVERY_TARGET_UNSET,
RECOVERY_TARGET_XID,
RECOVERY_TARGET_TIME,
RECOVERY_TARGET_NAME,
RECOVERY_TARGET_LSN,
RECOVERY_TARGET_IMMEDIATE
} RecoveryTargetType;
/*
* Recovery target TimeLine goal
*/
typedef enum
{
RECOVERY_TARGET_TIMELINE_CONTROLFILE,
RECOVERY_TARGET_TIMELINE_LATEST,
RECOVERY_TARGET_TIMELINE_NUMERIC
} RecoveryTargetTimeLineGoal;
/* Recovery pause states */
typedef enum RecoveryPauseState
{
RECOVERY_NOT_PAUSED, /* pause not requested */
RECOVERY_PAUSE_REQUESTED, /* pause requested, but not yet paused */
RECOVERY_PAUSED /* recovery is paused */
} RecoveryPauseState;
/* User-settable GUC parameters */
extern bool recoveryTargetInclusive;
extern int recoveryTargetAction;
extern int recovery_min_apply_delay;
extern char *PrimaryConnInfo;
extern char *PrimarySlotName;
extern char *recoveryRestoreCommand;
extern char *recoveryEndCommand;
extern char *archiveCleanupCommand;
/* indirectly set via GUC system */
extern TransactionId recoveryTargetXid;
extern char *recovery_target_time_string;
extern TimestampTz recoveryTargetTime;
extern const char *recoveryTargetName;
extern XLogRecPtr recoveryTargetLSN;
extern RecoveryTargetType recoveryTarget;
extern char *PromoteTriggerFile;
extern bool wal_receiver_create_temp_slot;
extern RecoveryTargetTimeLineGoal recoveryTargetTimeLineGoal;
extern TimeLineID recoveryTargetTLIRequested;
extern TimeLineID recoveryTargetTLI;
/* Have we already reached a consistent database state? */
extern bool reachedConsistency;
/* Are we currently in standby mode? */
extern bool StandbyMode;
extern Size XLogRecoveryShmemSize(void);
extern void XLogRecoveryShmemInit(void);
extern void InitWalRecovery(ControlFileData *ControlFile, bool *wasShutdownPtr, bool *haveBackupLabel, bool *haveTblspcMap);
extern void PerformWalRecovery(void);
/*
* FinishWalRecovery() returns this. It contains information about the point
* where recovery ended, and why it ended.
*/
typedef struct
{
/*
* Information about the last valid or applied record, after which new WAL
* can be appended. 'lastRec' is the position where the last record
* starts, and 'endOfLog' is its end. 'lastPage' is a copy of the last
* partial page that contains endOfLog (or NULL if endOfLog is exactly at
* page boundary). 'lastPageBeginPtr' is the position where the last page
* begins.
*
* endOfLogTLI is the TLI in the filename of the XLOG segment containing
* the last applied record. It could be different from lastRecTLI, if
* there was a timeline switch in that segment, and we were reading the
* old WAL from a segment belonging to a higher timeline.
*/
XLogRecPtr lastRec; /* start of last valid or applied record */
TimeLineID lastRecTLI;
XLogRecPtr endOfLog; /* end of last valid or applied record */
TimeLineID endOfLogTLI;
XLogRecPtr lastPageBeginPtr; /* LSN of page that contains endOfLog */
char *lastPage; /* copy of the last page, up to endOfLog */
/*
* abortedRecPtr is the start pointer of a broken record at end of WAL
* when recovery completes; missingContrecPtr is the location of the first
* contrecord that went missing. See CreateOverwriteContrecordRecord for
* details.
*/
XLogRecPtr abortedRecPtr;
XLogRecPtr missingContrecPtr;
/* short human-readable string describing why recovery ended */
char *recoveryStopReason;
/*
* If standby or recovery signal file was found, these flags are set
* accordingly.
*/
bool standby_signal_file_found;
bool recovery_signal_file_found;
} EndOfWalRecoveryInfo;
extern EndOfWalRecoveryInfo *FinishWalRecovery(void);
extern void ShutdownWalRecovery(void);
extern void RemovePromoteSignalFiles(void);
extern bool HotStandbyActive(void);
extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
extern RecoveryPauseState GetRecoveryPauseState(void);
extern void SetRecoveryPause(bool recoveryPause);
extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
extern TimestampTz GetLatestXTime(void);
extern TimestampTz GetCurrentChunkReplayStartTime(void);
extern XLogRecPtr GetCurrentReplayRecPtr(TimeLineID *replayEndTLI);
extern bool PromoteIsTriggered(void);
extern bool CheckPromoteSignal(void);
extern void WakeupRecovery(void);
extern void StartupRequestWalReceiverRestart(void);
extern void XLogRequestWalReceiverReply(void);
extern void RecoveryRequiresIntParameter(const char *param_name, int currValue, int minValue);
extern void xlog_outdesc(StringInfo buf, XLogReaderState *record);
#endif /* XLOGRECOVERY_H */

View File

@ -607,6 +607,7 @@ EndDirectModify_function
EndForeignInsert_function
EndForeignModify_function
EndForeignScan_function
EndOfWalRecoveryInfo
EndSampleScan_function
EnumItem
EolType
@ -2945,6 +2946,7 @@ XLogRecordBlockCompressHeader
XLogRecordBlockHeader
XLogRecordBlockImageHeader
XLogRecordBuffer
XLogRecoveryCtlData
XLogRedoAction
XLogSegNo
XLogSource