PostgreSQL Source Code git master
Loading...
Searching...
No Matches
test_tidstore.c File Reference
#include "postgres.h"
#include "access/tidstore.h"
#include "fmgr.h"
#include "storage/block.h"
#include "storage/itemptr.h"
#include "storage/lwlock.h"
#include "utils/array.h"
#include "utils/memutils.h"
Include dependency graph for test_tidstore.c:

Go to the source code of this file.

Data Structures

struct  ItemArray
 

Typedefs

typedef struct ItemArray ItemArray
 

Functions

 PG_FUNCTION_INFO_V1 (test_create)
 
 PG_FUNCTION_INFO_V1 (do_set_block_offsets)
 
 PG_FUNCTION_INFO_V1 (check_set_block_offsets)
 
 PG_FUNCTION_INFO_V1 (test_is_full)
 
 PG_FUNCTION_INFO_V1 (test_destroy)
 
static int itemptr_cmp (const void *left, const void *right)
 
static int offsetnumber_cmp (const void *a, const void *b)
 
Datum test_create (PG_FUNCTION_ARGS)
 
static void sanity_check_array (ArrayType *ta)
 
static void check_tidstore_available (void)
 
static void purge_from_verification_array (BlockNumber blkno)
 
Datum do_set_block_offsets (PG_FUNCTION_ARGS)
 
Datum check_set_block_offsets (PG_FUNCTION_ARGS)
 
Datum test_is_full (PG_FUNCTION_ARGS)
 
