Three copies of one table#
The source has 200,000 orders, 0.1% of them open, and an index on status. The query is SELECT * FROM orders WHERE status = 'open'. Run with PostgreSQL 18.
| Copy | Plan | Estimated rows |
|---|---|---|
| Source | Index Scan | 153 |
Schema only, then ANALYZE | Seq Scan, cost 0.00 | 1 |
Schema, and pg_dump --statistics-only from the source | Seq Scan, cost 0.00 | 1 |
The third, after inserting 20,000 rows with no ANALYZE | Index Scan | 15 |
The fourth, after ANALYZE | Bitmap Heap Scan | 10,000 |
In the third row, the import had worked: pg_stats listed the source's most common values. The plan did not change, because the table had no pages. The manual explains that the planner fetches the actual current number of pages, and if that differs from relpages, it scales reltuples. Row estimation examples
In the fourth, the rows that were inserted are half open, and the plan still follows the imported 0.1%. pg_class still said 200,000 tuples and 1,471 pages from the import, while the estimate of 15 was scaled to the real size. ANALYZE then replaced the imported statistics with the synthetic ones. Autovacuum can run that ANALYZE without you, so the reproduction may change while you look at it.
ALTER TABLE orders SET (autovacuum_enabled = false); -- on the scratch copy only
Choose a reproduction#
| Question | Copy |
|---|---|
| Does the query parse, and does an index match? | Schema only |
| Why does the planner choose this plan? | Schema, imported statistics and synthetic rows that fill a similar number of pages, with no ANALYZE |
| How long does it take? | Data of similar size and distribution, on similar hardware |
| Is the result correct? | Rows designed for the case |
pg_dump --statistics-only is listed in the pg_dump reference. Check that your client version has it. Planner settings have to match too; the EXPLAIN handoff lists them.
Statistics contain data#
The dump for this table has this line:
'most_common_vals', '{closed,open}'::text,
For a status column that is harmless. For an email, name or diagnosis column, the most common values and histogram bounds are real values from production. Read the file, and treat it with the same approval as a data extract. To leave a column out, delete its pg_restore_attribute_stats call and say so, because plans that depend on it will differ.
Worked example#
This example is fictional. Ana sends schema and statistics to show a bad plan. Ben gets a sequential scan with cost 0 and replies that it looks fine. Ana's message gets two more lines: the table's page count, and a generator for rows that fill as many pages. With autovacuum off for that table, Ben's plan matches the shape of production's and its estimate is close. He then tries an extended statistics object on the copy. Whether it is faster in production is a separate test.
Return to handing over evidence.