PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xlog.h
Go to the documentation of this file.
1 /*
2  * xlog.h
3  *
4  * PostgreSQL transaction log manager
5  *
6  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/access/xlog.h
10  */
11 #ifndef XLOG_H
12 #define XLOG_H
13 
14 #include "access/rmgr.h"
15 #include "access/xlogdefs.h"
16 #include "access/xloginsert.h"
17 #include "access/xlogreader.h"
18 #include "datatype/timestamp.h"
19 #include "lib/stringinfo.h"
20 #include "nodes/pg_list.h"
21 #include "storage/fd.h"
22 
23 
24 /* Sync methods */
25 #define SYNC_METHOD_FSYNC 0
26 #define SYNC_METHOD_FDATASYNC 1
27 #define SYNC_METHOD_OPEN 2 /* for O_SYNC */
28 #define SYNC_METHOD_FSYNC_WRITETHROUGH 3
29 #define SYNC_METHOD_OPEN_DSYNC 4 /* for O_DSYNC */
30 extern int sync_method;
31 
32 extern PGDLLIMPORT TimeLineID ThisTimeLineID; /* current TLI */
33 
34 /*
35  * Prior to 8.4, all activity during recovery was carried out by the startup
36  * process. This local variable continues to be used in many parts of the
37  * code to indicate actions taken by RecoveryManagers. Other processes that
38  * potentially perform work during recovery should check RecoveryInProgress().
39  * See XLogCtl notes in xlog.c.
40  */
41 extern bool InRecovery;
42 
43 /*
44  * Like InRecovery, standbyState is only valid in the startup process.
45  * In all other processes it will have the value STANDBY_DISABLED (so
46  * InHotStandby will read as FALSE).
47  *
48  * In DISABLED state, we're performing crash recovery or hot standby was
49  * disabled in postgresql.conf.
50  *
51  * In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
52  * we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
53  * to initialize our master-transaction tracking system.
54  *
55  * When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
56  * state. The tracked information might still be incomplete, so we can't allow
57  * connections yet, but redo functions must update the in-memory state when
58  * appropriate.
59  *
60  * In SNAPSHOT_READY mode, we have full knowledge of transactions that are
61  * (or were) running in the master at the current WAL location. Snapshots
62  * can be taken, and read-only queries can be run.
63  */
64 typedef enum
65 {
71 
73 
74 #define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
75 
76 /*
77  * Recovery target type.
78  * Only set during a Point in Time recovery, not when standby_mode = on
79  */
80 typedef enum
81 {
89 
93 
94 extern bool reachedConsistency;
95 
96 /* these variables are GUC parameters related to XLOG */
97 extern int min_wal_size;
98 extern int max_wal_size;
99 extern int wal_keep_segments;
100 extern int XLOGbuffers;
101 extern int XLogArchiveTimeout;
102 extern int wal_retrieve_retry_interval;
103 extern char *XLogArchiveCommand;
104 extern bool EnableHotStandby;
105 extern bool fullPageWrites;
106 extern bool wal_log_hints;
107 extern bool wal_compression;
108 extern bool log_checkpoints;
109 
110 extern int CheckPointSegments;
111 
112 /* Archive modes */
113 typedef enum ArchiveMode
114 {
115  ARCHIVE_MODE_OFF = 0, /* disabled */
116  ARCHIVE_MODE_ON, /* enabled while server is running normally */
117  ARCHIVE_MODE_ALWAYS /* enabled always (even during recovery) */
118 } ArchiveMode;
119 extern int XLogArchiveMode;
120 
121 /* WAL levels */
122 typedef enum WalLevel
123 {
127 } WalLevel;
128 
129 extern PGDLLIMPORT int wal_level;
130 
131 /* Is WAL archiving enabled (always or only while server is running normally)? */
132 #define XLogArchivingActive() \
133  (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)
134 /* Is WAL archiving enabled always (even during recovery)? */
135 #define XLogArchivingAlways() \
136  (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
137 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
138 
139 /*
140  * Is WAL-logging necessary for archival or log-shipping, or can we skip
141  * WAL-logging if we fsync() the data before committing instead?
142  */
143 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA)
144 
145 /*
146  * Is a full-page image needed for hint bit updates?
147  *
148  * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
149  * we have to protect them against torn page writes. When you only set
150  * individual bits on a page, it's still consistent no matter what combination
151  * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
152  * them if forced by wal_log_hints=on.
153  */
154 #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
155 
156 /* Do we need to WAL-log information required only for Hot Standby and logical replication? */
157 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA)
158 
159 /* Do we need to WAL-log information required only for logical replication? */
160 #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)
161 
162 #ifdef WAL_DEBUG
163 extern bool XLOG_DEBUG;
164 #endif
165 
166 /*
167  * OR-able request flag bits for checkpoints. The "cause" bits are used only
168  * for logging purposes. Note: the flags must be defined so that it's
169  * sensible to OR together request flags arising from different requestors.
170  */
171 
172 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
173 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
174 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint,
175  * but issued at end of WAL
176  * recovery */
177 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
178 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
179 #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those
180  * belonging to unlogged tables */
181 /* These are important to RequestCheckpoint */
182 #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
183 /* These indicate the cause of a checkpoint request */
184 #define CHECKPOINT_CAUSE_XLOG 0x0040 /* XLOG consumption */
185 #define CHECKPOINT_CAUSE_TIME 0x0080 /* Elapsed time */
186 
187 /* Checkpoint statistics */
188 typedef struct CheckpointStatsData
189 {
190  TimestampTz ckpt_start_t; /* start of checkpoint */
191  TimestampTz ckpt_write_t; /* start of flushing buffers */
192  TimestampTz ckpt_sync_t; /* start of fsyncs */
193  TimestampTz ckpt_sync_end_t; /* end of fsyncs */
194  TimestampTz ckpt_end_t; /* end of checkpoint */
195 
196  int ckpt_bufs_written; /* # of buffers written */
197 
198  int ckpt_segs_added; /* # of new xlog segments created */
199  int ckpt_segs_removed; /* # of xlog segments deleted */
200  int ckpt_segs_recycled; /* # of xlog segments recycled */
201 
202  int ckpt_sync_rels; /* # of relations synced */
203  uint64 ckpt_longest_sync; /* Longest sync for one relation */
204  uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
205  * times, which is not necessarily the
206  * same as the total elapsed time for
207  * the entire sync phase. */
209 
211 
212 struct XLogRecData;
213 
214 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn);
215 extern void XLogFlush(XLogRecPtr RecPtr);
216 extern bool XLogBackgroundFlush(void);
217 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
218 extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock);
219 extern int XLogFileOpen(XLogSegNo segno);
220 
221 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
223 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
225 
226 extern void xlog_redo(XLogReaderState *record);
227 extern void xlog_desc(StringInfo buf, XLogReaderState *record);
228 extern const char *xlog_identify(uint8 info);
229 
230 extern void issue_xlog_fsync(int fd, XLogSegNo segno);
231 
232 extern bool RecoveryInProgress(void);
233 extern bool HotStandbyActive(void);
234 extern bool HotStandbyActiveInReplay(void);
235 extern bool XLogInsertAllowed(void);
236 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
237 extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
238 extern XLogRecPtr GetXLogInsertRecPtr(void);
239 extern XLogRecPtr GetXLogWriteRecPtr(void);
240 extern bool RecoveryIsPaused(void);
241 extern void SetRecoveryPause(bool recoveryPause);
242 extern TimestampTz GetLatestXTime(void);
244 extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
245 
246 extern void UpdateControlFile(void);
247 extern uint64 GetSystemIdentifier(void);
248 extern bool DataChecksumsEnabled(void);
250 extern Size XLOGShmemSize(void);
251 extern void XLOGShmemInit(void);
252 extern void BootStrapXLOG(void);
253 extern void StartupXLOG(void);
254 extern void ShutdownXLOG(int code, Datum arg);
255 extern void InitXLOGAccess(void);
256 extern void CreateCheckPoint(int flags);
257 extern bool CreateRestartPoint(int flags);
258 extern void XLogPutNextOid(Oid nextOid);
259 extern XLogRecPtr XLogRestorePoint(const char *rpName);
260 extern void UpdateFullPageWrites(void);
261 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
262 extern XLogRecPtr GetRedoRecPtr(void);
263 extern XLogRecPtr GetInsertRecPtr(void);
264 extern XLogRecPtr GetFlushRecPtr(void);
265 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
266 extern void RemovePromoteSignalFiles(void);
267 
268 extern bool CheckPromoteSignal(void);
269 extern void WakeupRecovery(void);
270 extern void SetWalWriterSleeping(bool sleeping);
271 
272 extern void XLogRequestWalReceiverReply(void);
273 
274 extern void assign_max_wal_size(int newval, void *extra);
275 extern void assign_checkpoint_completion_target(double newval, void *extra);
276 
277 /*
278  * Starting/stopping a base backup
279  */
280 extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
281  TimeLineID *starttli_p, StringInfo labelfile, DIR *tblspcdir,
282  List **tablespaces, StringInfo tblspcmapfile, bool infotbssize,
283  bool needtblspcmapfile);
284 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
285  TimeLineID *stoptli_p);
286 extern void do_pg_abort_backup(void);
287 
288 /* File path names (all relative to $PGDATA) */
289 #define BACKUP_LABEL_FILE "backup_label"
290 #define BACKUP_LABEL_OLD "backup_label.old"
291 
292 #define TABLESPACE_MAP "tablespace_map"
293 #define TABLESPACE_MAP_OLD "tablespace_map.old"
294 
295 #endif /* XLOG_H */
void CreateCheckPoint(int flags)
Definition: xlog.c:8252
int wal_keep_segments
Definition: xlog.c:89
struct CheckpointStatsData CheckpointStatsData
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:10773
uint64 ckpt_agg_sync_time
Definition: xlog.h:204
TimestampTz ckpt_sync_end_t
Definition: xlog.h:193
bool CreateRestartPoint(int flags)
Definition: xlog.c:8799
int ckpt_sync_rels
Definition: xlog.h:202
bool XLogInsertAllowed(void)
Definition: xlog.c:7738
bool XLogNeedsFlush(XLogRecPtr RecPtr)
Definition: xlog.c:2896
uint32 TimeLineID
Definition: xlogdefs.h:45
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:7933
bool DataChecksumsEnabled(void)
Definition: xlog.c:4523
void WakeupRecovery(void)
Definition: xlog.c:11729
uint32 TransactionId
Definition: c.h:393
void XLogFlush(XLogRecPtr RecPtr)
Definition: xlog.c:2576
void xlog_desc(StringInfo buf, XLogReaderState *record)
Definition: xlogdesc.c:36
bool HotStandbyActive(void)
Definition: xlog.c:7699
int XLogFileOpen(XLogSegNo segno)
Definition: xlog.c:3331
TimestampTz ckpt_start_t
Definition: xlog.h:190
XLogRecPtr XactLastRecEnd
Definition: xlog.c:329
bool wal_log_hints
Definition: xlog.c:96
unsigned char uint8
Definition: c.h:263
int ckpt_segs_recycled
Definition: xlog.h:200
bool fullPageWrites
Definition: xlog.c:95
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3602
void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch)
Definition: xlog.c:8026
unsigned int Oid
Definition: postgres_ext.h:31
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:5740
bool CheckPromoteSignal(void)
Definition: xlog.c:11713
XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, DIR *tblspcdir, List **tablespaces, StringInfo tblspcmapfile, bool infotbssize, bool needtblspcmapfile)
Definition: xlog.c:9892
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:10738
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void UpdateFullPageWrites(void)
Definition: xlog.c:9226
bool XLogBackgroundFlush(void)
Definition: xlog.c:2765
#define PGDLLIMPORT
Definition: c.h:1059
void xlog_redo(XLogReaderState *record)
Definition: xlog.c:9330
char * XLogFileNameP(TimeLineID tli, XLogSegNo segno)
Definition: xlog.c:9845
int max_wal_size
Definition: xlog.c:87
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2467
void XLogSetAsyncXactLSN(XLogRecPtr record)
Definition: xlog.c:2426
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:3574
double TimestampTz
Definition: timestamp.h:51
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:7976
bool reachedConsistency
Definition: xlog.c:779
Definition: dirent.c:25
bool wal_compression
Definition: xlog.c:97
void assign_checkpoint_completion_target(double newval, void *extra)
Definition: xlog.c:2062
void UpdateControlFile(void)
Definition: xlog.c:4468
int XLogArchiveTimeout
Definition: xlog.c:91
int XLogArchiveMode
Definition: xlog.c:92
const char * xlog_identify(uint8 info)
Definition: xlogdesc.c:143
void InitXLOGAccess(void)
Definition: xlog.c:7910
HotStandbyState standbyState
Definition: xlog.c:191
PGDLLIMPORT int wal_level
Definition: xlog.c:100
bool EnableHotStandby
Definition: xlog.c:94
static char * buf
Definition: pg_test_fsync.c:65
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:10757
int XLOGbuffers
Definition: xlog.c:90
uint64 XLogSegNo
Definition: xlogdefs.h:34
unsigned int uint32
Definition: c.h:265
RecoveryTargetType
Definition: xlog.h:80
XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn)
Definition: xlog.c:900
void RemovePromoteSignalFiles(void)
Definition: xlog.c:11702
int ckpt_segs_removed
Definition: xlog.h:199
bool log_checkpoints
Definition: xlog.c:98
int min_wal_size
Definition: xlog.c:88
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:7992
int ckpt_bufs_written
Definition: xlog.h:196
void StartupXLOG(void)
Definition: xlog.c:5987
bool HotStandbyActiveInReplay(void)
Definition: xlog.c:7724
int CheckPointSegments
Definition: xlog.c:120
uintptr_t Datum
Definition: postgres.h:374
void issue_xlog_fsync(int fd, XLogSegNo segno)
Definition: xlog.c:9802
XLogRecPtr ProcLastRecPtr
Definition: xlog.c:328
bool InRecovery
Definition: xlog.c:188
TimestampTz GetCurrentChunkReplayStartTime(void)
Definition: xlog.c:5889
int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock)
Definition: xlog.c:2972
int sync_method
Definition: xlog.c:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
Definition: xlog.c:10353
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
Definition: xlog.c:330
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:11738
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:8056
XLogRecPtr GetFakeLSNForUnloggedRel(void)
Definition: xlog.c:4539
void do_pg_abort_backup(void)
Definition: xlog.c:10718
void assign_max_wal_size(int newval, void *extra)
Definition: xlog.c:2055
ArchiveMode
Definition: xlog.h:113
size_t Size
Definition: c.h:352
TimestampTz GetLatestXTime(void)
Definition: xlog.c:5859
TimestampTz ckpt_write_t
Definition: xlog.h:191
#define newval
void XLogRequestWalReceiverReply(void)
Definition: xlog.c:11749
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:9145
Size XLOGShmemSize(void)
Definition: xlog.c:4614
bool RecoveryInProgress(void)
Definition: xlog.c:7643
TimestampTz ckpt_sync_t
Definition: xlog.h:192
int ckpt_segs_added
Definition: xlog.h:198
void XLOGShmemInit(void)
Definition: xlog.c:4655
bool RecoveryIsPaused(void)
Definition: xlog.c:5728
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
void * arg
CheckpointStatsData CheckpointStats
Definition: xlog.c:169
PGDLLIMPORT TimeLineID ThisTimeLineID
Definition: xlog.c:175
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:7961
int wal_retrieve_retry_interval
Definition: xlog.c:103
void BootStrapXLOG(void)
Definition: xlog.c:4762
void XLogPutNextOid(Oid nextOid)
Definition: xlog.c:9093
Definition: pg_list.h:45
void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream)
Definition: xlog.c:5905
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4513
TimestampTz ckpt_end_t
Definition: xlog.h:194
HotStandbyState
Definition: xlog.h:64
WalLevel
Definition: xlog.h:122
uint64 ckpt_longest_sync
Definition: xlog.h:203
char * XLogArchiveCommand
Definition: xlog.c:93