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-2025, 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
44#define TestSlruCtl (&TestSlruCtlData)
45
48
49static bool
51{
52 elog(NOTICE, "Calling test_slru_scan_cb()");
53 return SlruScanDirCbDeleteAll(ctl, filename, segpage, data);
54}
55
58{
59 int64 pageno = PG_GETARG_INT64(0);
61 int slotno;
63
65 slotno = SimpleLruZeroPage(TestSlruCtl, pageno);
66
67 /* these should match */
68 Assert(TestSlruCtl->shared->page_number[slotno] == pageno);
69
70 /* mark the page as dirty so as it would get written */
71 TestSlruCtl->shared->page_dirty[slotno] = true;
72 TestSlruCtl->shared->page_status[slotno] = SLRU_PAGE_VALID;
73
74 /* write given data to the page, up to the limit of the page */
75 strncpy(TestSlruCtl->shared->page_buffer[slotno], data,
76 BLCKSZ - 1);
77
79 LWLockRelease(lock);
80
82}
83
86{
89}
90
93{
94 int64 pageno = PG_GETARG_INT64(0);
95 bool write_ok = PG_GETARG_BOOL(1);
96 char *data = NULL;
97 int slotno;
99
100 /* find page in buffers, reading it if necessary */
102 slotno = SimpleLruReadPage(TestSlruCtl, pageno,
103 write_ok, InvalidTransactionId);
104 data = (char *) TestSlruCtl->shared->page_buffer[slotno];
105 LWLockRelease(lock);
106
108}
109
110Datum
112{
113 int64 pageno = PG_GETARG_INT64(0);
114 char *data = NULL;
115 int slotno;
116 LWLock *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
117
118 /* find page in buffers, reading it if necessary */
120 pageno,
122 Assert(LWLockHeldByMe(lock));
123 data = (char *) TestSlruCtl->shared->page_buffer[slotno];
124 LWLockRelease(lock);
125
127}
128
129Datum
131{
132 int64 pageno = PG_GETARG_INT64(0);
133 bool found;
134 LWLock *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
135
138 LWLockRelease(lock);
139
140 PG_RETURN_BOOL(found);
141}
142
143Datum
145{
146 int64 pageno = PG_GETARG_INT64(0);
147 FileTag ftag;
148 char path[MAXPGPATH];
149
150 /* note that this flushes the full file a segment is located in */
151 ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
152 SlruSyncFileTag(TestSlruCtl, &ftag, path);
153
154 elog(NOTICE, "Called SlruSyncFileTag() for segment %lld on path %s",
155 (long long) ftag.segno, path);
156
158}
159
160Datum
162{
163 int64 pageno = PG_GETARG_INT64(0);
164 FileTag ftag;
165
166 ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
168
169 elog(NOTICE, "Called SlruDeleteSegment() for segment %lld",
170 (long long) ftag.segno);
171
173}
174
175Datum
177{
178 int64 pageno = PG_GETARG_INT64(0);
179
182}
183
184Datum
186{
187 /* this calls SlruScanDirCbDeleteAll() internally, ensuring deletion */
189
191}
192
193/*
194 * Module load callbacks and initialization.
195 */
196
197static void
199{
202
203 /* reserve shared memory for the test SLRU */
205}
206
207static bool
209{
210 return page1 < page2;
211}
212
213static void
215{
216 /*
217 * Short segments names are well tested elsewhere so in this test we are
218 * focusing on long names.
219 */
220 const bool long_segment_names = true;
221 const char slru_dir_name[] = "pg_test_slru";
222 int test_tranche_id;
223 int test_buffer_tranche_id;
224
227
228 /*
229 * Create the SLRU directory if it does not exist yet, from the root of
230 * the data directory.
231 */
232 (void) MakePGDirectory(slru_dir_name);
233
234 /* initialize the SLRU facility */
235 test_tranche_id = LWLockNewTrancheId();
236 LWLockRegisterTranche(test_tranche_id, "test_slru_tranche");
237
238 test_buffer_tranche_id = LWLockNewTrancheId();
239 LWLockRegisterTranche(test_tranche_id, "test_buffer_tranche");
240
242 SimpleLruInit(TestSlruCtl, "TestSLRU",
243 NUM_TEST_BUFFERS, 0, slru_dir_name,
244 test_buffer_tranche_id, test_tranche_id, SYNC_HANDLER_NONE,
245 long_segment_names);
246}
247
248void
250{
253 (errmsg("cannot load \"%s\" after startup", "test_slru"),
254 errdetail("\"%s\" must be loaded with \"shared_preload_libraries\".",
255 "test_slru")));
256
259
262}
#define Assert(condition)
Definition: c.h:815
int64_t int64
Definition: c.h:485
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3936
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#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:57
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:73
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1893
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:628
int LWLockNewTrancheId(void)
Definition: lwlock.c:603
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void(* shmem_request_hook_type)(void)
Definition: miscadmin.h:522
shmem_request_hook_type shmem_request_hook
Definition: miscinit.c:1837
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1834
#define MAXPGPATH
const void * data
static char * filename
Definition: pg_dumpall.c:119
uintptr_t Datum
Definition: postgres.h:69
tree ctl
Definition: radixtree.h:1838
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
Definition: slru.c:252
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
Definition: slru.c:605
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:732
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1322
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
Definition: slru.c:746
void SlruDeleteSegment(SlruCtl ctl, int64 segno)
Definition: slru.c:1526
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1791
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition: slru.c:1744
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition: slru.c:502
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1831
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition: slru.c:375
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition: slru.c:1408
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:199
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition: slru.h:175
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:39
@ SLRU_PAGE_VALID
Definition: slru.h:51
Definition: sync.h:51
uint64 segno
Definition: sync.h:55
Definition: lwlock.h:42
@ SYNC_HANDLER_NONE
Definition: sync.h:42
static void test_slru_shmem_startup(void)
Definition: test_slru.c:214
Datum test_slru_page_delete(PG_FUNCTION_ARGS)
Definition: test_slru.c:161
static bool test_slru_page_precedes_logically(int64 page1, int64 page2)
Definition: test_slru.c:208
Datum test_slru_page_write(PG_FUNCTION_ARGS)
Definition: test_slru.c:57
void _PG_init(void)
Definition: test_slru.c:249
Datum test_slru_page_exists(PG_FUNCTION_ARGS)
Definition: test_slru.c:130
Datum test_slru_page_writeall(PG_FUNCTION_ARGS)
Definition: test_slru.c:85
PG_MODULE_MAGIC
Definition: test_slru.c:25
Datum test_slru_page_readonly(PG_FUNCTION_ARGS)
Definition: test_slru.c:111
Datum test_slru_page_truncate(PG_FUNCTION_ARGS)
Definition: test_slru.c:176
static shmem_startup_hook_type prev_shmem_startup_hook
Definition: test_slru.c:47
static shmem_request_hook_type prev_shmem_request_hook
Definition: test_slru.c:46
Datum test_slru_page_read(PG_FUNCTION_ARGS)
Definition: test_slru.c:92
#define NUM_TEST_BUFFERS
Definition: test_slru.c:41
Datum test_slru_delete_all(PG_FUNCTION_ARGS)
Definition: test_slru.c:185
static void test_slru_shmem_request(void)
Definition: test_slru.c:198
PG_FUNCTION_INFO_V1(test_slru_page_write)
static SlruCtlData TestSlruCtlData
Definition: test_slru.c:43
Datum test_slru_page_sync(PG_FUNCTION_ARGS)
Definition: test_slru.c:144
static bool test_slru_scan_cb(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition: test_slru.c:50
#define TestSlruCtl
Definition: test_slru.c:44
#define InvalidTransactionId
Definition: transam.h:31
text * cstring_to_text(const char *s)
Definition: varlena.c:184
char * text_to_cstring(const text *t)
Definition: varlena.c:217