PostgreSQL interview question · Most asked

How do you read EXPLAIN ANALYZE output in PostgreSQL?

Summary

Read the plan tree from the innermost nodes outward. For each node compare estimated rows with actual rows, look at actual time (per loop, multiplied by loops), and add BUFFERS to see how many pages came from cache versus disk. Big estimate errors and nodes with most of the time are where to look.

Longer Answer

  • Node types: Seq Scan, Index Scan, Index Only Scan, Bitmap Heap Scan; joins Nested Loop, Hash Join, Merge Join; Sort (watch for external merge Disk), HashAggregate.
  • rows=1000 estimated versus actual rows=950000 usually means bad statistics or correlated columns, leading to a wrong join strategy.
  • Rows Removed by Filter shows wasted work an index could avoid.
  • EXPLAIN (ANALYZE, BUFFERS) shows shared hit (cache) versus read (disk).

EXPLAIN ANALYZE executes the statement; wrap writes in BEGIN ... ROLLBACK. Tools such as explain.depesz.com or explain.dalibo.com visualise plans.

Key Points

  • Inner nodes first; time x loops.
  • Estimated vs actual rows.
  • BUFFERS: cache vs disk.
  • Wrap DML in a rolled-back transaction.

Explanation

What interviewers look for

Comparing estimates with actuals and connecting a bad estimate to a bad plan choice.

Follow-up questions

  • What would you do if estimates are badly off?

More most-asked PostgreSQL questions

← All PostgreSQL questions