Skip to content

PostgreSQL 19 REPACK (CONCURRENTLY): Does It Block Writes?

PostgreSQL 19 REPACK (CONCURRENTLY) keeps a table available during most of its rewrite, but a writer can still wait during the final table swap.

PostgreSQL 19 REPACK (CONCURRENTLY) test in VisuaLeaf, shrinking a table from 696 MB to 139 MB.
Repacking a PostgreSQL 19 table concurrently in VisuaLeaf.

A regular VACUUM removed the dead rows from my test table, but its total relation size was still 70 MB. I then used PostgreSQL 19's REPACK (CONCURRENTLY), which reduced it to 14 MB.

That result was part of my earlier PostgreSQL 19 feature tests. However, it left one question: what happens to reads and writes while the table is being repacked?

I repeated the test with a larger table so the operation would run long enough to watch. Reads continued, and writes also worked during the main rewrite. At the final table swap, however, REPACK had to wait for an open read transaction to release its lock.

This is the important part: REPACK (CONCURRENTLY) reduces blocking, but it does not mean that the operation is completely lock-free.

Below is the complete test, including the commands I ran and the lock that appeared at the end.

Run this test in a test database, not on a production table.

What REPACK does differently from VACUUM

When rows are deleted or updated, PostgreSQL leaves dead row versions behind. A regular VACUUM cleans them up and makes their space available for reuse inside the table.

However, VACUUM usually does not reduce the total space used by the table and its indexes on disk.

REPACK writes the live rows into a new table file and rebuilds the indexes. Once the rewrite is complete, PostgreSQL replaces the old table and index files with the new ones. This allows the unused space to be returned to the operating system.

CommandMakes dead space reusableReturns space to the operating systemKeeps the table available
VACUUMYesUsually noYes
REPACKYesYesNo
REPACK (CONCURRENTLY)YesYesYes, except for the brief final swap

Routine VACUUM is still necessary. REPACK is useful when a table has become much larger than its live data and you need to recover that disk space.

Create a table for the concurrency test

I created a separate table called repack_write_demo. This keeps the test independent from the tables used in my other PostgreSQL 19 examples.

The following command removes only an older copy of this test table. It does not affect the other tables in the pg19_lab schema.

DROP TABLE IF EXISTS pg19_lab.repack_write_demo;

CREATE TABLE pg19_lab.repack_write_demo (
    id bigint PRIMARY KEY,
    payload text NOT NULL,
    updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
);

The primary key is important. REPACK (CONCURRENTLY) needs a primary key or a suitable index-based replica identity so it can apply changes made while the new table is being built.

I inserted 1.2 million rows. The larger table gave me enough time to run queries from several SQL editors while REPACK was still active.

INSERT INTO pg19_lab.repack_write_demo (id, payload)
SELECT
    n,
    repeat(md5(n::text), 16)
FROM generate_series(1, 1200000) AS g(n);

I then deleted four out of every five rows:

DELETE FROM pg19_lab.repack_write_demo
WHERE id % 5 <> 0;

This left 240,000 rows in the table.

SELECT
    count(*) AS row_count,
    pg_size_pretty(
        pg_total_relation_size('pg19_lab.repack_write_demo')
    ) AS total_size
FROM pg19_lab.repack_write_demo;

The exact size can be different on another server, but the total relation size remained much larger than the live rows required. Here, pg_total_relation_size() includes the table, its indexes, and any TOAST data.

VisuaLeaf SQL Editor showing a PostgreSQL query that counts the rows and checks the total size of repack_write_demo, returning 240,000 rows and 696 MB.
Before REPACK, the table contained 240,000 rows and occupied 696 MB.

Run a regular VACUUM

I ran a regular vacuum as a separate statement:

VACUUM pg19_lab.repack_write_demo;

I then checked the size again:

SELECT
    count(*) AS row_count,
    pg_size_pretty(
        pg_total_relation_size('pg19_lab.repack_write_demo')
    ) AS total_size
FROM pg19_lab.repack_write_demo;

The dead rows were cleaned up, but the total relation size remained about 696 MB. PostgreSQL could reuse the empty space, but it had not returned it to the operating system.

This is normal behavior for a regular VACUUM. It does not mean that the command failed.

Run REPACK (CONCURRENTLY)

I opened a new SQL editor and ran:

