Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * bufmgr.h
4 : : * POSTGRES buffer manager definitions.
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2025, 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/aio_types.h"
19 : : #include "storage/block.h"
20 : : #include "storage/buf.h"
21 : : #include "storage/bufpage.h"
22 : : #include "storage/relfilelocator.h"
23 : : #include "utils/relcache.h"
24 : : #include "utils/snapmgr.h"
25 : :
26 : : typedef void *Block;
27 : :
28 : : /*
29 : : * Possible arguments for GetAccessStrategy().
30 : : *
31 : : * If adding a new BufferAccessStrategyType, also add a new IOContext so
32 : : * IO statistics using this strategy are tracked.
33 : : */
34 : : typedef enum BufferAccessStrategyType
35 : : {
36 : : BAS_NORMAL, /* Normal random access */
37 : : BAS_BULKREAD, /* Large read-only scan (hint bit updates are
38 : : * ok) */
39 : : BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
40 : : BAS_VACUUM, /* VACUUM */
41 : : } BufferAccessStrategyType;
42 : :
43 : : /* Possible modes for ReadBufferExtended() */
44 : : typedef enum
45 : : {
46 : : RBM_NORMAL, /* Normal read */
47 : : RBM_ZERO_AND_LOCK, /* Don't read from disk, caller will
48 : : * initialize. Also locks the page. */
49 : : RBM_ZERO_AND_CLEANUP_LOCK, /* Like RBM_ZERO_AND_LOCK, but locks the page
50 : : * in "cleanup" mode */
51 : : RBM_ZERO_ON_ERROR, /* Read, but return an all-zeros page on error */
52 : : RBM_NORMAL_NO_LOG, /* Don't log page as invalid during WAL
53 : : * replay; otherwise same as RBM_NORMAL */
54 : : } ReadBufferMode;
55 : :
56 : : /*
57 : : * Type returned by PrefetchBuffer().
58 : : */
59 : : typedef struct PrefetchBufferResult
60 : : {
61 : : Buffer recent_buffer; /* If valid, a hit (recheck needed!) */
62 : : bool initiated_io; /* If true, a miss resulting in async I/O */
63 : : } PrefetchBufferResult;
64 : :
65 : : /*
66 : : * Flags influencing the behaviour of ExtendBufferedRel*
67 : : */
68 : : typedef enum ExtendBufferedFlags
69 : : {
70 : : /*
71 : : * Don't acquire extension lock. This is safe only if the relation isn't
72 : : * shared, an access exclusive lock is held or if this is the startup
73 : : * process.
74 : : */
75 : : EB_SKIP_EXTENSION_LOCK = (1 << 0),
76 : :
77 : : /* Is this extension part of recovery? */
78 : : EB_PERFORMING_RECOVERY = (1 << 1),
79 : :
80 : : /*
81 : : * Should the fork be created if it does not currently exist? This likely
82 : : * only ever makes sense for relation forks.
83 : : */
84 : : EB_CREATE_FORK_IF_NEEDED = (1 << 2),
85 : :
86 : : /* Should the first (possibly only) return buffer be returned locked? */
87 : : EB_LOCK_FIRST = (1 << 3),
88 : :
89 : : /* Should the smgr size cache be cleared? */
90 : : EB_CLEAR_SIZE_CACHE = (1 << 4),
91 : :
92 : : /* internal flags follow */
93 : : EB_LOCK_TARGET = (1 << 5),
94 : : } ExtendBufferedFlags;
95 : :
96 : : /* forward declared, to avoid including smgr.h here */
97 : : typedef struct SMgrRelationData *SMgrRelation;
98 : :
99 : : /*
100 : : * Some functions identify relations either by relation or smgr +
101 : : * relpersistence, initialized via the BMR_REL()/BMR_SMGR() macros below.
102 : : * This allows us to use the same function for both recovery and normal
103 : : * operation. When BMR_REL is used, it's not valid to cache its rd_smgr here,
104 : : * because our pointer would be obsolete in case of relcache invalidation.
105 : : * For simplicity, use BMR_GET_SMGR to read the smgr.
106 : : */
107 : : typedef struct BufferManagerRelation
108 : : {
109 : : Relation rel;
110 : : SMgrRelation smgr;
111 : : char relpersistence;
112 : : } BufferManagerRelation;
113 : :
114 : : #define BMR_REL(p_rel) \
115 : : ((BufferManagerRelation){.rel = p_rel})
116 : : #define BMR_SMGR(p_smgr, p_relpersistence) \
117 : : ((BufferManagerRelation){.smgr = p_smgr, .relpersistence = p_relpersistence})
118 : : #define BMR_GET_SMGR(bmr) \
119 : : (RelationIsValid((bmr).rel) ? RelationGetSmgr((bmr).rel) : (bmr).smgr)
120 : :
121 : : /* Zero out page if reading fails. */
122 : : #define READ_BUFFERS_ZERO_ON_ERROR (1 << 0)
123 : : /* Call smgrprefetch() if I/O necessary. */
124 : : #define READ_BUFFERS_ISSUE_ADVICE (1 << 1)
125 : : /* Don't treat page as invalid due to checksum failures. */
126 : : #define READ_BUFFERS_IGNORE_CHECKSUM_FAILURES (1 << 2)
127 : : /* IO will immediately be waited for */
128 : : #define READ_BUFFERS_SYNCHRONOUSLY (1 << 3)
129 : :
130 : :
131 : : struct ReadBuffersOperation
132 : : {
133 : : /* The following members should be set by the caller. */
134 : : Relation rel; /* optional */
135 : : SMgrRelation smgr;
136 : : char persistence;
137 : : ForkNumber forknum;
138 : : BufferAccessStrategy strategy;
139 : :
140 : : /*
141 : : * The following private members are private state for communication
142 : : * between StartReadBuffers() and WaitReadBuffers(), initialized only if
143 : : * an actual read is required, and should not be modified.
144 : : */
145 : : Buffer *buffers;
146 : : BlockNumber blocknum;
147 : : int flags;
148 : : int16 nblocks;
149 : : int16 nblocks_done;
150 : : PgAioWaitRef io_wref;
151 : : PgAioReturn io_return;
152 : : };
153 : :
154 : : typedef struct ReadBuffersOperation ReadBuffersOperation;
155 : :
156 : : /* to avoid having to expose buf_internals.h here */
157 : : typedef struct WritebackContext WritebackContext;
158 : :
159 : : /* in globals.c ... this duplicates miscadmin.h */
160 : : extern PGDLLIMPORT int NBuffers;
161 : :
162 : : /* in bufmgr.c */
163 : : extern PGDLLIMPORT bool zero_damaged_pages;
164 : : extern PGDLLIMPORT int bgwriter_lru_maxpages;
165 : : extern PGDLLIMPORT double bgwriter_lru_multiplier;
166 : : extern PGDLLIMPORT bool track_io_timing;
167 : :
168 : : #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 16
169 : : #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 16
170 : : extern PGDLLIMPORT int effective_io_concurrency;
171 : : extern PGDLLIMPORT int maintenance_io_concurrency;
172 : :
173 : : #define MAX_IO_COMBINE_LIMIT PG_IOV_MAX
174 : : #define DEFAULT_IO_COMBINE_LIMIT Min(MAX_IO_COMBINE_LIMIT, (128 * 1024) / BLCKSZ)
175 : : extern PGDLLIMPORT int io_combine_limit; /* min of the two GUCs below */
176 : : extern PGDLLIMPORT int io_combine_limit_guc;
177 : : extern PGDLLIMPORT int io_max_combine_limit;
178 : :
179 : : extern PGDLLIMPORT int checkpoint_flush_after;
180 : : extern PGDLLIMPORT int backend_flush_after;
181 : : extern PGDLLIMPORT int bgwriter_flush_after;
182 : :
183 : : extern PGDLLIMPORT const PgAioHandleCallbacks aio_shared_buffer_readv_cb;
184 : : extern PGDLLIMPORT const PgAioHandleCallbacks aio_local_buffer_readv_cb;
185 : :
186 : : /* in buf_init.c */
187 : : extern PGDLLIMPORT char *BufferBlocks;
188 : :
189 : : /* in localbuf.c */
190 : : extern PGDLLIMPORT int NLocBuffer;
191 : : extern PGDLLIMPORT Block *LocalBufferBlockPointers;
192 : : extern PGDLLIMPORT int32 *LocalRefCount;
193 : :
194 : : /* upper limit for effective_io_concurrency */
195 : : #define MAX_IO_CONCURRENCY 1000
196 : :
197 : : /* special block number for ReadBuffer() */
198 : : #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
199 : :
200 : : /*
201 : : * Buffer content lock modes (mode argument for LockBuffer())
202 : : */
203 : : typedef enum BufferLockMode
204 : : {
205 : : BUFFER_LOCK_UNLOCK,
206 : : BUFFER_LOCK_SHARE,
207 : : BUFFER_LOCK_EXCLUSIVE,
208 : : } BufferLockMode;
209 : :
210 : :
211 : : /*
212 : : * prototypes for functions in bufmgr.c
213 : : */
214 : : extern PrefetchBufferResult PrefetchSharedBuffer(SMgrRelation smgr_reln,
215 : : ForkNumber forkNum,
216 : : BlockNumber blockNum);
217 : : extern PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum,
218 : : BlockNumber blockNum);
219 : : extern bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum,
220 : : BlockNumber blockNum, Buffer recent_buffer);
221 : : extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
222 : : extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
223 : : BlockNumber blockNum, ReadBufferMode mode,
224 : : BufferAccessStrategy strategy);
225 : : extern Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator,
226 : : ForkNumber forkNum, BlockNumber blockNum,
227 : : ReadBufferMode mode, BufferAccessStrategy strategy,
228 : : bool permanent);
229 : :
230 : : extern bool StartReadBuffer(ReadBuffersOperation *operation,
231 : : Buffer *buffer,
232 : : BlockNumber blocknum,
233 : : int flags);
234 : : extern bool StartReadBuffers(ReadBuffersOperation *operation,
235 : : Buffer *buffers,
236 : : BlockNumber blockNum,
237 : : int *nblocks,
238 : : int flags);
239 : : extern void WaitReadBuffers(ReadBuffersOperation *operation);
240 : :
241 : : extern void ReleaseBuffer(Buffer buffer);
242 : : extern void UnlockReleaseBuffer(Buffer buffer);
243 : : extern bool BufferIsLockedByMe(Buffer buffer);
244 : : extern bool BufferIsLockedByMeInMode(Buffer buffer, BufferLockMode mode);
245 : : extern bool BufferIsDirty(Buffer buffer);
246 : : extern void MarkBufferDirty(Buffer buffer);
247 : : extern void IncrBufferRefCount(Buffer buffer);
248 : : extern void CheckBufferIsPinnedOnce(Buffer buffer);
249 : : extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
250 : : BlockNumber blockNum);
251 : :
252 : : extern Buffer ExtendBufferedRel(BufferManagerRelation bmr,
253 : : ForkNumber forkNum,
254 : : BufferAccessStrategy strategy,
255 : : uint32 flags);
256 : : extern BlockNumber ExtendBufferedRelBy(BufferManagerRelation bmr,
257 : : ForkNumber fork,
258 : : BufferAccessStrategy strategy,
259 : : uint32 flags,
260 : : uint32 extend_by,
261 : : Buffer *buffers,
262 : : uint32 *extended_by);
263 : : extern Buffer ExtendBufferedRelTo(BufferManagerRelation bmr,
264 : : ForkNumber fork,
265 : : BufferAccessStrategy strategy,
266 : : uint32 flags,
267 : : BlockNumber extend_to,
268 : : ReadBufferMode mode);
269 : :
270 : : extern void InitBufferManagerAccess(void);
271 : : extern void AtEOXact_Buffers(bool isCommit);
272 : : #ifdef USE_ASSERT_CHECKING
273 : : extern void AssertBufferLocksPermitCatalogRead(void);
274 : : #endif
275 : : extern char *DebugPrintBufferRefcount(Buffer buffer);
276 : : extern void CheckPointBuffers(int flags);
277 : : extern BlockNumber BufferGetBlockNumber(Buffer buffer);
278 : : extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
279 : : ForkNumber forkNum);
280 : : extern void FlushOneBuffer(Buffer buffer);
281 : : extern void FlushRelationBuffers(Relation rel);
282 : : extern void FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels);
283 : : extern void CreateAndCopyRelationData(RelFileLocator src_rlocator,
284 : : RelFileLocator dst_rlocator,
285 : : bool permanent);
286 : : extern void FlushDatabaseBuffers(Oid dbid);
287 : : extern void DropRelationBuffers(SMgrRelation smgr_reln,
288 : : ForkNumber *forkNum,
289 : : int nforks, BlockNumber *firstDelBlock);
290 : : extern void DropRelationsAllBuffers(SMgrRelation *smgr_reln,
291 : : int nlocators);
292 : : extern void DropDatabaseBuffers(Oid dbid);
293 : :
294 : : #define RelationGetNumberOfBlocks(reln) \
295 : : RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
296 : :
297 : : extern bool BufferIsPermanent(Buffer buffer);
298 : : extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
299 : : extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator,
300 : : ForkNumber *forknum, BlockNumber *blknum);
301 : :
302 : : extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
303 : :
304 : : extern void UnlockBuffers(void);
305 : : extern void LockBuffer(Buffer buffer, BufferLockMode mode);
306 : : extern bool ConditionalLockBuffer(Buffer buffer);
307 : : extern void LockBufferForCleanup(Buffer buffer);
308 : : extern bool ConditionalLockBufferForCleanup(Buffer buffer);
309 : : extern bool IsBufferCleanupOK(Buffer buffer);
310 : : extern bool HoldingBufferPinThatDelaysRecovery(void);
311 : :
312 : : extern bool BgBufferSync(WritebackContext *wb_context);
313 : :
314 : : extern uint32 GetPinLimit(void);
315 : : extern uint32 GetLocalPinLimit(void);
316 : : extern uint32 GetAdditionalPinLimit(void);
317 : : extern uint32 GetAdditionalLocalPinLimit(void);
318 : : extern void LimitAdditionalPins(uint32 *additional_pins);
319 : : extern void LimitAdditionalLocalPins(uint32 *additional_pins);
320 : :
321 : : extern bool EvictUnpinnedBuffer(Buffer buf, bool *buffer_flushed);
322 : : extern void EvictAllUnpinnedBuffers(int32 *buffers_evicted,
323 : : int32 *buffers_flushed,
324 : : int32 *buffers_skipped);
325 : : extern void EvictRelUnpinnedBuffers(Relation rel,
326 : : int32 *buffers_evicted,
327 : : int32 *buffers_flushed,
328 : : int32 *buffers_skipped);
329 : : extern bool MarkDirtyUnpinnedBuffer(Buffer buf, bool *buffer_already_dirty);
330 : : extern void MarkDirtyRelUnpinnedBuffers(Relation rel,
331 : : int32 *buffers_dirtied,
332 : : int32 *buffers_already_dirty,
333 : : int32 *buffers_skipped);
334 : : extern void MarkDirtyAllUnpinnedBuffers(int32 *buffers_dirtied,
335 : : int32 *buffers_already_dirty,
336 : : int32 *buffers_skipped);
337 : :
338 : : /* in buf_init.c */
339 : : extern void BufferManagerShmemInit(void);
340 : : extern Size BufferManagerShmemSize(void);
341 : :
342 : : /* in localbuf.c */
343 : : extern void AtProcExit_LocalBuffers(void);
344 : :
345 : : /* in freelist.c */
346 : :
347 : : extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
348 : : extern BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype,
349 : : int ring_size_kb);
350 : : extern int GetAccessStrategyBufferCount(BufferAccessStrategy strategy);
351 : : extern int GetAccessStrategyPinLimit(BufferAccessStrategy strategy);
352 : :
353 : : extern void FreeAccessStrategy(BufferAccessStrategy strategy);
354 : :
355 : :
356 : : /* inline functions */
357 : :
358 : : /*
359 : : * Although this header file is nominally backend-only, certain frontend
360 : : * programs like pg_waldump include it. For compilers that emit static
361 : : * inline functions even when they're unused, that leads to unsatisfied
362 : : * external references; hence hide these with #ifndef FRONTEND.
363 : : */
364 : :
365 : : #ifndef FRONTEND
366 : :
367 : : /*
368 : : * BufferIsValid
369 : : * True iff the given buffer number is valid (either as a shared
370 : : * or local buffer).
371 : : *
372 : : * Note: For a long time this was defined the same as BufferIsPinned,
373 : : * that is it would say False if you didn't hold a pin on the buffer.
374 : : * I believe this was bogus and served only to mask logic errors.
375 : : * Code should always know whether it has a buffer reference,
376 : : * independently of the pin state.
377 : : *
378 : : * Note: For a further long time this was not quite the inverse of the
379 : : * BufferIsInvalid() macro, in that it also did sanity checks to verify
380 : : * that the buffer number was in range. Most likely, this macro was
381 : : * originally intended only to be used in assertions, but its use has
382 : : * since expanded quite a bit, and the overhead of making those checks
383 : : * even in non-assert-enabled builds can be significant. Thus, we've
384 : : * now demoted the range checks to assertions within the macro itself.
385 : : */
386 : : static inline bool
1252 tgl@sss.pgh.pa.us 387 :CBC 2359994395 : BufferIsValid(Buffer bufnum)
388 : : {
389 [ - + ]: 2359994395 : Assert(bufnum <= NBuffers);
390 [ - + ]: 2359994395 : Assert(bufnum >= -NLocBuffer);
391 : :
392 : 2359994395 : return bufnum != InvalidBuffer;
393 : : }
394 : :
395 : : /*
396 : : * BufferGetBlock
397 : : * Returns a reference to a disk page image associated with a buffer.
398 : : *
399 : : * Note:
400 : : * Assumes buffer is valid.
401 : : */
402 : : static inline Block
403 : 355950089 : BufferGetBlock(Buffer buffer)
404 : : {
405 [ - + ]: 355950089 : Assert(BufferIsValid(buffer));
406 : :
407 [ + + ]: 355950089 : if (BufferIsLocal(buffer))
408 : 13643130 : return LocalBufferBlockPointers[-buffer - 1];
409 : : else
410 : 342306959 : return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
411 : : }
412 : :
413 : : /*
414 : : * BufferGetPageSize
415 : : * Returns the page size within a buffer.
416 : : *
417 : : * Notes:
418 : : * Assumes buffer is valid.
419 : : *
420 : : * The buffer can be a raw disk block and need not contain a valid
421 : : * (formatted) disk page.
422 : : */
423 : : /* XXX should dig out of buffer descriptor */
424 : : static inline Size
425 : 232814 : BufferGetPageSize(Buffer buffer)
426 : : {
308 peter@eisentraut.org 427 [ - + ]: 232814 : Assert(BufferIsValid(buffer));
1252 tgl@sss.pgh.pa.us 428 : 232814 : return (Size) BLCKSZ;
429 : : }
430 : :
431 : : /*
432 : : * BufferGetPage
433 : : * Returns the page associated with a buffer.
434 : : */
435 : : static inline Page
436 : 352997261 : BufferGetPage(Buffer buffer)
437 : : {
438 : 352997261 : return (Page) BufferGetBlock(buffer);
439 : : }
440 : :
441 : : #endif /* FRONTEND */
442 : :
443 : : #endif /* BUFMGR_H */
|