PostgreSQL Source Code  git master
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-2020, 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/xlogdefs.h"
17 #include "storage/lwlock.h"
18 
19 
20 /*
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.
25  *
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()).
32  */
33 #define SLRU_PAGES_PER_SEGMENT 32
34 
35 /*
36  * Page status codes. Note that these do not include the "dirty" bit.
37  * page_dirty can be true only in the VALID or WRITE_IN_PROGRESS states;
38  * in the latter case it implies that the page has been re-dirtied since
39  * the write started.
40  */
41 typedef enum
42 {
43  SLRU_PAGE_EMPTY, /* buffer is not in use */
44  SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
45  SLRU_PAGE_VALID, /* page is valid and not being written */
46  SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */
48 
49 /*
50  * Shared-memory state
51  */
52 typedef struct SlruSharedData
53 {
55 
56  /* Number of buffers managed by this SLRU structure */
57  int num_slots;
58 
59  /*
60  * Arrays holding info for each buffer slot. Page number is undefined
61  * when status is EMPTY, as is page_lru_count.
62  */
63  char **page_buffer;
65  bool *page_dirty;
69 
70  /*
71  * Optional array of WAL flush LSNs associated with entries in the SLRU
72  * pages. If not zero/NULL, we must flush WAL before writing pages (true
73  * for pg_xact, false for multixact, pg_subtrans, pg_notify). group_lsn[]
74  * has lsn_groups_per_page entries per buffer slot, each containing the
75  * highest LSN known for a contiguous group of SLRU entries on that slot's
76  * page.
77  */
80 
81  /*----------
82  * We mark a page "most recently used" by setting
83  * page_lru_count[slotno] = ++cur_lru_count;
84  * The oldest page is therefore the one with the highest value of
85  * cur_lru_count - page_lru_count[slotno]
86  * The counts will eventually wrap around, but this calculation still
87  * works as long as no page's age exceeds INT_MAX counts.
88  *----------
89  */
91 
92  /*
93  * latest_page_number is the page number of the current end of the log;
94  * this is not critical data, since we use it only to avoid swapping out
95  * the latest page.
96  */
98 
99  /* SLRU's index for statistics purposes (might not be unique) */
102 
104 
105 /*
106  * SlruCtlData is an unshared structure that points to the active information
107  * in shared memory.
108  */
109 typedef struct SlruCtlData
110 {
112 
113  /*
114  * This flag tells whether to fsync writes (true for pg_xact and multixact
115  * stuff, false for pg_subtrans and pg_notify).
116  */
117  bool do_fsync;
118 
119  /*
120  * Decide which of two page numbers is "older" for truncation purposes. We
121  * need to use comparison of TransactionIds here in order to do the right
122  * thing with wraparound XID arithmetic.
123  */
124  bool (*PagePrecedes) (int, int);
125 
126  /*
127  * Dir is set during SimpleLruInit and does not change thereafter. Since
128  * it's always the same, it doesn't need to be in shared memory.
129  */
130  char Dir[64];
131 } SlruCtlData;
132 
134 
135 
136 extern Size SimpleLruShmemSize(int nslots, int nlsns);
137 extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
138  LWLock *ctllock, const char *subdir, int tranche_id);
139 extern int SimpleLruZeroPage(SlruCtl ctl, int pageno);
140 extern int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
141  TransactionId xid);
142 extern int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno,
143  TransactionId xid);
144 extern void SimpleLruWritePage(SlruCtl ctl, int slotno);
145 extern void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied);
146 extern void SimpleLruTruncate(SlruCtl ctl, int cutoffPage);
147 extern bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno);
148 
149 typedef bool (*SlruScanCallback) (SlruCtl ctl, char *filename, int segpage,
150  void *data);
151 extern bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data);
152 extern void SlruDeleteSegment(SlruCtl ctl, int segno);
153 
154 /* SlruScanDirectory public callbacks */
155 extern bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename,
156  int segpage, void *data);
157 extern bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage,
158  void *data);
159 
160 #endif /* SLRU_H */
LWLock * ControlLock
Definition: slru.h:54
int * page_number
Definition: slru.h:66
struct SlruCtlData SlruCtlData
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id)
Definition: slru.c:175
Definition: lwlock.h:32
SlruPageStatus
Definition: slru.h:41
uint32 TransactionId
Definition: c.h:513
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1145
int latest_page_number
Definition: slru.h:97
char ** page_buffer
Definition: slru.h:63
SlruCtlData * SlruCtl
Definition: slru.h:133
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:597
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1213
int cur_lru_count
Definition: slru.h:90
int lsn_groups_per_page
Definition: slru.h:79
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:144
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1401
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:382
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1369
SlruPageStatus * page_status
Definition: slru.h:64
struct SlruSharedData SlruSharedData
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
LWLockPadded * buffer_locks
Definition: slru.h:68
XLogRecPtr * group_lsn
Definition: slru.h:78
bool do_fsync
Definition: slru.h:117
bool(* SlruScanCallback)(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.h:149
SlruSharedData * SlruShared
Definition: slru.h:103
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1424
int * page_lru_count
Definition: slru.h:67
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:466
int slru_stats_idx
Definition: slru.h:100
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:482
int num_slots
Definition: slru.h:57
const char * name
Definition: encode.c:555
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:267
static char * filename
Definition: pg_dumpall.c:90
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
Definition: slru.c:609
bool * page_dirty
Definition: slru.h:65
SlruShared shared
Definition: slru.h:111
void SlruDeleteSegment(SlruCtl ctl, int segno)
Definition: slru.c:1309
unsigned char bool
Definition: c.h:317