Skip to content

Dead tuples by table

Tables where dead rows pile up against live ones. Above 20% the autovacuum is losing; above 50% reads are paying for it.

PostgreSQL · SQL
SELECT schemaname || '.' || relname AS table_name,       n_live_tup AS live_rows,       n_dead_tup AS dead_rows,       ROUND(100.0 * n_dead_tup / NULLIF(n_live_tup + n_dead_tup, 0), 1) AS dead_pct,       last_autovacuum,       last_vacuumFROM pg_stat_user_tablesWHERE n_live_tup + n_dead_tup > 1000ORDER BY n_dead_tup DESCLIMIT 25

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What this query shows

When you UPDATE or DELETE a row, PostgreSQL does not remove the old version right away. It stays in the table as a dead tuple until VACUUM reclaims the space. A few dead tuples are normal. A table where they pile up gets bigger, scans slower and indexes bloated.

This query reads pg_stat_user_tables, skips tiny tables (fewer than 1,000 rows, live plus dead) and returns the 25 tables with the most dead tuples. Run it when queries on a table slowly get worse, when disk grows faster than your data, or as part of a regular PostgreSQL health check.

How to read each column

  • table_name: schema and table.
  • live_rows and dead_rows: n_live_tup and n_dead_tup. Both are estimates kept by the statistics collector, not exact counts.
  • dead_pct: dead rows as a share of live plus dead rows. This is the number to sort your attention by.
  • last_autovacuum and last_vacuum: when autovacuum and a manual VACUUM last finished on the table. NULL means it has not happened since the statistics were reset.

Read dead_rows and last_autovacuum together. Many dead rows with a recent autovacuum means vacuum is running but cannot keep up, or cannot remove the rows yet. Many dead rows with an old or empty last_autovacuum means vacuum is not reaching the table at all.

Healthy and worrying numbers

These are rules of thumb, not limits from the PostgreSQL documentation. Under 5% dead is usual on a busy table. Between 5% and 20% deserves a look. Above 20% autovacuum is losing, and above 50% reads already pay for the dead space.

The reference point is the autovacuum default: a table is vacuumed when dead tuples pass autovacuum_vacuum_threshold (50) plus autovacuum_vacuum_scale_factor (0.2) times the row count. On a 10 million row table that is about 2 million dead tuples before autovacuum even starts. That is why large tables bloat first.

What to do about it

Start by running vacuum by hand on the worst table and watch what it reports.

VACUUM (VERBOSE, ANALYZE) public.orders;

A plain VACUUM does not block reads or writes. It makes the space reusable inside the table but usually does not shrink the file. VACUUM FULL rewrites the table and returns space to the operating system, but it takes an ACCESS EXCLUSIVE lock for the whole run, so avoid it on a live table.

If the same tables come back every week, make autovacuum more aggressive for them instead of vacuuming by hand.

ALTER TABLE public.orders SET (
  autovacuum_vacuum_scale_factor = 0.02,
  autovacuum_vacuum_threshold = 1000
);

If VACUUM runs but dead_rows does not drop, something is holding the cleanup horizon back: a long open transaction, an abandoned replication slot or a prepared transaction. Check the open transactions query next.

Limits of this query

  • The counters reset when statistics are reset and after a crash recovery, so last_autovacuum can be empty on a healthy table.
  • n_dead_tup is an estimate. For exact bloat numbers you need the pgstattuple extension, which reads the table.
  • It only sees the database you are connected to, and only tables the role can see in pg_stat_user_tables.
  • Run it on the primary. A standby has no vacuum of its own.

Run it on a schedule in DataRunner

Connect the database to DataRunner, paste this query or start from the template, approve the SQL, then pick a schedule and a channel. Weekly is enough for most databases, daily if the workload is heavy on updates. Each run executes the query on your database and delivers the result as CSV, Excel, PDF or inline HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.

Every run is kept in the run history, so you can set today's result next to last week's. A schedule carries its own time zone, so a Monday at 8:00 is your Monday.

If the database sits in a private network, the bridge agent opens the connection from the inside out, so you do not need to open an inbound port.

Frequently asked questions

What causes dead tuples in PostgreSQL?
Every UPDATE writes a new row version and every DELETE only marks the old one as dead, because other transactions may still need to see it. The old versions become dead tuples once no transaction can see them, and VACUUM then frees the space. Heavy update or delete workloads, or anything that blocks vacuum, make them accumulate.
How do I check dead tuples in PostgreSQL?
Query pg_stat_user_tables and compare n_dead_tup with n_live_tup for each table, as this template does. The values are estimates, which is enough to find the tables that need attention. Use pgstattuple when you need an exact figure.
When should I run VACUUM manually?
When a table has a high dead percentage and autovacuum is not catching up, or right after a large bulk delete or update. Use VACUUM (VERBOSE, ANALYZE) so you see what it did and the planner statistics are refreshed. Reserve VACUUM FULL for planned maintenance, because it locks the table.
Does autovacuum clean dead tuples by itself?
Yes, when autovacuum is on, which is the default. It starts on a table once dead tuples pass the threshold plus the scale factor times the row count. On big tables the default scale factor of 0.2 is too lazy, so lower it per table.