Postgres REPACK vs VACUUM FULL vs pg_repack: how to shrink a bloated table

PostgreSQL 19's docs now call VACUUM FULL deprecated, 24 years after it was added. How to check table bloat first, why plain VACUUM rarely shrinks a file, what VACUUM FULL, pg_repack, pg_squeeze and the new REPACK (CONCURRENTLY) lock, how much disk each needs, and the two catches in REPACK's own docs.

PostgreSQL 19’s own documentation now describes VACUUM FULL as deprecated. The FULL option reads: “This option, which is deprecated, makes VACUUM behave like REPACK without a USING INDEX clause.” The command arrived in PostgreSQL 7.2, released on 4 February 2002, when everyday VACUUM stopped locking tables and “old semantics are available as VACUUM FULL”. Twenty-four years later it has a successor, REPACK, and for the first time in core, a way to rewrite a table while the application keeps reading and writing.

This post goes one level deeper than the REPACK section of our PostgreSQL 19 features post: how to measure bloat, what each tool does to locks and disk, and which one to pick. Why tables bloat in the first place (MVCC, dead tuples) is in Why your Postgres table is bigger than its data. Nothing was benchmarked for this post; every claim links to the document it comes from.

First, measure

An UPDATE in Postgres writes a new row version and leaves the old one behind; a DELETE only marks it. Until vacuum processes them, those dead row versions take space and every sequential scan reads past them. Two ways to see how much:

Cheap, estimated: pg_stat_user_tables keeps n_live_tup and n_dead_tup, documented as the “estimated number” of live and dead rows. No scan needed.

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,
       pg_size_pretty(pg_total_relation_size(relid)) AS total_size,
       last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 10;

Exact: the pgstattuple extension reads the table and reports dead_tuple_percent and free_percent (space already freed by vacuum but still inside the file). It “always performs a full-table scan”, taking only a read lock. On large tables, pgstattuple_approx skips pages the visibility map marks all-visible and returns exact dead-tuple numbers with an approximate free-space figure. By default only members of pg_stat_scan_tables (and superusers) may run them.

CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT * FROM pgstattuple_approx('orders');           -- cheaper
SELECT dead_tuple_percent, free_percent FROM pgstattuple('orders');  -- exact, full scan

A high free_percent with low dead_tuple_percent means vacuum has done its job and the space is waiting to be reused. Whether that is worth a rewrite depends on how many bytes that is and whether the table keeps growing, not on one magic percentage.

Tool 1: plain VACUUM

Plain VACUUM (what autovacuum runs) removes dead row versions and marks their space reusable. It runs alongside reads and writes, but the docs are clear that it “will not return the space to the operating system, except in the special case where one or more pages at the end of a table become entirely free and an exclusive table lock can be easily obtained.” New rows fill the holes, so a healthy table stops growing; it rarely shrinks.

It also does not start right away. Autovacuum vacuums a table when its dead rows exceed min(autovacuum_vacuum_max_threshold, autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × rows), with defaults of 100,000,000, 50 and 0.2. For a 100-million-row table that is 50 + 0.2 × 100,000,000 = 20,000,050 dead rows before the first run. Lowering the scale factor on large, busy tables is the cheapest bloat fix there is.

Tool 2: VACUUM FULL (now: REPACK)

VACUUM FULL writes a new copy of the table containing only live rows, with “no extra space” except what fillfactor reserves, then drops the old file, so the space really goes back to the operating system. The price:

  • An ACCESS EXCLUSIVE lock “acquired at the beginning and remains held throughout” (REPACK docs). That lock conflicts with every other lock mode, including the one a plain SELECT takes.
  • Disk: both copies exist until the end, so you need free space “at least equal to the sum of the table size and the index sizes”.

In PostgreSQL 19, the release notes put it plainly: “Add REPACK command which replaces VACUUM FULL and CLUSTER … The two former commands did similar things, but with confusing names, so unify them as REPACK. The old commands have been retained for compatibility.” REPACK t is the old VACUUM FULL t; REPACK t USING INDEX i is the old CLUSTER, which also sorts rows by the index (once; later changes are not kept in order). Progress for all three shows in pg_stat_progress_repack.

Tool 3: pg_repack and pg_squeeze

For years the online answer was an extension.

pg_repack (1.5.3) creates a log table and a trigger that records every INSERT, UPDATE and DELETE, builds a new copy of the table and its indexes, replays the log, swaps the tables in the system catalog and drops the original. It holds an ACCESS EXCLUSIVE lock “for a short period” at the start and at the final swap, and SHARE UPDATE EXCLUSIVE in between, so reads and writes continue. Its requirements: a primary key or a unique index on a NOT NULL column, free disk “about twice as large as the target table(s) and its indexes”, superuser or table owner, and no DDL on the table during the run. If it can’t get its exclusive lock within --wait-timeout (default 60 seconds) it “will forcibly cancel the conflicting queries” (--no-kill-backend skips the table instead). Its docs list support for PostgreSQL 9.5 through 18.

pg_squeeze does the same job with logical decoding instead of triggers, runs in background workers on the server, and can process registered tables on a cron-like schedule once their extra free space passes a threshold (free_space_extra, default 50 percent). It needs shared_preload_libraries, one extra replication slot, an identity index (primary key or REPLICA IDENTITY USING INDEX), and wal_level = logical on servers older than 19. Its final exclusive lock is capped by squeeze.max_xlock_time; past the cap it releases the lock, catches up and retries.

