1 /*-------------------------------------------------------------------------
4 * Simple LRU buffering for transaction status logfiles
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/include/access/slru.h,v 1.26 2010/02/16 22:34:50 tgl Exp $
11 *-------------------------------------------------------------------------
16 #include "access/xlogdefs.h"
17 #include "storage/lwlock.h"
21 * Define SLRU segment size. A page is the same BLCKSZ as is used everywhere
22 * else in Postgres. The segment size can be chosen somewhat arbitrarily;
23 * we make it 32 pages by default, or 256Kb, i.e. 1M transactions for CLOG
24 * or 64K transactions for SUBTRANS.
26 * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
27 * page numbering also wraps around at 0xFFFFFFFF/xxxx_XACTS_PER_PAGE (where
28 * xxxx is CLOG or SUBTRANS, respectively), and segment numbering at
29 * 0xFFFFFFFF/xxxx_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need
30 * take no explicit notice of that fact in slru.c, except when comparing
31 * segment and page numbers in SimpleLruTruncate (see PagePrecedes()).
33 * Note: slru.c currently assumes that segment file names will be four hex
34 * digits. This sets a lower bound on the segment size (64K transactions
35 * for 32-bit TransactionIds).
37 #define SLRU_PAGES_PER_SEGMENT 32
40 * Page status codes. Note that these do not include the "dirty" bit.
41 * page_dirty can be TRUE only in the VALID or WRITE_IN_PROGRESS states;
42 * in the latter case it implies that the page has been re-dirtied since
47 SLRU_PAGE_EMPTY, /* buffer is not in use */
48 SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
49 SLRU_PAGE_VALID, /* page is valid and not being written */
50 SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */
56 typedef struct SlruSharedData
60 /* Number of buffers managed by this SLRU structure */
64 * Arrays holding info for each buffer slot. Page number is undefined
65 * when status is EMPTY, as is page_lru_count.
68 SlruPageStatus *page_status;
72 LWLockId *buffer_locks;
75 * Optional array of WAL flush LSNs associated with entries in the SLRU
76 * pages. If not zero/NULL, we must flush WAL before writing pages (true
77 * for pg_clog, false for multixact, pg_subtrans, pg_notify). group_lsn[]
78 * has lsn_groups_per_page entries per buffer slot, each containing the
79 * highest LSN known for a contiguous group of SLRU entries on that slot's
82 XLogRecPtr *group_lsn;
83 int lsn_groups_per_page;
86 * We mark a page "most recently used" by setting
87 * page_lru_count[slotno] = ++cur_lru_count;
88 * The oldest page is therefore the one with the highest value of
89 * cur_lru_count - page_lru_count[slotno]
90 * The counts will eventually wrap around, but this calculation still
91 * works as long as no page's age exceeds INT_MAX counts.
97 * latest_page_number is the page number of the current end of the log;
98 * this is not critical data, since we use it only to avoid swapping out
101 int latest_page_number;
104 typedef SlruSharedData *SlruShared;
107 * SlruCtlData is an unshared structure that points to the active information
110 typedef struct SlruCtlData
115 * This flag tells whether to fsync writes (true for pg_clog and multixact
116 * stuff, false for pg_subtrans and pg_notify).
121 * Decide which of two page numbers is "older" for truncation purposes. We
122 * need to use comparison of TransactionIds here in order to do the right
123 * thing with wraparound XID arithmetic.
125 bool (*PagePrecedes) (int, int);
128 * Dir is set during SimpleLruInit and does not change thereafter. Since
129 * it's always the same, it doesn't need to be in shared memory.
134 typedef SlruCtlData *SlruCtl;
136 /* Opaque struct known only in slru.c */
137 typedef struct SlruFlushData *SlruFlush;
140 extern Size SimpleLruShmemSize(int nslots, int nlsns);
141 extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
142 LWLockId ctllock, const char *subdir);
143 extern int SimpleLruZeroPage(SlruCtl ctl, int pageno);
144 extern int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
146 extern int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno,
148 extern void SimpleLruWritePage(SlruCtl ctl, int slotno, SlruFlush fdata);
149 extern void SimpleLruFlush(SlruCtl ctl, bool checkpoint);
150 extern void SimpleLruTruncate(SlruCtl ctl, int cutoffPage);
151 extern bool SlruScanDirectory(SlruCtl ctl, int cutoffPage, bool doDeletions);