1/*-------------------------------------------------------------------------
4 * Simple LRU buffering for transaction status logfiles
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * src/include/access/slru.h
11 *-------------------------------------------------------------------------
21 * To avoid overflowing internal arithmetic and the size_t data type, the
22 * number of buffers must not exceed this number.
24 #define SLRU_MAX_ALLOWED_BUFFERS ((1024 * 1024 * 1024) / BLCKSZ)
27 * Define SLRU segment size. A page is the same BLCKSZ as is used everywhere
28 * else in Postgres. The segment size can be chosen somewhat arbitrarily;
29 * we make it 32 pages by default, or 256Kb, i.e. 1M transactions for CLOG
30 * or 64K transactions for SUBTRANS.
32 * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
33 * page numbering also wraps around at 0xFFFFFFFF/xxxx_XACTS_PER_PAGE (where
34 * xxxx is CLOG or SUBTRANS, respectively), and segment numbering at
35 * 0xFFFFFFFF/xxxx_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need
36 * take no explicit notice of that fact in slru.c, except when comparing
37 * segment and page numbers in SimpleLruTruncate (see PagePrecedes()).
39 #define SLRU_PAGES_PER_SEGMENT 32
42 * Page status codes. Note that these do not include the "dirty" bit.
43 * page_dirty can be true only in the VALID or WRITE_IN_PROGRESS states;
44 * in the latter case it implies that the page has been re-dirtied since
58 * SLRU bank locks are used to protect access to the other fields, except
59 * latest_page_number, which uses atomics; see comment in slru.c.
63 /* Number of buffers managed by this SLRU structure */
67 * Arrays holding info for each buffer slot. Page number is undefined
68 * when status is EMPTY, as is page_lru_count.
76 /* The buffer_locks protects the I/O on each buffer slots */
79 /* Locks to protect the in memory buffer slot access in SLRU bank. */
83 * A bank-wise LRU counter is maintained because we do a victim buffer
84 * search within a bank. Furthermore, manipulating an individual bank
85 * counter avoids frequent cache invalidation since we update it every time
88 * We mark a page "most recently used" by setting
89 * page_lru_count[slotno] = ++bank_cur_lru_count[bankno];
90 * The oldest page in the bank is therefore the one with the highest value
92 * bank_cur_lru_count[bankno] - page_lru_count[slotno]
93 * The counts will eventually wrap around, but this calculation still
94 * works as long as no page's age exceeds INT_MAX counts.
100 * Optional array of WAL flush LSNs associated with entries in the SLRU
101 * pages. If not zero/NULL, we must flush WAL before writing pages (true
102 * for pg_xact, false for everything else). group_lsn[] has
103 * lsn_groups_per_page entries per buffer slot, each containing the
104 * highest LSN known for a contiguous group of SLRU entries on that slot's
111 * latest_page_number is the page number of the current end of the log;
112 * this is not critical data, since we use it only to avoid swapping out
117 /* SLRU's index for statistics purposes (might not be unique) */
124 * SlruCtlData is an unshared structure that points to the active information
131 /* Number of banks in this SLRU. */
135 * If true, use long segment file names. Otherwise, use short file names.
137 * For details about the file name format, see SlruFileName().
142 * Which sync handler function to use when handing sync requests over to
143 * the checkpointer. SYNC_HANDLER_NONE to disable fsync (eg pg_notify).
148 * Decide whether a page is "older" for truncation and as a hint for
149 * evicting pages in LRU order. Return true if every entry of the first
150 * argument is older than every entry of the second argument. Note that
151 * !PagePrecedes(a,b) && !PagePrecedes(b,a) need not imply a==b; it also
152 * arises when some entries are older and some are not. For SLRUs using
153 * SimpleLruTruncate(), this must use modular arithmetic. (For others,
154 * the behavior of this callback has no functional implications.) Use
155 * SlruPagePrecedesUnitTests() in SLRUs meeting its criteria.
160 * Dir is set during SimpleLruInit and does not change thereafter. Since
161 * it's always the same, it doesn't need to be in shared memory.
169 * Get the SLRU bank lock for given SlruCtl and the pageno.
171 * This lock needs to be acquired to access the slru buffer slots in the
179 bankno = pageno %
ctl->nbanks;
180 return &(
ctl->shared->bank_locks[bankno].lock);
186 const char *subdir,
int buffer_tranche_id,
188 bool long_segment_names);
197#ifdef USE_ASSERT_CHECKING
200 #define SlruPagePrecedesUnitTests(ctl, per_page) do {} while (0)
212/* SlruScanDirectory public callbacks */
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
SlruSharedData * SlruShared
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
#define SlruPagePrecedesUnitTests(ctl, per_page)
void SimpleLruWritePage(SlruCtl ctl, int slotno)
bool(* SlruScanCallback)(SlruCtl ctl, char *filename, int64 segpage, void *data)
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
int SimpleLruAutotuneBuffers(int divisor, int max)
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
void SlruDeleteSegment(SlruCtl ctl, int64 segno)
struct SlruCtlData SlruCtlData
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
struct SlruSharedData SlruSharedData
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
@ SLRU_PAGE_WRITE_IN_PROGRESS
@ SLRU_PAGE_READ_IN_PROGRESS
Size SimpleLruShmemSize(int nslots, int nlsns)
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage, void *data)
bool check_slru_buffers(const char *name, int *newval)
bool(* PagePrecedes)(int64, int64)
SyncRequestHandler sync_handler
LWLockPadded * bank_locks
pg_atomic_uint64 latest_page_number
SlruPageStatus * page_status
LWLockPadded * buffer_locks
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)