LCOV - code coverage report
Current view: top level - src/include/storage - bufmgr.h (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 10 10 100.0 %
Date: 2024-04-27 03:11:33 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             :  */
      33             : typedef enum BufferAccessStrategyType
      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 */
      40             : } BufferAccessStrategyType;
      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 */
      53             : } ReadBufferMode;
      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 */
      62             : } PrefetchBufferResult;
      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             :      */
      74             :     EB_SKIP_EXTENSION_LOCK = (1 << 0),
      75             : 
      76             :     /* Is this extension part of recovery? */
      77             :     EB_PERFORMING_RECOVERY = (1 << 1),
      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             :      */
      83             :     EB_CREATE_FORK_IF_NEEDED = (1 << 2),
      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),
      93             : }           ExtendBufferedFlags;
      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             : {
     102             :     Relation    rel;
     103             :     struct SMgrRelationData *smgr;
     104             :     char        relpersistence;
     105             : } BufferManagerRelation;
     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             : typedef enum ReadBuffersFlags
     111             : {
     112             :     /* Zero out page if reading fails. */
     113             :     READ_BUFFERS_ZERO_ON_ERROR = (1 << 0),
     114             : 
     115             :     /* Call smgrprefetch() if I/O necessary. */
     116             :     READ_BUFFERS_ISSUE_ADVICE = (1 << 1),
     117             : } ReadBuffersFlags;
     118             : 
     119             : struct ReadBuffersOperation
     120             : {
     121             :     /*
     122             :      * The following members should be set by the caller.  If only smgr is
     123             :      * provided without rel, then smgr_persistence can be set to override the
     124             :      * default assumption of RELPERSISTENCE_PERMANENT.
     125             :      */
     126             :     Relation    rel;
     127             :     struct SMgrRelationData *smgr;
     128             :     char        smgr_persistence;
     129             :     ForkNumber  forknum;
     130             :     BufferAccessStrategy strategy;
     131             : 
     132             :     /*
     133             :      * The following private members are private state for communication
     134             :      * between StartReadBuffers() and WaitReadBuffers(), initialized only if
     135             :      * an actual read is required, and should not be modified.
     136             :      */
     137             :     Buffer     *buffers;
     138             :     BlockNumber blocknum;
     139             :     int         flags;
     140             :     int16       nblocks;
     141             :     int16       io_buffers_len;
     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             : /* only applicable when prefetching is available */
     162             : #ifdef USE_PREFETCH
     163             : #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 1
     164             : #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 10
     165             : #else
     166             : #define DEFAULT_EFFECTIVE_IO_CONCURRENCY 0
     167             : #define DEFAULT_MAINTENANCE_IO_CONCURRENCY 0
     168             : #endif
     169             : extern PGDLLIMPORT int effective_io_concurrency;
     170             : extern PGDLLIMPORT int maintenance_io_concurrency;
     171             : 
     172             : #define MAX_IO_COMBINE_LIMIT PG_IOV_MAX
     173             : #define DEFAULT_IO_COMBINE_LIMIT Min(MAX_IO_COMBINE_LIMIT, (128 * 1024) / BLCKSZ)
     174             : extern PGDLLIMPORT int io_combine_limit;
     175             : 
     176             : extern PGDLLIMPORT int checkpoint_flush_after;
     177             : extern PGDLLIMPORT int backend_flush_after;
     178             : extern PGDLLIMPORT int bgwriter_flush_after;
     179             : 
     180             : /* in buf_init.c */
     181             : extern PGDLLIMPORT char *BufferBlocks;
     182             : 
     183             : /* in localbuf.c */
     184             : extern PGDLLIMPORT int NLocBuffer;
     185             : extern PGDLLIMPORT Block *LocalBufferBlockPointers;
     186             : extern PGDLLIMPORT int32 *LocalRefCount;
     187             : 
     188             : /* upper limit for effective_io_concurrency */
     189             : #define MAX_IO_CONCURRENCY 1000
     190             : 
     191             : /* special block number for ReadBuffer() */
     192             : #define P_NEW   InvalidBlockNumber  /* grow the file to get a new page */
     193             : 
     194             : /*
     195             :  * Buffer content lock modes (mode argument for LockBuffer())
     196             :  */
     197             : #define BUFFER_LOCK_UNLOCK      0
     198             : #define BUFFER_LOCK_SHARE       1
     199             : #define BUFFER_LOCK_EXCLUSIVE   2
     200             : 
     201             : 
     202             : /*
     203             :  * prototypes for functions in bufmgr.c
     204             :  */
     205             : extern PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln,
     206             :                                                  ForkNumber forkNum,
     207             :                                                  BlockNumber blockNum);
     208             : extern PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum,
     209             :                                            BlockNumber blockNum);
     210             : extern bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum,
     211             :                              BlockNumber blockNum, Buffer recent_buffer);
     212             : extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
     213             : extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
     214             :                                  BlockNumber blockNum, ReadBufferMode mode,
     215             :                                  BufferAccessStrategy strategy);
     216             : extern Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator,
     217             :                                         ForkNumber forkNum, BlockNumber blockNum,
     218             :                                         ReadBufferMode mode, BufferAccessStrategy strategy,
     219             :                                         bool permanent);
     220             : 
     221             : extern bool StartReadBuffer(ReadBuffersOperation *operation,
     222             :                             Buffer *buffer,
     223             :                             BlockNumber blocknum,
     224             :                             int flags);
     225             : extern bool StartReadBuffers(ReadBuffersOperation *operation,
     226             :                              Buffer *buffers,
     227             :                              BlockNumber blocknum,
     228             :                              int *nblocks,
     229             :                              int flags);
     230             : extern void WaitReadBuffers(ReadBuffersOperation *operation);
     231             : 
     232             : extern void ReleaseBuffer(Buffer buffer);
     233             : extern void UnlockReleaseBuffer(Buffer buffer);
     234             : extern bool BufferIsExclusiveLocked(Buffer buffer);
     235             : extern bool BufferIsDirty(Buffer buffer);
     236             : extern void MarkBufferDirty(Buffer buffer);
     237             : extern void IncrBufferRefCount(Buffer buffer);
     238             : extern void CheckBufferIsPinnedOnce(Buffer buffer);
     239             : extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
     240             :                                    BlockNumber blockNum);
     241             : 
     242             : extern Buffer ExtendBufferedRel(BufferManagerRelation bmr,
     243             :                                 ForkNumber forkNum,
     244             :                                 BufferAccessStrategy strategy,
     245             :                                 uint32 flags);
     246             : extern BlockNumber ExtendBufferedRelBy(BufferManagerRelation bmr,
     247             :                                        ForkNumber fork,
     248             :                                        BufferAccessStrategy strategy,
     249             :                                        uint32 flags,
     250             :                                        uint32 extend_by,
     251             :                                        Buffer *buffers,
     252             :                                        uint32 *extended_by);
     253             : extern Buffer ExtendBufferedRelTo(BufferManagerRelation bmr,
     254             :                                   ForkNumber fork,
     255             :                                   BufferAccessStrategy strategy,
     256             :                                   uint32 flags,
     257             :                                   BlockNumber extend_to,
     258             :                                   ReadBufferMode mode);
     259             : 
     260             : extern void InitBufferPoolAccess(void);
     261             : extern void AtEOXact_Buffers(bool isCommit);
     262             : extern char *DebugPrintBufferRefcount(Buffer buffer);
     263             : extern void CheckPointBuffers(int flags);
     264             : extern BlockNumber BufferGetBlockNumber(Buffer buffer);
     265             : extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
     266             :                                                    ForkNumber forkNum);
     267             : extern void FlushOneBuffer(Buffer buffer);
     268             : extern void FlushRelationBuffers(Relation rel);
     269             : extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
     270             : extern void CreateAndCopyRelationData(RelFileLocator src_rlocator,
     271             :                                       RelFileLocator dst_rlocator,
     272             :                                       bool permanent);
     273             : extern void FlushDatabaseBuffers(Oid dbid);
     274             : extern void DropRelationBuffers(struct SMgrRelationData *smgr_reln,
     275             :                                 ForkNumber *forkNum,
     276             :                                 int nforks, BlockNumber *firstDelBlock);
     277             : extern void DropRelationsAllBuffers(struct SMgrRelationData **smgr_reln,
     278             :                                     int nlocators);
     279             : extern void DropDatabaseBuffers(Oid dbid);
     280             : 
     281             : #define RelationGetNumberOfBlocks(reln) \
     282             :     RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
     283             : 
     284             : extern bool BufferIsPermanent(Buffer buffer);
     285             : extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
     286             : 
     287             : #ifdef NOT_USED
     288             : extern void PrintPinnedBufs(void);
     289             : #endif
     290             : extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator,
     291             :                          ForkNumber *forknum, BlockNumber *blknum);
     292             : 
     293             : extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
     294             : 
     295             : extern void UnlockBuffers(void);
     296             : extern void LockBuffer(Buffer buffer, int mode);
     297             : extern bool ConditionalLockBuffer(Buffer buffer);
     298             : extern void LockBufferForCleanup(Buffer buffer);
     299             : extern bool ConditionalLockBufferForCleanup(Buffer buffer);
     300             : extern bool IsBufferCleanupOK(Buffer buffer);
     301             : extern bool HoldingBufferPinThatDelaysRecovery(void);
     302             : 
     303             : extern bool BgBufferSync(struct WritebackContext *wb_context);
     304             : 
     305             : extern void LimitAdditionalPins(uint32 *additional_pins);
     306             : extern void LimitAdditionalLocalPins(uint32 *additional_pins);
     307             : 
     308             : extern bool EvictUnpinnedBuffer(Buffer buf);
     309             : 
     310             : /* in buf_init.c */
     311             : extern void InitBufferPool(void);
     312             : extern Size BufferShmemSize(void);
     313             : 
     314             : /* in localbuf.c */
     315             : extern void AtProcExit_LocalBuffers(void);
     316             : 
     317             : /* in freelist.c */
     318             : 
     319             : extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
     320             : extern BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype,
     321             :                                                       int ring_size_kb);
     322             : extern int  GetAccessStrategyBufferCount(BufferAccessStrategy strategy);
     323             : extern int  GetAccessStrategyPinLimit(BufferAccessStrategy strategy);
     324             : 
     325             : extern void FreeAccessStrategy(BufferAccessStrategy strategy);
     326             : 
     327             : 
     328             : /* inline functions */
     329             : 
     330             : /*
     331             :  * Although this header file is nominally backend-only, certain frontend
     332             :  * programs like pg_waldump include it.  For compilers that emit static
     333             :  * inline functions even when they're unused, that leads to unsatisfied
     334             :  * external references; hence hide these with #ifndef FRONTEND.
     335             :  */
     336             : 
     337             : #ifndef FRONTEND
     338             : 
     339             : /*
     340             :  * BufferIsValid
     341             :  *      True iff the given buffer number is valid (either as a shared
     342             :  *      or local buffer).
     343             :  *
     344             :  * Note: For a long time this was defined the same as BufferIsPinned,
     345             :  * that is it would say False if you didn't hold a pin on the buffer.
     346             :  * I believe this was bogus and served only to mask logic errors.
     347             :  * Code should always know whether it has a buffer reference,
     348             :  * independently of the pin state.
     349             :  *
     350             :  * Note: For a further long time this was not quite the inverse of the
     351             :  * BufferIsInvalid() macro, in that it also did sanity checks to verify
     352             :  * that the buffer number was in range.  Most likely, this macro was
     353             :  * originally intended only to be used in assertions, but its use has
     354             :  * since expanded quite a bit, and the overhead of making those checks
     355             :  * even in non-assert-enabled builds can be significant.  Thus, we've
     356             :  * now demoted the range checks to assertions within the macro itself.
     357             :  */
     358             : static inline bool
     359   508288888 : BufferIsValid(Buffer bufnum)
     360             : {
     361             :     Assert(bufnum <= NBuffers);
     362             :     Assert(bufnum >= -NLocBuffer);
     363             : 
     364   508288888 :     return bufnum != InvalidBuffer;
     365             : }
     366             : 
     367             : /*
     368             :  * BufferGetBlock
     369             :  *      Returns a reference to a disk page image associated with a buffer.
     370             :  *
     371             :  * Note:
     372             :  *      Assumes buffer is valid.
     373             :  */
     374             : static inline Block
     375   596199516 : BufferGetBlock(Buffer buffer)
     376             : {
     377             :     Assert(BufferIsValid(buffer));
     378             : 
     379   596199516 :     if (BufferIsLocal(buffer))
     380    20057946 :         return LocalBufferBlockPointers[-buffer - 1];
     381             :     else
     382   576141570 :         return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
     383             : }
     384             : 
     385             : /*
     386             :  * BufferGetPageSize
     387             :  *      Returns the page size within a buffer.
     388             :  *
     389             :  * Notes:
     390             :  *      Assumes buffer is valid.
     391             :  *
     392             :  *      The buffer can be a raw disk block and need not contain a valid
     393             :  *      (formatted) disk page.
     394             :  */
     395             : /* XXX should dig out of buffer descriptor */
     396             : static inline Size
     397      407018 : BufferGetPageSize(Buffer buffer)
     398             : {
     399             :     AssertMacro(BufferIsValid(buffer));
     400      407018 :     return (Size) BLCKSZ;
     401             : }
     402             : 
     403             : /*
     404             :  * BufferGetPage
     405             :  *      Returns the page associated with a buffer.
     406             :  */
     407             : static inline Page
     408   593976740 : BufferGetPage(Buffer buffer)
     409             : {
     410   593976740 :     return (Page) BufferGetBlock(buffer);
     411             : }
     412             : 
     413             : #endif                          /* FRONTEND */
     414             : 
     415             : #endif                          /* BUFMGR_H */

Generated by: LCOV version 1.14