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/relfilelocator.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;
72 
73 /* only applicable when prefetching is available */
74 #ifdef USE_PREFETCH
75 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 1
76 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 10
77 #else
78 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 0
79 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 0
80 #endif
83 
87 
88 /* in buf_init.c */
89 extern PGDLLIMPORT char *BufferBlocks;
90 
91 /* in localbuf.c */
92 extern PGDLLIMPORT int NLocBuffer;
95 
96 /* upper limit for effective_io_concurrency */
97 #define MAX_IO_CONCURRENCY 1000
98 
99 /* special block number for ReadBuffer() */
100 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
101 
102 /*
103  * Buffer content lock modes (mode argument for LockBuffer())
104  */
105 #define BUFFER_LOCK_UNLOCK 0
106 #define BUFFER_LOCK_SHARE 1
107 #define BUFFER_LOCK_EXCLUSIVE 2
108 
109 
110 /*
111  * prototypes for functions in bufmgr.c
112  */
114  ForkNumber forkNum,
115  BlockNumber blockNum);
117  BlockNumber blockNum);
118 extern bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum,
119  BlockNumber blockNum, Buffer recent_buffer);
120 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
121 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
122  BlockNumber blockNum, ReadBufferMode mode,
123  BufferAccessStrategy strategy);
125  ForkNumber forkNum, BlockNumber blockNum,
127  bool permanent);
128 extern void ReleaseBuffer(Buffer buffer);
129 extern void UnlockReleaseBuffer(Buffer buffer);
130 extern void MarkBufferDirty(Buffer buffer);
131 extern void IncrBufferRefCount(Buffer buffer);
132 extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
133  BlockNumber blockNum);
134 
135 extern void InitBufferPoolAccess(void);
136 extern void AtEOXact_Buffers(bool isCommit);
137 extern void PrintBufferLeakWarning(Buffer buffer);
138 extern void CheckPointBuffers(int flags);
141  ForkNumber forkNum);
142 extern void FlushOneBuffer(Buffer buffer);
143 extern void FlushRelationBuffers(Relation rel);
144 extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
145 extern void CreateAndCopyRelationData(RelFileLocator src_rlocator,
146  RelFileLocator dst_rlocator,
147  bool permanent);
148 extern void FlushDatabaseBuffers(Oid dbid);
149 extern void DropRelationBuffers(struct SMgrRelationData *smgr_reln,
150  ForkNumber *forkNum,
151  int nforks, BlockNumber *firstDelBlock);
152 extern void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln,
153  int nlocators);
154 extern void DropDatabaseBuffers(Oid dbid);
155 
156 #define RelationGetNumberOfBlocks(reln) \
157  RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
158 
159 extern bool BufferIsPermanent(Buffer buffer);
160 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
161 
162 #ifdef NOT_USED
163 extern void PrintPinnedBufs(void);
164 #endif
165 extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator,
166  ForkNumber *forknum, BlockNumber *blknum);
167 
168 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
169 
170 extern void UnlockBuffers(void);
171 extern void LockBuffer(Buffer buffer, int mode);
172 extern bool ConditionalLockBuffer(Buffer buffer);
173 extern void LockBufferForCleanup(Buffer buffer);
174 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
175 extern bool IsBufferCleanupOK(Buffer buffer);
176 extern bool HoldingBufferPinThatDelaysRecovery(void);
177 
178 extern void AbortBufferIO(void);
179 
180 extern void BufmgrCommit(void);
181 extern bool BgBufferSync(struct WritebackContext *wb_context);
182 
183 extern void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation);
184 
185 /* in buf_init.c */
186 extern void InitBufferPool(void);
187 extern Size BufferShmemSize(void);
188 
189 /* in localbuf.c */
190 extern void AtProcExit_LocalBuffers(void);
191 
192 /* in freelist.c */
194 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
195 
196 
197 /* inline functions */
198 
199 /*
200  * Although this header file is nominally backend-only, certain frontend
201  * programs like pg_waldump include it. For compilers that emit static
202  * inline functions even when they're unused, that leads to unsatisfied
203  * external references; hence hide these with #ifndef FRONTEND.
204  */
205 
206 #ifndef FRONTEND
207 
208 /*
209  * BufferIsValid
210  * True iff the given buffer number is valid (either as a shared
211  * or local buffer).
212  *
213  * Note: For a long time this was defined the same as BufferIsPinned,
214  * that is it would say False if you didn't hold a pin on the buffer.
215  * I believe this was bogus and served only to mask logic errors.
216  * Code should always know whether it has a buffer reference,
217  * independently of the pin state.
218  *
219  * Note: For a further long time this was not quite the inverse of the
220  * BufferIsInvalid() macro, in that it also did sanity checks to verify
221  * that the buffer number was in range. Most likely, this macro was
222  * originally intended only to be used in assertions, but its use has
223  * since expanded quite a bit, and the overhead of making those checks
224  * even in non-assert-enabled builds can be significant. Thus, we've
225  * now demoted the range checks to assertions within the macro itself.
226  */
227 static inline bool
229 {
230  Assert(bufnum <= NBuffers);
231  Assert(bufnum >= -NLocBuffer);
232 
233  return bufnum != InvalidBuffer;
234 }
235 
236 /*
237  * BufferGetBlock
238  * Returns a reference to a disk page image associated with a buffer.
239  *
240  * Note:
241  * Assumes buffer is valid.
242  */
243 static inline Block
245 {
246  Assert(BufferIsValid(buffer));
247 
248  if (BufferIsLocal(buffer))
249  return LocalBufferBlockPointers[-buffer - 1];
250  else
251  return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
252 }
253 
254 /*
255  * BufferGetPageSize
256  * Returns the page size within a buffer.
257  *
258  * Notes:
259  * Assumes buffer is valid.
260  *
261  * The buffer can be a raw disk block and need not contain a valid
262  * (formatted) disk page.
263  */
264 /* XXX should dig out of buffer descriptor */
265 static inline Size
267 {
268  AssertMacro(BufferIsValid(buffer));
269  return (Size) BLCKSZ;
270 }
271 
272 /*
273  * BufferGetPage
274  * Returns the page associated with a buffer.
275  *
276  * When this is called as part of a scan, there may be a need for a nearby
277  * call to TestForOldSnapshot(). See the definition of that for details.
278  */
279 static inline Page
281 {
282  return (Page) BufferGetBlock(buffer);
283 }
284 
285 /*
286  * Check whether the given snapshot is too old to have safely read the given
287  * page from the given table. If so, throw a "snapshot too old" error.
288  *
289  * This test generally needs to be performed after every BufferGetPage() call
290  * that is executed as part of a scan. It is not needed for calls made for
291  * modifying the page (for example, to position to the right place to insert a
292  * new index tuple or for vacuuming). It may also be omitted where calls to
293  * lower-level functions will have already performed the test.
294  *
295  * Note that a NULL snapshot argument is allowed and causes a fast return
296  * without error; this is to support call sites which can be called from
297  * either scans or index modification areas.
298  *
299  * For best performance, keep the tests that are fastest and/or most likely to
300  * exclude a page from old snapshot testing near the front.
301  */
302 static inline void
303 TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
304 {
305  Assert(relation != NULL);
306 
307  if (old_snapshot_threshold >= 0
308  && (snapshot) != NULL
309  && ((snapshot)->snapshot_type == SNAPSHOT_MVCC
310  || (snapshot)->snapshot_type == SNAPSHOT_TOAST)
311  && !XLogRecPtrIsInvalid((snapshot)->lsn)
312  && PageGetLSN(page) > (snapshot)->lsn)
313  TestForOldSnapshot_impl(snapshot, relation);
314 }
315 
316 #endif /* FRONTEND */
317 
318 #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: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 IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:3969
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3413
bool BgBufferSync(struct WritebackContext *wb_context)
Definition: bufmgr.c:2224
PGDLLIMPORT int effective_io_concurrency
Definition: bufmgr.c:145
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2763
void DropRelationBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:1646
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:592
PGDLLIMPORT int bgwriter_flush_after
Definition: bufmgr.c:159
Size BufferShmemSize(void)
Definition: buf_init.c:157
void BufmgrCommit(void)
Definition: bufmgr.c:2749
PGDLLIMPORT bool zero_damaged_pages
Definition: bufmgr.c:134
PGDLLIMPORT Block * LocalBufferBlockPointers
Definition: localbuf.c:44
bool IsBufferCleanupOK(Buffer buffer)
Definition: bufmgr.c:4462
PGDLLIMPORT int bgwriter_lru_maxpages
Definition: bufmgr.c:135
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:280
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2594
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
struct PrefetchBufferResult PrefetchBufferResult
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:244
void CreateAndCopyRelationData(RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent)
Definition: bufmgr.c:3799
PGDLLIMPORT int maintenance_io_concurrency
Definition: bufmgr.c:152
void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation)
Definition: bufmgr.c:5025
void AtProcExit_LocalBuffers(void)
Definition: localbuf.c:609
PGDLLIMPORT bool track_io_timing
Definition: bufmgr.c:137
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2784
PGDLLIMPORT int NBuffers
Definition: globals.c:136
void InitBufferPool(void)
Definition: buf_init.c:68
void CheckPointBuffers(int flags)
Definition: bufmgr.c:2739
void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln, int nlocators)
bool BufferIsPermanent(Buffer buffer)
Definition: bufmgr.c:2982
void UnlockBuffers(void)
Definition: bufmgr.c:4144
void * Block
Definition: bufmgr.h:24
static Size BufferGetPageSize(Buffer buffer)
Definition: bufmgr.h:266
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4198
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2950
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3931
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:596
PGDLLIMPORT int32 * LocalRefCount
Definition: localbuf.c:45
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3012
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:4380
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3954
PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln, ForkNumber forkNum, BlockNumber blockNum)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1583
void InitBufferPoolAccess(void)
Definition: bufmgr.c:2611
void AbortBufferIO(void)
Definition: bufmgr.c:4642
PGDLLIMPORT int NLocBuffer
Definition: localbuf.c:41
void PrintBufferLeakWarning(Buffer buffer)
Definition: bufmgr.c:2695
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4229
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4172
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:4001
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:3519
Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
Definition: bufmgr.c:799
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:623
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3868
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:759
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:303
PGDLLIMPORT char * BufferBlocks
Definition: buf_init.c:22
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:712
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:4406
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:3911
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:228
Pointer Page
Definition: bufpage.h:78
static XLogRecPtr PageGetLSN(Page page)
Definition: bufpage.h:383
#define AssertMacro(condition)
Definition: c.h:795
#define PGDLLIMPORT
Definition: c.h:1252
signed int int32
Definition: c.h:430
size_t Size
Definition: c.h:541
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:54
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21