Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

You own the database platform for 300 PostgreSQL clusters. Bloat, wraparound warnings and vacuum-related incidents keep recurring. Design the operating model for MVCC housekeeping across the fleet.

Try it yourself

Isolation levels: what a second read sees

Step through two transactions and see exactly when read committed and repeatable read give different answers.

Read this example as text

T1 at read committed

  1. T1 begins. Transaction T1 starts with isolation level READ COMMITTED. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under read committed each statement looks afresh at what is committed when the statement starts.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 reads 150. The same query, inside one transaction, returned 100 and then 150. This is a non-repeatable read, and read committed allows it.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100, 100, then 150: a non-repeatable read.

T1 at repeatable read

  1. T1 begins. Transaction T1 starts with isolation level REPEATABLE READ. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under repeatable read this first read fixes T1's snapshot: T1 will keep seeing the data as it was committed at this moment.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 still reads 100. Its snapshot was fixed at its first read, so T2's commit is invisible to it. The read is repeatable.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100 three times: its reads were repeatable.

What this example leaves out
  • Modelled on PostgreSQL and MySQL InnoDB, where a plain SELECT reads a snapshot and never waits for a writer.
  • SQL Server's default read committed uses locks instead: there, T1's second read would wait for T2 to finish.
  • Only reads are compared. What happens when T1 also writes the row (a lost update, or an error in PostgreSQL) is not modelled.

At 300 clusters, MVCC housekeeping has to be a platform service rather than each DBA's craft. I would standardise autovacuum settings per workload class with per-table overrides kept as code, monitor wraparound age, dead tuples, the oldest snapshot and replication slots across the fleet, enforce timeouts at role level, keep rehearsed runbooks for wraparound and bloat, give application teams design rules that avoid long transactions and mass deletes, and report the cost of bloat back to the owning teams.

Why this question is asked

Bloat, wraparound warnings and vacuum incidents recur across a large estate for a simple reason: PostgreSQL's MVCC leaves cleanup work behind on every write, and that work is invisible until it is late. Fixing one cluster at a time never ends. Interviewers ask this design question to see whether we can turn the internals into an operating model with standards, signals, runbooks and ownership. In this answer, we will recap why housekeeping exists, then build the model in layers: configuration as code, fleet monitoring, enforced timeouts, runbooks, guidance for application teams and cost reporting, and finish with how to prioritise.

1A Quick Recap: Why MVCC Needs Housekeeping

Every UPDATE and DELETE in PostgreSQL leaves an old row version, a dead tuple, in the table. VACUUM removes dead tuples once no snapshot needs them, and it also freezes old rows, marking them visible to everyone, because transaction ids are 32-bit and would otherwise wrap around after about 2 billion transactions. If vacuum falls behind, tables bloat and queries slow down; if freezing falls far behind, PostgreSQL eventually stops accepting writes to protect data. Autovacuum does this work automatically, but its defaults are tuned for small databases, and anything that holds an old snapshot stops it from removing anything at all.

2Standard Settings by Workload Class, Kept as Code

Not every cluster needs custom tuning. We define three or four workload classes, for example small OLTP, large OLTP with hot tables, append-heavy event stores, and analytics, each with a baseline: more autovacuum workers, a higher autovacuum_vacuum_cost_limit on fast storage, and lower scale factors on large databases. The default autovacuum_vacuum_scale_factor of 0.2 means a 500-million-row table waits for 100 million dead rows before vacuuming, which is far too late.

Large hot tables get per-table overrides, and those overrides live in the same repository as the schema migrations, reviewed like code, so they are not lost when someone rebuilds a cluster.

SQL
-- in the migration repository, applied by the deployment pipeline
ALTER TABLE order_line SET (
autovacuum_vacuum_scale_factor = 0.01, -- vacuum after 1% of rows change, not 20%
autovacuum_vacuum_cost_limit = 2000, -- let this table's vacuum do more IO per round
fillfactor = 90 -- leave room for HOT updates
);

3Fleet Monitoring: The Signals That Predict Incidents

Each cluster exports the same small set of metrics to a central system, so we can rank all 300 by risk on one dashboard. Wraparound risk comes from age(datfrozenxid) per database and age(relfrozenxid) per table. Bloat risk comes from dead tuple ratios in pg_stat_user_tables and table size growth. Horizon holders show up as the oldest age(backend_xmin) in pg_stat_activity, the xmin of replication slots and old prepared transactions. Vacuum health comes from last_autovacuum times and pg_stat_progress_vacuum, which shows a running vacuum's phase and progress.

Fleet signals and thresholds
SignalSourcePage someone when
Wraparound ageage(datfrozenxid)over 1 billion
Oldest snapshotage(backend_xmin), pg_stat_activityolder than 1 hour on OLTP
Replication slotpg_replication_slots xmin, activeinactive with an old xmin
Dead tuple ratiopg_stat_user_tablesover 20% on a large table
Vacuum progresspg_stat_progress_vacuumone table vacuuming for over a day
Example thresholds; each class tunes its own.

4Mandatory Timeouts at Role Level

