Sub topic 12
Long-Running Transactions and Why They Cause Bloat
Prerequisites
This chapter assumes that you know basic SQL and understand BEGIN, COMMIT, and ROLLBACK. You should also know that PostgreSQL uses a snapshot to decide which row versions a query can see.
How an old snapshot causes bloat
An UPDATE does not overwrite a row in place. PostgreSQL creates a new row version and marks the old version as no longer current. A DELETE marks the existing version as deleted.
The old version is called a dead tuple after no transaction needs to see it. VACUUM can then make its space available for reuse.
A long-running transaction can keep an old snapshot. That snapshot might still be able to see a row version that a newer transaction replaced. PostgreSQL must preserve that old version until the long-running transaction ends.
This creates a simple chain:
The transaction does not need to be actively running a query. A session that is idle in transaction can still hold the transaction open after its last statement took a snapshot.
Finding the transaction that holds back vacuum
pg_stat_activity shows transaction timing and the snapshot horizon associated with a backend:
SELECT pid,
usename,
state,
xact_start,
query_start,
backend_xmin,
query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
ORDER BY xact_start;backend_xmin is the oldest transaction ID that this session may still need to see. A very old value can prevent vacuum from removing dead tuples.
You can compare the age of active horizons:
SELECT pid,
state,
xact_start,
age(backend_xmin) AS xmin_age,
query
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC;Look especially for sessions with an old xact_start, an old backend_xmin, and the state idle in transaction. Connection pools and application code commonly cause this pattern when they begin a transaction and fail to commit or roll it back promptly.
Measuring the accumulated dead tuples
PostgreSQL exposes estimated tuple counts through pg_stat_user_tables:
SELECT relname,
n_live_tup,
n_dead_tup,
last_autovacuum,
last_autoanalyze
FROM pg_stat_user_tables
WHERE relname = 'accounts';A high n_dead_tup value indicates cleanup work, but it does not by itself prove that a long transaction is responsible. Check the transaction horizon and workload as well. Updates that happen frequently, failed vacuum settings, and indexes that need cleanup can also contribute to table growth.
Practical prevention
- Keep transactions short.
- Do not leave a connection
idle in transaction. - Set an appropriate
idle_in_transaction_session_timeout. - Make application code handle errors with
ROLLBACK. - Monitor transaction age and dead-tuple counts.
- Investigate long transactions before increasing vacuum thresholds.
Exercise
Why can an idle transaction cause table bloat even when it is not running a query?
Show answer
If it holds an old snapshot, PostgreSQL must preserve row versions that the snapshot could still see. VACUUM cannot remove those versions until the transaction ends.