1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/*--------------------------------------------------------------------------
*
* test_tidstore.c
* Test TidStore data structure.
*
* Note: all locking in this test module is useless since there is only
* a single process to use the TidStore. It is meant to be an example of
* usage.
*
* Copyright (c) 2024-2025, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/test/modules/test_tidstore/test_tidstore.c
*
* -------------------------------------------------------------------------
*/
#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"
PG_MODULE_MAGIC;
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 TidStore *tidstore = NULL;
static size_t tidstore_empty_size;
/* array for verification of some tests */
typedef struct ItemArray
{
ItemPointerData *insert_tids;
ItemPointerData *lookup_tids;
ItemPointerData *iter_tids;
int max_tids;
int num_tids;
} ItemArray;
static ItemArray items;
/* comparator routine for ItemPointer */
static int
itemptr_cmp(const void *left, const void *right)
{
BlockNumber lblk,
rblk;
OffsetNumber loff,
roff;
lblk = ItemPointerGetBlockNumber((ItemPointer) left);
rblk = ItemPointerGetBlockNumber((ItemPointer) right);
if (lblk < rblk)
return -1;
if (lblk > rblk)
return 1;
loff = ItemPointerGetOffsetNumber((ItemPointer) left);
roff = ItemPointerGetOffsetNumber((ItemPointer) right);
if (loff < roff)
return -1;
if (loff > roff)
return 1;
return 0;
}
/*
* Create a TidStore. If shared is false, the tidstore is created
* on TopMemoryContext, otherwise on DSA. Although the tidstore
* is created on DSA, only the same process can subsequently use
* the tidstore. The tidstore handle is not shared anywhere.
*/
Datum
test_create(PG_FUNCTION_ARGS)
{
bool shared = PG_GETARG_BOOL(0);
MemoryContext old_ctx;
/* doesn't really matter, since it's just a hint */
size_t tidstore_max_size = 2 * 1024 * 1024;
size_t array_init_size = 1024;
Assert(tidstore == NULL);
/*
* Create the TidStore on TopMemoryContext so that the same process use it
* for subsequent tests.
*/
old_ctx = MemoryContextSwitchTo(TopMemoryContext);
if (shared)
{
int tranche_id;
tranche_id = LWLockNewTrancheId();
LWLockRegisterTranche(tranche_id, "test_tidstore");
tidstore = TidStoreCreateShared(tidstore_max_size, tranche_id);
/*
* Remain attached until end of backend or explicitly detached so that
* the same process use the tidstore for subsequent tests.
*/
dsa_pin_mapping(TidStoreGetDSA(tidstore));
}
else
/* VACUUM uses insert only, so we test the other option. */
tidstore = TidStoreCreateLocal(tidstore_max_size, false);
tidstore_empty_size = TidStoreMemoryUsage(tidstore);
items.num_tids = 0;
items.max_tids = array_init_size / sizeof(ItemPointerData);
items.insert_tids = (ItemPointerData *) palloc0(array_init_size);
items.lookup_tids = (ItemPointerData *) palloc0(array_init_size);
items.iter_tids = (ItemPointerData *) palloc0(array_init_size);
MemoryContextSwitchTo(old_ctx);
PG_RETURN_VOID();
}
static void
sanity_check_array(ArrayType *ta)
{
if (ARR_HASNULL(ta) && array_contains_nulls(ta))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("array must not contain nulls")));
if (ARR_NDIM(ta) > 1)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("argument must be empty or one-dimensional array")));
}
static void
check_tidstore_available(void)
{
if (tidstore == NULL)
elog(ERROR, "tidstore is not created");
}
static void
purge_from_verification_array(BlockNumber blkno)
{
int dst = 0;
for (int src = 0; src < items.num_tids; src++)
if (ItemPointerGetBlockNumber(&items.insert_tids[src]) != blkno)
items.insert_tids[dst++] = items.insert_tids[src];
items.num_tids = dst;
}
/* Set the given block and offsets pairs */
Datum
do_set_block_offsets(PG_FUNCTION_ARGS)
{
BlockNumber blkno = PG_GETARG_INT64(0);
ArrayType *ta = PG_GETARG_ARRAYTYPE_P_COPY(1);
OffsetNumber *offs;
int noffs;
check_tidstore_available();
sanity_check_array(ta);
noffs = ArrayGetNItems(ARR_NDIM(ta), ARR_DIMS(ta));
offs = ((OffsetNumber *) ARR_DATA_PTR(ta));
/* Set TIDs in the store */
TidStoreLockExclusive(tidstore);
TidStoreSetBlockOffsets(tidstore, blkno, offs, noffs);
TidStoreUnlock(tidstore);
/* Remove the existing items of blkno from the verification array */
purge_from_verification_array(blkno);
/* Set TIDs in verification array */
for (int i = 0; i < noffs; i++)
{
ItemPointer tid;
int idx = items.num_tids + i;
/* Enlarge the TID arrays if necessary */
if (idx >= items.max_tids)
{
items.max_tids *= 2;
items.insert_tids = repalloc(items.insert_tids, sizeof(ItemPointerData) * items.max_tids);
items.lookup_tids = repalloc(items.lookup_tids, sizeof(ItemPointerData) * items.max_tids);
items.iter_tids = repalloc(items.iter_tids, sizeof(ItemPointerData) * items.max_tids);
}
tid = &(items.insert_tids[idx]);
ItemPointerSet(tid, blkno, offs[i]);
}
/* Update statistics */
items.num_tids += noffs;
PG_RETURN_INT64(blkno);
}
/*
* Verify TIDs in store against the array.
*/
Datum
check_set_block_offsets(PG_FUNCTION_ARGS)
{
TidStoreIter *iter;
TidStoreIterResult *iter_result;
int num_iter_tids = 0;
int num_lookup_tids = 0;
BlockNumber prevblkno = 0;
check_tidstore_available();
/* lookup each member in the verification array */
for (int i = 0; i < items.num_tids; i++)
if (!TidStoreIsMember(tidstore, &items.insert_tids[i]))
elog(ERROR, "missing TID with block %u, offset %u",
ItemPointerGetBlockNumber(&items.insert_tids[i]),
ItemPointerGetOffsetNumber(&items.insert_tids[i]));
/*
* Lookup all possible TIDs for each distinct block in the verification
* array and save successful lookups in the lookup array.
*/
for (int i = 0; i < items.num_tids; i++)
{
BlockNumber blkno = ItemPointerGetBlockNumber(&items.insert_tids[i]);
if (i > 0 && blkno == prevblkno)
continue;
for (OffsetNumber offset = FirstOffsetNumber; offset < MaxOffsetNumber; offset++)
{
ItemPointerData tid;
ItemPointerSet(&tid, blkno, offset);
TidStoreLockShare(tidstore);
if (TidStoreIsMember(tidstore, &tid))
ItemPointerSet(&items.lookup_tids[num_lookup_tids++], blkno, offset);
TidStoreUnlock(tidstore);
}
prevblkno = blkno;
}
/* Collect TIDs stored in the tidstore, in order */
TidStoreLockShare(tidstore);
iter = TidStoreBeginIterate(tidstore);
while ((iter_result = TidStoreIterateNext(iter)) != NULL)
{
OffsetNumber offsets[MaxOffsetNumber];
int num_offsets;
num_offsets = TidStoreGetBlockOffsets(iter_result, offsets, lengthof(offsets));
Assert(num_offsets <= lengthof(offsets));
for (int i = 0; i < num_offsets; i++)
ItemPointerSet(&(items.iter_tids[num_iter_tids++]), iter_result->blkno,
offsets[i]);
}
TidStoreEndIterate(iter);
TidStoreUnlock(tidstore);
/*
* Sort verification and lookup arrays and test that all arrays are the
* same.
*/
if (num_lookup_tids != items.num_tids)
elog(ERROR, "should have %d TIDs, have %d", items.num_tids, num_lookup_tids);
if (num_iter_tids != items.num_tids)
elog(ERROR, "should have %d TIDs, have %d", items.num_tids, num_iter_tids);
qsort(items.insert_tids, items.num_tids, sizeof(ItemPointerData), itemptr_cmp);
qsort(items.lookup_tids, items.num_tids, sizeof(ItemPointerData), itemptr_cmp);
for (int i = 0; i < items.num_tids; i++)
{
if (itemptr_cmp(&items.insert_tids[i], &items.iter_tids[i]) != 0)
elog(ERROR, "TID iter array doesn't match verification array, got (%u,%u) expected (%u,%u)",
ItemPointerGetBlockNumber(&items.iter_tids[i]),
ItemPointerGetOffsetNumber(&items.iter_tids[i]),
ItemPointerGetBlockNumber(&items.insert_tids[i]),
ItemPointerGetOffsetNumber(&items.insert_tids[i]));
if (itemptr_cmp(&items.insert_tids[i], &items.lookup_tids[i]) != 0)
elog(ERROR, "TID lookup array doesn't match verification array, got (%u,%u) expected (%u,%u)",
ItemPointerGetBlockNumber(&items.lookup_tids[i]),
ItemPointerGetOffsetNumber(&items.lookup_tids[i]),
ItemPointerGetBlockNumber(&items.insert_tids[i]),
ItemPointerGetOffsetNumber(&items.insert_tids[i]));
}
PG_RETURN_VOID();
}
/*
* In real world use, we care if the memory usage is greater than
* some configured limit. Here we just want to verify that
* TidStoreMemoryUsage is not broken.
*/
Datum
test_is_full(PG_FUNCTION_ARGS)
{
bool is_full;
check_tidstore_available();
is_full = (TidStoreMemoryUsage(tidstore) > tidstore_empty_size);
PG_RETURN_BOOL(is_full);
}
/* Free the tidstore */
Datum
test_destroy(PG_FUNCTION_ARGS)
{
check_tidstore_available();
TidStoreDestroy(tidstore);
tidstore = NULL;
items.num_tids = 0;
pfree(items.insert_tids);
pfree(items.lookup_tids);
pfree(items.iter_tids);
PG_RETURN_VOID();
}
|