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
 
const char test_tranche_name [] = "test_slru_tranche"
 

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

244 {
246  ereport(ERROR,
247  (errmsg("cannot load \"%s\" after startup", "test_slru"),
248  errdetail("\"%s\" must be loaded with shared_preload_libraries.",
249  "test_slru")));
250 
253 
256 }
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:1773
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1770
static void test_slru_shmem_startup(void)
Definition: test_slru.c:217
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:201

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

189 {
190  /* this calls SlruScanDirCbDeleteAll() internally, ensuring deletion */
192 
193  PG_RETURN_VOID();
194 }
#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:57
#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 165 of file test_slru.c.

166 {
167  int pageno = PG_GETARG_INT32(0);
168  FileTag ftag;
169 
170  ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
172 
173  elog(NOTICE, "Called SlruDeleteSegment() for segment %d", ftag.segno);
174 
175  PG_RETURN_VOID();
176 }
#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 135 of file test_slru.c.

136 {
137  int pageno = PG_GETARG_INT32(0);
138  bool found;
139 
143 
144  PG_RETURN_BOOL(found);
145 }
#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:115
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 211 of file test_slru.c.

212 {
213  return page1 < page2;
214 }

Referenced by test_slru_shmem_startup().

◆ test_slru_page_read()

Datum test_slru_page_read ( PG_FUNCTION_ARGS  )

Definition at line 99 of file test_slru.c.

100 {
101  int pageno = PG_GETARG_INT32(0);
102  bool write_ok = PG_GETARG_BOOL(1);
103  char *data = NULL;
104  int slotno;
105 
106  /* find page in buffers, reading it if necessary */
108  slotno = SimpleLruReadPage(TestSlruCtl, pageno,
109  write_ok, InvalidTransactionId);
110  data = (char *) TestSlruCtl->shared->page_buffer[slotno];
112 
114 }
#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:189

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

118 {
119  int pageno = PG_GETARG_INT32(0);
120  char *data = NULL;
121  int slotno;
122 
123  /* find page in buffers, reading it if necessary */
125  pageno,
128  data = (char *) TestSlruCtl->shared->page_buffer[slotno];
130 
132 }
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 148 of file test_slru.c.

149 {
150  int pageno = PG_GETARG_INT32(0);
151  FileTag ftag;
152  char path[MAXPGPATH];
153 
154  /* note that this flushes the full file a segment is located in */
155  ftag.segno = pageno / SLRU_PAGES_PER_SEGMENT;
156  SlruSyncFileTag(TestSlruCtl, &ftag, path);
157 
158  elog(NOTICE, "Called SlruSyncFileTag() for segment %d on path %s",
159  ftag.segno, path);
160 
161  PG_RETURN_VOID();
162 }
#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 179 of file test_slru.c.

180 {
181  int pageno = PG_GETARG_INT32(0);
182 
184  PG_RETURN_VOID();
185 }
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 64 of file test_slru.c.

65 {
66  int pageno = PG_GETARG_INT32(0);
68  int slotno;
69 
71 
72  slotno = SimpleLruZeroPage(TestSlruCtl, pageno);
73 
74  /* these should match */
75  Assert(TestSlruCtl->shared->page_number[slotno] == pageno);
76 
77  /* mark the page as dirty so as it would get written */
78  TestSlruCtl->shared->page_dirty[slotno] = true;
79  TestSlruCtl->shared->page_status[slotno] = SLRU_PAGE_VALID;
80 
81  /* write given data to the page, up to the limit of the page */
82  strncpy(TestSlruCtl->shared->page_buffer[slotno], data,
83  BLCKSZ - 1);
84 
87 
89 }
#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:222

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

93 {
96 }
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 57 of file test_slru.c.

58 {
59  elog(NOTICE, "Calling test_slru_scan_cb()");
60  return SlruScanDirCbDeleteAll(ctl, filename, segpage, data);
61 }
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 201 of file test_slru.c.

202 {
205 
206  /* reserve shared memory for the test SLRU */
208 }
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 217 of file test_slru.c.

218 {
219  const char slru_dir_name[] = "pg_test_slru";
220  int test_tranche_id;
221 
224 
225  /*
226  * Create the SLRU directory if it does not exist yet, from the root of
227  * the data directory.
228  */
229  (void) MakePGDirectory(slru_dir_name);
230 
231  /* initialize the SLRU facility */
232  test_tranche_id = LWLockNewTrancheId();
233  LWLockRegisterTranche(test_tranche_id, "test_slru_tranche");
234  LWLockInitialize(TestSLRULock, test_tranche_id);
235 
237  SimpleLruInit(TestSlruCtl, "TestSLRU",
238  NUM_TEST_BUFFERS, 0, TestSLRULock, slru_dir_name,
239  test_tranche_id, SYNC_HANDLER_NONE);
240 }
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3713
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:211

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().

◆ test_tranche_name

const char test_tranche_name[] = "test_slru_tranche"

Definition at line 54 of file test_slru.c.

◆ 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.