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 #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 {
88 
91 
92 extern bool reachedConsistency;
93 
94 /* these variables are GUC parameters related to XLOG */
95 extern int min_wal_size;
96 extern int max_wal_size;
97 extern int wal_keep_segments;
98 extern int XLOGbuffers;
99 extern int XLogArchiveTimeout;
100 extern int wal_retrieve_retry_interval;
101 extern char *XLogArchiveCommand;
102 extern bool EnableHotStandby;
103 extern bool fullPageWrites;
104 extern bool wal_log_hints;
105 extern bool wal_compression;
106 extern bool log_checkpoints;
107 
108 extern int CheckPointSegments;
109 
110 /* Archive modes */
111 typedef enum ArchiveMode
112 {
113  ARCHIVE_MODE_OFF = 0, /* disabled */
114  ARCHIVE_MODE_ON, /* enabled while server is running normally */
115  ARCHIVE_MODE_ALWAYS /* enabled always (even during recovery) */
116 } ArchiveMode;
117 extern int XLogArchiveMode;
118 
119 /* WAL levels */
120 typedef enum WalLevel
121 {
126 } WalLevel;
127 extern int wal_level;
128 
129 #define XLogArchivingActive() \
130  (XLogArchiveMode > ARCHIVE_MODE_OFF && wal_level >= WAL_LEVEL_ARCHIVE)
131 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
132 
133 /*
134  * Is WAL-logging necessary for archival or log-shipping, or can we skip
135  * WAL-logging if we fsync() the data before committing instead?
136  */
137 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_ARCHIVE)
138 
139 /*
140  * Is a full-page image needed for hint bit updates?
141  *
142  * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
143  * we have to protect them against torn page writes. When you only set
144  * individual bits on a page, it's still consistent no matter what combination
145  * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
146  * them if forced by wal_log_hints=on.
147  */
148 #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
149 
150 /* Do we need to WAL-log information required only for Hot Standby and logical replication? */
151 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_HOT_STANDBY)
152 
153 /* Do we need to WAL-log information required only for logical replication? */
154 #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)
155 
156 #ifdef WAL_DEBUG
157 extern bool XLOG_DEBUG;
158 #endif
159 
160 /*
161  * OR-able request flag bits for checkpoints. The "cause" bits are used only
162  * for logging purposes. Note: the flags must be defined so that it's
163  * sensible to OR together request flags arising from different requestors.
164  */
165 
166 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
167 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
168 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint,
169  * but issued at end of WAL
170  * recovery */
171 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
172 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
173 #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those
174  * belonging to unlogged tables */
175 /* These are important to RequestCheckpoint */
176 #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
177 /* These indicate the cause of a checkpoint request */
178 #define CHECKPOINT_CAUSE_XLOG 0x0040 /* XLOG consumption */
179 #define CHECKPOINT_CAUSE_TIME 0x0080 /* Elapsed time */
180 
181 /* Checkpoint statistics */
182 typedef struct CheckpointStatsData
183 {
184  TimestampTz ckpt_start_t; /* start of checkpoint */
185  TimestampTz ckpt_write_t; /* start of flushing buffers */
186  TimestampTz ckpt_sync_t; /* start of fsyncs */
187  TimestampTz ckpt_sync_end_t; /* end of fsyncs */
188  TimestampTz ckpt_end_t; /* end of checkpoint */
189 
190  int ckpt_bufs_written; /* # of buffers written */
191 
192  int ckpt_segs_added; /* # of new xlog segments created */
193  int ckpt_segs_removed; /* # of xlog segments deleted */
194  int ckpt_segs_recycled; /* # of xlog segments recycled */
195 
196  int ckpt_sync_rels; /* # of relations synced */
197  uint64 ckpt_longest_sync; /* Longest sync for one relation */
198  uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
199  * times, which is not necessarily the
200  * same as the total elapsed time for
201  * the entire sync phase. */
203 
205 
206 struct XLogRecData;
207 
208 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn);
209 extern void XLogFlush(XLogRecPtr RecPtr);
210 extern bool XLogBackgroundFlush(void);
211 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
212 extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock);
213 extern int XLogFileOpen(XLogSegNo segno);
214 
215 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
217 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
219 
220 extern void xlog_redo(XLogReaderState *record);
221 extern void xlog_desc(StringInfo buf, XLogReaderState *record);
222 extern const char *xlog_identify(uint8 info);
223 
224 extern void issue_xlog_fsync(int fd, XLogSegNo segno);
225 
226 extern bool RecoveryInProgress(void);
227 extern bool HotStandbyActive(void);
228 extern bool HotStandbyActiveInReplay(void);
229 extern bool XLogInsertAllowed(void);
230 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
231 extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
232 extern XLogRecPtr GetXLogInsertRecPtr(void);
233 extern XLogRecPtr GetXLogWriteRecPtr(void);
234 extern bool RecoveryIsPaused(void);
235 extern void SetRecoveryPause(bool recoveryPause);
236 extern TimestampTz GetLatestXTime(void);
238 extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
239 
240 extern void UpdateControlFile(void);
241 extern uint64 GetSystemIdentifier(void);
242 extern bool DataChecksumsEnabled(void);
244 extern Size XLOGShmemSize(void);
245 extern void XLOGShmemInit(void);
246 extern void BootStrapXLOG(void);
247 extern void StartupXLOG(void);
248 extern void ShutdownXLOG(int code, Datum arg);
249 extern void InitXLOGAccess(void);
250 extern void CreateCheckPoint(int flags);
251 extern bool CreateRestartPoint(int flags);
252 extern void XLogPutNextOid(Oid nextOid);
253 extern XLogRecPtr XLogRestorePoint(const char *rpName);
254 extern void UpdateFullPageWrites(void);
255 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
256 extern XLogRecPtr GetRedoRecPtr(void);
257 extern XLogRecPtr GetInsertRecPtr(void);
258 extern XLogRecPtr GetFlushRecPtr(void);
259 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
260 
261 extern bool CheckPromoteSignal(void);
262 extern void WakeupRecovery(void);
263 extern void SetWalWriterSleeping(bool sleeping);
264 
265 extern void assign_max_wal_size(int newval, void *extra);
266 extern void assign_checkpoint_completion_target(double newval, void *extra);
267 
268 /*
269  * Starting/stopping a base backup
270  */
271 extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
272  TimeLineID *starttli_p, char **labelfile, DIR *tblspcdir,
273  List **tablespaces, char **tblspcmapfile, bool infotbssize,
274  bool needtblspcmapfile);
275 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
276  TimeLineID *stoptli_p);
277 extern void do_pg_abort_backup(void);
278 
279 /* File path names (all relative to $PGDATA) */
280 #define BACKUP_LABEL_FILE "backup_label"
281 #define BACKUP_LABEL_OLD "backup_label.old"
282 
283 #define TABLESPACE_MAP "tablespace_map"
284 #define TABLESPACE_MAP_OLD "tablespace_map.old"
285 
286 #endif /* XLOG_H */
void CreateCheckPoint(int flags)
Definition: xlog.c:8122
int wal_keep_segments
Definition: xlog.c:86
struct CheckpointStatsData CheckpointStatsData
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:10652
uint64 ckpt_agg_sync_time
Definition: xlog.h:198
TimestampTz ckpt_sync_end_t
Definition: xlog.h:187
bool CreateRestartPoint(int flags)
Definition: xlog.c:8679
int ckpt_sync_rels
Definition: xlog.h:196
bool XLogInsertAllowed(void)
Definition: xlog.c:7604
bool XLogNeedsFlush(XLogRecPtr RecPtr)
Definition: xlog.c:2818
uint32 TimeLineID
Definition: xlogdefs.h:45
XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, char **labelfile, DIR *tblspcdir, List **tablespaces, char **tblspcmapfile, bool infotbssize, bool needtblspcmapfile)
Definition: xlog.c:9778
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:7797
bool DataChecksumsEnabled(void)
Definition: xlog.c:4474
void WakeupRecovery(void)
Definition: xlog.c:11590
uint32 TransactionId
Definition: c.h:374
void XLogFlush(XLogRecPtr RecPtr)
Definition: xlog.c:2545
void xlog_desc(StringInfo buf, XLogReaderState *record)
Definition: xlogdesc.c:35
bool HotStandbyActive(void)
Definition: xlog.c:7565
int XLogFileOpen(XLogSegNo segno)
Definition: xlog.c:3282
TimestampTz ckpt_start_t
Definition: xlog.h:184
XLogRecPtr XactLastRecEnd
Definition: xlog.c:325
bool wal_log_hints
Definition: xlog.c:93
unsigned char uint8
Definition: c.h:239
int ckpt_segs_recycled
Definition: xlog.h:194
bool fullPageWrites
Definition: xlog.c:92
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3553
void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch)
Definition: xlog.c:7892
unsigned int Oid
Definition: postgres_ext.h:31
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:5632
bool CheckPromoteSignal(void)
Definition: xlog.c:11574
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:10617
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void UpdateFullPageWrites(void)
Definition: xlog.c:9098
bool XLogBackgroundFlush(void)
Definition: xlog.c:2727
#define PGDLLIMPORT
Definition: c.h:1052
void xlog_redo(XLogReaderState *record)
Definition: xlog.c:9202
char * XLogFileNameP(TimeLineID tli, XLogSegNo segno)
Definition: xlog.c:9731
int max_wal_size
Definition: xlog.c:84
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2436
void XLogSetAsyncXactLSN(XLogRecPtr record)
Definition: xlog.c:2395
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:3525
double TimestampTz
Definition: timestamp.h:51
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:7840
bool reachedConsistency
Definition: xlog.c:772
Definition: dirent.c:25
bool wal_compression
Definition: xlog.c:94
void assign_checkpoint_completion_target(double newval, void *extra)
Definition: xlog.c:2031
void UpdateControlFile(void)
Definition: xlog.c:4419
int XLogArchiveTimeout
Definition: xlog.c:88
int XLogArchiveMode
Definition: xlog.c:89
const char * xlog_identify(uint8 info)
Definition: xlogdesc.c:142
void InitXLOGAccess(void)
Definition: xlog.c:7774
HotStandbyState standbyState
Definition: xlog.c:188
bool EnableHotStandby
Definition: xlog.c:91
static char * buf
Definition: pg_test_fsync.c:65
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:10636
int XLOGbuffers
Definition: xlog.c:87
uint64 XLogSegNo
Definition: xlogdefs.h:34
unsigned int uint32
Definition: c.h:241
RecoveryTargetType
Definition: xlog.h:80
XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn)
Definition: xlog.c:895
int ckpt_segs_removed
Definition: xlog.h:193
bool log_checkpoints
Definition: xlog.c:95
int min_wal_size
Definition: xlog.c:85
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:7856
int ckpt_bufs_written
Definition: xlog.h:190
void StartupXLOG(void)
Definition: xlog.c:5886
bool HotStandbyActiveInReplay(void)
Definition: xlog.c:7590
int CheckPointSegments
Definition: xlog.c:117
uintptr_t Datum
Definition: postgres.h:374
void issue_xlog_fsync(int fd, XLogSegNo segno)
Definition: xlog.c:9688
bool InRecovery
Definition: xlog.c:185
TimestampTz GetCurrentChunkReplayStartTime(void)
Definition: xlog.c:5776
int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock)
Definition: xlog.c:2894
int sync_method
Definition: xlog.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
Definition: xlog.c:10241
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
Definition: xlog.c:326
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:11599
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:7922
XLogRecPtr GetFakeLSNForUnloggedRel(void)
Definition: xlog.c:4490
void do_pg_abort_backup(void)
Definition: xlog.c:10597
void assign_max_wal_size(int newval, void *extra)
Definition: xlog.c:2024
ArchiveMode
Definition: xlog.h:111
size_t Size
Definition: c.h:333
TimestampTz GetLatestXTime(void)
Definition: xlog.c:5746
TimestampTz ckpt_write_t
Definition: xlog.h:185
#define newval
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:9017
Size XLOGShmemSize(void)
Definition: xlog.c:4565
bool RecoveryInProgress(void)
Definition: xlog.c:7509
TimestampTz ckpt_sync_t
Definition: xlog.h:186
int ckpt_segs_added
Definition: xlog.h:192
void XLOGShmemInit(void)
Definition: xlog.c:4606
bool RecoveryIsPaused(void)
Definition: xlog.c:5620
int wal_level
Definition: xlog.c:97
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
void * arg
CheckpointStatsData CheckpointStats
Definition: xlog.c:166
PGDLLIMPORT TimeLineID ThisTimeLineID
Definition: xlog.c:172
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:7825
int wal_retrieve_retry_interval
Definition: xlog.c:100
void BootStrapXLOG(void)
Definition: xlog.c:4718
void XLogPutNextOid(Oid nextOid)
Definition: xlog.c:8965
Definition: pg_list.h:45
void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream)
Definition: xlog.c:5792
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4464
TimestampTz ckpt_end_t
Definition: xlog.h:188
HotStandbyState
Definition: xlog.h:64
WalLevel
Definition: xlog.h:120
uint64 ckpt_longest_sync
Definition: xlog.h:197
char * XLogArchiveCommand
Definition: xlog.c:90