What is a query execution plan?

In short
A query execution plan is the step-by-step method a database picks to run your SQL query. The planner builds several candidate plans, estimates the cost of each, and runs the cheapest one. You can see the chosen plan with the EXPLAIN command.
How the planner picks a plan
When you send a SQL query to PostgreSQL, the database does not run it straight away. First it works out a plan: a list of operations that together produce the rows you asked for. A part of the database called the planner builds these options and estimates how expensive each one is. The cheapest estimate wins.
The choice matters a lot. The same query can run in milliseconds with one plan and take minutes with another. The planner relies on statistics about your tables, such as how many rows they hold and how values are spread out, so keeping those statistics fresh helps it guess well.
Reading the plan tree
A plan is a tree of nodes. The nodes at the bottom are scan nodes. They read rows from a table, either by going through every page (a sequential scan), by looking up an index, or by using a bitmap built from an index. Nodes above them handle joins, aggregation, sorting and other work on those rows.
The EXPLAIN command prints one line per node. Each line shows the node type and the planner's estimates. Indented lines below it add details, such as the filter or index condition used at that node. The first line, for the top node, shows the total estimated cost of the whole plan.
Each estimate is printed in parentheses. Reading from left to right, you see the estimated start-up cost, the estimated total cost, the estimated number of rows the node outputs, and the estimated average width of each row in bytes. The start-up cost is the work done before the first row can come out, such as sorting. The total cost assumes the node runs to the end. The rows value counts the rows the node emits, not the rows it reads, so a filter lowers it.
The cost is measured in arbitrary units. By convention these units follow disk page fetches, so a sequential page read costs 1.0 by default. Costs only reflect what the planner can change. Time spent sending results to the client is not part of them.
A simple example
Suppose you run a query on a table with no WHERE clause:
EXPLAIN SELECT * FROM tenk1;
The output shows a single Seq Scan node. The planner has decided to read every row, because nothing narrows the search. Now add a condition:
EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100;
The plan changes. The planner uses a Bitmap Index Scan to find matching row locations in an index, then a Bitmap Heap Scan to fetch those rows from the table. It is cheaper than reading the whole table because far fewer pages are visited. For a query that fetches a single row by a unique value, the planner often uses a plain Index Scan instead.
Sorting works the same way. An ORDER BY may be satisfied by an index that already returns rows in the right order, in which case no extra step appears. Otherwise the plan shows an explicit Sort node with its sort key.
Common mistakes
The first mistake is to read only the top line. The total cost is useful, but the nodes underneath show where the work happens. Look for the most expensive node and check whether its row estimate is close to reality.
The second mistake is to expect exact numbers. Statistics come from a random sample, so the estimates can shift after ANALYZE runs. Small changes are normal. Large gaps between estimated and actual rows are a clue that statistics are stale or that a condition is hard to estimate.
The third mistake is to compare costs across different queries as if they were times. Costs are planner units, not seconds. Use them to compare plans for the same query.
When to use EXPLAIN
Use EXPLAIN when a query is slower than you expect, before you add an index or rewrite SQL. It shows whether the planner is scanning a whole table, sorting a large set or ignoring an index. If you need to pass the plan to a tool, use one of the machine-readable output formats, such as JSON, instead of the default text format.
If you are new to SQL indexes, read our guide on database indexes first. It explains the structures the planner relies on.
Alternatives and related tools
Some databases offer similar commands under different names, and their output varies. The idea stays the same: the database picks a plan, and you can inspect it. Tools that show plans graphically exist too, but they read the same underlying information. Start with the plain text output, learn the common node types, and move to other formats when you need automation.
FAQ
Does EXPLAIN run my query?
A plain EXPLAIN only shows the plan the planner would use, without running the query. Adding ANALYZE runs the query and reports the real timings and row counts next to the estimates.
Why does the plan change after I add an index?
A new index gives the planner another way to find rows. If the estimated cost of that route is lower, the planner chooses it. The change is not guaranteed, because a sequential scan can still be cheaper for many rows.
What does a large gap between estimated and actual rows mean?
It often means the statistics are out of date or do not capture how values are spread in a column. Running ANALYZE on the table is a common first step to refresh them.
Are the cost numbers in seconds?
No. Costs are arbitrary units based on planner settings, with sequential page reads as the reference point. Use them to compare plans for the same query, not to predict run time.
Sources
This page is based on the official documentation below. Read it for full detail.
Query execution plan — official documentation ↗Query execution plan in the news
Pinecone Releases Open Source pg_pinecone Extension for Vector Search in PostgreSQL
Pinecone has released pg_pinecone, an Apache 2.0 licensed PostgreSQL extension that adds its own vector search engine to existing pgvector columns. The company says it applies filters during search rather than after, and reports benchmark results against pgvector's HNSW index.
AlloyDB Omni Can Now Call Local Models Like Gemma 4 Directly From SQL
Google says its AlloyDB Omni PostgreSQL-compatible database now connects to locally hosted large language models such as Gemma 4 and Qwen3.8. The integration lets SQL queries call a model that runs inside the same environment as the data.
pgjumbo Adds Write Previews and Plan Comparison to a New PostgreSQL Client
Benjamin Ibrulj has released pgjumbo, a PostgreSQL client for macOS, Windows and Linux. It shows the likely impact of writes before they run and offers an optional AI assistant built on Claude Code.


