RDS for PostgreSQL Table Bloat Remediation and Space Reclamation
PostgreSQL uses multi-version concurrency control (MVCC). When UPDATE or DELETE operations are executed, the old versions of modified rows (known as dead tuples) are not physically purged immediately. Instead, they are retained in data pages to guarantee read consistency for concurrent transactions.
Over time, these dead tuples accumulate, causing table and index bloat. Severe bloat triggers several performance and operational bottlenecks:
- Storage waste: Excessive cloud disk space is occupied.
- Performance degradation: Full table scans read more pages, reducing cache hit rates.
- Higher maintenance cost: The backup and recovery time is prolonged.
This section introduces three mainstream space reclamation mechanisms (autovacuum, VACUUM FULL, and pg_repack) and provides selection guidelines for production environments.
How It Works
1. autovacuum: automatic space reuse (logical reclamation)
- Working principle: A background daemon periodically scans tables, identifies dead tuples, and marks them as reusable space. This reclaimed space is registered in the free space map (FSM) for future INSERT or UPDATE reuse.
- Space: This mechanism achieves only logical space reclamation and does not shrink the physical file size. The table file size remains unchanged at the OS level but internal fragments are reused.
- Locking and performance: It does not block reads or writes and consumes minimal resources.
- Ideal scenario: This mechanism is ideal for routine automatic maintenance and serves as the baseline defense against uncontrolled table bloat.
2. VACUUM FULL: physical space reclamation (blocking)
- Working principle: This mechanism rewrites the entire table by copying valid rows into new compact pages. Once the rewrite is complete, it discards the original table.
- Space: It achieves absolute physical space reclamation. Table files shrink to the minimum size required for valid data. The released space is returned to the OS.
- Locking and performance: An ACCESS EXCLUSIVE lock is required on the target table and reads and writes for that table are completely blocked. The process requires temporary disk space of about twice the size of the table.
- Ideal scenario: This mechanism is used within downtime maintenance windows or when a large amount of disk space must be reclaimed and online tools cannot be used.
3. pg_repack: online, lossless space reclamation (recommended for production environments)
- Working principle: This mechanism creates a new table to contain valid data, synchronizes incremental changes using triggers or logs, and then atomically swaps the metadata of the new and old tables.
- Space: It achieves physical space reclamation. Data pages are reorganized and excess space is released to the OS.
- Locking and performance: No reads or writes are blocked. Only a brief, transient lock (milliseconds) is required during the final metadata swap. The process requires additional temporary space approximately equal to the size of the target table.
- Ideal scenario: This mechanism is the preferred space reclamation solution for production environments requiring high availability.
Comparison of Space Reclamation Mechanisms
| Feature Dimension | autovacuum | VACUUM FULL | pg_repack |
|---|---|---|---|
| Reclamation type | Logical reuse (not returned to the OS) | Physical reclamation (returned to the OS) | Physical reclamation (returned to the OS) |
| Blocking impact | No lock; non-blocking | ACCESS EXCLUSIVE; fully blocking | Only a brief lock at the end (µs–ms) |
| Disk required | Very low | High (about 2x the table size) | Medium (about 1x the table size) |
| Index handling | Only dead tuples are cleaned up. | Indexes are rebuilt to eliminate index bloat. | Indexes are reorganized online. |
| Ideal scenarios | Routine automatic maintenance | Downtime maintenance window/Offline database | Online maintenance for production environments |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot