-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstat_activity_monitoring.sql
More file actions
executable file
·82 lines (75 loc) · 1.82 KB
/
stat_activity_monitoring.sql
File metadata and controls
executable file
·82 lines (75 loc) · 1.82 KB
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
/* some sample queries to analyze data gathered via stat_activity_monitoring.py */
-- top 5min time ranges with longest transactions
select
to_timestamp((extract(epoch from xact_start)::int / 300)*300) as tx_start_range,
count(*)
from (
select pid, xact_start, max(now) - min(now) as duration from stat_activity_history where xact_start is not null group by pid, xact_start order by 3 desc limit 100
) x
group by 1
order by 2 desc
limit 10;
-- most concurrent transactions
select
now,
sum(count) as total_active_tx,
string_agg(state||':'||count, ', ') as by_state
from (
select
now,
state,
count(*)
from
stat_activity_history
where
now in (
select
now
from
stat_activity_history
where
xact_start is not null
group by 1
order by count(*) desc
limit 10
)
group by 1, 2
order by 1, 3 desc
) x
group by 1
order by 2 desc, 1;
-- top 5min avg tx duration
select
to_timestamp((extract(epoch from now)::int / 300)*300) as tx_start_range,
avg(now - xact_start) as avg_xact_duration
from
stat_activity_history
where
xact_start is not null
group by 1
order by 2 desc
limit 10;
-- avg QPS (approx, due to possible PGSS evictions)
with q as (
select
now,
sum(rows) rows,
sum(calls) calls,
avg(mean_exec_time) mean_exec_time
from
stat_activity_history
group by
now
), qt as (
select
min(now) as t1,
max(now) as t2,
extract(epoch from max(now) - min(now)) as duration_s
from q
)
select
qt.*,
(((select rows from q where now = qt.t2) - (select rows from q where now = qt.t1)) / qt.duration_s)::numeric(12,2) as avg_rows_s,
(((select calls from q where now = qt.t2) - (select calls from q where now = qt.t1)) / qt.duration_s)::numeric(12,2) as avg_calls_s,
(select avg(mean_exec_time) from q)::numeric(12,2) as avg_mean_exec_time_ms
from qt ;