1/*--------------------------------------------------------------------------
4 * Test correctness of SLRU functions.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/test/modules/test_slru/test_slru.c
12 * -------------------------------------------------------------------------
28 * SQL-callable entry points
40/* Number of SLRU page slots */
41 #define NUM_TEST_BUFFERS 16
44 #define TestSlruCtl (&TestSlruCtlData)
67 /* these should match */
70 /* mark the page as dirty so as it would get written */
74 /* write given data to the page, up to the limit of the page */
100 /* find page in buffers, reading it if necessary */
118 /* find page in buffers, reading it if necessary */
150 /* note that this flushes the full file a segment is located in */
154 elog(
NOTICE,
"Called SlruSyncFileTag() for segment %" PRIu64
" on path %s",
169 elog(
NOTICE,
"Called SlruDeleteSegment() for segment %" PRIu64,
187 /* this calls SlruScanDirCbDeleteAll() internally, ensuring deletion */
194 * Module load callbacks and initialization.
203 /* reserve shared memory for the test SLRU */
210 return page1 < page2;
217 * Short segments names are well tested elsewhere so in this test we are
218 * focusing on long names.
220 const bool long_segment_names =
true;
221 const char slru_dir_name[] =
"pg_test_slru";
222 int test_tranche_id = -1;
223 int test_buffer_tranche_id = -1;
229 * Create the SLRU directory if it does not exist yet, from the root of
230 * the data directory.
235 * Initialize the SLRU facility. In EXEC_BACKEND builds, the
236 * shmem_startup_hook is called in the postmaster and in each backend, but
237 * we only need to generate the LWLock tranches once. Note that these
238 * tranche ID variables are not used by SimpleLruInit() when
239 * IsUnderPostmaster is true.
259 (
errmsg(
"cannot load \"%s\" after startup",
"test_slru"),
260 errdetail(
"\"%s\" must be loaded with \"shared_preload_libraries\".",
int errdetail(const char *fmt,...)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
int MakePGDirectory(const char *directoryName)
#define PG_GETARG_TEXT_PP(n)
#define PG_GETARG_INT64(n)
#define PG_RETURN_TEXT_P(x)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_BOOL(x)
Assert(PointerIsAligned(start, uint64))
void(* shmem_startup_hook_type)(void)
shmem_startup_hook_type shmem_startup_hook
void RequestAddinShmemSpace(Size size)
bool LWLockHeldByMe(LWLock *lock)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
int LWLockNewTrancheId(const char *name)
void LWLockRelease(LWLock *lock)
void(* shmem_request_hook_type)(void)
shmem_request_hook_type shmem_request_hook
bool process_shared_preload_libraries_in_progress
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)
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
void SimpleLruWritePage(SlruCtl ctl, int slotno)
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
void SlruDeleteSegment(SlruCtl ctl, int64 segno)
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Size SimpleLruShmemSize(int nslots, int nlsns)
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
#define SLRU_PAGES_PER_SEGMENT
static void test_slru_shmem_startup(void)
Datum test_slru_page_delete(PG_FUNCTION_ARGS)
static bool test_slru_page_precedes_logically(int64 page1, int64 page2)
Datum test_slru_page_write(PG_FUNCTION_ARGS)
Datum test_slru_page_exists(PG_FUNCTION_ARGS)
Datum test_slru_page_writeall(PG_FUNCTION_ARGS)
Datum test_slru_page_readonly(PG_FUNCTION_ARGS)
Datum test_slru_page_truncate(PG_FUNCTION_ARGS)
static shmem_startup_hook_type prev_shmem_startup_hook
static shmem_request_hook_type prev_shmem_request_hook
Datum test_slru_page_read(PG_FUNCTION_ARGS)
Datum test_slru_delete_all(PG_FUNCTION_ARGS)
static void test_slru_shmem_request(void)
PG_FUNCTION_INFO_V1(test_slru_page_write)
static SlruCtlData TestSlruCtlData
Datum test_slru_page_sync(PG_FUNCTION_ARGS)
static bool test_slru_scan_cb(SlruCtl ctl, char *filename, int64 segpage, void *data)
#define InvalidTransactionId
text * cstring_to_text(const char *s)
char * text_to_cstring(const text *t)