User Agents

Web browsers and everyday web tech, explained

pg_repack on Azure PostgreSQL: Reclaim Space Without Locks

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

pg_repack is a PostgreSQL extension that reclaims disk space from bloat in tables and indexes without long locks, making it ideal for busy Azure Database for PostgreSQL instances. Unlike VACUUM FULL, which locks the table exclusively, pg_repack rebuilds tables and indexes using a copy-and-swap method, allowing reads and writes to continue during most of the process. This guide walks you through enabling and using pg_repack on Azure PostgreSQL, including setup, execution, and troubleshooting common issues.

What You'll Need

  • An active Azure Database for PostgreSQL server (Single Server or Flexible Server)
  • Admin privileges on the PostgreSQL server (usually the admin login created at setup)
  • psql or another PostgreSQL client connected to your database
  • Sufficient disk space: pg_repack requires extra space to create a copy of the table during repack
  • Superuser role access or the azure_pg_admin role (required to install extensions)

Step-by-Step

  1. Verify pg_repack is supported on your Azure PostgreSQL version
    pg_repack is supported on Azure Database for PostgreSQL running PostgreSQL 10 and above. Check your server version using:
    SELECT version();
    If your version is 10+, proceed.
  2. Connect to your database as an admin user
    Use psql or a GUI tool like pgAdmin to connect with the admin account. For example, using psql:
    psql -h your-server.postgres.database.azure.com -U adminuser@your-server -d yourdb
  3. Enable the pg_repack extension
    Run the following command in your database:
    CREATE EXTENSION IF NOT EXISTS pg_repack;
    This will install the extension if it’s not already present. You may need to be logged in as a user with the azure_pg_admin role.
  4. Verify the extension is loaded
    Check that pg_repack appears in the list of installed extensions:
    \dx
    Look for pg_repack in the output. Alternatively, query:
    SELECT * FROM pg_extension WHERE extname = 'pg_repack';
  5. Test connectivity and permissions
    Run a dry-run check to confirm pg_repack can access the table you want to repack. Replace your_table with the actual table name:
    pg_repack --table=your_table --dry-run yourdb
    This will simulate the process without making changes.
  6. Run pg_repack on a specific table
    To repack a single table, use the command-line tool pg_repack from your local machine or a server with network access to Azure PostgreSQL:
    pg_repack -h your-server.postgres.database.azure.com -U adminuser -d yourdb --table=your_table
    You’ll be prompted for the password. The tool connects, creates a log table, copies data, and swaps the table atomically.
  7. Repack multiple tables or entire database
    To repack all bloat-affected tables in the database:
    pg_repack -h your-server.postgres.database.azure.com -U adminuser -d yourdb
    This runs without specifying a table, targeting all eligible tables with bloat.
  8. Monitor progress
    While pg_repack runs, you can monitor activity in the database:
    SELECT query FROM pg_stat_activity WHERE query LIKE '%pg_repack%';
    You can also watch system performance in the Azure portal under Metrics.
  9. Verify completion and cleanup
    After completion, pg_repack automatically removes its temporary objects. Confirm the original table size has decreased using:
    \dt+ your_table
    Compare before and after sizes to verify space recovery.
  10. Remove pg_repack (optional)
    If no longer needed, you can uninstall the extension:
    DROP EXTENSION pg_repack;
    Ensure no ongoing repack operations are running first.

Common Mistakes

  • Running without enough disk space: pg_repack duplicates the table, so ensure at least 1.5x the table size is free. Running out of space halts the process and may leave temporary objects.
  • Using a non-admin user without proper roles: Installing and running pg_repack requires azure_pg_admin or equivalent superuser permissions. Regular users will get permission denied errors.
  • Assuming it works on all object types: pg_repack does not support tables with certain features like unlogged tables, materialized views, or tables using triggers in specific configurations.
  • Not checking for long-running transactions: Active transactions can delay or conflict with pg_repack. Check pg_stat_activity before starting.

Tips

  • Schedule during low-traffic periods: Although pg_repack allows concurrent access, it increases I/O and CPU load. Run during off-peak hours to minimize impact.
  • Use --jobs to parallelize: On Flexible Server or high-resource instances, use --jobs=N to repack multiple tables concurrently, speeding up the process.
  • Monitor disk usage in Azure portal: Watch the “Storage” metric to see real-time changes as space is reclaimed.
  • Combine with regular vacuuming: pg_repack is not a replacement for routine VACUUM operations. Use it selectively for heavily bloated tables.

Troubleshooting

If pg_repack fails, first check the error message and PostgreSQL logs (available in Azure portal under “Logs”). Common issues include:

  • “Permission denied for schema pg_repack”: Ensure the user has CREATE privilege on the public schema or the default schema. Run:
    GRANT CREATE ON SCHEMA public TO your_user;
  • Connection timeout or SSL errors: Ensure your firewall allows outbound connections and the server has SSL enforcement configured. Use --no-ssl only if SSL is disabled (not recommended).
  • “pg_repack is already running on this table”: A previous run may have failed. Clean up leftover objects with:
    DROP TABLE IF EXISTS pg_repack.log_table_name;
    Or restart the server if needed.
  • Extension not found: Confirm the extension is installed in the correct database. pg_repack must be installed in each database where it’s used.

Frequently Asked Questions

Can I use pg_repack on Azure PostgreSQL Flexible Server?

Yes, pg_repack works on both Azure Database for PostgreSQL Single Server and Flexible Server, provided you have the required admin roles.

Does pg_repack lock my table?

It briefly locks the table at the start and end, but not during data copy. Most operations can continue, unlike VACUUM FULL which locks the entire time.

How much downtime does pg_repack cause?

Near zero. There is a brief lock during the final swap, typically under a second, even for large tables.

Can I run pg_repack from inside the database?

No. The pg_repack command-line tool must be run from an external machine with access to the database. It cannot be triggered from SQL.

Does pg_repack work on indexes?

Yes. By default, it repacks both tables and their indexes. You can also target indexes specifically using the --index flag.

Is pg_repack safe for production?

Yes, when used correctly. It’s designed for production use and includes rollback mechanisms if interrupted.