forked from TritonDataCenter/pgsqlstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsqlslower
executable file
·222 lines (190 loc) · 4.53 KB
/
pgsqlslower
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
#!/bin/bash
#
# pgsqlslower NMILLIS: report details on slow postgres queries
#
ps_arg0="pgsqlslower"
#
# Some of this boilerplate is common to several tools, but it's duplicated here
# because it's very useful for these to be standalone scripts.
#
ps_tmpfile="/var/tmp/$ps_arg0.$$"
ps_number_re='^[1-9][0-9]*$'
ps_synopsis="usage: $ps_arg0 NMILLISECONDS"
if [[ $# == 0 ]]; then
echo "$ps_synopsis" >&2
exit 2
fi
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
cat >&2 <<EOF
$ps_synopsis
Print details about queries taking longer than NMILLISECONDS from start to
finish on all postgresql instances on this system. Note that since this tool
traces query start to query done, it will never see queries taking longer than
NMILLISECONDS seconds.
This is similar to pgsqltxslower, except that it's tracing simple queries. For
extended queries that are part of transactions, see pgsqltxslower.
This tool requires privileges to use DTrace on postgres processes on this
system. If you see no output but expect some, check whether your user has
permissions to trace the postgres processes.
EOF
exit 2
elif ! [[ "$1" == "0" || "$1" =~ $ps_number_re ]]; then
echo "$ps_arg0: bad number of milliseconds" >&2
echo "$ps_synopsis" >&2
exit 2
fi
trap cleanup EXIT
function cleanup
{
rm -f "$ps_tmpfile"
}
if ! type dtrace > /dev/null 2>&1; then
echo "$ps_arg0: requires dtrace(1M), but not found" >&2
exit 1
fi
cat > "$ps_tmpfile" <<EOF
#!/usr/sbin/dtrace -Cs
#pragma D option quiet
#pragma D option zdefs
#pragma D option strsize=1024
#define THRESHOLD ($1)
#define MICROS(X) ((X) / 1000)
#define MICRODIFF(X, Y) (MICROS((X) - (Y)))
/*
* XXX can queries be started recursively? If so, this will do the right thing
* on query entry (only capturing the top-level one), but not on query-done.
*/
postgresql*:::query-start
/!self->start/
{
self->start = timestamp;
self->qstr = arg0;
self->nreads = 0;
self->ncachehits = 0;
self->nflushes = 0;
self->parse_start = 0;
self->parse_done = 0;
self->plan_start = 0;
self->plan_done = 0;
self->exec_start = 0;
self->exec_done = 0;
self->ntxnstarts = 0;
self->ntxncommits = 0;
self->ntxnaborts = 0;
}
/*
* Track elapsed time during query processing phases.
*/
postgresql*:::query-parse-start
/self->start/
{
self->parse_start = timestamp;
}
postgresql*:::query-parse-done
/self->parse_start/
{
self->parse_done = timestamp;
}
postgresql*:::query-plan-start
/self->start/
{
self->plan_start = timestamp;
}
postgresql*:::query-plan-done
/self->plan_start/
{
self->plan_done = timestamp;
}
postgresql*:::query-execute-start
/self->start/
{
self->exec_start = timestamp;
}
postgresql*:::query-execute-done
/self->exec_start/
{
self->exec_done = timestamp;
}
/*
* Track buffer reads.
*/
postgresql*:::buffer-read-done
/self->start && arg7/
{
self->ncachehits++;
}
postgresql*:::buffer-read-done
/self->start/
{
self->nreads++;
}
postgresql*:::buffer-flush-done
/self->start/
{
self->nflushes++;
}
/*
* Track transactions started, committed, or aborted.
*/
postgresql*:::transaction-start
/self->start/
{
self->ntxnstarts++;
}
postgresql*:::transaction-commit
/self->start/
{
self->ntxncommits++;
}
/*
* On a transaction abort, the current query is done, and we won't see a
* query-done probe.
*/
postgresql*:::transaction-abort
/self->start/
{
self->ntxnaborts++;
}
postgresql*:::transaction-abort,
postgresql*:::query-done
/self->start && (timestamp - self->start) > THRESHOLD * 1000000/
{
printf("QUERY: %s\n", copyinstr(self->qstr));
printf(" total time: %8d us (parse/plan/execute = %dus/%dus/%dus)\n",
MICRODIFF(timestamp, self->start),
self->parse_done == 0 ? 0 :
MICRODIFF(self->parse_done, self->parse_start),
self->plan_done == 0 ? 0 :
MICRODIFF(self->plan_done, self->plan_start),
self->exec_done == 0 ? 0 :
MICRODIFF(self->exec_done, self->exec_start));
printf(" txns: %d started, %d committed, %d aborted\n",
self->ntxnstarts, self->ntxncommits, self->ntxnaborts);
printf(" buffers: %d read (%d hit, %d missed), %d flushed\n",
self->nreads, self->ncachehits, self->nreads - self->ncachehits,
self->nflushes);
printf("\n");
}
/*
* Clean up state.
*/
postgresql*:::transaction-abort,
postgresql*:::query-done
{
self->qstr = 0;
self->start = 0;
self->nreads = 0;
self->ncachehits = 0;
self->nflushes = 0;
self->parse_start = 0;
self->parse_done = 0;
self->plan_start = 0;
self->plan_done = 0;
self->exec_start = 0;
self->exec_done = 0;
self->ntxnstarts = 0;
self->ntxncommits = 0;
self->ntxnaborts = 0;
}
EOF
dtrace -Cs "$ps_tmpfile"