EXPLAIN (ANALYZE, BUFFERS): A Short Method for Finding the Expensive Part
When a PostgreSQL query is slow, an index is an easy suggestion and a poor first diagnosis. EXPLAIN (ANALYZE, BUFFERS) gives you a way to compare what the planner expected with what actually happened, and to see where shared buffers were hit or read.
Start at the root of the plan, then inspect the children with the largest actual time and row counts. Compare estimated rows with actual rows. A large mismatch can push the planner toward a join order or scan method that looks cheap on paper and expensive in reality. Check loops too: a node that takes a small amount of time once may be costly when executed for every row from its parent.
Read buffer metrics as context, not a grade. A shared hit means PostgreSQL found a page in shared buffers; a shared read means it loaded a page into shared buffers. Neither tells you by itself whether the storage was slow, whether the operating-system cache helped, or whether the query is acceptable under normal concurrency. Pair the plan with application latency and system-level storage metrics.
ANALYZE executes the statement. Do not casually run it on a production UPDATE, DELETE, or function with side effects. If you need to inspect a modifying query, wrap it in a transaction and roll it back only when the effects are fully understood; external side effects may not roll back. Prefer staging data for invasive experiments.
After identifying the expensive node, change one thing: query shape, statistics, index, or data distribution. Re-run with the same parameters and comparable cache conditions. Keep a record of the old and new plan. The goal is not to force an index scan; it is to reduce end-to-end cost without making writes, other queries, or operational recovery worse.
The PostgreSQL EXPLAIN guide explains plan fields and execution timing. See also monitoring statistics for workload-level context.