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-2015, 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 
21 
22 /* Sync methods */
23 #define SYNC_METHOD_FSYNC 0
24 #define SYNC_METHOD_FDATASYNC 1
25 #define SYNC_METHOD_OPEN 2 /* for O_SYNC */
26 #define SYNC_METHOD_FSYNC_WRITETHROUGH 3
27 #define SYNC_METHOD_OPEN_DSYNC 4 /* for O_DSYNC */
28 extern int sync_method;
29 
30 extern PGDLLIMPORT TimeLineID ThisTimeLineID; /* current TLI */
31 
32 /*
33  * Prior to 8.4, all activity during recovery was carried out by the startup
34  * process. This local variable continues to be used in many parts of the
35  * code to indicate actions taken by RecoveryManagers. Other processes that
36  * potentially perform work during recovery should check RecoveryInProgress().
37  * See XLogCtl notes in xlog.c.
38  */
39 extern bool InRecovery;
40 
41 /*
42  * Like InRecovery, standbyState is only valid in the startup process.
43  * In all other processes it will have the value STANDBY_DISABLED (so
44  * InHotStandby will read as FALSE).
45  *
46  * In DISABLED state, we're performing crash recovery or hot standby was
47  * disabled in postgresql.conf.
48  *
49  * In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
50  * we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
51  * to initialize our master-transaction tracking system.
52  *
53  * When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
54  * state. The tracked information might still be incomplete, so we can't allow
55  * connections yet, but redo functions must update the in-memory state when
56  * appropriate.
57  *
58  * In SNAPSHOT_READY mode, we have full knowledge of transactions that are
59  * (or were) running in the master at the current WAL location. Snapshots
60  * can be taken, and read-only queries can be run.
61  */
62 typedef enum
63 {
69 
71 
72 #define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
73 
74 /*
75  * Recovery target type.
76  * Only set during a Point in Time recovery, not when standby_mode = on
77  */
78 typedef enum
79 {
86 
88 
89 extern bool reachedConsistency;
90 
91 /* these variables are GUC parameters related to XLOG */
92 extern int min_wal_size;
93 extern int max_wal_size;
94 extern int wal_keep_segments;
95 extern int XLOGbuffers;
96 extern int XLogArchiveTimeout;
98 extern bool XLogArchiveMode;
99 extern char *XLogArchiveCommand;
100 extern bool EnableHotStandby;
101 extern bool fullPageWrites;
102 extern bool wal_log_hints;
103 extern bool log_checkpoints;
104 
105 extern int CheckPointSegments;
106 
107 /* WAL levels */
108 typedef enum WalLevel
109 {
114 } WalLevel;
115 extern int wal_level;
116 
117 #define XLogArchivingActive() (XLogArchiveMode && wal_level >= WAL_LEVEL_ARCHIVE)
118 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
119 
120 /*
121  * Is WAL-logging necessary for archival or log-shipping, or can we skip
122  * WAL-logging if we fsync() the data before committing instead?
123  */
124 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_ARCHIVE)
125 
126 /*
127  * Is a full-page image needed for hint bit updates?
128  *
129  * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
130  * we have to protect them against torn page writes. When you only set
131  * individual bits on a page, it's still consistent no matter what combination
132  * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
133  * them if forced by wal_log_hints=on.
134  */
135 #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
136 
137 /* Do we need to WAL-log information required only for Hot Standby and logical replication? */
138 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_HOT_STANDBY)
139 
140 /* Do we need to WAL-log information required only for logical replication? */
141 #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)
142 
143 #ifdef WAL_DEBUG
144 extern bool XLOG_DEBUG;
145 #endif
146 
147 /*
148  * OR-able request flag bits for checkpoints. The "cause" bits are used only
149  * for logging purposes. Note: the flags must be defined so that it's
150  * sensible to OR together request flags arising from different requestors.
151  */
152 
153 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
154 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
155 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint,
156  * but issued at end of WAL
157  * recovery */
158 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
159 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
160 #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those
161  * belonging to unlogged tables */
162 /* These are important to RequestCheckpoint */
163 #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
164 /* These indicate the cause of a checkpoint request */
165 #define CHECKPOINT_CAUSE_XLOG 0x0040 /* XLOG consumption */
166 #define CHECKPOINT_CAUSE_TIME 0x0080 /* Elapsed time */
167 
168 /* Checkpoint statistics */
169 typedef struct CheckpointStatsData
170 {
171  TimestampTz ckpt_start_t; /* start of checkpoint */
172  TimestampTz ckpt_write_t; /* start of flushing buffers */
173  TimestampTz ckpt_sync_t; /* start of fsyncs */
174  TimestampTz ckpt_sync_end_t; /* end of fsyncs */
175  TimestampTz ckpt_end_t; /* end of checkpoint */
177  int ckpt_bufs_written; /* # of buffers written */
179  int ckpt_segs_added; /* # of new xlog segments created */
180  int ckpt_segs_removed; /* # of xlog segments deleted */
181  int ckpt_segs_recycled; /* # of xlog segments recycled */
183  int ckpt_sync_rels; /* # of relations synced */
184  uint64 ckpt_longest_sync; /* Longest sync for one relation */
185  uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
186  * times, which is not necessarily the
187  * same as the total elapsed time for
188  * the entire sync phase. */
190 
192 
193 struct XLogRecData;
194 
195 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn);
196 extern void XLogFlush(XLogRecPtr RecPtr);
197 extern bool XLogBackgroundFlush(void);
198 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
199 extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock);
200 extern int XLogFileOpen(XLogSegNo segno);
201 
202 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
204 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
206 
207 extern void xlog_redo(XLogReaderState *record);
208 extern void xlog_desc(StringInfo buf, XLogReaderState *record);
209 extern const char *xlog_identify(uint8 info);
210 
211 extern void issue_xlog_fsync(int fd, XLogSegNo segno);
212 
213 extern bool RecoveryInProgress(void);
214 extern bool HotStandbyActive(void);
215 extern bool HotStandbyActiveInReplay(void);
216 extern bool XLogInsertAllowed(void);
217 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
218 extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
219 extern XLogRecPtr GetXLogInsertRecPtr(void);
220 extern XLogRecPtr GetXLogWriteRecPtr(void);
221 extern bool RecoveryIsPaused(void);
222 extern void SetRecoveryPause(bool recoveryPause);
223 extern TimestampTz GetLatestXTime(void);
225 extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
226 
227 extern void UpdateControlFile(void);
228 extern uint64 GetSystemIdentifier(void);
229 extern bool DataChecksumsEnabled(void);
231 extern Size XLOGShmemSize(void);
232 extern void XLOGShmemInit(void);
233 extern void BootStrapXLOG(void);
234 extern void StartupXLOG(void);
235 extern void ShutdownXLOG(int code, Datum arg);
236 extern void InitXLOGAccess(void);
237 extern void CreateCheckPoint(int flags);
238 extern bool CreateRestartPoint(int flags);
239 extern void XLogPutNextOid(Oid nextOid);
240 extern XLogRecPtr XLogRestorePoint(const char *rpName);
241 extern void UpdateFullPageWrites(void);
242 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
243 extern XLogRecPtr GetRedoRecPtr(void);
244 extern XLogRecPtr GetInsertRecPtr(void);
245 extern XLogRecPtr GetFlushRecPtr(void);
246 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
247 
248 extern bool CheckPromoteSignal(void);
249 extern void WakeupRecovery(void);
250 extern void SetWalWriterSleeping(bool sleeping);
251 
252 extern void assign_max_wal_size(int newval, void *extra);
253 extern void assign_checkpoint_completion_target(double newval, void *extra);
254 
255 /*
256  * Starting/stopping a base backup
257  */
258 extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
259  TimeLineID *starttli_p, char **labelfile);
260 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
261  TimeLineID *stoptli_p);
262 extern void do_pg_abort_backup(void);
264 /* File path names (all relative to $PGDATA) */
265 #define BACKUP_LABEL_FILE "backup_label"
266 #define BACKUP_LABEL_OLD "backup_label.old"
267 
268 #endif /* XLOG_H */