PostgreSQL Source Code git master
Loading...
Searching...
No Matches
test_slru.c File Reference
#include "postgres.h"
#include "access/slru.h"
#include "access/transam.h"
#include "miscadmin.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/shmem.h"
#include "utils/builtins.h"
Include dependency graph for test_slru.c:

Go to the source code of this file.

Macros

#define NUM_TEST_BUFFERS   16
 
#define TestSlruCtl   (&TestSlruDesc)
 

Functions

 PG_FUNCTION_INFO_V1 (test_slru_page_write)
 
 PG_FUNCTION_INFO_V1 (test_slru_page_writeall)
 
 PG_FUNCTION_INFO_V1 (test_slru_page_read)
 
 PG_FUNCTION_INFO_V1 (test_slru_page_readonly)
 
 PG_FUNCTION_INFO_V1 (test_slru_page_exists)
 
 PG_FUNCTION_INFO_V1 (test_slru_page_sync)
 
 PG_FUNCTION_INFO_V1 (test_slru_page_delete)
 
 PG_FUNCTION_INFO_V1 (test_slru_page_truncate)
 
 PG_FUNCTION_INFO_V1 (test_slru_delete_all)
 
static void test_slru_shmem_request (void *arg)
 
static bool test_slru_page_precedes_logically (int64 page1, int64 page2)
 
static int test_slru_errdetail_for_io_error (const void *opaque_data)
 
static bool test_slru_scan_cb (SlruDesc *ctl, char *filename, int64 segpage, void *data)
 
Datum test_slru_page_write (PG_FUNCTION_ARGS)
 
Datum test_slru_page_writeall (PG_FUNCTION_ARGS)
 
Datum test_slru_page_read (PG_FUNCTION_ARGS)
 
Datum test_slru_page_readonly (PG_FUNCTION_ARGS)
 
Datum test_slru_page_exists (PG_FUNCTION_ARGS)
 
Datum test_slru_page_sync (PG_FUNCTION_ARGS)
 
Datum test_slru_page_delete (PG_FUNCTION_ARGS)
 
Datum test_slru_page_truncate (PG_FUNCTION_ARGS)
 
Datum test_slru_delete_all (PG_FUNCTION_ARGS)
 
void _PG_init (void)
 

Variables

 PG_MODULE_MAGIC
 
static const charTestSlruDir = "pg_test_slru"
 
static SlruDesc TestSlruDesc
 
static const ShmemCallbacks test_slru_shmem_callbacks
 

Macro Definition Documentation

◆ NUM_TEST_BUFFERS

#define NUM_TEST_BUFFERS   16

Definition at line 41 of file test_slru.c.

◆ TestSlruCtl

#define TestSlruCtl   (&TestSlruDesc)

Definition at line 55 of file test_slru.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 216 of file test_slru.c.

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}
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
int MakePGDirectory(const char *directoryName)
Definition fd.c:3963
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1788
static char * errmsg
static int fb(int x)
void RegisterShmemCallbacks(const ShmemCallbacks *callbacks)
Definition shmem.c:874
static const char * TestSlruDir
Definition test_slru.c:47
static const ShmemCallbacks test_slru_shmem_callbacks
Definition test_slru.c:51

References ereport, errdetail(), errmsg, ERROR, fb(), MakePGDirectory(), process_shared_preload_libraries_in_progress, RegisterShmemCallbacks(), test_slru_shmem_callbacks, and TestSlruDir.

◆ PG_FUNCTION_INFO_V1() [1/9]

PG_FUNCTION_INFO_V1 ( test_slru_delete_all  )

◆ PG_FUNCTION_INFO_V1() [2/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_delete  )

◆ PG_FUNCTION_INFO_V1() [3/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_exists  )

◆ PG_FUNCTION_INFO_V1() [4/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_read  )

◆ PG_FUNCTION_INFO_V1() [5/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_readonly  )

◆ PG_FUNCTION_INFO_V1() [6/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_sync  )

◆ PG_FUNCTION_INFO_V1() [7/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_truncate  )

◆ PG_FUNCTION_INFO_V1() [8/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_write  )

◆ PG_FUNCTION_INFO_V1() [9/9]

PG_FUNCTION_INFO_V1 ( test_slru_page_writeall  )

◆ test_slru_delete_all()

Datum test_slru_delete_all ( PG_FUNCTION_ARGS  )

Definition at line 193 of file test_slru.c.

194{
195 /* this calls SlruScanDirCbDeleteAll() internally, ensuring deletion */
197
199}
#define PG_RETURN_VOID()
Definition fmgr.h:350
bool SlruScanDirectory(SlruDesc *ctl, SlruScanCallback callback, void *data)
Definition slru.c:1844
static bool test_slru_scan_cb(SlruDesc *ctl, char *filename, int64 segpage, void *data)
Definition test_slru.c:58
#define TestSlruCtl
Definition test_slru.c:55

References fb(), PG_RETURN_VOID, SlruScanDirectory(), test_slru_scan_cb(), and TestSlruCtl.

◆ test_slru_errdetail_for_io_error()

static int test_slru_errdetail_for_io_error ( const void opaque_data)
static

Definition at line 208 of file test_slru.c.

209{
210 TransactionId xid = *(const TransactionId *) opaque_data;
211
212 return errdetail("Could not access test_slru entry %u.", xid);
213}
uint32 TransactionId
Definition c.h:736

References errdetail(), and fb().

Referenced by test_slru_shmem_request().

◆ test_slru_page_delete()

Datum test_slru_page_delete ( PG_FUNCTION_ARGS  )

Definition at line 169 of file test_slru.c.

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}
int64_t int64
Definition c.h:621
#define elog(elevel,...)
Definition elog.h:228
#define NOTICE
Definition elog.h:36
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define SLRU_PAGES_PER_SEGMENT
void SlruDeleteSegment(SlruDesc *ctl, int64 segno)
Definition slru.c:1576
Definition sync.h:51
uint64 segno
Definition sync.h:55

References elog, fb(), NOTICE, PG_GETARG_INT64, PG_RETURN_VOID, FileTag::segno, SLRU_PAGES_PER_SEGMENT, SlruDeleteSegment(), and TestSlruCtl.

◆ test_slru_page_exists()

Datum test_slru_page_exists ( PG_FUNCTION_ARGS  )

Definition at line 138 of file test_slru.c.

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}
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
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 SimpleLruDoesPhysicalPageExist(SlruDesc *ctl, int64 pageno)
Definition slru.c:795
static LWLock * SimpleLruGetBankLock(SlruDesc *ctl, int64 pageno)
Definition slru.h:207

References LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), PG_GETARG_INT64, PG_RETURN_BOOL, SimpleLruDoesPhysicalPageExist(), SimpleLruGetBankLock(), and TestSlruCtl.

◆ test_slru_page_precedes_logically()

static bool test_slru_page_precedes_logically ( int64  page1,
int64  page2 
)
static

Definition at line 202 of file test_slru.c.

203{
204 return page1 < page2;
205}

References fb().

Referenced by test_slru_shmem_request().

◆ test_slru_page_read()

Datum test_slru_page_read ( PG_FUNCTION_ARGS  )

Definition at line 100 of file test_slru.c.

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}
#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
const void * data
int SimpleLruReadPage(SlruDesc *ctl, int64 pageno, bool write_ok, const void *opaque_data)
Definition slru.c:550
text * cstring_to_text(const char *s)
Definition varlena.c:184

References cstring_to_text(), data, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_TRANSACTIONID, PG_RETURN_TEXT_P, SimpleLruGetBankLock(), SimpleLruReadPage(), and TestSlruCtl.

◆ test_slru_page_readonly()

Datum test_slru_page_readonly ( PG_FUNCTION_ARGS  )

Definition at line 119 of file test_slru.c.

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}
#define Assert(condition)
Definition c.h:943
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1885
int SimpleLruReadPage_ReadOnly(SlruDesc *ctl, int64 pageno, const void *opaque_data)
Definition slru.c:654

References Assert, cstring_to_text(), data, fb(), LWLockHeldByMe(), LWLockRelease(), PG_GETARG_INT64, PG_RETURN_TEXT_P, SimpleLruGetBankLock(), SimpleLruReadPage_ReadOnly(), and TestSlruCtl.

◆ test_slru_page_sync()

Datum test_slru_page_sync ( PG_FUNCTION_ARGS  )