REPACK (CONCURRENTLY) pg19_lab.repack_write_demo;

Keep auto-commit enabled and run this as its own statement. REPACK (CONCURRENTLY) cannot run inside a transaction block.

Without CONCURRENTLY, PostgreSQL takes an ACCESS EXCLUSIVE lock for the complete operation. Reads and writes must wait until the rewrite finishes.

With CONCURRENTLY, PostgreSQL performs most of the work with a less restrictive SHARE UPDATE EXCLUSIVE lock. It copies the live rows to a new file and uses logical decoding to collect the inserts, updates, and deletes that happen during the copy.

The stronger lock is delayed until the final table swap.

Monitor REPACK progress

While the command was running, I opened another SQL editor and queried the new PostgreSQL 19 progress view:

SELECT
    pid,
    relid::regclass AS table_name,
    phase,
    heap_tuples_scanned,
    heap_tuples_inserted,
    heap_tuples_updated,
    heap_tuples_deleted
FROM pg_stat_progress_repack;

The phases relevant to this test included:

PhaseMeaning
seq scanning heapReading the original table
writing new heapWriting the new table file
catch-upApplying concurrent changes or waiting for the final lock
swapping relation filesReplacing the old files with the new ones
performing final cleanupRemoving temporary data and finishing

The catch-up phase is especially useful. PostgreSQL also reports this phase while it is waiting for the ACCESS EXCLUSIVE lock needed for the final swap.

VisuaLeaf split SQL Editor showing REPACK (CONCURRENTLY) running on repack_write_demo and pg_stat_progress_repack reporting the seq scanning heap phase with 71,289 tuples scanned and inserted.
Monitoring a concurrent REPACK operation while PostgreSQL scans and rewrites the table.

Test a read while REPACK is running

In a second SQL editor, I ran a normal query while REPACK (CONCURRENTLY) was still active:

SELECT
    count(*) AS row_count,
    min(id) AS first_id,
    max(id) AS last_id
FROM pg19_lab.repack_write_demo;

The query completed normally. The table remained readable during the main copy and catch-up work.

A plain SELECT can still pause briefly if it arrives during the final ACCESS EXCLUSIVE lock. In this test, that final step was too short to create a visible delay for the read query.

Test a write during REPACK

I then inserted a new row from another SQL editor:

INSERT INTO pg19_lab.repack_write_demo (id, payload)
VALUES (
    2000001,
    repeat(md5('new-event'), 16)
)
RETURNING id, updated_at;

The insert completed while the main rewrite was running.

PostgreSQL captured the change through logical decoding and applied it to the new table before the final swap.

I also updated an existing row:

UPDATE pg19_lab.repack_write_demo
SET
    payload = repeat(md5('updated-during-repack'), 16),
    updated_at = clock_timestamp()
WHERE id = 5
RETURNING id, updated_at;

This update also completed during the main part of the operation.

VisuaLeaf split SQL Editor showing a concurrent REPACK operation running while an update to row 5 completes successfully and returns its new timestamp.
An UPDATE completes successfully while REPACK (CONCURRENTLY) is still running.

Make the final lock visible

The first writes completed because they ran during the main rewrite. I used another test to make the brief final lock easy to capture.

This test used three separate SQL editors. In the first editor, I disabled auto-commit and opened a read transaction:

BEGIN;

SELECT id
FROM pg19_lab.repack_write_demo
WHERE id = 5;

I left this transaction open. It continued to hold an ACCESS SHARE lock on the table.

In the second editor, I started REPACK (CONCURRENTLY) again:

REPACK (CONCURRENTLY) pg19_lab.repack_write_demo;

In the third editor, I checked the active locks and the current REPACK phase:

SELECT
    a.pid,
    l.mode,
    l.granted,
    a.state,
    a.wait_event_type,
    a.wait_event,
    p.phase,
    left(a.query, 60) AS query
FROM pg_stat_activity AS a
JOIN pg_locks AS l
    ON l.pid = a.pid
LEFT JOIN pg_stat_progress_repack AS p
    ON p.pid = a.pid
WHERE l.relation =
    'pg19_lab.repack_write_demo'::regclass
ORDER BY l.granted, a.pid, l.mode;

The result showed three relevant locks:

  • The read transaction held a granted AccessShareLock.
  • REPACK still held its granted ShareUpdateExclusiveLock.
  • REPACK was waiting for an AccessExclusiveLock before performing the final swap.

