PostgreSQL 18 introduces two new functions, `pg_restore_relation_stats` and `pg_restore_attribute_stats`, along with `pg_dump --statistics-only`, making optimizer statistics portable. This allows exporting production statistics and injecting them into test or local databases so the query planner produces realistic execution plans without needing production data. The post walks through practical examples: injecting table-level stats to change row estimates, adding column-level histogram bounds to trigger index scans, and simulating skewed MCV distributions to produce different plans per value. A full CI workflow is outlined, along with caveats around autovacuum overwriting injected stats and limitations with extended statistics (not covered until PostgreSQL 19).
Table of contents
The problempg_restore_relation_statspg_restore_attribute_statsInjecting a skewed distributionpg_dumpKeeping injected statistics aliveWhat's not covered?SecuritySort: