pg_repack in POSTGRESQL DBA
pg_repack
is a powerful PostgreSQL extension that allows you to reclaim space and optimize table performance without requiring heavy locks or downtime. It’s especially useful for removing table and index bloat caused by frequent UPDATE
and DELETE
operations.`
What is pg_repack
?
-
A non-blocking tool that physically rewrites tables and indexes to remove dead tuples.
-
Unlike
VACUUM FULL
, it does not lock writes, making it safe for production use. -
Rebuilds tables/indexes in the background using a shadow copy, then swaps it in.
Why is it needed?
-
PostgreSQL uses MVCC (Multi-Version Concurrency Control) to handle concurrent updates/deletes.
-
When rows are deleted or updated, old row versions remain as dead tuples, leading to bloat.
-
VACUUM
clears these, but doesn’t shrink the physical table size. -
VACUUM FULL
reclaims space but locks the table, which is not suitable for production. -
pg_repack
solves this by:-
Creating a shadow copy of the table.
-
Rebuilding it in the background.
-
Swapping it in with minimal locking.
-
At Learnomate Technologies, we provide the best training on PostgreSQL and other database technologies. For more insights, visit our YouTube channel. Don’t forget to check out our website for more details about our training programs. Follow my Medium account @ankush.thavali for more articles and updates.