Postgres 19 REPACK: Shrinking a Live Table
A table can be full of free space and still occupy the same number of bytes on disk. This is a familiar surprise in PostgreSQL: routine vacuuming makes space available inside the table, but often leaves the table file at its old size. PostgreSQL 19, released on November 19, 2026, adds a built-in REPACK command to make a compact copy. Its concurrent mode keeps ordinary reads and writes running during most of that work.
The distinction matters most after a large delete, a heavy update cycle, or a change in workload. If a table will soon fill its free pages again, rebuilding it may accomplish little. If the wasted bytes are substantial and persistent, the space may be worth returning to the operating system. The new command makes that decision easier to execute, but it does not make the rewrite free.
Why a vacuumed table can stay large
PostgreSQL uses multiversion concurrency control, or MVCC. An update writes a new row version; a delete marks the existing version obsolete. Older versions cannot disappear immediately because another transaction may still need to see them. Once no relevant transaction needs them, VACUUM can remove the dead versions and mark their space reusable. It also maintains visibility information and protects against transaction ID wraparound. Autovacuum performs this work routinely.
Imagine a table file of 100 pages, each initially full. A bulk delete removes half the rows, scattered evenly across those pages. Vacuum can make the holes reusable for future rows, yet the file may still contain 100 pages. Its size is not a direct measure of live data. PostgreSQL can sometimes truncate fully empty pages at the end of a table, but scattered empty space stays inside the file. The vacuum documentation therefore recommends ordinary vacuuming for steady-state maintenance and a table rewrite only when the extra disk space really needs to be recovered.
Dead-tuple estimates are a useful starting signal, not a measurement of file bloat. A table can have few dead tuples because vacuum has already removed them, yet still have a lot of reusable free space. Conversely, a high dead-tuple count can be temporary. Look at table and index sizes, the rate of growth, the workload, and whether the freed space is likely to be reused. For a closer measurement, tools such as pgstattuple can inspect a table, with the cost of scanning it.
SELECT schemaname, relname,
pg_size_pretty(pg_relation_size(relid)) AS table_bytes,
pg_size_pretty(pg_indexes_size(relid)) AS index_bytes,
n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables
ORDER BY pg_relation_size(relid) DESC;
These row counts are estimates. The query identifies large candidates and whether vacuum may be falling behind; it does not compute a trustworthy “bloat percentage.” A useful next question is what caused the space: an exceptional cleanup, ongoing updates, or autovacuum settings that need adjustment.
What REPACK actually does
REPACK writes the live rows to a new table file, builds replacement indexes, then swaps the new files into place and removes the old ones. The result is physically smaller because dead rows and unused interior gaps are not copied. The table’s fillfactor still controls reserved space in the new file. The command can also order rows by an index, which can improve locality for indexed range reads.
PostgreSQL 19 exposes three useful forms:
REPACK orders;
REPACK (CONCURRENTLY) orders;
REPACK (CONCURRENTLY, VERBOSE, ANALYZE)
orders USING INDEX orders_created_at_idx;
The first form takes an ACCESS EXCLUSIVE lock for the operation, like VACUUM FULL; readers and writers must wait. The second allows ordinary reads and writes during the bulk of the rewrite. The third also arranges the rows in index order, prints progress messages, and refreshes planner statistics afterward. ANALYZE is especially sensible after changing physical order, since the planner uses statistics about table ordering. The full PostgreSQL command reference defines the syntax and restrictions.
Without CONCURRENTLY, the implementation can lock the old table and copy a stable set of rows. Online rebuilding is harder: transactions can keep changing the source while its replacement is being constructed. PostgreSQL handles this by capturing changes through logical decoding, applying them to the replacement, then briefly taking an exclusive lock to apply the last changes and swap table and index files. Most of the work holds a SHARE UPDATE EXCLUSIVE lock, so normal data reads and writes can proceed. The final lock is usually short, but its duration depends on the changes accumulated while waiting for it.
This is a built-in counterpart to years of extension-based approaches. pg_repack used a shadow table and triggers to log writes. pg_squeeze used logical decoding and a background worker. Core REPACK (CONCURRENTLY) uses logical decoding too, with a SQL interface and no external executable or extension installation. Existing extension workflows may still have different scheduling or deployment advantages; the new command gives PostgreSQL itself a supported online option.
The price of remaining online
The rewrite reads the source table, writes a new table, and builds new indexes. While both copies coexist, PostgreSQL requires free disk space at least equal to the table plus its indexes. If index ordering uses a sequential scan and sort, it may need an additional temporary sort file as large as the table. Concurrent writes also create captured change data on disk and consume memory for rows updated or deleted during the operation. On a busy server, the added I/O and WAL activity can affect other queries even though they are not blocked by a long exclusive lock.
Craig Kerstiens’s Postgres 19 experiment illustrates the tradeoff. In his constrained, one-million-live-row test, a 493 MB table became 228 MB. VACUUM FULL took 2.7 seconds; plain REPACK took 5.9 seconds; concurrent REPACK took 9.4 seconds. Only the concurrent version kept the table accessible through the main rebuild. Those are measurements of one setup, not a speed ratio to assume for another server. Table size, index count, write rate, storage, and lock waits all change the result.
For a planned operation, estimate the work from the table and index sizes, check disk headroom, and watch the write rate. Run a trial against representative data if the table is important. REPACK needs the table’s MAINTAIN privilege. An invalid index must be repaired or removed first. The command is also a long-running transaction for transaction ID purposes, so an enormous repack can delay vacuum cleanup elsewhere; monitor it rather than treating it as a substitute for routine vacuum.
Where concurrent mode has sharp edges
The command cannot use concurrent mode for an unlogged or partitioned table, a non-heap table, a materialized view, a system catalog, or a TOAST table. It also needs a primary key or an index-based replica identity so captured changes can identify rows. It cannot run inside a transaction block, and the server must allow a repack replication slot through max_repack_replication_slots. Ordinary REPACK can process partitions; the CONCURRENTLY option cannot target a partitioned table. The command reference lists further exclusions and explains that DDL on the table during the run can make it fail.
There is a more subtle limit than disk or locks: concurrent REPACK is not fully MVCC-safe. After it commits, a transaction still using a snapshot from before the repack began may see the table as empty. This is a documented consequence of the file swap, and it matters for long-running snapshot transactions. Evaluate that behavior against the application before scheduling a concurrent rewrite.
The logical change stream is finite in practical terms, too. The docs say that if more than roughly 104 million rows are updated or deleted concurrently during one repack, the command fails. A sustained write rate can also make catch-up expensive. “Online” means the main copy does not hold an exclusive lock throughout; it does not mean unlimited changes or zero operational impact.
Watch the phases, not just elapsed time
PostgreSQL 19 exposes pg_stat_progress_repack. It reports scanning, sorting or writing, index work, catch-up, swapping, and cleanup phases. A simple live view is:
SELECT pid, relid::regclass AS table_name, command, phase,
heap_blks_scanned, heap_blks_total,
heap_tuples_inserted, heap_tuples_updated,
heap_tuples_deleted, index_rebuild_count
FROM pg_stat_progress_repack;
During a sequential heap scan, heap_blks_scanned / heap_blks_total gives progress for that phase, not the entire command. The index build, catch-up, and final swap still have to finish. A growing catch-up period is a signal that concurrent writes are competing with the rebuild. The progress reporting reference documents which counters advance in each phase.
Choose the maintenance job that matches the problem
Use regular VACUUM and autovacuum for the day-to-day removal of dead row versions, reuse of space, visibility maintenance, and wraparound protection. Tune autovacuum if it repeatedly falls behind. Use REPACK when there is a persistent reason to rewrite the physical table: recover significant disk space or put rows in useful index order. If a maintenance window permits blocking access, plain REPACK or VACUUM FULL may finish with less online bookkeeping. If the table must continue serving traffic, REPACK (CONCURRENTLY) provides that path after its resource and snapshot limits are checked. If only an index is bloated, rebuilding that index alone may be a smaller intervention.
The best outcome is often avoiding a rewrite at all. Free pages are valuable capacity when the same table will grow into them tomorrow. Postgres 19 makes compaction possible without a long read/write outage, but the decision still starts with understanding the table’s future, not just its current file size.