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