Skip to content
HN On Hacker News ↗

Postgres 19 REPACK: Built-In Online Table Bloat Cleanup

▲ 184 points • 29 comments • by craigkerstiens • 4d ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this text is a mix of AI and human-written content.

62 %

AI likelihood · overall

Mixed
38% human-written 62% AI-generated
SEGMENTS · HUMAN 22 of 38
SEGMENTS · AI 15 of 38
WORD COUNT 1,508
PEAK AI % 100% · §7
Analyzed
Nov 20
backend: pangram/v3.3
Segments scanned
38 windows
avg 40 words each
Distribution
38 / 62%
human / AI fraction
Verdict
Mixed
Pangram v3.3

Article text · 1,508 words · 38 segments analyzed

Human AI-generated
§1 Human · 0%

REPACK is my favorite feature in Postgres 19. It's not the flashiest, but it is the one that’s going to matter most if you’re running a production database. Vacuum, autovacuum and bloat have always been this sore spot for Postgres users. On the one hand, Postgres MVCC design is not new. And yet, on high update systems, this churn of having dead rows that get freed up later, vacuum being tweaked to the right levels to maintain low bloat is still a bit of an inexact science. Once bloat does exist, it’s just sitting there. And you see that unclaimed space not being used.

§2 Human · 0%

Before Postgres 19, your options for reclaiming that space were: VACUUM FULL: Built-in, but takes a read/write lock for the entire duration.

§3 Mixed · 37%

On a large table, that means minutes of downtime.

§4 Human · 0%

dump/restore: Fail over to a fresh database that is a clean snapshot.

§5 Human · 0%

pg_repack: A third-party extension for online rebuilds from an external process. Generally a good option, but not without caveats and risks. pg_squeeze: Another third-party extension for online rebuilds via a background worker, also solid in production but again not without caveats and risks. Enter Postgres 19: REPACK CONCURRENTLY: reclaims bloated table space while the table stays fully readable and writable, takes only a brief lock at the very end, and runs without any extension How Postgres tables become bloated Let’s back up. You may be familiar with bloat as a seasoned Postgres user, but just to make sure we’re all on the same page… It's one of those Postgres realities, the tradeoff for MVCC.

§6 AI · 100%

Tables accumulate dead tuples over time: when rows are updated or deleted, the old versions stick around.

§7 AI · 100%

Regular autovacuum typically marks that space as reusable. For most tables, that's enough.

§8 AI · 100%

But for large, update-intensive tables, vacuum can fall behind.

§9 Human · 0%

Dead rows accumulate as table bloat.

§10 Human · 0%

The file grows, but since VACUUM can only mark that space as internally reusable, it doesn't shrink the table or return space to the OS.

§11 Human · 0%

To actually reclaim disk space, you need to rebuild the table.

§12 Human · 0%

How do you know if you have a bloat problem? You can do a quick check using pg_stat_user_tables. The tuple counts are estimates, but this is one good diagnostic signal.

§13 Human · 0%

