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 TestSLRULock   (&TestSLRULock)
 
#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, int 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 (int page1, int page2)
 
static void test_slru_shmem_startup (void)
 
void _PG_init (void)
 

Variables

 PG_MODULE_MAGIC
 
LWLock TestSLRULock
 
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 48 of file test_slru.c.

◆ TestSLRULock

#define TestSLRULock   (&TestSLRULock)

Definition at line 45 of file test_slru.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 240 of file test_slru.c.

241 {
243  ereport(ERROR,
244  (errmsg("cannot load \"%s\" after startup", "test_slru"),
245  errdetail("\"%s\" must be loaded with shared_preload_libraries.",
246  "test_slru")));
247 
250 
253 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:56
shmem_request_hook_type shmem_request_hook
Definition: miscinit.c:1785
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1782
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:51
static shmem_request_hook_type prev_shmem_request_hook
Definition: test_slru.c:50
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:1554
static bool test_slru_scan_cb(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: test_slru.c:54
#define TestSlruCtl
Definition: test_slru.c:48

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

163 {
164  int pageno = PG_GETARG_INT32(0);
165  FileTag ftag;
166 
167  ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
169 
170  elog(NOTICE, "Called SlruDeleteSegment() for segment %u", ftag.segno);
171 
172  PG_RETURN_VOID();
173 }
#define NOTICE
Definition: elog.h:35
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
void SlruDeleteSegment(SlruCtl ctl, int segno)
Definition: slru.c:1328
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34
Definition: sync.h:51
uint32 segno
Definition: sync.h:55

References elog(), NOTICE, PG_GETARG_INT32, 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 132 of file test_slru.c.

133 {
134  int pageno = PG_GETARG_INT32(0);
135  bool found;
136 
140 
141  PG_RETURN_BOOL(found);
142 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_EXCLUSIVE
Definition: lwlock.h:116
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
Definition: slru.c:627
#define TestSLRULock
Definition: test_slru.c:45

References LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), PG_GETARG_INT32, PG_RETURN_BOOL, SimpleLruDoesPhysicalPageExist(), TestSlruCtl, and TestSLRULock.

◆ test_slru_page_precedes_logically()

static bool test_slru_page_precedes_logically ( int  page1,
int  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 96 of file test_slru.c.

97 {
98  int pageno = PG_GETARG_INT32(0);
99  bool write_ok = PG_GETARG_BOOL(1);
100  char *data = NULL;
101  int slotno;
102 
103  /* find page in buffers, reading it if necessary */
105  slotno = SimpleLruReadPage(TestSlruCtl, pageno,
106  write_ok, InvalidTransactionId);
107  data = (char *) TestSlruCtl->shared->page_buffer[slotno];
109 
111 }
#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, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:396
#define InvalidTransactionId
Definition: transam.h:31
text * cstring_to_text(const char *s)
Definition: varlena.c:182

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

◆ test_slru_page_readonly()

Datum test_slru_page_readonly ( PG_FUNCTION_ARGS  )

Definition at line 114 of file test_slru.c.

115 {
116  int pageno = PG_GETARG_INT32(0);
117  char *data = NULL;
118  int slotno;
119 
120  /* find page in buffers, reading it if necessary */
122  pageno,
125  data = (char *) TestSlruCtl->shared->page_buffer[slotno];
127 
129 }
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1919
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:496

References Assert(), cstring_to_text(), data, InvalidTransactionId, LWLockHeldByMe(), LWLockRelease(), PG_GETARG_INT32, PG_RETURN_TEXT_P, SimpleLruReadPage_ReadOnly(), TestSlruCtl, and TestSLRULock.

◆ test_slru_page_sync()

Datum test_slru_page_sync ( PG_FUNCTION_ARGS  )

Definition at line 145 of file test_slru.c.

146 {
147  int pageno = PG_GETARG_INT32(0);
148  FileTag ftag;
149  char path[MAXPGPATH];
150 
151  /* note that this flushes the full file a segment is located in */
152  ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
153  SlruSyncFileTag(TestSlruCtl, &ftag, path);
154 
155  elog(NOTICE, "Called SlruSyncFileTag() for segment %u on path %s",
156  ftag.segno, path);
157 
158  PG_RETURN_VOID();
159 }
#define MAXPGPATH
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1594

References elog(), MAXPGPATH, NOTICE, PG_GETARG_INT32, 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  int pageno = PG_GETARG_INT32(0);
179 
181  PG_RETURN_VOID();
182 }
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1227

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

◆ test_slru_page_write()

Datum test_slru_page_write ( PG_FUNCTION_ARGS  )

Definition at line 61 of file test_slru.c.

62 {
63  int pageno = PG_GETARG_INT32(0);
65  int slotno;
66 
68 
69  slotno = SimpleLruZeroPage(TestSlruCtl, pageno);
70 
71  /* these should match */
72  Assert(TestSlruCtl->shared->page_number[slotno] == pageno);
73 
74  /* mark the page as dirty so as it would get written */
75  TestSlruCtl->shared->page_dirty[slotno] = true;
76  TestSlruCtl->shared->page_status[slotno] = SLRU_PAGE_VALID;
77 
78  /* write given data to the page, up to the limit of the page */
79  strncpy(TestSlruCtl->shared->page_buffer[slotno], data,
80  BLCKSZ - 1);
81 
84 
86 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:615
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:281
@ SLRU_PAGE_VALID
Definition: slru.h:46
char * text_to_cstring(const text *t)
Definition: varlena.c:215

References Assert(), data, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_VOID, SimpleLruWritePage(), SimpleLruZeroPage(), SLRU_PAGE_VALID, TestSlruCtl, TestSLRULock, and text_to_cstring().

◆ test_slru_page_writeall()

Datum test_slru_page_writeall ( PG_FUNCTION_ARGS  )

Definition at line 89 of file test_slru.c.

90 {
93 }
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1157

References PG_RETURN_VOID, SimpleLruWriteAll(), and TestSlruCtl.

◆ test_slru_scan_cb()

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

Definition at line 54 of file test_slru.c.

55 {
56  elog(NOTICE, "Calling test_slru_scan_cb()");
57  return SlruScanDirCbDeleteAll(ctl, filename, segpage, data);
58 }
static char * filename
Definition: pg_dumpall.c:119
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1531

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:70
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:156
#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  const char slru_dir_name[] = "pg_test_slru";
217  int test_tranche_id;
218 
221 
222  /*
223  * Create the SLRU directory if it does not exist yet, from the root of
224  * the data directory.
225  */
226  (void) MakePGDirectory(slru_dir_name);
227 
228  /* initialize the SLRU facility */
229  test_tranche_id = LWLockNewTrancheId();
230  LWLockRegisterTranche(test_tranche_id, "test_slru_tranche");
231  LWLockInitialize(TestSLRULock, test_tranche_id);
232 
234  SimpleLruInit(TestSlruCtl, "TestSLRU",
235  NUM_TEST_BUFFERS, 0, TestSLRULock, slru_dir_name,
236  test_tranche_id, SYNC_HANDLER_NONE);
237 }
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3810
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:651
int LWLockNewTrancheId(void)
Definition: lwlock.c:627
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:730
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler)
Definition: slru.c:188
@ SYNC_HANDLER_NONE
Definition: sync.h:42
static bool test_slru_page_precedes_logically(int page1, int page2)
Definition: test_slru.c:208

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

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

Referenced by _PG_init(), and test_slru_shmem_startup().

◆ TestSlruCtlData

SlruCtlData TestSlruCtlData
static

Definition at line 47 of file test_slru.c.

◆ TestSLRULock

LWLock TestSLRULock

Definition at line 44 of file test_slru.c.