gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 satır
9.0KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * bufmgr.h
  4. * POSTGRES buffer manager definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, 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. /* forward declared, to avoid having to expose buf_internals.h here */
  45. struct WritebackContext;
  46. /* in globals.c ... this duplicates miscadmin.h */
  47. extern PGDLLIMPORT int NBuffers;
  48. /* in bufmgr.c */
  49. extern bool zero_damaged_pages;
  50. extern int bgwriter_lru_maxpages;
  51. extern double bgwriter_lru_multiplier;
  52. extern bool track_io_timing;
  53. extern int target_prefetch_pages;
  54. extern int checkpoint_flush_after;
  55. extern int backend_flush_after;
  56. extern int bgwriter_flush_after;
  57. /* in buf_init.c */
  58. extern PGDLLIMPORT char *BufferBlocks;
  59. /* in guc.c */
  60. extern int effective_io_concurrency;
  61. /* in localbuf.c */
  62. extern PGDLLIMPORT int NLocBuffer;
  63. extern PGDLLIMPORT Block *LocalBufferBlockPointers;
  64. extern PGDLLIMPORT int32 *LocalRefCount;
  65. /* upper limit for effective_io_concurrency */
  66. #define MAX_IO_CONCURRENCY 1000
  67. /* special block number for ReadBuffer() */
  68. #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
  69. /*
  70. * Buffer content lock modes (mode argument for LockBuffer())
  71. */
  72. #define BUFFER_LOCK_UNLOCK 0
  73. #define BUFFER_LOCK_SHARE 1
  74. #define BUFFER_LOCK_EXCLUSIVE 2
  75. /*
  76. * These routines are beaten on quite heavily, hence the macroization.
  77. */
  78. /*
  79. * BufferIsValid
  80. * True iff the given buffer number is valid (either as a shared
  81. * or local buffer).
  82. *
  83. * Note: For a long time this was defined the same as BufferIsPinned,
  84. * that is it would say False if you didn't hold a pin on the buffer.
  85. * I believe this was bogus and served only to mask logic errors.
  86. * Code should always know whether it has a buffer reference,
  87. * independently of the pin state.
  88. *
  89. * Note: For a further long time this was not quite the inverse of the
  90. * BufferIsInvalid() macro, in that it also did sanity checks to verify
  91. * that the buffer number was in range. Most likely, this macro was
  92. * originally intended only to be used in assertions, but its use has
  93. * since expanded quite a bit, and the overhead of making those checks
  94. * even in non-assert-enabled builds can be significant. Thus, we've
  95. * now demoted the range checks to assertions within the macro itself.
  96. */
  97. #define BufferIsValid(bufnum) \
  98. ( \
  99. AssertMacro((bufnum) <= NBuffers && (bufnum) >= -NLocBuffer), \
  100. (bufnum) != InvalidBuffer \
  101. )
  102. /*
  103. * BufferGetBlock
  104. * Returns a reference to a disk page image associated with a buffer.
  105. *
  106. * Note:
  107. * Assumes buffer is valid.
  108. */
  109. #define BufferGetBlock(buffer) \
  110. ( \
  111. AssertMacro(BufferIsValid(buffer)), \
  112. BufferIsLocal(buffer) ? \
  113. LocalBufferBlockPointers[-(buffer) - 1] \
  114. : \
  115. (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
  116. )
  117. /*
  118. * BufferGetPageSize
  119. * Returns the page size within a buffer.
  120. *
  121. * Notes:
  122. * Assumes buffer is valid.
  123. *
  124. * The buffer can be a raw disk block and need not contain a valid
  125. * (formatted) disk page.
  126. */
  127. /* XXX should dig out of buffer descriptor */
  128. #define BufferGetPageSize(buffer) \
  129. ( \
  130. AssertMacro(BufferIsValid(buffer)), \
  131. (Size)BLCKSZ \
  132. )
  133. /*
  134. * BufferGetPage
  135. * Returns the page associated with a buffer.
  136. *
  137. * When this is called as part of a scan, there may be a need for a nearby
  138. * call to TestForOldSnapshot(). See the definition of that for details.
  139. */
  140. #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
  141. /*
  142. * prototypes for functions in bufmgr.c
  143. */
  144. extern bool ComputeIoConcurrency(int io_concurrency, double *target);
  145. extern void PrefetchBuffer(Relation reln, ForkNumber forkNum,
  146. BlockNumber blockNum);
  147. extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
  148. extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
  149. BlockNumber blockNum, ReadBufferMode mode,
  150. BufferAccessStrategy strategy);
  151. extern Buffer ReadBufferWithoutRelcache(RelFileNode rnode,
  152. ForkNumber forkNum, BlockNumber blockNum,
  153. ReadBufferMode mode, BufferAccessStrategy strategy);
  154. extern void ReleaseBuffer(Buffer buffer);
  155. extern void UnlockReleaseBuffer(Buffer buffer);
  156. extern void MarkBufferDirty(Buffer buffer);
  157. extern void IncrBufferRefCount(Buffer buffer);
  158. extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
  159. BlockNumber blockNum);
  160. extern void InitBufferPool(void);
  161. extern void InitBufferPoolAccess(void);
  162. extern void InitBufferPoolBackend(void);
  163. extern void AtEOXact_Buffers(bool isCommit);
  164. extern void PrintBufferLeakWarning(Buffer buffer);
  165. extern void CheckPointBuffers(int flags);
  166. extern BlockNumber BufferGetBlockNumber(Buffer buffer);
  167. extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
  168. ForkNumber forkNum);
  169. extern void FlushOneBuffer(Buffer buffer);
  170. extern void FlushRelationBuffers(Relation rel);
  171. extern void FlushDatabaseBuffers(Oid dbid);
  172. extern void DropRelFileNodeBuffers(RelFileNodeBackend rnode,
  173. ForkNumber forkNum, BlockNumber firstDelBlock);
  174. extern void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes);
  175. extern void DropDatabaseBuffers(Oid dbid);
  176. #define RelationGetNumberOfBlocks(reln) \
  177. RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
  178. extern bool BufferIsPermanent(Buffer buffer);
  179. extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
  180. #ifdef NOT_USED
  181. extern void PrintPinnedBufs(void);
  182. #endif
  183. extern Size BufferShmemSize(void);
  184. extern void BufferGetTag(Buffer buffer, RelFileNode *rnode,
  185. ForkNumber *forknum, BlockNumber *blknum);
  186. extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
  187. extern void UnlockBuffers(void);
  188. extern void LockBuffer(Buffer buffer, int mode);
  189. extern bool ConditionalLockBuffer(Buffer buffer);
  190. extern void LockBufferForCleanup(Buffer buffer);
  191. extern bool ConditionalLockBufferForCleanup(Buffer buffer);
  192. extern bool IsBufferCleanupOK(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_waldump 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)->snapshot_type == SNAPSHOT_MVCC
  234. || (snapshot)->snapshot_type == SNAPSHOT_TOAST)
  235. && !XLogRecPtrIsInvalid((snapshot)->lsn)
  236. && PageGetLSN(page) > (snapshot)->lsn)
  237. TestForOldSnapshot_impl(snapshot, relation);
  238. }
  239. #endif /* FRONTEND */
  240. #endif /* BUFMGR_H */
上海开阖软件有限公司 沪ICP备12045867号-1