
You're monitoring your application's performance. A core API endpoint, once snappy, now frequently breaches its latency SLO. The underlying query, which hits your users or transactions table, looks simple. EXPLAIN ANALYZE shows a reasonable plan, yet the I/O time is climbing relentlessly. Your cloud provider's disk usage charts show steady, linear growth, far outpacing your actual data ingestion rate. You are likely a victim of PostgreSQL's most insidious and often misunderstood performance antagonist: table and index bloat.
This isn't a bug; it's a fundamental consequence of PostgreSQL's sophisticated Multi-Version Concurrency Control (MVCC) architecture. While MVCC is brilliant - enabling non-blocking reads and writes - it leaves behind a trail of "dead tuples" that, if left unmanaged, turn your lean database into a sluggish, storage-hungry monolith. This guide provides a deep, engineering-first dissection of the problem, from the atomic level of tuple visibility to production-grade strategies for detection, mitigation, and automated remediation.
Analyze queries for sargability, missing composite indexes & offsets
Analyze SQL queries for performance anti-patterns, wildcards, and slow offsets. Generates optimized queries and DDL composite indexes for PostgreSQL, MySQL, and SQLite.
To defeat bloat, you must first understand its origin. PostgreSQL achieves concurrency by never modifying a row in place. When you perform an UPDATE, Postgres effectively performs a DELETE and an INSERT. The old version of the row (a "tuple" in Postgres parlance) is not immediately erased; it's marked as "dead," and a new version is inserted. A DELETE simply marks the tuple as dead.
Each tuple has two hidden system columns that govern its visibility: xmin and xmax.
xmin: The transaction ID that created this tuple.xmax: The transaction ID that deleted this tuple. Initially 0.When a transaction T1 updates a row, it sets the xmax of the old tuple to T1 and inserts a new tuple with its xmin set to T1. Any other transaction that started before T1 committed can still see the old tuple because T1 is not yet in its "snapshot" of the database state. This is how Postgres avoids read locks.
A "dead tuple" is a row version whose xmax is set to a transaction ID that is visible to all currently running transactions. This means no active transaction could possibly need to see this version anymore, making it eligible for reclamation.
The process looks like this:
This "dead" space isn't returned to the operating system. It remains within the table's "heap" file, creating empty pockets. This is bloat. When enough dead tuples accumulate, your table and its indexes grow physically larger on disk than the live data they contain. This leads to:
The ultimate failure mode that VACUUM prevents is Transaction ID Wraparound. Transaction IDs are 32-bit integers. After ~2 billion write transactions, they wrap around. If old dead tuples aren't "frozen" (marked as visible to all future transactions), the database must shut down to prevent data corruption. Regular vacuuming is not just for performance; it's a mandatory operational requirement.
You can't fix what you can't measure. Generic monitoring tools won't pinpoint bloat. You need to query Postgres's internal statistics. The following query, adapted from various community sources, provides a reliable estimate without requiring extensions like pgstattuple. It works by comparing the statistically estimated size of live tuples to the actual on-disk size of the table.
This query is designed to be run by a superuser or a role with sufficient privileges to inspect table statistics.
-- Detects table and index bloat.
-- Works on PostgreSQL 9.5+
-- Note: The accuracy of this query depends on the freshness of table statistics.
-- Run ANALYZE on your tables before trusting this output for the first time.
WITH constants AS (
SELECT
current_setting('block_size')::numeric AS bs,
23 AS hdr,
4 AS ma
),
tables AS (
SELECT
ns.nspname AS schema_name,
tbl.relname AS table_name,
hdr,
ma,
bs,
tbl.reltuples,
tbl.relpages,
(
(tbl.reltuples * (
-- Average row width, accounting for overhead
AVG(stat.stanullfrac) * (
-- Average width of nullable columns
SELECT SUM(stynsp) FROM pg_type typ JOIN pg_attribute attr ON typ.oid = attr.atttypid WHERE attr.attrelid = tbl.oid AND NOT attr.attnotnull AND attr.attnum > 0
) + (1 - AVG(stat.stanullfrac)) * (
-- Average width of non-nullable columns
SELECT SUM(stynsp) FROM pg_type typ JOIN pg_attribute attr ON typ.oid = attr.atttypid WHERE attr.attrelid = tbl.oid AND attr.attnotnull AND attr.attnum > 0
)
) + ma - (
-- Null bitmap overhead
CASE WHEN tbl.reltuples > 0 THEN ((COUNT(DISTINCT stat.staattnum) + 7) / 8) ELSE 0 END
))
-- Add tuple header overhead
+ hdr
) AS avg_row_width
FROM
pg_class tbl
JOIN pg_namespace ns ON ns.oid = tbl.relnamespace
JOIN pg_attribute attr ON attr.attrelid = tbl.oid
JOIN pg_statistic stat ON stat.starelid = tbl.oid AND stat.staattnum = attr.attnum
WHERE
tbl.relkind = 'r' -- only tables
AND ns.nspname NOT IN ('pg_catalog', 'information_schema')
AND tbl.reltuples > 0
GROUP BY
ns.nspname, tbl.relname, hdr, ma, bs, tbl.reltuples, tbl.relpages
),
table_bloat AS (
SELECT
schema_name,
table_name,
reltuples AS live_tuples,
relpages AS page_count,
bs,
-- Estimated pages needed for live data
CEIL((reltuples * avg_row_width) / (bs - hdr)) AS estimated_pages,
relpages AS actual_pages,
-- Actual size
pg_size_pretty(relpages * bs) AS actual_size
FROM
tables
),
table_bloat_summary AS (
SELECT
schema_name,
table_name,
live_tuples,
-- Bloat in pages and bytes
(actual_pages - estimated_pages) AS bloat_pages,
pg_size_pretty((actual_pages - estimated_pages) * bs) AS bloat_size,
-- Bloat percentage
ROUND(100 * (actual_pages - estimated_pages) / actual_pages, 2) AS bloat_percentage
FROM
table_bloat
WHERE
-- Filter for significant bloat
(actual_pages - estimated_pages) > 0
),
index_bloat AS (
SELECT
ns.nspname AS schema_name,
tbl.relname AS table_name,
idx.relname AS index_name,
idx.reltuples,
idx.relpages,
-- Estimated index size
ROUND((idx.reltuples * (
-- Average index entry size
SELECT
-- Index header + item identifier + data
16 + AVG(pg_column_size(indexdatacolumns))
FROM
pg_index i
JOIN pg_stats s ON s.tablename = tbl.relname
AND s.attname = ANY(string_to_array(i.indkey::text, ' ')::text[])
WHERE i.indexrelid = idx.oid
)) / current_setting('block_size')::numeric, 1) AS estimated_pages,
idx.relpages AS actual_pages,
pg_size_pretty(idx.relpages * current_setting('block_size')::numeric) AS actual_size
FROM
pg_class idx
JOIN pg_index i ON i.indexrelid = idx.oid
JOIN pg_class tbl ON tbl.oid = i.indrelid
JOIN pg_namespace ns ON ns.oid = tbl.relnamespace
WHERE
idx.relkind = 'i' -- only indexes
AND ns.nspname NOT IN ('pg_catalog', 'information_schema')
AND idx.reltuples > 0
),
index_bloat_summary AS (
SELECT
schema_name,
table_name,
index_name,
reltuples,
(actual_pages - estimated_pages) AS bloat_pages,
pg_size_pretty((actual_pages - estimated_pages) * current_setting('block_size')::numeric) AS bloat_size,
ROUND(100 * (actual_pages - estimated_pages) / actual_pages, 2) AS bloat_percentage
FROM
index_bloat
WHERE
(actual_pages - estimated_pages) > 10 -- Filter for significant bloat
)
-- Combine and present the results
SELECT
'table' AS object_type,
schema_name,
table_name AS object_name,
bloat_percentage,
bloat_size
FROM
table_bloat_summary
WHERE bloat_percentage > 20.0
UNION ALL
SELECT
'index' AS object_type,
schema_name,
index_name AS object_name,
bloat_percentage,
bloat_size
FROM
index_bloat_summary
WHERE bloat_percentage > 30.0
ORDER BY
bloat_size DESC;
Interpreting the Output: Running this query will give you a list of the most bloated tables and indexes.
| object_type | schema_name | object_name | bloat_percentage | bloat_size |
|---|---|---|---|---|
| table | public | event_logs | 65.40 | 12 GB |
| index | public | event_logs_user_id_idx | 72.10 | 8 GB |
| index | public | users_last_login_at_idx | 45.80 | 2 GB |
A rule of thumb:
autovacuum is PostgreSQL's built-in janitorial service. It's a daemon process that periodically runs VACUUM and ANALYZE operations on tables that have seen a significant number of INSERTs, UPDATEs, or DELETEs.
However, the default autovacuum settings are notoriously conservative. They are designed to minimize resource consumption on low-spec hardware and are wholly inadequate for moderate-to-high write workloads. Relying on them is a common path to production performance degradation.
Autovacuum decides to trigger a VACUUM on a table when the number of dead tuples exceeds a threshold calculated by this formula:
vacuum_threshold = autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor * number_of_tuples
Default values are:
autovacuum_vacuum_threshold = 50autovacuum_vacuum_scale_factor = 0.20 (i.e., 20% of the table)Consider an event_logs table with 100 million rows. The threshold is 50 + 0.20 * 100,000,000 = 20,000,050. Autovacuum will wait until 20 million dead tuples accumulate before it even considers cleaning up. By that point, the table is already severely bloated. The scale factor is the primary culprit for large tables.
The solution is to move away from the global defaults and set aggressive, per-table configurations for your high-write tables.
autovacuum_vacuum_scale_factor.autovacuum_vacuum_threshold to ensure the table is vacuumed frequently, even if it's small.Let's tune our event_logs table.
-- Tune a specific high-write table.
-- A scale factor of 0.02 means vacuum triggers after 2% of the table has changed.
-- A threshold of 1000 ensures it runs even if the table is temporarily small.
ALTER TABLE public.event_logs SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_vacuum_threshold = 1000,
autovacuum_analyze_scale_factor = 0.01,
autovacuum_analyze_threshold = 500
);
-- For extremely high-throughput tables, you might also make the vacuum worker
-- faster by reducing its sleep time between operations.
-- This table will now be vacuumed more often and more quickly.
ALTER TABLE public.event_logs SET (
autovacuum_vacuum_cost_delay = '10ms' -- Default is 20ms on some versions
);
By setting these storage parameters directly on the table, you override the global postgresql.conf settings for that relation only. This is the correct, targeted approach. You don't want to make global settings overly aggressive, as that could add unnecessary I/O load to small, static tables.
To find your high-write tables, you can query pg_stat_user_tables:
SELECT
schemaname,
relname,
n_tup_ins,
n_tup_upd,
n_tup_del,
n_live_tup,
n_dead_tup,
last_autovacuum,
last_autoanalyze
FROM
pg_stat_user_tables
ORDER BY
n_tup_upd + n_tup_del DESC
LIMIT 20;
This shows you which tables are experiencing the most churn and are therefore the top candidates for custom autovacuum tuning.
pg_repackWhat if you've tuned autovacuum, but a table is already 12 GB overweight with 65% bloat? Autovacuum's standard VACUUM process does not shrink the on-disk file or reclaim space for the OS; it only marks space within the file as free for future INSERTs. To truly reclaim the space and shrink the file, you need to rewrite the table.
The native VACUUM FULL command does this, but it takes an ACCESS EXCLUSIVE lock on the table for the entire duration of the operation, blocking all reads and writes. For a large, critical production table, this is equivalent to taking your application offline for minutes or even hours. It's a non-starter.
Enter pg_repack, the gold-standard community extension for online table and index rebuilding.
pg_repack Workspg_repack rebuilds a table and its indexes with minimal locking, enabling true zero-downtime bloat removal.
INSERTs, UPDATEs, and DELETEs that occur during the process.ACCESS EXCLUSIVE lock, it:
The heavy lifting (steps 3-5) happens online. The exclusive lock (step 6) typically lasts for milliseconds to a few seconds, even for terabyte-scale tables, making it safe for production use.
pg_repackFirst, you need to install the extension on your database server and in your database.
-- In psql, connected to your target database
CREATE EXTENSION pg_repack;
Then, from a terminal on the database server or any machine with network access and the pg_repack binary:
# Example: Repack the 'event_logs' table in the 'mydb' database
# -k, --no-order: Skips clustering by primary key. Faster, but you lose physical ordering.
# -j 4: Use 4 parallel jobs for index creation (if supported).
# --wait-timeout=60: Wait up to 60 seconds to acquire the final lock. Abort if a long-running query blocks it.
pg_repack -d mydb -h myhost.db.example.com -U db_admin_user -k -j 4 --wait-timeout=60 --table public.event_logs
Prerequisites:
PRIMARY KEY or a UNIQUE index on a NOT NULL column.CREATE privileges on the schema.A final piece of the puzzle is the Visibility Map (VM). This is a compact bitmap data structure that tracks which pages in a table's heap file contain only tuples that are "all-visible" - meaning they are visible to all current and future transactions.
When VACUUM runs, it updates the VM. This has a huge performance implication for a specific type of query: index-only scans.
If you query a column that is fully contained within an index (e.g., SELECT user_id FROM event_logs WHERE processed_at > NOW() - '1 day'), and the VM confirms that all the tuples on the pages pointed to by the index are all-visible, Postgres can answer the query by reading the index alone. It never has to visit the main table heap. This is extremely fast.
If the VM is out of date due to infrequent vacuuming, Postgres can't be sure about tuple visibility. It is forced to perform an index scan followed by a heap fetch for every single row to check its xmin/xmax values. This completely negates the benefit of an index-only scan and is a common source of mysterious query slowness.
Aggressive autovacuum tuning isn't just about reclaiming space; it's about keeping the Visibility Map fresh to enable the query planner's most powerful optimizations.
A PostgreSQL database is experiencing steadily increasing disk usage despite a stable amount of 'live' application data. `EXPLAIN ANALYZE` for core queries shows reasonable plans but I/O time is climbing. Which of the following is the most likely root cause and its immediate consequence?
Database bloat is not a matter of "if," but "when." For any system with a dynamic write workload, a passive approach guarantees eventual performance degradation. A proactive, multi-layered strategy is essential for long-term health.
Your Action Plan:
pg_stat_user_tables. Apply aggressive, per-table autovacuum settings immediately. Don't wait for bloat to become a problem.pg_repack operation during a low-traffic period. The downtime is minimal, but it's still best practice to execute major operations with care.idle_in_transaction_session_timeout in PgBouncer to automatically terminate transactions that are idle but still open.By mastering this cycle of detection, prevention, and remediation, you move from being a reactive firefighter to a strategic database engineer, ensuring your PostgreSQL infrastructure remains fast, efficient, and reliable at scale.
Clean AI CLI logs, build cron expressions, decode JWTs, and calculate chmod permissions offline.

Analyzing PostgreSQL 17 EXPLAIN plans: optimizing memory sort buffers, HashJoin spills, bitmap index scans, and eliminating seq scans.
Read more
Zero-downtime change data capture (CDC) with PostgreSQL logical replication, Debezium, and Kafka: WAL management, slot lag, and failover resilience.
Read more
Stress-testing PgCat, PgBouncer, and Supavisor at 10,000+ connections: transaction mode overhead, client pooling latency, and zero-downtime failover.
Read more