-
Notifications
You must be signed in to change notification settings - Fork 20
/
powa--4.1.0.sql
4510 lines (4043 loc) · 169 KB
/
powa--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 "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_servers(
id serial PRIMARY KEY,
hostname text NOT NULL,
alias text,
port integer NOT NULL,
username text NOT NULL,
password text,
dbname text NOT NULL,
frequency integer NOT NULL default 300 CHECK (frequency = -1 OR frequency >= 5),
powa_coalesce integer NOT NULL default 100 CHECK (powa_coalesce >= 5),
retention interval NOT NULL default '1 day'::interval,
allow_ui_connection boolean NOT NULL default true,
version text,
UNIQUE (hostname, port),
UNIQUE(alias)
);
INSERT INTO public.powa_servers VALUES (0, '', '<local>', 0, '', NULL, '', -1, 100, '0 second');
CREATE TABLE powa_snapshot_metas(
srvid integer PRIMARY KEY,
coalesce_seq bigint NOT NULL default (1),
snapts timestamp with time zone NOT NULL default '-infinity'::timestamptz,
aggts timestamp with time zone NOT NULL default '-infinity'::timestamptz,
purgets timestamp with time zone NOT NULL default '-infinity'::timestamptz,
errors text[],
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO public.powa_snapshot_metas (srvid) VALUES (0);
CREATE TABLE powa_databases(
srvid integer NOT NULL,
oid oid,
datname name,
dropped timestamp with time zone,
PRIMARY KEY (srvid, oid),
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE powa_statements (
srvid integer NOT NULL,
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
query text NOT NULL,
last_present_ts timestamptz NULL DEFAULT now(),
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES powa_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
ALTER TABLE ONLY powa_statements
ADD CONSTRAINT powa_statements_pkey PRIMARY KEY (srvid, queryid, dbid, userid);
CREATE INDEX powa_statements_dbid_idx ON powa_statements(srvid, dbid);
CREATE INDEX powa_statements_userid_idx ON powa_statements(userid);
CREATE INDEX powa_statements_mru_idx ON powa_statements (last_present_ts);
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 STABLE
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 STABLE
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_exec_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,
plans bigint,
total_plan_time double precision,
wal_records bigint,
wal_fpi bigint,
wal_bytes numeric
);
/* pg_stat_statements operator support */
CREATE TYPE powa_statements_history_diff AS (
intvl interval,
calls bigint,
total_exec_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,
plans bigint,
total_plan_time double precision,
wal_records bigint,
wal_fpi bigint,
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;
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,
plans_per_sec double precision,
plantime_per_sec double precision,
wal_records_per_sec double precision,
wal_fpi_per_sec double precision,
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;
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 TYPE powa_all_relations_history_db_record AS (
ts timestamp with time zone,
seq_scan bigint,
idx_scan 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
);
/* pg_stat_all_relations_db operator support */
CREATE TYPE powa_all_relations_history_db_diff AS (
intvl interval,
seq_scan bigint,
idx_scan 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_db_mi(
a powa_all_relations_history_db_record,
b powa_all_relations_history_db_record)
RETURNS powa_all_relations_history_db_diff AS
$_$
DECLARE
res powa_all_relations_history_db_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.seq_scan = a.seq_scan - b.seq_scan;
res.idx_scan = a.idx_scan - b.idx_scan;
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_db_mi,
LEFTARG = powa_all_relations_history_db_record,
RIGHTARG = powa_all_relations_history_db_record
);
CREATE TYPE powa_all_relations_history_db_rate AS (
sec integer,
seq_scan_per_sec double precision,
idx_scan_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_db_div(
a powa_all_relations_history_db_record,
b powa_all_relations_history_db_record)
RETURNS powa_all_relations_history_db_rate AS
$_$
DECLARE
res powa_all_relations_history_db_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.seq_scan_per_sec = (a.seq_scan - b.seq_scan)::double precision / sec;
res.idx_scan_per_sec = (a.idx_scan - b.idx_scan)::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_db_div,
LEFTARG = powa_all_relations_history_db_record,
RIGHTARG = powa_all_relations_history_db_record
);
/* end of pg_stat_all_relations_db operator support */
CREATE TYPE powa_stat_bgwriter_history_record AS (
ts timestamp with time zone,
checkpoints_timed bigint,
checkpoints_req bigint,
checkpoint_write_time double precision,
checkpoint_sync_time double precision,
buffers_checkpoint bigint,
buffers_clean bigint,
maxwritten_clean bigint,
buffers_backend bigint,
buffers_backend_fsync bigint,
buffers_alloc bigint
);
/*pg_stat_bgwriter operator support */
CREATE TYPE powa_stat_bgwriter_history_diff AS (
intvl interval,
checkpoints_timed bigint,
checkpoints_req bigint,
checkpoint_write_time double precision,
checkpoint_sync_time double precision,
buffers_checkpoint double precision,
buffers_clean bigint,
maxwritten_clean bigint,
buffers_backend bigint,
buffers_backend_fsync bigint,
buffers_alloc bigint
);
CREATE OR REPLACE FUNCTION powa_stat_bgwriter_history_mi(
a powa_stat_bgwriter_history_record,
b powa_stat_bgwriter_history_record)
RETURNS powa_stat_bgwriter_history_diff AS
$_$
DECLARE
res powa_stat_bgwriter_history_diff;
BEGIN
res.intvl = a.ts - b.ts;
res.checkpoints_timed = a.checkpoints_timed - b.checkpoints_timed;
res.checkpoints_req = a.checkpoints_req - b.checkpoints_req;
res.checkpoint_write_time = a.checkpoint_write_time - b.checkpoint_write_time;
res.checkpoint_sync_time = a.checkpoint_sync_time - b.checkpoint_sync_time;
res.buffers_checkpoint = a.buffers_checkpoint - b.buffers_checkpoint;
res.buffers_clean = a.buffers_clean - b.buffers_clean;
res.maxwritten_clean = a.maxwritten_clean - b.maxwritten_clean;
res.buffers_backend = a.buffers_backend - b.buffers_backend;
res.buffers_backend_fsync = a.buffers_backend_fsync - b.buffers_backend_fsync;
res.buffers_alloc = a.buffers_alloc - b.buffers_alloc;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR - (
PROCEDURE = powa_stat_bgwriter_history_mi,
LEFTARG = powa_stat_bgwriter_history_record,
RIGHTARG = powa_stat_bgwriter_history_record
);
CREATE TYPE powa_stat_bgwriter_history_rate AS (
sec integer,
checkpoints_timed_per_sec double precision,
checkpoints_req_per_sec double precision,
checkpoint_write_time_per_sec double precision,
checkpoint_sync_time_per_sec double precision,
buffers_checkpoint_per_sec double precision,
buffers_clean_per_sec double precision,
maxwritten_clean_per_sec double precision,
buffers_backend_per_sec double precision,
buffers_backend_fsync_per_sec double precision,
buffers_alloc_per_sec double precision
);
CREATE OR REPLACE FUNCTION powa_stat_bgwriter_history_div(
a powa_stat_bgwriter_history_record,
b powa_stat_bgwriter_history_record)
RETURNS powa_stat_bgwriter_history_rate AS
$_$
DECLARE
res powa_stat_bgwriter_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.checkpoints_timed_per_sec = (a.checkpoints_timed - b.checkpoints_timed)::double precision / sec;
res.checkpoints_req_per_sec = (a.checkpoints_req - b.checkpoints_req)::double precision / sec;
res.checkpoint_write_time_per_sec = (a.checkpoint_write_time - b.checkpoint_write_time)::double precision / sec;
res.checkpoint_sync_time_per_sec = (a.checkpoint_sync_time - b.checkpoint_sync_time)::double precision / sec;
res.buffers_checkpoint_per_sec = (a.buffers_checkpoint - b.buffers_checkpoint)::double precision / sec;
res.buffers_clean_per_sec = (a.buffers_clean - b.buffers_clean)::double precision / sec;
res.maxwritten_clean_per_sec = (a.maxwritten_clean - b.maxwritten_clean)::double precision / sec;
res.buffers_backend_per_sec = (a.buffers_backend - b.buffers_backend)::double precision / sec;
res.buffers_backend_fsync_per_sec = (a.buffers_backend_fsync - b.buffers_backend_fsync)::double precision / sec;
res.buffers_alloc_per_sec = (a.buffers_alloc - b.buffers_alloc)::double precision / sec;
return res;
END;
$_$
LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR / (
PROCEDURE = powa_stat_bgwriter_history_div,
LEFTARG = powa_stat_bgwriter_history_record,
RIGHTARG = powa_stat_bgwriter_history_record
);
/* end of pg_stat_bgwriter operator support */
CREATE UNLOGGED TABLE public.powa_databases_src_tmp(
srvid integer NOT NULL,
oid oid NOT NULL,
datname name NOT NULL
);
CREATE UNLOGGED TABLE public.powa_statements_src_tmp (
srvid integer NOT NULL,
ts timestamp with time zone NOT NULL,
userid oid NOT NULL,
dbid oid NOT NULL,
queryid bigint NOT NULL,
query text NOT NULL,
calls bigint NOT NULL,
total_exec_time double precision NOT NULL,
rows bigint NOT NULL,
shared_blks_hit bigint NOT NULL,
shared_blks_read bigint NOT NULL,
shared_blks_dirtied bigint NOT NULL,
shared_blks_written bigint NOT NULL,
local_blks_hit bigint NOT NULL,
local_blks_read bigint NOT NULL,
local_blks_dirtied bigint NOT NULL,
local_blks_written bigint NOT NULL,
temp_blks_read bigint NOT NULL,
temp_blks_written bigint NOT NULL,
blk_read_time double precision NOT NULL,
blk_write_time double precision NOT NULL,
plans bigint NOT NULL,
total_plan_time double precision NOT NULL,
wal_records bigint NOT NULL,
wal_fpi bigint NOT NULL,
wal_bytes numeric NOT NULL
);
CREATE UNLOGGED TABLE public.powa_user_functions_src_tmp(
srvid integer NOT NULL,
ts timestamp with time zone NOT NULL,
dbid oid NOT NULL,
funcid oid NOT NULL,
calls bigint NOT NULL,
total_time double precision NOT NULL,
self_time double precision NOT NULL
);
CREATE UNLOGGED TABLE public.powa_all_relations_src_tmp(
srvid integer NOT NULL,
ts timestamp with time zone NOT NULL,
dbid oid NOT NULL,
relid oid NOT NULL,
numscan bigint NOT NULL,
tup_returned bigint NOT NULL,
tup_fetched bigint NOT NULL,
n_tup_ins bigint NOT NULL,
n_tup_upd bigint NOT NULL,
n_tup_del bigint NOT NULL,
n_tup_hot_upd bigint NOT NULL,
n_liv_tup bigint NOT NULL,
n_dead_tup bigint NOT NULL,
n_mod_since_analyze bigint NOT NULL,
blks_read bigint NOT NULL,
blks_hit bigint NOT NULL,
last_vacuum timestamp with time zone,
vacuum_count bigint NOT NULL,
last_autovacuum timestamp with time zone,
autovacuum_count bigint NOT NULL,
last_analyze timestamp with time zone,
analyze_count bigint NOT NULL,
last_autoanalyze timestamp with time zone,
autoanalyze_count bigint NOT NULL
);
CREATE UNLOGGED TABLE public.powa_stat_bgwriter_src_tmp(
srvid integer NOT NULL,
ts timestamp with time zone NOT NULL,
checkpoints_timed bigint NOT NULL,
checkpoints_req bigint NOT NULL,
checkpoint_write_time double precision NOT NULL,
checkpoint_sync_time double precision NOT NULL,
buffers_checkpoint bigint NOT NULL,
buffers_clean bigint NOT NULL,
maxwritten_clean bigint NOT NULL,
buffers_backend bigint NOT NULL,
buffers_backend_fsync bigint NOT NULL,
buffers_alloc bigint NOT NULL
);
CREATE TABLE powa_statements_history (
srvid integer NOT NULL,
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,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX powa_statements_history_query_ts ON powa_statements_history USING gist (srvid, queryid, coalesce_range);
CREATE TABLE powa_statements_history_db (
srvid integer NOT NULL,
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,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX powa_statements_history_db_ts ON powa_statements_history_db USING gist (srvid, dbid, coalesce_range);
CREATE TABLE powa_statements_history_current (
srvid integer NOT NULL,
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
record powa_statements_history_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX ON powa_statements_history_current(srvid);
CREATE TABLE powa_statements_history_current_db (
srvid integer NOT NULL,
dbid oid NOT NULL,
record powa_statements_history_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX ON powa_statements_history_current_db(srvid);
CREATE TABLE powa_user_functions_history (
srvid integer NOT NULL,
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,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX powa_user_functions_history_funcid_ts ON powa_user_functions_history USING gist (srvid, funcid, coalesce_range);
CREATE TABLE powa_user_functions_history_current (
srvid integer NOT NULL,
dbid oid NOT NULL,
funcid oid NOT NULL,
record powa_user_functions_history_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX ON powa_user_functions_history_current(srvid);
CREATE TABLE powa_all_relations_history (
srvid integer NOT NULL,
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,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX powa_all_relations_history_relid_ts ON powa_all_relations_history USING gist (srvid, relid, coalesce_range);
CREATE TABLE powa_all_relations_history_db (
srvid integer NOT NULL,
dbid oid NOT NULL,
coalesce_range tstzrange NOT NULL,
records powa_all_relations_history_db_record[] NOT NULL,
mins_in_range powa_all_relations_history_db_record NOT NULL,
maxs_in_range powa_all_relations_history_db_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX powa_all_relations_history_db_dbid_ts ON powa_all_relations_history_db USING gist (srvid, dbid, coalesce_range);
CREATE TABLE powa_all_relations_history_current (
srvid integer NOT NULL,
dbid oid NOT NULL,
relid oid NOT NULL,
record powa_all_relations_history_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX ON powa_all_relations_history_current(srvid);
CREATE TABLE powa_all_relations_history_current_db (
srvid integer NOT NULL,
dbid oid NOT NULL,
record powa_all_relations_history_db_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX ON powa_all_relations_history_current_db(srvid);
CREATE TABLE powa_stat_bgwriter_history (
srvid integer NOT NULL,
coalesce_range tstzrange NOT NULL,
records powa_stat_bgwriter_history_record[] NOT NULL,
mins_in_range powa_stat_bgwriter_history_record NOT NULL,
maxs_in_range powa_stat_bgwriter_history_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX powa_stat_bgwriter_history_ts ON powa_stat_bgwriter_history USING gist (srvid, coalesce_range);
CREATE TABLE powa_stat_bgwriter_history_current(
srvid integer NOT NULL,
record powa_stat_bgwriter_history_record NOT NULL,
FOREIGN KEY (srvid) REFERENCES powa_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE INDEX ON powa_stat_bgwriter_history_current(srvid);
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) VALUES
(0, 'pg_stat_statements'),
(0, 'powa');
CREATE TABLE public.powa_functions (
srvid integer NOT NULL,
module text NOT NULL,
operation text NOT NULL,
function_name text NOT NULL,
query_source text default NULL,
query_cleanup text default NULL,
added_manually boolean NOT NULL default true,
enabled boolean NOT NULL default true,
priority numeric NOT NULL default 10,
extname text,
CHECK (operation IN ('snapshot','aggregate','purge','unregister','reset')),
FOREIGN KEY (srvid) REFERENCES powa_servers(id)