3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "EXPLAIN" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 EXPLAIN \- show the execution plan of a statement
35 EXPLAIN [ ( \fIoption\fR [, \&.\&.\&.] ) ] \fIstatement\fR
37 where \fIoption\fR can be one of:
39 ANALYZE [ \fIboolean\fR ]
40 VERBOSE [ \fIboolean\fR ]
41 COSTS [ \fIboolean\fR ]
42 SETTINGS [ \fIboolean\fR ]
43 GENERIC_PLAN [ \fIboolean\fR ]
44 BUFFERS [ \fIboolean\fR ]
45 SERIALIZE [ { NONE | TEXT | BINARY } ]
47 TIMING [ \fIboolean\fR ]
48 SUMMARY [ \fIboolean\fR ]
49 MEMORY [ \fIboolean\fR ]
50 FORMAT { TEXT | XML | JSON | YAML }
54 This command displays the execution plan that the
56 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\&.
58 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
59 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
61 clause, the planner makes an appropriate interpolation between the endpoint costs to estimate which plan is really the cheapest\&.
65 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\&.
71 .nr an-no-space-flag 1
79 Keep in mind that the statement is actually executed when the
81 option is used\&. Although
83 will discard any output that a
85 would return, other side effects of the statement will happen as usual\&. If you wish to use
92 \fBCREATE TABLE AS\fR, or
94 statement without letting the command affect your data, use this approach:
101 EXPLAIN ANALYZE \&.\&.\&.;
113 Carry out the command and show actual run times and other statistics\&. This parameter defaults to
119 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
121 for more details\&. This parameter defaults to
127 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
133 Include information on configuration parameters\&. Specifically, include options affecting query planning with value different from the built\-in default value\&. This parameter defaults to
139 Allow the statement to contain parameter placeholders like
140 $1, and generate a generic plan that does not depend on the values of those parameters\&. See
142 for details about generic plans and the types of statement that support parameters\&. This parameter cannot be used together with
143 ANALYZE\&. It defaults to
149 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, local blocks and temporary file blocks (in milliseconds) if
153 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
155 indicates the number of previously unmodified blocks that were changed by this query; while the number of blocks
157 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\&. Buffers information is automatically included when
164 Include information on the cost of
166 the query\*(Aqs output data, that is converting it to text or binary format to send to the client\&. This can be a significant part of the time required for regular execution of the query, if the datatype output functions are expensive or if
167 TOASTed values must be fetched from out\-of\-line storage\&.
168 \fBEXPLAIN\fR\*(Aqs default behavior,
169 SERIALIZE NONE, does not perform these conversions\&. If
173 is specified, the appropriate conversions are performed, and the time spent doing so is measured (unless
175 is specified)\&. If the
177 option is also specified, then any buffer accesses involved in the conversions are counted too\&. In no case, however, will
179 actually send the resulting data to the client; hence network transmission costs cannot be investigated this way\&. Serialization may only be enabled when
181 is also enabled\&. If
183 is written without an argument,
190 Include information on WAL record generation\&. Specifically, include the number of records, number of full page images (fpi), the amount of WAL generated in bytes and the number of times the WAL buffers became full\&. In text format, only non\-zero values are printed\&. This parameter may only be used when
192 is also enabled\&. It defaults to
198 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
200 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
202 is also enabled\&. It defaults to
208 Include summary information (e\&.g\&., totaled timing information) after the query plan\&. Summary information is included by default when
210 is used but otherwise is not included by default, but can be enabled using this option\&. Planning time in
211 \fBEXPLAIN EXECUTE\fR
212 includes the time required to fetch the plan from the cache and the time required for re\-planning, if necessary\&.
217 Include information on memory consumption by the query planning phase\&. Specifically, include the precise amount of storage used by planner in\-memory structures, as well as total memory considering allocation overhead\&. This parameter defaults to
223 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
229 Specifies whether the selected option should be turned on or off\&. You can write
233 to enable the option, and
239 value can also be omitted, in which case
255 \fBCREATE TABLE AS\fR, or
256 \fBCREATE MATERIALIZED VIEW AS\fR
257 statement, whose execution plan you wish to see\&.
261 The command\*(Aqs result is a textual description of the plan selected for the
262 \fIstatement\fR, optionally annotated with execution statistics\&.
264 describes the information provided\&.
267 In order to allow the
269 query planner to make reasonably informed decisions when optimizing queries, the
271 data should be up\-to\-date for all tables used in the query\&. Normally the
273 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
275 rather than wait for autovacuum to catch up with the changes\&.
277 In order to measure the run\-time cost of each node in the execution plan, the current implementation of
278 \fBEXPLAIN ANALYZE\fR
279 adds profiling overhead to query execution\&. As a result, running
280 \fBEXPLAIN ANALYZE\fR
281 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\&.
284 To show the plan for a simple query on a table with a single
286 column and 10000 rows:
292 EXPLAIN SELECT * FROM foo;
295 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
296 Seq Scan on foo (cost=0\&.00\&.\&.155\&.00 rows=10000 width=4)
303 Here is the same query, with JSON output formatting:
309 EXPLAIN (FORMAT JSON) SELECT * FROM foo;
311 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
315 "Node Type": "Seq Scan",+
316 "Relation Name": "foo", +
318 "Startup Cost": 0\&.00, +
319 "Total Cost": 155\&.00, +
320 "Plan Rows": 10000, +
331 If there is an index and we use a query with an indexable
335 might show a different plan:
341 EXPLAIN SELECT * FROM foo WHERE i = 4;
344 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
345 Index Scan using fi on foo (cost=0\&.00\&.\&.5\&.98 rows=1 width=4)
353 Here is the same query, but in YAML format:
359 EXPLAIN (FORMAT YAML) SELECT * FROM foo WHERE i=\*(Aq4\*(Aq;
361 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
363 Node Type: "Index Scan" +
364 Scan Direction: "Forward"+
366 Relation Name: "foo" +
368 Startup Cost: 0\&.00 +
372 Index Cond: "(i = 4)"
379 XML format is left as an exercise for the reader\&.
381 Here is the same plan with cost estimates suppressed:
387 EXPLAIN (COSTS FALSE) SELECT * FROM foo WHERE i = 4;
390 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
391 Index Scan using fi on foo
399 Here is an example of a query plan for a query using an aggregate function:
405 EXPLAIN SELECT sum(i) FROM foo WHERE i < 10;
408 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\:\-\-
409 Aggregate (cost=23\&.93\&.\&.23\&.93 rows=1 width=4)
410 \-> Index Scan using fi on foo (cost=0\&.00\&.\&.23\&.92 rows=6 width=4)
418 Here is an example of using
419 \fBEXPLAIN EXECUTE\fR
420 to display the execution plan for a prepared query:
426 PREPARE query(int, int) AS SELECT sum(bar) FROM test
427 WHERE id > $1 AND id < $2
430 EXPLAIN ANALYZE EXECUTE query(100, 200);
433 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\:\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
434 HashAggregate (cost=10\&.77\&.\&.10\&.87 rows=10 width=12) (actual time=0\&.043\&.\&.0\&.044 rows=10\&.00 loops=1)
436 Batches: 1 Memory Usage: 24kB
437 Buffers: shared hit=4
438 \-> Index Scan using test_pkey on test (cost=0\&.29\&.\&.10\&.27 rows=99 width=8) (actual time=0\&.009\&.\&.0\&.025 rows=99\&.00 loops=1)
439 Index Cond: ((id > 100) AND (id < 200))
441 Buffers: shared hit=4
442 Planning Time: 0\&.244 ms
443 Execution Time: 0\&.073 ms
450 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
452 releases due to planner improvements\&. In addition, the
454 command uses random sampling to estimate data statistics; therefore, it is possible for cost estimates to change after a fresh run of
455 \fBANALYZE\fR, even if the actual distribution of data in the table has not changed\&.
457 Notice that the previous example showed a
459 plan for the specific parameter values given in
460 \fBEXECUTE\fR\&. We might also wish to see the generic plan for a parameterized query, which can be done with
467 EXPLAIN (GENERIC_PLAN)
468 SELECT sum(bar) FROM test
469 WHERE id > $1 AND id < $2
473 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\:\-\-\-\-\-\-\-\-\-\-\-\-
474 HashAggregate (cost=26\&.79\&.\&.26\&.89 rows=10 width=12)
476 \-> Index Scan using test_pkey on test (cost=0\&.29\&.\&.24\&.29 rows=500 width=8)
477 Index Cond: ((id > $1) AND (id < $2))
484 In this case the parser correctly inferred that
488 should have the same data type as
489 id, so the lack of parameter type information from
491 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:
497 EXPLAIN (GENERIC_PLAN)
498 SELECT sum(bar) FROM test
499 WHERE id > $1::integer AND id < $2::integer
510 statement defined in the SQL standard\&.
512 The following syntax was used before
514 version 9\&.0 and is still supported:
520 EXPLAIN [ ANALYZE ] [ VERBOSE ] \fIstatement\fR
526 Note that in this syntax, the options must be specified in exactly the order shown\&.