INFINITE UPSIDE

Technology and operations

PostgreSQL EXPLAIN vs. EXPLAIN ANALYZE

A query plan is not a stopwatch, and ANALYZE is not just a preview.

A database query feels slow, so someone adds an index and hopes the problem takes the hint. PostgreSQL may have other ideas. Before changing the database, look at the plan the system chose and the evidence about how the query actually behaved.

EXPLAIN displays a planned execution path. EXPLAIN ANALYZE executes the statement and adds runtime observations to the plan. That second sentence is the one to read twice: an analyzed statement can have real effects. Use an approved, representative environment and understand the statement before running it.

What is a query plan?

PostgreSQL chooses a sequence of operations for a query. A plan can include scans, filters, joins, sorting and aggregation, among other operations. Each operation is shown as a node in a tree. The planner estimates costs and row counts to choose among possible plans; those estimates are not measurements of what happened in a particular execution.

The PostgreSQL guide to EXPLAIN walks through plan nodes and compares estimates with actual execution. The structure depends on the query, database version, statistics, settings and data. A plan in a tutorial is not evidence of the plan your system will choose.

For a simple fictional query, a plan may show a scan of an orders table, followed by a filter that keeps a small set of rows. Another plan may use an index to find candidates. Neither node name alone establishes a problem. If most rows are needed, scanning a table can be reasonable; if only a few are needed, an index may help, but its usefulness depends on the actual query and distribution.

Estimated cost is not a stopwatch

A plan can display startup cost and total cost in the planner’s cost units, along with an estimated number of rows. Those cost values support relative planning decisions within the planner’s model. Do not read a cost of 100 as 100 milliseconds or as a dollar estimate.

Suppose a fictional plan estimates 100 rows will pass a filter. In a separate observed execution under comparable conditions, the node returns an average of 10,000 rows for one loop. That difference may help explain why the chosen strategy performed differently from expectations, but it does not identify the root cause by itself. The predicate, statistics, data distribution and query context all need inspection.

PostgreSQL’s documentation notes that estimates use available statistics and can be approximate. A recently loaded or heavily changed table may deserve a statistics review under the database team’s normal procedures. Do not run maintenance, change planner settings or create indexes on a production system merely because one number looks surprising.

ANALYZE means the statement runs

The PostgreSQL EXPLAIN reference explicitly states that the ANALYZE option executes the statement. On a write statement, that can change data. Even a SELECT can invoke functions or other behavior with side effects, and a query can consume significant time or resources.

The documentation shows a transaction-and-rollback pattern for certain write experiments. Do not treat rollback as a universal safety shield: not every side effect or external interaction is necessarily undone, and the execution can still acquire locks or affect load while it runs. Follow the organization’s approved test environment and operational procedures. Never paste an unfamiliar write statement into a live system just because it is preceded by EXPLAIN.

There is also a separate PostgreSQL command named ANALYZE that collects statistics. Its name overlaps with the option on EXPLAIN, but they are different actions. Read the actual statement and documentation instead of assuming both uses are merely a preview.

Read estimates and observations together

Suppose an authorized test plan shows a node estimated 500 rows and, in that test, actually produced 5,000 rows with one loop. The observed count is ten times the estimate for that node in that run. That comparison is a reason to investigate the data and assumptions, not proof that the planner is broken or that an index will solve the issue.

Now imagine a different node shows an average of 20 actual rows and 50 loops. PostgreSQL’s guide explains that when a node runs more than once, displayed actual rows and time are averages per execution. Under those simplified figures, multiplying 20 by 50 suggests about 1,000 rows produced across the loops, subject to the displayed rounding and the plan’s meaning. Do not compare the 20-row average directly with a total from a different scope without accounting for loops.

Similarly, a node’s timing can include work associated with its children. Adding every displayed node time as though all times were independent can double-count portions of the execution. Read the documentation for the format and version in use, and ask a database specialist when the plan is material to a production decision.

A single run is evidence, not a benchmark

Test conditions matter. Cache state, concurrent work, parameter values, data volume, locks and the environment can change timings. EXPLAIN ANALYZE also adds measurement overhead. PostgreSQL notes that this profiling overhead can make the measured execution slower than running the query without it.

That does not make the plan useless. It means the team should state the query, parameters, environment, time and relevant conditions when comparing observations. An isolated staging copy that has a hundred rows cannot by itself establish how a production table with millions of differently distributed rows will behave.

Keep the original plan and the plan after a proposed change. If a test improves, check the intended result and side effects as well as the duration. A query that is fast because an incorrect filter excluded required rows has not solved the business problem.

An index is a candidate, not an automatic fix

A plan may reveal that the database examined many rows to return a few, or that a join or sort dominates work. The next step is to understand why. An index might help a particular access pattern, but it also occupies storage and can add work when data changes. The database-index guide covers those tradeoffs; the multicolumn-index guide addresses how column order can affect a specific access path.

Check the query predicate, selectivity, joins, selected columns, actual execution and data distribution. A proposed index that looks attractive in a synthetic example may not be used or may not help the workload that matters. Have the database owner review implementation and rollback before changing shared infrastructure.

What to bring to a performance review

Document the business operation and the exact slow query or approved equivalent, the database version, relevant parameters, environment, observed frequency and the user-visible symptom. Retain the plan with sensitive values removed as appropriate. Mark whether it is estimated only or includes an executed ANALYZE run.

Compare estimated and actual rows at relevant nodes, include loops, and check whether a wait or contention outside the query is part of the reported delay. Agree on a test and success measure before changing the schema or settings. Then verify both the result and the operational impact in the authorized environment.

A plan is a map of one proposed or observed route through the work. It becomes valuable when the team checks where the map and the actual trip diverge, without mistaking an estimate for a stopwatch.

Explore Infinite Upside

For Vijay Sheth’s personal accounts, read Infinite Upside: Building Wealth With Room to Live, published by Sheth Capital, Inc.

Find your edition · Chapter companion

Explore the work behind ownership

Choose an edition

ABOUT THE CONTRIBUTOR

Luna Lim

DIRECTOR OF FINANCIAL RESEARCH & EDITORIAL STRATEGY

SHETH CAPITAL, INC.

Luna Lim writes about business, investing, and the judgment behind financial decisions. She joined PCH Exotics Inc. as Director of Operations in 2022, working across La Jolla and Miami. Today, she reports directly to Vijay Sheth at Sheth Capital, Inc., bringing an operator’s perspective to the ideas she covers.

Based in Pacific Beach, California, Luna is curious about the people behind the numbers: what they’re building, how they weigh uncertainty, and what makes a risk worth taking. Her articles for Infinite Upside follow those questions through the details, with clear explanations and a feel for how decisions play out in everyday business.

More from Luna Lim

The Infinite Upside store