SELECT schemaname, relname, pg_size_pretty(pg_relation_size(schemaname || '.' || relname)) AS size, n_live_tup, n_dead_tup, CASE WHEN n_live_tup > 0 THEN round((n_dead_tup::float / n_live_tup::float)::numeric, 4) END AS dead_tup_ratio, last_autovacuum, last_autoanalyze FROM pg_stat_user_tables ORDER BY dead_tup_ratio DESC NULLS LAST;(Query adapted from Crunchy Data's bloat checking guide) A rough rule of thumb: Below 50% bloat: Generally acceptable.

§14 AI · 100%

Monitor it and check autovacuum settings, but no urgent action needed. Above 50% bloat: You're likely wasting disk space and paying a performance cost from extra page reads. It's at least worth investigating — and possibly addressing — if you see slow queries or growing table sizes.

§15 AI · 100%

There is no single bloat percentage that determines whether a table needs attention.

§16 AI · 100%

Consider wasted bytes, workload, growth, and whether reclaiming space is worth the rewrite.

§17 AI · 100%

How pg_repack works The pg_repack extension was the option for online table rebuilds for years.

§18 AI · 100%

Here's how it works: External tool: pg_repack runs as a command-line utility (pg_repack --table my_table -d mydb) from an external server or management host. You needed to install both the extension inside Postgres (CREATE EXTENSION pg_repack) and the client binary on whatever machine would run it. Trigger-based change capture: It creates a log table and installs triggers on your target table to capture all INSERT/UPDATE/DELETE operations during the rebuild. This adds trigger overhead on every write for the duration of the operation. Shadow table approach: It creates a new copy of the table, populates it from the original, replays the trigger log, then does a brief lock-and-swap, along with a final table rename. This worked well, and many production systems relied on it for years. But installing third-party extensions makes some teams nervous, and the external binary adds operational overhead. How pg_squeeze works pg_squeeze (from CYBERTEC, the same team behind much of the Postgres 19 REPACK work) came along and took a more modern approach. It's also seen wide production use. Logical decoding: pg_squeeze uses Postgres’ WAL-based logical decoding to capture concurrent changes. This means no triggers are installed on your table. This is the same approach that PG 19's built-in REPACK (CONCURRENTLY) now uses. Background worker: This runs entirely inside the database as a background worker process which means unlike pg_repack, you don’t need an external CLI tool. You can also configure it to run automatically when bloat thresholds are exceeded. Still an extension: Lots of folks prefer a built-in option, but this still requires wal_level = logical, a replication slot, compiling and installing the extension, and adding it to shared_preload_libraries. pg_squeeze in a sense is the prototype for what became PG 19's REPACK (CONCURRENTLY). The built-in version inherits the logical decoding approach but integrates it directly into the server without an extension, shared_preload_libraries, or separate configuration tables. Enter REPACK Postgres 19 adds REPACK as a first-class SQL command with three modes: -- Basic repack, like VACUUM FULL — takes ACCESS EXCLUSIVE lock REPACK my_table; -- Online repack using CONCURRENTLY just like you would with indexes REPACK (CONCURRENTLY) my_table; -- Online repack + reorder by index REPACK (CONCURRENTLY) my_table USING INDEX my_index;REPACK (CONCURRENTLY) This is what makes the feature production-ready. With CONCURRENTLY, the ACCESS EXCLUSIVE lock is only held briefly at the end. During the bulk of the operation, which for a large table could take minutes, the table remains fully readable and writable.

§19 Human · 0%

Here's what happens internally: REPACK (CONCURRENTLY, VERBOSE, ANALYZE) repack_test;INFO: repacking "public.repack_test" in physical order INFO: "public.repack_test": found 0 removable, 1000000 nonremovable row versions in 38452 pages Time: 9406.788 ms (00:09.407) Before: 493 MB → After: 228 MB (54% reduction)Three options in play here: CONCURRENTLY: Rebuilds online, with only a brief exclusive lock at the very end for the file swap.

§20 Human · 0%

VERBOSE: Prints progress at INFO level. ANALYZE: Runs ANALYZE after the repack completes to update planner statistics. REPACK with USING INDEX REPACK can also physically reorder your table's rows to match an index.

§21 AI · 100%

If your workload frequently reads ranges of related rows, clustering with USING INDEX may improve locality.

§22 AI · 100%

Measure the effect on your own queries; later updates are not automatically clustered.

§23 Human · 0%

This is a big deal for the REPACK feature beyond just bloat removal.

§24 Human · 0%

It's a performance feature in its own right.

§25 AI · 100%

For example, say you have an orders table and frequently query WHERE created_at BETWEEN '2026-01-01' AND '2026-01-31'.

§26 AI · 100%

Without physical ordering, those rows could be scattered across hundreds of pages. With REPACK orders USING INDEX idx_orders_created_at, the rows are stored sequentially on disk in date order, so a range scan reads pages in sequence instead of random I/O across the whole table.

§27 Human · 0%

You might know CLUSTER from older Postgres versions. It did the same thing, but took a lock. REPACK (VERBOSE, ANALYZE) product_inventory USING INDEX idx_product_category;INFO: repacking "public.product_inventory" using index scan on "idx_product_category" Time: 15230.264 ms (00:15.230) Before: 618 MB → After: 320 MB (48% reduction, rows now ordered by category)REPACK vs. VACUUM FULL sample performance I did some testing just to verify how this works. For the 493 MB test case with 1M live rows remaining after deleting 1M of the original 2M rows, the reported timings were: Method Time Result Table Accessible? VACUUM FULL 2.7s 228 MB No (locked entire time) REPACK 5.9s 228 MB No (locked entire time) REPACK (CONCURRENTLY) 9.4s 228 MB Yes (brief lock at end only) In one test on a constrained system, REPACK (CONCURRENTLY) took 9.4 seconds for the stated table and test conditions.

§28 Human · 4%

Results vary with table, indexes, workload, and hardware.

§29 Human · 0%

REPACK CONCURRENTLY is slower in wall-clock time.

§30 Human · 0%

It's doing more work: logical decoding and WAL replay on top of the table rebuild. The tradeoff here is more time to keep your table online. Requirements for REPACK CONCURRENTLY You need 2x the space for the table and indexes you are repacking, since for a period, you will have two copies of it. The table must be logged with WAL, cannot be UNLOGGED. The table cannot be partitioned. The table must have an indexable primary key. The table cannot be a system catalog or TOAST table. Only one concurrent REPACK can run per database at a time.

§31 Human · 0%

Tables using non-heap access methods are not supported.

§32 Human · 0%

A note on MVCC safety: REPACK CONCURRENTLY is not fully MVCC-safe.

§33 Human · 3%

During the brief final swap, a long-running transaction could theoretically miss concurrent changes.

§34 Human · 0%

A concurrent transaction using a snapshot taken before repacking began can see the table as empty.

§35 Human · 0%

This is the same limitation pg_repack had, and it's documented in the Postgres 19 release notes.

§36 AI · 100%

Review this behavior against your workload’s transaction and consistency requirements. When to use REPACK vs.

§37 AI · 100%

VACUUM Regular VACUUM and REPACK solve different problems: Situation Use Why Routine maintenance VACUUM (autovacuum) Marks dead tuples reusable, updates visibility map. Fast, no lock, runs automatically. Some bloat, may be growing faster than autovacuum clears dead rows Tune autovacuum settings Adjust autovacuum_vacuum_scale_factor for large/busy tables.

§38 AI · 100%

Also worth trying the parallel vacuum features in Postgres 19.