1/*-------------------------------------------------------------------------
2 *
3 * slru.h
4 * Simple LRU buffering for transaction status logfiles
5 *
6 * Portions Copyright (c) 1996-2019, 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 */
44typedef 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 */
50} SlruPageStatus;
51
52/*
53 * Shared-memory state
54 */
55typedef struct SlruSharedData
56{
57 LWLock *ControlLock;
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;
67 SlruPageStatus *page_status;
68 bool *page_dirty;
69 int *page_number;
70 int *page_lru_count;
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 */
80 XLogRecPtr *group_lsn;
81 int lsn_groups_per_page;
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 */
92 int cur_lru_count;
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 */
99 int latest_page_number;
100
101 /* LWLocks */
102 int lwlock_tranche_id;
103 char lwlock_tranche_name[SLRU_MAX_NAME_LENGTH];
104 LWLockPadded *buffer_locks;
105} SlruSharedData;
106
107typedef SlruSharedData *SlruShared;
108
109/*
110 * SlruCtlData is an unshared structure that points to the active information
111 * in shared memory.
112 */
113typedef struct SlruCtlData
114{
115 SlruShared shared;
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
137typedef SlruCtlData *SlruCtl;
138
139
140extern Size SimpleLruShmemSize(int nslots, int nlsns);
141extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
142 LWLock *ctllock, const char *subdir, int tranche_id);
143extern int SimpleLruZeroPage(SlruCtl ctl, int pageno);
144extern int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
145 TransactionId xid);
146extern int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno,
147 TransactionId xid);
148extern void SimpleLruWritePage(SlruCtl ctl, int slotno);
149extern void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied);
150extern void SimpleLruTruncate(SlruCtl ctl, int cutoffPage);
151extern bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno);
152
153typedef bool (*SlruScanCallback) (SlruCtl ctl, char *filename, int segpage,
154 void *data);
155extern bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data);
156extern void SlruDeleteSegment(SlruCtl ctl, int segno);
157
158/* SlruScanDirectory public callbacks */
159extern bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename,
160 int segpage, void *data);
161extern bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage,
162 void *data);
163
164#endif /* SLRU_H */
165