OSDN Git Service

8f524df75e2392f10ea20b7defb9442fea6a3ec7
[pg-rex/syncrep.git] / src / include / access / xlogdefs.h
1 /*
2  * xlogdefs.h
3  *
4  * Postgres transaction log manager record pointer and
5  * timeline number definitions
6  *
7  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/access/xlogdefs.h,v 1.24 2010/01/02 16:58:01 momjian Exp $
11  */
12 #ifndef XLOG_DEFS_H
13 #define XLOG_DEFS_H
14
15 #include <fcntl.h>                              /* need open() flags */
16
17 /*
18  * Pointer to a location in the XLOG.  These pointers are 64 bits wide,
19  * because we don't want them ever to overflow.
20  *
21  * NOTE: xrecoff == 0 is used to indicate an invalid pointer.  This is OK
22  * because we use page headers in the XLOG, so no XLOG record can start
23  * right at the beginning of a file.
24  *
25  * NOTE: the "log file number" is somewhat misnamed, since the actual files
26  * making up the XLOG are much smaller than 4Gb.  Each actual file is an
27  * XLogSegSize-byte "segment" of a logical log file having the indicated
28  * xlogid.      The log file number and segment number together identify a
29  * physical XLOG file.  Segment number and offset within the physical file
30  * are computed from xrecoff div and mod XLogSegSize.
31  */
32 typedef struct XLogRecPtr
33 {
34         uint32          xlogid;                 /* log file #, 0 based */
35         uint32          xrecoff;                /* byte offset of location in log file */
36 } XLogRecPtr;
37
38 #define XLogRecPtrIsInvalid(r)  ((r).xrecoff == 0)
39
40
41 /*
42  * Macros for comparing XLogRecPtrs
43  *
44  * Beware of passing expressions with side-effects to these macros,
45  * since the arguments may be evaluated multiple times.
46  */
47 #define XLByteLT(a, b)          \
48                         ((a).xlogid < (b).xlogid || \
49                          ((a).xlogid == (b).xlogid && (a).xrecoff < (b).xrecoff))
50
51 #define XLByteLE(a, b)          \
52                         ((a).xlogid < (b).xlogid || \
53                          ((a).xlogid == (b).xlogid && (a).xrecoff <= (b).xrecoff))
54
55 #define XLByteEQ(a, b)          \
56                         ((a).xlogid == (b).xlogid && (a).xrecoff == (b).xrecoff)
57
58
59 /*
60  * TimeLineID (TLI) - identifies different database histories to prevent
61  * confusion after restoring a prior state of a database installation.
62  * TLI does not change in a normal stop/restart of the database (including
63  * crash-and-recover cases); but we must assign a new TLI after doing
64  * a recovery to a prior state, a/k/a point-in-time recovery.  This makes
65  * the new WAL logfile sequence we generate distinguishable from the
66  * sequence that was generated in the previous incarnation.
67  */
68 typedef uint32 TimeLineID;
69
70 /*
71  *      Because O_DIRECT bypasses the kernel buffers, and because we never
72  *      read those buffers except during crash recovery, it is a win to use
73  *      it in all cases where we sync on each write().  We could allow O_DIRECT
74  *      with fsync(), but because skipping the kernel buffer forces writes out
75  *      quickly, it seems best just to use it for O_SYNC.  It is hard to imagine
76  *      how fsync() could be a win for O_DIRECT compared to O_SYNC and O_DIRECT.
77  *      Also, O_DIRECT is never enough to force data to the drives, it merely
78  *      tries to bypass the kernel cache, so we still need O_SYNC or fsync().
79  */
80 #ifdef O_DIRECT
81 #define PG_O_DIRECT                             O_DIRECT
82 #else
83 #define PG_O_DIRECT                             0
84 #endif
85
86 /*
87  * This chunk of hackery attempts to determine which file sync methods
88  * are available on the current platform, and to choose an appropriate
89  * default method.      We assume that fsync() is always available, and that
90  * configure determined whether fdatasync() is.
91  */
92 #if defined(O_SYNC)
93 #define BARE_OPEN_SYNC_FLAG             O_SYNC
94 #elif defined(O_FSYNC)
95 #define BARE_OPEN_SYNC_FLAG             O_FSYNC
96 #endif
97 #ifdef BARE_OPEN_SYNC_FLAG
98 #define OPEN_SYNC_FLAG                  (BARE_OPEN_SYNC_FLAG | PG_O_DIRECT)
99 #endif
100
101 #if defined(O_DSYNC)
102 #if defined(OPEN_SYNC_FLAG)
103 /* O_DSYNC is distinct? */
104 #if O_DSYNC != BARE_OPEN_SYNC_FLAG
105 #define OPEN_DATASYNC_FLAG              (O_DSYNC | PG_O_DIRECT)
106 #endif
107 #else                                                   /* !defined(OPEN_SYNC_FLAG) */
108 /* Win32 only has O_DSYNC */
109 #define OPEN_DATASYNC_FLAG              (O_DSYNC | PG_O_DIRECT)
110 #endif
111 #endif
112
113 #if defined(OPEN_DATASYNC_FLAG)
114 #define DEFAULT_SYNC_METHOD             SYNC_METHOD_OPEN_DSYNC
115 #elif defined(HAVE_FDATASYNC)
116 #define DEFAULT_SYNC_METHOD             SYNC_METHOD_FDATASYNC
117 #elif defined(HAVE_FSYNC_WRITETHROUGH_ONLY)
118 #define DEFAULT_SYNC_METHOD             SYNC_METHOD_FSYNC_WRITETHROUGH
119 #else
120 #define DEFAULT_SYNC_METHOD             SYNC_METHOD_FSYNC
121 #endif
122
123 /*
124  * Limitation of buffer-alignment for direct IO depends on OS and filesystem,
125  * but XLOG_BLCKSZ is assumed to be enough for it.
126  */
127 #ifdef O_DIRECT
128 #define ALIGNOF_XLOG_BUFFER             XLOG_BLCKSZ
129 #else
130 #define ALIGNOF_XLOG_BUFFER             ALIGNOF_BUFFER
131 #endif
132
133 #endif   /* XLOG_DEFS_H */