Sub topic 3
What Table Bloat Is and Why It Hurts
Assume you know basic SQL, PostgreSQL transactions, indexes, and how to use psql. This chapter explains the storage problem that PostgreSQL's multiversion concurrency control (MVCC) creates and why routine cleanup matters.
A table is more than its current rows
PostgreSQL uses MVCC to let transactions read a consistent version of data while other transactions modify it. An UPDATE usually does not overwrite a row in place. Instead, PostgreSQL writes a new row version and leaves the old version in the table. A DELETE marks the existing row as deleted rather than immediately removing its bytes.
The old version is called a dead tuple. It becomes removable only after PostgreSQL knows that no active transaction can still see it.
A table stores rows in fixed-size pages. When dead tuples accumulate, the table can occupy more pages than its current live data needs. The unused space inside those pages, along with pages that must still be read, is commonly called table bloat.
Bloat does not mean that PostgreSQL has duplicated the logical data forever. It means that the physical storage contains obsolete row versions or poorly reused space.
Why bloat hurts
A sequential scan reads table pages, not just live rows. If a table has many mostly empty pages, PostgreSQL still spends time reading and processing them. Larger tables also need more disk space and more memory to cache useful data.
| Effect | Why it happens |
|---|---|
| Slower sequential scans | More pages must be read to find the same live rows |
| More disk usage | Old row versions and empty page space remain allocated |
| Higher cache pressure | Bloat displaces useful pages from memory |
| Longer backups | Physical backups include the larger relation |
| More maintenance work | `VACUUM` and statistics collection scan more storage |
Indexes can suffer separately. An index entry points to a row location, so updates and deletes can leave obsolete index entries too. A table can have moderate table bloat but severe index bloat, or the reverse.
Bloat is a workload problem
Frequently updated tables create dead tuples quickly. Tables with wide rows may need more pages for each new version. Updates to indexed columns can also require more index changes. Long-running transactions make the problem worse because they can keep old row versions visible, preventing cleanup.
Consider an orders table that receives new orders and repeatedly updates status, timestamps, and payment details. Its logical row count may stay nearly constant while its physical size grows. Without cleanup, queries that return a small result can still touch a much larger relation.
Bloat can also be temporary. VACUUM may make space reusable inside the existing table without reducing the file's size. That space can help future inserts, so unchanged file size does not prove that VACUUM failed. Returning space to the operating system requires a table rewrite such as VACUUM FULL or an online repacking method, each with different locking and operational costs.
A useful mental model
Track three different quantities:
- Live tuples: current row versions visible to normal transactions.
- Dead tuples: obsolete row versions waiting for safe cleanup.
- Allocated pages: disk space currently assigned to the table, including reusable empty space.
Bloat grows when allocated pages and dead or unused space increase faster than PostgreSQL can reuse them.
Exercise
An UPDATE changes one row, but the table's logical row count stays the same. Why can the table still grow?
Show answer
MVCC writes a new row version and leaves the old version as a dead tuple. Until cleanup makes its space reusable, the table may need additional pages.