PostgreSQL Source Code  git master
test_slru.c
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------
2  *
3  * test_slru.c
4  * Test correctness of SLRU functions.
5  *
6  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/test/modules/test_slru/test_slru.c
11  *
12  * -------------------------------------------------------------------------
13  */
14 
15 #include "postgres.h"
16 
17 #include "access/slru.h"
18 #include "access/transam.h"
19 #include "miscadmin.h"
20 #include "storage/fd.h"
21 #include "storage/ipc.h"
22 #include "storage/shmem.h"
23 #include "utils/builtins.h"
24 
26 
27 /*
28  * SQL-callable entry points
29  */
39 
40 /* Number of SLRU page slots */
41 #define NUM_TEST_BUFFERS 16
42 
43 /* SLRU control lock */
45 #define TestSLRULock (&TestSLRULock)
46 
48 #define TestSlruCtl (&TestSlruCtlData)
49 
52 
53 /* LWLock name */
54 const char test_tranche_name[] = "test_slru_tranche";
55 
56 static bool
57 test_slru_scan_cb(SlruCtl ctl, char *filename, int segpage, void *data)
58 {
59  elog(NOTICE, "Calling test_slru_scan_cb()");
60  return SlruScanDirCbDeleteAll(ctl, filename, segpage, data);
61 }
62 
63 Datum
65 {
66  int pageno = PG_GETARG_INT32(0);
68  int slotno;
69 
71 
72  slotno = SimpleLruZeroPage(TestSlruCtl, pageno);
73 
74  /* these should match */
75  Assert(TestSlruCtl->shared->page_number[slotno] == pageno);
76 
77  /* mark the page as dirty so as it would get written */
78  TestSlruCtl->shared->page_dirty[slotno] = true;
79  TestSlruCtl->shared->page_status[slotno] = SLRU_PAGE_VALID;
80 
81  /* write given data to the page, up to the limit of the page */
82  strncpy(TestSlruCtl->shared->page_buffer[slotno], data,
83  BLCKSZ - 1);
84 
87 
89 }
90 
91 Datum
93 {
96 }
97 
98 Datum
100 {
101  int pageno = PG_GETARG_INT32(0);
102  bool write_ok = PG_GETARG_BOOL(1);
103  char *data = NULL;
104  int slotno;
105 
106  /* find page in buffers, reading it if necessary */
108  slotno = SimpleLruReadPage(TestSlruCtl, pageno,
109  write_ok, InvalidTransactionId);
110  data = (char *) TestSlruCtl->shared->page_buffer[slotno];
112 
114 }
115 
116 Datum
118 {
119  int pageno = PG_GETARG_INT32(0);
120  char *data = NULL;
121  int slotno;
122 
123  /* find page in buffers, reading it if necessary */
125  pageno,
128  data = (char *) TestSlruCtl->shared->page_buffer[slotno];
130 
132 }
133 
134 Datum
136 {
137  int pageno = PG_GETARG_INT32(0);
138  bool found;
139 
143 
144  PG_RETURN_BOOL(found);
145 }
146 
147 Datum
149 {
150  int pageno = PG_GETARG_INT32(0);
151  FileTag ftag;
152  char path[MAXPGPATH];
153 
154  /* note that this flushes the full file a segment is located in */
155  ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
156  SlruSyncFileTag(TestSlruCtl, &ftag, path);
157 
158  elog(NOTICE, "Called SlruSyncFileTag() for segment %d on path %s",
159  ftag.segno, path);
160 
161  PG_RETURN_VOID();
162 }
163 
164 Datum
166 {
167  int pageno = PG_GETARG_INT32(0);
168  FileTag ftag;
169 
170  ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
172 
173  elog(NOTICE, "Called SlruDeleteSegment() for segment %d", ftag.segno);
174 
175  PG_RETURN_VOID();
176 }
177 
178 Datum
180 {
181  int pageno = PG_GETARG_INT32(0);
182 
184  PG_RETURN_VOID();
185 }
186 
187 Datum
189 {
190  /* this calls SlruScanDirCbDeleteAll() internally, ensuring deletion */
192 
193  PG_RETURN_VOID();
194 }
195 
196 /*
197  * Module load callbacks and initialization.
198  */
199 
200 static void
202 {
205 
206  /* reserve shared memory for the test SLRU */
208 }
209 
210 static bool
212 {
213  return page1 < page2;
214 }
215 
216 static void
218 {
219  const char slru_dir_name[] = "pg_test_slru";
220  int test_tranche_id;
221 
224 
225  /*
226  * Create the SLRU directory if it does not exist yet, from the root of
227  * the data directory.
228  */
229  (void) MakePGDirectory(slru_dir_name);
230 
231  /* initialize the SLRU facility */
232  test_tranche_id = LWLockNewTrancheId();
233  LWLockRegisterTranche(test_tranche_id, "test_slru_tranche");
234  LWLockInitialize(TestSLRULock, test_tranche_id);
235 
237  SimpleLruInit(TestSlruCtl, "TestSLRU",
238  NUM_TEST_BUFFERS, 0, TestSLRULock, slru_dir_name,
239  test_tranche_id, SYNC_HANDLER_NONE);
240 }
241 
242 void
243 _PG_init(void)
244 {
246  ereport(ERROR,
247  (errmsg("cannot load \"%s\" after startup", "test_slru"),
248  errdetail("\"%s\" must be loaded with shared_preload_libraries.",
249  "test_slru")));
250 
253 
256 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3713
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
void(* shmem_startup_hook_type)(void)
Definition: ipc.h:22
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:56
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:70
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1919
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:651
int LWLockNewTrancheId(void)
Definition: lwlock.c:627
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:730
@ LW_EXCLUSIVE
Definition: lwlock.h:115
void(* shmem_request_hook_type)(void)
Definition: miscadmin.h:486
shmem_request_hook_type shmem_request_hook
Definition: miscinit.c:1773
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1770
#define MAXPGPATH
const void * data
static char * filename
Definition: pg_dumpall.c:119
uintptr_t Datum
Definition: postgres.h:64
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:615
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1157
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1531
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler)
Definition: slru.c:188
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1227
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1554
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1594
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:281
void SlruDeleteSegment(SlruCtl ctl, int segno)
Definition: slru.c:1328
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:496
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
Definition: slru.c:627
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:396
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:156
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34
@ SLRU_PAGE_VALID
Definition: slru.h:46
Definition: sync.h:51
uint32 segno
Definition: sync.h:55
Definition: lwlock.h:40
@ SYNC_HANDLER_NONE
Definition: sync.h:42
static void test_slru_shmem_startup(void)
Definition: test_slru.c:217
Datum test_slru_page_delete(PG_FUNCTION_ARGS)
Definition: test_slru.c:165
static bool test_slru_page_precedes_logically(int page1, int page2)
Definition: test_slru.c:211
Datum test_slru_page_write(PG_FUNCTION_ARGS)
Definition: test_slru.c:64
void _PG_init(void)
Definition: test_slru.c:243
Datum test_slru_page_exists(PG_FUNCTION_ARGS)
Definition: test_slru.c:135
Datum test_slru_page_writeall(PG_FUNCTION_ARGS)
Definition: test_slru.c:92
PG_MODULE_MAGIC
Definition: test_slru.c:25
const char test_tranche_name[]
Definition: test_slru.c:54
Datum test_slru_page_readonly(PG_FUNCTION_ARGS)
Definition: test_slru.c:117
#define TestSLRULock
Definition: test_slru.c:45
Datum test_slru_page_truncate(PG_FUNCTION_ARGS)
Definition: test_slru.c:179
static shmem_startup_hook_type prev_shmem_startup_hook
Definition: test_slru.c:51
static shmem_request_hook_type prev_shmem_request_hook
Definition: test_slru.c:50
Datum test_slru_page_read(PG_FUNCTION_ARGS)
Definition: test_slru.c:99
#define NUM_TEST_BUFFERS
Definition: test_slru.c:41
Datum test_slru_delete_all(PG_FUNCTION_ARGS)
Definition: test_slru.c:188
static void test_slru_shmem_request(void)
Definition: test_slru.c:201
PG_FUNCTION_INFO_V1(test_slru_page_write)
static SlruCtlData TestSlruCtlData
Definition: test_slru.c:47
Datum test_slru_page_sync(PG_FUNCTION_ARGS)
Definition: test_slru.c:148
static bool test_slru_scan_cb(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: test_slru.c:57
#define TestSlruCtl
Definition: test_slru.c:48
#define InvalidTransactionId
Definition: transam.h:31
char * text_to_cstring(const text *t)
Definition: varlena.c:222
text * cstring_to_text(const char *s)
Definition: varlena.c:189