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-2024, 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 "port/pg_iovec.h"
18 #include "storage/block.h"
19 #include "storage/buf.h"
20 #include "storage/bufpage.h"
21 #include "storage/relfilelocator.h"
22 #include "utils/relcache.h"
23 #include "utils/snapmgr.h"
24 
25 typedef void *Block;
26 
27 /*
28  * Possible arguments for GetAccessStrategy().
29  *
30  * If adding a new BufferAccessStrategyType, also add a new IOContext so
31  * IO statistics using this strategy are tracked.
32  */
34 {
35  BAS_NORMAL, /* Normal random access */
36  BAS_BULKREAD, /* Large read-only scan (hint bit updates are
37  * ok) */
38  BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
39  BAS_VACUUM, /* VACUUM */
41 
42 /* Possible modes for ReadBufferExtended() */
43 typedef enum
44 {
45  RBM_NORMAL, /* Normal read */
46  RBM_ZERO_AND_LOCK, /* Don't read from disk, caller will
47  * initialize. Also locks the page. */
48  RBM_ZERO_AND_CLEANUP_LOCK, /* Like RBM_ZERO_AND_LOCK, but locks the page
49  * in "cleanup" mode */
50  RBM_ZERO_ON_ERROR, /* Read, but return an all-zeros page on error */
51  RBM_NORMAL_NO_LOG, /* Don't log page as invalid during WAL
52  * replay; otherwise same as RBM_NORMAL */
54 
55 /*
56  * Type returned by PrefetchBuffer().
57  */
58 typedef struct PrefetchBufferResult
59 {
60  Buffer recent_buffer; /* If valid, a hit (recheck needed!) */
61  bool initiated_io; /* If true, a miss resulting in async I/O */
63 
64 /*
65  * Flags influencing the behaviour of ExtendBufferedRel*
66  */
67 typedef enum ExtendBufferedFlags
68 {
69  /*
70  * Don't acquire extension lock. This is safe only if the relation isn't
71  * shared, an access exclusive lock is held or if this is the startup
72  * process.
73  */
75 
76  /* Is this extension part of recovery? */
78 
79  /*
80  * Should the fork be created if it does not currently exist? This likely
81  * only ever makes sense for relation forks.
82  */
84 
85  /* Should the first (possibly only) return buffer be returned locked? */
86  EB_LOCK_FIRST = (1 << 3),
87 
88  /* Should the smgr size cache be cleared? */
89  EB_CLEAR_SIZE_CACHE = (1 << 4),
90 
91  /* internal flags follow */
92  EB_LOCK_TARGET = (1 << 5),
94 
95 /*
96  * Some functions identify relations either by relation or smgr +
97  * relpersistence. Used via the BMR_REL()/BMR_SMGR() macros below. This
98  * allows us to use the same function for both recovery and normal operation.
99  */
100 typedef struct BufferManagerRelation
101 {
106 
107 #define BMR_REL(p_rel) ((BufferManagerRelation){.rel = p_rel})
108 #define BMR_SMGR(p_smgr, p_relpersistence) ((BufferManagerRelation){.smgr = p_smgr, .relpersistence = p_relpersistence})
109 
110 /* Zero out page if reading fails. */
111 #define READ_BUFFERS_ZERO_ON_ERROR (1 << 0)
112 /* Call smgrprefetch() if I/O necessary. */
113 #define READ_BUFFERS_ISSUE_ADVICE (1 << 1)
114 
116 {
117  /*
118  * The following members should be set by the caller. If only smgr is
119  * provided without rel, then smgr_persistence can be set to override the
120  * default assumption of RELPERSISTENCE_PERMANENT.
121  */
127 
128  /*
129  * The following private members are private state for communication
130  * between StartReadBuffers() and WaitReadBuffers(), initialized only if
131  * an actual read is required, and should not be modified.
132  */
135  int flags;
138 };
139 
141 
142 /* forward declared, to avoid having to expose buf_internals.h here */
143 struct WritebackContext;
144 
145 /* forward declared, to avoid including smgr.h here */
146 struct SMgrRelationData;
147 
148 /* in globals.c ... this duplicates miscadmin.h */
149 extern PGDLLIMPORT int NBuffers;
150 
151 /* in bufmgr.c */
152 extern PGDLLIMPORT bool zero_damaged_pages;
155 extern PGDLLIMPORT bool track_io_timing;
156 
157 /* only applicable when prefetching is available */
158 #ifdef USE_PREFETCH
159 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 1
160 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 10
161 #else
162 #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 0
163 #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 0
164 #endif
167 
168 #define MAX_IO_COMBINE_LIMIT PG_IOV_MAX
169 #define DEFAULT_IO_COMBINE_LIMIT Min(MAX_IO_COMBINE_LIMIT, (128 * 1024) / BLCKSZ)
170 extern PGDLLIMPORT int io_combine_limit;
171 
175 
176 /* in buf_init.c */
177 extern PGDLLIMPORT char *BufferBlocks;
178 
179 /* in localbuf.c */
180 extern PGDLLIMPORT int NLocBuffer;
183 
184 /* upper limit for effective_io_concurrency */
185 #define MAX_IO_CONCURRENCY 1000
186 
187 /* special block number for ReadBuffer() */
188 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
189 
190 /*
191  * Buffer content lock modes (mode argument for LockBuffer())
192  */
193 #define BUFFER_LOCK_UNLOCK 0
194 #define BUFFER_LOCK_SHARE 1
195 #define BUFFER_LOCK_EXCLUSIVE 2
196 
197 
198 /*
199  * prototypes for functions in bufmgr.c
200  */
202  ForkNumber forkNum,
203  BlockNumber blockNum);
205  BlockNumber blockNum);
206 extern bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum,
207  BlockNumber blockNum, Buffer recent_buffer);
208 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
209 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
210  BlockNumber blockNum, ReadBufferMode mode,
211  BufferAccessStrategy strategy);
213  ForkNumber forkNum, BlockNumber blockNum,
215  bool permanent);
216 
217 extern bool StartReadBuffer(ReadBuffersOperation *operation,
218  Buffer *buffer,
219  BlockNumber blocknum,
220  int flags);
221 extern bool StartReadBuffers(ReadBuffersOperation *operation,
222  Buffer *buffers,
223  BlockNumber blockNum,
224  int *nblocks,
225  int flags);
226 extern void WaitReadBuffers(ReadBuffersOperation *operation);
227 
228 extern void ReleaseBuffer(Buffer buffer);
229 extern void UnlockReleaseBuffer(Buffer buffer);
230 extern bool BufferIsExclusiveLocked(Buffer buffer);
231 extern bool BufferIsDirty(Buffer buffer);
232 extern void MarkBufferDirty(Buffer buffer);
233 extern void IncrBufferRefCount(Buffer buffer);
234 extern void CheckBufferIsPinnedOnce(Buffer buffer);
235 extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
236  BlockNumber blockNum);
237 
239  ForkNumber forkNum,
240  BufferAccessStrategy strategy,
241  uint32 flags);
243  ForkNumber fork,
244  BufferAccessStrategy strategy,
245  uint32 flags,
246  uint32 extend_by,
247  Buffer *buffers,
248  uint32 *extended_by);
250  ForkNumber fork,
251  BufferAccessStrategy strategy,
252  uint32 flags,
253  BlockNumber extend_to,
255 
256 extern void InitBufferPoolAccess(void);
257 extern void AtEOXact_Buffers(bool isCommit);
258 extern char *DebugPrintBufferRefcount(Buffer buffer);
259 extern void CheckPointBuffers(int flags);
262  ForkNumber forkNum);
263 extern void FlushOneBuffer(Buffer buffer);
264 extern void FlushRelationBuffers(Relation rel);
265 extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
266 extern void CreateAndCopyRelationData(RelFileLocator src_rlocator,
267  RelFileLocator dst_rlocator,
268  bool permanent);
269 extern void FlushDatabaseBuffers(Oid dbid);
270 extern void DropRelationBuffers(struct SMgrRelationData *smgr_reln,
271  ForkNumber *forkNum,
272  int nforks, BlockNumber *firstDelBlock);
273 extern void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln,
274  int nlocators);
275 extern void DropDatabaseBuffers(Oid dbid);
276 
277 #define RelationGetNumberOfBlocks(reln) \
278  RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
279 
280 extern bool BufferIsPermanent(Buffer buffer);
281 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
282 
283 #ifdef NOT_USED
284 extern void PrintPinnedBufs(void);
285 #endif
286 extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator,
287  ForkNumber *forknum, BlockNumber *blknum);
288 
289 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
290 
291 extern void UnlockBuffers(void);
292 extern void LockBuffer(Buffer buffer, int mode);
293 extern bool ConditionalLockBuffer(Buffer buffer);
294 extern void LockBufferForCleanup(Buffer buffer);
295 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
296 extern bool IsBufferCleanupOK(Buffer buffer);
297 extern bool HoldingBufferPinThatDelaysRecovery(void);
298 
299 extern bool BgBufferSync(struct WritebackContext *wb_context);
300 
301 extern void LimitAdditionalPins(uint32 *additional_pins);
302 extern void LimitAdditionalLocalPins(uint32 *additional_pins);
303 
304 extern bool EvictUnpinnedBuffer(Buffer buf);
305 
306 /* in buf_init.c */
307 extern void InitBufferPool(void);
308 extern Size BufferShmemSize(void);
309 
310 /* in localbuf.c */
311 extern void AtProcExit_LocalBuffers(void);
312 
313 /* in freelist.c */
314 
317  int ring_size_kb);
320 
321 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
322 
323 
324 /* inline functions */
325 
326 /*
327  * Although this header file is nominally backend-only, certain frontend
328  * programs like pg_waldump include it. For compilers that emit static
329  * inline functions even when they're unused, that leads to unsatisfied
330  * external references; hence hide these with #ifndef FRONTEND.
331  */
332 
333 #ifndef FRONTEND
334 
335 /*
336  * BufferIsValid
337  * True iff the given buffer number is valid (either as a shared
338  * or local buffer).
339  *
340  * Note: For a long time this was defined the same as BufferIsPinned,
341  * that is it would say False if you didn't hold a pin on the buffer.
342  * I believe this was bogus and served only to mask logic errors.
343  * Code should always know whether it has a buffer reference,
344  * independently of the pin state.
345  *
346  * Note: For a further long time this was not quite the inverse of the
347  * BufferIsInvalid() macro, in that it also did sanity checks to verify
348  * that the buffer number was in range. Most likely, this macro was
349  * originally intended only to be used in assertions, but its use has
350  * since expanded quite a bit, and the overhead of making those checks
351  * even in non-assert-enabled builds can be significant. Thus, we've
352  * now demoted the range checks to assertions within the macro itself.
353  */
354 static inline bool
356 {
357  Assert(bufnum <= NBuffers);
358  Assert(bufnum >= -NLocBuffer);
359 
360  return bufnum != InvalidBuffer;
361 }
362 
363 /*
364  * BufferGetBlock
365  * Returns a reference to a disk page image associated with a buffer.
366  *
367  * Note:
368  * Assumes buffer is valid.
369  */
370 static inline Block
372 {
373  Assert(BufferIsValid(buffer));
374 
375  if (BufferIsLocal(buffer))
376  return LocalBufferBlockPointers[-buffer - 1];
377  else
378  return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
379 }
380 
381 /*
382  * BufferGetPageSize
383  * Returns the page size within a buffer.
384  *
385  * Notes:
386  * Assumes buffer is valid.
387  *
388  * The buffer can be a raw disk block and need not contain a valid
389  * (formatted) disk page.
390  */
391 /* XXX should dig out of buffer descriptor */
392 static inline Size
394 {
395  AssertMacro(BufferIsValid(buffer));
396  return (Size) BLCKSZ;
397 }
398 
399 /*
400  * BufferGetPage
401  * Returns the page associated with a buffer.
402  */
403 static inline Page
405 {
406  return (Page) BufferGetBlock(buffer);
407 }
408 
409 #endif /* FRONTEND */
410 
411 #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:34
@ BAS_BULKREAD
Definition: bufmgr.h:36
@ BAS_NORMAL
Definition: bufmgr.h:35
@ BAS_VACUUM
Definition: bufmgr.h:39
@ BAS_BULKWRITE
Definition: bufmgr.h:38
void CheckBufferIsPinnedOnce(Buffer buffer)
Definition: bufmgr.c:5178
struct BufferManagerRelation BufferManagerRelation
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:4928
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:4375
bool BgBufferSync(struct WritebackContext *wb_context)
Definition: bufmgr.c:3177
PGDLLIMPORT int effective_io_concurrency
Definition: bufmgr.c:150
bool BufferIsExclusiveLocked(Buffer buffer)
Definition: bufmgr.c:2459
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3713
void DropRelationBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:2583
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:638
int GetAccessStrategyPinLimit(BufferAccessStrategy strategy)
Definition: freelist.c:647
bool StartReadBuffers(ReadBuffersOperation *operation, Buffer *buffers, BlockNumber blockNum, int *nblocks, int flags)
Definition: bufmgr.c:1352
PGDLLIMPORT int bgwriter_flush_after
Definition: bufmgr.c:171
Size BufferShmemSize(void)
Definition: buf_init.c:160
PGDLLIMPORT bool zero_damaged_pages
Definition: bufmgr.c:139
PGDLLIMPORT Block * LocalBufferBlockPointers
Definition: localbuf.c:45
bool IsBufferCleanupOK(Buffer buffer)
Definition: bufmgr.c:5428
PGDLLIMPORT int bgwriter_lru_maxpages
Definition: bufmgr.c:140
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:845
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:404
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3548
char * DebugPrintBufferRefcount(Buffer buffer)
Definition: bufmgr.c:3654
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:877
struct PrefetchBufferResult PrefetchBufferResult
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:371
void CreateAndCopyRelationData(RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent)
Definition: bufmgr.c:4770
PGDLLIMPORT int maintenance_io_concurrency
Definition: bufmgr.c:157
Buffer ExtendBufferedRelTo(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, BlockNumber extend_to, ReadBufferMode mode)
Definition: bufmgr.c:909
void AtProcExit_LocalBuffers(void)
Definition: localbuf.c:830
PGDLLIMPORT bool track_io_timing
Definition: bufmgr.c:142
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:3734
PGDLLIMPORT int NBuffers
Definition: globals.c:140
void LimitAdditionalLocalPins(uint32 *additional_pins)
Definition: localbuf.c:290
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:3699
bool BufferIsDirty(Buffer buffer)
Definition: bufmgr.c:2488
void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln, int nlocators)
bool BufferIsPermanent(Buffer buffer)
Definition: bufmgr.c:3944
void UnlockBuffers(void)
Definition: bufmgr.c:5103
void * Block
Definition: bufmgr.h:25
int GetAccessStrategyBufferCount(BufferAccessStrategy strategy)
Definition: freelist.c:624
static Size BufferGetPageSize(Buffer buffer)
Definition: bufmgr.h:393
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:5157
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:3912
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4896
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:681
PGDLLIMPORT int32 * LocalRefCount
Definition: localbuf.c:46
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3974
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:5346
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4913
void WaitReadBuffers(ReadBuffersOperation *operation)
Definition: bufmgr.c:1395
PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln, ForkNumber forkNum, BlockNumber blockNum)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2520
void InitBufferPoolAccess(void)
Definition: bufmgr.c:3565
PGDLLIMPORT int io_combine_limit
Definition: bufmgr.c:164
void LimitAdditionalPins(uint32 *additional_pins)
Definition: bufmgr.c:2104
PGDLLIMPORT int NLocBuffer
Definition: localbuf.c:42
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:5211
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5131
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:4960
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:4481
ExtendBufferedFlags
Definition: bufmgr.h:68
@ EB_LOCK_TARGET
Definition: bufmgr.h:92
@ EB_CLEAR_SIZE_CACHE
Definition: bufmgr.h:89
@ EB_PERFORMING_RECOVERY
Definition: bufmgr.h:77
@ EB_CREATE_FORK_IF_NEEDED
Definition: bufmgr.h:83
@ EB_SKIP_EXTENSION_LOCK
Definition: bufmgr.h:74
@ EB_LOCK_FIRST
Definition: bufmgr.h:86
Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
Definition: bufmgr.c:829
PGDLLIMPORT int backend_flush_after
Definition: bufmgr.c:172
PGDLLIMPORT double bgwriter_lru_multiplier
Definition: bufmgr.c:141
bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, Buffer recent_buffer)
Definition: bufmgr.c:669
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:4834
void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels)
bool EvictUnpinnedBuffer(Buffer buf)
Definition: bufmgr.c:6069
PGDLLIMPORT int checkpoint_flush_after
Definition: bufmgr.c:170
bool StartReadBuffer(ReadBuffersOperation *operation, Buffer *buffer, BlockNumber blocknum, int flags)
Definition: bufmgr.c:1367
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:792
PGDLLIMPORT char * BufferBlocks
Definition: buf_init.c:22
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:745
ReadBufferMode
Definition: bufmgr.h:44
@ RBM_ZERO_ON_ERROR
Definition: bufmgr.h:50
@ RBM_ZERO_AND_CLEANUP_LOCK
Definition: bufmgr.h:48
@ RBM_ZERO_AND_LOCK
Definition: bufmgr.h:46
@ RBM_NORMAL
Definition: bufmgr.h:45
@ RBM_NORMAL_NO_LOG
Definition: bufmgr.h:51
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:5372
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:4876
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:355
Pointer Page
Definition: bufpage.h:78
unsigned int uint32
Definition: c.h:506
#define AssertMacro(condition)
Definition: c.h:859
#define PGDLLIMPORT
Definition: c.h:1316
signed short int16
Definition: c.h:493
signed int int32
Definition: c.h:494
#define Assert(condition)
Definition: c.h:858
size_t Size
Definition: c.h:605
static PgChecksumMode mode
Definition: pg_checksums.c:56
static char * buf
Definition: pg_test_fsync.c:73
unsigned int Oid
Definition: postgres_ext.h:31
ForkNumber
Definition: relpath.h:48
struct SMgrRelationData * smgr
Definition: bufmgr.h:103
Buffer recent_buffer
Definition: bufmgr.h:60
ForkNumber forknum
Definition: bufmgr.h:125
int16 io_buffers_len
Definition: bufmgr.h:137
Buffer * buffers
Definition: bufmgr.h:133
BufferAccessStrategy strategy
Definition: bufmgr.h:126
BlockNumber blocknum
Definition: bufmgr.h:134
struct SMgrRelationData * smgr
Definition: bufmgr.h:123
uint64 XLogRecPtr
Definition: xlogdefs.h:21