EXPLAIN

Name

EXPLAIN  --  Shows the statement execution plan for a supplied query.

Synopsis

EXPLAIN [ VERBOSE ] query        
  

Parameters

VERBOSE

Pass this parameter to indicate that you wish to view verbose information about a query plan.

query

Use this parameter to enter the query you wish to have explained.

Results

NOTICE: QUERY PLAN: plan

Following this message will be an explicit query plan sent from the backend.

EXPLAIN

This message appears below the query plan.

Description

Use the EXPLAIN command to view the plan for a query, as generated by PostgreSQL's planner component. The plan explains how scanning of the tables referenced within your query will be scanned by the database server (either sequentially or through the use of an index). This will also list any algorithms used for scanning multiple tables.

What you will most likely want to use this command for is to determine the estimated execution cost of your specified query. These numbers (the 'cost') are an estimation by the planner component of the length of time necessary to run the query. The numbers represent disk page fetches; the more needed, the higher the number. The first number is the estimated startup time (the time before the first tuple can be returned). The second number is the estimated total time that the query will take to run.

If you pass the VERBOSE parameter, EXPLAIN will display the internal representation of the plan tree. This is fairly indecipherable to the average user and you should only use the parameter during debugging attempts, or other fairly high level interactions with the system.

Examples

The following example shows the results received when xecuting EXPLAIN on the states table, which appears above the output generated by EXPLAIN.

         Table "states"
  Attribute  |  Type   | Modifier
-------------+---------+----------
| 42 | Washington | WA           |
| 51 | Oregon     | OR           |
+----+------------+--------------+
booktown=# EXPLAIN SELECT * FROM states;

NOTICE:  QUERY PLAN:
 
Seq Scan on states  (cost=0.00..20.00 rows=1000 width=28)
  
EXPLAIN