Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

After a PostgreSQL major upgrade across 40 clusters, several services see plan regressions on skewed data (a few tenants own most rows). How do you manage the upgrade so plans do not regress unexpectedly next time?

Plans regress after a major upgrade mainly because pg_upgrade did not carry the planner statistics, so the first hours run on missing statistics, and because planner changes meet skewed data. Next time, we run vacuumdb --analyze-in-stages right after the upgrade (PostgreSQL 18 can keep statistics through pg_upgrade; check your version), capture top queries and plans beforehand and compare them on a production-sized staging copy, add extended statistics for skewed tenants, roll out cluster by cluster with a canary and a tested rollback, and record it all in a shared playbook.

Why this question is asked

Major upgrades are routine work at scale, and across 40 clusters they are a program, not an event. When some services regress after every upgrade, teams start postponing upgrades, which is worse: old versions lose support and security fixes. Interviewers ask this question to see whether we understand why plans change after an upgrade and whether we can turn that understanding into a repeatable process. In this answer, we will recap why plans change, fix the statistics gap, build a before-and-after plan comparison, deal with skewed tenants, design the rollout and rollback, and write the playbook.

1Why Plans Change After an Upgrade

A query plan depends on three things: the query, the statistics about the data, and the planner's cost model. A major upgrade can change the last two. pg_upgrade, the tool that upgrades a cluster in place by reusing the data files, historically did not transfer the optimizer statistics stored in pg_statistic. Until ANALYZE runs on each table, the planner works with defaults, and skewed data, such as a few tenants owning most rows, is exactly where defaults are most wrong. PostgreSQL 18 can carry statistics over during pg_upgrade, though extended statistics may still need a fresh ANALYZE; check your version's release notes.

The planner itself also changes between versions: new join strategies, different cost estimates, new optimisations. These are improvements on average, but on a specific skewed query a new choice can be slower. So we need both a fast way to restore statistics and a way to find changed plans before users do.

2Restoring Statistics Immediately

Right after the upgrade, before or as traffic returns, we run ANALYZE across the cluster. vacuumdb --analyze-in-stages does it in three passes: first with a very small statistics target so every table quickly gets rough statistics, then a medium target, then the normal one. Plans become reasonable within minutes and accurate within the hour, instead of waiting for a full analyze of the largest tables.

YAML
pg_upgrade --old-bindir ... --new-bindir ... --link
# start the new cluster, then:
vacuumdb --all --analyze-in-stages --jobs 8
# Generating minimal optimizer statistics (1 target)
# Generating medium optimizer statistics (10 targets)
# Generating default (full) optimizer statistics

3Comparing Plans Before and After

Before the upgrade, we capture the workload that matters. pg_stat_statements gives the top queries by total time and calls, with their normalised text. auto_explain, configured with a threshold such as 200 ms, logs the plans of slow executions. We keep both as a baseline per cluster.

Then we restore a recent production backup, with real data volume and real skew, into staging, upgrade it with the same procedure, run ANALYZE the same way, and replay the top queries with realistic parameters, including the large tenants. We compare plan shapes and timings with the baseline and investigate every query that is more than, say, twice as slow. Synthetic data cannot reproduce this: the regressions come from skew, so the test needs the real distribution.

Step throughUpgrade with plan checks
  1. Capture baselinepg_stat_statements top 200, auto_explain plans
  2. Restore production copy to stagingreal volume and skew
  3. Upgrade and analyze in stagessame steps as production
  4. Replay top queries, big tenants includedcompare plans and timings
  5. Any query more than 2x slower?
    YesFix: stats, extended stats, query, settingNoApprove for canary

5 steps in order. Play them, or step through them one at a time.

4Handling Skewed Tenants

For queries filtered by tenant, the planner needs to know that tenant 7 has 200 million rows and tenant 9,000 has 50. Raising the statistics target on tenant_id keeps more most-common values, and extended statistics on tenant_id with correlated columns fix combined filters. For the largest tenants, list partitioning, one partition per big tenant and a default partition for the rest, gives each its own statistics and plan.

Prepared statements need special attention. After five executions, PostgreSQL may switch from a custom plan, made for the actual tenant value, to a generic plan, made for any value. For skewed data, the generic plan can be good for small tenants and terrible for large ones, or the other way round. Setting plan_cache_mode = force_custom_plan for the affected service, per role or per session, removes that switch at the cost of planning each execution. This is worth checking after every upgrade, because the cost comparison behind the switch can change.

SQL
ALTER TABLE events ALTER COLUMN tenant_id SET STATISTICS 2000;
CREATE STATISTICS events_tenant_type (dependencies, mcv) ON tenant_id, event_type FROM events;
ANALYZE events;
ALTER ROLE reporting_service SET plan_cache_mode = force_custom_plan;

5Rolling Out Cluster by Cluster

We never upgrade all 40 clusters in one window. We start with a low-risk cluster, then a canary from each important workload type, watch latency, errors and the top queries' timing for a few days, and only then move to the rest in waves.

Each upgrade needs a rollback path that has actually been tested. pg_upgrade in link mode modifies the old data directory as soon as the new cluster starts, so going back means a restore. Two good options are setting up logical replication from the new version back to an old-version cluster, so we can switch back with recent data, or a tested restore from a backup taken just before, with a known recovery time. Alternatively, the upgrade itself can be done with logical replication to a new-version cluster, which keeps the old cluster intact until the switch.

6Recording It in a Playbook

Finally, we write the steps, scripts, checks and the problems found into an upgrade playbook shared across teams: how to capture baselines, how to build the staging copy, which settings and statistics fixes were needed and why, the rollout order and the rollback steps. Each upgrade updates the playbook. After two or three rounds, upgrades become boring, which is exactly the goal.

7Follow-up questions

Why not just pin the old plans?

PostgreSQL has no built-in plan pinning. Extensions such as pg_hint_plan can force plans, but pinned plans do not adapt as data grows, and they hide the cause. Fixing statistics and settings lets the planner choose well for today's data and tomorrow's. Hints remain a narrow, documented exception for a proven case.

How long should the canary run before the next wave?

Long enough to cover the workload's cycle: a full business day for most services, and a full week if there are weekly batch jobs or reports. The point is to see the heavy queries actually run on the new version before upgrading the clusters that run them too.

8Conclusion

In this answer, we saw that plans regress after a major upgrade because statistics may be missing until ANALYZE runs and because planner changes meet skewed data. We restore statistics immediately with vacuumdb --analyze-in-stages, capture top queries and plans with pg_stat_statements and auto_explain and compare them on a production-sized staging copy, give skewed tenants extended statistics, higher targets, partitioning or plan_cache_mode settings, roll out with canaries and a tested rollback, and keep the lessons in a shared playbook.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two causes. First, pg_upgrade traditionally does not carry the planner statistics, so until ANALYZE runs the planner is guessing, and on skewed data, where a few tenants own most rows, guesses are badly wrong.

Then, if they want more

PostgreSQL 18 can keep statistics through pg_upgrade, but I would check the version notes, and extended statistics may still need an analyze. So the first rule is to run vacuumdb analyze-in-stages right after the upgrade, which gives rough statistics in minutes and full ones soon after.

Second, the planner changes between versions, so I want to find changed plans before users do. Before the upgrade I would capture the top queries from pg_stat_statements and slow plans with auto_explain, then restore a production-sized copy to staging, upgrade it the same way, replay those queries including the biggest tenants, and investigate anything more than about twice as slow.

For skewed tenants, the fixes are a higher statistics target on tenant_id, extended statistics, partitions for the largest tenants, and plan_cache_mode force_custom_plan where generic prepared plans go wrong. The rollout is cluster by cluster, starting with a canary per workload type, with a tested rollback, either logical replication back to the old version or a timed restore. And everything goes into a shared upgrade playbook that each round improves.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 18
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "After a PostgreSQL major upgrade across 40 clusters, several services see plan...": https://scrivio-interview-library.netlify.app/databases/indexing/47ea4643c3/