Datum test_destroy (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static TidStoretidstore = NULL
 
static size_t tidstore_empty_size
 
static ItemArray items
 

Typedef Documentation

◆ ItemArray

Function Documentation

◆ check_set_block_offsets()

Datum check_set_block_offsets ( PG_FUNCTION_ARGS  )

Definition at line 234 of file test_tidstore.c.

235{
236 TidStoreIter *iter;
238 int num_iter_tids = 0;
239 int num_lookup_tids = 0;
240 BlockNumber prevblkno = 0;
241
243
244 /* lookup each member in the verification array */
245 for (int i = 0; i < items.num_tids; i++)
247 elog(ERROR, "missing TID with block %u, offset %u",
250
251 /*
252 * Lookup all possible TIDs for each distinct block in the verification
253 * array and save successful lookups in the lookup array.
254 */
255
256 for (int i = 0; i < items.num_tids; i++)
257 {
259
260 if (i > 0 && blkno == prevblkno)
261 continue;
262
263 for (OffsetNumber offset = FirstOffsetNumber; offset < MaxOffsetNumber; offset++)
264 {
265 ItemPointerData tid;
266
267 ItemPointerSet(&tid, blkno, offset);
268
270 if (TidStoreIsMember(tidstore, &tid))
273 }
274
275 prevblkno = blkno;
276 }
277
278 /* Collect TIDs stored in the tidstore, in order */
279
282 while ((iter_result = TidStoreIterateNext(iter)) != NULL)
283 {
285 int num_offsets;
286
288 Assert(num_offsets <= lengthof(offsets));
289 for (int i = 0; i < num_offsets; i++)
291 offsets[i]);
292 }
293 TidStoreEndIterate(iter);
295
296 /*
297 * Sort verification and lookup arrays and test that all arrays are the
298 * same.
299 */
300
302 elog(ERROR, "should have %d TIDs, have %d", items.num_tids, num_lookup_tids);
304 elog(ERROR, "should have %d TIDs, have %d", items.num_tids, num_iter_tids);
305
308 for (int i = 0; i < items.num_tids; i++)
309 {
311 elog(ERROR, "TID iter array doesn't match verification array, got (%u,%u) expected (%u,%u)",
317 elog(ERROR, "TID lookup array doesn't match verification array, got (%u,%u) expected (%u,%u)",
322 }
323
325}
uint32 BlockNumber
Definition block.h:31
#define Assert(condition)
Definition c.h:943
#define lengthof(array)
Definition c.h:873
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define PG_RETURN_VOID()
Definition fmgr.h:350
int i
Definition isn.c:77
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition itemptr.h:135
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition itemptr.h:103
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
#define MaxOffsetNumber
Definition off.h:28
#define qsort(a, b, c, d)
Definition port.h:495
static int fb(int x)
ItemPointerData * insert_tids
ItemPointerData * lookup_tids
ItemPointerData * iter_tids
static void check_tidstore_available(void)
static int itemptr_cmp(const void *left, const void *right)
static TidStore * tidstore
static ItemArray items
bool TidStoreIsMember(TidStore *ts, const ItemPointerData *tid)
Definition tidstore.c:421
TidStoreIter * TidStoreBeginIterate(TidStore *ts)
Definition tidstore.c:471
void TidStoreEndIterate(TidStoreIter *iter)
Definition tidstore.c:518
TidStoreIterResult * TidStoreIterateNext(TidStoreIter *iter)
Definition tidstore.c:493
void TidStoreLockShare(TidStore *ts)
Definition tidstore.c:294
void TidStoreUnlock(TidStore *ts)
Definition tidstore.c:301
int TidStoreGetBlockOffsets(TidStoreIterResult *result, OffsetNumber *offsets, int max_offsets)
Definition tidstore.c:566

References Assert, check_tidstore_available(), elog, ERROR, fb(), FirstOffsetNumber, i, ItemArray::insert_tids, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerSet(), itemptr_cmp(), items, ItemArray::iter_tids, lengthof, ItemArray::lookup_tids, MaxOffsetNumber, ItemArray::num_tids, PG_RETURN_VOID, qsort, tidstore, TidStoreBeginIterate(), TidStoreEndIterate(), TidStoreGetBlockOffsets(), TidStoreIsMember(), TidStoreIterateNext(), TidStoreLockShare(), and TidStoreUnlock().

◆ check_tidstore_available()

static void check_tidstore_available ( void  )
static

Definition at line 161 of file test_tidstore.c.

162{
163 if (tidstore == NULL)
164 elog(ERROR, "tidstore is not created");
165}

References elog, ERROR, fb(), and tidstore.

Referenced by check_set_block_offsets(), do_set_block_offsets(), test_destroy(), and test_is_full().

◆ do_set_block_offsets()

Datum do_set_block_offsets ( PG_FUNCTION_ARGS  )

Definition at line 181 of file test_tidstore.c.

182{
183 BlockNumber blkno = PG_GETARG_INT64(0);
186 int noffs;
187
190
193
194 /* TidStoreSetBlockOffsets() requires offsets to be strictly ascending. */
196
197 /* Set TIDs in the store */
201
202 /* Remove the existing items of blkno from the verification array */
204
205 /* Set TIDs in verification array */
206 for (int i = 0; i < noffs; i++)
207 {
208 ItemPointer tid;
209 int idx = items.num_tids + i;
210
211 /* Enlarge the TID arrays if necessary */
212 if (idx >= items.max_tids)
213 {
214 items.max_tids *= 2;
218 }
219
220 tid = &(items.insert_tids[idx]);
221 ItemPointerSet(tid, blkno, offs[i]);
222 }
223
224 /* Update statistics */
226
227 PG_RETURN_INT64(blkno);
228}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition array.h:264
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_DATA_PTR(a)
Definition array.h:322
#define ARR_DIMS(a)
Definition array.h:294
int ArrayGetNItems(int ndim, const int *dims)
Definition arrayutils.c:57
#define PG_RETURN_INT64(x)
Definition fmgr.h:370
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
static void sanity_check_array(ArrayType *ta)
static void purge_from_verification_array(BlockNumber blkno)
static int offsetnumber_cmp(const void *a, const void *b)
void TidStoreLockExclusive(TidStore *ts)
Definition tidstore.c:287
void TidStoreSetBlockOffsets(TidStore *ts, BlockNumber blkno, OffsetNumber *offsets, int num_offsets)
Definition tidstore.c:345

References ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, ArrayGetNItems(), check_tidstore_available(), fb(), i, idx(), ItemArray::insert_tids, ItemPointerSet(), items, ItemArray::iter_tids, ItemArray::lookup_tids, ItemArray::max_tids, ItemArray::num_tids, offsetnumber_cmp(), PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT64, PG_RETURN_INT64, purge_from_verification_array(), qsort, repalloc(), sanity_check_array(), tidstore, TidStoreLockExclusive(), TidStoreSetBlockOffsets(), and TidStoreUnlock().

◆ itemptr_cmp()

static int itemptr_cmp ( const void left,
const void right 
)
static

Definition at line 52 of file test_tidstore.c.

53{
55 rblk;
57 roff;
58
61
62 if (lblk < rblk)
63 return -1;
64 if (lblk > rblk)
65 return 1;
66
69
70 if (loff < roff)
71 return -1;
72 if (loff > roff)
73 return 1;
74
75 return 0;
76}

References fb(), ItemPointerGetBlockNumber(), and ItemPointerGetOffsetNumber().

Referenced by check_set_block_offsets().

◆ offsetnumber_cmp()

static int offsetnumber_cmp ( const void a,
const void b 
)
static

Definition at line 79 of file test_tidstore.c.

80{
81 OffsetNumber l = *(const OffsetNumber *) a;
82 OffsetNumber r = *(const OffsetNumber *) b;
83
84 if (l < r)
85 return -1;
86 else if (l > r)
87 return 1;
88 return 0;
89}
int b
Definition isn.c:74
int a
Definition isn.c:73

References a, and b.

Referenced by do_set_block_offsets().

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( check_set_block_offsets  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( do_set_block_offsets  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( test_create  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( test_destroy  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( test_is_full  )

◆ purge_from_verification_array()

static void purge_from_verification_array ( BlockNumber  blkno)
static

Definition at line 168 of file test_tidstore.c.

169{
170 int dst = 0;
171
172 for (int src = 0; src < items.num_tids; src++)
173 if (ItemPointerGetBlockNumber(&items.insert_tids[src]) != blkno)
176}

References fb(), ItemArray::insert_tids, ItemPointerGetBlockNumber(), items, and ItemArray::num_tids.

Referenced by do_set_block_offsets().

◆ sanity_check_array()

static void sanity_check_array ( ArrayType ta)
static

Definition at line 147 of file test_tidstore.c.

148{
152 errmsg("array must not contain nulls")));
153
154 if (ARR_NDIM(ta) > 1)
157 errmsg("argument must be empty or one-dimensional array")));
158}
#define ARR_HASNULL(a)
Definition array.h:291
bool array_contains_nulls(const ArrayType *array)
int errcode(int sqlerrcode)
Definition elog.c:875
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg

References ARR_HASNULL, ARR_NDIM, array_contains_nulls(), ereport, errcode(), errmsg, ERROR, and fb().

Referenced by do_set_block_offsets().

◆ test_create()

Datum test_create ( PG_FUNCTION_ARGS  )

Definition at line 98 of file test_tidstore.c.

99{
100 bool shared = PG_GETARG_BOOL(0);
102
103 /* doesn't really matter, since it's just a hint */
104 size_t tidstore_max_size = 2 * 1024 * 1024;
105 size_t array_init_size = 1024;
106
107 Assert(tidstore == NULL);
108
109 /*
110 * Create the TidStore on TopMemoryContext so that the same process use it
111 * for subsequent tests.
112 */
114
115 if (shared)
116 {
117 int tranche_id;
118
119 tranche_id = LWLockNewTrancheId("test_tidstore");
120
122
123 /*
124 * Remain attached until end of backend or explicitly detached so that
125 * the same process use the tidstore for subsequent tests.
126 */
128 }
129 else
130 /* VACUUM uses insert only, so we test the other option. */
132
134
135 items.num_tids = 0;
140
142
144}
void dsa_pin_mapping(dsa_area *area)
Definition dsa.c:650
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
int LWLockNewTrancheId(const char *name)
Definition lwlock.c:562
void * palloc0(Size size)
Definition mcxt.c:1417
MemoryContext TopMemoryContext
Definition mcxt.c:166
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static size_t tidstore_empty_size
dsa_area * TidStoreGetDSA(TidStore *ts)
Definition tidstore.c:544
TidStore * TidStoreCreateLocal(size_t max_bytes, bool insert_only)
Definition tidstore.c:162
size_t TidStoreMemoryUsage(TidStore *ts)
Definition tidstore.c:532
TidStore * TidStoreCreateShared(size_t max_bytes, int tranche_id)
Definition tidstore.c:208

References Assert, dsa_pin_mapping(), fb(), ItemArray::insert_tids, items, ItemArray::iter_tids, ItemArray::lookup_tids, LWLockNewTrancheId(), ItemArray::max_tids, MemoryContextSwitchTo(), ItemArray::num_tids, palloc0(), PG_GETARG_BOOL, PG_RETURN_VOID, tidstore, tidstore_empty_size, TidStoreCreateLocal(), TidStoreCreateShared(), TidStoreGetDSA(), TidStoreMemoryUsage(), and TopMemoryContext.

◆ test_destroy()

◆ test_is_full()

Datum test_is_full ( PG_FUNCTION_ARGS  )

Definition at line 333 of file test_tidstore.c.

334{
335 bool is_full;
336
338
340
342}
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360

References check_tidstore_available(), fb(), PG_RETURN_BOOL, tidstore, tidstore_empty_size, and TidStoreMemoryUsage().

Variable Documentation

◆ items

ItemArray items
static

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 27 of file test_tidstore.c.

◆ tidstore

◆ tidstore_empty_size

size_t tidstore_empty_size
static

Definition at line 36 of file test_tidstore.c.

Referenced by test_create(), and test_is_full().