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
|
'\" t
.\" Title: EXPLAIN
.\" Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\" Date: 2024
.\" Manual: PostgreSQL 16.3 Documentation
.\" Source: PostgreSQL 16.3
.\" Language: English
.\"
.TH "EXPLAIN" "7" "2024" "PostgreSQL 16.3" "PostgreSQL 16.3 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
EXPLAIN \- show the execution plan of a statement
.SH "SYNOPSIS"
.sp
.nf
EXPLAIN [ ( \fIoption\fR [, \&.\&.\&.] ) ] \fIstatement\fR
EXPLAIN [ ANALYZE ] [ VERBOSE ] \fIstatement\fR
where \fIoption\fR can be one of:
ANALYZE [ \fIboolean\fR ]
VERBOSE [ \fIboolean\fR ]
COSTS [ \fIboolean\fR ]
SETTINGS [ \fIboolean\fR ]
GENERIC_PLAN [ \fIboolean\fR ]
BUFFERS [ \fIboolean\fR ]
WAL [ \fIboolean\fR ]
TIMING [ \fIboolean\fR ]
SUMMARY [ \fIboolean\fR ]
FORMAT { TEXT | XML | JSON | YAML }
.fi
.SH "DESCRIPTION"
.PP
This command displays the execution plan that the
PostgreSQL
planner generates for the supplied statement\&. The execution plan shows how the table(s) referenced by the statement will be scanned \(em by plain sequential scan, index scan, etc\&. \(em and if multiple tables are referenced, what join algorithms will be used to bring together the required rows from each input table\&.
.PP
The most critical part of the display is the estimated statement execution cost, which is the planner\*(Aqs guess at how long it will take to run the statement (measured in cost units that are arbitrary, but conventionally mean disk page fetches)\&. Actually two numbers are shown: the start\-up cost before the first row can be returned, and the total cost to return all the rows\&. For most queries the total cost is what matters, but in contexts such as a subquery in
EXISTS, the planner will choose the smallest start\-up cost instead of the smallest total cost (since the executor will stop after getting one row, anyway)\&. Also, if you limit the number of rows to return with a
LIMIT
clause, the planner makes an appropriate interpolation between the endpoint costs to estimate which plan is really the cheapest\&.
.PP
The
ANALYZE
option causes the statement to be actually executed, not only planned\&. Then actual run time statistics are added to the display, including the total elapsed time expended within each plan node (in milliseconds) and the total number of rows it actually returned\&. This is useful for seeing whether the planner\*(Aqs estimates are close to reality\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBImportant\fR
.ps -1
.br
.PP
Keep in mind that the statement is actually executed when the
ANALYZE
option is used\&. Although
\fBEXPLAIN\fR
will discard any output that a
\fBSELECT\fR
would return, other side effects of the statement will happen as usual\&. If you wish to use
\fBEXPLAIN ANALYZE\fR
on an
\fBINSERT\fR,
\fBUPDATE\fR,
\fBDELETE\fR,
\fBMERGE\fR,
\fBCREATE TABLE AS\fR, or
\fBEXECUTE\fR
statement without letting the command affect your data, use this approach:
.sp
.if n \{\
.RS 4
.\}
.nf
BEGIN;
EXPLAIN ANALYZE \&.\&.\&.;
ROLLBACK;
.fi
.if n \{\
.RE
.\}
.sp .5v
.RE
.PP
Only the
ANALYZE
and
VERBOSE
options can be specified, and only in that order, without surrounding the option list in parentheses\&. Prior to
PostgreSQL
9\&.0, the unparenthesized syntax was the only one supported\&. It is expected that all new options will be supported only in the parenthesized syntax\&.
.SH "PARAMETERS"
.PP
ANALYZE
.RS 4
Carry out the command and show actual run times and other statistics\&. This parameter defaults to
FALSE\&.
.RE
.PP
VERBOSE
.RS 4
Display additional information regarding the plan\&. Specifically, include the output column list for each node in the plan tree, schema\-qualify table and function names, always label variables in expressions with their range table alias, and always print the name of each trigger for which statistics are displayed\&. The query identifier will also be displayed if one has been computed, see
compute_query_id
for more details\&. This parameter defaults to
FALSE\&.
.RE
.PP
COSTS
.RS 4
Include information on the estimated startup and total cost of each plan node, as well as the estimated number of rows and the estimated width of each row\&. This parameter defaults to
TRUE\&.
.RE
.PP
SETTINGS
.RS 4
Include information on configuration parameters\&. Specifically, include options affecting query planning with value different from the built\-in default value\&. This parameter defaults to
FALSE\&.
.RE
.PP
GENERIC_PLAN
.RS 4
Allow the statement to contain parameter placeholders like
$1, and generate a generic plan that does not depend on the values of those parameters\&. See
\fBPREPARE\fR
for details about generic plans and the types of statement that support parameters\&. This parameter cannot be used together with
ANALYZE\&. It defaults to
FALSE\&.
.RE
.PP
BUFFERS
.RS 4
Include information on buffer usage\&. Specifically, include the number of shared blocks hit, read, dirtied, and written, the number of local blocks hit, read, dirtied, and written, the number of temp blocks read and written, and the time spent reading and writing data file blocks and temporary file blocks (in milliseconds) if
track_io_timing
is enabled\&. A
\fIhit\fR
means that a read was avoided because the block was found already in cache when needed\&. Shared blocks contain data from regular tables and indexes; local blocks contain data from temporary tables and indexes; while temporary blocks contain short\-term working data used in sorts, hashes, Materialize plan nodes, and similar cases\&. The number of blocks
\fIdirtied\fR
indicates the number of previously unmodified blocks that were changed by this query; while the number of blocks
\fIwritten\fR
indicates the number of previously\-dirtied blocks evicted from cache by this backend during query processing\&. The number of blocks shown for an upper\-level node includes those used by all its child nodes\&. In text format, only non\-zero values are printed\&. This parameter defaults to
FALSE\&.
.RE
.PP
WAL
.RS 4
Include information on WAL record generation\&. Specifically, include the number of records, number of full page images (fpi) and the amount of WAL generated in bytes\&. In text format, only non\-zero values are printed\&. This parameter may only be used when
ANALYZE
is also enabled\&. It defaults to
FALSE\&.
.RE
.PP
TIMING
.RS 4
Include actual startup time and time spent in each node in the output\&. The overhead of repeatedly reading the system clock can slow down the query significantly on some systems, so it may be useful to set this parameter to
FALSE
when only actual row counts, and not exact times, are needed\&. Run time of the entire statement is always measured, even when node\-level timing is turned off with this option\&. This parameter may only be used when
ANALYZE
is also enabled\&. It defaults to
TRUE\&.
.RE
.PP
SUMMARY
.RS 4
Include summary information (e\&.g\&., totaled timing information) after the query plan\&. Summary information is included by default when
ANALYZE
is used but otherwise is not included by default, but can be enabled using this option\&. Planning time in
\fBEXPLAIN EXECUTE\fR
includes the time required to fetch the plan from the cache and the time required for re\-planning, if necessary\&.
.RE
.PP
FORMAT
.RS 4
Specify the output format, which can be TEXT, XML, JSON, or YAML\&. Non\-text output contains the same information as the text output format, but is easier for programs to parse\&. This parameter defaults to
TEXT\&.
.RE
.PP
\fIboolean\fR
.RS 4
Specifies whether the selected option should be turned on or off\&. You can write
TRUE,
ON, or
1
to enable the option, and
FALSE,
OFF, or
0
to disable it\&. The
\fIboolean\fR
value can also be omitted, in which case
TRUE
is assumed\&.
.RE
.PP
\fIstatement\fR
.RS 4
Any
\fBSELECT\fR,
\fBINSERT\fR,
\fBUPDATE\fR,
\fBDELETE\fR,
\fBMERGE\fR,
\fBVALUES\fR,
\fBEXECUTE\fR,
\fBDECLARE\fR,
\fBCREATE TABLE AS\fR, or
\fBCREATE MATERIALIZED VIEW AS\fR
statement, whose execution plan you wish to see\&.
.RE
.SH "OUTPUTS"
.PP
The command\*(Aqs result is a textual description of the plan selected for the
\fIstatement\fR, optionally annotated with execution statistics\&.
Section\ \&14.1
describes the information provided\&.
.SH "NOTES"
.PP
In order to allow the
PostgreSQL
query planner to make reasonably informed decisions when optimizing queries, the
pg_statistic
data should be up\-to\-date for all tables used in the query\&. Normally the
autovacuum daemon
will take care of that automatically\&. But if a table has recently had substantial changes in its contents, you might need to do a manual
\fBANALYZE\fR
rather than wait for autovacuum to catch up with the changes\&.
.PP
In order to measure the run\-time cost of each node in the execution plan, the current implementation of
\fBEXPLAIN ANALYZE\fR
adds profiling overhead to query execution\&. As a result, running
\fBEXPLAIN ANALYZE\fR
on a query can sometimes take significantly longer than executing the query normally\&. The amount of overhead depends on the nature of the query, as well as the platform being used\&. The worst case occurs for plan nodes that in themselves require very little time per execution, and on machines that have relatively slow operating system calls for obtaining the time of day\&.
.SH "EXAMPLES"
.PP
To show the plan for a simple query on a table with a single
integer
column and 10000 rows:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN SELECT * FROM foo;
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
Seq Scan on foo (cost=0\&.00\&.\&.155\&.00 rows=10000 width=4)
(1 row)
.fi
.if n \{\
.RE
.\}
.PP
Here is the same query, with JSON output formatting:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN (FORMAT JSON) SELECT * FROM foo;
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
[ +
{ +
"Plan": { +
"Node Type": "Seq Scan",+
"Relation Name": "foo", +
"Alias": "foo", +
"Startup Cost": 0\&.00, +
"Total Cost": 155\&.00, +
"Plan Rows": 10000, +
"Plan Width": 4 +
} +
} +
]
(1 row)
.fi
.if n \{\
.RE
.\}
.PP
If there is an index and we use a query with an indexable
WHERE
condition,
\fBEXPLAIN\fR
might show a different plan:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN SELECT * FROM foo WHERE i = 4;
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
Index Scan using fi on foo (cost=0\&.00\&.\&.5\&.98 rows=1 width=4)
Index Cond: (i = 4)
(2 rows)
.fi
.if n \{\
.RE
.\}
.PP
Here is the same query, but in YAML format:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN (FORMAT YAML) SELECT * FROM foo WHERE i=\*(Aq4\*(Aq;
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\- Plan: +
Node Type: "Index Scan" +
Scan Direction: "Forward"+
Index Name: "fi" +
Relation Name: "foo" +
Alias: "foo" +
Startup Cost: 0\&.00 +
Total Cost: 5\&.98 +
Plan Rows: 1 +
Plan Width: 4 +
Index Cond: "(i = 4)"
(1 row)
.fi
.if n \{\
.RE
.\}
.sp
XML format is left as an exercise for the reader\&.
.PP
Here is the same plan with cost estimates suppressed:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN (COSTS FALSE) SELECT * FROM foo WHERE i = 4;
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
Index Scan using fi on foo
Index Cond: (i = 4)
(2 rows)
.fi
.if n \{\
.RE
.\}
.PP
Here is an example of a query plan for a query using an aggregate function:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN SELECT sum(i) FROM foo WHERE i < 10;
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\:\-\-
Aggregate (cost=23\&.93\&.\&.23\&.93 rows=1 width=4)
\-> Index Scan using fi on foo (cost=0\&.00\&.\&.23\&.92 rows=6 width=4)
Index Cond: (i < 10)
(3 rows)
.fi
.if n \{\
.RE
.\}
.PP
Here is an example of using
\fBEXPLAIN EXECUTE\fR
to display the execution plan for a prepared query:
.sp
.if n \{\
.RS 4
.\}
.nf
PREPARE query(int, int) AS SELECT sum(bar) FROM test
WHERE id > $1 AND id < $2
GROUP BY foo;
EXPLAIN ANALYZE EXECUTE query(100, 200);
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\:\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
HashAggregate (cost=10\&.77\&.\&.10\&.87 rows=10 width=12) (actual time=0\&.043\&.\&.0\&.044 rows=10 loops=1)
Group Key: foo
Batches: 1 Memory Usage: 24kB
\-> Index Scan using test_pkey on test (cost=0\&.29\&.\&.10\&.27 rows=99 width=8) (actual time=0\&.009\&.\&.0\&.025 rows=99 loops=1)
Index Cond: ((id > 100) AND (id < 200))
Planning Time: 0\&.244 ms
Execution Time: 0\&.073 ms
(7 rows)
.fi
.if n \{\
.RE
.\}
.PP
Of course, the specific numbers shown here depend on the actual contents of the tables involved\&. Also note that the numbers, and even the selected query strategy, might vary between
PostgreSQL
releases due to planner improvements\&. In addition, the
\fBANALYZE\fR
command uses random sampling to estimate data statistics; therefore, it is possible for cost estimates to change after a fresh run of
\fBANALYZE\fR, even if the actual distribution of data in the table has not changed\&.
.PP
Notice that the previous example showed a
\(lqcustom\(rq
plan for the specific parameter values given in
\fBEXECUTE\fR\&. We might also wish to see the generic plan for a parameterized query, which can be done with
GENERIC_PLAN:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN (GENERIC_PLAN)
SELECT sum(bar) FROM test
WHERE id > $1 AND id < $2
GROUP BY foo;
QUERY PLAN
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\:\-\-\-\-\-\-\-\-\-\-\-\-
HashAggregate (cost=26\&.79\&.\&.26\&.89 rows=10 width=12)
Group Key: foo
\-> Index Scan using test_pkey on test (cost=0\&.29\&.\&.24\&.29 rows=500 width=8)
Index Cond: ((id > $1) AND (id < $2))
(4 rows)
.fi
.if n \{\
.RE
.\}
.sp
In this case the parser correctly inferred that
$1
and
$2
should have the same data type as
id, so the lack of parameter type information from
\fBPREPARE\fR
was not a problem\&. In other cases it might be necessary to explicitly specify types for the parameter symbols, which can be done by casting them, for example:
.sp
.if n \{\
.RS 4
.\}
.nf
EXPLAIN (GENERIC_PLAN)
SELECT sum(bar) FROM test
WHERE id > $1::integer AND id < $2::integer
GROUP BY foo;
.fi
.if n \{\
.RE
.\}
.sp
.SH "COMPATIBILITY"
.PP
There is no
\fBEXPLAIN\fR
statement defined in the SQL standard\&.
.SH "SEE ALSO"
\fBANALYZE\fR(7)
|