]> granicus.if.org Git - postgresql/blob - src/include/access/slru.h
Replace the pg_listener-based LISTEN/NOTIFY mechanism with an in-memory queue.
[postgresql] / src / include / access / slru.h
1 /*-------------------------------------------------------------------------
2  *
3  * slru.h
4  *              Simple LRU buffering for transaction status logfiles
5  *
6  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $PostgreSQL: pgsql/src/include/access/slru.h,v 1.26 2010/02/16 22:34:50 tgl Exp $
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 /*
40  * Page status codes.  Note that these do not include the "dirty" bit.
41  * page_dirty can be TRUE only in the VALID or WRITE_IN_PROGRESS states;
42  * in the latter case it implies that the page has been re-dirtied since
43  * the write started.
44  */
45 typedef enum
46 {
47         SLRU_PAGE_EMPTY,                        /* buffer is not in use */
48         SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
49         SLRU_PAGE_VALID,                        /* page is valid and not being written */
50         SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */
51 } SlruPageStatus;
52
53 /*
54  * Shared-memory state
55  */
56 typedef struct SlruSharedData
57 {
58         LWLockId        ControlLock;
59
60         /* Number of buffers managed by this SLRU structure */
61         int                     num_slots;
62
63         /*
64          * Arrays holding info for each buffer slot.  Page number is undefined
65          * when status is EMPTY, as is page_lru_count.
66          */
67         char      **page_buffer;
68         SlruPageStatus *page_status;
69         bool       *page_dirty;
70         int                *page_number;
71         int                *page_lru_count;
72         LWLockId   *buffer_locks;
73
74         /*
75          * Optional array of WAL flush LSNs associated with entries in the SLRU
76          * pages.  If not zero/NULL, we must flush WAL before writing pages (true
77          * for pg_clog, false for multixact, pg_subtrans, pg_notify).  group_lsn[]
78          * has lsn_groups_per_page entries per buffer slot, each containing the
79          * highest LSN known for a contiguous group of SLRU entries on that slot's
80          * page.
81          */
82         XLogRecPtr *group_lsn;
83         int                     lsn_groups_per_page;
84
85         /*----------
86          * We mark a page "most recently used" by setting
87          *              page_lru_count[slotno] = ++cur_lru_count;
88          * The oldest page is therefore the one with the highest value of
89          *              cur_lru_count - page_lru_count[slotno]
90          * The counts will eventually wrap around, but this calculation still
91          * works as long as no page's age exceeds INT_MAX counts.
92          *----------
93          */
94         int                     cur_lru_count;
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          */
101         int                     latest_page_number;
102 } SlruSharedData;
103
104 typedef SlruSharedData *SlruShared;
105
106 /*
107  * SlruCtlData is an unshared structure that points to the active information
108  * in shared memory.
109  */
110 typedef struct SlruCtlData
111 {
112         SlruShared      shared;
113
114         /*
115          * This flag tells whether to fsync writes (true for pg_clog and multixact
116          * stuff, false for pg_subtrans and pg_notify).
117          */
118         bool            do_fsync;
119
120         /*
121          * Decide which of two page numbers is "older" for truncation purposes. We
122          * need to use comparison of TransactionIds here in order to do the right
123          * thing with wraparound XID arithmetic.
124          */
125         bool            (*PagePrecedes) (int, int);
126
127         /*
128          * Dir is set during SimpleLruInit and does not change thereafter. Since
129          * it's always the same, it doesn't need to be in shared memory.
130          */
131         char            Dir[64];
132 } SlruCtlData;
133
134 typedef SlruCtlData *SlruCtl;
135
136 /* Opaque struct known only in slru.c */
137 typedef struct SlruFlushData *SlruFlush;
138
139
140 extern Size SimpleLruShmemSize(int nslots, int nlsns);
141 extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
142                           LWLockId ctllock, const char *subdir);
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, SlruFlush fdata);
149 extern void SimpleLruFlush(SlruCtl ctl, bool checkpoint);
150 extern void SimpleLruTruncate(SlruCtl ctl, int cutoffPage);
151 extern bool SlruScanDirectory(SlruCtl ctl, int cutoffPage, bool doDeletions);
152
153 #endif   /* SLRU_H */