-
Notifications
You must be signed in to change notification settings - Fork 3
/
pg_comment_stats.c
1192 lines (1038 loc) · 42.6 KB
/
pg_comment_stats.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 "postgres.h"
#include <math.h>
#include "access/tupdesc.h"
#include "access/htup_details.h"
#include "lib/stringinfo.h"
#include "postmaster/bgworker.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/shmem.h"
#include "utils/datetime.h"
#include "utils/dynahash.h"
#include "utils/timestamp.h"
#include "pg_comment_stats.h"
#include "utils/guc.h"
PG_MODULE_MAGIC;
static char *worker_name = "pgcs_worker";
static char *extension_name = "pg_comment_stats";
static HTAB *string_to_id = NULL;
static HTAB *id_to_string = NULL;
static GlobalInfo *global_variables = NULL;
static volatile sig_atomic_t got_sigterm = false;
static int stat_time_interval;
static int buffer_size_mb;
static Size buffer_size;
static char* excluded_keys = NULL;
void _PG_init(void);
void _PG_fini(void);
static pgsk_counters_hook_type prev_pgsk_counters_hook = NULL;
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;
static void pgcs_shmem_request(void);
#endif
static void pgcs_shmem_startup(void);
void pgcs_register_bgworker(void);
static void get_composite_key_by_query(char*, pgcsCompositeKey*, bool*);
static int get_index_of_comment_key(char*);
static int get_index_of_comment_key_lock_mode(char*, bool);
static int get_id_from_string(char*);
static Datum pgcs_internal_get_stats_time_interval(TimestampTz, TimestampTz, FunctionCallInfo);
void pgcs_add(void*, void*);
void pgcs_on_delete(void*, void*);
static uint64_t pgcs_find_optimal_memory_split(uint64_t, uint64_t, uint64_t*, uint64_t*, uint64_t*, int*, int*);
void pg_comment_stats_main(Datum main_arg);
void pgcs_store_aggregated_counters(pgskCounters*, const char*, int, pgskStoreKind);
static int
get_random_int(void)
{
int x;
x = (int) (random() & 0xFFFF) << 16;
x |= (int) (random() & 0xFFFF);
return x;
}
static void
pg_comment_stats_sigterm(SIGNAL_ARGS) {
int save_errno = errno;
got_sigterm = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
errno = save_errno;
}
void
_PG_init(void) {
if (!process_shared_preload_libraries_in_progress) {
elog(ERROR, "This module can only be loaded via shared_preload_libraries");
return;
}
pgcs_register_bgworker();
DefineCustomIntVariable("pg_comment_stats.buffer_size",
"Max amount of shared memory (in megabytes), that can be allocated",
"Default of 20, max of 5000",
&buffer_size_mb,
20,
min_buffer_size_mb,
max_buffer_size_mb,
PGC_SUSET,
GUC_UNIT_MB | GUC_NO_RESET_ALL,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pg_comment_stats.stat_time_interval",
"Duration of stat collecting interval",
"Default of 6000s, max of 360000s",
&stat_time_interval,
6000,
min_time_interval,
max_time_interval,
PGC_SUSET,
GUC_UNIT_S | GUC_NO_RESET_ALL,
NULL,
NULL,
NULL);
DefineCustomStringVariable("pg_comment_stats.excluded_keys",
"Excluded keys separated by ','",
NULL,
&excluded_keys,
NULL,
PGC_POSTMASTER,
0, /* no flags required */
NULL,
NULL,
NULL);
DefineCustomBoolVariable("pg_comment_stats.enabled",
"Flag to enable or disable stats collecting.",
NULL,
&pgcs_enabled,
true,
PGC_SIGHUP,
0, /* no flags required */
NULL,
NULL,
NULL);
buffer_size = ((Size)buffer_size_mb) * 1024 * 1024;
EmitWarningsOnPlaceholders("pg_comment_stats");
#if (PG_VERSION_NUM >= 150000)
prev_shmem_request_hook = shmem_request_hook;
shmem_request_hook = pgcs_shmem_request;
#else
RequestAddinShmemSpace(CACHELINEALIGN(buffer_size));
#endif
#if (PG_VERSION_NUM < 150000)
#if PG_VERSION_NUM >= 90500
RequestNamedLWLockTranche("pg_comment_stats", 1);
#else
RequestAddinLWLocks(1);
#endif
#endif
/* Install hook */
prev_pgsk_counters_hook = pgsk_counters_hook;
pgsk_counters_hook = pgcs_store_aggregated_counters;
prev_shmem_startup_hook = shmem_startup_hook;
shmem_startup_hook = pgcs_shmem_startup;
}
void
_PG_fini(void)
{
/* uninstall hook */
shmem_startup_hook = prev_shmem_startup_hook;
pgsk_counters_hook = prev_pgsk_counters_hook;
}
#if (PG_VERSION_NUM >= 150000)
/*
* Requests any additional shared memory required for our extension
*/
static void
pgcs_shmem_request(void)
{
if (prev_shmem_request_hook)
prev_shmem_request_hook();
RequestAddinShmemSpace(CACHELINEALIGN(buffer_size));
RequestNamedLWLockTranche("pg_comment_stats", 1);
}
#endif
static void
pgcs_shmem_startup(void)
{
bool found;
HASHCTL info;
bool found_global_info;
int id;
int items_count;
pgcsStringFromId *stringFromId;
uint64_t total_shmem;
uint64_t pgtb_size;
int global_var_const_size;
uint64_t string_to_id_htab_item;
uint64_t id_to_string_htab_item;
char excluded_keys_copy[max_parameters_count * max_parameter_length];
char* excluded_key;
if (prev_shmem_startup_hook)
prev_shmem_startup_hook();
/* Create or attach to the shared memory state */
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
global_var_const_size = sizeof(GlobalInfo);
string_to_id_htab_item = (sizeof(char) * max_parameter_length + sizeof(pgcsIdFromString));
id_to_string_htab_item = (sizeof(int) + sizeof(pgcsStringFromId));
total_shmem = buffer_size_mb * 1024 * 1024;
pgtb_size = pgcs_find_optimal_memory_split(0,
total_shmem,
&string_to_id_htab_item,
&id_to_string_htab_item,
&total_shmem,
&global_var_const_size,
&items_count);
pgtb_init(extension_name,
&pgcs_add,
&pgcs_on_delete,
(int)(stat_time_interval / buckets_count + 0.5),
pgtb_size,
sizeof(pgcsBucketItem),
sizeof(pgskCounters));
global_variables = ShmemInitStruct("pg_comment_stats global_variables",
sizeof(GlobalInfo),
&found_global_info);
global_variables->bucket_duration = (int)(stat_time_interval / buckets_count + 0.5);
global_variables->items_count = items_count;
elog(LOG, "pgcs: Max count of unique strings: %d", global_variables->items_count);
memset(&info, 0, sizeof(info));
info.keysize = sizeof(char) * max_parameter_length;
info.entrysize = sizeof(pgcsIdFromString);
string_to_id = ShmemInitHash("pg_comment_stats string_to_id_htab",
items_count, items_count,
&info,
HASH_ELEM | HASH_BLOBS);
memset(&info, 0, sizeof(info));
info.keysize = sizeof(int);
info.entrysize = sizeof(pgcsStringFromId);
id_to_string = ShmemInitHash("pg_comment_stats id_to_string_htab",
items_count, items_count,
&info,
HASH_ELEM | HASH_BLOBS);
id = comment_value_not_specified;
stringFromId = hash_search(id_to_string, (void *) &id, HASH_ENTER, &found);
global_variables->currents_strings_count = 1;
stringFromId->id = id;
SpinLockInit(&stringFromId->mutex);
memset(stringFromId->string, '\0', sizeof(stringFromId->string));
memset(&global_variables->excluded_keys, '\0', sizeof(global_variables->excluded_keys));
global_variables->excluded_keys_count = 0;
if (excluded_keys != NULL) {
/* make sure that variable not too big */
memset(&excluded_keys_copy, '\0', sizeof(excluded_keys_copy));
strlcpy(&excluded_keys_copy[0], excluded_keys, max_parameters_count * max_parameter_length);
excluded_key = strtok(excluded_keys_copy, ",");
while (excluded_key != NULL) {
strlcpy(&global_variables->excluded_keys[global_variables->excluded_keys_count][0], excluded_key, max_parameter_length - 1);
global_variables->excluded_keys_count += 1;
excluded_key = strtok(NULL, " ");
}
}
LWLockRelease(AddinShmemInitLock);
}
static bool
is_key_excluded(char *key) {
int i;
for (i = 0; i < global_variables->excluded_keys_count; ++i) {
if (strcmp(key, global_variables->excluded_keys[i]) == 0)
return true;
}
return false;
}
static int
get_index_of_comment_key(char *key) {
int key_index;
key_index = get_index_of_comment_key_lock_mode(key, false);
if (key_index == comment_key_not_specified) {
/* Now read all comment keys with write lock. If there is still no match - create one */
key_index = get_index_of_comment_key_lock_mode(key, true);
}
return key_index;
}
static int
get_index_of_comment_key_lock_mode(char *key, bool is_exclusive) {
int i;
if (global_variables == NULL) {
return comment_key_not_specified;
}
if (is_exclusive) {
LWLockAcquire(&global_variables->lock, LW_EXCLUSIVE);
} else {
LWLockAcquire(&global_variables->lock, LW_SHARED);
}
if (is_key_excluded(key)) {
LWLockRelease(&global_variables->lock);
return comment_key_not_specified;
}
for (i = 0; i < global_variables->keys_count; ++i) {
if (strcmp(key, global_variables->commentKeys[i]) == 0) {
LWLockRelease(&global_variables->lock);
return i;
}
}
if (!is_exclusive) {
LWLockRelease(&global_variables->lock);
return comment_key_not_specified;
}
if (global_variables->keys_count == max_parameters_count) {
global_variables->keys_overflow = true;
LWLockRelease(&global_variables->lock);
return comment_key_not_specified;
}
strcpy(global_variables->commentKeys[global_variables->keys_count++], key);
i = global_variables->keys_count - 1;
LWLockRelease(&global_variables->lock);
return i;
}
static void
get_composite_key_by_query(char *query, pgcsCompositeKey *compositeKey, bool *is_comment_exist) {
char query_copy[(max_parameter_length + 2) * max_parameters_count * 2];
char *end_of_comment;
char *start_of_comment;
int comment_length;
int len;
int i;
char end_of_key[2] = ":\0";
int key_index = -1;
char *query_prefix;
int value_id;
start_of_comment = strstr(query, "/*");
end_of_comment = strstr(query, "*/");
*is_comment_exist = false;
if (start_of_comment == NULL || end_of_comment == NULL) {
compositeKey = NULL;
return;
}
start_of_comment += 3;
comment_length = end_of_comment - start_of_comment;
memset(&query_copy, '\0', sizeof(query_copy));
strlcpy(query_copy, start_of_comment, comment_length + 1);
query_copy[comment_length] = '\0';
query_prefix = strtok(query_copy, " ");
for (i = 0; i < max_parameters_count; ++i) {
compositeKey->keyValues[i] = comment_value_not_specified;
}
while (query_prefix != NULL) {
len = strlen(query_prefix);
if (strcmp(query_prefix + len - 1, end_of_key) == 0 && key_index == -1) {
/* it's key */
query_prefix[len - 1] = '\0';
key_index = get_index_of_comment_key(query_prefix);
} else {
/* it's value */
if (key_index == comment_key_not_specified) {
query_prefix = strtok(NULL, " ");
continue;
}
value_id = get_id_from_string(query_prefix);
if (value_id == -1) {
return;
}
*is_comment_exist = true;
compositeKey->keyValues[key_index] = value_id;
key_index = -1;
}
query_prefix = strtok(NULL, " ");
}
if (key_index != -1)
elog(WARNING, "pg_comment_stats: incorrect comment");
}
static uint64_t
pgcs_find_optimal_items_count(uint64_t left_bound,
uint64_t right_bound,
uint64_t* string_to_id_htab_item,
uint64_t* id_to_string_htab_item,
uint64_t* total_size) {
uint64_t string_to_id_htab_size;
uint64_t id_to_string_htab_size;
uint64_t middle;
middle = (right_bound + left_bound) / 2;
string_to_id_htab_size = hash_estimate_size(middle, *string_to_id_htab_item);
id_to_string_htab_size = hash_estimate_size(middle, *id_to_string_htab_item);
if (left_bound + 1 == right_bound) {
return left_bound;
}
if (string_to_id_htab_size + id_to_string_htab_size > *total_size) {
return pgcs_find_optimal_items_count(left_bound,
middle,
string_to_id_htab_item,
id_to_string_htab_item,
total_size);
} else {
return pgcs_find_optimal_items_count(middle,
right_bound,
string_to_id_htab_item,
id_to_string_htab_item,
total_size);
}
}
static uint64_t
pgcs_find_optimal_memory_split(uint64_t left_bound,
uint64_t right_bound,
uint64_t* string_to_id_htab_item,
uint64_t* id_to_string_htab_item,
uint64_t* total_size,
int* global_var_const_size,
int* pgcs_items) {
int pgcs_max_items_count;
uint64_t middle;
uint64_t pgcs_size;
int pgtb_items;
middle = (right_bound + left_bound) / 2;
pgcs_size = *total_size - middle;
pgcs_max_items_count = pgcs_size / (*string_to_id_htab_item + *id_to_string_htab_item);
pgtb_items = pgtb_get_items_count(middle,
sizeof(pgcsBucketItem),
sizeof(pgskCounters));
*pgcs_items = pgcs_find_optimal_items_count(0,
pgcs_max_items_count,
string_to_id_htab_item,
id_to_string_htab_item,
&pgcs_size);
if (left_bound + 1 == right_bound) {
return left_bound;
}
if (pgtb_items < *pgcs_items) {
return pgcs_find_optimal_memory_split(middle,
right_bound,
string_to_id_htab_item,
id_to_string_htab_item,
total_size,
global_var_const_size,
pgcs_items);
} else {
return pgcs_find_optimal_memory_split(left_bound,
middle,
string_to_id_htab_item,
id_to_string_htab_item,
total_size,
global_var_const_size,
pgcs_items);
}
}
void
pgcs_store_aggregated_counters(pgskCounters* counters, const char* query_string, int level, pgskStoreKind kind) {
pgcsBucketItem key;
pgskCounters additional_counters;
bool is_comment_exist;
bool stored;
char query[(max_parameter_length + 1) * max_parameters_count];
if (global_variables == NULL) {
return;
}
if (!pgcs_enabled) {
return;
}
if (prev_pgsk_counters_hook)
prev_pgsk_counters_hook(counters, query_string, level, kind);
/* Now track only top-level statements */
/* TODO: maybe add support of other levels? */
if (level != 0)
return;
memset(&key.compositeKey, 0, sizeof(pgcsCompositeKey));
strlcpy(query, query_string, sizeof(query));
query[sizeof(query) - 1] = '\0';
get_composite_key_by_query((char*)&query, &key.compositeKey, &is_comment_exist);
if (!is_comment_exist) {
return;
}
key.database = MyDatabaseId;
key.user = GetUserId();
additional_counters.usage = 1;
additional_counters.utime = counters->utime;
additional_counters.stime = counters->stime;
#ifdef HAVE_GETRUSAGE
additional_counters.minflts = counters->minflts;
additional_counters.majflts = counters->majflts;
additional_counters.nswaps = counters->nswaps;
additional_counters.reads = counters->reads;
additional_counters.writes = counters->writes;
additional_counters.msgsnds = counters->msgsnds;
additional_counters.msgrcvs = counters->msgrcvs;
additional_counters.nsignals = counters->nsignals;
additional_counters.nvcsws = counters->nvcsws;
additional_counters.nivcsws = counters->nivcsws;
#endif
stored = pgtb_put(extension_name, &key, &additional_counters);
if (!stored) {
pgcs_on_delete((void*) &key, (void*) &additional_counters);
}
}
void pgcs_add(void* value, void* anotherValue) {
pgskCounters* counters;
pgskCounters* another_counters;
counters = (pgskCounters*) value;
another_counters = (pgskCounters*) anotherValue;
counters->usage += another_counters->usage;
counters->utime += another_counters->utime;
counters->stime += another_counters->stime;
counters->minflts += another_counters->minflts;
counters->majflts += another_counters->majflts;
counters->nswaps += another_counters->nswaps;
counters->reads += another_counters->reads;
counters->writes += another_counters->writes;
counters->msgsnds += another_counters->msgsnds;
counters->msgrcvs += another_counters->msgrcvs;
counters->nsignals += another_counters->nsignals;
counters->nvcsws += another_counters->nvcsws;
counters->nivcsws += another_counters->nivcsws;
}
void
pgcs_register_bgworker() {
BackgroundWorker worker;
MemSet(&worker, 0, sizeof(BackgroundWorker));
worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
worker.bgw_start_time = BgWorkerStart_PostmasterStart;
snprintf(worker.bgw_name, BGW_MAXLEN, "%s", worker_name);
sprintf(worker.bgw_library_name, "pg_comment_stats");
sprintf(worker.bgw_function_name, "pg_comment_stats_main");
/* Wait 10 seconds for restart after crash */
worker.bgw_restart_time = 10;
worker.bgw_main_arg = (Datum) 0;
worker.bgw_notify_pid = 0;
RegisterBackgroundWorker(&worker);
}
static int
get_id_from_string(char *string_pointer) {
char string[max_parameter_length];
pgcsIdFromString *idFromString;
pgcsStringFromId *stringFromId;
int id;
bool found;
if (!string_to_id || !id_to_string)
return comment_value_not_specified;
if (strlen(string_pointer) >= max_parameter_length) {
elog(WARNING, "pg_comment_stats: Comment value %s too long to store. Max length is %d",
string_pointer,
max_parameter_length);
return comment_value_not_specified;
}
memset(&string, '\0', max_parameter_length);
strcpy(string, string_pointer);
LWLockAcquire(&global_variables->lock, LW_SHARED);
idFromString = hash_search(string_to_id, (void *) &string, HASH_FIND, &found);
if (found) {
stringFromId = hash_search(id_to_string, (void *) &idFromString->id, HASH_FIND, &found);
} else {
if (global_variables->currents_strings_count >= global_variables->items_count) {
SpinLockAcquire(&global_variables->overflow_mutex);
if (!global_variables->max_strings_count_achieved) {
elog(WARNING,
"pg_comment_stats: Can't handle request. No more memory for save strings are available. "
"Current max count of unique strings = %d. \n"
"Decide to tune pg_comment_stats.buffer_size. ", global_variables->items_count);
}
global_variables->max_strings_count_achieved = true;
global_variables->strings_overflow_by += 1;
SpinLockRelease(&global_variables->overflow_mutex);
LWLockRelease(&global_variables->lock);
return comment_value_not_specified;
}
LWLockRelease(&global_variables->lock);
LWLockAcquire(&global_variables->lock, LW_EXCLUSIVE);
/* To prevent race we should */
/* check once more, that string still does not exist */
idFromString = hash_search(string_to_id, (void *) &string, HASH_FIND, &found);
if (found) {
/* If found - we can simply increase counter for string */
stringFromId = hash_search(id_to_string, (void *) &idFromString->id, HASH_FIND, &found);
} else {
/* If not found - insert string into hash tables */
/* Generate id that not used yet. */
found = true;
while (found) {
id = get_random_int();
stringFromId = hash_search(id_to_string, (void *) &id, HASH_FIND, &found);
}
stringFromId = hash_search(id_to_string, (void *) &id, HASH_ENTER_NULL, &found);
if (stringFromId == NULL) {
global_variables->out_of_shared_memory = true;
LWLockRelease(&global_variables->lock);
return comment_value_not_specified;
}
global_variables->currents_strings_count += 1;
stringFromId->id = id;
memset(stringFromId->string, '\0', max_parameter_length);
strcpy(stringFromId->string, string);
stringFromId->counter = 0;
SpinLockInit(&stringFromId->mutex);
idFromString = hash_search(string_to_id, (void *) &string, HASH_ENTER_NULL, &found);
if (idFromString == NULL) {
global_variables->currents_strings_count -= 1;
stringFromId = hash_search(id_to_string, (void *) &id, HASH_REMOVE, &found);
global_variables->out_of_shared_memory = true;
LWLockRelease(&global_variables->lock);
return comment_value_not_specified;
}
memset(idFromString->string, '\0', max_parameter_length);
strcpy(idFromString->string, string);
idFromString->id = id;
}
}
SpinLockAcquire(&stringFromId->mutex);
stringFromId->counter++;
SpinLockRelease(&stringFromId->mutex);
id = idFromString->id;
LWLockRelease(&global_variables->lock);
return id;
}
static void
get_string_from_id(int id, char* string) {
pgcsStringFromId *stringFromId;
bool found;
if (!string_to_id || !id_to_string) {
return;
}
stringFromId = hash_search(id_to_string, (void *) &id, HASH_FIND, &found);
strlcpy(string, stringFromId->string, max_parameter_length);
}
void pgcs_on_delete(void* key, void* value) {
pgcsBucketItem* item;
pgskCounters* counters;
pgcsStringFromId *string_struct;
int id;
int i;
bool found;
if (global_variables == NULL)
return;
LWLockAcquire(&global_variables->lock, LW_EXCLUSIVE);
item = (pgcsBucketItem*) key;
counters = (pgskCounters*) value;
for (i = 0; i < global_variables->keys_count; ++i) {
id = item->compositeKey.keyValues[i];
if (id == comment_value_not_specified) {
continue;
}
string_struct = hash_search(id_to_string, (void *) &id, HASH_FIND, &found);
string_struct->counter -= (int)(counters->usage + 0.5);
if (string_struct->counter == 0) {
hash_search(id_to_string, (void *) &id, HASH_REMOVE, &found);
hash_search(string_to_id, (void *) string_struct->string, HASH_REMOVE, &found);
global_variables->currents_strings_count -= 1;
}
}
LWLockRelease(&global_variables->lock);
}
static void
pgcs_init() {
LWLockAcquire(&global_variables->lock, LW_EXCLUSIVE);
memset(&global_variables->commentKeys, '\0', sizeof(global_variables->commentKeys));
global_variables->max_strings_count_achieved = false;
global_variables->keys_count = 0;
global_variables->strings_overflow_by = 0;
global_variables->out_of_shared_memory = false;
global_variables->keys_overflow = false;
SpinLockInit(&global_variables->overflow_mutex);
LWLockRelease(&global_variables->lock);
}
static void
pgcs_update_info() {
if (global_variables == NULL) {
return;
}
SpinLockAcquire(&global_variables->overflow_mutex);
if (global_variables->max_strings_count_achieved) {
elog(WARNING, "pg_comment_stats: Too many unique strings. Overflow by %d (%f%%)",
global_variables->strings_overflow_by, (double)global_variables->strings_overflow_by / global_variables->items_count);
}
if (global_variables->keys_overflow) {
elog(WARNING, "pg_comment_stats: Can not store more than %d parameters", max_parameters_count);
}
global_variables->keys_overflow = false;
global_variables->strings_overflow_by = 0;
global_variables->max_strings_count_achieved = false;
SpinLockRelease(&global_variables->overflow_mutex);
}
void
pg_comment_stats_main(Datum main_arg) {
TimestampTz timestamp;
int64 wait_microsec;
/* Register functions for SIGTERM management */
pqsignal(SIGTERM, pg_comment_stats_sigterm);
/* We're now ready to receive signals */
BackgroundWorkerUnblockSignals();
LWLockInitialize(&global_variables->lock, LWLockNewTrancheId());
pgcs_init();
wait_microsec = global_variables->bucket_duration * 1e6;
while (!got_sigterm) {
int rc;
/* Wait necessary amount of time */
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, wait_microsec / 1000, PG_WAIT_EXTENSION);
/* Emergency bailout if postmaster has died */
if (rc & WL_POSTMASTER_DEATH)
proc_exit(1);
/* Process signals */
timestamp = GetCurrentTimestamp();
ResetLatch(&MyProc->procLatch);
if (got_sigterm) {
/* Simply exit */
elog(DEBUG1, "bgworker pg_comment_stats signal: processed SIGTERM");
proc_exit(0);
}
/* Main work happens here */
pgcs_update_info();
if (global_variables->out_of_shared_memory) {
elog(WARNING, "pg_comment_stats: out of shared memory");
global_variables->out_of_shared_memory = false;
}
pgtb_tick(extension_name);
wait_microsec = (int64) global_variables->bucket_duration * 1e6 - (GetCurrentTimestamp() - timestamp);
if (wait_microsec < 0)
wait_microsec = 0;
}
/* No problems, so clean exit */
proc_exit(0);
}
static char *escape_json_string(const char *str) {
size_t str_len = strlen(str);
size_t pstr_len = 2 * (str_len + 1);
int i, j = 0;
char *pstr = palloc(pstr_len);
for (i = 0; i < str_len; i++) {
char ch = str[i];
switch (ch) {
case '\\':
pstr[j++] = '\\';
pstr[j++] = '\\';
break;
case '"':
pstr[j++] = '\\';
pstr[j++] = '"';
break;
case '\n':
pstr[j++] = '\\';
pstr[j++] = 'n';
break;
case '\r':
pstr[j++] = '\\';
pstr[j++] = 'r';
break;
case '\t':
pstr[j++] = '\\';
pstr[j++] = 't';
break;
case '\b':
pstr[j++] = '\\';
pstr[j++] = 'b';
break;
case '\f':
pstr[j++] = '\\';
pstr[j++] = 'f';
break;
default:
pstr[j++] = ch;
}
if (j == pstr_len) break;
}
pstr[j++] = '\0';
return pstr;
}
PG_FUNCTION_INFO_V1(pgcs_get_stats);
PG_FUNCTION_INFO_V1(pgcs_get_stats_time_interval);
Datum
get_jsonb_datum_from_key(pgcsCompositeKey *compositeKey) {
StringInfoData strbuf;
int i;
char component[max_parameter_length];
char *escaped_component;
const char* const_component;
bool some_value_stored;
int keys_count;
char commentKeys[max_parameters_count][max_parameter_length];
some_value_stored = false;
initStringInfo(&strbuf);
appendStringInfoChar(&strbuf, '{');
memcpy(&commentKeys, &global_variables->commentKeys, sizeof(commentKeys));
keys_count = global_variables->keys_count;
for (i = 0; i < keys_count; ++i) {
if (compositeKey->keyValues[i] == comment_value_not_specified) {
continue;
}
memset(&component, '\0', sizeof(component));
if (i > 0 && some_value_stored)
appendStringInfoChar(&strbuf, ',');
get_string_from_id(compositeKey->keyValues[i], (char*)&component);
const_component = component;
escaped_component = escape_json_string(const_component);
appendStringInfo(&strbuf, "\"%s\":\"%s\"", (char*)&commentKeys[i], escaped_component);
some_value_stored = true;
pfree(escaped_component);
}
appendStringInfoChar(&strbuf, '}');
appendStringInfoChar(&strbuf, '\0');
return DirectFunctionCall1(jsonb_in, CStringGetDatum(strbuf.data));
}
Datum
pgcs_get_stats(PG_FUNCTION_ARGS) {
TimestampTz timestamp_left;
TimestampTz timestamp_right;
timestamp_right = GetCurrentTimestamp();
timestamp_left = TimestampTzPlusMilliseconds(GetCurrentTimestamp(), (int64) global_variables->bucket_duration * (-buckets_count) * 1e3);
return pgcs_internal_get_stats_time_interval(timestamp_left, timestamp_right, fcinfo);
}
Datum
pgcs_get_stats_time_interval(PG_FUNCTION_ARGS) {
TimestampTz timestamp_left;
TimestampTz timestamp_right;
timestamp_left = PG_GETARG_TIMESTAMP(0);
timestamp_right = PG_GETARG_TIMESTAMP(1);
return pgcs_internal_get_stats_time_interval(timestamp_left, timestamp_right, fcinfo);
}
/* TODO: Currently it's not possible to remove key from stats. Maybe add this possibility? */
static Datum
pgcs_internal_get_stats_time_interval(TimestampTz timestamp_left, TimestampTz timestamp_right, FunctionCallInfo fcinfo) {
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
Tuplestorestate *tupstore;
TupleDesc tupdesc;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
int message_id;
pgcsBucketItem key;
pgskCounters value;
uint64 reads, writes;
char timestamp_left_s[max_parameter_length];
char timestamp_right_s[max_parameter_length];
int items_count;
void* result_ptr;
int length;
int i;
Datum values[PG_STAT_KCACHE_COLS + 1];
bool nulls[PG_STAT_KCACHE_COLS + 1];
/* Shmem structs not ready yet */
if (!global_variables)
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("pg_comment_stats must be loaded via shared_preload_libraries")));
/* check to see if caller supports us returning a tuplestore */
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
if (!(rsinfo->allowedModes & SFRM_Materialize))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("materialize mode required, but it is not allowed in this context")));
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("return type must be a row type")));
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
tupstore = tuplestore_begin_heap(true, false, work_mem);
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
MemoryContextSwitchTo(oldcontext);
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
MemSet(&key, 0, sizeof(pgcsBucketItem));
LWLockAcquire(&global_variables->lock, LW_EXCLUSIVE);
items_count = global_variables->items_count;
result_ptr = (void*) palloc(items_count * (sizeof(pgcsBucketItem) + sizeof(pgskCounters)));
memset(result_ptr, 0, items_count * (sizeof(pgcsBucketItem) + sizeof(pgskCounters)));
pgtb_get_stats_time_interval(extension_name, ×tamp_left, ×tamp_right, result_ptr, &length);
strcpy(timestamp_left_s, timestamptz_to_str(timestamp_left));
strcpy(timestamp_right_s, timestamptz_to_str(timestamp_right));
elog(NOTICE, "pgcs: Show stats from '%s' to '%s'", timestamp_left_s, timestamp_right_s);
/* put to tuplestore and clear bucket index -1 */
for (message_id = 0; message_id < length; ++message_id) {
memcpy(&key,
(char*)result_ptr + (sizeof(pgcsBucketItem) + sizeof(pgskCounters)) * message_id,
sizeof(pgcsBucketItem));
memcpy(&value,
(char*)result_ptr + (sizeof(pgcsBucketItem) + sizeof(pgskCounters)) * message_id + sizeof(pgcsBucketItem),
sizeof(pgskCounters));
MemSet(nulls, false, sizeof(nulls));
i = 0;
values[i++] = get_jsonb_datum_from_key(&key.compositeKey);
values[i++] = Int64GetDatum(value.usage);
values[i++] = ObjectIdGetDatum(key.user);
values[i++] = ObjectIdGetDatum(key.database);
#ifdef HAVE_GETRUSAGE
reads = value.reads * RUSAGE_BLOCK_SIZE;
writes = value.writes * RUSAGE_BLOCK_SIZE;
values[i++] = UInt64GetDatum(reads);
values[i++] = UInt64GetDatum(writes);
#else
nulls[i++] = true; /* reads */
nulls[i++] = true; /* writes */
#endif
values[i++] = Float8GetDatumFast(value.utime);
values[i++] = Float8GetDatumFast(value.stime);
#ifdef HAVE_GETRUSAGE
values[i++] = Int64GetDatumFast(value.minflts);
values[i++] = Int64GetDatumFast(value.majflts);
values[i++] = Int64GetDatumFast(value.nswaps);
values[i++] = Int64GetDatumFast(value.msgsnds);
values[i++] = Int64GetDatumFast(value.msgrcvs);
values[i++] = Int64GetDatumFast(value.nsignals);
values[i++] = Int64GetDatumFast(value.nvcsws);