Franck PachotDatabase Developer Advocate Minibook 17 · Database field guides All minibooks

Database field guide · 17

Optimizer Statistics

Cardinality, correlation, and adaptive evidence

Understand what histograms, column groups, sampling, partition statistics, and feedback can tell an optimizer.

Franck Pachot7 chaptersstatistics · cardinality · histograms · feedback

Build the mental model before choosing the mechanism.

This minibook was AI-generated from Franck Pachot's archived blog posts. Links to the original articles are included for source context and verification.

01

Statistics compress reality

An optimizer substitutes summaries for reading all data during planning. Row counts, distinct values, null fractions, density, and histograms preserve selected properties of a distribution.

  • Every summary loses information.
  • Sampling introduces bounded uncertainty.
  • Statistics age matters only relative to changed distribution.

02

Histograms model skew

Frequency and bucketed histograms improve equality and range estimates when values are not uniform, but bind handling and endpoint representation determine whether that knowledge is usable.

  • Collect histograms on evidence, not every column.
  • Popular and rare values may need different plans.
  • String endpoints and collation can limit precision.

03

Columns are not independent

Country and postal code, status and close date, or tenant and identifier often correlate. Extended statistics and Oracle column groups model facts that single-column summaries cannot.

  • Dependencies improve combined selectivity.
  • Multivariate NDV improves GROUP BY estimates.
  • Expression statistics must match query expressions.

04

Partitions need two scales

Local statistics describe one partition; global statistics describe the whole object. Incremental maintenance and synopses avoid rescanning all historical data after loading one partition.

  • Skew between partitions defeats naive roll-up.
  • Pruning does not eliminate the need to cost remaining partitions.
  • Publish new statistics through a controlled workflow.

05

Runtime evidence can adapt

Dynamic sampling, cardinality feedback, statistics collectors, and advisors fill gaps at parse or after execution. They have memory, persistence, and version-specific behavior.

  • Feedback treats a symptom unless the missing fact is identified.
  • Adaptive branches still consume planning and sometimes runtime work.
  • Know when learned evidence is invalidated.

06

Gather without surprise

Pending statistics, history, restore points, and representative tests make statistics changes reversible. A gather job is a production change because it can change every dependent plan.

  • Capture plans before publishing new statistics.
  • Use table-specific preferences for volatile objects.
  • Validate representative binds and partition ages.

07

Field manual

Concrete mechanics, diagnostic evidence, and executable patterns to carry into a real system.

01

Read PostgreSQL summaries

pg_stats reveals the lossy model used for one column. Compare MCV frequency, histogram coverage, null fraction, and correlation with the predicate that was misestimated.

SELECT attname, null_frac, n_distinct, most_common_vals,
       most_common_freqs, histogram_bounds, correlation
FROM pg_stats WHERE schemaname='public' AND tablename='orders';
02

Model correlation

PostgreSQL extended statistics can record dependencies, multivariate distinct counts, and multicolumn most-common values. ANALYZE populates the object after creation.

CREATE STATISTICS orders_tenant_status
  (dependencies, ndistinct, mcv)
ON tenant_id, status FROM orders;
ANALYZE orders;
03

Publish Oracle statistics safely

Pending statistics allow gathering and session-scoped testing before publication. Keep history retention sufficient to restore a known set.

EXEC dbms_stats.set_table_prefs(USER,'ORDERS','PUBLISH','FALSE');
EXEC dbms_stats.gather_table_stats(USER,'ORDERS');
ALTER SESSION SET optimizer_use_pending_statistics=TRUE;
-- Test, then DBMS_STATS.PUBLISH_PENDING_STATS.
04

Inspect estimate error

Runtime row-source statistics turn a vague bad plan into a cardinality problem at a specific operation. Gather them selectively because instrumentation has cost.

SELECT * FROM TABLE(dbms_xplan.display_cursor(
  sql_id => :sql_id, cursor_child_no => :child_no,
  format => 'ALLSTATS LAST +PREDICATE +PEEKED_BINDS'));

08

Source articles

Optional deep dives with the complete experiments and product-version context behind this guide.