Database tables continuously change as applications insert, update, and delete records. Over time, these operations can leave dead tuples and unused space inside PostgreSQL tables. This condition is commonly known as table bloat.
Table bloat can increase storage consumption and make database maintenance more expensive. It can also affect query performance when PostgreSQL needs to scan more pages than necessary.
PostgreSQL provides VACUUM to clean up dead tuples, but ordinary VACUUM does not generally return the unused space inside a table to the operating system. VACUUM FULL can reclaim that space, but it requires an ACCESS EXCLUSIVE lock for the duration of the table rewrite. This can block normal application queries and updates.
PostgreSQL 19 introduces a new REPACK command with a CONCURRENTLY option. It allows PostgreSQL to rebuild a table while applications continue reading and writing data during most of the operation.
In this article, we will explore how REPACK (CONCURRENTLY) works, how to execute it safely, how to monitor its progress, and what database administrators should consider before using it in production.
Understanding Table Bloat in PostgreSQL
PostgreSQL uses Multi-Version Concurrency Control (MVCC) to manage concurrent transactions. Instead of immediately overwriting an existing row version, PostgreSQL creates a new row version when an UPDATE occurs.
Similarly, when a row is deleted, PostgreSQL marks the row version as deleted rather than immediately removing it from the physical table file.
These old row versions are called dead tuples. Once no active transaction needs them, PostgreSQL can reclaim their space for reuse.
Consider an application that frequently updates an orders table:
UPDATE orders
SET status = 'Completed'
WHERE order_id = 1001;When this operation executes, PostgreSQL creates a new row version. The previous version eventually becomes eligible for cleanup.
If an application frequently updates and deletes records, a table can accumulate many dead tuples. Although VACUUM can make their space reusable, the physical table file may remain larger than the amount of live data requires.
This is where table rewriting becomes useful.
Understanding REPACK in PostgreSQL 19
PostgreSQL 19 introduces REPACK as a SQL command for rewriting tables and reclaiming storage occupied by dead tuples.
Unlike ordinary VACUUM, REPACK creates a new physical copy of the table containing the live rows. It also creates new copies of the table's indexes.
After the new files are ready, PostgreSQL replaces the old table and index files with the new ones.
The basic command is:
REPACK public.orders;However, this command uses an ACCESS EXCLUSIVE lock during the operation. Other transactions cannot access the table while that lock is held.
To reduce application disruption, PostgreSQL 19 supports the CONCURRENTLY option:
REPACK (CONCURRENTLY) public.orders;This option changes how PostgreSQL performs the table rewrite. The main copying and rebuilding work happens while normal database operations can continue.
The important difference is that concurrent repacking does not hold an ACCESS EXCLUSIVE lock throughout the entire operation. It still needs a short ACCESS EXCLUSIVE lock during the final file replacement phase.
Therefore, REPACK (CONCURRENTLY) significantly reduces blocking, but it does not guarantee that the table will never experience a lock wait.
How REPACK CONCURRENTLY Works
The concurrent operation uses a different process from a traditional table rewrite.
Initializing the Operation
PostgreSQL prepares the repacking operation and establishes the information needed to capture concurrent changes.
The concurrent implementation uses logical decoding and a replication slot to track changes made while the new table copy is being prepared.
This is important because applications may continue inserting, updating, and deleting rows during the operation.
Creating a New Table Copy
PostgreSQL reads the existing table and copies its live rows into a new physical file.
Dead tuples are not copied into the new table. If an index is specified, PostgreSQL can also arrange the rows according to the index ordering.
During this stage, PostgreSQL uses a SHARE UPDATE EXCLUSIVE lock rather than holding ACCESS EXCLUSIVE for the complete rewrite.
Normal SELECT, INSERT, UPDATE, and DELETE operations can generally continue.
Capturing Concurrent Changes
While PostgreSQL builds the new table, application transactions may modify the original table.
Those changes must also appear in the new copy. Otherwise, replacing the original table would lose updates that occurred during the rewrite.
PostgreSQL captures these changes through logical decoding and applies them to the new copy during the catch-up phase.
This is one of the important differences between concurrent and non-concurrent repacking.
Swapping the Table Files
After PostgreSQL has applied the captured changes, it requests an ACCESS EXCLUSIVE lock for the final replacement.
It performs a final pass over remaining captured changes and swaps the new table and index files into place.
The lock is generally held for a much shorter period than it would be during a complete non-concurrent rewrite. However, the duration depends on the remaining changes and whether PostgreSQL can acquire the required lock promptly.
Once the replacement is complete, PostgreSQL removes the old files and finishes the operation.
Running REPACK CONCURRENTLY on a Production Table
Before executing the command, check the table size, indexes, available disk space, and database activity.
For example, inspect the size of the orders table:
SELECT
pg_size_pretty(
pg_total_relation_size('public.orders')
) AS total_size,
pg_size_pretty(
pg_relation_size('public.orders')
) AS table_size,
pg_size_pretty(
pg_indexes_size('public.orders')
) AS index_size;The total size includes the table and its indexes, along with associated storage such as TOAST data.
Next, check whether the table has a primary key or suitable index-based replica identity. Concurrent repacking requires a primary key or an index-based replica identity.
You can inspect the replica identity configuration:
SELECT
c.relname,
c.relreplident
FROM pg_class c
JOIN pg_namespace n
ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relname = 'orders';The relreplident value indicates the configured replica identity mode. A primary key normally provides the required identity.
After confirming the prerequisites and available resources, execute:
REPACK (CONCURRENTLY, VERBOSE)
public.orders;The VERBOSE option prints informational progress messages during the operation.
You can also request statistics collection after the operation:
REPACK (CONCURRENTLY, ANALYZE, VERBOSE)
public.orders;ANALYZE helps PostgreSQL refresh planner statistics after the table rewrite. This form should be executed as a standalone command, not inside an explicit transaction block.
The executing role must have the MAINTAIN privilege on the table. The concurrent command must also run outside a transaction block.
Monitor REPACK Progress
Running a long maintenance operation without monitoring can make production troubleshooting difficult.
PostgreSQL 19 provides the pg_stat_progress_repack system view to monitor the operation.
Use the following query:
Join the conversation! Your thoughts help the community grow.