Most horizon incidents start with an application transaction left open. The standard therefore sets idle_in_transaction_session_timeout and statement_timeout on every application role (ALTER ROLE app SET ...), and transaction_timeout on PostgreSQL 17 and later. Values follow the workload class, for example 60 seconds idle and 30 seconds per statement for OLTP. A team that needs a longer limit, such as a nightly batch role, files an exception that names the role, the value and the reason, and the exception is visible in the configuration repository. This turns a recurring incident into a configuration check that fails safe.

5Runbooks for the Bad Days, Rehearsed

When wraparound age climbs, on-call engineers need a script, not a search engine. The wraparound runbook says how to find the horizon holders and end them, how to run a targeted VACUUM (FREEZE) on the oldest tables first, how PostgreSQL's failsafe (vacuum_failsafe_age) changes vacuum behaviour, and what to do if the database has already stopped accepting writes. The bloat runbook says when plain VACUUM is enough, when to rebuild with pg_repack online, and why VACUUM FULL is a last resort because it blocks reads and writes for the whole rewrite.

We rehearse both on a staging cluster a couple of times a year, with an engineer who has not done it before, because a runbook nobody has followed is a guess.

Step throughWraparound alert runbook
  1. Alert: age(datfrozenxid) over 1 billion
  2. Find horizon holderssessions, slots, prepared transactions, standby feedback
  3. End or fix thempg_terminate_backend, drop dead slots
  4. VACUUM (FREEZE) the oldest tables firstwatch pg_stat_progress_vacuum
  5. Age falling?
    YesClose, write the reviewNoEscalate, add workers and cost limit

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

6Guidance for Application Teams and Cost Reporting

Much of the housekeeping load is created by application design, so the platform publishes a short guide. Keep transactions short and never wait on remote calls inside them. Batch large updates into chunks of, say, 10,000 rows with a commit between chunks, so vacuum can work in between. Partition time-based data by month so old data is removed with DROP or DETACH PARTITION, which leaves no dead tuples, instead of a huge DELETE. Avoid updating indexed columns on hot rows when an append-only design would do.

Finally, we make the problem visible to the people who can fix it. A monthly report shows each team the storage and IO spent on bloat in their databases, estimated from dead tuple and bloat metrics, plus the incidents and timeouts their roles caused. When a team sees that one table costs them several terabytes, partitioning moves up their backlog without a mandate.

7Follow-up questions

How would you prioritize which clusters to fix first?

By how close each is to an outage, then by business impact. Wraparound comes first, because a cluster that stops accepting writes is a full outage: anything with age(datfrozenxid) past about 1 billion is fixed this week. Next come clusters with a horizon holder older than a day, because they get worse every hour. Then bloat, ranked by growth rate and by how critical the service is, so a 2 TB table growing 10% a week on the payments path beats a stable bloated table on an internal tool. The same ranking feeds the dashboard, so the order is visible and argued about with data.

Should we ever turn autovacuum off on a table?

Almost never. Disabling it does not stop anti-wraparound vacuums, which run anyway, and it lets bloat grow unseen. The rare valid case is a table that is bulk loaded and then dropped, where partitioning is the better answer.

How do you keep 300 configurations from drifting?

The class baselines and table overrides are applied by the deployment pipeline, and a nightly job compares each cluster's pg_settings and table options with the repository and reports differences.

8Conclusion

In this answer, we built an operating model for MVCC housekeeping across 300 clusters. It standardises autovacuum by workload class with table overrides as code, monitors wraparound age, dead tuples, the oldest backend_xmin, replication slots and vacuum progress centrally, enforces idle_in_transaction_session_timeout and statement_timeout at role level, keeps rehearsed runbooks for wraparound and pg_repack, teaches teams to keep transactions short, batch updates and partition old data, and reports the cost of bloat back to its owners.

What to say in the interviewAbout 70 seconds aloud

Open with

At this scale I'd treat vacuum as a platform service, not individual DBA work. First, standard autovacuum settings by workload class, say small OLTP, large OLTP, append-heavy and analytics, with per-table overrides like a scale factor of 0.01 on big hot tables, all kept as code and applied by the pipeline so they don't drift.

Then, if they want more

Second, fleet monitoring with the same signals everywhere: age of datfrozenxid and relfrozenxid for wraparound, dead tuple ratios, the oldest backend_xmin, replication slot xmin, and pg_stat_progress_vacuum, so I can rank 300 clusters by risk on one dashboard. Third, mandatory timeouts on every application role, idle_in_transaction_session_timeout and statement_timeout, and transaction_timeout on PostgreSQL 17, with documented exceptions.

Fourth, runbooks for wraparound and bloat, covering finding horizon holders, emergency freeze vacuums and pg_repack, and we rehearse them. Fifth, guidance for application teams: short transactions, batched updates, and partitioning so old data is dropped rather than deleted. And finally, a monthly report of bloat cost per team, because visibility gets things fixed.

For priority, wraparound risk first, then old horizon holders, then bloat by growth and business impact.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 17
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 "You own the database platform for 300 PostgreSQL clusters. Bloat, wraparound warnings...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/c56c829b68/