Developer and Text Tools

How to Read PostgreSQL EXPLAIN ANALYZE and Fix Slow Queries

Learn to read PostgreSQL EXPLAIN ANALYZE: nodes, costs, loops, self time and row estimates, plus the usual fixes for scans, spills and nested loops.

Saizul Amin
Saizul Amin2 Nov 2026 · 3 min read
How to Read PostgreSQL EXPLAIN ANALYZE and Fix Slow Queries

How to Read PostgreSQL EXPLAIN ANALYZE and Fix Slow Queries

Every PostgreSQL query is executed according to a plan: a tree of operations such as scans, joins, sorts and aggregates. EXPLAIN shows that plan, and EXPLAIN ANALYZE also runs the query and reports what really happened. Reading this output is the most useful skill for fixing slow queries, and it is easier than it looks once you know where to look. This guide walks through it with the free Postgres EXPLAIN plan visualizer.

Get a good plan to look at

Run EXPLAIN (ANALYZE, BUFFERS) in front of your query. ANALYZE executes the statement and adds actual times, row counts and loops; BUFFERS adds how many 8 kB pages came from memory and how many from disk. Because the statement really runs, wrap data-changing queries in BEGIN and ROLLBACK. Use realistic parameters: a plan for a customer with three orders says little about one with three million.

Read the tree from the inside out

The plan is printed as an indented tree. The deepest nodes run first and feed rows upward. Each node line shows the estimated startup and total cost, estimated rows and width, then, with ANALYZE, the actual startup and total time in milliseconds, actual rows and loops. Detail lines underneath show the filter, the index condition, the join condition, sort keys and sort method.

Loops and self time

Times on a node include its children, and they are per loop. A nested loop's inner side can run thousands of times, so a node showing 0.05 ms may in fact cost 50 ms. The useful number is self time: actual total time multiplied by loops, minus the time of the children. The visualizer computes it for every node and colours nodes by their share of the total, so the expensive part of the plan stands out immediately. The flame view shows the same thing as nested bars.

Compare estimates with reality

The planner chooses join types and scan methods based on estimated row counts. When an estimate is off by a factor of ten or a hundred, the chosen plan is often wrong: a nested loop chosen for 10 expected rows becomes painful for 100,000 real ones. Large differences usually mean stale statistics, so run ANALYZE on the table. Columns that are correlated, such as city and postcode, mislead the planner because it assumes independence; extended statistics with CREATE STATISTICS fix that.

The usual suspects

A sequential scan with a filter that removes most rows is the classic missing index. If the scan reads a million rows to keep twenty thousand, an index on the filtered columns lets PostgreSQL read only what it needs. Put the most selective equality column first and range conditions later.

A sort whose method says external merge, or a hash that needs more than one batch, has run out of work_mem and is writing temporary files. Raising work_mem for that query or session, or providing an index in the sort order, removes the spill. Be careful raising it globally, because every sort in every connection may use that much memory.

A nested loop whose inner side is a sequential scan executed thousands of times needs an index on the join column, or better estimates so the planner picks a hash join.

Finally, check the summary lines. Planning time larger than execution time hints at prepared statements, and JIT compilation on short queries often costs more than it saves.

Measure the fix

Save the original plan, make one change, and run EXPLAIN ANALYZE again. Compare execution time and buffers, not just cost. Run the query twice when cache effects matter, because the first run may read from disk and the second from memory.

Limits

Plans with parallel workers report averaged times, so shares are approximate. An index that helps one query adds write cost and storage to every insert and update, so measure on realistic data before adding it in production. Hints in the visualizer are rules of thumb; they do not replace understanding your data and workload.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages