Scrivio Interview library
ConceptFoundations8 min read · updated 12 November 2026

What is the difference between EXPLAIN and EXPLAIN ANALYZE in PostgreSQL, and what are the cost, rows and width numbers in a line such as: Seq Scan on orders (cost=0.00..18334.00 rows=1000000 width=52)?

EXPLAIN shows the plan the PostgreSQL planner would choose, with estimates only, and never runs the query. EXPLAIN ANALYZE really executes the query and adds the actual time, actual rows and loop count for every node. In a line such as Seq Scan on orders (cost=0.00..18334.00 rows=1000000 width=52), cost is the startup and total cost in abstract planner units, rows is the estimated number of rows the node returns, and width is the estimated average row size in bytes.

Why this question is asked

Almost every performance investigation in PostgreSQL starts with a query plan, so being able to read one is a basic skill for any backend developer. Interviewers ask this question because many people run EXPLAIN ANALYZE without knowing that it executes the statement, or read the cost as milliseconds, and both mistakes lead to wrong conclusions or even to deleted data. In this answer, we will see what the planner is and what EXPLAIN shows, how EXPLAIN ANALYZE differs and why it is dangerous for writes, what the cost, rows and width numbers mean (we will even rebuild the 18334 from the table size), and how to read a plan tree from the inside out.

1A Quick Recap of the Planner

When we send a SQL statement to PostgreSQL, it does not simply run it. The planner, the part of the database that decides how to execute a query, first builds several possible ways to get the result: read the whole table, use an index, join tables in one order or another. It gives each candidate a cost, an estimate of how much work it will take, and picks the cheapest one. That chosen strategy is the plan.

The planner never sees the data while it plans. It works from statistics that ANALYZE collected earlier, such as the number of rows and pages in a table and the most common values of each column. So every number in a plan produced by plain EXPLAIN is a prediction, and a prediction can be wrong.

2What Plain EXPLAIN Shows

Plain EXPLAIN asks the planner for its plan and prints it, and that is all. The query is not executed, no rows are read and no locks on rows are taken, so it returns in milliseconds even for a query that would run for an hour.

SQL
EXPLAIN SELECT * FROM orders;
-- QUERY PLAN
-- Seq Scan on orders (cost=0.00..18334.00 rows=1000000 width=52)

Notice that the line tells us the node type (Seq Scan, a sequential scan that reads every page of the table in order), the table, and three estimates in brackets. Because nothing ran, there is no time in the output at all. This is the safe tool to use first, and the only safe one for a statement that changes data.

3Reading cost, rows and width

The cost has two parts written as startup..total. The startup cost is the work done before the node can return its first row, and the total cost is the work to return all rows. A sequential scan can return its first row immediately, so its startup cost is 0.00. A Sort node, by contrast, has a high startup cost, because it must read all of its input before it can hand back the first sorted row.

The cost is not in milliseconds. It is in abstract planner units, scaled so that reading one page sequentially costs seq_page_cost, which is 1.0 by default. We can rebuild the number in our example. A sequential scan costs the number of pages times seq_page_cost plus the number of rows times cpu_tuple_cost, which is 0.01 by default.

SQL
SELECT relpages, reltuples FROM pg_class WHERE relname = 'orders';
-- relpages = 8334, reltuples = 1000000
-- 8334 pages * 1.0 (seq_page_cost) = 8334
-- 1000000 rows * 0.01 (cpu_tuple_cost) = 10000
-- total cost = 18334
The three numbers in one plan line
FieldExampleMeaning
cost (startup)0.00work before the first row can be returned
cost (total)18334.00work to return all rows, in planner units, not ms
rows1000000estimated rows this node returns
width52estimated average size of one returned row in bytes

So 18334 means roughly eight thousand page reads plus a million cheap row checks, expressed in the planner's own currency. It lets the planner compare plans with each other; it does not tell us how long the query will take on our hardware.

rows=1000000 is the estimated number of rows this node will return, not the number it reads. With a WHERE clause, a sequential scan still reads every row, but rows shows only those expected to pass the filter. width=52 is the estimated average size in bytes of each returned row, which the planner uses to estimate memory for sorts and hashes. Selecting fewer columns lowers the width.

4What EXPLAIN ANALYZE Adds

EXPLAIN ANALYZE plans the query, then really executes it, throws the result rows away and prints the plan with measured values next to the estimates.

SQL
EXPLAIN ANALYZE SELECT * FROM orders WHERE status = 'NEW';
-- Seq Scan on orders (cost=0.00..20834.00 rows=50000 width=52)
-- (actual time=0.015..95.210 rows=48210 loops=1)
-- Filter: (status = 'NEW'::text)
-- Rows Removed by Filter: 951790
-- Planning Time: 0.120 ms
-- Execution Time: 97.402 ms

Now we see actual time in real milliseconds (time to the first row..time to the last row), actual rows, and loops, the number of times this node was executed. Comparing rows=50000 with actual rows=48210 tells us the estimate was good. When estimates and actuals differ by ten times or more, we have usually found the reason for a bad plan.

5Running EXPLAIN ANALYZE Safely on Writes

But there is a catch. Because the statement really runs, EXPLAIN ANALYZE on an INSERT, UPDATE or DELETE really inserts, updates or deletes. The safe habit is to wrap it in a transaction and roll it back.

