PostgreSQL Source Code  git master
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   (&TestSlruCtlData)
 

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 bool test_slru_scan_cb (SlruCtl 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)
 
static void test_slru_shmem_request (void)
 
static bool test_slru_page_precedes_logically (int64 page1, int64 page2)
 
static void test_slru_shmem_startup (void)
 
void _PG_init (void)
 

Variables

 PG_MODULE_MAGIC
 
static SlruCtlData TestSlruCtlData
 
static shmem_request_hook_type prev_shmem_request_hook = NULL
 
static shmem_startup_hook_type prev_shmem_startup_hook = NULL
 

Macro Definition Documentation

◆ NUM_TEST_BUFFERS

#define NUM_TEST_BUFFERS   16

Definition at line 41 of file test_slru.c.

◆ TestSlruCtl

#define TestSlruCtl   (&TestSlruCtlData)

Definition at line 44 of file test_slru.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 249 of file test_slru.c.

250 {
252  ereport(ERROR,
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 }
int errdetail(const char *fmt,...)
Definition: elog.c:1206
int errmsg(const char *fmt,...)
Definition: elog.c:1073
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:62
shmem_request_hook_type shmem_request_hook
Definition: miscinit.c:1782
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1779
static void test_slru_shmem_startup(void)
Definition: test_slru.c:214
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
static void test_slru_shmem_request(void)
Definition: test_slru.c:198

References ereport, errdetail(), errmsg(), ERROR, prev_shmem_request_hook, prev_shmem_startup_hook, process_shared_preload_libraries_in_progress, shmem_request_hook, shmem_startup_hook, test_slru_shmem_request(), and test_slru_shmem_startup().

◆ 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 185 of file test_slru.c.

186 {
187  /* this calls SlruScanDirCbDeleteAll() internally, ensuring deletion */
189 
190  PG_RETURN_VOID();
191 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1774
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

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

◆ test_slru_page_delete()

Datum test_slru_page_delete ( PG_FUNCTION_ARGS  )

Definition at line 161 of file test_slru.c.

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 
172  PG_RETURN_VOID();
173 }
#define elog(elevel,...)
Definition: elog.h:224
#define NOTICE
Definition: elog.h:35
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
void SlruDeleteSegment(SlruCtl ctl, int64 segno)
Definition: slru.c:1509
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:39
Definition: sync.h:51
uint64 segno
Definition: sync.h:55

References elog, 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 130 of file test_slru.c.

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 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1175
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1788
@ LW_EXCLUSIVE
Definition: lwlock.h:116
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
Definition: slru.c:729
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition: slru.h:179
Definition: lwlock.h:41

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 208 of file test_slru.c.

209 {
210  return page1 < page2;
211 }

Referenced by test_slru_shmem_startup().

◆ test_slru_page_read()

Datum test_slru_page_read ( PG_FUNCTION_ARGS  )

Definition at line 92 of file test_slru.c.

93 {
94  int64 pageno = PG_GETARG_INT64(0);
95  bool write_ok = PG_GETARG_BOOL(1);
96  char *data = NULL;
97  int slotno;
98  LWLock *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
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 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
const void * data
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition: slru.c:488
#define InvalidTransactionId
Definition: transam.h:31
text * cstring_to_text(const char *s)
Definition: varlena.c:184

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

◆ test_slru_page_readonly()

Datum test_slru_page_readonly ( PG_FUNCTION_ARGS  )

Definition at line 111 of file test_slru.c.

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 }
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1900
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
Definition: slru.c:592

References Assert(), cstring_to_text(), data, InvalidTransactionId, 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 144 of file test_slru.c.

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 
157  PG_RETURN_VOID();
158 }
#define MAXPGPATH
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1814

References elog, 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 176 of file test_slru.c.

177 {
178  int64 pageno = PG_GETARG_INT64(0);
179 
181  PG_RETURN_VOID();
182 }
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition: slru.c:1391

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 57 of file test_slru.c.

58 {
59  int64 pageno = PG_GETARG_INT64(0);
61  int slotno;
62  LWLock *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
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 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:715
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition: slru.c:361
@ SLRU_PAGE_VALID
Definition: slru.h:51
char * text_to_cstring(const text *t)
Definition: varlena.c:217

References Assert(), data, 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 85 of file test_slru.c.

86 {
89 }
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1305

References PG_RETURN_VOID, SimpleLruWriteAll(), and TestSlruCtl.

◆ test_slru_scan_cb()

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

Definition at line 50 of file test_slru.c.

51 {
52  elog(NOTICE, "Calling test_slru_scan_cb()");
53  return SlruScanDirCbDeleteAll(ctl, filename, segpage, data);
54 }
static char * filename
Definition: pg_dumpall.c:121
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition: slru.c:1727

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

Referenced by test_slru_delete_all().

◆ test_slru_shmem_request()

static void test_slru_shmem_request ( void  )
static

Definition at line 198 of file test_slru.c.

199 {
202 
203  /* reserve shared memory for the test SLRU */
205 }
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:78
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:184
#define NUM_TEST_BUFFERS
Definition: test_slru.c:41

References NUM_TEST_BUFFERS, prev_shmem_request_hook, RequestAddinShmemSpace(), and SimpleLruShmemSize().

Referenced by _PG_init().

◆ test_slru_shmem_startup()

static void test_slru_shmem_startup ( void  )
static

Definition at line 214 of file test_slru.c.

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 }
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3913
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:631
int LWLockNewTrancheId(void)
Definition: lwlock.c:607
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:238
@ SYNC_HANDLER_NONE
Definition: sync.h:42
static bool test_slru_page_precedes_logically(int64 page1, int64 page2)
Definition: test_slru.c:208

References LWLockNewTrancheId(), LWLockRegisterTranche(), MakePGDirectory(), NUM_TEST_BUFFERS, prev_shmem_startup_hook, SimpleLruInit(), SYNC_HANDLER_NONE, test_slru_page_precedes_logically(), and TestSlruCtl.

Referenced by _PG_init().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 25 of file test_slru.c.

◆ prev_shmem_request_hook

shmem_request_hook_type prev_shmem_request_hook = NULL
static

Definition at line 46 of file test_slru.c.

Referenced by _PG_init(), and test_slru_shmem_request().

◆ prev_shmem_startup_hook

shmem_startup_hook_type prev_shmem_startup_hook = NULL
static

Definition at line 47 of file test_slru.c.

Referenced by _PG_init(), and test_slru_shmem_startup().

◆ TestSlruCtlData

SlruCtlData TestSlruCtlData
static

Definition at line 43 of file test_slru.c.