Definition at line 152 of file test_slru.c.

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}
#define MAXPGPATH
int SlruSyncFileTag(SlruDesc *ctl, const FileTag *ftag, char *path)
Definition slru.c:1884

References elog, fb(), MAXPGPATH, NOTICE, PG_GETARG_INT64, PG_RETURN_VOID, FileTag::segno, SLRU_PAGES_PER_SEGMENT, SlruSyncFileTag(), and TestSlruCtl.

◆ test_slru_page_truncate()

Datum test_slru_page_truncate ( PG_FUNCTION_ARGS  )

Definition at line 184 of file test_slru.c.

185{
186 int64 pageno = PG_GETARG_INT64(0);
187
190}
void SimpleLruTruncate(SlruDesc *ctl, int64 cutoffPage)
Definition slru.c:1458

References PG_GETARG_INT64, PG_RETURN_VOID, SimpleLruTruncate(), and TestSlruCtl.

◆ test_slru_page_write()

Datum test_slru_page_write ( PG_FUNCTION_ARGS  )

Definition at line 65 of file test_slru.c.

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}
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
int SimpleLruZeroPage(SlruDesc *ctl, int64 pageno)
Definition slru.c:397
void SimpleLruWritePage(SlruDesc *ctl, int slotno)
Definition slru.c:781
@ SLRU_PAGE_VALID
Definition slru.h:38
char * text_to_cstring(const text *t)
Definition varlena.c:217

References Assert, data, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_RETURN_VOID, SimpleLruGetBankLock(), SimpleLruWritePage(), SimpleLruZeroPage(), SLRU_PAGE_VALID, TestSlruCtl, and text_to_cstring().

◆ test_slru_page_writeall()

Datum test_slru_page_writeall ( PG_FUNCTION_ARGS  )

Definition at line 93 of file test_slru.c.

94{
97}
void SimpleLruWriteAll(SlruDesc *ctl, bool allow_redirtied)
Definition slru.c:1372

References PG_RETURN_VOID, SimpleLruWriteAll(), and TestSlruCtl.

◆ test_slru_scan_cb()

static bool test_slru_scan_cb ( SlruDesc ctl,
char filename,
int64  segpage,
void data 
)
static

Definition at line 58 of file test_slru.c.

59{
60 elog(NOTICE, "Calling test_slru_scan_cb()");
62}
static char * filename
Definition pg_dumpall.c:133
tree ctl
Definition radixtree.h:1838
bool SlruScanDirCbDeleteAll(SlruDesc *ctl, char *filename, int64 segpage, void *data)
Definition slru.c:1797

References ctl, data, elog, fb(), filename, NOTICE, and SlruScanDirCbDeleteAll().

Referenced by test_slru_delete_all().

◆ test_slru_shmem_request()

static void test_slru_shmem_request ( void arg)
static

Definition at line 234 of file test_slru.c.

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 SimpleLruRequest(...)
Definition slru.h:218
@ SYNC_HANDLER_NONE
Definition sync.h:42
static bool test_slru_page_precedes_logically(int64 page1, int64 page2)
Definition test_slru.c:202
static SlruDesc TestSlruDesc
Definition test_slru.c:49
#define NUM_TEST_BUFFERS
Definition test_slru.c:41
static int test_slru_errdetail_for_io_error(const void *opaque_data)
Definition test_slru.c:208
const char * name

References name, NUM_TEST_BUFFERS, SimpleLruRequest, SYNC_HANDLER_NONE, test_slru_errdetail_for_io_error(), test_slru_page_precedes_logically(), TestSlruDesc, and TestSlruDir.

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 25 of file test_slru.c.

◆ test_slru_shmem_callbacks

const ShmemCallbacks test_slru_shmem_callbacks
static
Initial value:
= {
}
static void test_slru_shmem_request(void *arg)
Definition test_slru.c:234

Definition at line 51 of file test_slru.c.

51 {
52 .request_fn = test_slru_shmem_request
53};

Referenced by _PG_init().

◆ TestSlruDesc

SlruDesc TestSlruDesc
static

Definition at line 49 of file test_slru.c.

Referenced by test_slru_shmem_request().

◆ TestSlruDir

const char* TestSlruDir = "pg_test_slru"
static

Definition at line 47 of file test_slru.c.

Referenced by _PG_init(), and test_slru_shmem_request().