Browse topics

EXPLAIN ANALYZE

Version
19
Review
Draft

Summary#

EXPLAIN shows the plan chosen for a statement. With ANALYZE, PostgreSQL also runs the statement and reports what each plan node actually did.

Syntax#

sql
EXPLAIN statement;

EXPLAIN (ANALYZE, BUFFERS) statement;

EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
statement;

Example#

The output below is from PostgreSQL 19.0, produced with psql -X -q -A -P null=NULL -v ON_ERROR_STOP=1 -f example.sql in a scratch database. The script rolls back at the end, so it leaves nothing behind. For repeatable output, it raises the statistics target to sample the whole demo table and turns off parallel plans, displayed costs, timings, the summary and buffer counts. Keep costs, timings and buffers when you investigate a real query. Plans can differ with other data or settings.

sql
BEGIN;
SET LOCAL max_parallel_workers_per_gather = 0;
SET LOCAL default_statistics_target = 1000;

CREATE TEMPORARY TABLE measurements (
    id     integer NOT NULL,
    device integer NOT NULL,
    value  integer NOT NULL
);

INSERT INTO measurements
SELECT n, n % 500, n % 97
FROM generate_series(1, 50000) AS g(n);
ANALYZE measurements;

EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM measurements WHERE device = 7;

CREATE INDEX measurements_device_idx ON measurements (device);
ANALYZE measurements;

EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM measurements WHERE device = 7;

-- ANALYZE executes the statement, so undo data changes.
SAVEPOINT before_delete;

EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
DELETE FROM measurements WHERE device = 7;

SELECT count(*) AS after_explain FROM measurements;
ROLLBACK TO SAVEPOINT before_delete;
SELECT count(*) AS after_rollback FROM measurements;

ROLLBACK;

Expected output#

Text
QUERY PLAN
Seq Scan on measurements (actual rows=100.00 loops=1)
  Filter: (device = 7)
  Rows Removed by Filter: 49900
(3 rows)
QUERY PLAN
Bitmap Heap Scan on measurements (actual rows=100.00 loops=1)
  Recheck Cond: (device = 7)
  Heap Blocks: exact=100
  ->  Bitmap Index Scan on measurements_device_idx (actual rows=100.00 loops=1)
        Index Cond: (device = 7)
        Index Searches: 1
(6 rows)
QUERY PLAN
Delete on measurements (actual rows=0.00 loops=1)
  ->  Bitmap Heap Scan on measurements (actual rows=100.00 loops=1)
        Recheck Cond: (device = 7)
        Heap Blocks: exact=100
        ->  Bitmap Index Scan on measurements_device_idx (actual rows=100.00 loops=1)
              Index Cond: (device = 7)
              Index Searches: 1
(7 rows)
after_explain
49900
(1 row)
after_rollback
50000
(1 row)

Common mistakes#

EXPLAIN ANALYZE executes INSERT, UPDATE, DELETE and MERGE too. Use a scratch database when a statement can have lasting effects, such as advancing a sequence. A rollback undoes the table changes in this example. When a plan node runs more than once, its actual row count is an average per loop.

Version applicability#

The example uses the SUMMARY option added in PostgreSQL 10. Since PostgreSQL 18, ANALYZE includes buffer information by default, row counts can show fractions and index scan nodes report Index Searches. The chosen plan can also differ between versions.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0