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-2023, 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/relfilelocator.h"
21 #include "utils/relcache.h"
22 #include "utils/snapmgr.h"
23 
24 typedef void *Block;
25 
26 /*
27  * Possible arguments for GetAccessStrategy().
28  *
29  * If adding a new BufferAccessStrategyType, also add a new IOContext so
30  * IO statistics using this strategy are tracked.
31  */
33 {
34  BAS_NORMAL, /* Normal random access */
35  BAS_BULKREAD, /* Large read-only scan (hint bit updates are
36  * ok) */
37  BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
38  BAS_VACUUM /* VACUUM */
40 
41 /* Possible modes for ReadBufferExtended() */
42 typedef enum
43 {
44  RBM_NORMAL, /* Normal read */
45  RBM_ZERO_AND_LOCK, /* Don't read from disk, caller will
46  * initialize. Also locks the page. */
47  RBM_ZERO_AND_CLEANUP_LOCK, /* Like RBM_ZERO_AND_LOCK, but locks the page
48  * in "cleanup" mode */
49  RBM_ZERO_ON_ERROR, /* Read, but return an all-zeros page on error */
50  RBM_NORMAL_NO_LOG /* Don't log page as invalid during WAL
51  * replay; otherwise same as RBM_NORMAL */
53 
54 /*
55  * Type returned by PrefetchBuffer().
56  */
57 typedef struct PrefetchBufferResult
58 {
59  Buffer recent_buffer; /* If valid, a hit (recheck needed!) */
60  bool initiated_io; /* If true, a miss resulting in async I/O */
62 
63 /* forward declared, to avoid having to expose buf_internals.h here */
64 struct WritebackContext;
65 
66 /* forward declared, to avoid including smgr.h here */
67 struct SMgrRelationData;
68 
69 /* in globals.c ... this duplicates miscadmin.h */
70 extern PGDLLIMPORT int NBuffers;
71 
72 /* in bufmgr.c */
76 extern PGDLLIMPORT bool track_io_timing;
77 
78 /* only applicable when prefetching is available */
79 #ifdef USE_PREFETCH
80 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 1
81 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 10
82 #else
83 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 0
84 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 0
85 #endif
88 
92 
93 /* in buf_init.c */
94 extern PGDLLIMPORT char *BufferBlocks;
95 
96 /* in localbuf.c */
97 extern PGDLLIMPORT int NLocBuffer;
100 
101 /* upper limit for effective_io_concurrency */
102 #define MAX_IO_CONCURRENCY 1000
103 
104 /* special block number for ReadBuffer() */
105 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
106 
107 /*
108  * Buffer content lock modes (mode argument for LockBuffer())
109  */
110 #define BUFFER_LOCK_UNLOCK 0
111 #define BUFFER_LOCK_SHARE 1
112 #define BUFFER_LOCK_EXCLUSIVE 2
113 
114 
115 /*
116  * prototypes for functions in bufmgr.c
117  */
119  ForkNumber forkNum,
120  BlockNumber blockNum);
122  BlockNumber blockNum);
123 extern bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum,
124  BlockNumber blockNum, Buffer recent_buffer);
125 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
126 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
127  BlockNumber blockNum, ReadBufferMode mode,
128  BufferAccessStrategy strategy);
130  ForkNumber forkNum, BlockNumber blockNum,
132  bool permanent);
133 extern void ReleaseBuffer(Buffer buffer);
134 extern void UnlockReleaseBuffer(Buffer buffer);
135 extern void MarkBufferDirty(Buffer buffer);
136 extern void IncrBufferRefCount(Buffer buffer);
137 extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
138  BlockNumber blockNum);
139 
140 extern void InitBufferPoolAccess(void);
141 extern void AtEOXact_Buffers(bool isCommit);
142 extern void PrintBufferLeakWarning(Buffer buffer);
143 extern void CheckPointBuffers(int flags);
146  ForkNumber forkNum);
147 extern void FlushOneBuffer(Buffer buffer);
148 extern void FlushRelationBuffers(Relation rel);
149 extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
150 extern void CreateAndCopyRelationData(RelFileLocator src_rlocator,
151  RelFileLocator dst_rlocator,
152  bool permanent);
153 extern void FlushDatabaseBuffers(Oid dbid);
154 extern void DropRelationBuffers(struct SMgrRelationData *smgr_reln,
155  ForkNumber *forkNum,
156  int nforks, BlockNumber *firstDelBlock);
157 extern void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln,
158  int nlocators);
159 extern void DropDatabaseBuffers(Oid dbid);
160 
161 #define RelationGetNumberOfBlocks(reln) \
162  RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
163 
164 extern bool BufferIsPermanent(Buffer buffer);
165 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
166 
167 #ifdef NOT_USED
168 extern void PrintPinnedBufs(void);
169 #endif
170 extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator,
171  ForkNumber *forknum, BlockNumber *blknum);
172 
173 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
174 
175 extern void UnlockBuffers(void);
176 extern void LockBuffer(Buffer buffer, int mode);
177 extern bool ConditionalLockBuffer(Buffer buffer);
178 extern void LockBufferForCleanup(Buffer buffer);
179 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
180 extern bool IsBufferCleanupOK(Buffer buffer);
181 extern bool HoldingBufferPinThatDelaysRecovery(void);
182 
183 extern void AbortBufferIO(void);
184 
185 extern bool BgBufferSync(struct WritebackContext *wb_context);
186 
187 extern void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation);
188 
189 /* in buf_init.c */
190 extern void InitBufferPool(void);
191 extern Size BufferShmemSize(void);
192 
193 /* in localbuf.c */
194 extern void AtProcExit_LocalBuffers(void);
195 
196 /* in freelist.c */
198 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
199 
200 
201 /* inline functions */
202 
203 /*
204  * Although this header file is nominally backend-only, certain frontend
205  * programs like pg_waldump include it. For compilers that emit static
206  * inline functions even when they're unused, that leads to unsatisfied
207  * external references; hence hide these with #ifndef FRONTEND.
208  */
209 
210 #ifndef FRONTEND
211 
212 /*
213  * BufferIsValid
214  * True iff the given buffer number is valid (either as a shared
215  * or local buffer).
216  *
217  * Note: For a long time this was defined the same as BufferIsPinned,
218  * that is it would say False if you didn't hold a pin on the buffer.
219  * I believe this was bogus and served only to mask logic errors.
220  * Code should always know whether it has a buffer reference,
221  * independently of the pin state.
222  *
223  * Note: For a further long time this was not quite the inverse of the
224  * BufferIsInvalid() macro, in that it also did sanity checks to verify
225  * that the buffer number was in range. Most likely, this macro was
226  * originally intended only to be used in assertions, but its use has
227  * since expanded quite a bit, and the overhead of making those checks
228  * even in non-assert-enabled builds can be significant. Thus, we've
229  * now demoted the range checks to assertions within the macro itself.
230  */
231 static inline bool
233 {
234  Assert(bufnum <= NBuffers);
235  Assert(bufnum >= -NLocBuffer);
236 
237  return bufnum != InvalidBuffer;
238 }
239 
240 /*
241  * BufferGetBlock
242  * Returns a reference to a disk page image associated with a buffer.
243  *
244  * Note:
245  * Assumes buffer is valid.
246  */
247 static inline Block
249 {
250  Assert(BufferIsValid(buffer));
251 
252  if (BufferIsLocal(buffer))
253  return LocalBufferBlockPointers[-buffer - 1];
254  else
255  return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
256 }
257 
258 /*
259  * BufferGetPageSize
260  * Returns the page size within a buffer.
261  *
262  * Notes:
263  * Assumes buffer is valid.
264  *
265  * The buffer can be a raw disk block and need not contain a valid
266  * (formatted) disk page.
267  */
268 /* XXX should dig out of buffer descriptor */
269 static inline Size
271 {
272  AssertMacro(BufferIsValid(buffer));
273  return (Size) BLCKSZ;
274 }
275 
276 /*
277  * BufferGetPage
278  * Returns the page associated with a buffer.
279  *
280  * When this is called as part of a scan, there may be a need for a nearby
281  * call to TestForOldSnapshot(). See the definition of that for details.
282  */
283 static inline Page
285 {
286  return (Page) BufferGetBlock(buffer);
287 }
288 
289 /*
290  * Check whether the given snapshot is too old to have safely read the given
291  * page from the given table. If so, throw a "snapshot too old" error.
292  *
293  * This test generally needs to be performed after every BufferGetPage() call
294  * that is executed as part of a scan. It is not needed for calls made for
295  * modifying the page (for example, to position to the right place to insert a
296  * new index tuple or for vacuuming). It may also be omitted where calls to
297  * lower-level functions will have already performed the test.
298  *
299  * Note that a NULL snapshot argument is allowed and causes a fast return
300  * without error; this is to support call sites which can be called from
301  * either scans or index modification areas.
302  *
303  * For best performance, keep the tests that are fastest and/or most likely to
304  * exclude a page from old snapshot testing near the front.
305  */
306 static inline void
307 TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
308 {
309  Assert(relation != NULL);
310 
311  if (old_snapshot_threshold >= 0
312  && (snapshot) != NULL
313  && ((snapshot)->snapshot_type == SNAPSHOT_MVCC
314  || (snapshot)->snapshot_type == SNAPSHOT_TOAST)
315  && !XLogRecPtrIsInvalid((snapshot)->lsn)
316  && PageGetLSN(page) > (snapshot)->lsn)
317  TestForOldSnapshot_impl(snapshot, relation);
318 }
319 
320 #endif /* FRONTEND */
321 
322 #endif /* BUFMGR_H */
uint32 BlockNumber
Definition: block.h:31
int Buffer
Definition: buf.h:23
#define InvalidBuffer
Definition: buf.h:25
#define BufferIsLocal(buffer)
Definition: buf.h:37
BufferAccessStrategyType
Definition: bufmgr.h:33
@ BAS_BULKREAD
Definition: bufmgr.h:35
@ BAS_NORMAL
Definition: bufmgr.h:34
@ BAS_VACUUM
Definition: bufmgr.h:38
@ BAS_BULKWRITE
Definition: bufmgr.h:37
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:4023
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3464
bool BgBufferSync(struct WritebackContext *wb_context)
Definition: bufmgr.c:2262
PGDLLIMPORT int effective_io_concurrency
Definition: bufmgr.c:145
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2791
void DropRelationBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:1684
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:584
PGDLLIMPORT int bgwriter_flush_after
Definition: bufmgr.c:159
Size BufferShmemSize(void)
Definition: buf_init.c:157
PGDLLIMPORT bool zero_damaged_pages
Definition: bufmgr.c:134
PGDLLIMPORT Block * LocalBufferBlockPointers
Definition: localbuf.c:45
bool IsBufferCleanupOK(Buffer buffer)
Definition: bufmgr.c:4509
PGDLLIMPORT int bgwriter_lru_maxpages
Definition: bufmgr.c:135
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:284
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2632
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
struct PrefetchBufferResult PrefetchBufferResult
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:248
void CreateAndCopyRelationData(RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent)
Definition: bufmgr.c:3853
PGDLLIMPORT int maintenance_io_concurrency
Definition: bufmgr.c:152
void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation)
Definition: bufmgr.c:5072
void AtProcExit_LocalBuffers(void)
Definition: localbuf.c:610
PGDLLIMPORT bool track_io_timing
Definition: bufmgr.c:137
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2812
PGDLLIMPORT int NBuffers
Definition: globals.c:136
void InitBufferPool(void)
Definition: buf_init.c:68
void CheckPointBuffers(int flags)
Definition: bufmgr.c:2777
void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln, int nlocators)
bool BufferIsPermanent(Buffer buffer)
Definition: bufmgr.c:3033
void UnlockBuffers(void)
Definition: bufmgr.c:4198
void * Block
Definition: bufmgr.h:24
static Size BufferGetPageSize(Buffer buffer)
Definition: bufmgr.h:270
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4252
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:3001
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3985
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:596
PGDLLIMPORT int32 * LocalRefCount
Definition: localbuf.c:46
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3063
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:4427
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4008
PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln, ForkNumber forkNum, BlockNumber blockNum)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1621
void InitBufferPoolAccess(void)
Definition: bufmgr.c:2649
void AbortBufferIO(void)
Definition: bufmgr.c:4689
PGDLLIMPORT int NLocBuffer
Definition: localbuf.c:42
void PrintBufferLeakWarning(Buffer buffer)
Definition: bufmgr.c:2733
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4283
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4226
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:4055
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:3570
Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
Definition: bufmgr.c:791
PGDLLIMPORT int backend_flush_after
Definition: bufmgr.c:160
PGDLLIMPORT double bgwriter_lru_multiplier
Definition: bufmgr.c:136
bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, Buffer recent_buffer)
Definition: bufmgr.c:615
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3922
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:751
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:307
PGDLLIMPORT char * BufferBlocks
Definition: buf_init.c:22
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:704
ReadBufferMode
Definition: bufmgr.h:43
@ RBM_ZERO_ON_ERROR
Definition: bufmgr.h:49
@ RBM_ZERO_AND_CLEANUP_LOCK
Definition: bufmgr.h:47
@ RBM_ZERO_AND_LOCK
Definition: bufmgr.h:45
@ RBM_NORMAL
Definition: bufmgr.h:44
@ RBM_NORMAL_NO_LOG
Definition: bufmgr.h:50
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4453
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:3965
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:232
Pointer Page
Definition: bufpage.h:78
static XLogRecPtr PageGetLSN(Page page)
Definition: bufpage.h:383
#define AssertMacro(condition)
Definition: c.h:843
#define PGDLLIMPORT
Definition: c.h:1303
signed int int32
Definition: c.h:478
size_t Size
Definition: c.h:589
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:48
int old_snapshot_threshold
Definition: snapmgr.c:79
@ SNAPSHOT_TOAST
Definition: snapshot.h:74
@ SNAPSHOT_MVCC
Definition: snapshot.h:50
Buffer recent_buffer
Definition: bufmgr.h:59
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21