-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcompression_scankey.c
More file actions
653 lines (579 loc) · 18.7 KB
/
Copy pathcompression_scankey.c
File metadata and controls
653 lines (579 loc) · 18.7 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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
* This file and its contents are licensed under the Timescale License.
* Please see the included NOTICE for copyright information and
* LICENSE-TIMESCALE for a copy of the license.
*/
#include <postgres.h>
#include <catalog/pg_am.h>
#include <parser/parse_coerce.h>
#include <parser/parse_relation.h>
#include <utils/typcache.h>
#include "compression.h"
#include "compression_dml.h"
#include "create.h"
#include "ts_catalog/array_utils.h"
static Oid deduce_filter_subtype(BatchFilter *filter, Oid att_typoid);
static bool create_segment_filter_scankey(Relation in_rel, char *segment_filter_col_name,
StrategyNumber strategy, Oid subtype, Oid opcode,
ScanKeyData *scankeys, int *num_scankeys,
Bitmapset **null_columns, Datum value, bool is_null_check,
bool is_array_op);
/*
* Test ScanKey against a slot.
*
* Unlike HeapKeyTest, this function takes into account SK_ISNULL
* and works correctly when looking for null values.
*
* If slot attribute is NULL and key is NOT NULL,
* (key >= NULL) returns True for nulls_first
* and (key <= NULL) returns True for !nulls_first (i.e. for NULLS LAST).
*/
bool
slot_key_test(TupleTableSlot *compressed_slot, ScanKey key, bool nulls_first)
{
/* No need to get the datum if we are only checking for NULL key */
if (key->sk_flags & SK_ISNULL)
{
return slot_attisnull(compressed_slot, key->sk_attno);
}
Datum val;
bool is_null;
val = slot_getattr(compressed_slot, key->sk_attno, &is_null);
if (is_null)
{
/* NULL < key i.e. NULL sorts before key argument */
if (nulls_first && (key->sk_strategy == BTLessStrategyNumber ||
key->sk_strategy == BTLessEqualStrategyNumber))
{
return true;
}
/* NULL > key i.e. NULL sorts after key argument */
if (!nulls_first && (key->sk_strategy == BTGreaterStrategyNumber ||
key->sk_strategy == BTGreaterEqualStrategyNumber))
{
return true;
}
return false;
}
return DatumGetBool(FunctionCall2Coll(&key->sk_func, key->sk_collation, val, key->sk_argument));
}
/*
* Build scankeys for decompressed tuple to check if it is part of the batch.
*
* The key_columns are the columns of the uncompressed chunk.
*/
ScanKeyData *
build_mem_scankeys_from_slot(Oid ht_relid, CompressionSettings *settings, Relation out_rel,
tuple_filtering_constraints *constraints, TupleTableSlot *slot,
int *num_scankeys, AttrNumber **slot_attnos)
{
ScanKeyData *scankeys = NULL;
int key_index = 0;
TupleDesc out_desc = RelationGetDescr(out_rel);
if (bms_is_empty(constraints->key_columns))
{
*num_scankeys = key_index;
return scankeys;
}
int max_key_columns = bms_num_members(constraints->key_columns);
scankeys = palloc(sizeof(ScanKeyData) * max_key_columns);
*slot_attnos = palloc0(sizeof(AttrNumber) * max_key_columns);
AttrNumber attno = -1;
while ((attno = bms_next_member(constraints->key_columns, attno)) > 0)
{
bool isnull;
/*
* slot has the physical layout of the hypertable, so we need to
* get the attribute number of the hypertable for the column.
*/
char *attname = get_attname(out_rel->rd_id, attno, false);
/*
* We can skip any segmentby columns here since they have already been
* checked during batch filtering.
*/
if (ts_array_is_member(settings->fd.segmentby, attname))
{
continue;
}
AttrNumber ht_attno = get_attnum(ht_relid, attname);
Datum value = slot_getattr(slot, ht_attno, &isnull);
(*slot_attnos)[key_index] = ht_attno;
Oid atttypid = TupleDescAttr(out_desc, AttrNumberGetAttrOffset(attno))->atttypid;
TypeCacheEntry *tce = lookup_type_cache(atttypid, TYPECACHE_BTREE_OPFAMILY);
/*
* Should never happen since the column is part of unique constraint
* and should therefore have the required opfamily
*/
if (!OidIsValid(tce->btree_opf))
{
elog(ERROR, "no btree opfamily for type \"%s\"", format_type_be(atttypid));
}
Oid opr = get_opfamily_member(tce->btree_opf, atttypid, atttypid, BTEqualStrategyNumber);
/*
* Fall back to btree operator input type when it is binary compatible with
* the column type and no operator for column type could be found.
*/
if (!OidIsValid(opr) && IsBinaryCoercible(atttypid, tce->btree_opintype))
{
opr = get_opfamily_member(tce->btree_opf,
tce->btree_opintype,
tce->btree_opintype,
BTEqualStrategyNumber);
}
if (!OidIsValid(opr))
{
elog(ERROR, "no operator found for type \"%s\"", format_type_be(atttypid));
}
ScanKeyEntryInitialize(&scankeys[key_index++],
isnull ? SK_ISNULL : 0,
attno,
BTEqualStrategyNumber,
atttypid,
TupleDescAttr(out_desc, AttrNumberGetAttrOffset(attno))
->attcollation,
get_opcode(opr),
isnull ? 0 : value);
}
*num_scankeys = key_index;
return scankeys;
}
/*
* Build scankeys for decompression of specific batches. key_columns references the
* columns of the uncompressed chunk.
*/
ScanKeyData *
build_heap_scankeys(Oid hypertable_relid, Relation in_rel, Relation out_rel,
CompressionSettings *settings, Bitmapset *key_columns, Bitmapset **null_columns,
TupleTableSlot *slot, int *num_scankeys, AttrNumber **slot_attnos)
{
int key_index = 0;
ScanKeyData *scankeys = NULL;
if (!bms_is_empty(key_columns))
{
int max_key_columns = bms_num_members(key_columns) * 2;
scankeys = palloc0(max_key_columns * sizeof(ScanKeyData));
*slot_attnos = palloc0(max_key_columns * sizeof(AttrNumber));
AttrNumber attno = -1;
while ((attno = bms_next_member(key_columns, attno)) > 0)
{
char *attname = get_attname(out_rel->rd_id, attno, false);
bool isnull;
AttrNumber ht_attno = get_attnum(hypertable_relid, attname);
/*
* This is a not very precise but easy assertion to detect attno
* mismatch at least in some cases. The mismatch might happen if the
* hypertable and chunk layout are different because of dropped
* columns, and we're using a wrong slot type here.
*/
PG_USED_FOR_ASSERTS_ONLY Oid ht_atttype = get_atttype(hypertable_relid, ht_attno);
PG_USED_FOR_ASSERTS_ONLY Oid slot_atttype =
TupleDescAttr(slot->tts_tupleDescriptor, AttrNumberGetAttrOffset(ht_attno))
->atttypid;
Assert(ht_atttype == slot_atttype);
Datum value = slot_getattr(slot, ht_attno, &isnull);
/*
* There are 3 possible scenarios we have to consider
* when dealing with columns which are part of unique
* constraints.
*
* 1. Column is segmentby-Column
* In this case we can add a single ScanKey with an
* equality check for the value.
* 2. Column is orderby-Column
* In this we can add 2 ScanKeys with range constraints
* utilizing batch metadata.
* 3. Column is neither segmentby nor orderby
* In this case we cannot utilize this column for
* batch filtering as the values are compressed and
* we have no metadata.
*/
if (ts_array_is_member(settings->fd.segmentby, attname))
{
if (create_segment_filter_scankey(in_rel,
attname,
BTEqualStrategyNumber,
InvalidOid,
InvalidOid,
scankeys,
&key_index,
null_columns,
value,
isnull,
false))
{
(*slot_attnos)[key_index - 1] = ht_attno;
}
}
if (ts_array_is_member(settings->fd.orderby, attname))
{
/* Cannot optimize orderby columns with NULL values since those
* are not visible in metadata
*/
if (isnull)
{
continue;
}
int16 index = ts_array_position(settings->fd.orderby, attname);
if (create_segment_filter_scankey(in_rel,
column_segment_min_name(index),
BTLessEqualStrategyNumber,
InvalidOid,
InvalidOid,
scankeys,
&key_index,
null_columns,
value,
false,
false /* is_null_check */
))
{
(*slot_attnos)[key_index - 1] = ht_attno;
}
if (create_segment_filter_scankey(in_rel,
column_segment_max_name(index),
BTGreaterEqualStrategyNumber,
InvalidOid,
InvalidOid,
scankeys,
&key_index,
null_columns,
value,
false,
false /* is_null_check */
))
{
(*slot_attnos)[key_index - 1] = ht_attno;
}
}
}
}
*num_scankeys = key_index;
return scankeys;
}
/*
* This method will build scan keys required to do index
* scans on compressed chunks.
*/
ScanKeyData *
build_index_scankeys(Relation index_rel, List *index_filters, int *num_scankeys)
{
ListCell *lc;
BatchFilter *filter = NULL;
*num_scankeys = list_length(index_filters);
ScanKeyData *scankey = palloc0(sizeof(ScanKeyData) * (*num_scankeys));
int idx = 0;
int flags;
/* Order scankeys based on index attribute order */
for (int idx_attno = 1; idx_attno <= index_rel->rd_index->indnkeyatts && idx < *num_scankeys;
idx_attno++)
{
AttrNumber attno = index_rel->rd_index->indkey.values[AttrNumberGetAttrOffset(idx_attno)];
char *attname = get_attname(index_rel->rd_index->indrelid, attno, false);
Oid typoid = attnumTypeId(index_rel, idx_attno);
foreach (lc, index_filters)
{
filter = lfirst(lc);
if (!strcmp(attname, NameStr(filter->column_name)))
{
flags = 0;
if (filter->is_null_check)
{
flags = SK_ISNULL | (filter->is_null ? SK_SEARCHNULL : SK_SEARCHNOTNULL);
}
if (filter->is_array_op)
{
flags |= SK_SEARCHARRAY;
}
ScanKeyEntryInitialize(&scankey[idx++],
flags,
idx_attno,
filter->strategy,
deduce_filter_subtype(filter, typoid), /* subtype */
filter->collation,
filter->opcode,
filter->value ? filter->value->constvalue : 0);
}
}
}
Assert(idx == *num_scankeys);
return scankey;
}
/* This method is used to find matching index on compressed chunk
* and build scan keys from the slot data
*/
ScanKeyData *
build_index_scankeys_using_slot(Oid hypertable_relid, Relation in_rel, Relation out_rel,
Bitmapset *key_columns, TupleTableSlot *slot,
Relation *result_index_rel, Bitmapset **index_columns,
int *num_scan_keys, AttrNumber **slot_attnos)
{
List *index_oids;
ListCell *lc;
ScanKeyData *scankeys = NULL;
/* get list of indexes defined on compressed chunk */
index_oids = RelationGetIndexList(in_rel);
*num_scan_keys = 0;
foreach (lc, index_oids)
{
Relation index_rel = index_open(lfirst_oid(lc), AccessShareLock);
IndexInfo *index_info = BuildIndexInfo(index_rel);
/* Can't use partial or expression indexes */
if (index_info->ii_Predicate != NIL || index_info->ii_Expressions != NIL)
{
index_close(index_rel, AccessShareLock);
continue;
}
/* Can only use Btree indexes */
if (index_info->ii_Am != BTREE_AM_OID)
{
index_close(index_rel, AccessShareLock);
continue;
}
/*
* Must have at least two attributes, index we are looking for contains
* at least one segmentby column and a sequence number.
*/
if (index_rel->rd_index->indnatts < 2)
{
index_close(index_rel, AccessShareLock);
continue;
}
scankeys = palloc0((index_rel->rd_index->indnatts) * sizeof(ScanKeyData));
*slot_attnos = palloc0((index_rel->rd_index->indnatts) * sizeof(AttrNumber));
/*
* Using only key attributes to exclude covering columns
* only interested in filtering here
*/
for (int i = 0; i < index_rel->rd_index->indnkeyatts; i++)
{
AttrNumber idx_attnum = AttrOffsetGetAttrNumber(i);
AttrNumber in_attnum = index_rel->rd_index->indkey.values[i];
const NameData *attname = attnumAttName(in_rel, in_attnum);
AttrNumber column_attno = get_attnum(out_rel->rd_id, NameStr(*attname));
/* Make sure we find columns in key columns in order to select the right index */
if (!bms_is_member(column_attno, key_columns))
{
break;
}
bool isnull;
AttrNumber ht_attno = get_attnum(hypertable_relid, NameStr(*attname));
Datum value = slot_getattr(slot, ht_attno, &isnull);
(*slot_attnos)[*num_scan_keys] = ht_attno;
Oid atttypid = attnumTypeId(index_rel, idx_attnum);
TypeCacheEntry *tce = lookup_type_cache(atttypid, TYPECACHE_BTREE_OPFAMILY);
if (!OidIsValid(tce->btree_opf))
{
elog(ERROR, "no btree opfamily for type \"%s\"", format_type_be(atttypid));
}
Oid opr =
get_opfamily_member(tce->btree_opf, atttypid, atttypid, BTEqualStrategyNumber);
/*
* Fall back to btree operator input type when it is binary compatible with
* the column type and no operator for column type could be found.
*/
if (!OidIsValid(opr) && IsBinaryCoercible(atttypid, tce->btree_opintype))
{
opr = get_opfamily_member(tce->btree_opf,
tce->btree_opintype,
tce->btree_opintype,
BTEqualStrategyNumber);
}
/* No operator could be found so we can't create the scankey. */
if (!OidIsValid(opr))
{
continue;
}
Oid opcode = get_opcode(opr);
Ensure(OidIsValid(opcode),
"no opcode found for column operator of a hypertable column");
*index_columns = bms_add_member(*index_columns, column_attno);
ScanKeyEntryInitialize(&scankeys[(*num_scan_keys)++],
isnull ? SK_ISNULL | SK_SEARCHNULL : 0, /* flags */
idx_attnum,
BTEqualStrategyNumber,
InvalidOid, /* No strategy subtype. */
attnumCollationId(index_rel, idx_attnum),
opcode,
isnull ? 0 : value);
}
if (*num_scan_keys > 0)
{
*result_index_rel = index_rel;
break;
}
else
{
index_close(index_rel, AccessShareLock);
pfree(scankeys);
scankeys = NULL;
}
}
return scankeys;
}
/*
* This method will build scan keys for predicates including
* SEGMENT BY column with attribute number from compressed chunk
* if condition is like <segmentbycol> = <const value>, else
* OUT param null_columns is saved with column attribute number.
*/
ScanKeyData *
build_update_delete_scankeys(Relation in_rel, List *heap_filters, int *num_scankeys,
Bitmapset **null_columns, bool *delete_only)
{
ListCell *lc;
BatchFilter *filter;
int key_index = 0;
ScanKeyData *scankeys = palloc0(heap_filters->length * sizeof(ScanKeyData));
foreach (lc, heap_filters)
{
filter = lfirst(lc);
AttrNumber attno = get_attnum(in_rel->rd_id, NameStr(filter->column_name));
Oid typoid = get_atttype(in_rel->rd_id, attno);
if (attno == InvalidAttrNumber)
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" of relation \"%s\" does not exist",
NameStr(filter->column_name),
RelationGetRelationName(in_rel))));
}
bool added = create_segment_filter_scankey(in_rel,
NameStr(filter->column_name),
filter->strategy,
deduce_filter_subtype(filter, typoid),
filter->opcode,
scankeys,
&key_index,
null_columns,
filter->value ? filter->value->constvalue : 0,
filter->is_null_check,
filter->is_array_op);
/*
* When we plan to DELETE directly on compressed chunks we
* need to ensure all query constraints could be applied
* to the compressed scan and disable direct DELETE when
* we are skipping filters.
*/
if (*delete_only && !added)
{
*delete_only = false;
}
}
*num_scankeys = key_index;
return scankeys;
}
static bool
create_segment_filter_scankey(Relation in_rel, char *segment_filter_col_name,
StrategyNumber strategy, Oid subtype, Oid opcode,
ScanKeyData *scankeys, int *num_scankeys, Bitmapset **null_columns,
Datum value, bool is_null_check, bool is_array_op)
{
AttrNumber cmp_attno = get_attnum(in_rel->rd_id, segment_filter_col_name);
Assert(cmp_attno != InvalidAttrNumber);
/* This should never happen but if it does happen, we can't generate a scan key for
* the filter column so just skip it */
if (cmp_attno == InvalidAttrNumber)
{
return false;
}
int flags = is_array_op ? SK_SEARCHARRAY : 0;
/*
* In PG versions <= 14 NULL values are always considered distinct
* from other NULL values and therefore NULLABLE multi-columnn
* unique constraints might expose unexpected behaviour in the
* presence of NULL values.
* Since SK_SEARCHNULL is not supported by heap scans we cannot
* build a ScanKey for NOT NULL and instead have to do those
* checks manually.
*/
if (is_null_check)
{
*null_columns = bms_add_member(*null_columns, cmp_attno);
return false;
}
Oid opr;
/*
* All btree operators will have a valid strategy here. For
* non-btree operators e.g. <> we directly take the opcode
* here. We could do the same for btree in certain cases
* but some filters get transformed to min/max filters and
* won't keep the initial opcode so we would need to disambiguate
* between them.
*/
if (strategy == InvalidStrategy)
{
opr = opcode;
}
else
{
Oid atttypid = TupleDescAttr(in_rel->rd_att, AttrNumberGetAttrOffset(cmp_attno))->atttypid;
TypeCacheEntry *tce = lookup_type_cache(atttypid, TYPECACHE_BTREE_OPFAMILY);
if (!OidIsValid(tce->btree_opf))
{
elog(ERROR, "no btree opfamily for type \"%s\"", format_type_be(atttypid));
}
opr = get_opfamily_member(tce->btree_opf, atttypid, atttypid, strategy);
/*
* Fall back to btree operator input type when it is binary compatible with
* the column type and no operator for column type could be found.
*/
if (!OidIsValid(opr) && IsBinaryCoercible(atttypid, tce->btree_opintype))
{
opr = get_opfamily_member(tce->btree_opf,
tce->btree_opintype,
tce->btree_opintype,
strategy);
}
/* No operator could be found so we can't create the scankey. */
if (!OidIsValid(opr))
{
return false;
}
opr = get_opcode(opr);
}
/* We should never end up here but: no opcode, no optimization */
if (!OidIsValid(opr))
{
return false;
}
ScanKeyEntryInitialize(&scankeys[(*num_scankeys)++],
flags,
cmp_attno,
strategy,
subtype,
TupleDescAttr(in_rel->rd_att, AttrNumberGetAttrOffset(cmp_attno))
->attcollation,
opr,
value);
return true;
}
/*
* Get the subtype for an indexscan from the provided filter. We also
* need to handle array constants appropriately.
*/
static Oid
deduce_filter_subtype(BatchFilter *filter, Oid att_typoid)
{
Oid subtype = InvalidOid;
if (!filter->value)
{
return InvalidOid;
}
/*
* Check if the filter type is different from the att type. If yes, the
* subtype needs to be set appropriately.
*/
if (att_typoid != filter->value->consttype)
{
/* For an array type get its element type */
if (filter->is_array_op)
{
subtype = get_element_type(filter->value->consttype);
}
else
{
subtype = filter->value->consttype;
}
}
return subtype;
}