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