-
Notifications
You must be signed in to change notification settings - Fork 848
/
scanner.c
529 lines (451 loc) · 13.1 KB
/
scanner.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <access/relscan.h>
#include <access/xact.h>
#include <access/htup_details.h>
#include <executor/tuptable.h>
#include <storage/lmgr.h>
#include <storage/bufmgr.h>
#include <storage/procarray.h>
#include <utils/rel.h>
#include <utils/palloc.h>
#include <utils/snapmgr.h>
#include "scanner.h"
enum ScannerType
{
ScannerTypeTable,
ScannerTypeIndex,
};
/*
* Scanner can implement both index and heap scans in a single interface.
*/
typedef struct Scanner
{
Relation (*openscan)(ScannerCtx *ctx);
ScanDesc (*beginscan)(ScannerCtx *ctx);
bool (*getnext)(ScannerCtx *ctx);
void (*rescan)(ScannerCtx *ctx);
void (*endscan)(ScannerCtx *ctx);
void (*closescan)(ScannerCtx *ctx);
} Scanner;
/* Functions implementing heap scans */
static Relation
table_scanner_open(ScannerCtx *ctx)
{
ctx->tablerel = table_open(ctx->table, ctx->lockmode);
return ctx->tablerel;
}
static ScanDesc
table_scanner_beginscan(ScannerCtx *ctx)
{
ctx->internal.scan.table_scan =
table_beginscan(ctx->tablerel, ctx->snapshot, ctx->nkeys, ctx->scankey);
return ctx->internal.scan;
}
static bool
table_scanner_getnext(ScannerCtx *ctx)
{
bool success = table_scan_getnextslot(ctx->internal.scan.table_scan,
ForwardScanDirection,
ctx->internal.tinfo.slot);
return success;
}
static void
table_scanner_rescan(ScannerCtx *ctx)
{
table_rescan(ctx->internal.scan.table_scan, ctx->scankey);
}
static void
table_scanner_endscan(ScannerCtx *ctx)
{
table_endscan(ctx->internal.scan.table_scan);
}
static void
table_scanner_close(ScannerCtx *ctx)
{
LOCKMODE lockmode = (ctx->flags & SCANNER_F_KEEPLOCK) ? NoLock : ctx->lockmode;
table_close(ctx->tablerel, lockmode);
}
/* Functions implementing index scans */
static Relation
index_scanner_open(ScannerCtx *ctx)
{
ctx->tablerel = table_open(ctx->table, ctx->lockmode);
ctx->indexrel = index_open(ctx->index, ctx->lockmode);
return ctx->indexrel;
}
static ScanDesc
index_scanner_beginscan(ScannerCtx *ctx)
{
InternalScannerCtx *ictx = &ctx->internal;
ictx->scan.index_scan =
index_beginscan(ctx->tablerel, ctx->indexrel, ctx->snapshot, ctx->nkeys, ctx->norderbys);
ictx->scan.index_scan->xs_want_itup = ctx->want_itup;
index_rescan(ictx->scan.index_scan, ctx->scankey, ctx->nkeys, NULL, ctx->norderbys);
return ictx->scan;
}
static bool
index_scanner_getnext(ScannerCtx *ctx)
{
InternalScannerCtx *ictx = &ctx->internal;
bool success;
success = index_getnext_slot(ictx->scan.index_scan, ctx->scandirection, ictx->tinfo.slot);
ictx->tinfo.ituple = ictx->scan.index_scan->xs_itup;
ictx->tinfo.ituple_desc = ictx->scan.index_scan->xs_itupdesc;
return success;
}
static void
index_scanner_rescan(ScannerCtx *ctx)
{
index_rescan(ctx->internal.scan.index_scan, ctx->scankey, ctx->nkeys, NULL, ctx->norderbys);
}
static void
index_scanner_endscan(ScannerCtx *ctx)
{
index_endscan(ctx->internal.scan.index_scan);
}
static void
index_scanner_close(ScannerCtx *ctx)
{
LOCKMODE lockmode = (ctx->flags & SCANNER_F_KEEPLOCK) ? NoLock : ctx->lockmode;
index_close(ctx->indexrel, ctx->lockmode);
table_close(ctx->tablerel, lockmode);
}
/*
* Two scanners by type: heap and index scanners.
*/
static Scanner scanners[] = {
[ScannerTypeTable] = {
.openscan = table_scanner_open,
.beginscan = table_scanner_beginscan,
.getnext = table_scanner_getnext,
.rescan = table_scanner_rescan,
.endscan = table_scanner_endscan,
.closescan = table_scanner_close,
},
[ScannerTypeIndex] = {
.openscan = index_scanner_open,
.beginscan = index_scanner_beginscan,
.getnext = index_scanner_getnext,
.rescan = index_scanner_rescan,
.endscan = index_scanner_endscan,
.closescan = index_scanner_close,
}
};
static inline Scanner *
scanner_ctx_get_scanner(ScannerCtx *ctx)
{
if (OidIsValid(ctx->index))
return &scanners[ScannerTypeIndex];
else
return &scanners[ScannerTypeTable];
}
TSDLLEXPORT void
ts_scanner_rescan(ScannerCtx *ctx, const ScanKey scankey)
{
Scanner *scanner = scanner_ctx_get_scanner(ctx);
MemoryContext oldmcxt;
/* If scankey is NULL, the existing scan key was already updated or the
* old should be reused */
if (NULL != scankey)
memcpy(ctx->scankey, scankey, sizeof(*ctx->scankey));
oldmcxt = MemoryContextSwitchTo(ctx->internal.scan_mcxt);
scanner->rescan(ctx);
MemoryContextSwitchTo(oldmcxt);
}
static void
prepare_scan(ScannerCtx *ctx)
{
ctx->internal.ended = false;
ctx->internal.registered_snapshot = false;
if (ctx->internal.scan_mcxt == NULL)
ctx->internal.scan_mcxt = CurrentMemoryContext;
if (ctx->snapshot == NULL)
{
/*
* We use SnapshotSelf by default, for historical reasons mostly, but
* we probably want to move to an MVCC snapshot as the default. The
* difference is that a Self snapshot is an "instant" snapshot and can
* see its own changes. More importantly, however, unlike an MVCC
* snapshot, a Self snapshot is not subject to the strictness of
* SERIALIZABLE isolation mode.
*
* This is important in case of, e.g., concurrent chunk creation by
* two transactions; we'd like a transaction to use a new chunk as
* soon as the creating transaction commits, so that there aren't
* multiple transactions creating the same chunk and all but one fails
* with a conflict. However, under SERIALIZABLE mode a transaction is
* only allowed to read data from transactions that were committed
* prior to transaction start. This means that two or more
* transactions that create the same chunk must have all but the first
* committed transaction fail.
*
* Therefore, we probably want to exempt internal bookkeeping metadata
* from full SERIALIZABLE semantics (at least in the case of chunk
* creation), or otherwise the INSERT behavior will be different for
* hypertables compared to regular tables under SERIALIZABLE
* mode.
*/
MemoryContext oldmcxt = MemoryContextSwitchTo(ctx->internal.scan_mcxt);
ctx->snapshot = RegisterSnapshot(GetSnapshotData(SnapshotSelf));
/*
* Invalidate the PG catalog snapshot to ensure it is refreshed and
* up-to-date with the snapshot used to scan TimescaleDB
* metadata. Since TimescaleDB metadata is often joined with PG
* catalog data (e.g., calling get_relname_relid() to fill in chunk
* table Oid), this avoids any potential problems where the different
* snapshots used to scan TimescaleDB metadata and PG catalog metadata
* aren't in sync.
*
* Ideally, a catalog snapshot would be used to scan TimescaleDB
* metadata, but that will change the behavior of chunk creation in
* SERIALIZED mode, as described above.
*/
InvalidateCatalogSnapshot();
ctx->internal.registered_snapshot = true;
MemoryContextSwitchTo(oldmcxt);
}
}
TSDLLEXPORT Relation
ts_scanner_open(ScannerCtx *ctx)
{
Scanner *scanner = scanner_ctx_get_scanner(ctx);
MemoryContext oldmcxt;
Relation rel;
Assert(NULL == ctx->tablerel);
prepare_scan(ctx);
Assert(ctx->internal.scan_mcxt != NULL);
oldmcxt = MemoryContextSwitchTo(ctx->internal.scan_mcxt);
rel = scanner->openscan(ctx);
MemoryContextSwitchTo(oldmcxt);
return rel;
}
/*
* Start either a heap or index scan depending on the information in the
* ScannerCtx. ScannerCtx must be setup by caller with the proper information
* for the scan, including filters and callbacks for found tuples.
*/
TSDLLEXPORT void
ts_scanner_start_scan(ScannerCtx *ctx)
{
InternalScannerCtx *ictx = &ctx->internal;
Scanner *scanner;
TupleDesc tuple_desc;
MemoryContext oldmcxt;
if (ictx->started)
{
Assert(!ictx->ended);
Assert(ctx->tablerel);
Assert(OidIsValid(ctx->table));
return;
}
if (ctx->tablerel == NULL)
{
Assert(NULL == ctx->indexrel);
ts_scanner_open(ctx);
}
else
{
/*
* Relations already opened by caller: Only need to prepare the scan
* and set relation Oids so that the scanner knows which scanner
* implementation to use. Respect the auto-closing behavior set by the
* user, which is to auto close if unspecified.
*/
prepare_scan(ctx);
ctx->table = RelationGetRelid(ctx->tablerel);
if (NULL != ctx->indexrel)
ctx->index = RelationGetRelid(ctx->indexrel);
}
Assert(ctx->internal.scan_mcxt != NULL);
oldmcxt = MemoryContextSwitchTo(ctx->internal.scan_mcxt);
scanner = scanner_ctx_get_scanner(ctx);
scanner->beginscan(ctx);
tuple_desc = RelationGetDescr(ctx->tablerel);
ictx->tinfo.scanrel = ctx->tablerel;
ictx->tinfo.mctx = ctx->result_mctx == NULL ? CurrentMemoryContext : ctx->result_mctx;
ictx->tinfo.slot = MakeSingleTupleTableSlot(tuple_desc, table_slot_callbacks(ctx->tablerel));
MemoryContextSwitchTo(oldmcxt);
/* Call pre-scan handler, if any. */
if (ctx->prescan != NULL)
ctx->prescan(ctx->data);
ictx->started = true;
}
static inline bool
ts_scanner_limit_reached(ScannerCtx *ctx)
{
return ctx->limit > 0 && ctx->internal.tinfo.count >= ctx->limit;
}
static void
scanner_cleanup(ScannerCtx *ctx)
{
InternalScannerCtx *ictx = &ctx->internal;
if (ictx->registered_snapshot)
{
UnregisterSnapshot(ctx->snapshot);
ctx->snapshot = NULL;
}
if (NULL != ictx->tinfo.slot)
{
ExecDropSingleTupleTableSlot(ictx->tinfo.slot);
ictx->tinfo.slot = NULL;
}
if (NULL != ictx->scan_mcxt)
{
ictx->scan_mcxt = NULL;
}
}
TSDLLEXPORT void
ts_scanner_end_scan(ScannerCtx *ctx)
{
InternalScannerCtx *ictx = &ctx->internal;
Scanner *scanner = scanner_ctx_get_scanner(ctx);
MemoryContext oldmcxt;
if (ictx->ended)
return;
/* Call post-scan handler, if any. */
if (ctx->postscan != NULL)
ctx->postscan(ictx->tinfo.count, ctx->data);
oldmcxt = MemoryContextSwitchTo(ctx->internal.scan_mcxt);
scanner->endscan(ctx);
MemoryContextSwitchTo(oldmcxt);
scanner_cleanup(ctx);
ictx->ended = true;
ictx->started = false;
}
TSDLLEXPORT void
ts_scanner_close(ScannerCtx *ctx)
{
Scanner *scanner = scanner_ctx_get_scanner(ctx);
Assert(ctx->internal.ended);
if (NULL != ctx->tablerel)
{
scanner->closescan(ctx);
ctx->tablerel = NULL;
ctx->indexrel = NULL;
}
}
TSDLLEXPORT TupleInfo *
ts_scanner_next(ScannerCtx *ctx)
{
InternalScannerCtx *ictx = &ctx->internal;
Scanner *scanner = scanner_ctx_get_scanner(ctx);
bool is_valid = false;
if (!ts_scanner_limit_reached(ctx))
{
MemoryContext oldmcxt = MemoryContextSwitchTo(ctx->internal.scan_mcxt);
is_valid = scanner->getnext(ctx);
MemoryContextSwitchTo(oldmcxt);
}
while (is_valid)
{
if (ctx->filter == NULL || ctx->filter(&ictx->tinfo, ctx->data) == SCAN_INCLUDE)
{
ictx->tinfo.count++;
if (ctx->tuplock)
{
TupleTableSlot *slot = ictx->tinfo.slot;
Assert(ctx->snapshot);
ictx->tinfo.lockresult = table_tuple_lock(ctx->tablerel,
&(slot->tts_tid),
ctx->snapshot,
slot,
GetCurrentCommandId(false),
ctx->tuplock->lockmode,
ctx->tuplock->waitpolicy,
ctx->tuplock->lockflags,
&ictx->tinfo.lockfd);
}
/* stop at a valid tuple */
return &ictx->tinfo;
}
if (ts_scanner_limit_reached(ctx))
is_valid = false;
else
{
MemoryContext oldmcxt = MemoryContextSwitchTo(ctx->internal.scan_mcxt);
is_valid = scanner->getnext(ctx);
MemoryContextSwitchTo(oldmcxt);
}
}
if (!(ctx->flags & SCANNER_F_NOEND))
ts_scanner_end_scan(ctx);
if (!(ctx->flags & SCANNER_F_NOEND_AND_NOCLOSE))
ts_scanner_close(ctx);
return NULL;
}
/*
* Perform either a heap or index scan depending on the information in the
* ScannerCtx. ScannerCtx must be setup by caller with the proper information
* for the scan, including filters and callbacks for found tuples.
*
* Return the number of tuples that were found.
*/
TSDLLEXPORT int
ts_scanner_scan(ScannerCtx *ctx)
{
TupleInfo *tinfo;
MemSet(&ctx->internal, 0, sizeof(ctx->internal));
for (ts_scanner_start_scan(ctx); (tinfo = ts_scanner_next(ctx));)
{
if (ctx->tuple_found != NULL)
{
ScanTupleResult scan_result = ctx->tuple_found(tinfo, ctx->data);
/* Call tuple_found handler. Abort the scan if the handler wants us to */
if (scan_result == SCAN_DONE)
{
if (!(ctx->flags & SCANNER_F_NOEND))
ts_scanner_end_scan(ctx);
if (!(ctx->flags & SCANNER_F_NOEND_AND_NOCLOSE))
ts_scanner_close(ctx);
break;
}
else if (scan_result == SCAN_RESCAN)
{
ctx->internal.tinfo.count = 0;
ts_scanner_rescan(ctx, NULL);
continue;
}
}
}
return ctx->internal.tinfo.count;
}
TSDLLEXPORT bool
ts_scanner_scan_one(ScannerCtx *ctx, bool fail_if_not_found, const char *item_type)
{
int num_found = ts_scanner_scan(ctx);
ctx->limit = 2;
switch (num_found)
{
case 0:
if (fail_if_not_found)
{
elog(ERROR, "%s not found", item_type);
}
return false;
case 1:
return true;
default:
elog(ERROR, "more than one %s found", item_type);
return false;
}
}
ItemPointer
ts_scanner_get_tuple_tid(TupleInfo *ti)
{
return &ti->slot->tts_tid;
}
HeapTuple
ts_scanner_fetch_heap_tuple(const TupleInfo *ti, bool materialize, bool *should_free)
{
return ExecFetchSlotHeapTuple(ti->slot, materialize, should_free);
}
TupleDesc
ts_scanner_get_tupledesc(const TupleInfo *ti)
{
return ti->slot->tts_tupleDescriptor;
}