-
Notifications
You must be signed in to change notification settings - Fork 20
/
powa--4.0.1--4.1.0.sql
1554 lines (1432 loc) · 70.9 KB
/
powa--4.0.1--4.1.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 "ALTER EXTENSION powa" to load this file. \quit
ALTER TABLE public.powa_statements ADD last_present_ts timestamptz NULL DEFAULT now();
--- Create a performance index to speed up clean up process
CREATE INDEX powa_statements_mru_idx ON powa_statements (last_present_ts);
CREATE OR REPLACE FUNCTION powa_qualstats_snapshot(_srvid integer) RETURNS void as $PROC$
DECLARE
result bool;
v_funcname text := 'powa_qualstats_snapshot';
v_rowcount bigint;
BEGIN
PERFORM powa_log(format('running %I', v_funcname));
PERFORM powa_prevent_concurrent_snapshot(_srvid);
WITH capture AS (
SELECT *
FROM powa_qualstats_src(_srvid) q
WHERE EXISTS (SELECT 1
FROM powa_statements s
WHERE s.srvid = _srvid
AND q.queryid = s.queryid
AND q.dbid = s.dbid
AND q.userid = s.dbid)
),
missing_quals AS (
INSERT INTO public.powa_qualstats_quals (srvid, qualid, queryid, dbid, userid, quals)
SELECT DISTINCT _srvid AS srvid, qs.qualnodeid, qs.queryid, qs.dbid, qs.userid,
array_agg(DISTINCT q::qual_type)
FROM capture qs,
LATERAL (SELECT (unnest(quals)).*) as q
WHERE NOT EXISTS (
SELECT 1
FROM powa_qualstats_quals nh
WHERE nh.srvid = _srvid
AND nh.qualid = qs.qualnodeid
AND nh.queryid = qs.queryid
AND nh.dbid = qs.dbid
AND nh.userid = qs.userid
)
GROUP BY srvid, qualnodeid, qs.queryid, qs.dbid, qs.userid
RETURNING *
),
by_qual AS (
INSERT INTO public.powa_qualstats_quals_history_current (srvid, qualid, queryid,
dbid, userid, ts, occurences, execution_count, nbfiltered,
mean_err_estimate_ratio, mean_err_estimate_num)
SELECT _srvid AS srvid, qs.qualnodeid, qs.queryid, qs.dbid, qs.userid,
ts, sum(occurences), sum(execution_count), sum(nbfiltered),
avg(mean_err_estimate_ratio), avg(mean_err_estimate_num)
FROM capture as qs
GROUP BY srvid, ts, qualnodeid, qs.queryid, qs.dbid, qs.userid
RETURNING *
),
by_qual_with_const AS (
INSERT INTO public.powa_qualstats_constvalues_history_current(srvid, qualid,
queryid, dbid, userid, ts, occurences, execution_count, nbfiltered,
mean_err_estimate_ratio, mean_err_estimate_num, constvalues)
SELECT _srvid, qualnodeid, qs.queryid, qs.dbid, qs.userid, ts,
occurences, execution_count, nbfiltered, mean_err_estimate_ratio,
mean_err_estimate_num, constvalues
FROM capture as qs
)
SELECT COUNT(*) into v_rowcount
FROM capture;
perform powa_log(format('%I - rowcount: %s',
v_funcname, v_rowcount));
IF (_srvid != 0) THEN
DELETE FROM powa_qualstats_src_tmp WHERE srvid = _srvid;
END IF;
result := true;
-- pg_qualstats metrics are not accumulated, so we force a reset after every
-- snapshot. For local snapshot this is done here, remote snapshots will
-- rely on the collector doing it through query_cleanup.
IF (_srvid = 0) THEN
PERFORM pg_qualstats_reset();
END IF;
END
$PROC$ language plpgsql; /* end of powa_qualstats_snapshot */
DO $anon$
BEGIN
IF current_setting('server_version_num')::int < 90600 THEN
CREATE FUNCTION public.powa_get_guc (guc text, def text DEFAULT NULL) RETURNS text
LANGUAGE plpgsql
AS $_$
DECLARE
v_val text;
BEGIN
BEGIN
SELECT current_setting(guc) INTO v_val;
EXCEPTION WHEN OTHERS THEN
v_val = def;
END;
RETURN v_val;
END;
$_$;
ELSE
CREATE FUNCTION public.powa_get_guc (guc text, def text DEFAULT NULL) RETURNS text
LANGUAGE plpgsql
AS $_$
BEGIN
RETURN COALESCE(current_setting(guc, true), def);
END;
$_$;
END IF;
END;
$anon$;
CREATE OR REPLACE FUNCTION public.powa_log (msg text) RETURNS void
LANGUAGE plpgsql
AS $_$
BEGIN
IF powa_get_guc('powa.debug', 'false')::bool THEN
RAISE WARNING '%', msg;
ELSE
RAISE DEBUG '%', msg;
END IF;
END;
$_$;
--
-- handle pg_stat_statements transition to 1.8
--
ALTER TYPE powa_statements_history_record RENAME ATTRIBUTE total_time TO total_exec_time CASCADE;
ALTER TYPE powa_statements_history_record ADD ATTRIBUTE plans bigint;
ALTER TYPE powa_statements_history_record ADD ATTRIBUTE total_plan_time double precision;
ALTER TYPE powa_statements_history_record ADD ATTRIBUTE wal_records bigint;
ALTER TYPE powa_statements_history_record ADD ATTRIBUTE wal_fpi bigint;
ALTER TYPE powa_statements_history_record ADD ATTRIBUTE wal_bytes numeric;
ALTER TYPE powa_statements_history_diff RENAME ATTRIBUTE total_time TO total_exec_time CASCADE;
ALTER TYPE powa_statements_history_diff ADD ATTRIBUTE plans bigint;
ALTER TYPE powa_statements_history_diff ADD ATTRIBUTE total_plan_time double precision;
ALTER TYPE powa_statements_history_diff ADD ATTRIBUTE wal_records bigint;
ALTER TYPE powa_statements_history_diff ADD ATTRIBUTE wal_fpi bigint;
ALTER TYPE powa_statements_history_diff ADD ATTRIBUTE wal_bytes numeric;
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_exec_time = a.total_exec_time - b.total_exec_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;
res.plans = a.plans - b.plans;
res.total_plan_time = a.total_plan_time - b.total_plan_time;
res.wal_records = a.wal_records - b.wal_records;
res.wal_fpi = a.wal_fpi - b.wal_fpi;
res.wal_bytes = a.wal_bytes - b.wal_bytes;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
ALTER TYPE powa_statements_history_rate ADD ATTRIBUTE plans_per_sec double precision;
ALTER TYPE powa_statements_history_rate ADD ATTRIBUTE plantime_per_sec double precision;
ALTER TYPE powa_statements_history_rate ADD ATTRIBUTE wal_records_per_sec double precision;
ALTER TYPE powa_statements_history_rate ADD ATTRIBUTE wal_fpi_per_sec double precision;
ALTER TYPE powa_statements_history_rate ADD ATTRIBUTE wal_bytes_per_sec numeric;
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_exec_time - b.total_exec_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;
res.plans_per_sec = (a.plans - b.plans)::double precision / sec;
res.plantime_per_sec = (a.total_plan_time - b.total_plan_time)::double precision / sec;
res.wal_records_per_sec = (a.wal_records - b.wal_records)::double precision / sec;
res.wal_fpi_per_sec = (a.wal_fpi - b.wal_fpi)::double precision / sec;
res.wal_bytes_per_sec = (a.wal_bytes - b.wal_bytes)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
TRUNCATE TABLE public.powa_statements_src_tmp;
ALTER TABLE public.powa_statements_src_tmp RENAME total_time TO total_exec_time;
ALTER TABLE public.powa_statements_src_tmp ADD plans bigint NOT NULL;
ALTER TABLE public.powa_statements_src_tmp ADD total_plan_time double precision NOT NULL;
ALTER TABLE public.powa_statements_src_tmp ADD wal_records bigint NOT NULL;
ALTER TABLE public.powa_statements_src_tmp ADD wal_fpi bigint NOT NULL;
ALTER TABLE public.powa_statements_src_tmp ADD wal_bytes numeric NOT NULL;
DROP FUNCTION powa_statements_src(integer);
CREATE OR REPLACE FUNCTION powa_statements_src(IN _srvid integer,
OUT ts timestamp with time zone,
OUT userid oid,
OUT dbid oid,
OUT queryid bigint,
OUT query text,
OUT calls bigint,
OUT total_exec_time double precision,
OUT rows bigint,
OUT shared_blks_hit bigint,
OUT shared_blks_read bigint,
OUT shared_blks_dirtied bigint,
OUT shared_blks_written bigint,
OUT local_blks_hit bigint,
OUT local_blks_read bigint,
OUT local_blks_dirtied bigint,
OUT local_blks_written bigint,
OUT temp_blks_read bigint,
OUT temp_blks_written bigint,
OUT blk_read_time double precision,
OUT blk_write_time double precision,
OUT plans bigint,
OUT total_plan_time float8,
OUT wal_records bigint,
OUT wal_fpi bigint,
OUT wal_bytes numeric
)
RETURNS SETOF record
STABLE
AS $PROC$
DECLARE
v_pgss integer[];
BEGIN
IF (_srvid = 0) THEN
SELECT regexp_split_to_array(extversion, '\.') INTO STRICT v_pgss
FROM pg_extension
WHERE extname = 'pg_stat_statements';
IF (v_pgss[1] = 1 AND v_pgss[2] < 8) THEN
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
pgss.shared_blks_written, pgss.local_blks_hit,
pgss.local_blks_read, pgss.local_blks_dirtied,
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time,pgss.blk_write_time,
0::bigint, 0::double precision,
0::bigint, 0::bigint, 0::numeric
FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
JOIN pg_roles r ON pgss.userid = r.oid
WHERE pgss.query !~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)'
AND NOT (r.rolname = ANY (string_to_array(
powa_get_guc('powa.ignored_users', ''),
',')));
ELSE
RETURN QUERY SELECT now(),
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
pgss.shared_blks_written, pgss.local_blks_hit,
pgss.local_blks_read, pgss.local_blks_dirtied,
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes
FROM pg_stat_statements pgss
JOIN pg_database d ON d.oid = pgss.dbid
JOIN pg_roles r ON pgss.userid = r.oid
WHERE pgss.query !~* '^[[:space:]]*(DEALLOCATE|BEGIN|PREPARE TRANSACTION|COMMIT PREPARED|ROLLBACK PREPARED)'
AND NOT (r.rolname = ANY (string_to_array(
powa_get_guc('powa.ignored_users', ''),
',')));
END IF;
ELSE
RETURN QUERY SELECT pgss.ts,
pgss.userid, pgss.dbid, pgss.queryid, pgss.query,
pgss.calls, pgss.total_exec_time,
pgss.rows, pgss.shared_blks_hit,
pgss.shared_blks_read, pgss.shared_blks_dirtied,
pgss.shared_blks_written, pgss.local_blks_hit,
pgss.local_blks_read, pgss.local_blks_dirtied,
pgss.local_blks_written, pgss.temp_blks_read,
pgss.temp_blks_written, pgss.blk_read_time, pgss.blk_write_time,
pgss.plans, pgss.total_plan_time,
pgss.wal_records, pgss.wal_fpi, pgss.wal_bytes
FROM powa_statements_src_tmp pgss WHERE srvid = _srvid;
END IF;
END;
$PROC$ LANGUAGE plpgsql; /* end of powa_statements_src */
CREATE OR REPLACE FUNCTION powa_statements_snapshot(_srvid integer) RETURNS void AS $PROC$
DECLARE
result boolean;
v_funcname text := 'powa_statements_snapshot';
v_rowcount bigint;
BEGIN
-- In this function, we capture statements, and also aggregate counters by database
-- so that the first screens of powa stay reactive even though there may be thousands
-- of different statements
-- We only capture databases that are still there
PERFORM powa_log(format('running %I', v_funcname));
PERFORM powa_prevent_concurrent_snapshot(_srvid);
WITH capture AS(
SELECT *
FROM powa_statements_src(_srvid)
),
mru as (UPDATE powa_statements set last_present_ts = now()
FROM capture
WHERE powa_statements.queryid = capture.queryid
AND powa_statements.dbid = capture.dbid
AND powa_statements.userid = capture.userid
AND powa_statements.srvid = _srvid
),
missing_statements AS(
INSERT INTO public.powa_statements (srvid, queryid, dbid, userid, query)
SELECT DISTINCT _srvid, queryid, dbid, userid, query
FROM capture c
WHERE NOT EXISTS (SELECT 1
FROM powa_statements ps
WHERE ps.queryid = c.queryid
AND ps.dbid = c.dbid
AND ps.userid = c.userid
AND ps.srvid = _srvid
)
),
by_query AS (
INSERT INTO public.powa_statements_history_current
SELECT _srvid, queryid, dbid, userid,
ROW(
ts, calls, total_exec_time, rows,
shared_blks_hit, shared_blks_read, shared_blks_dirtied,
shared_blks_written, local_blks_hit, local_blks_read,
local_blks_dirtied, local_blks_written, temp_blks_read,
temp_blks_written, blk_read_time, blk_write_time,
plans, total_plan_time,
wal_records, wal_fpi, wal_bytes
)::powa_statements_history_record AS record
FROM capture
),
by_database AS (
INSERT INTO public.powa_statements_history_current_db
SELECT _srvid, dbid,
ROW(
ts, sum(calls),
sum(total_exec_time), sum(rows), sum(shared_blks_hit),
sum(shared_blks_read), sum(shared_blks_dirtied),
sum(shared_blks_written), sum(local_blks_hit),
sum(local_blks_read), sum(local_blks_dirtied),
sum(local_blks_written), sum(temp_blks_read),
sum(temp_blks_written), sum(blk_read_time), sum(blk_write_time),
sum(plans), sum(total_plan_time),
sum(wal_records), sum(wal_fpi), sum(wal_bytes)
)::powa_statements_history_record AS record
FROM capture
GROUP BY dbid, ts
)
SELECT count(*) INTO v_rowcount
FROM capture;
perform powa_log(format('%I - rowcount: %s',
v_funcname, v_rowcount));
IF (_srvid != 0) THEN
DELETE FROM powa_statements_src_tmp WHERE srvid = _srvid;
END IF;
result := true; -- For now we don't care. What could we do on error except crash anyway?
END;
$PROC$ language plpgsql; /* end of powa_statements_snapshot */
CREATE OR REPLACE FUNCTION powa_statements_aggregate(_srvid integer)
RETURNS void AS $PROC$
DECLARE
v_funcname text := 'powa_statements_aggregate(' || _srvid || ')';
v_rowcount bigint;
BEGIN
PERFORM powa_log(format('running %I', v_funcname));
PERFORM powa_prevent_concurrent_snapshot(_srvid);
-- aggregate statements table
INSERT INTO public.powa_statements_history
SELECT srvid, queryid, dbid, userid,
tstzrange(min((record).ts), max((record).ts),'[]'),
array_agg(record),
ROW(min((record).ts),
min((record).calls),min((record).total_exec_time),
min((record).rows),
min((record).shared_blks_hit),min((record).shared_blks_read),
min((record).shared_blks_dirtied),min((record).shared_blks_written),
min((record).local_blks_hit),min((record).local_blks_read),
min((record).local_blks_dirtied),min((record).local_blks_written),
min((record).temp_blks_read),min((record).temp_blks_written),
min((record).blk_read_time),min((record).blk_write_time),
min((record).plans),min((record).total_plan_time),
min((record).wal_records),min((record).wal_fpi),
min((record).wal_bytes)
)::powa_statements_history_record,
ROW(max((record).ts),
max((record).calls),max((record).total_exec_time),
max((record).rows),
max((record).shared_blks_hit),max((record).shared_blks_read),
max((record).shared_blks_dirtied),max((record).shared_blks_written),
max((record).local_blks_hit),max((record).local_blks_read),
max((record).local_blks_dirtied),max((record).local_blks_written),
max((record).temp_blks_read),max((record).temp_blks_written),
max((record).blk_read_time),max((record).blk_write_time),
max((record).plans),max((record).total_plan_time),
max((record).wal_records),max((record).wal_fpi),
max((record).wal_bytes)
)::powa_statements_history_record
FROM powa_statements_history_current
WHERE srvid = _srvid
GROUP BY srvid, queryid, dbid, userid;
GET DIAGNOSTICS v_rowcount = ROW_COUNT;
perform powa_log(format('%I (powa_statements_history) - rowcount: %s',
v_funcname, v_rowcount));
DELETE FROM powa_statements_history_current WHERE srvid = _srvid;
-- aggregate db table
INSERT INTO public.powa_statements_history_db
SELECT srvid, dbid,
tstzrange(min((record).ts), max((record).ts),'[]'),
array_agg(record),
ROW(min((record).ts),
min((record).calls),min((record).total_exec_time),
min((record).rows),
min((record).shared_blks_hit),min((record).shared_blks_read),
min((record).shared_blks_dirtied),min((record).shared_blks_written),
min((record).local_blks_hit),min((record).local_blks_read),
min((record).local_blks_dirtied),min((record).local_blks_written),
min((record).temp_blks_read),min((record).temp_blks_written),
min((record).blk_read_time),min((record).blk_write_time),
min((record).plans),min((record).total_plan_time),
min((record).wal_records),min((record).wal_fpi),
min((record).wal_bytes)
)::powa_statements_history_record,
ROW(max((record).ts),
max((record).calls),max((record).total_exec_time),
max((record).rows),
max((record).shared_blks_hit),max((record).shared_blks_read),
max((record).shared_blks_dirtied),max((record).shared_blks_written),
max((record).local_blks_hit),max((record).local_blks_read),
max((record).local_blks_dirtied),max((record).local_blks_written),
max((record).temp_blks_read),max((record).temp_blks_written),
max((record).blk_read_time),max((record).blk_write_time),
max((record).plans),max((record).total_plan_time),
max((record).wal_records),max((record).wal_fpi),
max((record).wal_bytes)
)::powa_statements_history_record
FROM powa_statements_history_current_db
WHERE srvid = _srvid
GROUP BY srvid, dbid;
GET DIAGNOSTICS v_rowcount = ROW_COUNT;
perform powa_log(format('%I (powa_statements_history_db) - rowcount: %s',
v_funcname, v_rowcount));
DELETE FROM powa_statements_history_current_db WHERE srvid = _srvid;
END;
$PROC$ LANGUAGE plpgsql; /* end of powa_statements_aggregate */
CREATE OR REPLACE FUNCTION powa_statements_purge(_srvid integer)
RETURNS void AS $PROC$
DECLARE
v_funcname text := 'powa_statements_purge(' || _srvid || ')';
v_rowcount bigint;
v_retention interval;
BEGIN
PERFORM powa_log(format('running %I', v_funcname));
PERFORM powa_prevent_concurrent_snapshot(_srvid);
SELECT powa_get_server_retention(_srvid) INTO v_retention;
-- Delete obsolete data. We only bother with already coalesced data
DELETE FROM powa_statements_history
WHERE upper(coalesce_range)< (now() - v_retention)
AND srvid = _srvid;
GET DIAGNOSTICS v_rowcount = ROW_COUNT;
perform powa_log(format('%I (powa_statements_hitory) - rowcount: %s',
v_funcname, v_rowcount));
DELETE FROM powa_statements_history_db
WHERE upper(coalesce_range)< (now() - v_retention)
AND srvid = _srvid;
GET DIAGNOSTICS v_rowcount = ROW_COUNT;
perform powa_log(format('%I (powa_statements_history_db) - rowcount: %s',
v_funcname, v_rowcount));
DELETE FROM powa_statements
WHERE last_present_ts < (now() - v_retention)
AND srvid = _srvid;
GET DIAGNOSTICS v_rowcount = ROW_COUNT;
perform powa_log(format('%I (powa_statements) - rowcount: %s',
v_funcname, v_rowcount));
END;
$PROC$ LANGUAGE plpgsql; /* end of powa_statements_purge */
-- automatically configure powa for local snapshot if supported extension are
-- created locally
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_activate_extension(0, 'pg_stat_kcache');
PERFORM public.powa_activate_extension(0, 'pg_qualstats');
PERFORM public.powa_activate_extension(0, 'pg_track_settings');
PERFORM public.powa_activate_extension(0, 'pg_wait_sampling');
END;
$_$; /* end of powa_check_created_extensions */
-- automatically remove extensions from local snapshot if supported extension
-- is removed locally
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
WITH ext AS (
SELECT object_name
FROM pg_event_trigger_dropped_objects() d
WHERE d.object_type = 'extension'
)
SELECT function_name INTO funcname
FROM powa_functions f
JOIN ext ON f.module = ext.object_name
WHERE operation = 'unregister'
ORDER BY module;
IF ( funcname IS NOT NULL ) THEN
BEGIN
PERFORM powa_log(format('running %I', funcname));
EXECUTE 'SELECT ' || quote_ident(funcname) || '(0)';
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS
v_state = RETURNED_SQLSTATE,
v_msg = MESSAGE_TEXT,
v_detail = PG_EXCEPTION_DETAIL,
v_hint = PG_EXCEPTION_HINT,
v_context = PG_EXCEPTION_CONTEXT;
RAISE WARNING 'powa_check_dropped_extensions(): function "%" failed:
state : %
message: %
detail : %
hint : %
context: %', funcname, v_state, v_msg, v_detail, v_hint, v_context;
END;
END IF;
END;
$_$; /* end of powa_check_dropped_extensions */
ALTER TABLE public.powa_servers ADD version text;
ALTER TABLE public.powa_functions ADD extname text;
UPDATE public.powa_functions SET extname = module WHERE module != 'pg_stat_bgwriter';
UPDATE public.powa_functions SET extname = 'powa' WHERE extname LIKE 'powa%';
CREATE TABLE public.powa_extensions (
srvid integer,
extname text,
version text,
PRIMARY KEY (srvid, extname),
FOREIGN KEY (srvid) REFERENCES public.powa_servers (id)
);
INSERT INTO public.powa_extensions (srvid, extname)
SELECT DISTINCT srvid,
CASE WHEN module LIKE 'powa%' THEN 'powa' ELSE module END
FROM public.powa_functions
WHERE module != 'pg_stat_bgwriter';
ALTER TABLE public.powa_functions
ADD FOREIGN KEY (srvid, extname)
REFERENCES public.powa_extensions (srvid, extname)
ON UPDATE CASCADE ON DELETE CASCADE;
DROP FUNCTION public.powa_activate_extension(integer, text);
-- Register the module if needed, and set the enabled flag to on. This
-- function should only be callsed by powa_register_server.
CREATE OR REPLACE FUNCTION public.powa_activate_extension(_srvid integer, _module text) RETURNS boolean
AS $_$
DECLARE
v_ext_registered boolean;
v_manually boolean;
v_found boolean;
v_extname text;
BEGIN
SELECT COUNT(*) > 0 INTO v_ext_registered
FROM powa_functions
WHERE module = _module
AND srvid = _srvid;
IF (_module LIKE 'powa%') THEN
v_extname = 'powa';
ELSIF (_module = 'pg_stat_bgwriter') THEN
v_extname = NULL;
ELSE
v_extname = _module;
END IF;
-- the rows may already be present, but the enabled flag could be off,
-- so enabled it everywhere it's disabled. We don't check for other cases,
-- for instance if part of the needed rows were deleted.
IF (v_ext_registered) THEN
UPDATE powa_functions
SET enabled = true
WHERE enabled = false
AND srvid = _srvid
AND module = _module;
RETURN true;
END IF;
-- Add the row in powa_extensions if needed. Note that since we add the
-- row before knowing if it's a supported extension, we may have to remove
-- it later.
IF (v_extname IS NOT NULL) THEN
SELECT COUNT(*) = 1 INTO v_found
FROM public.powa_extensions
WHERE srvid = _srvid
AND extname = v_extname;
IF NOT v_found THEN
INSERT INTO public.powa_extensions (srvid, extname)
VALUES (_srvid, v_extname);
END IF;
END IF;
-- default extensions for non-local server have to be dumped
SELECT _srvid != 0 INTO v_manually;
IF (_module = 'pg_stat_statements') THEN
INSERT INTO public.powa_functions(srvid, extname, module, operation, function_name,
query_source, added_manually, enabled, priority)
VALUES
(_srvid, 'pg_stat_statements', 'pg_stat_statements', 'snapshot', 'powa_databases_snapshot', 'powa_databases_src', v_manually, true, -1),
(_srvid, 'pg_stat_statements', 'pg_stat_statements', 'snapshot', 'powa_statements_snapshot', 'powa_statements_src', v_manually, true, default),
(_srvid, 'pg_stat_statements', 'pg_stat_statements', 'aggregate', 'powa_statements_aggregate', NULL, v_manually, true, default),
(_srvid, 'pg_stat_statements', 'pg_stat_statements', 'purge', 'powa_statements_purge', NULL, v_manually, true, default),
(_srvid, 'pg_stat_statements', 'pg_stat_statements', 'purge', 'powa_databases_purge', NULL, v_manually, true, default),
(_srvid, 'pg_stat_statements', 'pg_stat_statements', 'reset', 'powa_statements_reset', NULL, v_manually, true, default);
ELSIF (_module = 'powa_stat_user_functions') THEN
INSERT INTO public.powa_functions(srvid, extname, module, operation, function_name,
query_source, added_manually, enabled, priority)
VALUES
(_srvid, 'powa', 'powa_stat_user_functions', 'snapshot', 'powa_user_functions_snapshot', 'powa_user_functions_src', v_manually, true, default),
(_srvid, 'powa', 'powa_stat_user_functions', 'aggregate', 'powa_user_functions_aggregate', NULL, v_manually, true, default),
(_srvid, 'powa', 'powa_stat_user_functions', 'purge', 'powa_user_functions_purge', NULL, v_manually, true, default),
(_srvid, 'powa', 'powa_stat_user_functions', 'reset', 'powa_user_functions_reset', NULL, v_manually, true, default);
ELSIF (_module = 'powa_stat_all_relations') THEN
INSERT INTO public.powa_functions(srvid, extname, module, operation, function_name,
query_source, added_manually, enabled, priority)
VALUES
(_srvid, 'powa', 'powa_stat_all_relations', 'snapshot', 'powa_all_relations_snapshot', 'powa_all_relations_src', v_manually, true, default),
(_srvid, 'powa', 'powa_stat_all_relations', 'aggregate', 'powa_all_relations_aggregate', NULL, v_manually, true, default),
(_srvid, 'powa', 'powa_stat_all_relations', 'purge', 'powa_all_relations_purge', NULL, v_manually, true, default),
(_srvid, 'powa', 'powa_stat_all_relations', 'reset', 'powa_all_relations_reset', NULL, v_manually, true, default);
ELSIF (_module = 'pg_stat_bgwriter') THEN
INSERT INTO public.powa_functions(srvid, extname, module, operation, function_name,
query_source, added_manually, enabled, priority)
VALUES
(_srvid, NULL, 'pg_stat_bgwriter', 'snapshot', 'powa_stat_bgwriter_snapshot', 'powa_stat_bgwriter_src', v_manually, true, default),
(_srvid, NULL, 'pg_stat_bgwriter', 'aggregate', 'powa_stat_bgwriter_aggregate', NULL, v_manually, true, default),
(_srvid, NULL, 'pg_stat_bgwriter', 'purge', 'powa_stat_bgwriter_purge', NULL, v_manually, true, default),
(_srvid, NULL, 'pg_stat_bgwriter', 'reset', 'powa_stat_bgwriter_reset', NULL, v_manually, true, default);
ELSIF (_module = 'pg_stat_kcache') THEN
RETURN powa_kcache_register(_srvid);
ELSIF (_module = 'pg_qualstats') THEN
RETURN powa_qualstats_register(_srvid);
ELSIF (_module = 'pg_wait_sampling') THEN
RETURN powa_wait_sampling_register(_srvid);
ELSIF (_module = 'pg_track_settings') THEN
RETURN powa_track_settings_register(_srvid);
ELSE
-- remove the previously added row in powa_extensions
IF (v_extname IS NOT NULL) THEN
DELETE FROM public.powa_extensions
WHERE srvid = _srvid AND extname = v_extname;
END IF;
RETURN false;
END IF;
return true;
END;
$_$ LANGUAGE plpgsql; /* end of powa_activate_extension */
/*
* register pg_stat_kcache extension
*/
CREATE OR REPLACE function public.powa_kcache_register(_srvid integer = 0) RETURNS bool AS
$_$
DECLARE
v_func_present bool;
v_ext_present bool;
BEGIN
-- Only check for extension availability for local server
IF (_srvid = 0) THEN
SELECT COUNT(*) = 1 INTO v_ext_present
FROM pg_extension
WHERE extname = 'pg_stat_kcache';
ELSE
v_ext_present = true;
END IF;
IF ( v_ext_present ) THEN
SELECT COUNT(*) > 0 INTO v_func_present
FROM public.powa_functions
WHERE module = 'pg_stat_kcache'
AND srvid = _srvid;
IF ( NOT v_func_present) THEN
PERFORM powa_log('registering pg_stat_kcache');
INSERT INTO public.powa_functions (srvid, extname, module, operation, function_name, query_source, added_manually, enabled, priority)
VALUES (_srvid, 'pg_stat_kcache', 'pg_stat_kcache', 'snapshot', 'powa_kcache_snapshot', 'powa_kcache_src', true, true, -1),
(_srvid, 'pg_stat_kcache', 'pg_stat_kcache', 'aggregate', 'powa_kcache_aggregate', NULL, true, true, default),
(_srvid, 'pg_stat_kcache', 'pg_stat_kcache', 'unregister', 'powa_kcache_unregister', NULL, true, true, default),
(_srvid, 'pg_stat_kcache', 'pg_stat_kcache', 'purge', 'powa_kcache_purge', NULL, true, true, default),
(_srvid, 'pg_stat_kcache', 'pg_stat_kcache', 'reset', 'powa_kcache_reset', NULL, true, true, default);
END IF;
END IF;
RETURN true;
END;
$_$
language plpgsql; /* end of powa_kcache_register */
/*
* powa_qualstats_register
*/
CREATE OR REPLACE function public.powa_qualstats_register(_srvid integer = 0) RETURNS bool AS
$_$
DECLARE
v_func_present bool;
v_ext_present bool;
BEGIN
IF (_srvid = 0) THEN
SELECT COUNT(*) = 1 INTO v_ext_present
FROM pg_extension
WHERE extname = 'pg_qualstats';
ELSE
v_ext_present = true;
END IF;
IF ( v_ext_present) THEN
SELECT COUNT(*) > 0 INTO v_func_present
FROM public.powa_functions
WHERE module = 'pg_qualstats'
AND srvid = _srvid;
IF ( NOT v_func_present) THEN
PERFORM powa_log('registering pg_qualstats');
INSERT INTO public.powa_functions (srvid, extname, module, operation, function_name, query_source, query_cleanup, added_manually, enabled)
VALUES (_srvid, 'pg_qualstats', 'pg_qualstats', 'snapshot', 'powa_qualstats_snapshot', 'powa_qualstats_src', 'SELECT pg_qualstats_reset()', true, true),
(_srvid, 'pg_qualstats', 'pg_qualstats', 'aggregate', 'powa_qualstats_aggregate', NULL, NULL, true, true),
(_srvid, 'pg_qualstats', 'pg_qualstats', 'unregister', 'powa_qualstats_unregister', NULL, NULL, true, true),
(_srvid, 'pg_qualstats', 'pg_qualstats', 'purge', 'powa_qualstats_purge', NULL, NULL, true, true),
(_srvid, 'pg_qualstats', 'pg_qualstats', 'reset', 'powa_qualstats_reset', NULL, NULL, true, true);
END IF;
END IF;
RETURN true;
END;
$_$
language plpgsql; /* end of powa_qualstats_register */
/*
* register pg_wait_sampling extension
*/
CREATE OR REPLACE function public.powa_wait_sampling_register(_srvid integer = 0) RETURNS bool AS
$_$
DECLARE
v_func_present bool;
v_ext_present bool;
BEGIN
-- Only check for extension availability for local server
IF (_srvid = 0) THEN
SELECT COUNT(*) = 1 INTO v_ext_present
FROM pg_extension
WHERE extname = 'pg_wait_sampling';
ELSE
v_ext_present = true;
END IF;
IF ( v_ext_present ) THEN
SELECT COUNT(*) > 0 INTO v_func_present
FROM public.powa_functions
WHERE module = 'pg_wait_sampling'
AND srvid = _srvid;
IF ( NOT v_func_present) THEN
PERFORM powa_log('registering pg_wait_sampling');
INSERT INTO public.powa_functions (srvid, extname, module, operation, function_name, query_source, added_manually, enabled)
VALUES (_srvid, 'pg_wait_sampling', 'pg_wait_sampling', 'snapshot', 'powa_wait_sampling_snapshot', 'powa_wait_sampling_src', true, true),
(_srvid, 'pg_wait_sampling', 'pg_wait_sampling', 'aggregate', 'powa_wait_sampling_aggregate', NULL, true, true),
(_srvid, 'pg_wait_sampling', 'pg_wait_sampling', 'unregister', 'powa_wait_sampling_unregister', NULL, true, true),
(_srvid, 'pg_wait_sampling', 'pg_wait_sampling', 'purge', 'powa_wait_sampling_purge', NULL, true, true),
(_srvid, 'pg_wait_sampling', 'pg_wait_sampling', 'reset', 'powa_wait_sampling_reset', NULL, true, true);
END IF;
END IF;
RETURN true;
END;
$_$
language plpgsql; /* end of powa_wait_sampling_register */
CREATE OR REPLACE FUNCTION powa_track_settings_register(_srvid integer = 0) RETURNS bool AS $_$
DECLARE
v_func_present bool;
v_ext_present bool;
BEGIN
IF (_srvid = 0) THEN
SELECT COUNT(*) = 1 INTO v_ext_present
FROM pg_extension
WHERE extname = 'pg_track_settings';
ELSE
v_ext_present = true;
END IF;
IF ( v_ext_present ) THEN
SELECT COUNT(*) > 0 INTO v_func_present
FROM public.powa_functions
WHERE module = 'pg_track_settings'
AND srvid = _srvid;
IF ( NOT v_func_present) THEN
PERFORM powa_log('registering pg_track_settings');
-- This extension handles its own storage, just add its snapshot,
-- reset and an unregister function.
INSERT INTO public.powa_functions (srvid, extname, module, operation, function_name, query_source, added_manually, enabled)
VALUES (_srvid, 'pg_track_settings', 'pg_track_settings', 'snapshot', 'pg_track_settings_snapshot_settings', 'pg_track_settings_settings_src', true, true),
(_srvid, 'pg_track_settings', 'pg_track_settings', 'snapshot', 'pg_track_settings_snapshot_rds', 'pg_track_settings_rds_src', true, true),
(_srvid, 'pg_track_settings', 'pg_track_settings', 'snapshot', 'pg_track_settings_snapshot_reboot', 'pg_track_settings_reboot_src', true, true),
(_srvid, 'pg_track_settings', 'pg_track_settings', 'reset', 'pg_track_settings_reset', NULL, true, true),
(_srvid, 'pg_track_settings', 'pg_track_settings', 'unregister', 'powa_track_settings_unregister', NULL, true, true);
END IF;
END IF;
RETURN true;
END;
$_$ language plpgsql; /* end of pg_track_settings_register */
CREATE OR REPLACE FUNCTION powa_qualstats_src(IN _srvid integer,
OUT ts timestamp with time zone,
OUT uniquequalnodeid bigint,
OUT dbid oid,
OUT userid oid,
OUT qualnodeid bigint,
OUT occurences bigint,
OUT execution_count bigint,
OUT nbfiltered bigint,
OUT mean_err_estimate_ratio double precision,
OUT mean_err_estimate_num double precision,
OUT queryid bigint,
OUT constvalues varchar[],
OUT quals qual_type[]
) RETURNS SETOF record STABLE AS $PROC$
DECLARE
is_v2 bool;
ratio_col text := 'qs.mean_err_estimate_ratio';
num_col text := 'qs.mean_err_estimate_num';
sql text;
BEGIN
IF (_srvid = 0) THEN
SELECT substr(extversion, 1, 1)::int >=2 INTO is_v2
FROM pg_extension
WHERE extname = 'pg_qualstats';
IF is_v2 IS DISTINCT FROM 'true'::bool THEN
ratio_col := 'NULL::double precision';
num_col := 'NULL::double precision';
END IF;
sql := format($sql$
SELECT now(), pgqs.uniquequalnodeid, pgqs.dbid, pgqs.userid,
pgqs.qualnodeid, pgqs.occurences, pgqs.execution_count,
pgqs.nbfiltered, pgqs.mean_err_estimate_ratio,
pgqs.mean_err_estimate_num, pgqs.queryid, pgqs.constvalues,
pgqs.quals
FROM (
SELECT coalesce(i.uniquequalid, i.uniquequalnodeid) AS uniquequalnodeid,
i.dbid, i.userid, coalesce(i.qualid, i.qualnodeid) AS qualnodeid,
i.occurences, i.execution_count, i.nbfiltered,
i.mean_err_estimate_ratio, i.mean_err_estimate_num,
i.queryid,
array_agg(i.constvalue order by i.constant_position) AS constvalues,
array_agg(ROW(i.relid, i.attnum, i.opno, i.eval_type)::qual_type) AS quals
FROM
(
SELECT qs.dbid,
CASE WHEN lrelid IS NOT NULL THEN lrelid
WHEN rrelid IS NOT NULL THEN rrelid
END as relid,
qs.userid as userid,
CASE WHEN lrelid IS NOT NULL THEN lattnum
WHEN rrelid IS NOT NULL THEN rattnum
END as attnum,
qs.opno as opno,
qs.qualid as qualid,
qs.uniquequalid as uniquequalid,
qs.qualnodeid as qualnodeid,
qs.uniquequalnodeid as uniquequalnodeid,
qs.occurences as occurences,
qs.execution_count as execution_count,
qs.queryid as queryid,
qs.constvalue as constvalue,
qs.nbfiltered as nbfiltered,
%s AS mean_err_estimate_ratio,
%s AS mean_err_estimate_num,
qs.eval_type,
qs.constant_position
FROM pg_qualstats() qs
WHERE (qs.lrelid IS NULL) != (qs.rrelid IS NULL)
) i
GROUP BY coalesce(i.uniquequalid, i.uniquequalnodeid),
coalesce(i.qualid, i.qualnodeid), i.dbid, i.userid,
i.occurences, i.execution_count, i.nbfiltered,
i.mean_err_estimate_ratio, i.mean_err_estimate_num,
i.queryid
) pgqs
JOIN (
-- if we use remote capture, powa_statements won't be
-- populated, so we have to to retrieve the content of both
-- statements sources. Since there can (and probably) be
-- duplicates, we use a UNION on purpose
SELECT s1.queryid, s1.dbid, s1.userid
FROM pg_stat_statements s1