Codework Tech
then
Talk to us↗

PostgreSQL 19 REPACK CONCURRENTLY: Clear Table Bloat Without Locking Writers

PostgreSQL 19 ships REPACK CONCURRENTLY, which clears table bloat without blocking reads or writes. Here is what it needs, where it refuses to run, and what teams still on 18 should do.

Pornsit Bureenok

5 min read

Two database table cylinders side by side: the left one is tall with amber dashed gaps labelled bloated 20 GB, an arrow points to a shorter solid teal one labelled packed 6 GB, with the heading PostgreSQL 19 REPACK CONCURRENTLY

Key takeaways

  • PostgreSQL 19 adds REPACK, which rewrites a table and returns space to the OS; the CONCURRENTLY mode does it without blocking reads or writes
  • CONCURRENTLY runs most of the work under SHARE UPDATE EXCLUSIVE and takes ACCESS EXCLUSIVE only for the final file swap
  • The table needs a primary key or index-based replica identity, and free disk space equal to the table plus all of its indexes
  • CONCURRENTLY is not MVCC-safe and fails if more than roughly 104 million rows change while it runs
  • Teams on 18 or older should stay with pg_repack; VACUUM's FULL option is now documented as deprecated

A table that has taken years of UPDATEs and DELETEs always ends up the same way: disk usage keeps growing while the row count stays flat, anything that scans the whole table gets noticeably slower, and cleaning it up properly means waiting for a 2 a.m. maintenance window, because VACUUM FULL locks the table for the entire rewrite. PostgreSQL 19, released on 19 November 2026, adds a command aimed straight at that problem: REPACK, and its most interesting mode, REPACK CONCURRENTLY, rewrites the whole table without blocking reads or writes.

For a back-end team this is bigger than it sounds. Clearing bloat used to mean asking for downtime or installing an extension.

Where table bloat comes from, and why plain VACUUM doesn't give space back

Postgres stores rows under MVCC. An UPDATE doesn't overwrite the old row, it writes a new version and leaves the old one behind as a dead tuple. Plain VACUUM cleans those dead tuples up, but it only marks that space as reusable. The table file itself doesn't shrink.

So a 20 GB table holding 6 GB of live data keeps occupying 20 GB, and every sequential scan still reads those mostly empty pages.

Until now you had two options. VACUUM FULL or CLUSTER rewrite the file compactly, but they hold an ACCESS EXCLUSIVE lock for the whole run, which on a large table means locking it out for tens of minutes. The other option was the pg_repack extension, which works online, but you need permission to install it and you have to keep its version in step with the server.

How REPACK CONCURRENTLY works

It copies the table into a new file while the original keeps serving reads and writes, then swaps the files at the end.

The PostgreSQL 19 REPACK documentation says that without CONCURRENTLY the command takes an ACCESS EXCLUSIVE lock at the start and holds it for the whole operation, exactly like VACUUM FULL. With CONCURRENTLY, the bulk of the work runs under SHARE UPDATE EXCLUSIVE, which doesn't block DML, and ACCESS EXCLUSIVE is taken only for the final swap. Changes made during the copy are captured with logical decoding and applied before that last lock is requested, so it is usually held briefly.

The syntax:

sql
-- reclaim space on one table without blocking other work
REPACK (CONCURRENTLY, VERBOSE) orders;

-- also physically order rows by an index, then ANALYZE
REPACK (CONCURRENTLY, ANALYZE) orders USING INDEX orders_created_at_idx;

Progress shows up in the pg_stat_progress_repack view. PostgreSQL 19 also adds pg_stat_lock and pg_stat_autovacuum_scores, so you get a better picture of lock activity and of how autovacuum is prioritising tables.

What you need before using REPACK CONCURRENTLY

The requirement people hit first is replica identity. The docs state that CONCURRENTLY cannot be used when the table lacks a primary key and an index-based replica identity. Log-style tables that nobody ever gave a primary key are out immediately.

Per the same page, CONCURRENTLY also refuses to run when:

  • the relation isn't a table, for example a materialized view
  • the table is UNLOGGED, partitioned, a system catalog or a TOAST table
  • the table's access method isn't heap
  • the command runs inside a transaction block
  • no additional replication slot is available. max_repack_replication_slots defaults to 5 and can only be set at server start (configuration docs)

Plan for the disk, too. The docs say you need free space at least equal to the size of the table plus all of its indexes, because a temporary copy is created. With USING INDEX, if the planner picks a sequential scan plus a sort, you also need room for a sort file up to the size of the table. And you need the MAINTAIN privilege on the table.

