• PostgreSQL
  • databases
  • SQL
  • operations

PostgreSQL 19 REPACK can shorten locks, not remove the trade-offs

PostgreSQL 19, released November 19, 2026, adds REPACK (CONCURRENTLY) for reclaiming table space while the table remains usable for most of the operation. What it does, which tables qualify, and how to check a table before and after.

Picture an events table that receives a steady stream of inserts, and a nightly job that deletes rows older than 30 days. After a few months the table holds about the same number of rows as before, yet it takes far more disk space. Queries that scan it read more pages than they need to. The usual fix is to rewrite the table into a compact new file, but until PostgreSQL 19 the built-in ways to do that blocked every reader and writer for the whole rewrite.

PostgreSQL 19, released on November 19, 2026, adds the REPACK command and a CONCURRENTLY option that lets other sessions keep reading and writing the table during most of the rewrite. The option shortens the blocking; it does not remove it, and it comes with a list of conditions. This article explains why tables grow this way, what REPACK (CONCURRENTLY) changes, which tables qualify, and how to check a table before and after running it. PostgreSQL 19 release notes

Why a table keeps its old space

PostgreSQL uses MVCC, short for multiversion concurrency control. Instead of overwriting a row in place, an UPDATE writes a new version of the row, and a DELETE marks the old version as no longer current. The old version stays on disk because another transaction that started earlier may still need to see it. A transaction is a group of statements that succeed or fail together.

Once no transaction can see an old row version any more, it is a “dead tuple” (tuple is PostgreSQL’s word for a row version). VACUUM finds dead tuples and marks their space as free for new rows in the same table. The PostgreSQL documentation is explicit that standard VACUUM does not give that space back to the operating system, except when whole pages at the end of the table become empty. Routine vacuuming

That is usually fine: new rows fill the freed space. It stops being fine after a one-off event, such as deleting most of a large table, when the free space will not be reused soon. The file stays large. “Bloat” is the informal name for this unused space inside a table or index.

To shrink the file, PostgreSQL has to write a complete new copy of the table without the dead space, build new indexes, and then switch to the new files. The same documentation lists the commands that do this: VACUUM FULL, CLUSTER, REPACK, and some forms of ALTER TABLE.

What REPACK replaces

Before version 19, VACUUM FULL rewrote a table to remove dead space, and CLUSTER rewrote it in the order of an index. The release notes say the two commands did similar things under confusing names, so PostgreSQL 19 unifies them as REPACK. VACUUM FULL and CLUSTER are kept for compatibility, so existing scripts keep working.

The basic forms look like this:

REPACK public.events;                               -- rewrite to remove dead space
REPACK public.events USING INDEX events_pkey;       -- rewrite in the order of an index
REPACK (CONCURRENTLY) public.events;                -- rewrite while the table stays usable

The command also accepts VERBOSE and ANALYZE options. REPACK command reference

What the concurrent option changes

A lock is how PostgreSQL stops two sessions from doing conflicting things to the same table. The strongest table lock, ACCESS EXCLUSIVE, blocks every other session, including plain SELECT queries. Without CONCURRENTLY, REPACK holds that lock for the whole rewrite. On a large table that can mean minutes or hours of an unusable table.

With CONCURRENTLY, the bulk of the work runs under a less restrictive lock. While PostgreSQL copies the live rows into the new file, it also captures the inserts, updates and deletes that other sessions make in the meantime, and applies them to the new copy. Only the final step, swapping the old files for the new ones, takes ACCESS EXCLUSIVE. The documentation says this final lock is typically short, depending on how many changes have accumulated and how long it takes to acquire the lock.

To capture those changes, the concurrent mode creates a replication slot. A replication slot is a server-side marker that makes PostgreSQL keep the change records a consumer still needs. The number of slots available for this purpose is limited by the new setting max_repack_replication_slots.

Which tables qualify

CONCURRENTLY is refused when any of the following is true. Each term is explained briefly:

  1. The relation is not a table, for example a materialized view (a stored query result).
  2. The table is UNLOGGED. Unlogged tables are not written to the write-ahead log (WAL), the change log PostgreSQL uses for crash recovery and replication.
  3. The table is partitioned, meaning a parent table that splits its rows into child tables.
  4. The table is a system catalog (PostgreSQL’s own internal tables) or a TOAST table (the side table that stores large column values).
  5. The table’s access method is not heap. The access method is the storage format; heap is PostgreSQL’s standard one.
  6. The table is declared a catalog table with the user_catalog_table storage parameter.
  7. The table has neither a primary key nor an index-based replica identity. Replica identity is the setting that decides which columns identify a row in the change stream.
  8. The command runs inside a transaction block (between BEGIN and COMMIT).
  9. max_repack_replication_slots does not allow one more replication slot.

These conditions come from the REPACK reference page. REPACK restrictions

Limits to plan for

Disk space. The old files cannot be released until the new ones are complete, so the routine-vacuuming documentation warns that table-rewriting commands temporarily use extra disk space approximately equal to the size of the table. Make sure that space is free before you start.

Old snapshots see an empty table. A snapshot is the view of the data a transaction works with. The REPACK page warns that the concurrent mode is not MVCC-safe: after it commits, a concurrent transaction using a snapshot taken before the repack started will see the table as empty. Long-running reports or REPEATABLE READ transactions that span the operation are the cases to look for.

Heavy write traffic. PostgreSQL needs a small fixed amount of memory for each row updated or deleted during the run. If more than about 104 million rows are concurrently updated or deleted, the command fails.

Schema changes. DDL (statements that change table structure, such as ALTER TABLE) run by other sessions can prevent the command from completing.

Ordering is only a snapshot. With USING INDEX, the concurrent mode does not try to order rows inserted after it started, so the new file is ordered only as of the start.

It is not vacuum. The REPACK page notes that it rewrites the whole table and its indexes, takes longer than VACUUM, and is not the recommended way to prevent transaction-ID wraparound (the point where PostgreSQL’s transaction counter must be recycled, which regular vacuuming handles).

Check a table before and after

The steps below are a method, not a recorded run. I validated the catalog and size queries with PostgreSQL 18.7, which has the same pg_class, pg_index and pg_am columns. max_repack_replication_slots, pg_stat_progress_repack and REPACK itself exist only in PostgreSQL 19, so those lines were not run here. Run everything in psql connected to the target database.

Step 1: record the size. pg_relation_size measures the table’s main data file. pg_total_relation_size adds its indexes and TOAST data:

SELECT pg_size_pretty(pg_relation_size('public.events'))       AS heap,
       pg_size_pretty(pg_total_relation_size('public.events')) AS total;

Write both numbers down. Compare total with the free space on the database volume: you need at least the table’s size free.

Step 2: check the prerequisites. pg_class describes every table, pg_index every index, and pg_am the access methods:

SELECT c.relkind, c.relpersistence, am.amname, c.relreplident,
       EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisprimary)   AS has_pk,
       EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisreplident) AS has_replident_index
FROM pg_class c LEFT JOIN pg_am am ON am.oid = c.relam
WHERE c.oid = 'public.events'::regclass;

Read the result like this:

  • relkind should be r, an ordinary table. p means partitioned.
  • relpersistence should be p, permanent. u means unlogged.
  • amname should be heap.
  • has_pk or has_replident_index should be true.

On PostgreSQL 18.7, a fresh table with a bigint primary key returned r | p | heap | d | t | f. The d in relreplident is the default replica identity, which uses the primary key.

Step 3: check the slot setting (PostgreSQL 19):

SHOW max_repack_replication_slots;

Make sure it leaves room beside any other repack already running.

Step 4: run the command, outside any BEGIN block:

REPACK (CONCURRENTLY) public.events;

Step 5: watch the progress from a second psql session:

SELECT * FROM pg_stat_progress_repack;

Each backend running REPACK reports a row in this view while it works.

Step 6: measure again. Rerun the size query from step 1. The difference between the two total values is roughly the space returned to the operating system, less whatever concurrent writes added meanwhile.

Common mistakes

  • Running the command inside a transaction block, often because a migration tool wraps every script in BEGIN/COMMIT. The concurrent mode refuses this.
  • Starting without enough free disk for a second copy of the table.
  • Scheduling it while a long report or backup transaction holds an old snapshot.
  • Using it as routine cleanup. Regular VACUUM and autovacuum remain the everyday tools.

Choose between REPACK and the alternatives

  • Plain VACUUM is right when the goal is routine cleanup or wraparound protection. It does not rewrite the table and does not shrink the file. Reach for REPACK only when the dead space itself is the problem.
  • The pg_repack extension remains the option on PostgreSQL 18 and earlier, because REPACK is new in 19. Once you are on 19 and the table meets the built-in command’s prerequisites, the core command avoids installing and upgrading a separate extension.
  • Dropping or detaching partitions beats any rewrite when the bloat sits in old partitions you no longer need. PostgreSQL’s partitioning guide describes dropping a partition as a fast way to remove old data without deleting rows one by one, and DETACH PARTITION ... CONCURRENTLY as a lower-lock way to take it out first. Note that CONCURRENTLY refuses a partitioned table as such. Partition maintenance

Conclusion

REPACK (CONCURRENTLY) in PostgreSQL 19 lets you shrink a bloated table while it stays usable for most of the rewrite. It is not lock-free and not for every table. Check the prerequisites in the catalog first, make sure there is disk space for a second copy, avoid old snapshots and conflicting DDL during the run, and measure the size before and after. These details are scoped to PostgreSQL 19, released November 19, 2026.

Written by Duy Pham Independent developer. I build the apps on this site and write about how they work.

More about me