Sphere
Log in Join

Postgres 15 VACUUM not reclaiming disk space: dead tuples, bloat and VACUUM FULL

Our events table on Postgres 15.4 hit 340 GB on a 400 GB volume. The retention job deletes anything older than 30 days, every night, and supposedly autovacuum was on. Disk kept climbing. I assumed VACUUM was broken. It wasn't, quite.

## Checking what autovacuum was doing

First stop was pg_stat_user_tables:

sql
SELECT relname, n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'events';

 relname | n_live_tup | n_dead_tup | last_autovacuum
---------+------------+------------+-------------------------------
events | 21840391 | 61029338 | 2024-05-19 03:12:44.221069+00

More dead tuples than live ones, and autovacuum had run. So it was running but couldn't reclaim anything. The usual culprit is an old transaction still holding a snapshot:

sql
SELECT pid, state, age(clock_timestamp(), xact_start) AS age, query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
ORDER BY xact_start;

There was a reporting session idle in transaction for six hours. A dead tuple isn't removable while any transaction might still need to see it, so vacuum skipped nearly everything. That's the whole story behind "VACUUM ran but did nothing" more often than people expect.

## Plain VACUUM doesn't shrink files

I ran this and watched the size:

sql
VACUUM (VERBOSE, ANALYZE) events;
SELECT pg_size_pretty(pg_total_relation_size('events'));

The size was unchanged, which is normal. Plain VACUUM marks space as reusable inside the table; it doesn't truncate the file unless the free space happens to be at the very end. New inserts will reuse it eventually, so this isn't necessarily a problem. Our problem was that dead tuples kept being created faster than they were reused.

To measure bloat properly I tried pgstattuple and got:

ERROR:  function pgstattuple(unknown) does not exist
LINE 1: SELECT * FROM pgstattuple('events');
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

It's an extension:

sql
CREATE EXTENSION pgstattuple;
SELECT dead_tuple_percent, free_percent FROM pgstattuple('events');

 dead_tuple_percent | free_percent
--------------------+--------------
61.42 | 58.91

Yep, bloated. One caveat: pgstattuple reads the whole relation, so don't run it on a busy production table without thinking about the I/O. A rough estimate from pg_stat_user_tables is often enough for a first pass.

## VACUUM FULL and the lock you'll hate

VACUUM FULL events rewrites the table into a new file and swaps it in. It needs ACCESS EXCLUSIVE the whole time, which blocks every read and write. I tried it with a short timeout during a busy window:

sql
SET lock_timeout = '5s';
VACUUM FULL events;

ERROR:  canceling statement due to lock timeout

Fair. For a 340 GB table the rewrite itself would have taken well over an hour anyway. We ended up using pg_repack 1.4.8, which rebuilds online with only brief locks, during a low-traffic Sunday slot. It needed superuser, which took a separate conversation with our DBA.

## What we changed

The reporting job now ends its transactions, and we set idle_in_transaction_session_timeout = '10min' so a forgotten BEGIN can't pin a snapshot all day. Autovacuum settings for events got more aggressive: autovacuum_vacuum_scale_factor = 0.02 instead of the default 0.2. Disk is back down to 190 GB. Worth checking n_dead_tup before blaming VACUUM.