New PostgreSQL Extension pg_repack Optimizes Tables Without Downtime

08 Jul 2024

PostgreSQL users frequently encounter the issue of database bloat, which can severely degrade performance. Traditional methods such as VACUUM FULL and CLUSTER can be quite disruptive, but pg_repack offers an online solution that minimizes downtime. Here, we delve into the installation and configuration of pg_repack on a PostgreSQL 14 setup running on Red Hat Enterprise Linux 9 (RHEL 9).

What is pg_repack?

pg_repack is a PostgreSQL extension designed to eliminate bloat from tables and indexes. Unlike CLUSTER and VACUUM FULL, pg_repack works online without requiring an exclusive lock on the tables being processed, making it a more efficient and less intrusive option.

Installation

  1. Download the RPM Package
    First, download the pg_repack RPM package for PostgreSQL 14 on RHEL 9:
    wget https://download.postgresql.org/pub/repos/yum/14/redhat/rhel-9-x86_64/pg_repack_14-1.5.0-1PGDG.rhel9.x86_64.rpm
  2. Install the RPM Package
    Next, install the downloaded RPM package:
    rpm -iv pg_repack_14-1.5.0-1PGDG.rhel9.x86_64.rpm
  3. Verify Installation
    Verify the installation by listing the files included in the package:
    rpm -ql pg_repack_14-1.5.0-1PGDG.rhel9.x86_64.rpm | grep bin
    Alternatively, use the find command to locate the pg_repack binary:
    find / -name pg_repack
  4. Update PATH Environment Variable
    Add the PostgreSQL bin directory to your PATH:
    export PATH=$PATH:/usr/pgsql-14/bin
    Make this change permanent by adding it to your ~/.bashrc file:
    echo 'export PATH=$PATH:/usr/pgsql-14/bin' >> ~/.bashrc
    source ~/.bashrc

Configuration

  1. Create the Extension
    Connect to your PostgreSQL database and create the pg_repack extension:
    psql -c "CREATE EXTENSION pg_repack" -d employee
  2. Verify pg_repack Availability
    To ensure pg_repack is available, list the PostgreSQL binaries:
    pg_re
    You should see pg_repack listed among the other PostgreSQL binaries:
    pg_receivewal   pg_recvlogical  pg_repack       pg_resetwal     pg_restore      pg_rewind
    If you don't see it, you may have forgotten to source your bashrc (source ~/.bashrc)

Usage

Basic Usage
To repack a specific database (e.g., employee), use the following command:

pg_repack -d employee

Advanced Options
pg_repack offers several options to customize its behavior. Here are some useful ones:

  • Repack all databases:
    pg_repack -a
  • Repack a specific table:
    pg_repack -t table_name -d employee
  • Repack tables in a specific schema:
    pg_repack -s schema_name -d employee
  • Move repacked tables to a new tablespace:
    pg_repack --tablespace=new_tablespace -d employee
  • Order by specific columns:
    pg_repack --order-by=column_name -d employee

For a complete list of options, refer to the pg_repack help:

pg_repack --help

Top charts for Desktop

uTorrent

uTorrent

Latest update uTorrent download for free for Windows PC or Android mobile

5
1032 reviews
7248820
downloads
Zona

Zona

Latest update Zona download for free for Windows PC or Android mobile

4
614 reviews
1657187
downloads
WinRAR

WinRAR

Streamline file management with fast compression, secure your documents, and save space.

5
735 reviews
708161
downloads
Minecraft

Minecraft

Shape environments, explore vast worlds, and survive against monsters with endless creativity.

5
750 reviews
487454
downloads

News and reviews for Desktop

Adds Native Sudo to Windows 11 for Developers

Microsoft introduces a native sudo feature in Windows 11. It comes with limitations, sparking comparisons with the more mature gsudo.

Read more

Key PC Games Arriving in 2026 Across Steam

In 2026, PC games, including major and indie releases, will feature prominently on Steam, offering fresh options for North America.

Read more

Windows 11 Adds Native NVMe Driver for Faster SSD Performance

Microsoft introduces native NVMe driver on Windows 11 25H2; users experience SSD speed boosts.

Read more

New Game+ Showcase Highlights Indie Releases and Announcements

The New Game+ Showcase on 2024-01-08 spotlighted new Xbox and PC games, including Atomic Heart 2 and Beautiful Light, fueling gaming excitement.

Read more

Igrosoft Features in UK £5 Deposit Casinos

Igrosoft slot games now available at UK casinos offering £5 minimum deposits. Expect welcome bonuses and popular titles.

Read more

Enhance Windows 11 Taskbar with Windhawk Customizations

Windhawk tool enriches Windows 11 taskbar, adding customization options and themes.

Read more

Windows 11 Enhances Access with PowerToys Command Palette

PowerToys Command Palette streamlines app launching and system commands on Windows 11, enhancing user productivity.

Read more

Humble Bundle Offers 7 PC Games for $13.80, Benefits Charity

Humble Bundle's Decked Out Collection offers 7 PC games for $13.80 with proceeds going to the American Cancer Society.

Read more

Affordable Antivirus Options for Home Devices

Discover budget-friendly antivirus deals under $30 for 2026 with essential security features.

Read more

Free Script Removes AI Features from Windows 11

A new script disables AI features like Copilot in Windows 11, offering a cleaner interface.

Read more