bufmgr.h 9.0 KB

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