-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg-stat-activity.sql
163 lines (146 loc) · 4.05 KB
/
pg-stat-activity.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
-- while [ 1 = 1 ];do psql -h localhost -F',' -A -f /backups/dbscripts/pgstatactivity-snap.sql >> /tmp/pgstatactivity-$(echo $(hostname) | cut -d'.' -f1 | cut -d'-' -f1)-$(date +%Y-%m-%d-%H).csv; echo "pid:$$"; sleep 20; done &
-- queries > 3 minutes
SELECT now()::date AS date,
now()::time(0) AS time,
datname,
pid,
(now() - query_start)::time(0) AS runtime,
EXTRACT(EPOCH FROM (now() - query_start))::INT AS runtime_secs,
EXTRACT(EPOCH FROM (now() - query_start))*1000::INT AS runtime_millisecs,
query_start::timestamp(0),
usename,
client_addr,
client_port,
state,
regexp_replace(query,'[\s\u2028]+', ' ', 'g') AS query
FROM pg_stat_activity
WHERE state <> 'idle'
-- AND now() - query_start > '3 MINUTES'::INTERVAL
AND now() - query_start > '30 MILLISECONDS'::INTERVAL
-- ORDER BY EXTRACT(EPOCH FROM (now() - query_start))::INT DESC;
ORDER BY (now() - query_start)::time(0) DESC;
-- query stat by pid
SELECT now() AS ts,
datname,
pid,
now() - query_start AS runtime,
EXTRACT(EPOCH FROM (now() - query_start))::INT AS runtime_secs,
query_start,
usename,
client_addr,
client_port,
state,
waiting,
regexp_replace(query,'[\s\u2028]+', ' ', 'g') AS query
FROM pg_stat_activity
WHERE pid = '21195'
ORDER BY runtime_secs DESC ;
-- count by client connection
SELECT datname,
client_addr,
count(*)
FROM pg_stat_activity
WHERE
client_addr IS NOT NULL -- ignore autovacuum jobs
GROUP BY 1,2
ORDER BY 3 DESC ;
-- count by state
SELECT datname,
state,
count(*)
FROM pg_stat_activity
WHERE state IS NOT NULL
GROUP BY 1,2
ORDER BY 3 DESC ;
-- count by client connection, state
SELECT datname,
client_addr,
state,
count(*)
FROM pg_stat_activity
WHERE client_addr IS NOT NULL -- ignore autovacuum jobs
GROUP BY 1,2,3
ORDER BY 4 DESC ;
-- count by client connection, state, user
SELECT datname,
usename,
client_addr,
state,
count(*)
FROM pg_stat_activity
-- WHERE client_addr IS NOT NULL -- ignore autovacuum jobs
GROUP BY 1,2,3,4
ORDER BY 5 DESC ;
-- autovacuum query
SELECT now()::date AS date,
now()::time(0) AS time,
pid,
datname,
(now() - query_start)::time(0) as runtime,
query_start::timestamp(0),
client_addr,
client_port,
state,
waiting,
regexp_replace(query,'[\s\u2028]+', ' ', 'g') AS query
FROM pg_stat_activity
WHERE query ilike 'autovacuum%'
ORDER BY EXTRACT(EPOCH FROM (now() - query_start))::INT DESC;
-- autovacuum query
SELECT now()::date AS date,
now()::time(0) AS time,
pid,
datname,
(now() - query_start)::time(0) as runtime,
query_start::timestamp(0),
state,
query
FROM pg_stat_activity
WHERE query ilike 'autovacuum%'
ORDER BY EXTRACT(EPOCH FROM (now() - query_start))::INT DESC;
-- vacuum query
SELECT now()::date AS date,
now()::time(0) AS time,
pid,
datname,
(now() - query_start)::time(0) as runtime,
query_start::timestamp(0),
client_addr,
client_port,
state,
waiting,
regexp_replace(query,'[\s\u2028]+', ' ', 'g') AS query
FROM pg_stat_activity
WHERE query ilike 'vacuum%'
ORDER BY EXTRACT(EPOCH FROM (now() - query_start))::INT DESC;
-- check repack activity
SELECT now()::date AS date,
now()::time(0) AS time,
pid,
datname,
-- EXTRACT(EPOCH FROM (now() - query_start))::INT AS runtime_secs,
(now() - query_start)::time(0) as runtime,
query_start::timestamp(0),
client_addr,
client_port,
state,
waiting,
regexp_replace(query,'[\s\u2028]+', ' ', 'g') AS query
FROM pg_stat_activity
WHERE query ilike 'LOCK TABLE%'
ORDER BY EXTRACT(EPOCH FROM (now() - query_start))::INT DESC;
-- size of the cluster
SELECT sum(pg_database_size(d.datname)) AS db_size_bytes,
pg_size_pretty(sum(pg_database_size(d.datname))) AS db_size_pretty
FROM pg_database d
GROUP BY d.datdba;
select pid, datname , query_start, xact_start
, pg_cancel_backend(pid) as action
from pg_stat_activity where state <> 'idle'
and pid <> pg_backend_pid()
and now() - query_start > '0.1 seconds'::INTERVAL
select pid, pg_terminate_backend(pid) as termination_state
from pg_stat_activity
where state='idle'
and now() - query_start > '180 SECOND'::INTERVAL
and pid <> pg_backend_pid();