-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathimcs.c
5821 lines (5526 loc) · 209 KB
/
imcs.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
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
#include <unistd.h>
#include <float.h>
#include <math.h>
#include <limits.h>
#include "imcs.h"
#include "btree.h"
#include "disk.h"
#include "func.h"
#include "smp.h"
#include "executor/spi.h"
#include "commands/trigger.h"
#include "utils/timestamp.h"
#include "utils/rel.h"
#include "utils/date.h"
#include "utils/datetime.h"
#if PG_VERSION_NUM<120000
#include "utils/nabstime.h"
#endif
#if PG_VERSION_NUM>=120000
#include "utils/float.h"
#endif
#include "utils/syscache.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_language.h"
#include "tsearch/ts_locale.h"
#if PG_VERSION_NUM>=90300
#include "access/htup_details.h"
#endif
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#define IMCS_TRACE(cmd) if (imcs_trace) elog(NOTICE, "IMCS command: %s", imcs_command_mnem[imcs_cmd_##cmd]); imcs_command_profile[imcs_cmd_##cmd] += 1
#define MAX_NUMELEM_LEN 32
#define OUTPUT_BUF_RESERVE 8
typedef struct imcs_free_page_t
{
struct imcs_free_page_t* next;
} imcs_free_page_t;
typedef struct imcs_state_t
{
LWLockId lock; /* protects timeseries search/modification */
imcs_free_page_t* free_pages; /* list of free B-Tree pages */
size_t n_used_pages;
imcs_disk_cache_t disk_cache;
} imcs_state_t;
typedef enum
{
LOCK_NONE,
LOCK_SHARED,
LOCK_EXCLUSIVE
} imcs_lock_t;
static imcs_state_t* imcs;
static HTAB* imcs_hash;
static HTAB* imcs_dict;
static imcs_thread_pool_t* imcs_thread_pool;
static imcs_lock_t imcs_lock = LOCK_NONE;
static imcs_mutex_t* imcs_alloc_mutex;
static MemoryContext imcs_mem_ctx;
static imcs_tls_t* imcs_tls;
static imcs_error_handler_t* imcs_error_handlers;
static Datum imcs_project_result_cache;
static size_t imcs_project_redundant_calls;
static size_t imcs_project_call_count;
static bool imcs_project_caching = true;
static bool imcs_substitute_nulls = false;
static bool imcs_autoload = true;
static bool imcs_serializable = true;
static bool imcs_trace = false;
int imcs_cache_size = 0;
char* imcs_file_path;
int imcs_page_size = 4096;
int imcs_tile_size = 128;
int imcs_dict_size = IMCS_SMALL_DICTIONARY;
bool imcs_use_rle = false;
static int imcs_output_string_limit = 1024;
static bool imcs_flush_file;
static int shmem_size = 1024;
static int n_timeseries = 10000;
static int n_threads = 0;
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
#if PG_VERSION_NUM>=150000
static shmem_request_hook_type prev_shmem_request_hook = NULL;
#endif
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
static int imcs_command_profile[imcs_cmd_last_command];
const Oid imcs_elem_type_to_oid[] = {CHAROID, INT2OID, INT4OID, DATEOID, INT8OID, TIMEOID, TIMESTAMPOID, CASHOID, FLOAT4OID, FLOAT8OID, BPCHAROID};
const char* const imcs_type_mnems[] = {"char", "int2", "int4", "date", "int8", "time", "timestamp", "money", "float4", "float8", "bpchar"};
static const int imcs_type_mnem_lens[] = {4, 4, 4, 4, 4, 4, 8, 5, 6, 6, 6};
static const int imcs_type_sizeof[] = {1,2,4,4,8,8,8,8,4,8,0};
static const char* const imcs_command_mnem[] =
{
"parse",
"const",
"cast",
"add",
"mul",
"sub",
"div",
"mod",
"pow",
"and",
"or",
"xor",
"concat",
"cat",
"eq",
"ne",
"ge",
"le",
"lt",
"gt",
"maxof",
"minof",
"neg",
"not",
"bit_not",
"abs",
"limit",
"sin",
"cos",
"tan",
"exp",
"asin",
"acos",
"atan",
"sqrt",
"log",
"ceil",
"floor",
"isnan",
"wsum",
"wavg",
"corr",
"cov",
"norm",
"thin",
"iif",
"if",
"filter",
"filter_pos",
"filter_first_pos",
"unique",
"reverse",
"diff",
"trend",
"repeat",
"count",
"approxdc",
"max",
"min",
"avg",
"sum",
"prd",
"var",
"dev",
"all",
"any",
"median",
"group_count",
"group_approxdc",
"group_max",
"group_min",
"group_avg",
"group_sum",
"group_var",
"group_dev",
"group_all",
"group_any",
"group_last",
"group_first",
"win_group_max",
"win_group_min",
"win_group_avg",
"win_group_sum",
"win_group_var",
"win_group_dev",
"win_group_all",
"win_group_any",
"win_group_last",
"win_group_first",
"grid_max",
"grid_min",
"grid_avg",
"grid_sum",
"grid_var",
"grid_dev",
"window_max",
"window_min",
"window_avg",
"window_sum",
"window_var",
"window_dev",
"window_ema",
"window_atr",
"hash_count",
"hash_dup_count",
"hash_max",
"hash_min",
"hash_avg",
"hash_sum",
"hash_all",
"hash_any",
"top_max",
"top_min",
"top_max_pos",
"top_min_pos",
"cum_max",
"cum_min",
"cum_avg",
"cum_sum",
"cum_prd",
"cum_var",
"cum_dev",
"histogram",
"cross",
"extrema",
"stretch",
"stretch0",
"asof_join",
"asof_join_pos",
"join",
"join_pos",
"map",
"union",
"empty",
"project",
"project_agg",
"year",
"month",
"mday",
"wday",
"hour",
"minute",
"second",
"week",
"quarter",
"call",
"to_array",
"from_array",
"like",
"ilike",
"sort",
"sort_pos",
"rank",
"dense_rank",
"quantile"
};
#define MB (1024*1024)
#define MAX_SQL_STMT_LEN 256
#define MAX_CUT_VALUES 16
#define IMCS_INIT_OUTPUT_BUF_SIZE (16*1024)
#define IMCS_MIN_OUTPUT_BUF_SIZE (MAX_NUMELEM_LEN)
typedef struct {
char* id;
Oid db;
} imcs_hash_key_t;
typedef struct {
imcs_hash_key_t key;
imcs_timeseries_t value;
} imcs_hash_entry_t;
imcs_dict_entry_t** imcs_dict_code_map;
/*---- Function declarations ----*/
void _PG_init(void);
void _PG_fini(void);
PG_FUNCTION_INFO_V1(columnar_store_initialized);
PG_FUNCTION_INFO_V1(columnar_store_lock);
PG_FUNCTION_INFO_V1(columnar_store_get);
PG_FUNCTION_INFO_V1(columnar_store_span);
PG_FUNCTION_INFO_V1(columnar_store_load);
PG_FUNCTION_INFO_V1(columnar_store_load_column);
PG_FUNCTION_INFO_V1(columnar_store_truncate_column);
PG_FUNCTION_INFO_V1(columnar_store_delete);
PG_FUNCTION_INFO_V1(columnar_store_truncate);
PG_FUNCTION_INFO_V1(columnar_store_insert_trigger);
PG_FUNCTION_INFO_V1(columnar_store_search_int8);
PG_FUNCTION_INFO_V1(columnar_store_search_int16);
PG_FUNCTION_INFO_V1(columnar_store_search_int32);
PG_FUNCTION_INFO_V1(columnar_store_search_int64);
PG_FUNCTION_INFO_V1(columnar_store_search_float);
PG_FUNCTION_INFO_V1(columnar_store_search_double);
PG_FUNCTION_INFO_V1(columnar_store_append_int8);
PG_FUNCTION_INFO_V1(columnar_store_append_int16);
PG_FUNCTION_INFO_V1(columnar_store_append_int32);
PG_FUNCTION_INFO_V1(columnar_store_append_int64);
PG_FUNCTION_INFO_V1(columnar_store_append_float);
PG_FUNCTION_INFO_V1(columnar_store_append_double);
PG_FUNCTION_INFO_V1(columnar_store_append_char);
PG_FUNCTION_INFO_V1(columnar_store_count);
PG_FUNCTION_INFO_V1(columnar_store_first_int8);
PG_FUNCTION_INFO_V1(columnar_store_first_int16);
PG_FUNCTION_INFO_V1(columnar_store_first_int32);
PG_FUNCTION_INFO_V1(columnar_store_first_int64);
PG_FUNCTION_INFO_V1(columnar_store_first_float);
PG_FUNCTION_INFO_V1(columnar_store_first_double);
PG_FUNCTION_INFO_V1(columnar_store_last_int8);
PG_FUNCTION_INFO_V1(columnar_store_last_int16);
PG_FUNCTION_INFO_V1(columnar_store_last_int32);
PG_FUNCTION_INFO_V1(columnar_store_last_int64);
PG_FUNCTION_INFO_V1(columnar_store_last_float);
PG_FUNCTION_INFO_V1(columnar_store_last_double);
PG_FUNCTION_INFO_V1(columnar_store_join_int8);
PG_FUNCTION_INFO_V1(columnar_store_join_int16);
PG_FUNCTION_INFO_V1(columnar_store_join_int32);
PG_FUNCTION_INFO_V1(columnar_store_join_int64);
PG_FUNCTION_INFO_V1(columnar_store_join_float);
PG_FUNCTION_INFO_V1(columnar_store_join_double);
PG_FUNCTION_INFO_V1(cs_used_memory);
PG_FUNCTION_INFO_V1(cs_delete_all);
PG_FUNCTION_INFO_V1(cs_parse_tid);
PG_FUNCTION_INFO_V1(cs_const_num);
PG_FUNCTION_INFO_V1(cs_const_dt);
PG_FUNCTION_INFO_V1(cs_const_str);
PG_FUNCTION_INFO_V1(cs_cast_tid);
PG_FUNCTION_INFO_V1(cs_type);
PG_FUNCTION_INFO_V1(cs_elem_size);
PG_FUNCTION_INFO_V1(cs_input_function);
PG_FUNCTION_INFO_V1(cs_output_function);
PG_FUNCTION_INFO_V1(cs_receive_function);
PG_FUNCTION_INFO_V1(cs_send_function);
PG_FUNCTION_INFO_V1(cs_add);
PG_FUNCTION_INFO_V1(cs_mul);
PG_FUNCTION_INFO_V1(cs_sub);
PG_FUNCTION_INFO_V1(cs_div);
PG_FUNCTION_INFO_V1(cs_mod);
PG_FUNCTION_INFO_V1(cs_pow);
PG_FUNCTION_INFO_V1(cs_and);
PG_FUNCTION_INFO_V1(cs_or);
PG_FUNCTION_INFO_V1(cs_xor);
PG_FUNCTION_INFO_V1(cs_concat);
PG_FUNCTION_INFO_V1(cs_cat);
PG_FUNCTION_INFO_V1(cs_cut);
PG_FUNCTION_INFO_V1(cs_as);
PG_FUNCTION_INFO_V1(cs_as_array);
PG_FUNCTION_INFO_V1(cs_eq);
PG_FUNCTION_INFO_V1(cs_ne);
PG_FUNCTION_INFO_V1(cs_ge);
PG_FUNCTION_INFO_V1(cs_le);
PG_FUNCTION_INFO_V1(cs_lt);
PG_FUNCTION_INFO_V1(cs_gt);
PG_FUNCTION_INFO_V1(cs_like);
PG_FUNCTION_INFO_V1(cs_ilike);
PG_FUNCTION_INFO_V1(cs_maxof);
PG_FUNCTION_INFO_V1(cs_minof);
PG_FUNCTION_INFO_V1(cs_neg);
PG_FUNCTION_INFO_V1(cs_not);
PG_FUNCTION_INFO_V1(cs_bit_not);
PG_FUNCTION_INFO_V1(cs_abs);
PG_FUNCTION_INFO_V1(cs_limit);
PG_FUNCTION_INFO_V1(cs_sin);
PG_FUNCTION_INFO_V1(cs_cos);
PG_FUNCTION_INFO_V1(cs_tan);
PG_FUNCTION_INFO_V1(cs_exp);
PG_FUNCTION_INFO_V1(cs_asin);
PG_FUNCTION_INFO_V1(cs_acos);
PG_FUNCTION_INFO_V1(cs_atan);
PG_FUNCTION_INFO_V1(cs_sqrt);
PG_FUNCTION_INFO_V1(cs_log);
PG_FUNCTION_INFO_V1(cs_ceil);
PG_FUNCTION_INFO_V1(cs_floor);
PG_FUNCTION_INFO_V1(cs_isnan);
PG_FUNCTION_INFO_V1(cs_wsum);
PG_FUNCTION_INFO_V1(cs_wavg);
PG_FUNCTION_INFO_V1(cs_corr);
PG_FUNCTION_INFO_V1(cs_cov);
PG_FUNCTION_INFO_V1(cs_norm);
PG_FUNCTION_INFO_V1(cs_thin);
PG_FUNCTION_INFO_V1(cs_iif);
PG_FUNCTION_INFO_V1(cs_if);
PG_FUNCTION_INFO_V1(cs_filter);
PG_FUNCTION_INFO_V1(cs_filter_pos);
PG_FUNCTION_INFO_V1(cs_filter_first_pos);
PG_FUNCTION_INFO_V1(cs_unique);
PG_FUNCTION_INFO_V1(cs_reverse);
PG_FUNCTION_INFO_V1(cs_diff);
PG_FUNCTION_INFO_V1(cs_trend);
PG_FUNCTION_INFO_V1(cs_repeat);
PG_FUNCTION_INFO_V1(cs_count);
PG_FUNCTION_INFO_V1(cs_approxdc);
PG_FUNCTION_INFO_V1(cs_max);
PG_FUNCTION_INFO_V1(cs_min);
PG_FUNCTION_INFO_V1(cs_avg);
PG_FUNCTION_INFO_V1(cs_sum);
PG_FUNCTION_INFO_V1(cs_any);
PG_FUNCTION_INFO_V1(cs_all);
PG_FUNCTION_INFO_V1(cs_prd);
PG_FUNCTION_INFO_V1(cs_var);
PG_FUNCTION_INFO_V1(cs_dev);
PG_FUNCTION_INFO_V1(cs_median);
PG_FUNCTION_INFO_V1(cs_group_count);
PG_FUNCTION_INFO_V1(cs_group_approxdc);
PG_FUNCTION_INFO_V1(cs_group_max);
PG_FUNCTION_INFO_V1(cs_group_min);
PG_FUNCTION_INFO_V1(cs_group_avg);
PG_FUNCTION_INFO_V1(cs_group_sum);
PG_FUNCTION_INFO_V1(cs_group_var);
PG_FUNCTION_INFO_V1(cs_group_dev);
PG_FUNCTION_INFO_V1(cs_group_any);
PG_FUNCTION_INFO_V1(cs_group_all);
PG_FUNCTION_INFO_V1(cs_group_last);
PG_FUNCTION_INFO_V1(cs_group_first);
PG_FUNCTION_INFO_V1(cs_win_group_max);
PG_FUNCTION_INFO_V1(cs_win_group_min);
PG_FUNCTION_INFO_V1(cs_win_group_avg);
PG_FUNCTION_INFO_V1(cs_win_group_sum);
PG_FUNCTION_INFO_V1(cs_win_group_var);
PG_FUNCTION_INFO_V1(cs_win_group_dev);
PG_FUNCTION_INFO_V1(cs_win_group_any);
PG_FUNCTION_INFO_V1(cs_win_group_all);
PG_FUNCTION_INFO_V1(cs_win_group_last);
PG_FUNCTION_INFO_V1(cs_win_group_first);
PG_FUNCTION_INFO_V1(cs_grid_max);
PG_FUNCTION_INFO_V1(cs_grid_min);
PG_FUNCTION_INFO_V1(cs_grid_avg);
PG_FUNCTION_INFO_V1(cs_grid_sum);
PG_FUNCTION_INFO_V1(cs_grid_var);
PG_FUNCTION_INFO_V1(cs_grid_dev);
PG_FUNCTION_INFO_V1(cs_window_max);
PG_FUNCTION_INFO_V1(cs_window_min);
PG_FUNCTION_INFO_V1(cs_window_avg);
PG_FUNCTION_INFO_V1(cs_window_sum);
PG_FUNCTION_INFO_V1(cs_window_var);
PG_FUNCTION_INFO_V1(cs_window_dev);
PG_FUNCTION_INFO_V1(cs_window_ema);
PG_FUNCTION_INFO_V1(cs_window_atr);
PG_FUNCTION_INFO_V1(cs_hash_count);
PG_FUNCTION_INFO_V1(cs_hash_dup_count);
PG_FUNCTION_INFO_V1(cs_hash_max);
PG_FUNCTION_INFO_V1(cs_hash_min);
PG_FUNCTION_INFO_V1(cs_hash_avg);
PG_FUNCTION_INFO_V1(cs_hash_sum);
PG_FUNCTION_INFO_V1(cs_hash_any);
PG_FUNCTION_INFO_V1(cs_hash_all);
PG_FUNCTION_INFO_V1(cs_top_max);
PG_FUNCTION_INFO_V1(cs_top_min);
PG_FUNCTION_INFO_V1(cs_top_max_pos);
PG_FUNCTION_INFO_V1(cs_top_min_pos);
PG_FUNCTION_INFO_V1(cs_cum_max);
PG_FUNCTION_INFO_V1(cs_cum_min);
PG_FUNCTION_INFO_V1(cs_cum_avg);
PG_FUNCTION_INFO_V1(cs_cum_sum);
PG_FUNCTION_INFO_V1(cs_cum_prd);
PG_FUNCTION_INFO_V1(cs_cum_var);
PG_FUNCTION_INFO_V1(cs_cum_dev);
PG_FUNCTION_INFO_V1(cs_histogram);
PG_FUNCTION_INFO_V1(cs_cross);
PG_FUNCTION_INFO_V1(cs_extrema);
PG_FUNCTION_INFO_V1(cs_stretch);
PG_FUNCTION_INFO_V1(cs_stretch0);
PG_FUNCTION_INFO_V1(cs_asof_join);
PG_FUNCTION_INFO_V1(cs_asof_join_pos);
PG_FUNCTION_INFO_V1(cs_join);
PG_FUNCTION_INFO_V1(cs_join_pos);
PG_FUNCTION_INFO_V1(cs_map);
PG_FUNCTION_INFO_V1(cs_union);
PG_FUNCTION_INFO_V1(cs_empty);
PG_FUNCTION_INFO_V1(cs_project);
PG_FUNCTION_INFO_V1(cs_project_agg);
PG_FUNCTION_INFO_V1(cs_year);
PG_FUNCTION_INFO_V1(cs_month);
PG_FUNCTION_INFO_V1(cs_mday);
PG_FUNCTION_INFO_V1(cs_wday);
PG_FUNCTION_INFO_V1(cs_hour);
PG_FUNCTION_INFO_V1(cs_minute);
PG_FUNCTION_INFO_V1(cs_second);
PG_FUNCTION_INFO_V1(cs_week);
PG_FUNCTION_INFO_V1(cs_quarter);
PG_FUNCTION_INFO_V1(cs_call);
PG_FUNCTION_INFO_V1(cs_to_array);
PG_FUNCTION_INFO_V1(cs_from_array);
PG_FUNCTION_INFO_V1(cs_profile);
PG_FUNCTION_INFO_V1(cs_sort);
PG_FUNCTION_INFO_V1(cs_sort_pos);
PG_FUNCTION_INFO_V1(cs_rank);
PG_FUNCTION_INFO_V1(cs_dense_rank);
PG_FUNCTION_INFO_V1(cs_quantile);
PG_FUNCTION_INFO_V1(cs_str2code);
PG_FUNCTION_INFO_V1(cs_code2str);
PG_FUNCTION_INFO_V1(cs_cut_and_code2str);
PG_FUNCTION_INFO_V1(cs_dictionary_size);
Datum columnar_store_initialized(PG_FUNCTION_ARGS);
Datum columnar_store_get(PG_FUNCTION_ARGS);
Datum columnar_store_lock(PG_FUNCTION_ARGS);
Datum columnar_store_span(PG_FUNCTION_ARGS);
Datum columnar_store_load(PG_FUNCTION_ARGS);
Datum columnar_store_load_column(PG_FUNCTION_ARGS);
Datum columnar_store_truncate_column(PG_FUNCTION_ARGS);
Datum columnar_store_delete(PG_FUNCTION_ARGS);
Datum columnar_store_truncate(PG_FUNCTION_ARGS);
Datum columnar_store_insert_trigger(PG_FUNCTION_ARGS);
Datum columnar_store_search_int8(PG_FUNCTION_ARGS);
Datum columnar_store_search_int16(PG_FUNCTION_ARGS);
Datum columnar_store_search_int32(PG_FUNCTION_ARGS);
Datum columnar_store_search_int64(PG_FUNCTION_ARGS);
Datum columnar_store_search_float(PG_FUNCTION_ARGS);
Datum columnar_store_search_double(PG_FUNCTION_ARGS);
Datum columnar_store_append_int8(PG_FUNCTION_ARGS);
Datum columnar_store_append_int16(PG_FUNCTION_ARGS);
Datum columnar_store_append_int32(PG_FUNCTION_ARGS);
Datum columnar_store_append_int64(PG_FUNCTION_ARGS);
Datum columnar_store_append_float(PG_FUNCTION_ARGS);
Datum columnar_store_append_double(PG_FUNCTION_ARGS);
Datum columnar_store_append_char(PG_FUNCTION_ARGS);
Datum columnar_store_count(PG_FUNCTION_ARGS);
Datum columnar_store_first_int8(PG_FUNCTION_ARGS);
Datum columnar_store_first_int16(PG_FUNCTION_ARGS);
Datum columnar_store_first_int32(PG_FUNCTION_ARGS);
Datum columnar_store_first_int64(PG_FUNCTION_ARGS);
Datum columnar_store_first_float(PG_FUNCTION_ARGS);
Datum columnar_store_first_double(PG_FUNCTION_ARGS);
Datum columnar_store_last_int8(PG_FUNCTION_ARGS);
Datum columnar_store_last_int16(PG_FUNCTION_ARGS);
Datum columnar_store_last_int32(PG_FUNCTION_ARGS);
Datum columnar_store_last_int64(PG_FUNCTION_ARGS);
Datum columnar_store_last_float(PG_FUNCTION_ARGS);
Datum columnar_store_last_double(PG_FUNCTION_ARGS);
Datum columnar_store_join_int8(PG_FUNCTION_ARGS);
Datum columnar_store_join_int16(PG_FUNCTION_ARGS);
Datum columnar_store_join_int32(PG_FUNCTION_ARGS);
Datum columnar_store_join_int64(PG_FUNCTION_ARGS);
Datum columnar_store_join_float(PG_FUNCTION_ARGS);
Datum columnar_store_join_double(PG_FUNCTION_ARGS);
Datum cs_used_memory(PG_FUNCTION_ARGS);
Datum cs_delete_all(PG_FUNCTION_ARGS);
Datum cs_parse_tid(PG_FUNCTION_ARGS);
Datum cs_const_num(PG_FUNCTION_ARGS);
Datum cs_const_dt(PG_FUNCTION_ARGS);
Datum cs_const_str(PG_FUNCTION_ARGS);
Datum cs_cast_tid(PG_FUNCTION_ARGS);
Datum cs_type(PG_FUNCTION_ARGS);
Datum cs_elem_size(PG_FUNCTION_ARGS);
Datum cs_input_function(PG_FUNCTION_ARGS);
Datum cs_output_function(PG_FUNCTION_ARGS);
Datum cs_receive_function(PG_FUNCTION_ARGS);
Datum cs_send_function(PG_FUNCTION_ARGS);
Datum cs_add(PG_FUNCTION_ARGS);
Datum cs_mul(PG_FUNCTION_ARGS);
Datum cs_sub(PG_FUNCTION_ARGS);
Datum cs_div(PG_FUNCTION_ARGS);
Datum cs_mod(PG_FUNCTION_ARGS);
Datum cs_pow(PG_FUNCTION_ARGS);
Datum cs_and(PG_FUNCTION_ARGS);
Datum cs_or(PG_FUNCTION_ARGS);
Datum cs_xor(PG_FUNCTION_ARGS);
Datum cs_concat(PG_FUNCTION_ARGS);
Datum cs_cat(PG_FUNCTION_ARGS);
Datum cs_cut(PG_FUNCTION_ARGS);
Datum cs_as(PG_FUNCTION_ARGS);
Datum cs_as_array(PG_FUNCTION_ARGS);
Datum cs_eq(PG_FUNCTION_ARGS);
Datum cs_ne(PG_FUNCTION_ARGS);
Datum cs_ge(PG_FUNCTION_ARGS);
Datum cs_le(PG_FUNCTION_ARGS);
Datum cs_lt(PG_FUNCTION_ARGS);
Datum cs_gt(PG_FUNCTION_ARGS);
Datum cs_like(PG_FUNCTION_ARGS);
Datum cs_ilike(PG_FUNCTION_ARGS);
Datum cs_maxof(PG_FUNCTION_ARGS);
Datum cs_minof(PG_FUNCTION_ARGS);
Datum cs_neg(PG_FUNCTION_ARGS);
Datum cs_not(PG_FUNCTION_ARGS);
Datum cs_bit_not(PG_FUNCTION_ARGS);
Datum cs_abs(PG_FUNCTION_ARGS);
Datum cs_limit(PG_FUNCTION_ARGS);
Datum cs_sin(PG_FUNCTION_ARGS);
Datum cs_cos(PG_FUNCTION_ARGS);
Datum cs_tan(PG_FUNCTION_ARGS);
Datum cs_exp(PG_FUNCTION_ARGS);
Datum cs_asin(PG_FUNCTION_ARGS);
Datum cs_acos(PG_FUNCTION_ARGS);
Datum cs_atan(PG_FUNCTION_ARGS);
Datum cs_sqrt(PG_FUNCTION_ARGS);
Datum cs_log(PG_FUNCTION_ARGS);
Datum cs_ceil(PG_FUNCTION_ARGS);
Datum cs_floor(PG_FUNCTION_ARGS);
Datum cs_isnan(PG_FUNCTION_ARGS);
Datum cs_wsum(PG_FUNCTION_ARGS);
Datum cs_wavg(PG_FUNCTION_ARGS);
Datum cs_corr(PG_FUNCTION_ARGS);
Datum cs_cov(PG_FUNCTION_ARGS);
Datum cs_norm(PG_FUNCTION_ARGS);
Datum cs_thin(PG_FUNCTION_ARGS);
Datum cs_iif(PG_FUNCTION_ARGS);
Datum cs_if(PG_FUNCTION_ARGS);
Datum cs_filter(PG_FUNCTION_ARGS);
Datum cs_filter_pos(PG_FUNCTION_ARGS);
Datum cs_filter_first_pos(PG_FUNCTION_ARGS);
Datum cs_unique(PG_FUNCTION_ARGS);
Datum cs_reverse(PG_FUNCTION_ARGS);
Datum cs_diff(PG_FUNCTION_ARGS);
Datum cs_trend(PG_FUNCTION_ARGS);
Datum cs_repeat(PG_FUNCTION_ARGS);
Datum cs_count(PG_FUNCTION_ARGS);
Datum cs_approxdc(PG_FUNCTION_ARGS);
Datum cs_max(PG_FUNCTION_ARGS);
Datum cs_min(PG_FUNCTION_ARGS);
Datum cs_avg(PG_FUNCTION_ARGS);
Datum cs_sum(PG_FUNCTION_ARGS);
Datum cs_any(PG_FUNCTION_ARGS);
Datum cs_all(PG_FUNCTION_ARGS);
Datum cs_prd(PG_FUNCTION_ARGS);
Datum cs_var(PG_FUNCTION_ARGS);
Datum cs_dev(PG_FUNCTION_ARGS);
Datum cs_median(PG_FUNCTION_ARGS);
Datum cs_group_count(PG_FUNCTION_ARGS);
Datum cs_group_approxdc(PG_FUNCTION_ARGS);
Datum cs_group_max(PG_FUNCTION_ARGS);
Datum cs_group_min(PG_FUNCTION_ARGS);
Datum cs_group_avg(PG_FUNCTION_ARGS);
Datum cs_group_sum(PG_FUNCTION_ARGS);
Datum cs_group_any(PG_FUNCTION_ARGS);
Datum cs_group_all(PG_FUNCTION_ARGS);
Datum cs_group_var(PG_FUNCTION_ARGS);
Datum cs_group_dev(PG_FUNCTION_ARGS);
Datum cs_group_last(PG_FUNCTION_ARGS);
Datum cs_group_first(PG_FUNCTION_ARGS);
Datum cs_grid_max(PG_FUNCTION_ARGS);
Datum cs_grid_min(PG_FUNCTION_ARGS);
Datum cs_grid_avg(PG_FUNCTION_ARGS);
Datum cs_grid_sum(PG_FUNCTION_ARGS);
Datum cs_grid_var(PG_FUNCTION_ARGS);
Datum cs_grid_dev(PG_FUNCTION_ARGS);
Datum cs_window_max(PG_FUNCTION_ARGS);
Datum cs_window_min(PG_FUNCTION_ARGS);
Datum cs_window_avg(PG_FUNCTION_ARGS);
Datum cs_window_sum(PG_FUNCTION_ARGS);
Datum cs_window_var(PG_FUNCTION_ARGS);
Datum cs_window_dev(PG_FUNCTION_ARGS);
Datum cs_window_ema(PG_FUNCTION_ARGS);
Datum cs_window_atr(PG_FUNCTION_ARGS);
Datum cs_hash_count(PG_FUNCTION_ARGS);
Datum cs_hash_dup_count(PG_FUNCTION_ARGS);
Datum cs_hash_max(PG_FUNCTION_ARGS);
Datum cs_hash_min(PG_FUNCTION_ARGS);
Datum cs_hash_avg(PG_FUNCTION_ARGS);
Datum cs_hash_sum(PG_FUNCTION_ARGS);
Datum cs_hash_any(PG_FUNCTION_ARGS);
Datum cs_hash_all(PG_FUNCTION_ARGS);
Datum cs_top_max(PG_FUNCTION_ARGS);
Datum cs_top_min(PG_FUNCTION_ARGS);
Datum cs_top_max_pos(PG_FUNCTION_ARGS);
Datum cs_top_min_pos(PG_FUNCTION_ARGS);
Datum cs_cum_max(PG_FUNCTION_ARGS);
Datum cs_cum_min(PG_FUNCTION_ARGS);
Datum cs_cum_avg(PG_FUNCTION_ARGS);
Datum cs_cum_sum(PG_FUNCTION_ARGS);
Datum cs_cum_prd(PG_FUNCTION_ARGS);
Datum cs_cum_var(PG_FUNCTION_ARGS);
Datum cs_cum_dev(PG_FUNCTION_ARGS);
Datum cs_histogram(PG_FUNCTION_ARGS);
Datum cs_cross(PG_FUNCTION_ARGS);
Datum cs_extrema(PG_FUNCTION_ARGS);
Datum cs_stretch(PG_FUNCTION_ARGS);
Datum cs_stretch0(PG_FUNCTION_ARGS);
Datum cs_asof_join(PG_FUNCTION_ARGS);
Datum cs_asof_join_pos(PG_FUNCTION_ARGS);
Datum cs_join(PG_FUNCTION_ARGS);
Datum cs_join_pos(PG_FUNCTION_ARGS);
Datum cs_map(PG_FUNCTION_ARGS);
Datum cs_union(PG_FUNCTION_ARGS);
Datum cs_empty(PG_FUNCTION_ARGS);
Datum cs_project(PG_FUNCTION_ARGS);
Datum cs_project_agg(PG_FUNCTION_ARGS);
Datum cs_year(PG_FUNCTION_ARGS);
Datum cs_month(PG_FUNCTION_ARGS);
Datum cs_mday(PG_FUNCTION_ARGS);
Datum cs_wday(PG_FUNCTION_ARGS);
Datum cs_hour(PG_FUNCTION_ARGS);
Datum cs_minute(PG_FUNCTION_ARGS);
Datum cs_second(PG_FUNCTION_ARGS);
Datum cs_week(PG_FUNCTION_ARGS);
Datum cs_quarter(PG_FUNCTION_ARGS);
Datum cs_call(PG_FUNCTION_ARGS);
Datum cs_to_array(PG_FUNCTION_ARGS);
Datum cs_from_array(PG_FUNCTION_ARGS);
Datum cs_profile(PG_FUNCTION_ARGS);
Datum cs_sort(PG_FUNCTION_ARGS);
Datum cs_sort_pos(PG_FUNCTION_ARGS);
Datum cs_rank(PG_FUNCTION_ARGS);
Datum cs_dense_rank(PG_FUNCTION_ARGS);
Datum cs_quantile(PG_FUNCTION_ARGS);
Datum cs_str2code(PG_FUNCTION_ARGS);
Datum cs_code2str(PG_FUNCTION_ARGS);
Datum cs_cut_and_code2str(PG_FUNCTION_ARGS);
Datum cs_dictionary_size(PG_FUNCTION_ARGS);
void imcs_ereport(int err_code, char const* err_msg,...)
{
static char err_buf[IMCS_MAX_ERROR_MSG_LEN];
va_list args;
imcs_error_handler_t* hnd = imcs_tls != NULL ? (imcs_error_handler_t*)imcs_tls->get(imcs_tls) : NULL;
va_start(args, err_msg);
if (hnd != NULL) {
vsprintf(hnd->err_msg, err_msg, args);
va_end(args);
hnd->err_code = err_code;
longjmp(hnd->unwind_buf, 1);
} else {
vsprintf(err_buf, err_msg, args);
va_end(args);
ereport(ERROR, (errcode(err_code), errmsg("%s", err_buf)));
}
}
static void imcs_shmem_startup(void);
static void imcs_shmem_request(void);
static uint32 imcs_hash_fn(const void *key, Size keysize)
{
imcs_hash_key_t* ik = (imcs_hash_key_t*)key;
char const* id = ik->id;
uint32 h = ik->db;
while (*id != 0) {
h = h*31 + *id++;
}
return h;
}
static int imcs_match_fn(const void *key1, const void *key2, Size keysize)
{
imcs_hash_key_t* ik1 = (imcs_hash_key_t*)key1;
imcs_hash_key_t* ik2 = (imcs_hash_key_t*)key2;
return ik1->db == ik2->db && strcmp(ik1->id, ik2->id) == 0 ? 0 : 1;
}
static void* imcs_keycopy_fn(void *dest, const void *src, Size keysize)
{
imcs_hash_key_t* dk = (imcs_hash_key_t*)dest;
imcs_hash_key_t* sk = (imcs_hash_key_t*)src;
dk->db = sk->db;
dk->id = (char*)ShmemAlloc(strlen(sk->id)+1);
if (dk->id == NULL) {
imcs_ereport(ERRCODE_OUT_OF_MEMORY, "not enough shared memory for hash entry");
}
strcpy(dk->id, sk->id);
return dk;
}
static void imcs_init_hash()
{
static HASHCTL info;
info.keysize = sizeof(imcs_hash_key_t);
info.entrysize = sizeof(imcs_hash_entry_t);
info.hash = imcs_hash_fn;
info.match = imcs_match_fn;
info.keycopy = imcs_keycopy_fn;
imcs_hash = ShmemInitHash("imcs hash",
n_timeseries, n_timeseries*10,
&info,
HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_KEYCOPY);
}
static uint32 imcs_dict_hash_fn(const void *key, Size keysize)
{
imcs_dict_key_t const* dk = (imcs_dict_key_t const*)key;
char* val = dk->val;
size_t len = dk->len;
size_t i;
uint32 h = 0;
for (i = 0; i < len; i++) {
h = h*31 + val[i];
}
return h;
}
static int imcs_dict_match_fn(const void *key1, const void *key2, Size keysize)
{
imcs_dict_key_t const* dk1 = (imcs_dict_key_t const*)key1;
imcs_dict_key_t const* dk2 = (imcs_dict_key_t const*)key2;
return dk1->len == dk2->len ? memcmp(dk1->val, dk2->val, dk1->len) : dk1->len - dk2->len;
}
static void* imcs_dict_keycopy_fn(void *dest, const void *src, Size keysize)
{
imcs_dict_key_t* dk = (imcs_dict_key_t*)dest;
imcs_dict_key_t* sk = (imcs_dict_key_t*)src;
dk->val = (char*)ShmemAlloc(sk->len);
if (dk->val == NULL) {
imcs_ereport(ERRCODE_OUT_OF_MEMORY, "not enough shared memory for dictionary entry");
}
dk->len = sk->len;
memcpy(dk->val, sk->val, sk->len);
return dk;
}
static void imcs_init_dict()
{
if (imcs_dict_size != 0) {
static HASHCTL info;
info.keysize = sizeof(imcs_dict_key_t);
info.entrysize = sizeof(imcs_dict_entry_t);
info.hash = imcs_dict_hash_fn;
info.match = imcs_dict_match_fn;
info.keycopy = imcs_dict_keycopy_fn;
imcs_dict = ShmemInitHash("imcs dictionary",
imcs_dict_size, imcs_dict_size,
&info,
HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_KEYCOPY);
imcs_dict_code_map = (imcs_dict_entry_t**)ShmemAlloc(imcs_dict_size*sizeof(imcs_dict_entry_t*));
if (imcs_dict_code_map == NULL) {
imcs_ereport(ERRCODE_OUT_OF_MEMORY, "not enough shared memory for dictionary map");
}
}
}
static void imcs_executor_end(QueryDesc *queryDesc)
{
if (CurrentMemoryContext == TopTransactionContext) {
imcs_project_redundant_calls = 0;
imcs_project_call_count = 0;
if (!imcs_serializable && imcs && imcs_lock != LOCK_NONE) {
if (LWLockHeldByMe(imcs->lock)) {
LWLockRelease(imcs->lock);
}
imcs_lock = LOCK_NONE;
}
if (imcs_mem_ctx) {
MemoryContextReset(imcs_mem_ctx);
}
}
if (prev_ExecutorEnd) {
prev_ExecutorEnd(queryDesc);
} else {
standard_ExecutorEnd(queryDesc);
}
}
static void imcs_trans_callback(XactEvent event, void *arg)
{
if (event == XACT_EVENT_COMMIT || event == XACT_EVENT_ABORT) {
imcs_project_redundant_calls = 0;
imcs_project_call_count = 0;
if (imcs && imcs_lock != LOCK_NONE) {
if (LWLockHeldByMe(imcs->lock)) {
if (event == XACT_EVENT_COMMIT && imcs_flush_file) {
imcs_disk_flush();
}
LWLockRelease(imcs->lock);
}
imcs_lock = LOCK_NONE;
}
if (imcs_mem_ctx) {
MemoryContextReset(imcs_mem_ctx);
}
}
}
imcs_timeseries_t* imcs_get_timeseries(char const* id, imcs_elem_typeid_t elem_type, bool is_timestamp, int elem_size, bool create)
{
imcs_timeseries_t* ts;
imcs_hash_entry_t* entry;
imcs_hash_key_t key;
bool found;
int autoload_attempts = imcs_autoload ? 2 : 0;
if (id == NULL) {
return NULL;
}
if (imcs == NULL) {
imcs_ereport(ERRCODE_LOCK_NOT_AVAILABLE, "Columnar store was not properly initialized, please check that imcs plugin was added to shared_preload_libraries list");
}
if (create) {
if (imcs_lock != LOCK_EXCLUSIVE) {
if (imcs_lock != LOCK_NONE) {
LWLockRelease(imcs->lock);
}
LWLockAcquire(imcs->lock, LW_EXCLUSIVE);
imcs_lock = LOCK_EXCLUSIVE;
}
} else if (imcs_lock == LOCK_NONE) {
LWLockAcquire(imcs->lock, LW_SHARED);
imcs_lock = LOCK_SHARED;
}
Retry:
key.id = (char*)id;
key.db = MyDatabaseId;
entry = (imcs_hash_entry_t*)hash_search(imcs_hash, &key, HASH_FIND, NULL);
if (entry == NULL) {
if (!create) {
if (autoload_attempts && elem_size != 0) { /* elem_size == 0 when imcs_get_timeseries is called from columnar_store_initialized */
char const* sep = strchr(id, '-');
if (sep != NULL) {
size_t table_name_len = sep - id;
char* table_name = (char*)palloc(table_name_len + 1);
int rc;
memcpy(table_name, id, table_name_len);
table_name[table_name_len] = '\0';
key.id = table_name;
if (autoload_attempts == 2 && !hash_search(imcs_hash, &key, HASH_FIND, NULL)) {
/* load all table */
char stmt[MAX_SQL_STMT_LEN];
SPI_connect();
sprintf(stmt, "select %s_load()", table_name);
rc = SPI_execute(stmt, true, 0);
SPI_finish();
if (rc != SPI_OK_SELECT) {
elog(ERROR, "Select failed with status %d", rc);
}
autoload_attempts = 1;
goto Retry;
} else if (autoload_attempts == 1 && !is_timestamp) {
/* load particular column */
char stmt[MAX_SQL_STMT_LEN];
char* column_name = (char*)sep + 1;
sep = strchr(column_name, '-');
if (sep != NULL) {
int column_name_len = sep - column_name;
char* buf = (char*)palloc(column_name_len + 1);
memcpy(buf, column_name, column_name_len);
column_name = buf;
column_name[column_name_len] = '\0';
}
if (strcmp(column_name, id) != 0) {
SPI_connect();
sprintf(stmt, "select %s_load_column('%s')", table_name, column_name);
rc = SPI_execute(stmt, true, 0);
SPI_finish();
if (rc != SPI_OK_SELECT) {
elog(ERROR, "Select failed with status %d", rc);
}
autoload_attempts = 0; /* do not make more than one attempt of autoloading data */
goto Retry;
}
}
}
}
return NULL;
}
/* Find or create an entry with desired hash code */
entry = (imcs_hash_entry_t*)hash_search(imcs_hash, &key, HASH_ENTER, &found);
ts = &entry->value;
if (!found) {
/* New entry, initialize it */
ts->root_page = NULL;
ts->count = 0;
ts->elem_type = elem_type;
ts->elem_size = elem_size;
ts->is_timestamp = is_timestamp;
}
} else {
ts = &entry->value;
}
if (elem_size != 0 /* elem_size == 0 when imcs_get_timeseries is called from columnar_store_initialized */
&& (ts->elem_type != elem_type ||
ts->elem_size != elem_size ||
ts->is_timestamp != is_timestamp))
{
imcs_ereport(ERRCODE_DATATYPE_MISMATCH, "data format was changed");
}
return ts;
}
/* imcs_alloc can be concurrently invoked from multiple threads, so as far as MemoryContextAlloc is non reetrant we have to use mutex here
*/
void* imcs_alloc(size_t size)
{
void* ptr;
imcs_alloc_mutex->lock(imcs_alloc_mutex);
ptr = MemoryContextAlloc(imcs_mem_ctx, size);
imcs_alloc_mutex->unlock(imcs_alloc_mutex);
return ptr;
}
/* align returned memory on 16-byte boundary to allow use of SSE vector instructions.
* This memory should not be deallocated using imcs_free
*/
void* imcs_alloc_aligned(size_t size)
{
char* ptr;