PostgreSQL Source Code git master
Loading...
Searching...
No Matches
slru.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * slru.h
4 * Simple LRU buffering for transaction status logfiles
5 *
6 * Portions Copyright (c) 1996-2026, 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
16#include "access/transam.h"
17#include "access/xlogdefs.h"
18#include "storage/lwlock.h"
19#include "storage/sync.h"
20
21/*
22 * To avoid overflowing internal arithmetic and the size_t data type, the
23 * number of buffers must not exceed this number.
24 */
25#define SLRU_MAX_ALLOWED_BUFFERS ((1024 * 1024 * 1024) / BLCKSZ)
26
27/*
28 * Page status codes. Note that these do not include the "dirty" bit.
29 * page_dirty can be true only in the VALID or WRITE_IN_PROGRESS states;
30 * in the latter case it implies that the page has been re-dirtied since
31 * the write started.
32 */
33typedef enum
34{
35 SLRU_PAGE_EMPTY, /* buffer is not in use */
36 SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
37 SLRU_PAGE_VALID, /* page is valid and not being written */
38 SLRU_PAGE_WRITE_IN_PROGRESS, /* page is being written out */
40
41/*
42 * Shared-memory state
43 *
44 * SLRU bank locks are used to protect access to the other fields, except
45 * latest_page_number, which uses atomics; see comment in slru.c.
46 */
47typedef struct SlruSharedData
48{
49 /* Number of buffers managed by this SLRU structure */
51
52 /*
53 * Arrays holding info for each buffer slot. Page number is undefined
54 * when status is EMPTY, as is page_lru_count.
55 */
61
62 /* The buffer_locks protects the I/O on each buffer slots */
64
65 /* Locks to protect the in memory buffer slot access in SLRU bank. */
67
68 /*----------
69 * A bank-wise LRU counter is maintained because we do a victim buffer
70 * search within a bank. Furthermore, manipulating an individual bank
71 * counter avoids frequent cache invalidation since we update it every time
72 * we access the page.
73 *
74 * We mark a page "most recently used" by setting
75 * page_lru_count[slotno] = ++bank_cur_lru_count[bankno];
76 * The oldest page in the bank is therefore the one with the highest value
77 * of
78 * bank_cur_lru_count[bankno] - page_lru_count[slotno]
79 * The counts will eventually wrap around, but this calculation still
80 * works as long as no page's age exceeds INT_MAX counts.
81 *----------
82 */
84
85 /*
86 * Optional array of WAL flush LSNs associated with entries in the SLRU
87 * pages. If not zero/NULL, we must flush WAL before writing pages (true
88 * for pg_xact, false for everything else). group_lsn[] has
89 * lsn_groups_per_page entries per buffer slot, each containing the
90 * highest LSN known for a contiguous group of SLRU entries on that slot's
91 * page.
92 */
95
96 /*
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
99 * the latest page.
100 */
102
103 /* SLRU's index for statistics purposes (might not be unique) */
106
108
109/*
110 * SlruCtlData is an unshared structure that points to the active information
111 * in shared memory.
112 */
113typedef struct SlruCtlData
114{
116
117 /* Number of banks in this SLRU. */
119
120 /*
121 * If true, use long segment file names. Otherwise, use short file names.
122 *
123 * For details about the file name format, see SlruFileName().
124 */
126
127 /*
128 * Which sync handler function to use when handing sync requests over to
129 * the checkpointer. SYNC_HANDLER_NONE to disable fsync (eg pg_notify).
130 */
132
133 /*
134 * Decide whether a page is "older" for truncation and as a hint for
135 * evicting pages in LRU order. Return true if every entry of the first
136 * argument is older than every entry of the second argument. Note that
137 * !PagePrecedes(a,b) && !PagePrecedes(b,a) need not imply a==b; it also
138 * arises when some entries are older and some are not. For SLRUs using
139 * SimpleLruTruncate(), this must use modular arithmetic. (For others,
140 * the behavior of this callback has no functional implications.) Use
141 * SlruPagePrecedesUnitTests() in SLRUs meeting its criteria.
142 */
144
145 /*
146 * Callback to provide more details on an I/O error. This is called as
147 * part of ereport(), and the callback function is expected to call
148 * errdetail() to provide more context on the SLRU access.
149 *
150 * The opaque_data argument here is the argument that was passed to the
151 * SimpleLruReadPage() function.
152 */
154
155 /*
156 * Dir is set during SimpleLruInit and does not change thereafter. Since
157 * it's always the same, it doesn't need to be in shared memory.
158 */
159 char Dir[64];
161
163
164/*
165 * Get the SLRU bank lock for given SlruCtl and the pageno.
166 *
167 * This lock needs to be acquired to access the slru buffer slots in the
168 * respective bank.
169 */
170static inline LWLock *
172{
173 int bankno;
174
175 bankno = pageno % ctl->nbanks;
176 return &(ctl->shared->bank_locks[bankno].lock);
177}
178
179extern Size SimpleLruShmemSize(int nslots, int nlsns);
180extern int SimpleLruAutotuneBuffers(int divisor, int max);
181extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
182 const char *subdir, int buffer_tranche_id,
183 int bank_tranche_id, SyncRequestHandler sync_handler,
184 bool long_segment_names);
185extern int SimpleLruZeroPage(SlruCtl ctl, int64 pageno);
186extern void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno);
187extern int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
188 const void *opaque_data);
190 const void *opaque_data);
191extern void SimpleLruWritePage(SlruCtl ctl, int slotno);
193#ifdef USE_ASSERT_CHECKING
195#else
196#define SlruPagePrecedesUnitTests(ctl, per_page) do {} while (0)
197#endif
200
202 void *data);
204extern void SlruDeleteSegment(SlruCtl ctl, int64 segno);
205
206extern int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path);
207
208/* SlruScanDirectory public callbacks */
210 int64 segpage, void *data);
212 void *data);
213extern bool check_slru_buffers(const char *name, int *newval);
214
215#endif /* SLRU_H */
int64_t int64
Definition c.h:615
uint16_t uint16
Definition c.h:617
size_t Size
Definition c.h:691
#define newval
const void * data
static char * filename
Definition pg_dumpall.c:133
static int fb(int x)
tree ctl
Definition radixtree.h:1838
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)
Definition slru.c:254
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition slru.h:171
SlruSharedData * SlruShared
Definition slru.h:107
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition slru.h:196
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition slru.c:764
bool(* SlruScanCallback)(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition slru.h:201
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition slru.c:1355
int SimpleLruAutotuneBuffers(int divisor, int max)
Definition slru.c:233
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, const void *opaque_data)
Definition slru.c:533
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
Definition slru.c:778
void SlruDeleteSegment(SlruCtl ctl, int64 segno)
Definition slru.c:1559
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition slru.c:1824
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition slru.c:1777
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition slru.c:1864
SlruCtlData * SlruCtl
Definition slru.h:162
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition slru.c:380
void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
Definition slru.c:449
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition slru.c:1441
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, const void *opaque_data)
Definition slru.c:637
SlruPageStatus
Definition slru.h:34
@ SLRU_PAGE_VALID
Definition slru.h:37
@ SLRU_PAGE_WRITE_IN_PROGRESS
Definition slru.h:38
@ SLRU_PAGE_READ_IN_PROGRESS
Definition slru.h:36
@ SLRU_PAGE_EMPTY
Definition slru.h:35
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition slru.c:200
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition slru.c:1745
bool check_slru_buffers(const char *name, int *newval)
Definition slru.c:360
Definition sync.h:51
bool(* PagePrecedes)(int64, int64)
Definition slru.h:143
bool long_segment_names
Definition slru.h:125
SyncRequestHandler sync_handler
Definition slru.h:131
uint16 nbanks
Definition slru.h:118
SlruShared shared
Definition slru.h:115
int(* errdetail_for_io_error)(const void *opaque_data)
Definition slru.h:153
char Dir[64]
Definition slru.h:159
int slru_stats_idx
Definition slru.h:104
int64 * page_number
Definition slru.h:59
int num_slots
Definition slru.h:50
LWLockPadded * bank_locks
Definition slru.h:66
int * page_lru_count
Definition slru.h:60
pg_atomic_uint64 latest_page_number
Definition slru.h:101
XLogRecPtr * group_lsn
Definition slru.h:93
int * bank_cur_lru_count
Definition slru.h:83
int lsn_groups_per_page
Definition slru.h:94
SlruPageStatus * page_status
Definition slru.h:57
bool * page_dirty
Definition slru.h:58
LWLockPadded * buffer_locks
Definition slru.h:63
char ** page_buffer
Definition slru.h:56
SyncRequestHandler
Definition sync.h:36
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
const char * name
uint64 XLogRecPtr
Definition xlogdefs.h:21