Caveats the documentation spells out

First and most important: CONCURRENTLY is not MVCC-safe. The docs say that after it commits, transactions using snapshots taken before the repack started may see the table as empty. Long-running reports and transactions that stay open for a while should not overlap with it.

Second, it can fail. It fails if more than about 104 million rows are concurrently updated or deleted, and it may fail if another transaction runs DDL on the table. It also refuses to process a table with an invalid index, so drop or reindex that first.

Third, REPACK itself counts as a long-running transaction, which can stop VACUUM from removing dead rows in other tables while it runs. On very large tables, keep an eye on pg_stat_activity.backend_xid.

Fourth, clustering with USING INDEX is a one-time operation. Rows written afterwards are not in that order, so set a fillfactor below 100 if you want the ordering to survive longer, and run ANALYZE when you're done, because the planner keeps ordering statistics.

Do you still need pg_repack and VACUUM FULL?

It depends on the version you're on.

On PostgreSQL 18 or older, keep using pg_repack. Its documentation covers versions 9.5 to 18, and the constraints are similar: the table needs a PRIMARY KEY or a unique index on NOT NULL columns, and a full-table repack needs free disk space roughly twice the size of the tables and their indexes.

On 19, the built-in command covers the normal cases, and the win is operational: no extension to install, no version to track, nothing to ask a managed-service provider to enable. As for VACUUM FULL, the PostgreSQL 19 VACUUM docs now describe the FULL option as deprecated and explain that it makes VACUUM behave like REPACK without a USING INDEX clause. The old commands still work for compatibility, but maintenance scripts should start moving over to REPACK.

Should you upgrade to PostgreSQL 19 now?

My honest take: don't move production this month, but do start testing on staging.

The reason not to rush is that 19.0 just shipped and most managed services haven't caught up. On AWS, PostgreSQL 19 is still in the RDS Database Preview Environment, where instances are retained for a maximum of 60 days and then deleted automatically. Fine for evaluation, not a place to put anything real.

The reason to start now is that 19 brings behaviour changes beyond REPACK. JIT compilation is disabled by default, because its cost estimates proved unreliable. TOAST uses lz4 compression by default when the build includes LZ4 support. A successful md5 login now logs a warning, and RADIUS authentication support has been removed entirely. Better to read about those today than to discover them on migration day.

Teams still on PostgreSQL 14 have less of a choice: 14 stopped receiving fixes on 12 November 2026, so the upgrade is going into a sprint either way, and if you're moving anyway, moving to 19 buys more time.

More posts on the same topics in Back-end & APIs.

If your team is weighing up a database upgrade plan, or chasing slow queries caused by bloat, talk to the Codework team.

Talk to our team

Frequently asked questions

Does REPACK CONCURRENTLY really lock the table?

Briefly. Most of the work runs under SHARE UPDATE EXCLUSIVE, which doesn't block reads or writes, and ACCESS EXCLUSIVE is taken only for the final swap, unlike VACUUM FULL which holds it for the entire rewrite.

Can I use REPACK CONCURRENTLY on a table without a primary key?

No. The documentation states the mode cannot be used when a table lacks both a primary key and an index-based replica identity. Add a primary key first, or run a non-concurrent REPACK in a maintenance window.

Can I drop pg_repack once I'm on PostgreSQL 19?

For normal cases the built-in REPACK replaces it. If you still run servers on 18 or older, keep pg_repack, because REPACK only exists from 19 onwards.

How much extra disk space should I plan for?

At least the size of the table plus all of its indexes, since the command builds a copy before dropping the original. With USING INDEX and a sort-based plan, add room for a sort file up to the table size.

Sources

  1. [1]PostgreSQL 19 Released — PostgreSQL Global Development Group, 19 Nov 2026
  2. [2]REPACK — PostgreSQL 19 Documentation — PostgreSQL Global Development Group
  3. [3]VACUUM — PostgreSQL 19 Documentation — PostgreSQL Global Development Group
  4. [4]pg_repack documentation — PGXN
  5. [5]PostgreSQL 19 Beta 3 is now available in Amazon RDS Database Preview Environment — Amazon Web Services, 18 Aug 2026
  • Database
  • Performance
  • PostgreSQL
  • Release Update

Written by

Pornsit Bureenok

Technical Lead

A good system has to be reliable, scalable and maintainable.

Let's build together

Have an idea worth building?

Tell us about it, and we'll work out the best way to bring it to life.