Quick revision
The short answer
Index bloat is space inside index pages that is held by dead or removed entries, or left empty, and is not reused well, so the index is bigger and needs more I/O than its live data requires. In PostgreSQL, REINDEX rebuilds an index but blocks writes to the table, while REINDEX CONCURRENTLY (PostgreSQL 12 and later) does not, though it takes longer and can leave an invalid index if it fails. In MySQL InnoDB, OPTIMIZE TABLE or ALTER TABLE ... FORCE rebuilds the table and its indexes, mostly online, and dropping and re-adding an index also works.
What to remember
In this answer, we saw that index bloat is space held by dead or removed entries, or left in half-empty pages, that is not reused efficiently, so the index is larger and needs more I/O than its live data requires. We measured it with pgstatindex's leaf density. In PostgreSQL, REINDEX blocks writes while REINDEX CONCURRENTLY, from version 12, does not but takes longer and can leave an invalid index that we find with pg_index.indisvalid and drop. In MySQL, OPTIMIZE TABLE or ALTER TABLE ... FORCE rebuilds the table and indexes mostly online, and dropping and re-adding an index rebuilds one. Rebuilds cost I/O and replication traffic, so we schedule them and watch replica lag.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Index bloat is space inside index pages that is held by dead or removed entries, or left empty, and is not reused well, so the index is bigger and needs more I/O than its live data requires. In PostgreSQL, REINDEX rebuilds an index but blocks writes to the table, while REINDEX CONCURRENTLY (PostgreSQL 12 and later) does not, though it takes longer and can leave an invalid index if it fails. In MySQL InnoDB, OPTIMIZE TABLE or ALTER TABLE ... FORCE rebuilds the table and its indexes, mostly online, and dropping and re-adding an index also works.
Why this question is asked
Bloat is one of the quiet ways a database slows down. Nothing fails, but the same query reads more pages each month, the cache holds less useful data and disk usage grows faster than the data. Fixing it requires a rebuild, and choosing the wrong rebuild command on a busy table can block writes for the whole duration. Interviewers ask this to check that we know what bloat is, how to find it, and which commands are safe in production. In this answer, we will see how bloat forms, how to measure it, the PostgreSQL commands and their locks, the MySQL commands, and how to schedule rebuilds safely.
1A Quick Recap of How Bloat Forms
A B-tree index is made of pages, 8 KB in PostgreSQL and 16 KB in InnoDB, each holding many entries. When rows are deleted or updated, their old index entries become dead. In PostgreSQL, vacuum later removes dead entries and the space inside the page can be reused, but only for new entries whose keys belong on that same page. Pages that become completely empty can be recycled, but half-empty pages are not merged back together. If the workload deletes old keys and inserts new keys elsewhere in the tree, as with a timestamp or a sequence, the freed space is in the wrong place and stays unused.
So bloat is not lost data; it is wasted space. An index whose pages are on average 30 percent full needs about three times as many pages as a freshly built one, and every scan reads three times as many pages.
| Fresh index | Bloated index | |
|---|---|---|
| Live entries | 10,000,000 | 10,000,000 |
| Average leaf density | about 90 percent | about 30 percent |
| Leaf pages | about 40,000 | about 120,000 |
| Pages read for a range of 1 percent | about 400 | about 1,200 |
2Measuring Bloat
In PostgreSQL, the pgstattuple extension measures it directly. pgstatindex reads an index and reports its leaf density, the average fullness of leaf pages, and its fragmentation. A freshly built B-tree has a density near 90 percent, because the default fillfactor for B-tree indexes is 90.
pgstatindex reads the whole index, so on very large indexes we run it off-peak or use estimation queries based on catalog statistics, which are cheaper but approximate. In MySQL, information_schema.TABLES shows DATA_FREE and INDEX_LENGTH, and the innodb_index_stats table gives page counts per index; comparing them with the expected size gives an estimate.
3Rebuilding in PostgreSQL: REINDEX and Its Locks
REINDEX builds a new copy of the index from the table, which is compact, and then replaces the old one. Plain REINDEX holds a lock on the table that blocks INSERT, UPDATE and DELETE for the whole build, and it also blocks reads that would use that index. On a large table that can be many minutes of blocked writes, so it is for maintenance windows only.
REINDEX CONCURRENTLY, available since PostgreSQL 12, builds the new index while writes continue. It creates a second index, fills it in two passes while tracking concurrent changes, waits for transactions that could still use the old one, then swaps them and drops the old one. It takes longer and uses more resources, needs space for both copies at once, and cannot run inside a transaction block.
Notice the failure case. If a concurrent build is interrupted, it leaves an index marked invalid, flagged by pg_index.indisvalid = false. The planner never uses it, but writes still maintain it, so it costs write time and space until we drop it. Checking for invalid indexes after every concurrent build, and in regular monitoring, is a small habit that prevents a silent cost.
4Rebuilding in MySQL InnoDB
InnoDB rebuilds indexes by rebuilding the table. OPTIMIZE TABLE on an InnoDB table is mapped to ALTER TABLE ... FORCE, which recreates the table and all its indexes and then updates statistics. With InnoDB online DDL, this runs mostly online: concurrent reads and writes continue during the copy, and an exclusive metadata lock is needed only briefly at the start and the end. Those brief locks still have to wait for long-running transactions on the table, and while waiting they block new queries, so a long transaction can turn a short lock into an outage.
Adding ALGORITHM=INPLACE, LOCK=NONE makes the statement fail immediately if it cannot run without blocking, instead of silently taking a stronger lock. Dropping and re-adding one index in the same statement rebuilds just that index. For very large or busy tables, tools such as gh-ost or pt-online-schema-change give more control over load and replication lag.
5Scheduling Rebuilds Safely
A rebuild reads the whole table and writes a whole new index, which costs I/O and generates WAL or binlog traffic that replicas must replay. We schedule rebuilds in low-traffic windows, run one at a time per cluster, watch replica lag and stop if it grows, and make sure there is disk space for the old and new copies at once. Then we fix the cause, such as autovacuum settings or the workload pattern, so the index does not bloat again within weeks.
6Follow-up questions
Does VACUUM FULL fix index bloat?
Yes, VACUUM FULL rewrites the table and rebuilds all its indexes, so it removes table and index bloat together. But it takes an ACCESS EXCLUSIVE lock for the whole operation, blocking reads as well as writes, so on a live table we use REINDEX CONCURRENTLY for indexes or pg_repack, which rebuilds a table and its indexes online with only short locks.
Can REINDEX CONCURRENTLY rebuild every index?
Not all of them. It cannot rebuild indexes on system catalogs concurrently, and REINDEX DATABASE or SYSTEM forms have restrictions; check your version. For ordinary user indexes, including those backing primary keys and unique constraints, it works, and a unique index rebuild will fail if duplicates exist, leaving an invalid index to drop.
7Conclusion
In this answer, we saw that index bloat is space held by dead or removed entries, or left in half-empty pages, that is not reused efficiently, so the index is larger and needs more I/O than its live data requires. We measured it with pgstatindex's leaf density. In PostgreSQL, REINDEX blocks writes while REINDEX CONCURRENTLY, from version 12, does not but takes longer and can leave an invalid index that we find with pg_index.indisvalid and drop. In MySQL, OPTIMIZE TABLE or ALTER TABLE ... FORCE rebuilds the table and indexes mostly online, and dropping and re-adding an index rebuilds one. Rebuilds cost I/O and replication traffic, so we schedule them and watch replica lag.
Open with
Index bloat means the index takes more pages than its live entries need. Deletes and updates leave dead entries; vacuum removes them in PostgreSQL, but the free space can only be reused by keys that belong on the same page, and half-empty pages are not merged back.
Then, if they want more
So with patterns like deleting old timestamps and inserting new ones, pages stay mostly empty, the index grows, and every scan reads more pages. I would measure it with pgstatindex from pgstattuple: a fresh B-tree is around 90 percent leaf density, and a bloated one might be 30.
To rebuild in PostgreSQL there is REINDEX, which blocks writes on the table for the whole build, so it is for maintenance windows, and REINDEX CONCURRENTLY, from version 12, which lets writes continue but takes longer, needs space for both copies, and if it fails leaves an invalid index.
I would look for those with pg_index indisvalid false and drop them, because they are still maintained on writes. In MySQL InnoDB, OPTIMIZE TABLE maps to ALTER TABLE FORCE, which rebuilds the table and indexes, mostly online with online DDL, though it needs a brief metadata lock at the start and end.
Dropping and adding an index in one ALTER rebuilds just that index. Either way, a rebuild is heavy I/O and replication traffic, so I schedule it off-peak, one at a time, and watch replica lag.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 12
- 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.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
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 "What does index bloat mean, and what commands rebuild an index in PostgreSQL and in...": https://scrivio-interview-library.netlify.app/databases/indexing/0466f617c3/