forked from powa-team/powa-archivist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowa--3.2.0.sql
2296 lines (2009 loc) · 79.9 KB
/
powa--3.2.0.sql
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
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
--\echo Use "CREATE EXTENSION powa" to load this file. \quit
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET search_path = public, pg_catalog;
CREATE TABLE powa_databases(
oid oid PRIMARY KEY,
datname name,
dropped timestamp with time zone
);
CREATE FUNCTION powa_stat_user_functions(IN dbid oid, OUT funcid oid,
OUT calls bigint,
OUT total_time double precision,
OUT self_time double precision)
RETURNS SETOF record
LANGUAGE c COST 100
AS '$libdir/powa', 'powa_stat_user_functions';
CREATE FUNCTION powa_stat_all_rel(IN dbid oid,
OUT relid oid,
OUT numscan bigint,
OUT tup_returned bigint,
OUT tup_fetched bigint,
OUT n_tup_ins bigint,
OUT n_tup_upd bigint,
OUT n_tup_del bigint,
OUT n_tup_hot_upd bigint,
OUT n_liv_tup bigint,
OUT n_dead_tup bigint,
OUT n_mod_since_analyze bigint,
OUT blks_read bigint,
OUT blks_hit bigint,
OUT last_vacuum timestamp with time zone,
OUT vacuum_count bigint,
OUT last_autovacuum timestamp with time zone,
OUT autovacuum_count bigint,
OUT last_analyze timestamp with time zone,
OUT analyze_count bigint,
OUT last_autoanalyze timestamp with time zone,
OUT autoanalyze_count bigint)
RETURNS SETOF record
LANGUAGE c COST 100
AS '$libdir/powa', 'powa_stat_all_rel';
CREATE TYPE powa_statements_history_record AS (
ts timestamp with time zone,
calls bigint,
total_time double precision,
rows bigint,
shared_blks_hit bigint,
shared_blks_read bigint,
shared_blks_dirtied bigint,
shared_blks_written bigint,
local_blks_hit bigint,
local_blks_read bigint,
local_blks_dirtied bigint,
local_blks_written bigint,
temp_blks_read bigint,
temp_blks_written bigint,
blk_read_time double precision,
blk_write_time double precision
);
/* pg_stat_statements operator support */
CREATE TYPE powa_statements_history_diff AS (
intvl interval,
calls bigint,
total_time double precision,
rows bigint,
shared_blks_hit bigint,
shared_blks_read bigint,
shared_blks_dirtied bigint,
shared_blks_written bigint,
local_blks_hit bigint,
local_blks_read bigint,
local_blks_dirtied bigint,
local_blks_written bigint,
temp_blks_read bigint,
temp_blks_written bigint,
blk_read_time double precision,
blk_write_time double precision
);
CREATE OR REPLACE FUNCTION powa_statements_history_mi(
a powa_statements_history_record,
b powa_statements_history_record)
RETURNS powa_statements_history_diff AS
$_$
DECLARE
res powa_statements_history_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.calls = a.calls - b.calls;
res.total_time = a.total_time - b.total_time;
res.rows = a.rows - b.rows;
res.shared_blks_hit = a.shared_blks_hit - b.shared_blks_hit;
res.shared_blks_read = a.shared_blks_read - b.shared_blks_read;
res.shared_blks_dirtied = a.shared_blks_dirtied - b.shared_blks_dirtied;
res.shared_blks_written = a.shared_blks_written - b.shared_blks_written;
res.local_blks_hit = a.local_blks_hit - b.local_blks_hit;
res.local_blks_read = a.local_blks_read - b.local_blks_read;
res.local_blks_dirtied = a.local_blks_dirtied - b.local_blks_dirtied;
res.local_blks_written = a.local_blks_written - b.local_blks_written;
res.temp_blks_read = a.temp_blks_read - b.temp_blks_read;
res.temp_blks_written = a.temp_blks_written - b.temp_blks_written;
res.blk_read_time = a.blk_read_time - b.blk_read_time;
res.blk_write_time = a.blk_write_time - b.blk_write_time;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR - (
PROCEDURE = powa_statements_history_mi,
LEFTARG = powa_statements_history_record,
RIGHTARG = powa_statements_history_record
);
CREATE TYPE powa_statements_history_rate AS (
sec integer,
calls_per_sec double precision,
runtime_per_sec double precision,
rows_per_sec double precision,
shared_blks_hit_per_sec double precision,
shared_blks_read_per_sec double precision,
shared_blks_dirtied_per_sec double precision,
shared_blks_written_per_sec double precision,
local_blks_hit_per_sec double precision,
local_blks_read_per_sec double precision,
local_blks_dirtied_per_sec double precision,
local_blks_written_per_sec double precision,
temp_blks_read_per_sec double precision,
temp_blks_written_per_sec double precision,
blk_read_time_per_sec double precision,
blk_write_time_per_sec double precision
);
CREATE OR REPLACE FUNCTION powa_statements_history_div(
a powa_statements_history_record,
b powa_statements_history_record)
RETURNS powa_statements_history_rate AS
$_$
DECLARE
res powa_statements_history_rate;
sec integer;
BEGIN
res.sec = extract(EPOCH FROM (a.ts - b.ts));
IF res.sec = 0 THEN
sec = 1;
ELSE
sec = res.sec;
END IF;
res.calls_per_sec = (a.calls - b.calls)::double precision / sec;
res.runtime_per_sec = (a.total_time - b.total_time)::double precision / sec;
res.rows_per_sec = (a.rows - b.rows)::double precision / sec;
res.shared_blks_hit_per_sec = (a.shared_blks_hit - b.shared_blks_hit)::double precision / sec;
res.shared_blks_read_per_sec = (a.shared_blks_read - b.shared_blks_read)::double precision / sec;
res.shared_blks_dirtied_per_sec = (a.shared_blks_dirtied - b.shared_blks_dirtied)::double precision / sec;
res.shared_blks_written_per_sec = (a.shared_blks_written - b.shared_blks_written)::double precision / sec;
res.local_blks_hit_per_sec = (a.local_blks_hit - b.local_blks_hit)::double precision / sec;
res.local_blks_read_per_sec = (a.local_blks_read - b.local_blks_read)::double precision / sec;
res.local_blks_dirtied_per_sec = (a.local_blks_dirtied - b.local_blks_dirtied)::double precision / sec;
res.local_blks_written_per_sec = (a.local_blks_written - b.local_blks_written)::double precision / sec;
res.temp_blks_read_per_sec = (a.temp_blks_read - b.temp_blks_read)::double precision / sec;
res.temp_blks_written_per_sec = (a.temp_blks_written - b.temp_blks_written)::double precision / sec;
res.blk_read_time_per_sec = (a.blk_read_time - b.blk_read_time)::double precision / sec;
res.blk_write_time_per_sec = (a.blk_write_time - b.blk_write_time)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR / (
PROCEDURE = powa_statements_history_div,
LEFTARG = powa_statements_history_record,
RIGHTARG = powa_statements_history_record
);
/* end of pg_stat_statements operator support */
CREATE TYPE powa_user_functions_history_record AS (
ts timestamp with time zone,
calls bigint,
total_time double precision,
self_time double precision
);
/* pg_stat_user_functions operator support */
CREATE TYPE powa_user_functions_history_diff AS (
intvl interval,
calls bigint,
total_time double precision,
self_time double precision
);
CREATE OR REPLACE FUNCTION powa_user_functions_history_mi(
a powa_user_functions_history_record,
b powa_user_functions_history_record)
RETURNS powa_user_functions_history_diff AS
$_$
DECLARE
res powa_user_functions_history_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.calls = a.calls - b.calls;
res.total_time = a.total_time - b.total_time;
res.self_time = a.self_time - b.self_time;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR - (
PROCEDURE = powa_user_functions_history_mi,
LEFTARG = powa_user_functions_history_record,
RIGHTARG = powa_user_functions_history_record
);
CREATE TYPE powa_user_functions_history_rate AS (
sec integer,
calls_per_sec double precision,
total_time_per_sec double precision,
self_time_per_sec double precision
);
CREATE OR REPLACE FUNCTION powa_user_functions_history_div(
a powa_user_functions_history_record,
b powa_user_functions_history_record)
RETURNS powa_user_functions_history_rate AS
$_$
DECLARE
res powa_user_functions_history_rate;
sec integer;
BEGIN
res.sec = extract(EPOCH FROM (a.ts - b.ts));
IF res.sec = 0 THEN
sec = 1;
ELSE
sec = res.sec;
END IF;
res.calls_per_sec = (a.calls - b.calls)::double precision / sec;
res.total_time_per_sec = (a.total_time - b.total_time)::double precision / sec;
res.self_time_per_sec = (a.self_time - b.self_time)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR / (
PROCEDURE = powa_user_functions_history_div,
LEFTARG = powa_user_functions_history_record,
RIGHTARG = powa_user_functions_history_record
);
/* end of pg_stat_user_functions operator support */
CREATE TYPE powa_all_relations_history_record AS (
ts timestamp with time zone,
numscan bigint,
tup_returned bigint,
tup_fetched bigint,
n_tup_ins bigint,
n_tup_upd bigint,
n_tup_del bigint,
n_tup_hot_upd bigint,
n_liv_tup bigint,
n_dead_tup bigint,
n_mod_since_analyze bigint,
blks_read bigint,
blks_hit bigint,
last_vacuum timestamp with time zone,
vacuum_count bigint,
last_autovacuum timestamp with time zone,
autovacuum_count bigint,
last_analyze timestamp with time zone,
analyze_count bigint,
last_autoanalyze timestamp with time zone,
autoanalyze_count bigint
);
/* pg_stat_all_relations operator support */
CREATE TYPE powa_all_relations_history_diff AS (
intvl interval,
numscan bigint,
tup_returned bigint,
tup_fetched bigint,
n_tup_ins bigint,
n_tup_upd bigint,
n_tup_del bigint,
n_tup_hot_upd bigint,
n_liv_tup bigint,
n_dead_tup bigint,
n_mod_since_analyze bigint,
blks_read bigint,
blks_hit bigint,
vacuum_count bigint,
autovacuum_count bigint,
analyze_count bigint,
autoanalyze_count bigint
);
CREATE OR REPLACE FUNCTION powa_all_relations_history_mi(
a powa_all_relations_history_record,
b powa_all_relations_history_record)
RETURNS powa_all_relations_history_diff AS
$_$
DECLARE
res powa_all_relations_history_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.numscan = a.numscan - b.numscan;
res.tup_returned = a.tup_returned - b.tup_returned;
res.tup_fetched = a.tup_fetched - b.tup_fetched;
res.n_tup_ins = a.n_tup_ins - b.n_tup_ins;
res.n_tup_upd = a.n_tup_upd - b.n_tup_upd;
res.n_tup_del = a.n_tup_del - b.n_tup_del;
res.n_tup_hot_upd = a.n_tup_hot_upd - b.n_tup_hot_upd;
res.n_liv_tup = a.n_liv_tup - b.n_liv_tup;
res.n_dead_tup = a.n_dead_tup - b.n_dead_tup;
res.n_mod_since_analyze = a.n_mod_since_analyze - b.n_mod_since_analyze;
res.blks_read = a.blks_read - b.blks_read;
res.blks_hit = a.blks_hit - b.blks_hit;
res.vacuum_count = a.vacuum_count - b.vacuum_count;
res.autovacuum_count = a.autovacuum_count - b.autovacuum_count;
res.analyze_count = a.analyze_count - b.analyze_count;
res.autoanalyze_count = a.autoanalyze_count - b.autoanalyze_count;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR - (
PROCEDURE = powa_all_relations_history_mi,
LEFTARG = powa_all_relations_history_record,
RIGHTARG = powa_all_relations_history_record
);
CREATE TYPE powa_all_relations_history_rate AS (
sec integer,
numscan_per_sec double precision,
tup_returned_per_sec double precision,
tup_fetched_per_sec double precision,
n_tup_ins_per_sec double precision,
n_tup_upd_per_sec double precision,
n_tup_del_per_sec double precision,
n_tup_hot_upd_per_sec double precision,
n_liv_tup_per_sec double precision,
n_dead_tup_per_sec double precision,
n_mod_since_analyze_per_sec double precision,
blks_read_per_sec double precision,
blks_hit_per_sec double precision,
vacuum_count_per_sec double precision,
autovacuum_count_per_sec double precision,
analyze_count_per_sec double precision,
autoanalyze_count_per_sec double precision
);
CREATE OR REPLACE FUNCTION powa_all_relations_history_div(
a powa_all_relations_history_record,
b powa_all_relations_history_record)
RETURNS powa_all_relations_history_rate AS
$_$
DECLARE
res powa_all_relations_history_rate;
sec integer;
BEGIN
res.sec = extract(EPOCH FROM (a.ts - b.ts));
IF res.sec = 0 THEN
sec = 1;
ELSE
sec = res.sec;
END IF;
res.numscan_per_sec = (a.numscan - b.numscan)::double precision / sec;
res.tup_returned_per_sec = (a.tup_returned - b.tup_returned)::double precision / sec;
res.tup_fetched_per_sec = (a.tup_fetched - b.tup_fetched)::double precision / sec;
res.n_tup_ins_per_sec = (a.n_tup_ins - b.n_tup_ins)::double precision / sec;
res.n_tup_upd_per_sec = (a.n_tup_upd - b.n_tup_upd)::double precision / sec;
res.n_tup_del_per_sec = (a.n_tup_del - b.n_tup_del)::double precision / sec;
res.n_tup_hot_upd_per_sec = (a.n_tup_hot_upd - b.n_tup_hot_upd)::double precision / sec;
res.n_liv_tup_per_sec = (a.n_liv_tup - b.n_liv_tup)::double precision / sec;
res.n_dead_tup_per_sec = (a.n_dead_tup - b.n_dead_tup)::double precision / sec;
res.n_mod_since_analyze_per_sec = (a.n_mod_since_analyze - b.n_mod_since_analyze)::double precision / sec;
res.blks_read_per_sec = (a.blks_read - b.blks_read)::double precision / sec;
res.blks_hit_per_sec = (a.blks_hit - b.blks_hit)::double precision / sec;
res.vacuum_count_per_sec = (a.vacuum_count - b.vacuum_count)::double precision / sec;
res.autovacuum_count_per_sec = (a.autovacuum_count - b.autovacuum_count)::double precision / sec;
res.analyze_count_per_sec = (a.analyze_count - b.analyze_count)::double precision / sec;
res.autoanalyze_count_per_sec = (a.autoanalyze_count - b.autoanalyze_count)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR / (
PROCEDURE = powa_all_relations_history_div,
LEFTARG = powa_all_relations_history_record,
RIGHTARG = powa_all_relations_history_record
);
/* end of pg_stat_all_relations operator support */
CREATE TABLE powa_last_aggregation (
aggts timestamp with time zone
);
INSERT INTO powa_last_aggregation(aggts) VALUES (current_timestamp);
CREATE TABLE powa_last_purge (
purgets timestamp with time zone
);
INSERT INTO powa_last_purge (purgets) VALUES (current_timestamp);
CREATE TABLE powa_statements (
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
query text NOT NULL
);
ALTER TABLE ONLY powa_statements
ADD CONSTRAINT powa_statements_pkey PRIMARY KEY (queryid, dbid, userid);
CREATE INDEX powa_statements_dbid_idx ON powa_statements(dbid);
CREATE INDEX powa_statements_userid_idx ON powa_statements(userid);
CREATE TABLE powa_statements_history (
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
coalesce_range tstzrange NOT NULL,
records powa_statements_history_record[] NOT NULL,
mins_in_range powa_statements_history_record NOT NULL,
maxs_in_range powa_statements_history_record NOT NULL
);
CREATE INDEX powa_statements_history_query_ts ON powa_statements_history USING gist (queryid, coalesce_range);
CREATE TABLE powa_statements_history_db (
dbid oid NOT NULL,
coalesce_range tstzrange NOT NULL,
records powa_statements_history_record[] NOT NULL,
mins_in_range powa_statements_history_record NOT NULL,
maxs_in_range powa_statements_history_record NOT NULL
);
CREATE INDEX powa_statements_history_db_ts ON powa_statements_history_db USING gist (dbid, coalesce_range);
CREATE TABLE powa_statements_history_current (
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
record powa_statements_history_record NOT NULL
);
CREATE TABLE powa_statements_history_current_db (
dbid oid NOT NULL,
record powa_statements_history_record NOT NULL
);
CREATE TABLE powa_user_functions_history (
dbid oid NOT NULL,
funcid oid NOT NULL,
coalesce_range tstzrange NOT NULL,
records powa_user_functions_history_record[] NOT NULL,
mins_in_range powa_user_functions_history_record NOT NULL,
maxs_in_range powa_user_functions_history_record NOT NULL
);
CREATE INDEX powa_user_functions_history_funcid_ts ON powa_user_functions_history USING gist (funcid, coalesce_range);
CREATE TABLE powa_user_functions_history_current (
dbid oid NOT NULL,
funcid oid NOT NULL,
record powa_user_functions_history_record NOT NULL
);
CREATE TABLE powa_all_relations_history (
dbid oid NOT NULL,
relid oid NOT NULL,
coalesce_range tstzrange NOT NULL,
records powa_all_relations_history_record[] NOT NULL,
mins_in_range powa_all_relations_history_record NOT NULL,
maxs_in_range powa_all_relations_history_record NOT NULL
);
CREATE INDEX powa_all_relations_history_relid_ts ON powa_all_relations_history USING gist (relid, coalesce_range);
CREATE TABLE powa_all_relations_history_current (
dbid oid NOT NULL,
relid oid NOT NULL,
record powa_all_relations_history_record NOT NULL
);
CREATE SEQUENCE powa_coalesce_sequence INCREMENT BY 1
START WITH 1
CYCLE;
CREATE TABLE powa_functions (
module text NOT NULL,
operation text NOT NULL,
function_name text NOT NULL,
added_manually boolean NOT NULL default true,
enabled boolean NOT NULL default true,
CHECK (operation IN ('snapshot','aggregate','purge','unregister','reset'))
);
INSERT INTO powa_functions (module, operation, function_name, added_manually, enabled) VALUES
('pg_stat_statements', 'snapshot', 'powa_statements_snapshot', false, true),
('powa_stat_user_functions', 'snapshot', 'powa_user_functions_snapshot', false, true),
('powa_stat_all_relations', 'snapshot', 'powa_all_relations_snapshot', false, true),
('pg_stat_statements', 'aggregate','powa_statements_aggregate', false, true),
('powa_stat_user_functions', 'aggregate','powa_user_functions_aggregate', false, true),
('powa_stat_all_relations', 'aggregate','powa_all_relations_aggregate', false, true),
('pg_stat_statements', 'purge', 'powa_statements_purge', false, true),
('powa_stat_user_functions', 'purge', 'powa_user_functions_purge', false, true),
('powa_stat_all_relations', 'purge', 'powa_all_relations_purge', false, true),
('pg_stat_statements', 'reset', 'powa_statements_reset', false, true),
('powa_stat_user_functions', 'reset', 'powa_user_functions_reset', false, true),
('powa_stat_all_relations', 'reset', 'powa_all_relations_reset', false, true);
CREATE FUNCTION powa_log (msg text) RETURNS void
LANGUAGE plpgsql
AS $_$
BEGIN
IF current_setting('powa.debug')::bool THEN
RAISE WARNING '%', msg;
ELSE
RAISE DEBUG '%', msg;
END IF;
END;
$_$;
/* pg_stat_kcache integration - part 1 */
CREATE TYPE public.kcache_type AS (
ts timestamptz,
reads bigint,
writes bigint,
user_time double precision,
system_time double precision
);
/* pg_stat_kcache operator support */
CREATE TYPE powa_kcache_diff AS (
intvl interval,
reads bigint,
writes bigint,
user_time double precision,
system_time double precision
);
CREATE OR REPLACE FUNCTION powa_kcache_mi(
a kcache_type,
b kcache_type)
RETURNS powa_kcache_diff AS
$_$
DECLARE
res powa_kcache_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.reads = a.reads - b.reads;
res.writes = a.writes - b.writes;
res.user_time = a.user_time - b.user_time;
res.system_time = a.system_time - b.system_time;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR - (
PROCEDURE = powa_kcache_mi,
LEFTARG = kcache_type,
RIGHTARG = kcache_type
);
CREATE TYPE powa_kcache_rate AS (
sec integer,
reads_per_sec double precision,
writes_per_sec double precision,
user_time_per_sec double precision,
system_time_per_sec double precision
);
CREATE OR REPLACE FUNCTION powa_kcache_div(
a kcache_type,
b kcache_type)
RETURNS powa_kcache_rate AS
$_$
DECLARE
res powa_kcache_rate;
sec integer;
BEGIN
res.sec = extract(EPOCH FROM (a.ts - b.ts));
IF res.sec = 0 THEN
sec = 1;
ELSE
sec = res.sec;
END IF;
res.reads_per_sec = (a.reads - b.reads)::double precision / sec;
res.writes_per_sec = (a.writes - b.writes)::double precision / sec;
res.user_time_per_sec = (a.user_time - b.user_time)::double precision / sec;
res.system_time_per_sec = (a.system_time - b.system_time)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR / (
PROCEDURE = powa_kcache_div,
LEFTARG = kcache_type,
RIGHTARG = kcache_type
);
/* end of pg_stat_kcache operator support */
CREATE TABLE public.powa_kcache_metrics (
coalesce_range tstzrange NOT NULL,
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
metrics public.kcache_type[] NOT NULL,
mins_in_range public.kcache_type NOT NULL,
maxs_in_range public.kcache_type NOT NULL,
PRIMARY KEY (coalesce_range, queryid, dbid, userid)
);
CREATE INDEX ON public.powa_kcache_metrics (queryid);
CREATE TABLE public.powa_kcache_metrics_db (
coalesce_range tstzrange NOT NULL,
dbid oid NOT NULL,
metrics public.kcache_type[] NOT NULL,
mins_in_range public.kcache_type NOT NULL,
maxs_in_range public.kcache_type NOT NULL,
PRIMARY KEY (coalesce_range, dbid)
);
CREATE TABLE public.powa_kcache_metrics_current (
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
metrics kcache_type NULL NULL
);
CREATE TABLE public.powa_kcache_metrics_current_db (
dbid oid NOT NULL,
metrics kcache_type NULL NULL
);
/* end of pg_stat_kcache integration - part 1 */
/* pg_qualstats integration - part 1 */
CREATE TYPE public.qual_type AS (
relid oid,
attnum integer,
opno oid,
eval_type "char"
);
CREATE TYPE public.qual_values AS (
constants text[],
occurences bigint,
execution_count bigint,
nbfiltered bigint
);
CREATE TYPE powa_qualstats_history_item AS (
ts timestamptz,
occurences bigint,
execution_count bigint,
nbfiltered bigint
);
/* pg_qualstats operator support */
CREATE TYPE powa_qualstats_history_diff AS (
intvl interval,
occurences bigint,
execution_count bigint,
nbfiltered bigint
);
CREATE OR REPLACE FUNCTION powa_qualstats_history_mi(
a powa_qualstats_history_item,
b powa_qualstats_history_item)
RETURNS powa_qualstats_history_diff AS
$_$
DECLARE
res powa_qualstats_history_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.occurences = a.occurences - b.occurences;
res.execution_count = a.execution_count - b.execution_count;
res.nbfiltered = a.nbfiltered - b.nbfiltered;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR - (
PROCEDURE = powa_qualstats_history_mi,
LEFTARG = powa_qualstats_history_item,
RIGHTARG = powa_qualstats_history_item
);
CREATE TYPE powa_qualstats_history_rate AS (
sec integer,
occurences_per_sec double precision,
execution_count_per_sec double precision,
nbfiltered_per_sec double precision
);
CREATE OR REPLACE FUNCTION powa_qualstats_history_div(
a powa_qualstats_history_item,
b powa_qualstats_history_item)
RETURNS powa_qualstats_history_rate AS
$_$
DECLARE
res powa_qualstats_history_rate;
sec integer;
BEGIN
res.sec = extract(EPOCH FROM (a.ts - b.ts));
IF res.sec = 0 THEN
sec = 1;
ELSE
sec = res.sec;
END IF;
res.occurences_per_sec = (a.occurences - b.occurences)::double precision / sec;
res.execution_count_per_sec = (a.execution_count - b.execution_count)::double precision / sec;
res.nbfiltered_per_sec = (a.nbfiltered - b.nbfiltered)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR / (
PROCEDURE = powa_qualstats_history_div,
LEFTARG = powa_qualstats_history_item,
RIGHTARG = powa_qualstats_history_item
);
/* end of pg_qualstats operator support */
CREATE TABLE public.powa_qualstats_quals (
qualid bigint,
queryid bigint,
dbid oid,
userid oid,
quals public.qual_type[],
PRIMARY KEY (qualid, queryid, dbid, userid),
FOREIGN KEY (queryid, dbid, userid) REFERENCES powa_statements(queryid, dbid, userid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE public.powa_qualstats_quals_history (
qualid bigint,
queryid bigint,
dbid oid,
userid oid,
coalesce_range tstzrange,
records powa_qualstats_history_item[],
mins_in_range powa_qualstats_history_item,
maxs_in_range powa_qualstats_history_item,
FOREIGN KEY (qualid, queryid, dbid, userid) REFERENCES public.powa_qualstats_quals (qualid, queryid, dbid, userid) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE public.powa_qualstats_quals_history_current (
qualid bigint,
queryid bigint,
dbid oid,
userid oid,
ts timestamptz,
occurences bigint,
execution_count bigint,
nbfiltered bigint,
FOREIGN KEY (qualid, queryid, dbid, userid) REFERENCES powa_qualstats_quals(qualid, queryid, dbid, userid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE public.powa_qualstats_constvalues_history (
qualid bigint,
queryid bigint,
dbid oid,
userid oid,
coalesce_range tstzrange,
most_used qual_values[],
most_filtering qual_values[],
least_filtering qual_values[],
most_executed qual_values[],
FOREIGN KEY (qualid, queryid, dbid, userid) REFERENCES public.powa_qualstats_quals (qualid, queryid, dbid, userid) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE public.powa_qualstats_constvalues_history_current (
qualid bigint,
queryid bigint,
dbid oid,
userid oid,
ts timestamptz,
constvalues text[],
occurences bigint,
execution_count bigint,
nbfiltered bigint,
FOREIGN KEY (qualid, queryid, dbid, userid) REFERENCES public.powa_qualstats_quals (qualid, queryid, dbid, userid) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX ON powa_qualstats_constvalues_history USING gist (queryid, qualid, coalesce_range);
CREATE INDEX ON powa_qualstats_constvalues_history (qualid, queryid);
CREATE INDEX ON powa_qualstats_quals(queryid);
/* end of pg_qualstats_integration - part 1 */
/* pg_wait_sampling integration - part 1 */
CREATE TYPE public.wait_sampling_type AS (
ts timestamptz,
count bigint
);
/* pg_wait_sampling operator support */
CREATE TYPE wait_sampling_diff AS (
intvl interval,
count bigint
);
CREATE OR REPLACE FUNCTION wait_sampling_mi(
a wait_sampling_type,
b wait_sampling_type)
RETURNS wait_sampling_diff AS
$_$
DECLARE
res wait_sampling_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.count = a.count - b.count;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR - (
PROCEDURE = wait_sampling_mi,
LEFTARG = wait_sampling_type,
RIGHTARG = wait_sampling_type
);
CREATE TYPE wait_sampling_rate AS (
sec integer,
count_per_sec double precision
);
CREATE OR REPLACE FUNCTION wait_sampling_div(
a wait_sampling_type,
b wait_sampling_type)
RETURNS wait_sampling_rate AS
$_$
DECLARE
res wait_sampling_rate;
sec integer;
BEGIN
res.sec = extract(EPOCH FROM (a.ts - b.ts));
IF res.sec = 0 THEN
sec = 1;
ELSE
sec = res.sec;
END IF;
res.count_per_sec = (a.count - b.count)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR / (
PROCEDURE = wait_sampling_div,
LEFTARG = wait_sampling_type,
RIGHTARG = wait_sampling_type
);
/* end of pg_wait_sampling operator support */
CREATE TABLE public.powa_wait_sampling_history (
coalesce_range tstzrange NOT NULL,
queryid bigint NOT NULL,
dbid oid NOT NULL,
event_type text NOT NULL,
event text NOT NULL,
records public.wait_sampling_type[] NOT NULL,
mins_in_range public.wait_sampling_type NOT NULL,
maxs_in_range public.wait_sampling_type NOT NULL,
PRIMARY KEY (coalesce_range, queryid, dbid, event_type, event)
);
CREATE INDEX ON public.powa_wait_sampling_history (queryid);
CREATE TABLE public.powa_wait_sampling_history_db (
coalesce_range tstzrange NOT NULL,
dbid oid NOT NULL,
event_type text NOT NULL,
event text NOT NULL,
records public.wait_sampling_type[] NOT NULL,
mins_in_range public.wait_sampling_type NOT NULL,
maxs_in_range public.wait_sampling_type NOT NULL,
PRIMARY KEY (coalesce_range, dbid, event_type, event)
);
CREATE TABLE public.powa_wait_sampling_history_current (
queryid bigint NOT NULL,
dbid oid NOT NULL,
event_type text NOT NULL,
event text NOT NULL,
record wait_sampling_type NOT NULL
);
CREATE TABLE public.powa_wait_sampling_history_current_db (
dbid oid NOT NULL,
event_type text NOT NULL,
event text NOT NULL,
record wait_sampling_type NOT NULL
);
/* end of pg_wait_sampling integration - part 1 */
-- Mark all of powa's tables as "to be dumped"
SELECT pg_catalog.pg_extension_config_dump('powa_statements','');
SELECT pg_catalog.pg_extension_config_dump('powa_statements_history','');
SELECT pg_catalog.pg_extension_config_dump('powa_statements_history_db','');
SELECT pg_catalog.pg_extension_config_dump('powa_statements_history_current','');
SELECT pg_catalog.pg_extension_config_dump('powa_statements_history_current_db','');
SELECT pg_catalog.pg_extension_config_dump('powa_user_functions_history','');
SELECT pg_catalog.pg_extension_config_dump('powa_user_functions_history_current','');
SELECT pg_catalog.pg_extension_config_dump('powa_all_relations_history','');
SELECT pg_catalog.pg_extension_config_dump('powa_all_relations_history_current','');
SELECT pg_catalog.pg_extension_config_dump('powa_functions','WHERE added_manually');
SELECT pg_catalog.pg_extension_config_dump('powa_kcache_metrics','');
SELECT pg_catalog.pg_extension_config_dump('powa_kcache_metrics_db','');
SELECT pg_catalog.pg_extension_config_dump('powa_kcache_metrics_current','');
SELECT pg_catalog.pg_extension_config_dump('powa_kcache_metrics_current_db','');
SELECT pg_catalog.pg_extension_config_dump('powa_qualstats_quals','');
SELECT pg_catalog.pg_extension_config_dump('powa_qualstats_quals_history','');
SELECT pg_catalog.pg_extension_config_dump('powa_qualstats_quals_history_current','');
SELECT pg_catalog.pg_extension_config_dump('powa_qualstats_constvalues_history','');
SELECT pg_catalog.pg_extension_config_dump('powa_qualstats_constvalues_history_current','');
SELECT pg_catalog.pg_extension_config_dump('powa_wait_sampling_history','');
SELECT pg_catalog.pg_extension_config_dump('powa_wait_sampling_history_db','');
SELECT pg_catalog.pg_extension_config_dump('powa_wait_sampling_history_current','');
SELECT pg_catalog.pg_extension_config_dump('powa_wait_sampling_history_current_db','');
CREATE OR REPLACE FUNCTION public.powa_check_created_extensions()
RETURNS event_trigger
LANGUAGE plpgsql
AS $_$
DECLARE
BEGIN
/* We have for now no way for a proper handling of this event,
* as we don't have a table with the list of supported extensions.
* So just call every powa_*_register() function we know each time an
* extension is created. Powa should be in a dedicated database and the
* register function handle to be called several time, so it's not critical
*/
PERFORM public.powa_kcache_register();
PERFORM public.powa_qualstats_register();
PERFORM public.powa_track_settings_register();
PERFORM public.powa_wait_sampling_register();
END;
$_$; /* end of powa_check_created_extensions */
CREATE EVENT TRIGGER powa_check_created_extensions
ON ddl_command_end
WHEN tag IN ('CREATE EXTENSION')
EXECUTE PROCEDURE public.powa_check_created_extensions() ;
CREATE OR REPLACE FUNCTION public.powa_check_dropped_extensions()
RETURNS event_trigger
LANGUAGE plpgsql
AS $_$
DECLARE
funcname text;
v_state text;
v_msg text;
v_detail text;
v_hint text;
v_context text;
BEGIN
-- We unregister extensions regardless the "enabled" field