-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcompression_settings.c
More file actions
1765 lines (1564 loc) · 47.3 KB
/
Copy pathcompression_settings.c
File metadata and controls
1765 lines (1564 loc) · 47.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
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <catalog/pg_inherits.h>
#include <catalog/pg_type.h>
#include <parser/parse_func.h>
#include <utils/builtins.h>
#include <utils/lsyscache.h>
#include "foreach_ptr.h"
#include "jsonb_utils.h"
#include "scan_iterator.h"
#include "scanner.h"
#include "ts_catalog/array_utils.h"
#include "ts_catalog/catalog.h"
#include "ts_catalog/compression_settings.h"
#include <common/md5.h>
#include <utils/palloc.h>
TSDLLEXPORT const char *ts_sparse_index_type_names[] = { "bloom", "minmax", "firstlast" };
TSDLLEXPORT const char *ts_sparse_index_source_names[] = { "config", "default", "orderby" };
TSDLLEXPORT const char *ts_sparse_index_common_keys[] = { "type", "column", "source", NULL };
static ScanTupleResult compression_settings_tuple_update(TupleInfo *ti, void *data);
static HeapTuple compression_settings_formdata_make_tuple(const FormData_compression_settings *fd,
TupleDesc desc);
static Bitmapset *resolve_columns_to_attnos(List *column_names, Oid relid);
static bool sparse_index_values_equal(List *left, List *right);
static bool sparse_index_object_equal(SparseIndexSettingsObject *left,
SparseIndexSettingsObject *right);
/*
* Compare two compression settings for equality
*/
bool
ts_compression_settings_equal(const CompressionSettings *left, const CompressionSettings *right)
{
return ts_array_equal(left->fd.segmentby, right->fd.segmentby) &&
ts_array_equal(left->fd.orderby, right->fd.orderby) &&
ts_array_equal(left->fd.orderby_desc, right->fd.orderby_desc) &&
ts_array_equal(left->fd.orderby_nullsfirst, right->fd.orderby_nullsfirst) &&
ts_sparse_index_equal(left->fd.index, right->fd.index);
}
/*
* Compare two compression settings for equality while ignoring default values.
*
* This essentially means that any NULL
* values should be considered a match because they represent default
* values which are determined at chunk level.
*
* This also means first argument needs to be the hypertable because chunks
* cannot have implicit defaults.
*/
bool
ts_compression_settings_equal_with_defaults(const CompressionSettings *ht,
const CompressionSettings *chunk)
{
Assert(!OidIsValid(ht->fd.compress_relid));
return (ht->fd.segmentby == NULL || ts_array_equal(ht->fd.segmentby, chunk->fd.segmentby)) &&
(ht->fd.orderby == NULL || ts_array_equal(ht->fd.orderby, chunk->fd.orderby)) &&
(ht->fd.orderby_desc == NULL ||
ts_array_equal(ht->fd.orderby_desc, chunk->fd.orderby_desc)) &&
(ht->fd.orderby_nullsfirst == NULL ||
ts_array_equal(ht->fd.orderby_nullsfirst, chunk->fd.orderby_nullsfirst)) &&
(ht->fd.index == NULL || ts_sparse_index_equal(ht->fd.index, chunk->fd.index));
}
/*
* Compare two string value lists for equality (order-sensitive).
*/
static bool
sparse_index_values_equal(List *left, List *right)
{
if (list_length(left) != list_length(right))
{
return false;
}
ListCell *lc_left, *lc_right;
forboth (lc_left, left, lc_right, right)
{
if (strcmp((const char *) lfirst(lc_left), (const char *) lfirst(lc_right)) != 0)
{
return false;
}
}
return true;
}
/*
* Compare two sparse index objects for equality.
* Two objects are equal if they have the same pairs with the same values.
*/
static bool
sparse_index_object_equal(SparseIndexSettingsObject *left, SparseIndexSettingsObject *right)
{
if (list_length(left->pairs) != list_length(right->pairs))
{
return false;
}
foreach_ptr(SparseIndexSettingsPair, lpair, left->pairs)
{
bool found = false;
foreach_ptr(SparseIndexSettingsPair, rpair, right->pairs)
{
if (strcmp(lpair->key, rpair->key) == 0)
{
if (!sparse_index_values_equal(lpair->values, rpair->values))
{
return false;
}
found = true;
break;
}
}
if (!found)
{
return false;
}
}
return true;
}
/*
* Compare two sparse index JSONB settings for equality, independent of
* the order of objects in the array. Each object is matched by its
* key-value pairs (type, column, source).
*/
bool
ts_sparse_index_equal(const Jsonb *left, const Jsonb *right)
{
if (left == right)
{
return true;
}
if (left == NULL || right == NULL)
{
return false;
}
SparseIndexSettings *left_settings = ts_convert_to_sparse_index_settings((Jsonb *) left);
SparseIndexSettings *right_settings = ts_convert_to_sparse_index_settings((Jsonb *) right);
int n_left = list_length(left_settings->objects);
int n_right = list_length(right_settings->objects);
if (n_left != n_right)
{
ts_free_sparse_index_settings(left_settings);
ts_free_sparse_index_settings(right_settings);
return false;
}
/* for tracking */
bool *already_found = palloc0(sizeof(bool) * n_left);
bool equal = true;
foreach_ptr(SparseIndexSettingsObject, lobj, left_settings->objects)
{
int ri = 0;
bool found = false;
foreach_ptr(SparseIndexSettingsObject, robj, right_settings->objects)
{
if (!already_found[ri] && sparse_index_object_equal(lobj, robj))
{
already_found[ri] = true;
found = true;
break;
}
ri++;
}
if (!found)
{
equal = false;
break;
}
}
pfree(already_found);
ts_free_sparse_index_settings(left_settings);
ts_free_sparse_index_settings(right_settings);
return equal;
}
CompressionSettings *
ts_compression_settings_materialize(const CompressionSettings *src, Oid relid, Oid compress_relid)
{
CompressionSettings *dst = ts_compression_settings_create(relid,
compress_relid,
src->fd.segmentby,
src->fd.orderby,
src->fd.orderby_desc,
src->fd.orderby_nullsfirst,
src->fd.index);
return dst;
}
CompressionSettings *
ts_compression_settings_create(Oid relid, Oid compress_relid, ArrayType *segmentby,
ArrayType *orderby, ArrayType *orderby_desc,
ArrayType *orderby_nullsfirst, Jsonb *sparse_index)
{
Catalog *catalog = ts_catalog_get();
CatalogSecurityContext sec_ctx;
Relation rel;
FormData_compression_settings fd;
Assert(OidIsValid(relid));
/*
* The default compression settings will always have orderby settings but the user may have
* chosen to overwrite it. For both cases all 3 orderby arrays must either have the same number
* of entries or be all NULL.
*/
Assert((orderby && orderby_desc && orderby_nullsfirst) ||
(!orderby && !orderby_desc && !orderby_nullsfirst));
fd.relid = relid;
fd.compress_relid = compress_relid;
fd.segmentby = segmentby;
fd.orderby = orderby;
fd.orderby_desc = orderby_desc;
fd.orderby_nullsfirst = orderby_nullsfirst;
fd.index = sparse_index;
rel = table_open(catalog_get_table_id(catalog, COMPRESSION_SETTINGS), RowExclusiveLock);
HeapTuple new_tuple = compression_settings_formdata_make_tuple(&fd, RelationGetDescr(rel));
ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx);
ts_catalog_insert(rel, new_tuple);
ts_catalog_restore_user(&sec_ctx);
heap_freetuple(new_tuple);
table_close(rel, RowExclusiveLock);
return ts_compression_settings_get(relid);
}
static void
compression_settings_fill_from_tuple(CompressionSettings *settings, TupleInfo *ti)
{
FormData_compression_settings *fd = &settings->fd;
Datum values[Natts_compression_settings];
bool nulls[Natts_compression_settings];
bool should_free;
HeapTuple tuple = ts_scanner_fetch_heap_tuple(ti, false, &should_free);
heap_deform_tuple(tuple, ts_scanner_get_tupledesc(ti), values, nulls);
MemoryContext old = MemoryContextSwitchTo(ti->mctx);
fd->relid = DatumGetObjectId(values[AttrNumberGetAttrOffset(Anum_compression_settings_relid)]);
if (nulls[AttrNumberGetAttrOffset(Anum_compression_settings_compress_relid)])
{
fd->compress_relid = InvalidOid;
}
else
{
fd->compress_relid = DatumGetObjectId(
values[AttrNumberGetAttrOffset(Anum_compression_settings_compress_relid)]);
}
if (nulls[AttrNumberGetAttrOffset(Anum_compression_settings_segmentby)])
{
fd->segmentby = NULL;
}
else
{
fd->segmentby = DatumGetArrayTypePCopy(
values[AttrNumberGetAttrOffset(Anum_compression_settings_segmentby)]);
}
if (nulls[AttrNumberGetAttrOffset(Anum_compression_settings_orderby)])
{
fd->orderby = NULL;
}
else
{
fd->orderby = DatumGetArrayTypePCopy(
values[AttrNumberGetAttrOffset(Anum_compression_settings_orderby)]);
}
if (nulls[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_desc)])
{
fd->orderby_desc = NULL;
}
else
{
fd->orderby_desc = DatumGetArrayTypePCopy(
values[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_desc)]);
}
if (nulls[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_nullsfirst)])
{
fd->orderby_nullsfirst = NULL;
}
else
{
fd->orderby_nullsfirst = DatumGetArrayTypePCopy(
values[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_nullsfirst)]);
}
if (nulls[AttrNumberGetAttrOffset(Anum_compression_settings_index)])
{
fd->index = NULL;
}
else
{
fd->index =
DatumGetJsonbPCopy(values[AttrNumberGetAttrOffset(Anum_compression_settings_index)]);
}
MemoryContextSwitchTo(old);
if (should_free)
{
heap_freetuple(tuple);
}
}
static void
init_scan_by_relid(ScanIterator *iterator, Oid relid)
{
iterator->ctx.index =
catalog_get_index(ts_catalog_get(), COMPRESSION_SETTINGS, COMPRESSION_SETTINGS_PKEY);
ts_scan_iterator_scan_key_init(iterator,
Anum_compression_settings_pkey_relid,
BTEqualStrategyNumber,
F_OIDEQ,
ObjectIdGetDatum(relid));
}
static void
init_scan_by_compress_relid(ScanIterator *iterator, Oid compress_relid)
{
iterator->ctx.index = catalog_get_index(ts_catalog_get(),
COMPRESSION_SETTINGS,
COMPRESSION_SETTINGS_COMPRESS_RELID_IDX);
ts_scan_iterator_scan_key_init(iterator,
Anum_compression_settings_compress_relid_idx_relid,
BTEqualStrategyNumber,
F_OIDEQ,
ObjectIdGetDatum(compress_relid));
}
/*
* Get the compression settings for the relation referred to by 'relid'.
*/
TSDLLEXPORT CompressionSettings *
ts_compression_settings_get(Oid relid)
{
CompressionSettings *settings = NULL;
ScanIterator iterator =
ts_scan_iterator_create(COMPRESSION_SETTINGS, AccessShareLock, CurrentMemoryContext);
init_scan_by_relid(&iterator, relid);
ts_scanner_start_scan(&iterator.ctx);
TupleInfo *ti = ts_scanner_next(&iterator.ctx);
if (!ti)
{
return NULL;
}
settings = palloc0(sizeof(CompressionSettings));
compression_settings_fill_from_tuple(settings, ti);
ts_scan_iterator_close(&iterator);
return settings;
}
/*
* Get the compression settings for a relation given its associated compressed
* relation.
*/
TSDLLEXPORT CompressionSettings *
ts_compression_settings_get_by_compress_relid(Oid compress_relid)
{
CompressionSettings *settings = NULL;
ScanIterator iterator =
ts_scan_iterator_create(COMPRESSION_SETTINGS, AccessShareLock, CurrentMemoryContext);
init_scan_by_compress_relid(&iterator, compress_relid);
ts_scanner_start_scan(&iterator.ctx);
TupleInfo *ti = ts_scanner_next(&iterator.ctx);
if (!ti)
{
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("compression settings not found for relation \"%s\"",
get_rel_name(compress_relid))));
}
settings = palloc0(sizeof(CompressionSettings));
compression_settings_fill_from_tuple(settings, ti);
ts_scan_iterator_close(&iterator);
return settings;
}
/*
* Check whether 'relid' is the relation holding the compressed data of a
* compressed chunk.
*/
TSDLLEXPORT bool
ts_relation_is_compressed_chunk_relation(Oid compress_relid)
{
if (!OidIsValid(compress_relid))
{
return false;
}
ScanIterator iterator =
ts_scan_iterator_create(COMPRESSION_SETTINGS, AccessShareLock, CurrentMemoryContext);
init_scan_by_compress_relid(&iterator, compress_relid);
ts_scanner_start_scan(&iterator.ctx);
bool found = ts_scanner_next(&iterator.ctx) != NULL;
ts_scan_iterator_close(&iterator);
return found;
}
/*
* Get the OID of the uncompressed relation given the OID of the relation
* holding its compressed data.
*
* Returns InvalidOid if 'compress_relid' is not the compressed-data relation
* of any compressed chunk.
*/
TSDLLEXPORT Oid
ts_relation_get_uncompressed_relid(Oid compress_relid)
{
if (!OidIsValid(compress_relid))
{
return InvalidOid;
}
ScanIterator iterator =
ts_scan_iterator_create(COMPRESSION_SETTINGS, AccessShareLock, CurrentMemoryContext);
init_scan_by_compress_relid(&iterator, compress_relid);
Oid relid = InvalidOid;
ts_scanner_start_scan(&iterator.ctx);
TupleInfo *ti = ts_scanner_next(&iterator.ctx);
if (ti)
{
bool isnull;
Datum datum = slot_getattr(ti->slot, Anum_compression_settings_relid, &isnull);
if (!isnull)
{
relid = DatumGetObjectId(datum);
}
}
ts_scan_iterator_close(&iterator);
return relid;
}
/*
* Delete entries matching the non-compressed relation.
*/
TSDLLEXPORT bool
ts_compression_settings_delete(Oid relid)
{
if (!OidIsValid(relid))
{
return false;
}
int count = 0;
ScanIterator iterator =
ts_scan_iterator_create(COMPRESSION_SETTINGS, RowExclusiveLock, CurrentMemoryContext);
init_scan_by_relid(&iterator, relid);
ts_scanner_foreach(&iterator)
{
TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator);
ts_catalog_delete_tid(ti->scanrel, ts_scanner_get_tuple_tid(ti));
count++;
}
return count > 0;
}
/*
* Delete entries matching a compressed relation.
*/
TSDLLEXPORT bool
ts_compression_settings_delete_by_compress_relid(Oid relid)
{
if (!OidIsValid(relid))
{
return false;
}
int count = 0;
ScanIterator iterator =
ts_scan_iterator_create(COMPRESSION_SETTINGS, RowExclusiveLock, CurrentMemoryContext);
init_scan_by_compress_relid(&iterator, relid);
ts_scanner_foreach(&iterator)
{
TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator);
ts_catalog_delete_tid(ti->scanrel, ts_scanner_get_tuple_tid(ti));
count++;
}
return count > 0;
}
/*
* Delete entries matching either the primary key (non-compressed relation) or
* the secondary key (compressed relation).
*/
TSDLLEXPORT bool
ts_compression_settings_delete_any(Oid relid)
{
if (!ts_compression_settings_delete(relid))
{
return ts_compression_settings_delete_by_compress_relid(relid);
}
return true;
}
static void
compression_settings_rename_column(CompressionSettings *settings, const char *old, const char *new)
{
Jsonb *replacejsonb = NULL;
bool replaced = false;
settings->fd.segmentby = ts_array_replace_text(settings->fd.segmentby, old, new);
settings->fd.orderby = ts_array_replace_text(settings->fd.orderby, old, new);
replacejsonb = settings->fd.index;
if (replacejsonb)
{
SparseIndexSettings *parsed_settings = ts_convert_to_sparse_index_settings(replacejsonb);
if (parsed_settings)
{
foreach_ptr(SparseIndexSettingsObject, obj, parsed_settings->objects)
{
Assert(obj != NULL);
if (!obj)
{
continue;
}
foreach_ptr(SparseIndexSettingsPair, pair, obj->pairs)
{
Assert(pair != NULL);
if (!pair)
{
continue;
}
ListCell *value_cell = NULL;
foreach (value_cell, pair->values)
{
const char *value = (const char *) lfirst(value_cell);
Assert(value != NULL);
if (!value)
{
continue;
}
if (strcmp(value, old) == 0)
{
value_cell->ptr_value =
ts_sparse_index_settings_pstrdup(parsed_settings, new);
replaced = true;
}
}
}
}
if (replaced)
{
replacejsonb = ts_convert_from_sparse_index_settings(parsed_settings);
}
ts_free_sparse_index_settings(parsed_settings);
}
}
settings->fd.index = replaced ? replacejsonb : settings->fd.index;
ts_compression_settings_update(settings);
}
TSDLLEXPORT void
ts_compression_settings_rename_column_cascade(Oid parent_relid, const char *old, const char *new)
{
CompressionSettings *settings = ts_compression_settings_get(parent_relid);
if (settings)
{
compression_settings_rename_column(settings, old, new);
}
List *children = find_inheritance_children(parent_relid, NoLock);
ListCell *lc;
foreach (lc, children)
{
Oid relid = lfirst_oid(lc);
settings = ts_compression_settings_get(relid);
if (settings)
{
compression_settings_rename_column(settings, old, new);
}
}
}
TSDLLEXPORT int
ts_compression_settings_update(CompressionSettings *settings)
{
Catalog *catalog = ts_catalog_get();
FormData_compression_settings *fd = &settings->fd;
ScanKeyData scankey[1];
if (settings->fd.orderby && (settings->fd.segmentby || settings->fd.index))
{
Datum datum;
bool isnull;
ArrayIterator it = array_create_iterator(settings->fd.orderby, 0, NULL);
while (array_iterate(it, &datum, &isnull))
{
if (settings->fd.segmentby &&
ts_array_is_member(settings->fd.segmentby, TextDatumGetCString(datum)))
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot use column \"%s\" for both ordering and segmenting",
TextDatumGetCString(datum)),
errhint("Use separate columns for the timescaledb.compress_orderby and"
" timescaledb.compress_segmentby options.")));
}
if (settings->fd.index &&
ts_contains_sparse_index_config(settings,
TextDatumGetCString(datum),
ts_sparse_index_type_names
[_SparseIndexTypeEnumBloom],
/* skip_column_arrays = */ true))
{
/* disallow single column bloom index on orderby columns, composite bloom is
* allowed, that is why we set 'skip_column_arrays' to true */
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("the orderby column \"%s\" cannot have a bloom sparse index ",
TextDatumGetCString(datum)),
errdetail("For orderby columns, a minmax sparse index is added "
"automatically and cannot have bloom sparse index.")));
}
}
}
if (settings->fd.index && settings->fd.segmentby)
{
Datum datum;
bool isnull;
ArrayIterator it = array_create_iterator(settings->fd.segmentby, 0, NULL);
while (array_iterate(it, &datum, &isnull))
{
for (int i = 0; i < _SparseIndexTypeEnumMax; i++)
{
if (ts_contains_sparse_index_config(settings,
TextDatumGetCString(datum),
ts_sparse_index_type_names[i],
/* skip_column_arrays = */ false))
{
/* segmentby columns cannot have sparse indexes of any type, including composite
* bloom that is why we set 'skip_column_arrays' to false, which will look
* inside column name arrays, so composite bloom filters are checked too */
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("the segmentby column \"%s\" can not have sparse "
"indexes",
TextDatumGetCString(datum))));
}
}
}
}
/*
* The default compression settings will always have orderby settings but the user may have
* chosen to overwrite it. For both cases all 3 orderby arrays must either have the same number
* of entries or be all NULL.
*/
Assert(
(settings->fd.orderby && settings->fd.orderby_desc && settings->fd.orderby_nullsfirst) ||
(!settings->fd.orderby && !settings->fd.orderby_desc && !settings->fd.orderby_nullsfirst));
ScanKeyInit(&scankey[0],
Anum_compression_settings_pkey_relid,
BTEqualStrategyNumber,
F_OIDEQ,
ObjectIdGetDatum(fd->relid));
ScannerCtx scanctx = {
.table = catalog_get_table_id(catalog, COMPRESSION_SETTINGS),
.index = catalog_get_index(catalog, COMPRESSION_SETTINGS, COMPRESSION_SETTINGS_PKEY),
.nkeys = 1,
.scankey = scankey,
.data = settings,
.tuple_found = compression_settings_tuple_update,
.lockmode = RowExclusiveLock,
.scandirection = ForwardScanDirection,
};
return ts_scanner_scan(&scanctx);
}
static HeapTuple
compression_settings_formdata_make_tuple(const FormData_compression_settings *fd, TupleDesc desc)
{
Datum values[Natts_compression_settings] = { 0 };
bool nulls[Natts_compression_settings] = { false };
values[AttrNumberGetAttrOffset(Anum_compression_settings_relid)] = ObjectIdGetDatum(fd->relid);
if (OidIsValid(fd->compress_relid))
{
values[AttrNumberGetAttrOffset(Anum_compression_settings_compress_relid)] =
ObjectIdGetDatum(fd->compress_relid);
}
else
{
nulls[AttrNumberGetAttrOffset(Anum_compression_settings_compress_relid)] = true;
}
if (fd->segmentby)
{
values[AttrNumberGetAttrOffset(Anum_compression_settings_segmentby)] =
PointerGetDatum(fd->segmentby);
}
else
{
nulls[AttrNumberGetAttrOffset(Anum_compression_settings_segmentby)] = true;
}
if (fd->orderby)
{
values[AttrNumberGetAttrOffset(Anum_compression_settings_orderby)] =
PointerGetDatum(fd->orderby);
}
else
{
nulls[AttrNumberGetAttrOffset(Anum_compression_settings_orderby)] = true;
}
if (fd->orderby_desc)
{
values[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_desc)] =
PointerGetDatum(fd->orderby_desc);
}
else
{
nulls[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_desc)] = true;
}
if (fd->orderby_nullsfirst)
{
values[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_nullsfirst)] =
PointerGetDatum(fd->orderby_nullsfirst);
}
else
{
nulls[AttrNumberGetAttrOffset(Anum_compression_settings_orderby_nullsfirst)] = true;
}
if (fd->index)
{
values[AttrNumberGetAttrOffset(Anum_compression_settings_index)] =
JsonbPGetDatum(fd->index);
}
else
{
nulls[AttrNumberGetAttrOffset(Anum_compression_settings_index)] = true;
}
return heap_form_tuple(desc, values, nulls);
}
static ScanTupleResult
compression_settings_tuple_update(TupleInfo *ti, void *data)
{
CompressionSettings *settings = data;
HeapTuple new_tuple;
CatalogSecurityContext sec_ctx;
new_tuple =
compression_settings_formdata_make_tuple(&settings->fd, ts_scanner_get_tupledesc(ti));
ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx);
ts_catalog_update_tid(ti->scanrel, ts_scanner_get_tuple_tid(ti), new_tuple);
ts_catalog_restore_user(&sec_ctx);
heap_freetuple(new_tuple);
return SCAN_DONE;
}
void
ts_convert_sparse_index_config_to_jsonb(JsonbParseState *parse_state, SparseIndexConfigBase *config)
{
MinmaxIndexColumnConfig *minmax_config = NULL;
BloomFilterConfig *bloom_config = NULL;
pushJsonbValue(&parse_state, WJB_BEGIN_OBJECT, NULL);
ts_jsonb_add_str(parse_state,
ts_sparse_index_common_keys[SparseIndexKeyType],
ts_sparse_index_type_names[config->type]); /* type */
switch (config->type)
{
case _SparseIndexTypeEnumMinmax:
minmax_config = (MinmaxIndexColumnConfig *) config;
ts_jsonb_add_str(parse_state,
ts_sparse_index_common_keys[SparseIndexKeyCol],
minmax_config->col); /* column */
break;
case _SparseIndexTypeEnumFirstLast:
{
FirstLastIndexColumnConfig *firstlast_config = (FirstLastIndexColumnConfig *) config;
ts_jsonb_add_str(parse_state,
ts_sparse_index_common_keys[SparseIndexKeyCol],
firstlast_config->col); /* column */
break;
}
case _SparseIndexTypeEnumBloom:
bloom_config = (BloomFilterConfig *) config;
if (bloom_config->num_columns > 1)
{
/* add the column names as an array */
const char *column_names[MAX_BLOOM_FILTER_COLUMNS] = { 0 };
for (int i = 0; i < bloom_config->num_columns; i++)
{
column_names[i] = bloom_config->columns[i].name;
}
ts_jsonb_add_str_array(parse_state,
ts_sparse_index_common_keys[SparseIndexKeyCol],
column_names,
bloom_config->num_columns);
}
else
{
ts_jsonb_add_str(parse_state,
ts_sparse_index_common_keys[SparseIndexKeyCol],
bloom_config->columns[0].name); /* column */
}
break;
default:
elog(ERROR, "invalid sparse index type: %d", config->type);
};
ts_jsonb_add_str(parse_state,
ts_sparse_index_common_keys[SparseIndexKeySource],
ts_sparse_index_source_names[config->source]); /* source */
pushJsonbValue(&parse_state, WJB_END_OBJECT, NULL);
}
bool
ts_contains_sparse_index_config(CompressionSettings *settings, const char *attname,
const char *sparse_index_type, bool skip_column_arrays)
{
bool result = false;
if (settings == NULL || settings->fd.index == NULL || attname == NULL)
{
return false;
}
SparseIndexSettings *parsed = ts_convert_to_sparse_index_settings(settings->fd.index);
if (parsed == NULL)
{
return false;
}
foreach_ptr(SparseIndexSettingsObject, obj, parsed->objects)
{
const char *index_type = NULL;
bool attname_found = false;
foreach_ptr(SparseIndexSettingsPair, pair, obj->pairs)
{
if (strcmp(pair->key, ts_sparse_index_common_keys[SparseIndexKeyCol]) == 0)
{
if (skip_column_arrays && list_length(pair->values) > 1)
{
continue;
}
foreach_ptr(const char, value, pair->values)
{
if (strcmp(value, attname) == 0)
{
attname_found = true;
break;
}
}
}
if (strcmp(pair->key, ts_sparse_index_common_keys[SparseIndexKeyType]) == 0)
{
index_type = (const char *) lfirst(list_head(pair->values));
if (strcmp(index_type, sparse_index_type) != 0)
{
break;
}
else if (attname_found)
{
result = true;
break;
}
}
if (attname_found && index_type != NULL)
{
result = strcmp(index_type, sparse_index_type) == 0;
break;
}
}
}
ts_free_sparse_index_settings(parsed);
return result;
}
bool
ts_column_has_sparse_index(CompressionSettings *settings, const char *attname)
{
if (!settings->fd.index)
{
return false;
}
for (int i = 0; i < _SparseIndexTypeEnumMax; i++)
{
if (ts_contains_sparse_index_config(settings,
attname,
ts_sparse_index_type_names[i],
false))
{
return true;
}
}
return false;
}
bool
ts_accept_for_segmentby(CompressionSettings *settings, Form_pg_attribute attr)
{
if (attr->attisdropped || attr->attgenerated)
{
return false;
}
const char *attname = NameStr(attr->attname);
if (ts_array_is_member(settings->fd.orderby, attname))
{
return false;
}
if (ts_column_has_sparse_index(settings, attname))
{
return false;
}
switch (attr->atttypid)
{
case INT2OID:
case INT4OID:
case INT8OID:
case OIDOID:
case NAMEOID:
case REGTYPEOID:
case REGCLASSOID:
case TEXTOID:
case VARCHAROID:
case BPCHAROID:
return true;
default:
break;
}
if (get_typtype(attr->atttypid) == TYPTYPE_ENUM)
{
return true;
}
return false;
}
/* Sparse indexes are only set by default when no user configuration exists */
bool
ts_can_set_default_sparse_index(CompressionSettings *settings)
{
return (settings->fd.index == NULL) ||
!ts_jsonb_has_key_value_str_field(settings->fd.index,