-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.c
More file actions
377 lines (315 loc) · 10.3 KB
/
Copy pathbitmap.c
File metadata and controls
377 lines (315 loc) · 10.3 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <postgres.h>
#include <fmgr.h>
#include <string.h>
#include <storage/bufmgr.h>
#include <access/reloptions.h>
#include <access/tableam.h>
#include <storage/indexfsm.h>
#include <commands/vacuum.h>
#include <access/generic_xlog.h>
#include <nodes/execnodes.h>
#include <utils/memutils.h>
#include "bitmap.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
void _PG_init(void);
static relopt_kind bm_relopt_kind;
/*
* Module initialize function: initialize info about bitmap relation options.
*
*/
void
_PG_init(void)
{
bm_relopt_kind = add_reloption_kind();
}
bytea *
bmoptions(Datum reloptions, bool validate)
{
static const relopt_parse_elt tab[] = {{}, {}};
return (bytea *) build_reloptions(reloptions, validate,
bm_relopt_kind,
sizeof(BitmapOptions),
tab, lengthof(tab));
}
static BlockNumber
bm_insert_tuple(Relation index, BlockNumber startBlk, ItemPointer ctid)
{
Buffer buffer = InvalidBuffer;
Buffer nbuffer = InvalidBuffer;
BitmapTuple *tup = bitmap_form_tuple(ctid);
Page page;
BitmapPageOpaque opaque;
BlockNumber blkno = startBlk;
GenericXLogState *gxstate;
bool inserted;
/* insert bitmap tuple from the first block */
while (blkno != InvalidBlockNumber)
{
buffer = ReadBuffer(index, blkno);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
gxstate = GenericXLogStart(index);
page = GenericXLogRegisterBuffer(gxstate, buffer, 0);
/*
* TODO: we insert index tuple when there's page have space, it can
* result storing mulitple index tuples for the same heap block in
* different index blocks due to we remove index tuple on vacuum. we
* don't have enough metrics to decide if we need to optimize on this
* or not.
*/
/* do not salvage recently vacuumed page, not cleaned up yet */
/* update existing index tuple or insert new */
if (!BitmapPageDeleted(page) && bm_page_add_tup(page, tup, &inserted))
{
GenericXLogFinish(gxstate);
UnlockReleaseBuffer(buffer);
return startBlk;
}
opaque = BitmapPageGetOpaque(page);
blkno = opaque->nextBlk;
GenericXLogAbort(gxstate);
/* keep last buffer active for linking new buffer page */
if (blkno != InvalidBlockNumber)
UnlockReleaseBuffer(buffer);
}
nbuffer = bm_newbuffer_locked(index);
blkno = BufferGetBlockNumber(nbuffer);
gxstate = GenericXLogStart(index);
page = GenericXLogRegisterBuffer(gxstate, nbuffer, GENERIC_XLOG_FULL_IMAGE);
bm_init_page(page, BITMAP_PAGE_INDEX);
if (!bm_page_add_tup(page, tup, &inserted))
elog(ERROR, "insert bitmap tuple failed on new page");
if (buffer != InvalidBuffer)
{
page = GenericXLogRegisterBuffer(gxstate, buffer, 0);
opaque = BitmapPageGetOpaque(page);
opaque->nextBlk = blkno;
}
GenericXLogFinish(gxstate);
UnlockReleaseBuffer(nbuffer);
if (buffer != InvalidBuffer)
UnlockReleaseBuffer(buffer);
return startBlk == InvalidBlockNumber ? blkno : startBlk;
}
bool
bminsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid,
Relation heapRel, IndexUniqueCheck checkUnique,
bool indexUnchanged, IndexInfo *indexInfo)
{
BitmapState *state = (BitmapState *) indexInfo->ii_AmCache;
MemoryContext oldCxt;
BitmapMetaPageData *metadata;
BlockNumber startBlk;
Buffer buffer;
Page page;
int valindex = -1;
GenericXLogState *gxstate;
if (state == NULL)
{
oldCxt = MemoryContextSwitchTo(indexInfo->ii_Context);
state = palloc0(sizeof(BitmapState));
state->tmpCxt = AllocSetContextCreate(CurrentMemoryContext, "bitmap insert context",
ALLOCSET_DEFAULT_SIZES);
indexInfo->ii_AmCache = (void *) state;
MemoryContextSwitchTo(oldCxt);
}
oldCxt = MemoryContextSwitchTo(state->tmpCxt);
/* value page addresses contention of inserting same values */
valindex = bm_insert_val(index, values, isnull);
metadata = bm_get_meta(index);
startBlk = metadata->startBlk[valindex];
state->startBlk = bm_insert_tuple(index, startBlk, ht_ctid);
/*
* index value does not exists or exist but no index tuples due to
* deletion we need to increase distinct value in either case
*/
if (startBlk == InvalidBlockNumber)
{
gxstate = GenericXLogStart(index);
buffer = ReadBuffer(index, BITMAP_METAPAGE_BLKNO);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
page = GenericXLogRegisterBuffer(gxstate, buffer, 0);
metadata = BitmapPageGetMeta(page);
/* meta data already being updated by concurrent insertion */
if (metadata->startBlk[valindex] != InvalidBlockNumber)
{
GenericXLogAbort(gxstate);
}
else
{
metadata->ndistinct += 1;
metadata->startBlk[valindex] = state->startBlk;
GenericXLogFinish(gxstate);
}
UnlockReleaseBuffer(buffer);
}
MemoryContextSwitchTo(oldCxt);
MemoryContextReset(state->tmpCxt);
return false;
}
static void
bmBuildCallback(Relation index, ItemPointer tid, Datum *values,
bool *isnull, bool tupleIsAlive, void *state)
{
BitmapBuildState *buildstate = (BitmapBuildState *) state;
MemoryContext oldCtx;
BitmapPageOpaque opaque;
BitmapTuple *btup;
BlockNumber blkno;
Page bufpage;
Page page,
prevpage;
Buffer buffer,
pbuffer = InvalidBuffer;
GenericXLogState *gxstate;
int valindex;
bool inserted;
oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
valindex = bm_insert_val(index, values, isnull);
if (valindex == buildstate->ndistinct)
{
buildstate->ndistinct++;
}
if (!buildstate->blocks[valindex])
{
buildstate->blocks[valindex] = (PGAlignedBlock *) palloc0(BLCKSZ);
bm_init_page((Page) buildstate->blocks[valindex], BITMAP_PAGE_INDEX);
}
btup = bitmap_form_tuple(tid);
bufpage = (Page) buildstate->blocks[valindex];
if (!bm_page_add_tup(bufpage, btup, &inserted))
{
gxstate = GenericXLogStart(index);
buffer = bm_newbuffer_locked(index);
blkno = BufferGetBlockNumber(buffer);
page = GenericXLogRegisterBuffer(gxstate, buffer, GENERIC_XLOG_FULL_IMAGE);
if (buildstate->startBlks[valindex] == InvalidBlockNumber)
buildstate->startBlks[valindex] = blkno;
/* set next block number in previous block page */
if (buildstate->prevBlks[valindex] != InvalidBlockNumber)
{
pbuffer = ReadBuffer(index, buildstate->prevBlks[valindex]);
LockBuffer(pbuffer, BUFFER_LOCK_EXCLUSIVE);
prevpage = GenericXLogRegisterBuffer(gxstate, pbuffer, 0);
opaque = BitmapPageGetOpaque(prevpage);
opaque->nextBlk = blkno;
}
buildstate->prevBlks[valindex] = blkno;
memcpy(page, bufpage, BLCKSZ);
GenericXLogFinish(gxstate);
UnlockReleaseBuffer(buffer);
if (pbuffer != InvalidBuffer)
UnlockReleaseBuffer(pbuffer);
bm_init_page(bufpage, BITMAP_PAGE_INDEX);
if (!bm_page_add_tup(bufpage, btup, &inserted))
elog(ERROR, "could not add new tuple to empty page");
}
if (inserted) {
buildstate->indtuples++;
}
MemoryContextSwitchTo(oldCtx);
}
IndexBuildResult *
bmbuild(Relation heap, Relation index,
IndexInfo *indexInfo)
{
IndexBuildResult *result;
double reltuples;
BitmapBuildState buildstate;
Buffer buffer;
Page metapage;
BitmapMetaPageData *metadata;
GenericXLogState *gxstate;
if (RelationGetNumberOfBlocks(index) != 0)
elog(ERROR, "index \"%s\" already contains data",
RelationGetRelationName(index));
gxstate = GenericXLogStart(index);
/* Initialize the meta page */
bm_init_metapage(index, MAIN_FORKNUM);
buffer = ReadBuffer(index, BITMAP_METAPAGE_BLKNO);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
metapage = GenericXLogRegisterBuffer(gxstate, buffer, 0);
metadata = BitmapPageGetMeta(metapage);
/* Init value page */
bm_init_valuepage(index, MAIN_FORKNUM);
/* Initialize the build state */
memset(&buildstate, 0, sizeof(buildstate));
buildstate.tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
"Bitmap build temporary context",
ALLOCSET_DEFAULT_SIZES);
buildstate.blocks = palloc0(sizeof(PGAlignedBlock *) * MAX_DISTINCT);
buildstate.startBlks = palloc0(sizeof(BlockNumber) * MAX_DISTINCT);
buildstate.prevBlks = palloc0(sizeof(BlockNumber) * MAX_DISTINCT);
memset(buildstate.startBlks, 0xFF, sizeof(BlockNumber) * MAX_DISTINCT);
memset(buildstate.prevBlks, 0xFF, sizeof(BlockNumber) * MAX_DISTINCT);
/* Do the heap scan */
reltuples = table_index_build_scan(heap, index, indexInfo, true, true,
bmBuildCallback, (void *) &buildstate,
NULL);
bm_flush_cached(index, &buildstate);
metadata->ndistinct = buildstate.ndistinct;
memcpy(metadata->startBlk, buildstate.startBlks, sizeof(BlockNumber) * buildstate.ndistinct);
GenericXLogFinish(gxstate);
UnlockReleaseBuffer(buffer);
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
result->heap_tuples = reltuples;
result->index_tuples = buildstate.indtuples;
MemoryContextDelete(buildstate.tmpCtx);
return result;
}
void
bmbuildempty(Relation index)
{
bm_init_metapage(index, INIT_FORKNUM);
bm_init_valuepage(index, INIT_FORKNUM);
}
PG_FUNCTION_INFO_V1(bmhandler);
Datum
bmhandler(PG_FUNCTION_ARGS)
{
IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
amroutine->amstrategies = BITMAP_NSTRATEGIES;
amroutine->amsupport = 1;
amroutine->amoptsprocnum = 0;
amroutine->amcanorder = false;
amroutine->amcanorderbyop = false;
amroutine->amcanbackward = false;
amroutine->amcanunique = false;
amroutine->amcanmulticol = true;
amroutine->amoptionalkey = false;
amroutine->amsearcharray = false;
amroutine->amsearchnulls = true;
amroutine->amstorage = false;
amroutine->amclusterable = false;
amroutine->ampredlocks = false;
amroutine->amcanparallel = false;
amroutine->amcaninclude = false;
amroutine->amusemaintenanceworkmem = false;
amroutine->amparallelvacuumoptions =
VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_CLEANUP;
amroutine->amkeytype = InvalidOid;
amroutine->ambuild = bmbuild;
amroutine->ambuildempty = bmbuildempty;
amroutine->aminsert = bminsert;
amroutine->ambulkdelete = bmbulkdelete;
amroutine->amvacuumcleanup = bmvacuumcleanup;
amroutine->amcanreturn = NULL;
amroutine->amcostestimate = bmcostestimate;
amroutine->amoptions = bmoptions;
amroutine->amproperty = NULL;
amroutine->ambuildphasename = NULL;
amroutine->amvalidate = bmvalidate;
amroutine->amadjustmembers = NULL;
amroutine->ambeginscan = bmbeginscan;
amroutine->amrescan = bmrescan;
amroutine->amgettuple = bmgettuple;
amroutine->amgetbitmap = bmgetbitmap;
amroutine->amendscan = bmendscan;
amroutine->ammarkpos = NULL;
amroutine->amrestrpos = NULL;
amroutine->amestimateparallelscan = NULL;
amroutine->aminitparallelscan = NULL;
amroutine->amparallelrescan = NULL;
PG_RETURN_POINTER(amroutine);
}