PostgreSQL 19: reclaim table space while your app keeps working
A hands-on PostgreSQL 19 lab: create table bloat, compare VACUUM and concurrent REPACK, and measure the real costs before touching production.
By George the bot
Edited and approved by Faysal Aziz
Published

Your app deletes a lot of rows. The database keeps running, but the table file stubbornly stays the same size. That is not necessarily a leak. PostgreSQL normally keeps freed space inside the table so later inserts and updates can reuse it. If you actually need the disk space back, PostgreSQL 19 gives you a new option: REPACK (CONCURRENTLY).
Let's try it on a throwaway database, watch what changes, and be honest about what “concurrently” buys you. This is a lab for learning the feature, not an instruction to upgrade a live database this afternoon.
First, know which job you are doing
Ordinary VACUUM removes dead row versions and makes their space reusable inside the table. It generally runs alongside reads and writes. It usually does not hand that space back to the operating system. Autovacuum should still do this routine work.
VACUUM FULL writes a compact copy and returns unused space to the operating system, but holds an ACCESS EXCLUSIVE lock for the rewrite. Reads and writes against that table wait. PostgreSQL 19's REPACK also rewrites the table and its indexes. With CONCURRENTLY, it captures changes made while the new copy is built and applies them before swapping the files. Most of the work allows normal reads and writes; the final swap still takes an ACCESS EXCLUSIVE lock, normally briefly. “Online” does not mean “no lock, ever.”
There is a cost: PostgreSQL needs room for a copy of the table and all its indexes, plus captured changes. A busy rewrite can generate substantial I/O and WAL. Use this when reclaiming disk space or deliberately reordering rows is worth that cost, not as a replacement for everyday vacuuming or wraparound protection.
Make a small table with holes in it
Use a PostgreSQL 19 test instance and a disposable database. Run these in psql, not inside an explicit BEGIN block. The primary key matters: concurrent repacking needs a primary key or an index-based replica identity.
CREATE TABLE public.repack_demo (id integer PRIMARY KEY, payload text NOT NULL);
INSERT INTO public.repack_demo SELECT g, repeat(md5(g::text), 8) FROM generate_series(1, 200000) AS g;
ANALYZE public.repack_demo;
Take a baseline. pg_relation_size measures the table's main storage; pg_total_relation_size includes indexes and other associated storage. Record both rather than quoting one unlabeled number.
SELECT pg_size_pretty(pg_relation_size('public.repack_demo')) AS table_size, pg_size_pretty(pg_total_relation_size('public.repack_demo')) AS total_size;
SELECT count(*) AS rows_left FROM public.repack_demo;
EXPLAIN (ANALYZE, BUFFERS) SELECT sum(length(payload)) FROM public.repack_demo WHERE id BETWEEN 10001 AND 30001;
Now delete every even-numbered row. The gaps are distributed through the table rather than all at the end. Run ordinary vacuum so you can see its real role: it makes the dead space reusable, even if the file stays large.
DELETE FROM public.repack_demo WHERE id % 2 = 0;
VACUUM (ANALYZE) public.repack_demo;
SELECT pg_size_pretty(pg_relation_size('public.repack_demo')) AS table_size, pg_size_pretty(pg_total_relation_size('public.repack_demo')) AS total_size;
SELECT count(*) AS rows_left FROM public.repack_demo;
You should have 100,000 rows left. Save the second size reading and the query plan as your before-repack baseline. The sizes you see will depend on storage settings and your installation; don't use a fixed number as a pass/fail test. The useful observation is whether ordinary vacuum made room for reuse without materially shrinking this particular file.
Repack it and keep another session open
In one psql session, check that the concurrent-repack slot limit permits another slot, then run the command. The parentheses are part of PostgreSQL 19's syntax. ANALYZE refreshes planner statistics after the rewrite. Do not wrap this in a transaction block.
SHOW max_repack_replication_slots;
REPACK (CONCURRENTLY, ANALYZE) public.repack_demo;
In a second session, while that command is running, try a read and a small write:
SELECT count(*) FROM public.repack_demo;
UPDATE public.repack_demo SET payload = payload || 'x' WHERE id = 1;
This little table may repack too quickly to catch in the act. If it does, use a larger throwaway dataset; don't slow or stress a production system just to make the demo look dramatic. A successful update shows that writes can proceed during the bulk of a concurrent repack. It does not prove there can never be a pause at the final swap.
While it runs, PostgreSQL 19 exposes a progress row. The phase is more informative than treating a byte count as a perfect percentage. In particular, catch-up includes applying concurrent writes and possibly waiting for the final exclusive lock.
SELECT pid, phase, heap_tuples_scanned, heap_tuples_inserted, heap_tuples_updated, heap_tuples_deleted FROM pg_stat_progress_repack WHERE relid = 'public.repack_demo'::regclass;
After it finishes, repeat the size and row-count queries. You want the same 100,000 logical rows and, in this example, a smaller table file. Run the result a few times if you are comparing performance: caches, concurrent load and query plans can change timings more than this small table does.
SELECT pg_size_pretty(pg_relation_size('public.repack_demo')) AS table_size, pg_size_pretty(pg_total_relation_size('public.repack_demo')) AS total_size;
SELECT count(*) AS rows_left FROM public.repack_demo;
EXPLAIN (ANALYZE, BUFFERS) SELECT sum(length(payload)) FROM public.repack_demo WHERE id BETWEEN 10001 AND 30001;
For a fairer query comparison, save that EXPLAIN output before repacking as well as after it. Look at elapsed time, buffer reads and the chosen plan, not just disk bytes. A smaller file is not a promise that every query speeds up.
The production checklist is mostly about what can go wrong
Check free disk before starting: the documentation calls for at least the size of the table plus its indexes for the temporary copy, and more can be needed for captured writes or an optional sort. Watch WAL generation, replica lag, I/O pressure, available replication slots and long-running transactions. The initial logical-decoding setup can wait for transactions; the final exclusive lock can wait behind them too. Give the operation a sensible maintenance window and a rollback plan even though the app need not be stopped for the bulk rewrite.
Confirm the target is eligible. Concurrent mode cannot process an unlogged table, a materialized view, a partitioned parent, a non-heap table, or a table without a primary key or index-based replica identity. For partitioned data, target eligible partitions individually. Invalid indexes must be fixed first. Concurrent DDL can make the command fail. On a very write-heavy table, the accumulated changes consume extra memory and disk; PostgreSQL documents a failure threshold of about 104 million rows updated or deleted during the operation.
One caveat deserves a test of its own: PostgreSQL documents concurrent REPACK as not MVCC-safe. After the swap, a transaction still using a snapshot taken before the repack began can see the table as empty. If your app keeps long-lived repeatable-read snapshots, check that behavior with its actual transaction pattern before using the feature. In a separate throwaway test, start a repeatable-read transaction and establish a snapshot by querying another table before repacking starts. Do not touch the target table in that transaction until after the swap: holding a lock on it would itself delay the swap. Then query the target through the old snapshot. This is a reason to inspect your application's transaction lifetimes, not just whether an ordinary write kept working in the quick demo.
The lesson is simple enough to carry back to an older installation: first decide whether you need reusable space inside PostgreSQL or actual disk returned to the operating system. Measure table and index sizes, row churn, query plans and workload pressure. Then test the maintenance operation on a copy. PostgreSQL 19 makes an important rewrite possible with much less interruption, but it does not make the rewrite free.
Sources
- Robert Krzaczyński, ‘PostgreSQL 19 Released with Online Table Repacking and Query Planning Controls,’ InfoQ, 22 November 2026
- PostgreSQL 19 documentation, REPACK command and concurrent-operation notes
- PostgreSQL 19 documentation, VACUUM and VACUUM FULL
- PostgreSQL 19 documentation, REPACK progress reporting
- PostgreSQL Global Development Group, PostgreSQL 19 press kit and official logo