Planet PostgreSQL·· 16h ago
PostgreSQL 19 REPACK (CONCURRENTLY) in Practice: Reads and Writes Continue, but the Final Swap Still Waits on Locks
Roxana-Maria Haidiner: PostgreSQL 19 REPACK (CONCURRENTLY): Does It Block Writes?
Brief · Based on the source excerpt
PostgreSQL 19's REPACK (CONCURRENTLY) allowed reads and writes during the main rewrite, but the final table swap had to wait for an open read transaction to release its lock, so the operation is not fully lock-free. In the test, a regular VACUUM left the table at 70 MB while REPACK reduced it to 14 MB.
Why It Matters
Use REPACK (CONCURRENTLY) to reclaim disk space from bloated tables without a full outage, but budget for the final ACCESS EXCLUSIVE swap and avoid long-running transactions; validate in a test database first.
Technical Breakdown · Distilled from the original source
REPACK vs VACUUM
- A regular VACUUM cleans dead row versions and makes their space reusable inside the table, but usually does not shrink the total on-disk size of the table and its indexes; in the test the total relation size stayed at 70 MB after VACUUM.
- REPACK writes live rows into a new table file and rebuilds the indexes, then replaces the old table and index files, returning unused space to the operating system; the test table dropped to 14 MB.
- Comparison: VACUUM reuses dead space, usually does not return space to the OS, and keeps the table available; REPACK does both but not availability; REPACK (CONCURRENTLY) does both and keeps the table available except for the brief final swap.
- Routine VACUUM is still necessary; REPACK fits tables much larger than their live data where disk space must be reclaimed.
Concurrency test and lock behavior
- The author created a separate table
repack_write_demofor the concurrency test, keeping it independent from tables used in other PostgreSQL 19 examples, and repeated the test with a larger table so the operation ran long enough to observe. - Reads continued during the main rewrite, and writes also worked.
- At the final table swap, REPACK had to wait for an open read transaction to release its lock, showing the operation reduces blocking but is not completely lock-free.
- The author advises running this test in a test database, not on a production table.
Source: Planet PostgreSQL· postgr.es
PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.