Sub topic 2
Dead Tuples: What `UPDATE` and `DELETE` Leave Behind
This chapter assumes basic SQL, PostgreSQL transactions, indexes, and use of psql. You should know how to run SELECT, UPDATE, and DELETE statements.
PostgreSQL keeps row versions
PostgreSQL uses MVCC, or multiversion concurrency control. MVCC lets transactions read a consistent view of data while other transactions change it. Instead of replacing a row immediately, PostgreSQL writes a new row version and keeps the old version until it is safe to remove.
A row version is the physical copy of a row stored in a table. Each version contains transaction information that PostgreSQL uses to decide which transactions can see it.
Consider this table:
CREATE TABLE accounts (
account_id integer PRIMARY KEY,
balance numeric NOT NULL
);
INSERT INTO accounts VALUES (1, 100);
UPDATE accounts
SET balance = 125
WHERE account_id = 1;After the UPDATE, the table logically contains one account with a balance of 125. Physically, PostgreSQL may contain both versions:
| Row version | Balance | Status |
|---|---|---|
| Older version | 100 | Dead after it is no longer visible |
| Newer version | 125 | Current |
The old version is not removed during the UPDATE. It becomes a dead tuple, which is PostgreSQL's name for a row version that no transaction can see anymore.
What `UPDATE` and `DELETE` do
An UPDATE normally creates a new row version and marks the previous version as replaced. A DELETE marks the current row version as deleted. Neither operation immediately compacts the table.
PostgreSQL must wait before removing the old version. A transaction that started earlier might still need to read it. Once PostgreSQL knows that no active transaction can see the old version, VACUUM can clean it up.
A deleted row follows the same principle:
BEGIN;
DELETE FROM accounts
WHERE account_id = 1;
-- The row is invisible to this transaction after the DELETE.
-- Other transactions may still need the old version.
COMMIT;After the commit, the row is logically gone. Its old tuple remains physically present until cleanup.
Why dead tuples matter
Dead tuples consume table pages. Queries may need to inspect and skip them, increasing I/O and reducing cache efficiency. Large numbers of dead tuples also make sequential scans read more data than the live rows require.
Updates can also affect indexes. If an update changes an indexed column, PostgreSQL generally needs new index entries. The old index entry may remain until index cleanup. Therefore, a table with frequent updates can develop both table bloat and index bloat.
You can inspect estimated live and dead tuple counts with the statistics view:
SELECT relname,
n_live_tup,
n_dead_tup,
round(
100.0 * n_dead_tup /
nullif(n_live_tup + n_dead_tup, 0),
1
) AS dead_pct
FROM pg_stat_user_tables
WHERE relname = 'accounts';These values are estimates collected by PostgreSQL. VACUUM updates this information and performs cleanup. The next chapter explains how table bloat grows from this leftover space.
Exercise
Why does PostgreSQL create a new row version instead of replacing the old row immediately?
Show answer
MVCC keeps the old version available for transactions that still need the earlier, consistent view of the data.