SQL
BEGIN;
EXPLAIN ANALYZE DELETE FROM orders WHERE created_at < '2020-01-01';
-- Delete on orders (actual time=... rows=0 loops=1) ...
ROLLBACK; -- the rows are back; nothing was deleted for good

Notice that even inside a rolled-back transaction the statement still takes row locks and does all the work while it runs, so on a busy production table this is still something to do with care, ideally on a copy.

6Reading the Plan Tree From the Inside Out

Real plans are trees. Each node gets rows from its children, does its job, and passes rows to its parent. PostgreSQL prints the root first and indents the children, so we read the plan from the most indented node outwards, which is also the order in which data flows.

Java
EXPLAIN SELECT c.name, count(*)
FROM orders o JOIN customers c ON c.id = o.customer_id
GROUP BY c.name;
-- HashAggregate (cost=26000.00..26100.00 rows=10000 width=40)
-- -> Hash Join (cost=330.00..21000.00 rows=1000000 width=32)
-- Hash Cond: (o.customer_id = c.id)
-- -> Seq Scan on orders o (cost=0.00..18334.00 rows=1000000 width=8)
-- -> Hash (cost=205.00..205.00 rows=10000 width=40)
-- -> Seq Scan on customers c (cost=0.00..205.00 rows=10000 width=40)
Step throughHow to read a plan
  1. Find the most indented nodesthey run first and read the tables
  2. Check rows and width at each nodeestimated output, not rows read
  3. Move up one level at a timeeach parent consumes its children's rows
  4. Subtract child cost from parent costa parent's cost already includes its children
  5. With ANALYZE, compare estimate and actuala 10x gap points to the cause

5 steps in order. Play them, or step through them one at a time.

Here the two Seq Scan nodes run first, the Hash node builds a hash table from customers, the Hash Join matches orders against it, and the HashAggregate on top groups the result. An important detail is that a parent's cost includes the cost of its children. The Hash Join's total of 21000 already contains the 18334 of the orders scan and the 205 of the customers scan, so we never add the costs of a parent and its child together. To find where the work is, we look at how much each node adds over its children.

7Follow-up questions

Why can EXPLAIN ANALYZE itself be slower than the real query?

Because it measures time for every row at every node. To do that it reads the system clock repeatedly, and on some machines and virtual machines reading the clock is expensive, so a query that processes millions of rows can take noticeably longer under EXPLAIN ANALYZE than when it runs normally. The option TIMING OFF keeps the actual row counts and loops but drops the per-node timing, which removes most of that overhead. The pg_test_timing tool shows how expensive the clock is on a given server.

What does the BUFFERS option add?

EXPLAIN (ANALYZE, BUFFERS) adds, for each node, how many 8 kB pages were found in PostgreSQL's shared buffer cache (shared hit), how many had to be read from the operating system (read), and how many were dirtied or written. This shows how much data a query touches, which is often more stable and more useful than its timing. In recent versions, ANALYZE includes buffer information by default; check your version.

Is a lower cost always a faster query?

Usually, within one query on one server, but not always. The cost is the planner's estimate based on statistics and cost settings. If the statistics are stale or the settings do not match the hardware, a plan with a lower cost can run slower. Costs are also not comparable between different queries in any strict way, so the actual time from EXPLAIN ANALYZE is what we trust in the end.

8Conclusion

In this answer, we saw that EXPLAIN only asks the planner for its predicted plan, while EXPLAIN ANALYZE runs the statement and reports actual time, actual rows and loops, which is why we wrap writes in BEGIN and ROLLBACK. We decoded cost as startup..total in planner units where a sequential page read costs 1, rebuilt 18334 from 8334 pages and a million rows, and learned that rows and width are the estimated output rows and average row size. Finally, we read a plan tree from the innermost node outwards, remembering that parent costs include their children.

What to say in the interviewAbout 90 seconds aloud

Open with

EXPLAIN shows me the plan the planner has chosen, but only its estimates; the query does not run. EXPLAIN ANALYZE actually executes the query and adds actual time, actual rows and loops for every node, so I can compare what the planner expected with what really happened.

Then, if they want more

Because it really executes, an EXPLAIN ANALYZE on an UPDATE or DELETE changes data, so I always wrap it in BEGIN and ROLLBACK, and preferably run it on a copy.

In a line like Seq Scan on orders with cost 0.00..18334.00, the first number is the startup cost, the work before the first row comes out, and the second is the total cost. These are abstract planner units where reading one page sequentially costs 1 by default, not milliseconds.

For a sequential scan the total is pages times seq_page_cost plus rows times cpu_tuple_cost, so 8334 pages and a million rows at 0.01 give exactly 18334. rows is the estimated number of rows the node returns, and width is the estimated average row size in bytes, 52 here.

Plans are trees: I read them from the most indented node outwards, and I remember that a parent's cost already includes its children, so I look at what each node adds. The most useful thing to check with ANALYZE is a big gap between estimated and actual rows, because that is usually why the planner picked a bad plan.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between EXPLAIN and EXPLAIN ANALYZE in PostgreSQL, and what are...": https://scrivio-interview-library.netlify.app/databases/query-optimization/e0ad2328fc/