The design of pg_squeeze matters for PostgreSQL 19: the commit that added REPACK (CONCURRENTLY), 28d534e2a (6 April 2026), credits Antonin Houska for the design and most of the code, “heavily based on his own pg_squeeze third-party implementation.”

Tool 4: REPACK (CONCURRENTLY)

With CONCURRENTLY, the docs say “the bulk of the operation is run with SHARE UPDATE EXCLUSIVE lock, and the ACCESS EXCLUSIVE lock is only acquired during the final phase to swap the table and index files.” SHARE UPDATE EXCLUSIVE doesn’t conflict with SELECT, INSERT, UPDATE or DELETE; it does block schema changes, VACUUM, ANALYZE and CREATE INDEX on that table. The commit message describes the mechanics: take an MVCC snapshot, create a replication slot at that snapshot, copy the table, and let a background worker decode the changes made meanwhile, then reapply them just before the swap.

REPACK (CONCURRENTLY) orders;                     -- online rewrite
REPACK (CONCURRENTLY, ANALYZE) orders USING INDEX; -- and re-cluster by the known index
SELECT phase, heap_tuples_updated FROM pg_stat_progress_repack;
startendVACUUMVACUUM FULL / REPACKpg_repackREPACK (CONCURRENTLY)ACCESS EXCLUSIVE: no reads, no writesSHARE UPDATE EXCLUSIVE: reads + writes go on
Schematic, not to scale in time. Sources: PostgreSQL 19 docs for VACUUM, REPACK and explicit locking; pg_repack 1.5.3 docs. Plain VACUUM can also take a brief ACCESS EXCLUSIVE lock to truncate empty pages at the end, only if it gets it easily. pg_squeeze behaves like REPACK (CONCURRENTLY), with its final lock capped by squeeze.max_xlock_time.

The progress phases show the order: initializing (for CONCURRENTLY this also enables logical decoding if effective_wal_level isn’t already logical, then waits for transactions that have a transaction ID to finish), scanning, writing the new heap, catch-up (replaying concurrent changes and waiting for the exclusive lock), swapping relation files, rebuilding indexes, cleanup. So a server running the default wal_level = replica doesn’t need a restart for it.

The fine print

From the REPACK docs, CONCURRENTLY is not possible when the table:

  • lacks a primary key and an index-based replica identity;
  • is partitioned, UNLOGGED, a system catalog or TOAST table, or not a heap table;
  • is processed inside a transaction block, or no slot is free under max_repack_replication_slots (default 5).

Two more limits are worth knowing before you run it on a busy table:

  • “if more than about 104 million rows are concurrently updated or deleted during the execution of REPACK, the command fails.” Each changed row costs a small fixed amount of memory, outside maintenance_work_mem, and full copies of changed rows are written to disk.
  • DDL on the table during the run can make it fail.
VACUUMnoneVACUUM FULL / REPACKVACUUM FULL / REPACK: ≥ 1× (table + indexes)≥ 1× (table + indexes)REPACK (CONCURRENTLY)REPACK (CONCURRENTLY): ≥ 1× + changes made meanwhile≥ 1× + changes made meanwhilepg_repackpg_repack: about 2×about 2×pg_squeezepg_squeeze: about 2×about 2×
As stated in the docs, not measured: REPACK "free disk space at least equal to the sum of the table size and the index sizes" (concurrent changes are also stored on disk); pg_repack and pg_squeeze "about twice as large as the target table and its indexes". Bars to scale.

The warning

The REPACK page carries a warning box: “REPACK with the CONCURRENTLY option is not MVCC-safe. After the repacking transaction commits, the table will appear empty to concurrent transactions, if they are using a snapshot taken before repacking started.” The MVCC caveats page narrows it down (it’s the same rule as for TRUNCATE): this “will only be an issue for a transaction that did not access the table in question before the command started”, because a transaction that already read it holds a lock that the swap has to wait for. The risky case is a long REPEATABLE READ or SERIALIZABLE transaction, such as a report or a pg_dump, that reads the table for the first time after the swap.

One more: “REPACK is considered a single long-running transaction, which prevents VACUUM from cleaning up dead rows from other tables.” On a very large table, watch pg_stat_activity.backend_xid for the REPACK process, or you trade bloat in one table for bloat in all the others.

Which one to pick

Situation Tool Lock Free disk
Space is being reused, table isn’t growing plain VACUUM + autovacuum tuning SHARE UPDATE EXCLUSIVE none
Need the disk back, downtime is fine VACUUM FULL / REPACK ACCESS EXCLUSIVE, whole run ≥ table + indexes
Need it online, PostgreSQL 19 REPACK (CONCURRENTLY) short ACCESS EXCLUSIVE at the swap ≥ table + indexes, + changes
Need it online, PostgreSQL 18 or older pg_repack / pg_squeeze short ACCESS EXCLUSIVE at start/end about 2× table + indexes

Whatever you pick, rehearse it on a copy of the database first (on PostgreSQL 18 and a copy-on-write filesystem, a clone of a database was reported at 0.21 s), and run the online versions when no long transaction is open. Then fix the cause: per-table autovacuum settings for the busy tables and no sessions left idle in transaction, or the bloat comes back.

Sources