EXPLAIN (Show the Query Plan)
Last updated: 2026-11-04
EXPLAIN (Show the Query Plan)
EXPLAIN shows the plan the planner picked for a statement: sequential scan or index scan, estimated row counts, and costs. It does not run the query, so it costs almost nothing, which makes it the first stop when investigating a slow query. Adding ANALYZE actually executes the statement and reports real timings.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
EXPLAIN [ ( <option> [, ...] ) ] <statement>
-- common options
-- ANALYZE run the statement and report actual timings
-- BUFFERS show shared buffer hit statistics
-- VERBOSE print full output-column information
-- FORMAT TEXT | JSON | YAML
⚙️ Parameters
| ANALYZE | Executes the statement and reports actual timings |
|---|---|
| BUFFERS | Shows buffer hit and read statistics |
| VERBOSE | Prints the full output-column list |
| FORMAT | Output format: TEXT, JSON, or YAML |
Returns:No return value. The plan is printed as a text table.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
DROP TABLE
CREATE TABLE
INSERT 0 1000
ANALYZE
QUERY PLAN
--------------------------------------------------------
Seq Scan on orders (cost=0.00..19.50 rows=667 width=17)
Filter: (amount > '500'::numeric)
📌 Key Points
- EXPLAIN only plans; use EXPLAIN ANALYZE for real timings — and note that it really does run the statement, so UPDATE and DELETE statements change data.
- cost=startup..total is the planner's estimate from statistics, not elapsed time.
- A large gap between estimated rows and actual rows means the statistics are stale — run ANALYZE.
- BUFFERS reports shared buffer hits, which pinpoints I/O-bound queries.
- FORMAT JSON or YAML makes the plan machine-readable for slow-query log analysis.
- A Seq Scan is not automatically worse than an Index Scan: for large fractions of a table the sequential scan is faster.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhat privileges does EXPLAIN need?
AYou need SELECT on the tables involved; EXPLAIN ANALYZE runs the statement for real and therefore needs the corresponding write privileges on writable tables.
QWhy do the EXPLAIN costs not match the real elapsed time?
ACosts are the planner's relative unit for comparing plans, not milliseconds. For real elapsed time, read the actual time reported by EXPLAIN ANALYZE.