EXPLAIN
EXPLAIN
EXPLAIN — show the execution plan of a statement
Synopsis
Description
This command displays the execution plan that the Tacnode planner generates for the supplied statement. The execution plan shows how the table(s) referenced by the statement will be scanned — by plain sequential scan, index scan, etc. — and if multiple tables are referenced, what join algorithms will be used to bring together the required rows from each input table.
The most critical part of the display is the estimated statement execution cost, which is the planner's 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.
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's estimates are close to reality.
Important
Keep in mind that the statement is actually executed when the ANALYZE
option is used. Although EXPLAIN
will discard any output that a SELECT
would return, other side effects of the statement will happen as usual. If you wish to use EXPLAIN ANALYZE
on an INSERT
, UPDATE
, DELETE
, CREATE TABLE AS
, or EXECUTE
statement without letting the command affect your data, use this approach:
Only the ANALYZE
and VERBOSE
options can be specified, and only in that order, without surrounding the option list in parentheses. Prior to Tacnode 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.
Parameters
ANALYZE
Carry out the command and show actual run times and other statistics. This parameter defaults to
FALSE
.VERBOSE
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 "guc-compute-query-id" for more details. This parameter defaults to
FALSE
.COSTS
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
.SETTINGS
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
.BUFFERS
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 (in milliseconds) if "guc-track-io-timing" is enabled. A hit 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 temp blocks contain short-term working data used in sorts, hashes, Materialize plan nodes, and similar cases. The number of blocks dirtied indicates the number of previously unmodified blocks that were changed by this query; while the number of blocks written 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. It defaults to
FALSE
.WAL
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 toFALSE
.TIMING
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 whenANALYZE
is also enabled. It defaults toTRUE
.SUMMARY
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 inEXPLAIN EXECUTE
includes the time required to fetch the plan from the cache and the time required for re-planning, if necessary.FORMAT
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
.boolean
Specifies whether the selected option should be turned on or off. You can write
TRUE
,ON
, or1
to enable the option, andFALSE
,OFF
, or0
to disable it. Theboolean
value can also be omitted, in which caseTRUE
is assumed.statement
Any
SELECT
,INSERT
,UPDATE
,DELETE
,VALUES
,EXECUTE
,DECLARE
,CREATE TABLE AS
, orCREATE MATERIALIZED VIEW AS
statement, whose execution plan you wish to see.
Outputs
The command's result is a textual description of the plan selected for the statement
, optionally annotated with execution statistics. "using-explain" describes the information provided.
Notes
In order to allow the Tacnode 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 ANALYZE rather than wait for autovacuum to catch up with the changes.
In order to measure the run-time cost of each node in the execution plan, the current implementation of EXPLAIN ANALYZE
adds profiling overhead to query execution. As a result, running EXPLAIN ANALYZE
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.
Examples
To show the plan for a simple query on a table with a single integer
column and 10000 rows:
Here is the same query, with JSON output formatting:
If there is an index and we use a query with an indexable WHERE
condition, EXPLAIN
might show a different plan:
Here is the same query, but in YAML format:
XML format is left as an exercise for the reader.
Here is the same plan with cost estimates suppressed:
Here is an example of a query plan for a query using an aggregate function:
Here is an example of using EXPLAIN EXECUTE
to display the execution plan for a prepared query:
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 Tacnode releases due to planner improvements. In addition, the ANALYZE
command uses random sampling to estimate data statistics; therefore, it is possible for cost estimates to change after a fresh run of ANALYZE
, even if the actual distribution of data in the table has not changed.
Compatibility
There is no EXPLAIN
statement defined in the SQL standard.