PostgreSQL Source Code  git master
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-2022, 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 "storage/block.h"
18 #include "storage/buf.h"
19 #include "storage/bufpage.h"
20 #include "storage/relfilenode.h"
21 #include "utils/relcache.h"
22 #include "utils/snapmgr.h"
23 
24 typedef void *Block;
25 
26 /* Possible arguments for GetAccessStrategy() */
28 {
29  BAS_NORMAL, /* Normal random access */
30  BAS_BULKREAD, /* Large read-only scan (hint bit updates are
31  * ok) */
32  BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
33  BAS_VACUUM /* VACUUM */
35 
36 /* Possible modes for ReadBufferExtended() */
37 typedef enum
38 {
39  RBM_NORMAL, /* Normal read */
40  RBM_ZERO_AND_LOCK, /* Don't read from disk, caller will
41  * initialize. Also locks the page. */
42  RBM_ZERO_AND_CLEANUP_LOCK, /* Like RBM_ZERO_AND_LOCK, but locks the page
43  * in "cleanup" mode */
44  RBM_ZERO_ON_ERROR, /* Read, but return an all-zeros page on error */
45  RBM_NORMAL_NO_LOG /* Don't log page as invalid during WAL
46  * replay; otherwise same as RBM_NORMAL */
48 
49 /*
50  * Type returned by PrefetchBuffer().
51  */
52 typedef struct PrefetchBufferResult
53 {
54  Buffer recent_buffer; /* If valid, a hit (recheck needed!) */
55  bool initiated_io; /* If true, a miss resulting in async I/O */
57 
58 /* forward declared, to avoid having to expose buf_internals.h here */
59 struct WritebackContext;
60 
61 /* forward declared, to avoid including smgr.h here */
62 struct SMgrRelationData;
63 
64 /* in globals.c ... this duplicates miscadmin.h */
65 extern PGDLLIMPORT int NBuffers;
66 
67 /* in bufmgr.c */
71 extern PGDLLIMPORT bool track_io_timing;
74 
78 
79 /* in buf_init.c */
80 extern PGDLLIMPORT char *BufferBlocks;
81 
82 /* in localbuf.c */
83 extern PGDLLIMPORT int NLocBuffer;
86 
87 /* upper limit for effective_io_concurrency */
88 #define MAX_IO_CONCURRENCY 1000
89 
90 /* special block number for ReadBuffer() */
91 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
92 
93 /*
94  * Buffer content lock modes (mode argument for LockBuffer())
95  */
96 #define BUFFER_LOCK_UNLOCK 0
97 #define BUFFER_LOCK_SHARE 1
98 #define BUFFER_LOCK_EXCLUSIVE 2
99 
100 /*
101  * These routines are beaten on quite heavily, hence the macroization.
102  */
103 
104 /*
105  * BufferIsValid
106  * True iff the given buffer number is valid (either as a shared
107  * or local buffer).
108  *
109  * Note: For a long time this was defined the same as BufferIsPinned,
110  * that is it would say False if you didn't hold a pin on the buffer.
111  * I believe this was bogus and served only to mask logic errors.
112  * Code should always know whether it has a buffer reference,
113  * independently of the pin state.
114  *
115  * Note: For a further long time this was not quite the inverse of the
116  * BufferIsInvalid() macro, in that it also did sanity checks to verify
117  * that the buffer number was in range. Most likely, this macro was
118  * originally intended only to be used in assertions, but its use has
119  * since expanded quite a bit, and the overhead of making those checks
120  * even in non-assert-enabled builds can be significant. Thus, we've
121  * now demoted the range checks to assertions within the macro itself.
122  */
123 #define BufferIsValid(bufnum) \
124 ( \
125  AssertMacro((bufnum) <= NBuffers && (bufnum) >= -NLocBuffer), \
126  (bufnum) != InvalidBuffer \
127 )
128 
129 /*
130  * BufferGetBlock
131  * Returns a reference to a disk page image associated with a buffer.
132  *
133  * Note:
134  * Assumes buffer is valid.
135  */
136 #define BufferGetBlock(buffer) \
137 ( \
138  AssertMacro(BufferIsValid(buffer)), \
139  BufferIsLocal(buffer) ? \
140  LocalBufferBlockPointers[-(buffer) - 1] \
141  : \
142  (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
143 )
144 
145 /*
146  * BufferGetPageSize
147  * Returns the page size within a buffer.
148  *
149  * Notes:
150  * Assumes buffer is valid.
151  *
152  * The buffer can be a raw disk block and need not contain a valid
153  * (formatted) disk page.
154  */
155 /* XXX should dig out of buffer descriptor */
156 #define BufferGetPageSize(buffer) \
157 ( \
158  AssertMacro(BufferIsValid(buffer)), \
159  (Size)BLCKSZ \
160 )
161 
162 /*
163  * BufferGetPage
164  * Returns the page associated with a buffer.
165  *
166  * When this is called as part of a scan, there may be a need for a nearby
167  * call to TestForOldSnapshot(). See the definition of that for details.
168  */
169 #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
170 
171 /*
172  * prototypes for functions in bufmgr.c
173  */
175  ForkNumber forkNum,
176  BlockNumber blockNum);
178  BlockNumber blockNum);
179 extern bool ReadRecentBuffer(RelFileNode rnode, ForkNumber forkNum,
180  BlockNumber blockNum, Buffer recent_buffer);
181 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
182 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
183  BlockNumber blockNum, ReadBufferMode mode,
184  BufferAccessStrategy strategy);
186  ForkNumber forkNum, BlockNumber blockNum,
188  bool permanent);
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 AtEOXact_Buffers(bool isCommit);
199 extern void PrintBufferLeakWarning(Buffer buffer);
200 extern void CheckPointBuffers(int flags);
203  ForkNumber forkNum);
204 extern void FlushOneBuffer(Buffer buffer);
205 extern void FlushRelationBuffers(Relation rel);
206 extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
207 extern void CreateAndCopyRelationData(RelFileNode src_rnode,
208  RelFileNode dst_rnode,
209  bool permanent);
210 extern void FlushDatabaseBuffers(Oid dbid);
211 extern void DropRelFileNodeBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum,
212  int nforks, BlockNumber *firstDelBlock);
213 extern void DropRelFileNodesAllBuffers(struct SMgrRelationData **smgr_reln, int nnodes);
214 extern void DropDatabaseBuffers(Oid dbid);
215 
216 #define RelationGetNumberOfBlocks(reln) \
217  RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
218 
219 extern bool BufferIsPermanent(Buffer buffer);
220 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
221 
222 #ifdef NOT_USED
223 extern void PrintPinnedBufs(void);
224 #endif
225 extern Size BufferShmemSize(void);
226 extern void BufferGetTag(Buffer buffer, RelFileNode *rnode,
227  ForkNumber *forknum, BlockNumber *blknum);
228 
229 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
230 
231 extern void UnlockBuffers(void);
232 extern void LockBuffer(Buffer buffer, int mode);
233 extern bool ConditionalLockBuffer(Buffer buffer);
234 extern void LockBufferForCleanup(Buffer buffer);
235 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
236 extern bool IsBufferCleanupOK(Buffer buffer);
237 extern bool HoldingBufferPinThatDelaysRecovery(void);
238 
239 extern void AbortBufferIO(void);
240 
241 extern void BufmgrCommit(void);
242 extern bool BgBufferSync(struct WritebackContext *wb_context);
243 
244 extern void AtProcExit_LocalBuffers(void);
245 
246 extern void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation);
247 
248 /* in freelist.c */
250 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
251 
252 
253 /* inline functions */
254 
255 /*
256  * Although this header file is nominally backend-only, certain frontend
257  * programs like pg_waldump include it. For compilers that emit static
258  * inline functions even when they're unused, that leads to unsatisfied
259  * external references; hence hide these with #ifndef FRONTEND.
260  */
261 
262 #ifndef FRONTEND
263 
264 /*
265  * Check whether the given snapshot is too old to have safely read the given
266  * page from the given table. If so, throw a "snapshot too old" error.
267  *
268  * This test generally needs to be performed after every BufferGetPage() call
269  * that is executed as part of a scan. It is not needed for calls made for
270  * modifying the page (for example, to position to the right place to insert a
271  * new index tuple or for vacuuming). It may also be omitted where calls to
272  * lower-level functions will have already performed the test.
273  *
274  * Note that a NULL snapshot argument is allowed and causes a fast return
275  * without error; this is to support call sites which can be called from
276  * either scans or index modification areas.
277  *
278  * For best performance, keep the tests that are fastest and/or most likely to
279  * exclude a page from old snapshot testing near the front.
280  */
281 static inline void
282 TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
283 {
284  Assert(relation != NULL);
285 
286  if (old_snapshot_threshold >= 0
287  && (snapshot) != NULL
288  && ((snapshot)->snapshot_type == SNAPSHOT_MVCC
289  || (snapshot)->snapshot_type == SNAPSHOT_TOAST)
290  && !XLogRecPtrIsInvalid((snapshot)->lsn)
291  && PageGetLSN(page) > (snapshot)->lsn)
292  TestForOldSnapshot_impl(snapshot, relation);
293 }
294 
295 #endif /* FRONTEND */
296 
297 #endif /* BUFMGR_H */
uint32 BlockNumber
Definition: block.h:31
int Buffer
Definition: buf.h:23
BufferAccessStrategyType
Definition: bufmgr.h:28
@ BAS_BULKREAD
Definition: bufmgr.h:30
@ BAS_NORMAL
Definition: bufmgr.h:29
@ BAS_VACUUM
Definition: bufmgr.h:33
@ BAS_BULKWRITE
Definition: bufmgr.h:32
void CreateAndCopyRelationData(RelFileNode src_rnode, RelFileNode dst_rnode, bool permanent)
Definition: bufmgr.c:3778
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:3953
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3404
bool BgBufferSync(struct WritebackContext *wb_context)
Definition: bufmgr.c:2217
PGDLLIMPORT int effective_io_concurrency
Definition: bufmgr.c:145
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2755
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:1636
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:592
void DropRelFileNodesAllBuffers(struct SMgrRelationData **smgr_reln, int nnodes)
PGDLLIMPORT int bgwriter_flush_after
Definition: bufmgr.c:159
Size BufferShmemSize(void)
Definition: buf_init.c:157
void BufmgrCommit(void)
Definition: bufmgr.c:2741
PGDLLIMPORT bool zero_damaged_pages
Definition: bufmgr.c:134
PGDLLIMPORT Block * LocalBufferBlockPointers
Definition: localbuf.c:44
bool IsBufferCleanupOK(Buffer buffer)
Definition: bufmgr.c:4446
Buffer ReadBufferWithoutRelcache(RelFileNode rnode, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
Definition: bufmgr.c:789
PGDLLIMPORT int bgwriter_lru_maxpages
Definition: bufmgr.c:135
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2587
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
struct PrefetchBufferResult PrefetchBufferResult
PGDLLIMPORT int maintenance_io_concurrency
Definition: bufmgr.c:152
void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation)
Definition: bufmgr.c:4997
void AtProcExit_LocalBuffers(void)
Definition: localbuf.c:588
PGDLLIMPORT bool track_io_timing
Definition: bufmgr.c:137
PGDLLIMPORT int NBuffers
Definition: globals.c:136
void InitBufferPool(void)
Definition: buf_init.c:68
void CheckPointBuffers(int flags)
Definition: bufmgr.c:2731
bool ReadRecentBuffer(RelFileNode rnode, ForkNumber forkNum, BlockNumber blockNum, Buffer recent_buffer)
Definition: bufmgr.c:623
bool BufferIsPermanent(Buffer buffer)
Definition: bufmgr.c:2974
void UnlockBuffers(void)
Definition: bufmgr.c:4128
void * Block
Definition: bufmgr.h:24
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4182
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2942
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3915
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:596
PGDLLIMPORT int32 * LocalRefCount
Definition: localbuf.c:45
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3004
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:4364
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3938
PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln, ForkNumber forkNum, BlockNumber blockNum)
void DropRelFileNodeBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1573
void InitBufferPoolAccess(void)
Definition: bufmgr.c:2604
void AbortBufferIO(void)
Definition: bufmgr.c:4626
PGDLLIMPORT int NLocBuffer
Definition: localbuf.c:41
void PrintBufferLeakWarning(Buffer buffer)
Definition: bufmgr.c:2688
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4213
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4156
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3985
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:3508
PGDLLIMPORT int backend_flush_after
Definition: bufmgr.c:160
PGDLLIMPORT double bgwriter_lru_multiplier
Definition: bufmgr.c:136
void BufferGetTag(Buffer buffer, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2776
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3852
void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels)
PGDLLIMPORT int checkpoint_flush_after
Definition: bufmgr.c:158
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:749
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:282
PGDLLIMPORT char * BufferBlocks
Definition: buf_init.c:22
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:702
ReadBufferMode
Definition: bufmgr.h:38
@ RBM_ZERO_ON_ERROR
Definition: bufmgr.h:44
@ RBM_ZERO_AND_CLEANUP_LOCK
Definition: bufmgr.h:42
@ RBM_ZERO_AND_LOCK
Definition: bufmgr.h:40
@ RBM_NORMAL
Definition: bufmgr.h:39
@ RBM_NORMAL_NO_LOG
Definition: bufmgr.h:45
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4390
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:3895
Pointer Page
Definition: bufpage.h:78
#define PageGetLSN(page)
Definition: bufpage.h:365
#define PGDLLIMPORT
Definition: c.h:1342
signed int int32
Definition: c.h:440
size_t Size
Definition: c.h:551
Assert(fmt[strlen(fmt) - 1] !='\n')
static PgChecksumMode mode
Definition: pg_checksums.c:65
unsigned int Oid
Definition: postgres_ext.h:31
ForkNumber
Definition: relpath.h:41
int old_snapshot_threshold
Definition: snapmgr.c:78
@ SNAPSHOT_TOAST
Definition: snapshot.h:74
@ SNAPSHOT_MVCC
Definition: snapshot.h:50
Buffer recent_buffer
Definition: bufmgr.h:54
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21