PostgreSQL Source Code git master
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-2025, 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
35static TidStore *tidstore = NULL;
36static size_t tidstore_empty_size;
37
38/* array for verification of some tests */
39typedef struct ItemArray
40{
47
49
50/* comparator routine for ItemPointer */
51static int
52itemptr_cmp(const void *left, const void *right)
53{
54 BlockNumber lblk,
55 rblk;
56 OffsetNumber loff,
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
78/*
79 * Create a TidStore. If shared is false, the tidstore is created
80 * on TopMemoryContext, otherwise on DSA. Although the tidstore
81 * is created on DSA, only the same process can subsequently use
82 * the tidstore. The tidstore handle is not shared anywhere.
83*/
86{
87 bool shared = PG_GETARG_BOOL(0);
88 MemoryContext old_ctx;
89
90 /* doesn't really matter, since it's just a hint */
91 size_t tidstore_max_size = 2 * 1024 * 1024;
92 size_t array_init_size = 1024;
93
94 Assert(tidstore == NULL);
95
96 /*
97 * Create the TidStore on TopMemoryContext so that the same process use it
98 * for subsequent tests.
99 */
101
102 if (shared)
103 {
104 int tranche_id;
105
106 tranche_id = LWLockNewTrancheId("test_tidstore");
107
108 tidstore = TidStoreCreateShared(tidstore_max_size, tranche_id);
109
110 /*
111 * Remain attached until end of backend or explicitly detached so that
112 * the same process use the tidstore for subsequent tests.
113 */
115 }
116 else
117 /* VACUUM uses insert only, so we test the other option. */
118 tidstore = TidStoreCreateLocal(tidstore_max_size, false);
119
121
122 items.num_tids = 0;
123 items.max_tids = array_init_size / sizeof(ItemPointerData);
124 items.insert_tids = (ItemPointerData *) palloc0(array_init_size);
125 items.lookup_tids = (ItemPointerData *) palloc0(array_init_size);
126 items.iter_tids = (ItemPointerData *) palloc0(array_init_size);
127
128 MemoryContextSwitchTo(old_ctx);
129
131}
132
133static void
135{
136 if (ARR_HASNULL(ta) && array_contains_nulls(ta))
138 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
139 errmsg("array must not contain nulls")));
140
141 if (ARR_NDIM(ta) > 1)
143 (errcode(ERRCODE_DATA_EXCEPTION),
144 errmsg("argument must be empty or one-dimensional array")));
145}
146
147static void
149{
150 if (tidstore == NULL)
151 elog(ERROR, "tidstore is not created");
152}
153
154static void
156{
157 int dst = 0;
158
159 for (int src = 0; src < items.num_tids; src++)
160 if (ItemPointerGetBlockNumber(&items.insert_tids[src]) != blkno)
161 items.insert_tids[dst++] = items.insert_tids[src];
162 items.num_tids = dst;
163}
164
165
166/* Set the given block and offsets pairs */
167Datum
169{
170 BlockNumber blkno = PG_GETARG_INT64(0);
172 OffsetNumber *offs;
173 int noffs;
174
177
178 noffs = ArrayGetNItems(ARR_NDIM(ta), ARR_DIMS(ta));
179 offs = ((OffsetNumber *) ARR_DATA_PTR(ta));
180
181 /* Set TIDs in the store */
183 TidStoreSetBlockOffsets(tidstore, blkno, offs, noffs);
185
186 /* Remove the existing items of blkno from the verification array */
188
189 /* Set TIDs in verification array */
190 for (int i = 0; i < noffs; i++)
191 {
192 ItemPointer tid;
193 int idx = items.num_tids + i;
194
195 /* Enlarge the TID arrays if necessary */
196 if (idx >= items.max_tids)
197 {
198 items.max_tids *= 2;
202 }
203
204 tid = &(items.insert_tids[idx]);
205 ItemPointerSet(tid, blkno, offs[i]);
206 }
207
208 /* Update statistics */
209 items.num_tids += noffs;
210
211 PG_RETURN_INT64(blkno);
212}
213
214/*
215 * Verify TIDs in store against the array.
216 */
217Datum
219{
220 TidStoreIter *iter;
221 TidStoreIterResult *iter_result;
222 int num_iter_tids = 0;
223 int num_lookup_tids = 0;
224 BlockNumber prevblkno = 0;
225
227
228 /* lookup each member in the verification array */
229 for (int i = 0; i < items.num_tids; i++)
231 elog(ERROR, "missing TID with block %u, offset %u",
234
235 /*
236 * Lookup all possible TIDs for each distinct block in the verification
237 * array and save successful lookups in the lookup array.
238 */
239
240 for (int i = 0; i < items.num_tids; i++)
241 {
243
244 if (i > 0 && blkno == prevblkno)
245 continue;
246
247 for (OffsetNumber offset = FirstOffsetNumber; offset < MaxOffsetNumber; offset++)
248 {
249 ItemPointerData tid;
250
251 ItemPointerSet(&tid, blkno, offset);
252
254 if (TidStoreIsMember(tidstore, &tid))
255 ItemPointerSet(&items.lookup_tids[num_lookup_tids++], blkno, offset);
257 }
258
259 prevblkno = blkno;
260 }
261
262 /* Collect TIDs stored in the tidstore, in order */
263
266 while ((iter_result = TidStoreIterateNext(iter)) != NULL)
267 {
269 int num_offsets;
270
271 num_offsets = TidStoreGetBlockOffsets(iter_result, offsets, lengthof(offsets));
272 Assert(num_offsets <= lengthof(offsets));
273 for (int i = 0; i < num_offsets; i++)
274 ItemPointerSet(&(items.iter_tids[num_iter_tids++]), iter_result->blkno,
275 offsets[i]);
276 }
277 TidStoreEndIterate(iter);
279
280 /*
281 * Sort verification and lookup arrays and test that all arrays are the
282 * same.
283 */
284
285 if (num_lookup_tids != items.num_tids)
286 elog(ERROR, "should have %d TIDs, have %d", items.num_tids, num_lookup_tids);
287 if (num_iter_tids != items.num_tids)
288 elog(ERROR, "should have %d TIDs, have %d", items.num_tids, num_iter_tids);
289
292 for (int i = 0; i < items.num_tids; i++)
293 {
295 elog(ERROR, "TID iter array doesn't match verification array, got (%u,%u) expected (%u,%u)",
301 elog(ERROR, "TID lookup array doesn't match verification array, got (%u,%u) expected (%u,%u)",
306 }
307
309}
310
311/*
312 * In real world use, we care if the memory usage is greater than
313 * some configured limit. Here we just want to verify that
314 * TidStoreMemoryUsage is not broken.
315 */
316Datum
318{
319 bool is_full;
320
322
324
325 PG_RETURN_BOOL(is_full);
326}
327
328/* Free the tidstore */
329Datum
331{
333
335 tidstore = NULL;
336 items.num_tids = 0;
340
342}
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
#define ARR_HASNULL(a)
Definition: array.h:291
bool array_contains_nulls(const ArrayType *array)
Definition: arrayfuncs.c:3768
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:57
uint32 BlockNumber
Definition: block.h:31
#define lengthof(array)
Definition: c.h:792
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:650
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#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:359
Assert(PointerIsAligned(start, uint64))
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
struct ItemPointerData ItemPointerData
int LWLockNewTrancheId(const char *name)
Definition: lwlock.c:596
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1610
void pfree(void *pointer)
Definition: mcxt.c:1594
void * palloc0(Size size)
Definition: mcxt.c:1395
MemoryContext TopMemoryContext
Definition: mcxt.c:166
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:124
#define qsort(a, b, c, d)
Definition: port.h:479
uint64_t Datum
Definition: postgres.h:70
ItemPointerData * insert_tids
Definition: test_tidstore.c:41
ItemPointerData * lookup_tids
Definition: test_tidstore.c:42
ItemPointerData * iter_tids
Definition: test_tidstore.c:43
BlockNumber blkno
Definition: tidstore.h:29
struct ItemArray ItemArray
static void sanity_check_array(ArrayType *ta)
static void check_tidstore_available(void)
static int itemptr_cmp(const void *left, const void *right)
Definition: test_tidstore.c:52
PG_FUNCTION_INFO_V1(test_create)
PG_MODULE_MAGIC
Definition: test_tidstore.c:27
Datum do_set_block_offsets(PG_FUNCTION_ARGS)
Datum test_create(PG_FUNCTION_ARGS)
Definition: test_tidstore.c:85
static TidStore * tidstore
Definition: test_tidstore.c:35
static ItemArray items
Definition: test_tidstore.c:48
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 size_t tidstore_empty_size
Definition: test_tidstore.c:36
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