slru.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*-------------------------------------------------------------------------
  2. *
  3. * slru.h
  4. * Simple LRU buffering for transaction status logfiles
  5. *
  6. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/access/slru.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef SLRU_H
  14. #define SLRU_H
  15. #include "access/xlogdefs.h"
  16. #include "storage/lwlock.h"
  17. /*
  18. * Define SLRU segment size. A page is the same BLCKSZ as is used everywhere
  19. * else in Postgres. The segment size can be chosen somewhat arbitrarily;
  20. * we make it 32 pages by default, or 256Kb, i.e. 1M transactions for CLOG
  21. * or 64K transactions for SUBTRANS.
  22. *
  23. * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
  24. * page numbering also wraps around at 0xFFFFFFFF/xxxx_XACTS_PER_PAGE (where
  25. * xxxx is CLOG or SUBTRANS, respectively), and segment numbering at
  26. * 0xFFFFFFFF/xxxx_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need
  27. * take no explicit notice of that fact in slru.c, except when comparing
  28. * segment and page numbers in SimpleLruTruncate (see PagePrecedes()).
  29. *
  30. * Note: slru.c currently assumes that segment file names will be four hex
  31. * digits. This sets a lower bound on the segment size (64K transactions
  32. * for 32-bit TransactionIds).
  33. */
  34. #define SLRU_PAGES_PER_SEGMENT 32
  35. /* Maximum length of an SLRU name */
  36. #define SLRU_MAX_NAME_LENGTH 32
  37. /*
  38. * Page status codes. Note that these do not include the "dirty" bit.
  39. * page_dirty can be TRUE only in the VALID or WRITE_IN_PROGRESS states;
  40. * in the latter case it implies that the page has been re-dirtied since
  41. * the write started.
  42. */
  43. typedef enum
  44. {
  45. SLRU_PAGE_EMPTY, /* buffer is not in use */
  46. SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
  47. SLRU_PAGE_VALID, /* page is valid and not being written */
  48. SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */
  49. } SlruPageStatus;
  50. /*
  51. * Shared-memory state
  52. */
  53. typedef struct SlruSharedData
  54. {
  55. LWLock *ControlLock;
  56. /* Number of buffers managed by this SLRU structure */
  57. int num_slots;
  58. /*
  59. * Arrays holding info for each buffer slot. Page number is undefined
  60. * when status is EMPTY, as is page_lru_count.
  61. */
  62. char **page_buffer;
  63. SlruPageStatus *page_status;
  64. bool *page_dirty;
  65. int *page_number;
  66. int *page_lru_count;
  67. /*
  68. * Optional array of WAL flush LSNs associated with entries in the SLRU
  69. * pages. If not zero/NULL, we must flush WAL before writing pages (true
  70. * for pg_clog, false for multixact, pg_subtrans, pg_notify). group_lsn[]
  71. * has lsn_groups_per_page entries per buffer slot, each containing the
  72. * highest LSN known for a contiguous group of SLRU entries on that slot's
  73. * page.
  74. */
  75. XLogRecPtr *group_lsn;
  76. int lsn_groups_per_page;
  77. /*----------
  78. * We mark a page "most recently used" by setting
  79. * page_lru_count[slotno] = ++cur_lru_count;
  80. * The oldest page is therefore the one with the highest value of
  81. * cur_lru_count - page_lru_count[slotno]
  82. * The counts will eventually wrap around, but this calculation still
  83. * works as long as no page's age exceeds INT_MAX counts.
  84. *----------
  85. */
  86. int cur_lru_count;
  87. /*
  88. * latest_page_number is the page number of the current end of the log;
  89. * this is not critical data, since we use it only to avoid swapping out
  90. * the latest page.
  91. */
  92. int latest_page_number;
  93. /* LWLocks */
  94. int lwlock_tranche_id;
  95. LWLockTranche lwlock_tranche;
  96. char lwlock_tranche_name[SLRU_MAX_NAME_LENGTH];
  97. LWLockPadded *buffer_locks;
  98. } SlruSharedData;
  99. typedef SlruSharedData *SlruShared;
  100. /*
  101. * SlruCtlData is an unshared structure that points to the active information
  102. * in shared memory.
  103. */
  104. typedef struct SlruCtlData
  105. {
  106. SlruShared shared;
  107. /*
  108. * This flag tells whether to fsync writes (true for pg_clog and multixact
  109. * stuff, false for pg_subtrans and pg_notify).
  110. */
  111. bool do_fsync;
  112. /*
  113. * Decide which of two page numbers is "older" for truncation purposes. We
  114. * need to use comparison of TransactionIds here in order to do the right
  115. * thing with wraparound XID arithmetic.
  116. */
  117. bool (*PagePrecedes) (int, int);
  118. /*
  119. * Dir is set during SimpleLruInit and does not change thereafter. Since
  120. * it's always the same, it doesn't need to be in shared memory.
  121. */
  122. char Dir[64];
  123. } SlruCtlData;
  124. typedef SlruCtlData *SlruCtl;
  125. extern Size SimpleLruShmemSize(int nslots, int nlsns);
  126. extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
  127. LWLock *ctllock, const char *subdir, int tranche_id);
  128. extern int SimpleLruZeroPage(SlruCtl ctl, int pageno);
  129. extern int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
  130. TransactionId xid);
  131. extern int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno,
  132. TransactionId xid);
  133. extern void SimpleLruWritePage(SlruCtl ctl, int slotno);
  134. extern void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied);
  135. extern void SimpleLruTruncate(SlruCtl ctl, int cutoffPage);
  136. extern bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno);
  137. typedef bool (*SlruScanCallback) (SlruCtl ctl, char *filename, int segpage,
  138. void *data);
  139. extern bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data);
  140. extern void SlruDeleteSegment(SlruCtl ctl, int segno);
  141. /* SlruScanDirectory public callbacks */
  142. extern bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename,
  143. int segpage, void *data);
  144. extern bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage,
  145. void *data);
  146. #endif /* SLRU_H */