PostgreSQL Source Code git master
Loading...
Searching...
No Matches
test_tidstore.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * test_tidstore.c
4 * Test TidStore data structure.
5 *
6 * Note: all locking in this test module is useless since there is only
7 * a single process to use the TidStore. It is meant to be an example of
8 * usage.
9 *
10 * Copyright (c) 2024-2026, PostgreSQL Global Development Group
11 *
12 * IDENTIFICATION
13 * src/test/modules/test_tidstore/test_tidstore.c
14 *
15 * -------------------------------------------------------------------------
16 */
17#include "postgres.h"
18
19#include "access/tidstore.h"
20#include "fmgr.h"
21#include "storage/block.h"
22#include "storage/itemptr.h"
23#include "storage/lwlock.h"
24#include "utils/array.h"
25#include "utils/memutils.h"
26
28
34
36static size_t tidstore_empty_size;
37
38/* array for verification of some tests */
47
49
50/* comparator routine for ItemPointer */
51static int
52itemptr_cmp(const void *left, const void *right)
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}
77
78static int
79offsetnumber_cmp(const void *a, const void *b)
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}
90
91/*
92 * Create a TidStore. If shared is false, the tidstore is created
93 * on TopMemoryContext, otherwise on DSA. Although the tidstore
94 * is created on DSA, only the same process can subsequently use
95 * the tidstore. The tidstore handle is not shared anywhere.
96 */
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}
145
146static void
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}
159
160static void
162{
163 if (tidstore == NULL)
164 elog(ERROR, "tidstore is not created");
165}
166
167static void
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}
177
178
179/* Set the given block and offsets pairs */
180Datum
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}
229
230/*
231 * Verify TIDs in store against the array.
232 */
233Datum
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}
326
327/*
328 * In real world use, we care if the memory usage is greater than
329 * some configured limit. Here we just want to verify that
330 * TidStoreMemoryUsage is not broken.
331 */
332Datum
343
344/* Free the tidstore */
345Datum
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:263
#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
#define ARR_HASNULL(a)
Definition array.h:291
bool array_contains_nulls(const ArrayType *array)
int ArrayGetNItems(int ndim, const int *dims)
Definition arrayutils.c:57
uint32 BlockNumber
Definition block.h:31
#define Assert(condition)
Definition c.h:943
#define lengthof(array)
Definition c.h:873
void dsa_pin_mapping(dsa_area *area)
Definition dsa.c:650
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_RETURN_INT64(x)
Definition fmgr.h:370
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
int b
Definition isn.c:74
int a
Definition isn.c:73
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
int LWLockNewTrancheId(const char *name)
Definition lwlock.c:562
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1635
void pfree(void *pointer)
Definition mcxt.c:1619
void * palloc0(Size size)
Definition mcxt.c:1420
MemoryContext TopMemoryContext
Definition mcxt.c:167
static char * errmsg
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
#define MaxOffsetNumber
Definition off.h:28
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
#define qsort(a, b, c, d)
Definition port.h:496
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
ItemPointerData * insert_tids
ItemPointerData * lookup_tids
ItemPointerData * iter_tids
static void sanity_check_array(ArrayType *ta)
static void check_tidstore_available(void)
static int itemptr_cmp(const void *left, const void *right)
PG_MODULE_MAGIC
Datum do_set_block_offsets(PG_FUNCTION_ARGS)
Datum test_create(PG_FUNCTION_ARGS)
static TidStore * tidstore
static ItemArray items
static void purge_from_verification_array(BlockNumber blkno)
Datum check_set_block_offsets(PG_FUNCTION_ARGS)
Datum test_destroy(PG_FUNCTION_ARGS)
Datum test_is_full(PG_FUNCTION_ARGS)
static int offsetnumber_cmp(const void *a, const void *b)
static size_t tidstore_empty_size
dsa_area * TidStoreGetDSA(TidStore *ts)
Definition tidstore.c:544
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
TidStore * TidStoreCreateLocal(size_t max_bytes, bool insert_only)
Definition tidstore.c:162
void TidStoreDestroy(TidStore *ts)
Definition tidstore.c:317
void TidStoreUnlock(TidStore *ts)
Definition tidstore.c:301
int TidStoreGetBlockOffsets(TidStoreIterResult *result, OffsetNumber *offsets, int max_offsets)
Definition tidstore.c:566
void TidStoreLockExclusive(TidStore *ts)
Definition tidstore.c:287
void TidStoreSetBlockOffsets(TidStore *ts, BlockNumber blkno, OffsetNumber *offsets, int num_offsets)
Definition tidstore.c:345
size_t TidStoreMemoryUsage(TidStore *ts)
Definition tidstore.c:532
TidStore * TidStoreCreateShared(size_t max_bytes, int tranche_id)
Definition tidstore.c:208