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 /*
64  * Flags influencing the behaviour of ExtendBufferedRel*
65  */
66 typedef enum ExtendBufferedFlags
67 {
68  /*
69  * Don't acquire extension lock. This is safe only if the relation isn't
70  * shared, an access exclusive lock is held or if this is the startup
71  * process.
72  */
74 
75  /* Is this extension part of recovery? */
77 
78  /*
79  * Should the fork be created if it does not currently exist? This likely
80  * only ever makes sense for relation forks.
81  */
83 
84  /* Should the first (possibly only) return buffer be returned locked? */
85  EB_LOCK_FIRST = (1 << 3),
86 
87  /* Should the smgr size cache be cleared? */
88  EB_CLEAR_SIZE_CACHE = (1 << 4),
89 
90  /* internal flags follow */
91  EB_LOCK_TARGET = (1 << 5),
93 
94 /*
95  * Some functions identify relations either by relation or smgr +
96  * relpersistence. Used via the BMR_REL()/BMR_SMGR() macros below. This
97  * allows us to use the same function for both recovery and normal operation.
98  */
99 typedef struct BufferManagerRelation
100 {
105 
106 #define BMR_REL(p_rel) ((BufferManagerRelation){.rel = p_rel})
107 #define BMR_SMGR(p_smgr, p_relpersistence) ((BufferManagerRelation){.smgr = p_smgr, .relpersistence = p_relpersistence})
108 
109 
110 /* forward declared, to avoid having to expose buf_internals.h here */
111 struct WritebackContext;
112 
113 /* forward declared, to avoid including smgr.h here */
114 struct SMgrRelationData;
115 
116 /* in globals.c ... this duplicates miscadmin.h */
117 extern PGDLLIMPORT int NBuffers;
118 
119 /* in bufmgr.c */
120 extern PGDLLIMPORT bool zero_damaged_pages;
123 extern PGDLLIMPORT bool track_io_timing;
124 
125 /* only applicable when prefetching is available */
126 #ifdef USE_PREFETCH
127 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 1
128 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 10
129 #else
130 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 0
131 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 0
132 #endif
135 
139 
140 /* in buf_init.c */
141 extern PGDLLIMPORT char *BufferBlocks;
142 
143 /* in localbuf.c */
144 extern PGDLLIMPORT int NLocBuffer;
147 
148 /* upper limit for effective_io_concurrency */
149 #define MAX_IO_CONCURRENCY 1000
150 
151 /* special block number for ReadBuffer() */
152 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
153 
154 /*
155  * Buffer content lock modes (mode argument for LockBuffer())
156  */
157 #define BUFFER_LOCK_UNLOCK 0
158 #define BUFFER_LOCK_SHARE 1
159 #define BUFFER_LOCK_EXCLUSIVE 2
160 
161 
162 /*
163  * prototypes for functions in bufmgr.c
164  */
166  ForkNumber forkNum,
167  BlockNumber blockNum);
169  BlockNumber blockNum);
170 extern bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum,
171  BlockNumber blockNum, Buffer recent_buffer);
172 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
173 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
174  BlockNumber blockNum, ReadBufferMode mode,
175  BufferAccessStrategy strategy);
177  ForkNumber forkNum, BlockNumber blockNum,
179  bool permanent);
180 extern void ReleaseBuffer(Buffer buffer);
181 extern void UnlockReleaseBuffer(Buffer buffer);
182 extern bool BufferIsExclusiveLocked(Buffer buffer);
183 extern bool BufferIsDirty(Buffer buffer);
184 extern void MarkBufferDirty(Buffer buffer);
185 extern void IncrBufferRefCount(Buffer buffer);
186 extern void CheckBufferIsPinnedOnce(Buffer buffer);
187 extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
188  BlockNumber blockNum);
189 
191  ForkNumber forkNum,
192  BufferAccessStrategy strategy,
193  uint32 flags);
195  ForkNumber fork,
196  BufferAccessStrategy strategy,
197  uint32 flags,
198  uint32 extend_by,
199  Buffer *buffers,
200  uint32 *extended_by);
202  ForkNumber fork,
203  BufferAccessStrategy strategy,
204  uint32 flags,
205  BlockNumber extend_to,
207 
208 extern void InitBufferPoolAccess(void);
209 extern void AtEOXact_Buffers(bool isCommit);
210 extern char *DebugPrintBufferRefcount(Buffer buffer);
211 extern void CheckPointBuffers(int flags);
214  ForkNumber forkNum);
215 extern void FlushOneBuffer(Buffer buffer);
216 extern void FlushRelationBuffers(Relation rel);
217 extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
218 extern void CreateAndCopyRelationData(RelFileLocator src_rlocator,
219  RelFileLocator dst_rlocator,
220  bool permanent);
221 extern void FlushDatabaseBuffers(Oid dbid);
222 extern void DropRelationBuffers(struct SMgrRelationData *smgr_reln,
223  ForkNumber *forkNum,
224  int nforks, BlockNumber *firstDelBlock);
225 extern void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln,
226  int nlocators);
227 extern void DropDatabaseBuffers(Oid dbid);
228 
229 #define RelationGetNumberOfBlocks(reln) \
230  RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
231 
232 extern bool BufferIsPermanent(Buffer buffer);
233 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
234 
235 #ifdef NOT_USED
236 extern void PrintPinnedBufs(void);
237 #endif
238 extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator,
239  ForkNumber *forknum, BlockNumber *blknum);
240 
241 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
242 
243 extern void UnlockBuffers(void);
244 extern void LockBuffer(Buffer buffer, int mode);
245 extern bool ConditionalLockBuffer(Buffer buffer);
246 extern void LockBufferForCleanup(Buffer buffer);
247 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
248 extern bool IsBufferCleanupOK(Buffer buffer);
249 extern bool HoldingBufferPinThatDelaysRecovery(void);
250 
251 extern bool BgBufferSync(struct WritebackContext *wb_context);
252 
253 /* in buf_init.c */
254 extern void InitBufferPool(void);
255 extern Size BufferShmemSize(void);
256 
257 /* in localbuf.c */
258 extern void AtProcExit_LocalBuffers(void);
259 
260 /* in freelist.c */
261 
264  int ring_size_kb);
266 
267 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
268 
269 
270 /* inline functions */
271 
272 /*
273  * Although this header file is nominally backend-only, certain frontend
274  * programs like pg_waldump include it. For compilers that emit static
275  * inline functions even when they're unused, that leads to unsatisfied
276  * external references; hence hide these with #ifndef FRONTEND.
277  */
278 
279 #ifndef FRONTEND
280 
281 /*
282  * BufferIsValid
283  * True iff the given buffer number is valid (either as a shared
284  * or local buffer).
285  *
286  * Note: For a long time this was defined the same as BufferIsPinned,
287  * that is it would say False if you didn't hold a pin on the buffer.
288  * I believe this was bogus and served only to mask logic errors.
289  * Code should always know whether it has a buffer reference,
290  * independently of the pin state.
291  *
292  * Note: For a further long time this was not quite the inverse of the
293  * BufferIsInvalid() macro, in that it also did sanity checks to verify
294  * that the buffer number was in range. Most likely, this macro was
295  * originally intended only to be used in assertions, but its use has
296  * since expanded quite a bit, and the overhead of making those checks
297  * even in non-assert-enabled builds can be significant. Thus, we've
298  * now demoted the range checks to assertions within the macro itself.
299  */
300 static inline bool
302 {
303  Assert(bufnum <= NBuffers);
304  Assert(bufnum >= -NLocBuffer);
305 
306  return bufnum != InvalidBuffer;
307 }
308 
309 /*
310  * BufferGetBlock
311  * Returns a reference to a disk page image associated with a buffer.
312  *
313  * Note:
314  * Assumes buffer is valid.
315  */
316 static inline Block
318 {
319  Assert(BufferIsValid(buffer));
320 
321  if (BufferIsLocal(buffer))
322  return LocalBufferBlockPointers[-buffer - 1];
323  else
324  return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
325 }
326 
327 /*
328  * BufferGetPageSize
329  * Returns the page size within a buffer.
330  *
331  * Notes:
332  * Assumes buffer is valid.
333  *
334  * The buffer can be a raw disk block and need not contain a valid
335  * (formatted) disk page.
336  */
337 /* XXX should dig out of buffer descriptor */
338 static inline Size
340 {
341  AssertMacro(BufferIsValid(buffer));
342  return (Size) BLCKSZ;
343 }
344 
345 /*
346  * BufferGetPage
347  * Returns the page associated with a buffer.
348  */
349 static inline Page
351 {
352  return (Page) BufferGetBlock(buffer);
353 }
354 
355 #endif /* FRONTEND */
356 
357 #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 CheckBufferIsPinnedOnce(Buffer buffer)
Definition: bufmgr.c:4855
struct BufferManagerRelation BufferManagerRelation
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:4605
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:4048
bool BgBufferSync(struct WritebackContext *wb_context)
Definition: bufmgr.c:2850
PGDLLIMPORT int effective_io_concurrency
Definition: bufmgr.c:147
bool BufferIsExclusiveLocked(Buffer buffer)
Definition: bufmgr.c:2137
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3386
void DropRelationBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:2261
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:628
PGDLLIMPORT int bgwriter_flush_after
Definition: bufmgr.c:161
Size BufferShmemSize(void)
Definition: buf_init.c:160
PGDLLIMPORT bool zero_damaged_pages
Definition: bufmgr.c:136
PGDLLIMPORT Block * LocalBufferBlockPointers
Definition: localbuf.c:46
bool IsBufferCleanupOK(Buffer buffer)
Definition: bufmgr.c:5105
PGDLLIMPORT int bgwriter_lru_maxpages
Definition: bufmgr.c:137
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:839
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3221
char * DebugPrintBufferRefcount(Buffer buffer)
Definition: bufmgr.c:3327
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
BlockNumber ExtendBufferedRelBy(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, uint32 extend_by, Buffer *buffers, uint32 *extended_by)
Definition: bufmgr.c:871
struct PrefetchBufferResult PrefetchBufferResult
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:317
void CreateAndCopyRelationData(RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent)
Definition: bufmgr.c:4442
PGDLLIMPORT int maintenance_io_concurrency
Definition: bufmgr.c:154
Buffer ExtendBufferedRelTo(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, BlockNumber extend_to, ReadBufferMode mode)
Definition: bufmgr.c:903
void AtProcExit_LocalBuffers(void)
Definition: localbuf.c:826
PGDLLIMPORT bool track_io_timing
Definition: bufmgr.c:139
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:3407
PGDLLIMPORT int NBuffers
Definition: globals.c:138
void InitBufferPool(void)
Definition: buf_init.c:68
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition: freelist.c:584
void CheckPointBuffers(int flags)
Definition: bufmgr.c:3372
bool BufferIsDirty(Buffer buffer)
Definition: bufmgr.c:2166
void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln, int nlocators)
bool BufferIsPermanent(Buffer buffer)
Definition: bufmgr.c:3617
void UnlockBuffers(void)
Definition: bufmgr.c:4780
void * Block
Definition: bufmgr.h:24
int GetAccessStrategyBufferCount(BufferAccessStrategy strategy)
Definition: freelist.c:624
static Size BufferGetPageSize(Buffer buffer)
Definition: bufmgr.h:339
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4834
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:3585
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4573
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:639
PGDLLIMPORT int32 * LocalRefCount
Definition: localbuf.c:47
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3647
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:5023
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4590
PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln, ForkNumber forkNum, BlockNumber blockNum)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2198
void InitBufferPoolAccess(void)
Definition: bufmgr.c:3238
PGDLLIMPORT int NLocBuffer
Definition: localbuf.c:43
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4888
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4808
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:4637
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:4154
ExtendBufferedFlags
Definition: bufmgr.h:67
@ EB_LOCK_TARGET
Definition: bufmgr.h:91
@ EB_CLEAR_SIZE_CACHE
Definition: bufmgr.h:88
@ EB_PERFORMING_RECOVERY
Definition: bufmgr.h:76
@ EB_CREATE_FORK_IF_NEEDED
Definition: bufmgr.h:82
@ EB_SKIP_EXTENSION_LOCK
Definition: bufmgr.h:73
@ EB_LOCK_FIRST
Definition: bufmgr.h:85
Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
Definition: bufmgr.c:822
PGDLLIMPORT int backend_flush_after
Definition: bufmgr.c:162
PGDLLIMPORT double bgwriter_lru_multiplier
Definition: bufmgr.c:138
bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, Buffer recent_buffer)
Definition: bufmgr.c:659
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:4511
void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels)
PGDLLIMPORT int checkpoint_flush_after
Definition: bufmgr.c:160
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:782
PGDLLIMPORT char * BufferBlocks
Definition: buf_init.c:22
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:735
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:5049
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:4553
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:301
Pointer Page
Definition: bufpage.h:78
unsigned int uint32
Definition: c.h:495
#define AssertMacro(condition)
Definition: c.h:848
#define PGDLLIMPORT
Definition: c.h:1326
signed int int32
Definition: c.h:483
size_t Size
Definition: c.h:594
Assert(fmt[strlen(fmt) - 1] !='\n')
static PgChecksumMode mode
Definition: pg_checksums.c:56
unsigned int Oid
Definition: postgres_ext.h:31
ForkNumber
Definition: relpath.h:48
struct SMgrRelationData * smgr
Definition: bufmgr.h:102
Buffer recent_buffer
Definition: bufmgr.h:59
uint64 XLogRecPtr
Definition: xlogdefs.h:21