PostgreSQL and MySQL inside · senior
VACUUM, bloat and transaction id wraparound
Dead row versions must be cleaned by VACUUM. When something blocks the cleaning, tables bloat and slow down, and in the worst case PostgreSQL runs out of transaction ids and refuses writes.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Autovacuum starts for a table when the dead rows pass a threshold: a base number plus a scale factor times the table size. The default scale factor is 0.2 (20% of the table), which is too slow for a very big or very hot table. You can set it per table.
- Plain VACUUM makes space reusable inside the file but rarely gives it back to the operating system. VACUUM FULL rewrites the table under an ACCESS EXCLUSIVE lock, so it blocks reads and writes. pg_repack rewrites a table online.
- What blocks cleanup: the oldest open transaction (backend_xmin in pg_stat_activity), an abandoned replication slot with an old xmin (it also keeps WAL on disk), a replica with hot_standby_feedback, and prepared transactions.
- How to look: pg_stat_user_tables (dead rows, last autovacuum), the autovacuum log line 'dead but not yet removable', pg_stat_activity, pg_replication_slots, pg_prepared_xacts.
- Transaction ids are 32 bits. For every id, two billion ids are older and two billion are newer, so every table must be vacuumed (frozen) at least once every two billion transactions.
- Warnings start when a database is 100 million transactions from the wraparound point. With fewer than 3 million left, PostgreSQL refuses commands that need a new transaction id, so writes fail while reads still work.
- Fix for a wraparound warning: find and remove the blocker (an idle-in-transaction session, a stale slot), then run a plain VACUUM on the oldest tables. The docs warn against VACUUM FULL here.
- Watch age(datfrozenxid) per database and age(relfrozenxid) per table, and alert long before the limit. Also run ANALYZE after a big load, because stale statistics give bad plans.
The red pen
Common traps: mistake → fix
Mistake: VACUUM FULL is the normal fix for a bloated table
Fix: it locks the table; use plain VACUUM, then an online rewrite tool if you must shrink the file.
Mistake: Wraparound is only a warning, it can wait
Fix: when ids run out, PostgreSQL stops accepting writes.
Mistake: Adding disk fixes bloat
Fix: the dead rows are still there, and the cause (a blocker) is still there.
Mistake: Autovacuum is slow because it is badly written
Fix: often something is holding the old rows, so it cannot remove them.
Easy to hard
Questions interviewers ask, easy to hard
- What does autovacuum do, and when does it start?
- A table has 2 million live rows and takes 180 GB. What do you check?
- What is an abandoned replication slot, and why is it dangerous?
- What does the message 'must be vacuumed within 9,812,004 transactions' mean, and what do you do?
- How would you shrink a bloated table without downtime?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.