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 : : /*
97 : : * Some functions identify relations either by relation or smgr +
98 : : * relpersistence. Used via the BMR_REL()/BMR_SMGR() macros below. This
99 : : * allows us to use the same function for both recovery and normal operation.
100 : : */
101 : : typedef struct BufferManagerRelation
102 : : {
103 : : Relation rel;
104 : : struct SMgrRelationData *smgr;
105 : : char relpersistence;
106 : : } BufferManagerRelation;
107 : :
108 : : #define BMR_REL(p_rel) ((BufferManagerRelation){.rel = p_rel})
109 : : #define BMR_SMGR(p_smgr, p_relpersistence) ((BufferManagerRelation){.smgr = p_smgr, .relpersistence = p_relpersistence})
110 : :
111 : : /* Zero out page if reading fails. */
112 : : #define READ_BUFFERS_ZERO_ON_ERROR (1 << 0)
113 : : /* Call smgrprefetch() if I/O necessary. */
114 : : #define READ_BUFFERS_ISSUE_ADVICE (1 << 1)
115 : : /* Don't treat page as invalid due to checksum failures. */
116 : : #define READ_BUFFERS_IGNORE_CHECKSUM_FAILURES (1 << 2)
117 : : /* IO will immediately be waited for */
118 : : #define READ_BUFFERS_SYNCHRONOUSLY (1 << 3)
119 : :
120 : :
121 : : struct ReadBuffersOperation
122 : : {
123 : : /* The following members should be set by the caller. */
124 : : Relation rel; /* optional */
125 : : struct SMgrRelationData *smgr;
126 : : char persistence;
127 : : ForkNumber forknum;
128 : : BufferAccessStrategy strategy;
129 : :
130 : : /*
131 : : * The following private members are private state for communication
132 : : * between StartReadBuffers() and WaitReadBuffers(), initialized only if
133 : : * an actual read is required, and should not be modified.
134 : : */
135 : : Buffer *buffers;
136 : : BlockNumber blocknum;
137 : : int flags;
138 : : int16 nblocks;
139 : : int16 nblocks_done;
140 : : PgAioWaitRef io_wref;
141 : : PgAioReturn io_return;
142 : : };
143 : :
144 : : typedef struct ReadBuffersOperation ReadBuffersOperation;
145 : :
146 : : /* forward declared, to avoid having to expose buf_internals.h here */
147 : : struct WritebackContext;
148 : :
149 : : /* forward declared, to avoid including smgr.h here */
150 : : struct SMgrRelationData;
151 : :
152 : : /* in globals.c ... this duplicates miscadmin.h */
153 : : extern PGDLLIMPORT int NBuffers;
154 : :
155 : : /* in bufmgr.c */
156 : : extern PGDLLIMPORT bool zero_damaged_pages;
157 : : extern PGDLLIMPORT int bgwriter_lru_maxpages;
158 : : extern PGDLLIMPORT double bgwriter_lru_multiplier;
159 : : extern PGDLLIMPORT bool track_io_timing;
160 : :
161 : : #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 16
162 : : #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 16
163 : : extern PGDLLIMPORT int effective_io_concurrency;
164 : : extern PGDLLIMPORT int maintenance_io_concurrency;
165 : :
166 : : #define MAX_IO_COMBINE_LIMIT PG_IOV_MAX
167 : : #define DEFAULT_IO_COMBINE_LIMIT Min(MAX_IO_COMBINE_LIMIT, (128 * 1024) / BLCKSZ)
168 : : extern PGDLLIMPORT int io_combine_limit; /* min of the two GUCs below */
169 : : extern PGDLLIMPORT int io_combine_limit_guc;
170 : : extern PGDLLIMPORT int io_max_combine_limit;
171 : :
172 : : extern PGDLLIMPORT int checkpoint_flush_after;
173 : : extern PGDLLIMPORT int backend_flush_after;
174 : : extern PGDLLIMPORT int bgwriter_flush_after;
175 : :
176 : : extern PGDLLIMPORT const PgAioHandleCallbacks aio_shared_buffer_readv_cb;
177 : : extern PGDLLIMPORT const PgAioHandleCallbacks aio_local_buffer_readv_cb;
178 : :
179 : : /* in buf_init.c */
180 : : extern PGDLLIMPORT char *BufferBlocks;
181 : :
182 : : /* in localbuf.c */
183 : : extern PGDLLIMPORT int NLocBuffer;
184 : : extern PGDLLIMPORT Block *LocalBufferBlockPointers;
185 : : extern PGDLLIMPORT int32 *LocalRefCount;
186 : :
187 : : /* upper limit for effective_io_concurrency */
188 : : #define MAX_IO_CONCURRENCY 1000
189 : :
190 : : /* special block number for ReadBuffer() */
191 : : #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
192 : :
193 : : /*
194 : : * Buffer content lock modes (mode argument for LockBuffer())
195 : : */
196 : : #define BUFFER_LOCK_UNLOCK 0
197 : : #define BUFFER_LOCK_SHARE 1
198 : : #define BUFFER_LOCK_EXCLUSIVE 2
199 : :
200 : :
201 : : /*
202 : : * prototypes for functions in bufmgr.c
203 : : */
204 : : extern PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln,
205 : : ForkNumber forkNum,
206 : : BlockNumber blockNum);
207 : : extern PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum,
208 : : BlockNumber blockNum);
209 : : extern bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum,
210 : : BlockNumber blockNum, Buffer recent_buffer);
211 : : extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
212 : : extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
213 : : BlockNumber blockNum, ReadBufferMode mode,
214 : : BufferAccessStrategy strategy);
215 : : extern Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator,
216 : : ForkNumber forkNum, BlockNumber blockNum,
217 : : ReadBufferMode mode, BufferAccessStrategy strategy,
218 : : bool permanent);
219 : :
220 : : extern bool StartReadBuffer(ReadBuffersOperation *operation,
221 : : Buffer *buffer,
222 : : BlockNumber blocknum,
223 : : int flags);
224 : : extern bool StartReadBuffers(ReadBuffersOperation *operation,
225 : : Buffer *buffers,
226 : : BlockNumber blockNum,
227 : : int *nblocks,
228 : : int flags);
229 : : extern void WaitReadBuffers(ReadBuffersOperation *operation);
230 : :
231 : : extern void ReleaseBuffer(Buffer buffer);
232 : : extern void UnlockReleaseBuffer(Buffer buffer);
233 : : extern bool BufferIsExclusiveLocked(Buffer buffer);
234 : : extern bool BufferIsDirty(Buffer buffer);
235 : : extern void MarkBufferDirty(Buffer buffer);
236 : : extern void IncrBufferRefCount(Buffer buffer);
237 : : extern void CheckBufferIsPinnedOnce(Buffer buffer);
238 : : extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
239 : : BlockNumber blockNum);
240 : :
241 : : extern Buffer ExtendBufferedRel(BufferManagerRelation bmr,
242 : : ForkNumber forkNum,
243 : : BufferAccessStrategy strategy,
244 : : uint32 flags);
245 : : extern BlockNumber ExtendBufferedRelBy(BufferManagerRelation bmr,
246 : : ForkNumber fork,
247 : : BufferAccessStrategy strategy,
248 : : uint32 flags,
249 : : uint32 extend_by,
250 : : Buffer *buffers,
251 : : uint32 *extended_by);
252 : : extern Buffer ExtendBufferedRelTo(BufferManagerRelation bmr,
253 : : ForkNumber fork,
254 : : BufferAccessStrategy strategy,
255 : : uint32 flags,
256 : : BlockNumber extend_to,
257 : : ReadBufferMode mode);
258 : :
259 : : extern void InitBufferManagerAccess(void);
260 : : extern void AtEOXact_Buffers(bool isCommit);
261 : : #ifdef USE_ASSERT_CHECKING
262 : : extern void AssertBufferLocksPermitCatalogRead(void);
263 : : #endif
264 : : extern char *DebugPrintBufferRefcount(Buffer buffer);
265 : : extern void CheckPointBuffers(int flags);
266 : : extern BlockNumber BufferGetBlockNumber(Buffer buffer);
267 : : extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
268 : : ForkNumber forkNum);
269 : : extern void FlushOneBuffer(Buffer buffer);
270 : : extern void FlushRelationBuffers(Relation rel);
271 : : extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
272 : : extern void CreateAndCopyRelationData(RelFileLocator src_rlocator,
273 : : RelFileLocator dst_rlocator,
274 : : bool permanent);
275 : : extern void FlushDatabaseBuffers(Oid dbid);
276 : : extern void DropRelationBuffers(struct SMgrRelationData *smgr_reln,
277 : : ForkNumber *forkNum,
278 : : int nforks, BlockNumber *firstDelBlock);
279 : : extern void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln,
280 : : int nlocators);
281 : : extern void DropDatabaseBuffers(Oid dbid);
282 : :
283 : : #define RelationGetNumberOfBlocks(reln) \
284 : : RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
285 : :
286 : : extern bool BufferIsPermanent(Buffer buffer);
287 : : extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
288 : : extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator,
289 : : ForkNumber *forknum, BlockNumber *blknum);
290 : :
291 : : extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
292 : :
293 : : extern void UnlockBuffers(void);
294 : : extern void LockBuffer(Buffer buffer, int mode);
295 : : extern bool ConditionalLockBuffer(Buffer buffer);
296 : : extern void LockBufferForCleanup(Buffer buffer);
297 : : extern bool ConditionalLockBufferForCleanup(Buffer buffer);
298 : : extern bool IsBufferCleanupOK(Buffer buffer);
299 : : extern bool HoldingBufferPinThatDelaysRecovery(void);
300 : :
301 : : extern bool BgBufferSync(struct WritebackContext *wb_context);
302 : :
303 : : extern uint32 GetPinLimit(void);
304 : : extern uint32 GetLocalPinLimit(void);
305 : : extern uint32 GetAdditionalPinLimit(void);
306 : : extern uint32 GetAdditionalLocalPinLimit(void);
307 : : extern void LimitAdditionalPins(uint32 *additional_pins);
308 : : extern void LimitAdditionalLocalPins(uint32 *additional_pins);
309 : :
310 : : extern bool EvictUnpinnedBuffer(Buffer buf, bool *buffer_flushed);
311 : : extern void EvictAllUnpinnedBuffers(int32 *buffers_evicted,
312 : : int32 *buffers_flushed,
313 : : int32 *buffers_skipped);
314 : : extern void EvictRelUnpinnedBuffers(Relation rel,
315 : : int32 *buffers_evicted,
316 : : int32 *buffers_flushed,
317 : : int32 *buffers_skipped);
318 : :
319 : : /* in buf_init.c */
320 : : extern void BufferManagerShmemInit(void);
321 : : extern Size BufferManagerShmemSize(void);
322 : :
323 : : /* in localbuf.c */
324 : : extern void AtProcExit_LocalBuffers(void);
325 : :
326 : : /* in freelist.c */
327 : :
328 : : extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
329 : : extern BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype,
330 : : int ring_size_kb);
331 : : extern int GetAccessStrategyBufferCount(BufferAccessStrategy strategy);
332 : : extern int GetAccessStrategyPinLimit(BufferAccessStrategy strategy);
333 : :
334 : : extern void FreeAccessStrategy(BufferAccessStrategy strategy);
335 : :
336 : :
337 : : /* inline functions */
338 : :
339 : : /*
340 : : * Although this header file is nominally backend-only, certain frontend
341 : : * programs like pg_waldump include it. For compilers that emit static
342 : : * inline functions even when they're unused, that leads to unsatisfied
343 : : * external references; hence hide these with #ifndef FRONTEND.
344 : : */
345 : :
346 : : #ifndef FRONTEND
347 : :
348 : : /*
349 : : * BufferIsValid
350 : : * True iff the given buffer number is valid (either as a shared
351 : : * or local buffer).
352 : : *
353 : : * Note: For a long time this was defined the same as BufferIsPinned,
354 : : * that is it would say False if you didn't hold a pin on the buffer.
355 : : * I believe this was bogus and served only to mask logic errors.
356 : : * Code should always know whether it has a buffer reference,
357 : : * independently of the pin state.
358 : : *
359 : : * Note: For a further long time this was not quite the inverse of the
360 : : * BufferIsInvalid() macro, in that it also did sanity checks to verify
361 : : * that the buffer number was in range. Most likely, this macro was
362 : : * originally intended only to be used in assertions, but its use has
363 : : * since expanded quite a bit, and the overhead of making those checks
364 : : * even in non-assert-enabled builds can be significant. Thus, we've
365 : : * now demoted the range checks to assertions within the macro itself.
366 : : */
367 : : static inline bool
1151 tgl@sss.pgh.pa.us 368 :CBC 2173878828 : BufferIsValid(Buffer bufnum)
369 : : {
370 [ - + ]: 2173878828 : Assert(bufnum <= NBuffers);
371 [ - + ]: 2173878828 : Assert(bufnum >= -NLocBuffer);
372 : :
373 : 2173878828 : return bufnum != InvalidBuffer;
374 : : }
375 : :
376 : : /*
377 : : * BufferGetBlock
378 : : * Returns a reference to a disk page image associated with a buffer.
379 : : *
380 : : * Note:
381 : : * Assumes buffer is valid.
382 : : */
383 : : static inline Block
384 : 347226375 : BufferGetBlock(Buffer buffer)
385 : : {
386 [ - + ]: 347226375 : Assert(BufferIsValid(buffer));
387 : :
388 [ + + ]: 347226375 : if (BufferIsLocal(buffer))
389 : 13631483 : return LocalBufferBlockPointers[-buffer - 1];
390 : : else
391 : 333594892 : return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
392 : : }
393 : :
394 : : /*
395 : : * BufferGetPageSize
396 : : * Returns the page size within a buffer.
397 : : *
398 : : * Notes:
399 : : * Assumes buffer is valid.
400 : : *
401 : : * The buffer can be a raw disk block and need not contain a valid
402 : : * (formatted) disk page.
403 : : */
404 : : /* XXX should dig out of buffer descriptor */
405 : : static inline Size
406 : 228711 : BufferGetPageSize(Buffer buffer)
407 : : {
207 peter@eisentraut.org 408 [ - + ]: 228711 : Assert(BufferIsValid(buffer));
1151 tgl@sss.pgh.pa.us 409 : 228711 : return (Size) BLCKSZ;
410 : : }
411 : :
412 : : /*
413 : : * BufferGetPage
414 : : * Returns the page associated with a buffer.
415 : : */
416 : : static inline Page
417 : 344424881 : BufferGetPage(Buffer buffer)
418 : : {
419 : 344424881 : return (Page) BufferGetBlock(buffer);
420 : : }
421 : :
422 : : #endif /* FRONTEND */
423 : :
424 : : #endif /* BUFMGR_H */
|