Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

In a PostgreSQL fleet, several heavily updated tables have B-tree indexes three to five times larger than after a fresh rebuild, and queries using them have slowed. How do you explain index bloat, and what process do you set up across teams to manage it?

Index bloat comes from MVCC: updates and deletes leave dead index entries until vacuum removes them, and B-tree pages that are only partly emptied are never merged back, so the index keeps its size while its density falls. Non-HOT updates add a new entry to every index, which makes it worse; PostgreSQL 13 deduplication and 14 bottom-up deletion reduce it. We measure with pgstatindex, rebuild with REINDEX INDEX CONCURRENTLY or pg_repack, prevent it with per-table autovacuum tuning, dropping unused indexes and fillfactor for HOT updates, and run the whole thing as a platform service with dashboards, scheduled rebuilds and alerts.

Why this question is asked

An index that is four times its natural size costs four times the memory to cache and up to four times the reads to scan, so queries that were fast at launch slowly decay. In a fleet, each team tends to discover this during an incident and fix it by hand. Interviewers ask this at senior level to see whether we understand where bloat comes from and whether we can turn a recurring fire into a routine. In this answer, we will recap how PostgreSQL updates rows and indexes, see why indexes bloat and what newer versions do about it, measure and rebuild bloated indexes safely, and design the process for many teams.

1A Quick Recap: MVCC and Index Entries

PostgreSQL uses MVCC, multi-version concurrency control: an UPDATE does not change a row in place but writes a new version of it, and the old version stays until no running transaction can see it. A DELETE only marks the row as deleted. Indexes point to row versions by their physical location, the tuple id.

So after an update, the index normally needs a new entry pointing to the new version, while the old entry still points to the old version. The old entry becomes dead when the old version does, and only vacuum, run by the autovacuum daemon or by hand, removes dead entries from index pages.

2Why B-Tree Indexes Bloat

Vacuum removes dead entries, but it does not compact the tree. If a leaf page held 300 entries and 250 of them were removed, the page now holds 50 and stays a separate page. New entries may reuse that space, but only if their keys belong in that page's key range. PostgreSQL can delete a leaf page that becomes completely empty, but pages that are mostly empty are not merged with their neighbours. Over months of updates the index keeps its number of pages while its average density falls.

Two patterns make this much worse. Non-HOT updates insert a new entry into every index on the table, even indexes whose columns did not change; HOT updates, heap-only tuples, avoid this when no indexed column changes and the new version fits on the same heap page. And a long-running transaction or a forgotten replication slot holds back vacuum, so dead entries pile up faster than they are removed.

A leaf page through its life
MomentEntries on pageDensity
after a fresh REINDEXabout 33090% (B-tree default fillfactor)
after a week of updates and splitsabout 250 live + 80 deadmixed
after vacuumabout 120 live35%, page not merged
months later, three times as many pagesabout 100 livethe index is 3x its natural size
Vacuum makes space reusable but never shrinks or merges a B-tree.

3What Newer PostgreSQL Versions Do About It

PostgreSQL 13 added B-tree deduplication: when many entries have the same key, as in an index on status, they are stored as one key with a list of tuple ids, so the index is much smaller and splits less often. PostgreSQL 14 added bottom-up index deletion: when an update is about to split a leaf page, PostgreSQL first checks whether entries on that page point to dead versions created by updates of the same rows, and removes them instead of splitting. Together they greatly reduce bloat from non-HOT updates, but they do not eliminate it, and an index built before an upgrade to 13 must be rebuilt to use deduplication.

4Measuring and Rebuilding Safely

We measure bloat directly with the pgstattuple extension. pgstatindex() reads the whole index and reports avg_leaf_density, the average fill of leaf pages; a fresh B-tree is around 90, and anything below about 50 is worth a rebuild. Reading every page is expensive on big indexes, so for fleet dashboards we use estimate queries based on table statistics, and confirm with pgstatindex before acting.

To rebuild without blocking writes we use REINDEX INDEX CONCURRENTLY, available since PostgreSQL 12. It builds a new copy of the index while writes continue, then swaps it in and drops the old one. It needs extra disk space for the new copy and waits for running transactions at several points. The external tool pg_repack can also rebuild indexes, and whole tables, online.

SQL
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT pg_size_pretty(pg_relation_size('shipment_status_idx')) AS size, -- 4.1 GB
avg_leaf_density -- 21.4
FROM pgstatindex('shipment_status_idx');
REINDEX INDEX CONCURRENTLY shipment_status_idx; -- writes continue during the rebuild
SELECT pg_size_pretty(pg_relation_size('shipment_status_idx')); -- 1.0 GB
SELECT avg_leaf_density FROM pgstatindex('shipment_status_idx'); -- 89.7

5Preventing Bloat Instead of Fixing It

Rebuilds treat the symptom, so we also reduce the causes. Autovacuum defaults are tuned for average tables: a table is vacuumed after about 20 percent of its rows change, which for a 200-million-row table means waiting for 40 million dead rows. For heavily updated tables we lower autovacuum_vacuum_scale_factor per table, and raise the cost limit so vacuum keeps up. We look in pg_stat_user_indexes for indexes with idx_scan = 0 over weeks, which cost write work and bloat while serving no query, and drop them after checking replicas too. And we lower the table's fillfactor, for example to 85, so updated versions find room on the same page and qualify as HOT, which adds no index entries at all.

SQL
ALTER TABLE shipment SET (autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_cost_limit = 2000);
ALTER TABLE shipment SET (fillfactor = 85); -- applies to new pages; rewrite to apply everywhere
SELECT indexrelname, idx_scan FROM pg_stat_user_indexes
WHERE relname = 'shipment' ORDER BY idx_scan; -- shipment_legacy_ref_idx | 0

6Making It a Platform Job

Across a fleet, the important change is ownership. Instead of each team discovering bloat during an incident, the database platform team runs it as a service. A nightly job estimates bloat for every index and publishes it on a dashboard per database and team. Indexes over a threshold, such as 2 times their estimated natural size and more than 1 GB, are queued for REINDEX CONCURRENTLY in each database's agreed low-traffic window, with checks for disk space and for long-running transactions first. Alerts go to the owning team when autovacuum falls behind or a table's HOT ratio drops, because those predict bloat weeks ahead.

The platform team also publishes a short guide: how to read the dashboard, when to lower fillfactor, how to request a rebuild outside the schedule. Teams keep ownership of their schema decisions, while the repetitive work is automated.

Step throughThe fleet bloat routine
  1. Nightly bloat estimate per indexpublished on a dashboard
  2. Over threshold?2x natural size and over 1 GB
    YesQueue for rebuildNoKeep watching
  3. Pre-checksdisk space, long transactions, window
  4. REINDEX INDEX CONCURRENTLYin the low-traffic window
  5. Clean up and reportdrop invalid leftovers, record size saved

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

7Follow-up questions

What can make REINDEX CONCURRENTLY fail and what does it leave behind?

It can fail on a deadlock, a cancelled session or statement timeout, running out of disk space, or a uniqueness violation when rebuilding a unique index whose data has duplicates. It cannot run inside a transaction block. If it fails, it leaves an invalid index, usually named with a _ccnew suffix, which is still maintained on every write but never used by queries. We find such indexes with indisvalid = false in pg_index and drop them before retrying.

Why not just run VACUUM FULL?

VACUUM FULL rewrites the table and its indexes compactly, but it takes an ACCESS EXCLUSIVE lock for the whole time, blocking reads and writes, which is unacceptable on a busy table. REINDEX CONCURRENTLY and pg_repack do the work online.

How can a long transaction cause bloat in every table?

Vacuum may only remove versions that no running transaction could still see. A transaction open for six hours, or a stale replication slot, holds that horizon back for the whole database, so dead entries accumulate everywhere until it ends.

8Conclusion

In this answer, we saw that MVCC updates and deletes leave dead index entries that vacuum removes without merging partly empty pages, so B-tree indexes keep growing while their density drops, faster with non-HOT updates. PostgreSQL 13 deduplication and 14 bottom-up deletion reduce this. We measure with pgstatindex, rebuild online with REINDEX INDEX CONCURRENTLY or pg_repack, prevent bloat with per-table autovacuum settings, dropping unused indexes and fillfactor for HOT updates, and run the whole process as a platform service with dashboards, scheduled rebuilds and alerts.

What to say in the interviewAbout 90 seconds aloud

Open with

Index bloat comes from MVCC. An update writes a new row version, and unless it is a HOT update, every index on the table gets a new entry while the old one stays until vacuum removes it.

Then, if they want more

Vacuum does remove dead entries, but it never merges partly empty B-tree pages, so after months of updates an index can have three to five times the pages it needs, which costs memory and reads. Long-running transactions make it worse because they stop vacuum from cleaning.

PostgreSQL 13 added deduplication of equal keys and 14 added bottom-up deletion, which removes dead entries before splitting a page, so recent versions bloat less, but it still happens. I would measure with pgstatindex from pgstattuple, where avg_leaf_density near 90 is fresh and below about 50 is worth rebuilding, using cheaper estimates for fleet-wide dashboards.

Rebuilds use REINDEX INDEX CONCURRENTLY, which works online since PostgreSQL 12, or pg_repack. To prevent it, I would tune autovacuum per table for the hot ones, drop indexes with zero scans in pg_stat_user_indexes, and lower fillfactor so updates stay HOT.

For the fleet I would make it a platform job: nightly estimates on a dashboard per team, automatic concurrent rebuilds in agreed windows with disk and long-transaction checks, cleanup of invalid indexes left by failed rebuilds, and alerts when autovacuum falls behind, so teams stop discovering bloat during incidents.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 13, PostgreSQL 12, PostgreSQL 14
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 "In a PostgreSQL fleet, several heavily updated tables have B-tree indexes three to five...": https://scrivio-interview-library.netlify.app/databases/storage-internals/598c730e8c/