PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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
43static void test_slru_shmem_request(void *arg);
45static int test_slru_errdetail_for_io_error(const void *opaque_data);
46
47static const char *TestSlruDir = "pg_test_slru";
48
50
54
55#define TestSlruCtl (&TestSlruDesc)
56
57static bool
59{
60 elog(NOTICE, "Calling test_slru_scan_cb()");
62}
63
66{
67 int64 pageno = PG_GETARG_INT64(0);
69 int slotno;
71
74
75 /* these should match */
76 Assert(TestSlruCtl->shared->page_number[slotno] == pageno);
77
78 /* mark the page as dirty so as it would get written */
79 TestSlruCtl->shared->page_dirty[slotno] = true;
80 TestSlruCtl->shared->page_status[slotno] = SLRU_PAGE_VALID;
81
82 /* write given data to the page, up to the limit of the page */
83 strncpy(TestSlruCtl->shared->page_buffer[slotno], data,
84 BLCKSZ - 1);
85
87 LWLockRelease(lock);
88
90}
91
98
101{
102 int64 pageno = PG_GETARG_INT64(0);
103 bool write_ok = PG_GETARG_BOOL(1);
105 char *data = NULL;
106 int slotno;
107 LWLock *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
108
109 /* find page in buffers, reading it if necessary */
112 data = (char *) TestSlruCtl->shared->page_buffer[slotno];
113 LWLockRelease(lock);
114
116}
117
118Datum
120{
121 int64 pageno = PG_GETARG_INT64(0);
122 char *data = NULL;
123 int slotno;
124 LWLock *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
125
126 /* find page in buffers, reading it if necessary */
128 pageno,
129 NULL);
130 Assert(LWLockHeldByMe(lock));
131 data = (char *) TestSlruCtl->shared->page_buffer[slotno];
132 LWLockRelease(lock);
133
135}
136
137Datum
139{
140 int64 pageno = PG_GETARG_INT64(0);
141 bool found;
142 LWLock *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
143
146 LWLockRelease(lock);
147
148 PG_RETURN_BOOL(found);
149}
150
151Datum
153{
154 int64 pageno = PG_GETARG_INT64(0);
155 FileTag ftag;
156 char path[MAXPGPATH];
157
158 /* note that this flushes the full file a segment is located in */
159 ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
160 SlruSyncFileTag(TestSlruCtl, &ftag, path);
161
162 elog(NOTICE, "Called SlruSyncFileTag() for segment %" PRIu64 " on path %s",
163 ftag.segno, path);
164
166}
167
168Datum
170{
171 int64 pageno = PG_GETARG_INT64(0);
172 FileTag ftag;
173
174 ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
176
177 elog(NOTICE, "Called SlruDeleteSegment() for segment %" PRIu64,
178 ftag.segno);
179
181}
182
183Datum
191
192Datum
194{
195 /* this calls SlruScanDirCbDeleteAll() internally, ensuring deletion */
197
199}
200
201static bool
206
207static int
209{
210 TransactionId xid = *(const TransactionId *) opaque_data;
211
212 return errdetail("Could not access test_slru entry %u.", xid);
213}
214
215void
217{
220 (errmsg("cannot load \"%s\" after startup", "test_slru"),
221 errdetail("\"%s\" must be loaded with \"shared_preload_libraries\".",
222 "test_slru")));
223
224 /*
225 * Create the SLRU directory if it does not exist yet, from the root of
226 * the data directory.
227 */
229
231}
232
233static void
235{
237 .name = "TestSLRU",
238 .Dir = TestSlruDir,
239
240 /*
241 * Short segments names are well tested elsewhere so in this test we are
242 * focusing on long names.
243 */
244 .long_segment_names = true,
245
246 .nslots = NUM_TEST_BUFFERS,
247 .nlsns = 0,
248
249 .sync_handler = SYNC_HANDLER_NONE,
250 .PagePrecedes = test_slru_page_precedes_logically,
251 .errdetail_for_io_error = test_slru_errdetail_for_io_error,
252
253 /* let slru.c assign these */
254 .buffer_tranche_id = 0,
255 .bank_tranche_id = 0,
256 );
257}
#define Assert(condition)
Definition c.h:943
int64_t int64
Definition c.h:621
uint32 TransactionId
Definition c.h:736
Datum arg
Definition elog.c:1322
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define NOTICE
Definition elog.h:36
#define ereport(elevel,...)
Definition elog.h:152
int MakePGDirectory(const char *directoryName)
Definition fd.c:3963
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_TRANSACTIONID(n)
Definition fmgr.h:280
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#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:360
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1885
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1788
static char * errmsg
#define MAXPGPATH
#define SLRU_PAGES_PER_SEGMENT
const void * data
static char * filename
Definition pg_dumpall.c:133
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
tree ctl
Definition radixtree.h:1838
void RegisterShmemCallbacks(const ShmemCallbacks *callbacks)
Definition shmem.c:874
bool SlruScanDirectory(SlruDesc *ctl, SlruScanCallback callback, void *data)
Definition slru.c:1844
int SimpleLruReadPage_ReadOnly(SlruDesc *ctl, int64 pageno, const void *opaque_data)
Definition slru.c:654
void SimpleLruTruncate(SlruDesc *ctl, int64 cutoffPage)
Definition slru.c:1458
void SlruDeleteSegment(SlruDesc *ctl, int64 segno)
Definition slru.c:1576
int SimpleLruZeroPage(SlruDesc *ctl, int64 pageno)
Definition slru.c:397
bool SimpleLruDoesPhysicalPageExist(SlruDesc *ctl, int64 pageno)
Definition slru.c:795
bool SlruScanDirCbDeleteAll(SlruDesc *ctl, char *filename, int64 segpage, void *data)
Definition slru.c:1797
void SimpleLruWritePage(SlruDesc *ctl, int slotno)
Definition slru.c:781
void SimpleLruWriteAll(SlruDesc *ctl, bool allow_redirtied)
Definition slru.c:1372
int SimpleLruReadPage(SlruDesc *ctl, int64 pageno, bool write_ok, const void *opaque_data)
Definition slru.c:550
int SlruSyncFileTag(SlruDesc *ctl, const FileTag *ftag, char *path)
Definition slru.c:1884
#define SimpleLruRequest(...)
Definition slru.h:218
static LWLock * SimpleLruGetBankLock(SlruDesc *ctl, int64 pageno)
Definition slru.h:207
@ SLRU_PAGE_VALID
Definition slru.h:38
Definition sync.h:51
uint64 segno
Definition sync.h:55
ShmemRequestCallback request_fn
Definition shmem.h:133
@ SYNC_HANDLER_NONE
Definition sync.h:42
static const char * TestSlruDir
Definition test_slru.c:47
Datum test_slru_page_delete(PG_FUNCTION_ARGS)
Definition test_slru.c:169
static bool test_slru_page_precedes_logically(int64 page1, int64 page2)
Definition test_slru.c:202
Datum test_slru_page_write(PG_FUNCTION_ARGS)
Definition test_slru.c:65
void _PG_init(void)
Definition test_slru.c:216
Datum test_slru_page_exists(PG_FUNCTION_ARGS)
Definition test_slru.c:138
Datum test_slru_page_writeall(PG_FUNCTION_ARGS)
Definition test_slru.c:93
static SlruDesc TestSlruDesc
Definition test_slru.c:49
PG_MODULE_MAGIC
Definition test_slru.c:25
Datum test_slru_page_readonly(PG_FUNCTION_ARGS)
Definition test_slru.c:119
static void test_slru_shmem_request(void *arg)
Definition test_slru.c:234
Datum test_slru_page_truncate(PG_FUNCTION_ARGS)
Definition test_slru.c:184
static bool test_slru_scan_cb(SlruDesc *ctl, char *filename, int64 segpage, void *data)
Definition test_slru.c:58
Datum test_slru_page_read(PG_FUNCTION_ARGS)
Definition test_slru.c:100
#define NUM_TEST_BUFFERS
Definition test_slru.c:41
Datum test_slru_delete_all(PG_FUNCTION_ARGS)
Definition test_slru.c:193
static const ShmemCallbacks test_slru_shmem_callbacks
Definition test_slru.c:51
Datum test_slru_page_sync(PG_FUNCTION_ARGS)
Definition test_slru.c:152
static int test_slru_errdetail_for_io_error(const void *opaque_data)
Definition test_slru.c:208
#define TestSlruCtl
Definition test_slru.c:55
text * cstring_to_text(const char *s)
Definition varlena.c:184
char * text_to_cstring(const text *t)
Definition varlena.c:217
const char * name