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