liveBook · Manning

Optimizer statistics

As explained in PostgreSQL Mistakes and How to Avoid Them

Optimizer statistics are summarized data-distribution measurements that a query planner uses to estimate query costs and select execution plans.

Optimizer statistics summarize data characteristics so PostgreSQL can estimate query costs and choose an execution plan. Important inputs include cardinality, distinct-value counts, and histograms. section 6.7

Treating statistics collection as optional is a performance mistake. Disabling autovacuum or autoanalyze, or weakening autovacuum for a write-intensive workload, can leave statistics inaccurate while bloat accumulates. section 6.7 section 6.13

Use these operational checks when query performance deteriorates:

  • Inspect EXPLAIN output to identify whether inaccurate estimates contribute to a poor plan. section 6.13
  • Keep autovacuum effective rather than disabling it to save resources; write-heavy systems need maintenance capacity. section 6.7
  • If maintenance competes with application work, scale the system, reduce costly queries, or reconsider the database architecture instead of suppressing statistics collection. section 6.7

A tempting production shortcut

A write-intensive PostgreSQL deployment turns down autovacuum to reduce resource consumption. Over time, bloat grows and optimizer statistics become inaccurate, increasing the chance of poor plans and application delays. Checking EXPLAIN provides a direct way to investigate the slow query; restoring effective maintenance addresses the underlying operational mistake. section 6.7 section 6.13

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains cardinality, distinct values, and histograms as planner inputs but does not detail their collection or estimation algorithms.
Examples
Uses write-intensive workloads, weakened autovacuum, accumulating bloat, inaccurate statistics, and EXPLAIN diagnosis as concrete cases.
What you can do after
Diagnose/debug with it by checking EXPLAIN and preserve effective autovacuum rather than trading short-term resource savings for degraded plans.
How it connects
Connects optimizer statistics with query planning, autovacuum, autoanalyze, bloat, and EXPLAIN-based troubleshooting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.