codekingpro/portable-devtools
114k
1/*-------------------------------------------------------------------------2 *3 * slru.h4 * Simple LRU buffering for transaction status logfiles5 *6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group7 * Portions Copyright (c) 1994, Regents of the University of California8 *9 * src/include/access/slru.h10 *11 *-------------------------------------------------------------------------12 */13#ifndef SLRU_H14#define SLRU_H15 16#include "access/xlogdefs.h"17#include "storage/lwlock.h"18#include "storage/sync.h"19 20 21/*22 * Define SLRU segment size. A page is the same BLCKSZ as is used everywhere23 * else in Postgres. The segment size can be chosen somewhat arbitrarily;24 * we make it 32 pages by default, or 256Kb, i.e. 1M transactions for CLOG25 * or 64K transactions for SUBTRANS.26 *27 * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,28 * page numbering also wraps around at 0xFFFFFFFF/xxxx_XACTS_PER_PAGE (where29 * xxxx is CLOG or SUBTRANS, respectively), and segment numbering at30 * 0xFFFFFFFF/xxxx_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need31 * take no explicit notice of that fact in slru.c, except when comparing32 * segment and page numbers in SimpleLruTruncate (see PagePrecedes()).33 */34#define SLRU_PAGES_PER_SEGMENT 3235 36/*37 * Page status codes. Note that these do not include the "dirty" bit.38 * page_dirty can be true only in the VALID or WRITE_IN_PROGRESS states;39 * in the latter case it implies that the page has been re-dirtied since40 * the write started.41 */42typedef enum43{44 SLRU_PAGE_EMPTY, /* buffer is not in use */45 SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */46 SLRU_PAGE_VALID, /* page is valid and not being written */47 SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */48} SlruPageStatus;49 50/*51 * Shared-memory state52 */53typedef struct SlruSharedData54{55 LWLock *ControlLock;56 57 /* Number of buffers managed by this SLRU structure */58 int num_slots;59 60 /*61 * Arrays holding info for each buffer slot. Page number is undefined62 * when status is EMPTY, as is page_lru_count.63 */64 char **page_buffer;65 SlruPageStatus *page_status;66 bool *page_dirty;67 int *page_number;68 int *page_lru_count;69 LWLockPadded *buffer_locks;70 71 /*72 * Optional array of WAL flush LSNs associated with entries in the SLRU73 * pages. If not zero/NULL, we must flush WAL before writing pages (true74 * for pg_xact, false for multixact, pg_subtrans, pg_notify). group_lsn[]75 * has lsn_groups_per_page entries per buffer slot, each containing the76 * highest LSN known for a contiguous group of SLRU entries on that slot's77 * page.78 */79 XLogRecPtr *group_lsn;80 int lsn_groups_per_page;81 82 /*----------83 * We mark a page "most recently used" by setting84 * page_lru_count[slotno] = ++cur_lru_count;85 * The oldest page is therefore the one with the highest value of86 * cur_lru_count - page_lru_count[slotno]87 * The counts will eventually wrap around, but this calculation still88 * works as long as no page's age exceeds INT_MAX counts.89 *----------90 */91 int cur_lru_count;92 93 /*94 * latest_page_number is the page number of the current end of the log;95 * this is not critical data, since we use it only to avoid swapping out96 * the latest page.97 */98 int latest_page_number;99 100 /* SLRU's index for statistics purposes (might not be unique) */101 int slru_stats_idx;102} SlruSharedData;103 104typedef SlruSharedData *SlruShared;105 106/*107 * SlruCtlData is an unshared structure that points to the active information108 * in shared memory.109 */110typedef struct SlruCtlData111{112 SlruShared shared;113 114 /*115 * Which sync handler function to use when handing sync requests over to116 * the checkpointer. SYNC_HANDLER_NONE to disable fsync (eg pg_notify).117 */118 SyncRequestHandler sync_handler;119 120 /*121 * Decide whether a page is "older" for truncation and as a hint for122 * evicting pages in LRU order. Return true if every entry of the first123 * argument is older than every entry of the second argument. Note that124 * !PagePrecedes(a,b) && !PagePrecedes(b,a) need not imply a==b; it also125 * arises when some entries are older and some are not. For SLRUs using126 * SimpleLruTruncate(), this must use modular arithmetic. (For others,127 * the behavior of this callback has no functional implications.) Use128 * SlruPagePrecedesUnitTests() in SLRUs meeting its criteria.129 */130 bool (*PagePrecedes) (int, int);131 132 /*133 * Dir is set during SimpleLruInit and does not change thereafter. Since134 * it's always the same, it doesn't need to be in shared memory.135 */136 char Dir[64];137} SlruCtlData;138 139typedef SlruCtlData *SlruCtl;140 141 142extern Size SimpleLruShmemSize(int nslots, int nlsns);143extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,144 LWLock *ctllock, const char *subdir, int tranche_id,145 SyncRequestHandler sync_handler);146extern int SimpleLruZeroPage(SlruCtl ctl, int pageno);147extern int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,148 TransactionId xid);149extern int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno,150 TransactionId xid);151extern void SimpleLruWritePage(SlruCtl ctl, int slotno);152extern void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied);153#ifdef USE_ASSERT_CHECKING154extern void SlruPagePrecedesUnitTests(SlruCtl ctl, int per_page);155#else156#define SlruPagePrecedesUnitTests(ctl, per_page) do {} while (0)157#endif158extern void SimpleLruTruncate(SlruCtl ctl, int cutoffPage);159extern bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno);160 161typedef bool (*SlruScanCallback) (SlruCtl ctl, char *filename, int segpage,162 void *data);163extern bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data);164extern void SlruDeleteSegment(SlruCtl ctl, int segno);165 166extern int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path);167 168/* SlruScanDirectory public callbacks */169extern bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename,170 int segpage, void *data);171extern bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage,172 void *data);173 174#endif /* SLRU_H */175 