PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
bufmgr.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * bufmgr.h
4  * POSTGRES buffer manager definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/bufmgr.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef BUFMGR_H
15 #define BUFMGR_H
16 
17 #include "catalog/catalog.h"
18 #include "storage/block.h"
19 #include "storage/buf.h"
20 #include "storage/bufpage.h"
21 #include "storage/relfilenode.h"
22 #include "utils/relcache.h"
23 #include "utils/snapmgr.h"
24 #include "utils/tqual.h"
25 
26 typedef void *Block;
27 
28 /* Possible arguments for GetAccessStrategy() */
30 {
31  BAS_NORMAL, /* Normal random access */
32  BAS_BULKREAD, /* Large read-only scan (hint bit updates are
33  * ok) */
34  BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
35  BAS_VACUUM /* VACUUM */
37 
38 /* Possible modes for ReadBufferExtended() */
39 typedef enum
40 {
41  RBM_NORMAL, /* Normal read */
42  RBM_ZERO_AND_LOCK, /* Don't read from disk, caller will
43  * initialize. Also locks the page. */
44  RBM_ZERO_AND_CLEANUP_LOCK, /* Like RBM_ZERO_AND_LOCK, but locks the page
45  * in "cleanup" mode */
46  RBM_ZERO_ON_ERROR, /* Read, but return an all-zeros page on error */
47  RBM_NORMAL_NO_LOG /* Don't log page as invalid during WAL
48  * replay; otherwise same as RBM_NORMAL */
50 
51 /* forward declared, to avoid having to expose buf_internals.h here */
52 struct WritebackContext;
53 
54 /* in globals.c ... this duplicates miscadmin.h */
55 extern PGDLLIMPORT int NBuffers;
56 
57 /* in bufmgr.c */
58 #define WRITEBACK_MAX_PENDING_FLUSHES 256
59 
60 /* FIXME: Also default to on for mmap && msync(MS_ASYNC)? */
61 #ifdef HAVE_SYNC_FILE_RANGE
62 #define DEFAULT_CHECKPOINT_FLUSH_AFTER 32
63 #define DEFAULT_BACKEND_FLUSH_AFTER 16
64 #define DEFAULT_BGWRITER_FLUSH_AFTER 64
65 #else
66 #define DEFAULT_CHECKPOINT_FLUSH_AFTER 0
67 #define DEFAULT_BACKEND_FLUSH_AFTER 0
68 #define DEFAULT_BGWRITER_FLUSH_AFTER 0
69 #endif /* HAVE_SYNC_FILE_RANGE */
70 
71 extern bool zero_damaged_pages;
72 extern int bgwriter_lru_maxpages;
73 extern double bgwriter_lru_multiplier;
74 extern bool track_io_timing;
75 extern int target_prefetch_pages;
76 
77 extern int checkpoint_flush_after;
78 extern int backend_flush_after;
79 extern int bgwriter_flush_after;
80 
81 /* in buf_init.c */
82 extern PGDLLIMPORT char *BufferBlocks;
83 
84 /* in guc.c */
85 extern int effective_io_concurrency;
86 
87 /* in localbuf.c */
88 extern PGDLLIMPORT int NLocBuffer;
91 
92 /* upper limit for effective_io_concurrency */
93 #define MAX_IO_CONCURRENCY 1000
94 
95 /* special block number for ReadBuffer() */
96 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
97 
98 /*
99  * Buffer content lock modes (mode argument for LockBuffer())
100  */
101 #define BUFFER_LOCK_UNLOCK 0
102 #define BUFFER_LOCK_SHARE 1
103 #define BUFFER_LOCK_EXCLUSIVE 2
104 
105 /*
106  * These routines are beaten on quite heavily, hence the macroization.
107  */
108 
109 /*
110  * BufferIsValid
111  * True iff the given buffer number is valid (either as a shared
112  * or local buffer).
113  *
114  * Note: For a long time this was defined the same as BufferIsPinned,
115  * that is it would say False if you didn't hold a pin on the buffer.
116  * I believe this was bogus and served only to mask logic errors.
117  * Code should always know whether it has a buffer reference,
118  * independently of the pin state.
119  *
120  * Note: For a further long time this was not quite the inverse of the
121  * BufferIsInvalid() macro, in that it also did sanity checks to verify
122  * that the buffer number was in range. Most likely, this macro was
123  * originally intended only to be used in assertions, but its use has
124  * since expanded quite a bit, and the overhead of making those checks
125  * even in non-assert-enabled builds can be significant. Thus, we've
126  * now demoted the range checks to assertions within the macro itself.
127  */
128 #define BufferIsValid(bufnum) \
129 ( \
130  AssertMacro((bufnum) <= NBuffers && (bufnum) >= -NLocBuffer), \
131  (bufnum) != InvalidBuffer \
132 )
133 
134 /*
135  * BufferGetBlock
136  * Returns a reference to a disk page image associated with a buffer.
137  *
138  * Note:
139  * Assumes buffer is valid.
140  */
141 #define BufferGetBlock(buffer) \
142 ( \
143  AssertMacro(BufferIsValid(buffer)), \
144  BufferIsLocal(buffer) ? \
145  LocalBufferBlockPointers[-(buffer) - 1] \
146  : \
147  (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
148 )
149 
150 /*
151  * BufferGetPageSize
152  * Returns the page size within a buffer.
153  *
154  * Notes:
155  * Assumes buffer is valid.
156  *
157  * The buffer can be a raw disk block and need not contain a valid
158  * (formatted) disk page.
159  */
160 /* XXX should dig out of buffer descriptor */
161 #define BufferGetPageSize(buffer) \
162 ( \
163  AssertMacro(BufferIsValid(buffer)), \
164  (Size)BLCKSZ \
165 )
166 
167 /*
168  * BufferGetPage
169  * Returns the page associated with a buffer.
170  *
171  * When this is called as part of a scan, there may be a need for a nearby
172  * call to TestForOldSnapshot(). See the definition of that for details.
173  */
174 #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
175 
176 /*
177  * prototypes for functions in bufmgr.c
178  */
179 extern bool ComputeIoConcurrency(int io_concurrency, double *target);
180 extern void PrefetchBuffer(Relation reln, ForkNumber forkNum,
181  BlockNumber blockNum);
182 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
183 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
184  BlockNumber blockNum, ReadBufferMode mode,
185  BufferAccessStrategy strategy);
187  ForkNumber forkNum, BlockNumber blockNum,
188  ReadBufferMode mode, BufferAccessStrategy strategy);
189 extern void ReleaseBuffer(Buffer buffer);
190 extern void UnlockReleaseBuffer(Buffer buffer);
191 extern void MarkBufferDirty(Buffer buffer);
192 extern void IncrBufferRefCount(Buffer buffer);
193 extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
194  BlockNumber blockNum);
195 
196 extern void InitBufferPool(void);
197 extern void InitBufferPoolAccess(void);
198 extern void InitBufferPoolBackend(void);
199 extern void AtEOXact_Buffers(bool isCommit);
200 extern void PrintBufferLeakWarning(Buffer buffer);
201 extern void CheckPointBuffers(int flags);
204  ForkNumber forkNum);
205 extern void FlushOneBuffer(Buffer buffer);
206 extern void FlushRelationBuffers(Relation rel);
207 extern void FlushDatabaseBuffers(Oid dbid);
209  ForkNumber forkNum, BlockNumber firstDelBlock);
210 extern void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes);
211 extern void DropDatabaseBuffers(Oid dbid);
212 
213 #define RelationGetNumberOfBlocks(reln) \
214  RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
215 
216 extern bool BufferIsPermanent(Buffer buffer);
217 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
218 
219 #ifdef NOT_USED
220 extern void PrintPinnedBufs(void);
221 #endif
222 extern Size BufferShmemSize(void);
223 extern void BufferGetTag(Buffer buffer, RelFileNode *rnode,
224  ForkNumber *forknum, BlockNumber *blknum);
225 
226 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
227 
228 extern void UnlockBuffers(void);
229 extern void LockBuffer(Buffer buffer, int mode);
230 extern bool ConditionalLockBuffer(Buffer buffer);
231 extern void LockBufferForCleanup(Buffer buffer);
232 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
233 extern bool HoldingBufferPinThatDelaysRecovery(void);
234 
235 extern void AbortBufferIO(void);
236 
237 extern void BufmgrCommit(void);
238 extern bool BgBufferSync(struct WritebackContext *wb_context);
239 
240 extern void AtProcExit_LocalBuffers(void);
241 
242 extern void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation);
243 
244 /* in freelist.c */
246 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
247 
248 
249 /* inline functions */
250 
251 /*
252  * Although this header file is nominally backend-only, certain frontend
253  * programs like pg_xlogdump include it. For compilers that emit static
254  * inline functions even when they're unused, that leads to unsatisfied
255  * external references; hence hide these with #ifndef FRONTEND.
256  */
257 
258 #ifndef FRONTEND
259 
260 /*
261  * Check whether the given snapshot is too old to have safely read the given
262  * page from the given table. If so, throw a "snapshot too old" error.
263  *
264  * This test generally needs to be performed after every BufferGetPage() call
265  * that is executed as part of a scan. It is not needed for calls made for
266  * modifying the page (for example, to position to the right place to insert a
267  * new index tuple or for vacuuming). It may also be omitted where calls to
268  * lower-level functions will have already performed the test.
269  *
270  * Note that a NULL snapshot argument is allowed and causes a fast return
271  * without error; this is to support call sites which can be called from
272  * either scans or index modification areas.
273  *
274  * For best performance, keep the tests that are fastest and/or most likely to
275  * exclude a page from old snapshot testing near the front.
276  */
277 static inline void
278 TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
279 {
280  Assert(relation != NULL);
281 
282  if (old_snapshot_threshold >= 0
283  && (snapshot) != NULL
284  && (snapshot)->satisfies == HeapTupleSatisfiesMVCC
285  && !XLogRecPtrIsInvalid((snapshot)->lsn)
286  && PageGetLSN(page) > (snapshot)->lsn)
287  TestForOldSnapshot_impl(snapshot, relation);
288 }
289 
290 #endif /* FRONTEND */
291 
292 #endif /* BUFMGR_H */
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:3330
void PrintBufferLeakWarning(Buffer buffer)
Definition: bufmgr.c:2514
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:278
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3229
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:525
bool ComputeIoConcurrency(int io_concurrency, double *target)
Definition: bufmgr.c:467
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2398
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:3680
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:3131
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
PGDLLIMPORT char * BufferBlocks
Definition: buf_init.c:22
int checkpoint_flush_after
Definition: bufmgr.c:118
void BufmgrCommit(void)
Definition: bufmgr.c:2574
PGDLLIMPORT int32 * LocalRefCount
Definition: localbuf.c:44
bool track_io_timing
Definition: bufmgr.c:111
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
Size BufferShmemSize(void)
Definition: buf_init.c:173
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2588
uint32 BlockNumber
Definition: block.h:31
int effective_io_concurrency
Definition: bufmgr.c:112
void InitBufferPool(void)
Definition: buf_init.c:70
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3706
unsigned int Oid
Definition: postgres_ext.h:31
int bgwriter_lru_maxpages
Definition: bufmgr.c:109
void InitBufferPoolBackend(void)
Definition: bufmgr.c:2444
#define PGDLLIMPORT
Definition: c.h:1043
signed int int32
Definition: c.h:253
PGDLLIMPORT int NLocBuffer
Definition: localbuf.c:40
void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes)
Definition: bufmgr.c:2923
double bgwriter_lru_multiplier
Definition: bufmgr.c:110
bool BgBufferSync(struct WritebackContext *wb_context)
Definition: bufmgr.c:2028
Buffer ReadBufferWithoutRelcache(RelFileNode rnode, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:682
void AtProcExit_LocalBuffers(void)
Definition: localbuf.c:571
void UnlockBuffers(void)
Definition: bufmgr.c:3501
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2815
void InitBufferPoolAccess(void)
Definition: bufmgr.c:2420
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:1508
void AbortBufferIO(void)
Definition: bufmgr.c:3913
int bgwriter_flush_after
Definition: bufmgr.c:119
ForkNumber
Definition: relpath.h:24
bool zero_damaged_pages
Definition: bufmgr.c:108
void DropRelFileNodeBuffers(RelFileNodeBackend rnode, ForkNumber forkNum, BlockNumber firstDelBlock)
Definition: bufmgr.c:2866
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
ReadBufferMode
Definition: bufmgr.h:39
void PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:529
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:3272
bool BufferIsPermanent(Buffer buffer)
Definition: bufmgr.c:2785
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:3555
void BufferGetTag(Buffer buffer, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2609
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:667
int backend_flush_after
Definition: bufmgr.c:120
BufferAccessStrategyType
Definition: bufmgr.h:29
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2771
size_t Size
Definition: c.h:352
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3026
#define PageGetLSN(page)
Definition: bufpage.h:363
PGDLLIMPORT int NBuffers
Definition: globals.c:120
void CheckPointBuffers(int flags)
Definition: bufmgr.c:2557
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
int old_snapshot_threshold
Definition: snapmgr.c:70
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3362
void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation)
Definition: bufmgr.c:4291
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3586
bool HeapTupleSatisfiesMVCC(HeapTuple htup, Snapshot snapshot, Buffer buffer)
Definition: tqual.c:967
int Buffer
Definition: buf.h:23
PGDLLIMPORT Block * LocalBufferBlockPointers
Definition: localbuf.c:43
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:580
Pointer Page
Definition: bufpage.h:74
void * Block
Definition: bufmgr.h:26
int target_prefetch_pages
Definition: bufmgr.c:129