The strongest lock was not held throughout the rewrite. It was requested only at the end, but the open read transaction still delayed it. If the final ACCESS EXCLUSIVE lock is granted, new reads and writes must wait until the swap finishes.

Three VisuaLeaf SQL Editors showing an open read transaction, a concurrent REPACK operation, and PostgreSQL locks. REPACK holds a granted ShareUpdateExclusiveLock but waits for an ungranted AccessExclusiveLock because another session holds an AccessShareLock.
During the final swap, REPACK (CONCURRENTLY) waits for an ACCESS EXCLUSIVE lock while the open read transaction retains its ACCESS SHARE lock.

After capturing the result, I returned to the first editor and committed the read transaction:

COMMIT;

REPACK then acquired the ACCESS EXCLUSIVE lock, applied any remaining captured changes, swapped the files, and completed.

Check that the concurrent changes were preserved

After REPACK finished, I checked the rows that were inserted or updated during the tests:

SELECT
    id,
    right(payload, 32) AS payload_end,
    updated_at
FROM pg19_lab.repack_write_demo
WHERE id IN (5, 2000001)
ORDER BY id;

Both rows were present with their expected values. The insert and update were not lost when PostgreSQL swapped the table files.

I also checked the final table size:

SELECT
    count(*) AS row_count,
    pg_size_pretty(
        pg_total_relation_size('pg19_lab.repack_write_demo')
    ) AS total_size
FROM pg19_lab.repack_write_demo;

The live rows remained available, while the unused space left by the large delete was reclaimed.

VisuaLeaf SQL Editor showing the repack_write_demo table after concurrent REPACK, with 240,001 rows and a total size of 139 MB.
After REPACK (CONCURRENTLY), the table retained 240,001 rows while its total size fell from 696 MB to 139 MB.

REPACK (CONCURRENTLY) is not lock-free

The test produced three different results:

  1. Reads worked during the main rewrite.
  2. Inserts and updates worked while PostgreSQL copied the table and collected concurrent changes.
  3. An open read transaction delayed the final ACCESS EXCLUSIVE lock required for the file swap.

The useful distinction is not blocking versus non-blocking. REPACK (CONCURRENTLY) moves the strongest lock from the entire rewrite to the short final swap.

The length of that final pause depends on the workload. Long transactions can delay the lock, and new work can queue while REPACK waits. Changes that continue to arrive must also be applied before the table files can be swapped.

Requirements and limits

Before running REPACK (CONCURRENTLY) on a real table, check the following:

  • You need the MAINTAIN privilege on the table.
  • The table needs a primary key or a suitable index-based replica identity.
  • The command must run outside a transaction block.
  • PostgreSQL needs free disk space for a copy of the table and its indexes.
  • wal_level must be replica or higher, and an additional replication slot must be available under max_repack_replication_slots.
  • Concurrent mode does not support partitioned or UNLOGGED tables.
  • DDL changes on the table can cause the operation to fail.
  • A heavily updated table creates more changes for PostgreSQL to store and apply during catch-up.

The PostgreSQL documentation also includes an MVCC warning. After repacking commits, a concurrent transaction can see the table as empty if it is using a snapshot taken before REPACK started and had not accessed the table before the command began. A transaction that accessed the table earlier would already hold an ACCESS SHARE lock and delay the final swap instead. Check old and long-running transactions before using concurrent repacking on a production system.

REPACK should not replace routine vacuuming or be used as the main protection against transaction ID wraparound. It rewrites the complete table and all its indexes, so it is much heavier than VACUUM.

When to use REPACK (CONCURRENTLY)

It can be useful when:

  • a large delete or update left a table much larger than its live data;
  • you need to return that unused space to the operating system;
  • the table cannot remain blocked for the complete rewrite;
  • you have enough free disk space for the temporary copy;
  • a short pause during the final swap is acceptable.

Avoid scheduling it at the busiest time of day. Check for long transactions first, monitor pg_stat_progress_repack, and watch the lock queue as the command enters catch-up.

In my original size test, REPACK (CONCURRENTLY) reduced the table from 70 MB to 14 MB. In the larger concurrency test, reads and writes continued during the main rewrite, and all changes were preserved. However, an open transaction still delayed the final table swap.

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

References