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-2014, 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 CheckPointSegments;
93 extern int wal_keep_segments;
94 extern int XLOGbuffers;
95 extern int XLogArchiveTimeout;
96 extern bool XLogArchiveMode;
97 extern char *XLogArchiveCommand;
98 extern bool EnableHotStandby;
99 extern bool fullPageWrites;
100 extern bool wal_log_hints;
101 extern bool log_checkpoints;
102 
103 /* WAL levels */
104 typedef enum WalLevel
105 {
110 } WalLevel;
111 extern int wal_level;
112 
113 #define XLogArchivingActive() (XLogArchiveMode && wal_level >= WAL_LEVEL_ARCHIVE)
114 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
115 
116 /*
117  * Is WAL-logging necessary for archival or log-shipping, or can we skip
118  * WAL-logging if we fsync() the data before committing instead?
119  */
120 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_ARCHIVE)
121 
122 /*
123  * Is a full-page image needed for hint bit updates?
124  *
125  * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
126  * we have to protect them against torn page writes. When you only set
127  * individual bits on a page, it's still consistent no matter what combination
128  * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
129  * them if forced by wal_log_hints=on.
130  */
131 #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
132 
133 /* Do we need to WAL-log information required only for Hot Standby and logical replication? */
134 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_HOT_STANDBY)
135 
136 /* Do we need to WAL-log information required only for logical replication? */
137 #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)
138 
139 #ifdef WAL_DEBUG
140 extern bool XLOG_DEBUG;
141 #endif
142 
143 /*
144  * OR-able request flag bits for checkpoints. The "cause" bits are used only
145  * for logging purposes. Note: the flags must be defined so that it's
146  * sensible to OR together request flags arising from different requestors.
147  */
148 
149 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
150 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
151 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint,
152  * but issued at end of WAL
153  * recovery */
154 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
155 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
156 #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those
157  * belonging to unlogged tables */
158 /* These are important to RequestCheckpoint */
159 #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
160 /* These indicate the cause of a checkpoint request */
161 #define CHECKPOINT_CAUSE_XLOG 0x0040 /* XLOG consumption */
162 #define CHECKPOINT_CAUSE_TIME 0x0080 /* Elapsed time */
163 
164 /* Checkpoint statistics */
165 typedef struct CheckpointStatsData
166 {
167  TimestampTz ckpt_start_t; /* start of checkpoint */
168  TimestampTz ckpt_write_t; /* start of flushing buffers */
169  TimestampTz ckpt_sync_t; /* start of fsyncs */
170  TimestampTz ckpt_sync_end_t; /* end of fsyncs */
171  TimestampTz ckpt_end_t; /* end of checkpoint */
173  int ckpt_bufs_written; /* # of buffers written */
175  int ckpt_segs_added; /* # of new xlog segments created */
176  int ckpt_segs_removed; /* # of xlog segments deleted */
177  int ckpt_segs_recycled; /* # of xlog segments recycled */
179  int ckpt_sync_rels; /* # of relations synced */
180  uint64 ckpt_longest_sync; /* Longest sync for one relation */
181  uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
182  * times, which is not necessarily the
183  * same as the total elapsed time for
184  * the entire sync phase. */
186 
188 
189 struct XLogRecData;
190 
191 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn);
192 extern void XLogFlush(XLogRecPtr RecPtr);
193 extern bool XLogBackgroundFlush(void);
194 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
195 extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock);
196 extern int XLogFileOpen(XLogSegNo segno);
197 
198 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
200 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
202 
203 extern void xlog_redo(XLogReaderState *record);
204 extern void xlog_desc(StringInfo buf, XLogReaderState *record);
205 extern const char *xlog_identify(uint8 info);
206 
207 extern void issue_xlog_fsync(int fd, XLogSegNo segno);
208 
209 extern bool RecoveryInProgress(void);
210 extern bool HotStandbyActive(void);
211 extern bool HotStandbyActiveInReplay(void);
212 extern bool XLogInsertAllowed(void);
213 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
214 extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
215 extern XLogRecPtr GetXLogInsertRecPtr(void);
216 extern XLogRecPtr GetXLogWriteRecPtr(void);
217 extern bool RecoveryIsPaused(void);
218 extern void SetRecoveryPause(bool recoveryPause);
219 extern TimestampTz GetLatestXTime(void);
221 extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
222 
223 extern void UpdateControlFile(void);
224 extern uint64 GetSystemIdentifier(void);
225 extern bool DataChecksumsEnabled(void);
227 extern Size XLOGShmemSize(void);
228 extern void XLOGShmemInit(void);
229 extern void BootStrapXLOG(void);
230 extern void StartupXLOG(void);
231 extern void ShutdownXLOG(int code, Datum arg);
232 extern void InitXLOGAccess(void);
233 extern void CreateCheckPoint(int flags);
234 extern bool CreateRestartPoint(int flags);
235 extern void XLogPutNextOid(Oid nextOid);
236 extern XLogRecPtr XLogRestorePoint(const char *rpName);
237 extern void UpdateFullPageWrites(void);
238 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
239 extern XLogRecPtr GetRedoRecPtr(void);
240 extern XLogRecPtr GetInsertRecPtr(void);
241 extern XLogRecPtr GetFlushRecPtr(void);
242 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
243 
244 extern bool CheckPromoteSignal(void);
245 extern void WakeupRecovery(void);
246 extern void SetWalWriterSleeping(bool sleeping);
247 
248 /*
249  * Starting/stopping a base backup
250  */
251 extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
252  TimeLineID *starttli_p, char **labelfile);
253 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
254  TimeLineID *stoptli_p);
255 extern void do_pg_abort_backup(void);
257 /* File path names (all relative to $PGDATA) */
258 #define BACKUP_LABEL_FILE "backup_label"
259 #define BACKUP_LABEL_OLD "backup_label.old"
260 
261 #endif /* XLOG_H */