Reduce the storage occupied by the tables of an ApsaraDB RDS for PostgreSQL instance
As UPDATE and DELETE operations accumulate dead row versions in your tables, tablespace fragments build up and the physical size of those tables grows. The autovacuum feature reclaims space within individual pages but cannot reduce the total page count — so disk usage stays the same or keeps increasing. Left unresolved, table bloat raises storage costs and slows down reads, writes, and sequential scans.
To reclaim disk space, use one of these three methods: VACUUM FULL, pg_repack, or pg_squeeze.
Choose a method
Each method makes a different trade-off between simplicity and availability impact.
| Method | Benefit | Limitation | Best for |
|---|---|---|---|
| VACUUM FULL | No prerequisites — no extensions or client tools required | Holds an exclusive lock for the entire operation; the table is inaccessible until it completes | Tables that can tolerate downtime during maintenance |
| pg_repack | Allows reads and writes (INSERT, UPDATE, DELETE, SELECT) while the table is being rebuilt | Requires a primary key or unique key; requires the pg_repack extension and a pg_repack client installed locally | Tables that are frequently written to |
| pg_squeeze | Allows reads and writes while the table is being rebuilt; no client tool required | Requires a primary key or unique key; may fail to complete if incremental data is written continuously at a high rate | Tables with low or moderate write activity |
When to choose: If a maintenance window is acceptable, VACUUM FULL is the simplest option. For tables in production environments where the table must stay online, use pg_repack or pg_squeeze.
Verify table size before and after
Before running any method, record the current table size. Run the same query after the operation to confirm the reduction.
-- Check table size
SELECT pg_size_pretty(pg_table_size('<schema>.<table_name>'));Replace <schema>.<table_name> with the actual schema and table name, for example public.orders.
VACUUM FULL
VACUUM FULL rebuilds a table by creating a complete replica and then dropping the original. It requires no extensions or client tools, but holds an exclusive lock on the table for the entire duration — blocking all reads and writes until it finishes.
Run the following command in the database:
VACUUM FULL <table_name>;pg_repack
pg_repack rebuilds a table without holding a long-term exclusive lock. Reads and writes continue normally during the rebuild.
How it works
Creates a new table as a snapshot copy of the original.
Adds a trigger to the original table to capture all changes (INSERT, UPDATE, DELETE) into a temporary table.
Applies the captured changes to the new table until the temporary table is empty, then swaps the tables.
Because pg_repack uses triggers and a temporary table to track changes, write throughput is reduced to approximately 10% of normal during the operation. This ensures all incremental data is captured and applied, making pg_repack reliable even under continuous write load.
Prerequisites
Before you begin, make sure you have:
A table with a primary key or unique key
The pg_repack extension enabled in the database
The pg_repack client installed on your local machine
For step-by-step instructions, see Use the pg_repack extension to clear tablespaces.
pg_squeeze
pg_squeeze works similarly to pg_repack — it rebuilds the table while allowing reads and writes — but uses logical replication instead of triggers to track incremental changes. This means no client tool is needed; install the pg_squeeze extension directly in the database. pg_squeeze also supports automatic detection and scheduling of cleanup operations.
How it works
Creates a new table as a snapshot copy of the original.
Uses logical replication to stream incremental changes into PostgreSQL's internal data structures.
Applies the streamed changes to the new table, then swaps the tables.
Because logical replication is used instead of triggers, write throughput is reduced to approximately 50% of normal during the operation. However, if incremental data is generated faster than pg_squeeze can apply it, the operation may fail to complete.
Prerequisites
Before you begin, make sure you have:
A table with a primary key or unique key
The pg_squeeze extension installed in the database
For step-by-step instructions, see Use the pg_squeeze extension to shrink bloated tables and indexes.
Performance considerations
The following compares pg_repack and pg_squeeze under different write conditions during a cleanup operation.
| Write activity during cleanup | pg_repack | pg_squeeze |
|---|---|---|
| No incremental writes | Similar performance | Similar performance |
| Steady incremental writes | Write throughput reduced to ~10%; operation completes reliably | Write throughput reduced to ~50%; delivers better overall performance than pg_repack |
| High-volume continuous writes | Write throughput reduced to ~10%; operation completes reliably | May fail to complete — incremental data accumulates faster than it can be applied |
Use pg_repack for tables that receive frequent writes. Use pg_squeeze for tables with low to moderate write activity.