2
0

bufmgr.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*-------------------------------------------------------------------------
  2. *
  3. * bufmgr.h
  4. * POSTGRES buffer manager definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, 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. #include "storage/block.h"
  17. #include "storage/buf.h"
  18. #include "storage/bufpage.h"
  19. #include "storage/relfilenode.h"
  20. #include "utils/relcache.h"
  21. #include "utils/snapmgr.h"
  22. typedef void *Block;
  23. /* Possible arguments for GetAccessStrategy() */
  24. typedef enum BufferAccessStrategyType
  25. {
  26. BAS_NORMAL, /* Normal random access */
  27. BAS_BULKREAD, /* Large read-only scan (hint bit updates are
  28. * ok) */
  29. BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
  30. BAS_VACUUM /* VACUUM */
  31. } BufferAccessStrategyType;
  32. /* Possible modes for ReadBufferExtended() */
  33. typedef enum
  34. {
  35. RBM_NORMAL, /* Normal read */
  36. RBM_ZERO_AND_LOCK, /* Don't read from disk, caller will
  37. * initialize. Also locks the page. */
  38. RBM_ZERO_AND_CLEANUP_LOCK, /* Like RBM_ZERO_AND_LOCK, but locks the page
  39. * in "cleanup" mode */
  40. RBM_ZERO_ON_ERROR, /* Read, but return an all-zeros page on error */
  41. RBM_NORMAL_NO_LOG /* Don't log page as invalid during WAL
  42. * replay; otherwise same as RBM_NORMAL */
  43. } ReadBufferMode;
  44. /*
  45. * Type returned by PrefetchBuffer().
  46. */
  47. typedef struct PrefetchBufferResult
  48. {
  49. Buffer recent_buffer; /* If valid, a hit (recheck needed!) */
  50. bool initiated_io; /* If true, a miss resulting in async I/O */
  51. } PrefetchBufferResult;
  52. /* forward declared, to avoid having to expose buf_internals.h here */
  53. struct WritebackContext;
  54. /* forward declared, to avoid including smgr.h here */
  55. struct SMgrRelationData;
  56. /* in globals.c ... this duplicates miscadmin.h */
  57. extern PGDLLIMPORT int NBuffers;
  58. /* in bufmgr.c */
  59. extern PGDLLIMPORT bool zero_damaged_pages;
  60. extern PGDLLIMPORT int bgwriter_lru_maxpages;
  61. extern PGDLLIMPORT double bgwriter_lru_multiplier;
  62. extern PGDLLIMPORT bool track_io_timing;
  63. extern PGDLLIMPORT int effective_io_concurrency;
  64. extern PGDLLIMPORT int maintenance_io_concurrency;
  65. extern PGDLLIMPORT int checkpoint_flush_after;
  66. extern PGDLLIMPORT int backend_flush_after;
  67. extern PGDLLIMPORT int bgwriter_flush_after;
  68. /* in buf_init.c */
  69. extern PGDLLIMPORT char *BufferBlocks;
  70. /* in localbuf.c */
  71. extern PGDLLIMPORT int NLocBuffer;
  72. extern PGDLLIMPORT Block *LocalBufferBlockPointers;
  73. extern PGDLLIMPORT int32 *LocalRefCount;
  74. /* upper limit for effective_io_concurrency */
  75. #define MAX_IO_CONCURRENCY 1000
  76. /* special block number for ReadBuffer() */
  77. #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
  78. /*
  79. * Buffer content lock modes (mode argument for LockBuffer())
  80. */
  81. #define BUFFER_LOCK_UNLOCK 0
  82. #define BUFFER_LOCK_SHARE 1
  83. #define BUFFER_LOCK_EXCLUSIVE 2
  84. /*
  85. * These routines are beaten on quite heavily, hence the macroization.
  86. */
  87. /*
  88. * BufferIsValid
  89. * True iff the given buffer number is valid (either as a shared
  90. * or local buffer).
  91. *
  92. * Note: For a long time this was defined the same as BufferIsPinned,
  93. * that is it would say False if you didn't hold a pin on the buffer.
  94. * I believe this was bogus and served only to mask logic errors.
  95. * Code should always know whether it has a buffer reference,
  96. * independently of the pin state.
  97. *
  98. * Note: For a further long time this was not quite the inverse of the
  99. * BufferIsInvalid() macro, in that it also did sanity checks to verify
  100. * that the buffer number was in range. Most likely, this macro was
  101. * originally intended only to be used in assertions, but its use has
  102. * since expanded quite a bit, and the overhead of making those checks
  103. * even in non-assert-enabled builds can be significant. Thus, we've
  104. * now demoted the range checks to assertions within the macro itself.
  105. */
  106. #define BufferIsValid(bufnum) \
  107. ( \
  108. AssertMacro((bufnum) <= NBuffers && (bufnum) >= -NLocBuffer), \
  109. (bufnum) != InvalidBuffer \
  110. )
  111. /*
  112. * BufferGetBlock
  113. * Returns a reference to a disk page image associated with a buffer.
  114. *
  115. * Note:
  116. * Assumes buffer is valid.
  117. */
  118. #define BufferGetBlock(buffer) \
  119. ( \
  120. AssertMacro(BufferIsValid(buffer)), \
  121. BufferIsLocal(buffer) ? \
  122. LocalBufferBlockPointers[-(buffer) - 1] \
  123. : \
  124. (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
  125. )
  126. /*
  127. * BufferGetPageSize
  128. * Returns the page size within a buffer.
  129. *
  130. * Notes:
  131. * Assumes buffer is valid.
  132. *
  133. * The buffer can be a raw disk block and need not contain a valid
  134. * (formatted) disk page.
  135. */
  136. /* XXX should dig out of buffer descriptor */
  137. #define BufferGetPageSize(buffer) \
  138. ( \
  139. AssertMacro(BufferIsValid(buffer)), \
  140. (Size)BLCKSZ \
  141. )
  142. /*
  143. * BufferGetPage
  144. * Returns the page associated with a buffer.
  145. *
  146. * When this is called as part of a scan, there may be a need for a nearby
  147. * call to TestForOldSnapshot(). See the definition of that for details.
  148. */
  149. #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
  150. /*
  151. * prototypes for functions in bufmgr.c
  152. */
  153. extern PrefetchBufferResult PrefetchSharedBuffer(struct SMgrRelationData *smgr_reln,
  154. ForkNumber forkNum,
  155. BlockNumber blockNum);
  156. extern PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum,
  157. BlockNumber blockNum);
  158. extern bool ReadRecentBuffer(RelFileNode rnode, ForkNumber forkNum,
  159. BlockNumber blockNum, Buffer recent_buffer);
  160. extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
  161. extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
  162. BlockNumber blockNum, ReadBufferMode mode,
  163. BufferAccessStrategy strategy);
  164. extern Buffer ReadBufferWithoutRelcache(RelFileNode rnode,
  165. ForkNumber forkNum, BlockNumber blockNum,
  166. ReadBufferMode mode, BufferAccessStrategy strategy,
  167. bool permanent);
  168. extern void ReleaseBuffer(Buffer buffer);
  169. extern void UnlockReleaseBuffer(Buffer buffer);
  170. extern void MarkBufferDirty(Buffer buffer);
  171. extern void IncrBufferRefCount(Buffer buffer);
  172. extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
  173. BlockNumber blockNum);
  174. extern void InitBufferPool(void);
  175. extern void InitBufferPoolAccess(void);
  176. extern void AtEOXact_Buffers(bool isCommit);
  177. extern void PrintBufferLeakWarning(Buffer buffer);
  178. extern void CheckPointBuffers(int flags);
  179. extern BlockNumber BufferGetBlockNumber(Buffer buffer);
  180. extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
  181. ForkNumber forkNum);
  182. extern void FlushOneBuffer(Buffer buffer);
  183. extern void FlushRelationBuffers(Relation rel);
  184. extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels);
  185. extern void CreateAndCopyRelationData(RelFileNode src_rnode,
  186. RelFileNode dst_rnode,
  187. bool permanent);
  188. extern void FlushDatabaseBuffers(Oid dbid);
  189. extern void DropRelFileNodeBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum,
  190. int nforks, BlockNumber *firstDelBlock);
  191. extern void DropRelFileNodesAllBuffers(struct SMgrRelationData **smgr_reln, int nnodes);
  192. extern void DropDatabaseBuffers(Oid dbid);
  193. #define RelationGetNumberOfBlocks(reln) \
  194. RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
  195. extern bool BufferIsPermanent(Buffer buffer);
  196. extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
  197. #ifdef NOT_USED
  198. extern void PrintPinnedBufs(void);
  199. #endif
  200. extern Size BufferShmemSize(void);
  201. extern void BufferGetTag(Buffer buffer, RelFileNode *rnode,
  202. ForkNumber *forknum, BlockNumber *blknum);
  203. extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
  204. extern void UnlockBuffers(void);
  205. extern void LockBuffer(Buffer buffer, int mode);
  206. extern bool ConditionalLockBuffer(Buffer buffer);
  207. extern void LockBufferForCleanup(Buffer buffer);
  208. extern bool ConditionalLockBufferForCleanup(Buffer buffer);
  209. extern bool IsBufferCleanupOK(Buffer buffer);
  210. extern bool HoldingBufferPinThatDelaysRecovery(void);
  211. extern void AbortBufferIO(void);
  212. extern void BufmgrCommit(void);
  213. extern bool BgBufferSync(struct WritebackContext *wb_context);
  214. extern void AtProcExit_LocalBuffers(void);
  215. extern void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation);
  216. /* in freelist.c */
  217. extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
  218. extern void FreeAccessStrategy(BufferAccessStrategy strategy);
  219. /* inline functions */
  220. /*
  221. * Although this header file is nominally backend-only, certain frontend
  222. * programs like pg_waldump include it. For compilers that emit static
  223. * inline functions even when they're unused, that leads to unsatisfied
  224. * external references; hence hide these with #ifndef FRONTEND.
  225. */
  226. #ifndef FRONTEND
  227. /*
  228. * Check whether the given snapshot is too old to have safely read the given
  229. * page from the given table. If so, throw a "snapshot too old" error.
  230. *
  231. * This test generally needs to be performed after every BufferGetPage() call
  232. * that is executed as part of a scan. It is not needed for calls made for
  233. * modifying the page (for example, to position to the right place to insert a
  234. * new index tuple or for vacuuming). It may also be omitted where calls to
  235. * lower-level functions will have already performed the test.
  236. *
  237. * Note that a NULL snapshot argument is allowed and causes a fast return
  238. * without error; this is to support call sites which can be called from
  239. * either scans or index modification areas.
  240. *
  241. * For best performance, keep the tests that are fastest and/or most likely to
  242. * exclude a page from old snapshot testing near the front.
  243. */
  244. static inline void
  245. TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
  246. {
  247. Assert(relation != NULL);
  248. if (old_snapshot_threshold >= 0
  249. && (snapshot) != NULL
  250. && ((snapshot)->snapshot_type == SNAPSHOT_MVCC
  251. || (snapshot)->snapshot_type == SNAPSHOT_TOAST)
  252. && !XLogRecPtrIsInvalid((snapshot)->lsn)
  253. && PageGetLSN(page) > (snapshot)->lsn)
  254. TestForOldSnapshot_impl(snapshot, relation);
  255. }
  256. #endif /* FRONTEND */
  257. #endif /* BUFMGR_H */