Production query plans without production data

· Source: Simon Willison's Weblog · Field: Technology & Digital — Software Development & Engineering, Database Systems · Depth: Intermediate, quick

Summary

PostgreSQL 18, released in September 2025, introduces new functions, `pg_restore_relation_stats()` and `pg_restore_attribute_stats()`, that enable developers to replicate production query plans in development environments without needing to copy large datasets. These functions allow users to manually inject or restore database statistics, which are crucial for the query planner's decision-making. For instance, one can simulate a column's data distribution, like a `status` column being 95% `delivered`, to observe how PostgreSQL might choose between an index scan or a full table scan. Statistics dumps are notably small, typically under 1MB for databases with hundreds of tables, contrasting sharply with potentially hundreds of gigabytes of production data. SQLite offers a similar capability through its writable `sqlite_stat1` and `sqlite_stat4` tables, allowing manual control over query planner statistics.

Key takeaway

For database administrators and developers troubleshooting query performance issues, these new PostgreSQL 18 functions and existing SQLite capabilities are critical. You can now accurately predict production query plans in a development environment by importing only tiny statistical metadata, saving significant time and resources compared to copying entire production databases. This allows for more precise optimization and debugging of complex queries.

Key insights

Replicating production query plans in development is possible by transferring small statistical metadata, not full datasets.

Principles

Method

Use `pg_restore_attribute_stats()` to manually define column statistics for PostgreSQL query plan simulation.

In practice

Topics

Best for: Software Engineer, Data Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Simon Willison's Weblog.