PostgreSQL 19Dev & Eng

Content type: Concept

6 min read

PostgreSQL 19: REPACK (CONCURRENTLY) shrinks tables without blocking reads, but locks at the end

A test published on Planet PostgreSQL shows that REPACK (CONCURRENTLY) allows reading and writing during most of a table's reorganization. But there is a final moment when the lock returns, and it can take longer than it seems.

Article · Dev & Eng
Reading
6 min
Tables
1
Code
1

A test published on Planet PostgreSQL shows that REPACK (CONCURRENTLY) allows reading and writing during most of a table's reorganization. But there is a final moment when the lock returns, and it can take longer than it seems.

Every database that receives many UPDATE and DELETE operations accumulates the same problem: the table becomes physically larger than the live data it holds. A routine VACUUM handles the logical cleanup, but it doesn't return space to the operating system. PostgreSQL 19 brought the REPACK command, with the REPACK (CONCURRENTLY) variant, to tackle exactly this excess of space without requiring a full maintenance window. A test published on Planet PostgreSQL, on the VisuaLeaf blog, measured exactly what happens to reads and writes while the command runs, and the answer has nuance.

What REPACK does that VACUUM doesn't

When a row is deleted or updated, PostgreSQL leaves behind a dead version of the row (the MVCC model requires this). A regular VACUUM cleans up these dead versions and marks the space as reusable within the table's own file, but it normally doesn't shrink the file on disk.

REPACK, on the other hand, writes the live rows to a new file and rebuilds the indexes from scratch. When the rewrite finishes, PostgreSQL replaces the old files with the new ones, and only then does the unused space return to the operating system.

CommandFrees dead spaceReturns space to OSKeeps table available
VACUUMYesUsually notYes
REPACKYesYesNo
REPACK (CONCURRENTLY)YesYesYes, except during the brief final swap

The test: a table with 1.2 million rows

To have enough time to observe the behavior in real time, the test created a table with id bigint PRIMARY KEY, inserted 1.2 million rows, and deleted four out of every five, leaving 240,000 live rows. Before any cleanup, pg_total_relation_size() (which sums table, indexes, and TOAST data) showed 696 MB.

A regular VACUUM pg19_lab.repack_write_demo; cleaned up the dead rows, but the total size remained at around 696 MB: the space became reusable internally, without being returned to the operating system. This is expected behavior for VACUUM, not a flaw.

Next, running REPACK (CONCURRENTLY) pg19_lab.repack_write_demo; as a standalone statement outside a transaction block, the size dropped from 696 MB to 139 MB, a reduction of about 80%. In an earlier, smaller test published on the same blog, the same operation had already reduced a table from 70 MB to 14 MB.

VisuaLeaf blog card illustrates the REPACK CONCURRENTLY test on PostgreSQL 19, showing the table's reduction from 696 MB to 139 MB alongside the SQL editor screens with the commands and lock monitoring
VisuaLeaf blog card illustrates the REPACK CONCURRENTLY test on PostgreSQL 19, showing the table's reduction from 696 MB to 139 MB alongside the SQL editor screens with the commands and lock monitoring. Reprodução: postgr.es.

Under the hood: why writes keep working

Without CONCURRENTLY, PostgreSQL takes an ACCESS EXCLUSIVE lock for the entire operation: reads and writes simply wait until it finishes. With CONCURRENTLY, most of the work runs under a more permissive lock, the SHARE UPDATE EXCLUSIVE lock, which doesn't block concurrent reads or writes.

While copying the live rows to the new file, PostgreSQL uses logical decoding to capture, in parallel, every INSERT, UPDATE, and DELETE that reaches the original table. That's why the table needs a PRIMARY KEY or an index-based replica identity: without it, the mechanism has no way to identify which row changed in order to apply the change to the new copy. In the test, an INSERT and an UPDATE fired from other sessions while REPACK was running completed normally, and both changes appeared intact in the table after the swap.

The moment that actually blocks

The test isolated the critical moment using three simultaneous sessions. In the first, it opened a read transaction and left it open:

sql
BEGIN;
SELECT id FROM pg19_lab.repack_write_demo WHERE id = 5;

In the second, it fired REPACK (CONCURRENTLY) again. In the third, it queried pg_locks along with pg_stat_activity and pg_stat_progress_repack. The result showed three locks at the same time: the read transaction still held a granted AccessShareLock; REPACK held its granted ShareUpdateExclusiveLock; and REPACK was waiting on an AccessExclusiveLock, needed only for the final file swap.

While the read transaction remained open, REPACK stayed stuck in the catch-up phase, visible in pg_stat_progress_repack, unable to swap the files. As soon as the transaction issued COMMIT, the exclusive lock was granted, the pending changes were applied, and the operation finished.

That is what CONCURRENTLY means here: much less blocking, not no blocking.

Test published on the VisuaLeaf blog (Planet PostgreSQL)

Tracking progress in production

The pg_stat_progress_repack view, new in PostgreSQL 19, exposes phases that are worth monitoring before trusting a concurrent REPACK during peak hours:

The SQL editor displays the query against pg_stat_progress_repack showing the 'seq scanning heap' phase and the count of tuples scanned and inserted during REPACK CONCURRENTLY
The SQL editor displays the query against pg_stat_progress_repack showing the 'seq scanning heap' phase and the count of tuples scanned and inserted during REPACK CONCURRENTLY. Reprodução: postgr.es.
  • seq scanning heap: reading the original table;
  • writing new heap: writing the new file;
  • catch-up: applying concurrent changes, or waiting for the final lock;
  • swapping relation files: actual swap of the files;
  • performing final cleanup: removal of temporary data.

The catch-up phase deserves extra attention: it's where the command gets stuck waiting for the AccessExclusiveLock, and it's where a long, forgotten transaction in another developer's tab can hold up the operation for much longer than expected.

Requirements and limits before using in production

Before running REPACK (CONCURRENTLY) outside a test environment, the following points raised by the source and by PostgreSQL's documentation apply:

  1. The MAINTAIN privilege on the table is required.
  2. The table needs a PRIMARY KEY or an index-based replica identity.
  3. The command must run outside a transaction block.
  4. Free disk space is needed for a full copy of the table and its indexes.
  5. wal_level must be replica or higher, with a replication slot available under max_repack_replication_slots.
  6. Concurrent mode doesn't work on partitioned or UNLOGGED tables.
  7. Any DDL on the table during the operation can make REPACK fail.

PostgreSQL's documentation also includes an MVCC warning worth noting: after REPACK commits, a transaction that took its snapshot before the operation started and never accessed the table may see it as empty. A transaction that had already accessed the table before, like the one in the test, already holds an ACCESS SHARE lock and ends up delaying the final swap instead of seeing this side effect.

When it's worth it, and when it isn't

In summary: REPACK (CONCURRENTLY) solves a specific problem, a table much larger than the live data justifies, without requiring it to be unavailable throughout the entire rewrite. It doesn't replace routine VACUUM or serve as the main protection against transaction ID wraparound, because it rewrites the entire table and all its indexes, something far heavier than a regular cleanup scan.

It makes sense to use it when a bulk DELETE or UPDATE has left the table bloated, there is spare disk space for the temporary copy, and a short pause at the final swap is tolerable. Before scheduling it, it's worth checking for long-open transactions, monitoring pg_stat_progress_repack, and avoiding peak hours, exactly as the test showed.

The arrival of a native command for this kind of reorganization in PostgreSQL 19 doesn't change operational discipline: you still need to know what's running on the database before pulling the trigger, because the strong lock still exists, just shorter and more predictable.

Translated from the Brazilian Portuguese original · Read the original

Read also