Learning center

Copied PostgreSQL statistics do not reproduce the plan on an empty table

Importing production statistics into an empty copy does not bring the production plan with it, because the planner uses the table's actual size. Give the copy a comparable number of pages, and check what the statistics reveal before you share them.

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.

CopyPlanEstimated rows
SourceIndex Scan153
Schema only, then ANALYZESeq Scan, cost 0.001
Schema, and pg_dump --statistics-only from the sourceSeq Scan, cost 0.001
The third, after inserting 20,000 rows with no ANALYZEIndex Scan15
The fourth, after ANALYZEBitmap Heap Scan10,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#

QuestionCopy
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.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading