1 <!-- doc/src/sgml/auto-explain.sgml -->
3 <sect1 id=
"auto-explain" xreflabel=
"auto_explain">
4 <title>auto_explain
— log execution plans of slow queries
</title>
6 <indexterm zone=
"auto-explain">
7 <primary>auto_explain
</primary>
11 The
<filename>auto_explain
</filename> module provides a means for
12 logging execution plans of slow statements automatically, without
13 having to run
<xref linkend=
"sql-explain"/>
14 by hand. This is especially helpful for tracking down un-optimized queries
15 in large applications.
19 The module provides no SQL-accessible functions. To use it, simply
20 load it into the server. You can load it into an individual session:
26 (You must be superuser to do that.) More typical usage is to preload
27 it into some or all sessions by including
<literal>auto_explain
</literal> in
28 <xref linkend=
"guc-session-preload-libraries"/> or
29 <xref linkend=
"guc-shared-preload-libraries"/> in
30 <filename>postgresql.conf
</filename>. Then you can track unexpectedly slow queries
31 no matter when they happen. Of course there is a price in overhead for
35 <sect2 id=
"auto-explain-configuration-parameters">
36 <title>Configuration Parameters
</title>
39 There are several configuration parameters that control the behavior of
40 <filename>auto_explain
</filename>. Note that the default behavior is
41 to do nothing, so you must set at least
42 <varname>auto_explain.log_min_duration
</varname> if you want any results.
46 <varlistentry id=
"auto-explain-configuration-parameters-log-min-duration">
48 <varname>auto_explain.log_min_duration
</varname> (
<type>integer
</type>)
50 <primary><varname>auto_explain.log_min_duration
</varname> configuration parameter
</primary>
55 <varname>auto_explain.log_min_duration
</varname> is the minimum statement
56 execution time, in milliseconds, that will cause the statement's plan to
57 be logged. Setting this to
<literal>0</literal> logs all plans.
58 <literal>-
1</literal> (the default) disables logging of plans. For
59 example, if you set it to
<literal>250ms
</literal> then all statements
60 that run
250ms or longer will be logged. Only superusers can change this
66 <varlistentry id=
"auto-explain-configuration-parameters-log-parameter-max-length">
68 <varname>auto_explain.log_parameter_max_length
</varname> (
<type>integer
</type>)
70 <primary><varname>auto_explain.log_parameter_max_length
</varname> configuration parameter
</primary>
75 <varname>auto_explain.log_parameter_max_length
</varname> controls the
76 logging of query parameter values. A value of
<literal>-
1</literal> (the
77 default) logs the parameter values in full.
<literal>0</literal> disables
78 logging of parameter values. A value greater than zero truncates each
79 parameter value to that many bytes. Only superusers can change this
85 <varlistentry id=
"auto-explain-configuration-parameters-log-analyze">
87 <varname>auto_explain.log_analyze
</varname> (
<type>boolean
</type>)
89 <primary><varname>auto_explain.log_analyze
</varname> configuration parameter
</primary>
94 <varname>auto_explain.log_analyze
</varname> causes
<command>EXPLAIN ANALYZE
</command>
95 output, rather than just
<command>EXPLAIN
</command> output, to be printed
96 when an execution plan is logged. This parameter is off by default.
97 Only superusers can change this setting.
101 When this parameter is on, per-plan-node timing occurs for all
102 statements executed, whether or not they run long enough to actually
103 get logged. This can have an extremely negative impact on performance.
104 Turning off
<varname>auto_explain.log_timing
</varname> ameliorates the
105 performance cost, at the price of obtaining less information.
111 <varlistentry id=
"auto-explain-configuration-parameters-log-buffers">
113 <varname>auto_explain.log_buffers
</varname> (
<type>boolean
</type>)
115 <primary><varname>auto_explain.log_buffers
</varname> configuration parameter
</primary>
120 <varname>auto_explain.log_buffers
</varname> controls whether buffer
121 usage statistics are printed when an execution plan is logged; it's
122 equivalent to the
<literal>BUFFERS
</literal> option of
<command>EXPLAIN
</command>.
123 This parameter has no effect
124 unless
<varname>auto_explain.log_analyze
</varname> is enabled.
125 This parameter is off by default.
126 Only superusers can change this setting.
131 <varlistentry id=
"auto-explain-configuration-parameters-log-wal">
133 <varname>auto_explain.log_wal
</varname> (
<type>boolean
</type>)
135 <primary><varname>auto_explain.log_wal
</varname> configuration parameter
</primary>
140 <varname>auto_explain.log_wal
</varname> controls whether WAL
141 usage statistics are printed when an execution plan is logged; it's
142 equivalent to the
<literal>WAL
</literal> option of
<command>EXPLAIN
</command>.
143 This parameter has no effect
144 unless
<varname>auto_explain.log_analyze
</varname> is enabled.
145 This parameter is off by default.
146 Only superusers can change this setting.
151 <varlistentry id=
"auto-explain-configuration-parameters-log-timing">
153 <varname>auto_explain.log_timing
</varname> (
<type>boolean
</type>)
155 <primary><varname>auto_explain.log_timing
</varname> configuration parameter
</primary>
160 <varname>auto_explain.log_timing
</varname> controls whether per-node
161 timing information is printed when an execution plan is logged; it's
162 equivalent to the
<literal>TIMING
</literal> option of
<command>EXPLAIN
</command>.
163 The overhead of repeatedly reading the system clock can slow down
164 queries significantly on some systems, so it may be useful to set this
165 parameter to off when only actual row counts, and not exact times, are
167 This parameter has no effect
168 unless
<varname>auto_explain.log_analyze
</varname> is enabled.
169 This parameter is on by default.
170 Only superusers can change this setting.
175 <varlistentry id=
"auto-explain-configuration-parameters-log-triggers">
177 <varname>auto_explain.log_triggers
</varname> (
<type>boolean
</type>)
179 <primary><varname>auto_explain.log_triggers
</varname> configuration parameter
</primary>
184 <varname>auto_explain.log_triggers
</varname> causes trigger
185 execution statistics to be included when an execution plan is logged.
186 This parameter has no effect
187 unless
<varname>auto_explain.log_analyze
</varname> is enabled.
188 This parameter is off by default.
189 Only superusers can change this setting.
194 <varlistentry id=
"auto-explain-configuration-parameters-log-verbose">
196 <varname>auto_explain.log_verbose
</varname> (
<type>boolean
</type>)
198 <primary><varname>auto_explain.log_verbose
</varname> configuration parameter
</primary>
203 <varname>auto_explain.log_verbose
</varname> controls whether verbose
204 details are printed when an execution plan is logged; it's
205 equivalent to the
<literal>VERBOSE
</literal> option of
<command>EXPLAIN
</command>.
206 This parameter is off by default.
207 Only superusers can change this setting.
212 <varlistentry id=
"auto-explain-configuration-parameters-log-settings">
214 <varname>auto_explain.log_settings
</varname> (
<type>boolean
</type>)
216 <primary><varname>auto_explain.log_settings
</varname> configuration parameter
</primary>
221 <varname>auto_explain.log_settings
</varname> controls whether information
222 about modified configuration options is printed when an execution plan is logged.
223 Only options affecting query planning with value different from the built-in
224 default value are included in the output. This parameter is off by default.
225 Only superusers can change this setting.
230 <varlistentry id=
"auto-explain-configuration-parameters-log-format">
232 <varname>auto_explain.log_format
</varname> (
<type>enum
</type>)
234 <primary><varname>auto_explain.log_format
</varname> configuration parameter
</primary>
239 <varname>auto_explain.log_format
</varname> selects the
240 <command>EXPLAIN
</command> output format to be used.
241 The allowed values are
<literal>text
</literal>,
<literal>xml
</literal>,
242 <literal>json
</literal>, and
<literal>yaml
</literal>. The default is text.
243 Only superusers can change this setting.
248 <varlistentry id=
"auto-explain-configuration-parameters-log-level">
250 <varname>auto_explain.log_level
</varname> (
<type>enum
</type>)
252 <primary><varname>auto_explain.log_level
</varname> configuration parameter
</primary>
257 <varname>auto_explain.log_level
</varname> selects the log level at which
258 auto_explain will log the query plan.
259 Valid values are
<literal>DEBUG5
</literal>,
<literal>DEBUG4
</literal>,
260 <literal>DEBUG3
</literal>,
<literal>DEBUG2
</literal>,
261 <literal>DEBUG1
</literal>,
<literal>INFO
</literal>,
262 <literal>NOTICE
</literal>,
<literal>WARNING
</literal>,
263 and
<literal>LOG
</literal>. The default is
<literal>LOG
</literal>.
264 Only superusers can change this setting.
269 <varlistentry id=
"auto-explain-configuration-parameters-log-nested-statements">
271 <varname>auto_explain.log_nested_statements
</varname> (
<type>boolean
</type>)
273 <primary><varname>auto_explain.log_nested_statements
</varname> configuration parameter
</primary>
278 <varname>auto_explain.log_nested_statements
</varname> causes nested
279 statements (statements executed inside a function) to be considered
280 for logging. When it is off, only top-level query plans are logged. This
281 parameter is off by default. Only superusers can change this setting.
286 <varlistentry id=
"auto-explain-configuration-parameters-sample-rate">
288 <varname>auto_explain.sample_rate
</varname> (
<type>real
</type>)
290 <primary><varname>auto_explain.sample_rate
</varname> configuration parameter
</primary>
295 <varname>auto_explain.sample_rate
</varname> causes auto_explain to only
296 explain a fraction of the statements in each session. The default is
1,
297 meaning explain all the queries. In case of nested statements, either all
298 will be explained or none. Only superusers can change this setting.
305 In ordinary usage, these parameters are set
306 in
<filename>postgresql.conf
</filename>, although superusers can alter them
307 on-the-fly within their own sessions.
308 Typical usage might be:
313 session_preload_libraries = 'auto_explain'
315 auto_explain.log_min_duration = '
3s'
319 <sect2 id=
"auto-explain-example">
320 <title>Example
</title>
323 postgres=# LOAD 'auto_explain';
324 postgres=# SET auto_explain.log_min_duration =
0;
325 postgres=# SET auto_explain.log_analyze = true;
326 postgres=# SELECT count(*)
327 FROM pg_class, pg_index
328 WHERE oid = indrelid AND indisunique;
332 This might produce log output such as:
336 LOG: duration:
3.651 ms plan:
337 Query Text: SELECT count(*)
338 FROM pg_class, pg_index
339 WHERE oid = indrelid AND indisunique;
340 Aggregate (cost=
16.79.
.16.80 rows=
1 width=
0) (actual time=
3.626.
.3.627 rows=
1 loops=
1)
341 -
> Hash Join (cost=
4.17.
.16.55 rows=
92 width=
0) (actual time=
3.349.
.3.594 rows=
92 loops=
1)
342 Hash Cond: (pg_class.oid = pg_index.indrelid)
343 -
> Seq Scan on pg_class (cost=
0.00.
.9.55 rows=
255 width=
4) (actual time=
0.016.
.0.140 rows=
255 loops=
1)
344 -
> Hash (cost=
3.02.
.3.02 rows=
92 width=
4) (actual time=
3.238.
.3.238 rows=
92 loops=
1)
345 Buckets:
1024 Batches:
1 Memory Usage:
4kB
346 -
> Seq Scan on pg_index (cost=
0.00.
.3.02 rows=
92 width=
4) (actual time=
0.008.
.3.187 rows=
92 loops=
1)
351 <sect2 id=
"auto-explain-author">
352 <title>Author
</title>
355 Takahiro Itagaki
<email>itagaki.takahiro@oss.ntt.co.jp
</email>