Sub topic 10
Rewriting with Minimal Locking: pg_repack and REPACK
Assume you know basic SQL, PostgreSQL transactions, indexes, and how to use psql. This chapter focuses on rewriting a bloated table while keeping normal application traffic available.
Why online repacking is different
VACUUM marks space inside a table as reusable, but it usually does not return that space to the operating system. VACUUM FULL rewrites the table and can return space, but it holds an ACCESS EXCLUSIVE lock for the rewrite. That lock blocks reads and writes on the table.
Online repacking takes a different approach. It builds a new copy while the original table remains available, records changes made during the copy, applies those changes to the new copy, and then swaps the tables during a short locking step.
The rewrite needs extra disk space for the replacement table and its indexes. It also uses CPU, memory, I/O, and WAL. Plan the operation for a period when the database has enough capacity.
Using pg_repack
pg_repack is an extension and command-line client that rebuilds a table and its indexes with only short exclusive locks at the beginning and end. The table normally remains readable and writable during most of the operation.
The extension must be installed in the database first. The client program must also be installed where you run the command.
CREATE EXTENSION IF NOT EXISTS pg_repack;A typical command repacks one table:
pg_repack --dbname=mydb --table=public.ordersBy default, pg_repack needs a primary key or a unique index whose columns are all NOT NULL. It uses that key to identify rows while it copies and synchronizes changes. Without a suitable key, the operation cannot reliably match an updated or deleted row in the replacement table.
You also need sufficient privileges. In a production environment, check the extension version, client version, database connection, replication impact, and available disk space before starting.
REPACK where supported
Some PostgreSQL versions or compatible systems provide a built-in REPACK command. Its behavior depends on that system and version. Check the documentation before using it.
REPACK public.orders;Do not assume that this command exists in every PostgreSQL installation. Where it is available, determine whether the plain command takes a strong lock like VACUUM FULL or whether the version provides a lower-locking form. The locking behavior is part of the maintenance decision.
Choosing a method
| Method | Space returned to the operating system | Normal table access during most work | Main cost |
|---|---|---|---|
| `VACUUM` | Usually no | Yes | Leaves the table file in place |
| `VACUUM FULL` | Yes | No | Long strong lock and table rewrite |
| `pg_repack` | Yes | Usually yes | Extra space, WAL, and extension setup |
| `REPACK` | Depends on implementation | Depends on version | Verify version-specific behavior |
Use ordinary VACUUM when you need routine cleanup and space reuse. Use pg_repack when the table needs a physical rewrite but application traffic must continue. Use VACUUM FULL when a maintenance window permits blocking and simplicity matters. Use REPACK only after confirming the command's exact behavior on your system.
Exercise
Why can pg_repack keep a table available during most of its work, while VACUUM FULL cannot?
Show answer
pg_repack copies rows to a replacement table and synchronizes changes before a short final swap. VACUUM FULL rewrites the existing table while holding an ACCESS EXCLUSIVE lock.