w3resource

Complete Guide to PostgreSQL VACUUM Command


Understanding the VACUUM Command in PostgreSQL

The VACUUM command in PostgreSQL is used to reclaim storage occupied by dead tuples and update table statistics. Dead tuples are created due to updates and deletions but are not immediately removed to maintain the MVCC (Multi-Version Concurrency Control) feature of PostgreSQL. Regular vacuuming ensures optimal database performance by cleaning up these unused tuples and preventing table bloat.


Syntax:

VACUUM [FULL] [FREEZE] [VERBOSE] [ANALYZE] table_name;
  • FULL: Performs a complete vacuum, reclaiming all available space. Locks the table.
  • FREEZE: Marks tuples as frozen to prevent transaction wraparound issues.
  • VERBOSE: Provides detailed output about the vacuuming process.
  • ANALYZE: Updates statistics for the query planner.
  • table_name: Specifies the table to vacuum. If omitted, all tables are vacuumed.

Examples

1. Basic Vacuum

Code:

-- Perform a basic vacuum operation on the entire database
VACUUM;

Explanation:

  • Cleans up dead tuples but does not lock tables or update statistics.

2. Vacuum with Full Cleanup

Code:

-- Perform a FULL vacuum on a specific table
VACUUM FULL employees;

Explanation:

  • Reclaims all storage for the employees table. This locks the table during the operation.

3. Vacuum with Verbose Output

Code:

-- Get detailed information during the vacuum process
VACUUM VERBOSE employees;

Explanation:

  • Displays vacuum progress and statistics, useful for debugging and monitoring.

4. Vacuum with Analyze

Code:

-- Reclaim storage and update statistics for better query planning
VACUUM ANALYZE orders;

Explanation:

  • Performs vacuum and updates statistics for the orders table, improving query performance.

Autovacuum in PostgreSQL

PostgreSQL includes an autovacuum process that runs automatically to handle routine vacuuming and analyze operations. The autovacuum process is controlled by the following parameters in the postgresql.conf file:

  • autovacuum: Enables or disables the autovacuum feature.
  • autovacuum_vacuum_threshold: Minimum number of tuples before vacuuming.
  • autovacuum_analyze_threshold: Minimum number of tuples before analyzing.
  • autovacuum_vacuum_cost_limit: Maximum cost for vacuum operations.

Example Configuration:

Code:

# Enable autovacuum
autovacuum = on

# Customize thresholds
autovacuum_vacuum_threshold = 50
autovacuum_analyze_threshold = 50

Vacuum Best Practices

    1. Schedule Regular Vacuuming: If autovacuum is disabled, schedule manual vacuum operations.

    2. Monitor Table Size: Use the pg_stat_user_tables view to track table bloat and vacuum frequency.

    3. Avoid Excessive FULL Vacuuming: Use VACUUM FULL sparingly as it locks tables and can impact performance.

    4. Analyze After Bulk Inserts or Updates: Update query planner statistics using VACUUM ANALYZE to optimize performance.

Real-World Use Case

Scenario: Handling Table Bloat in a High-Traffic Application

  • Problem: Frequent updates and deletions cause table bloat, slowing down queries.
  • Solution: Set up autovacuum with adjusted thresholds and perform VACUUM FULL during maintenance windows to reclaim storage.

Query Example:

Code:

-- Schedule a full vacuum during low traffic
VACUUM FULL products;

Monitoring Vacuum Operations

Check Autovacuum Activity

Code:

-- View autovacuum activity for all tables
SELECT relname, n_tup_ins, n_tup_upd, n_tup_del, last_vacuum, last_autovacuum
FROM pg_stat_user_tables;

Check Table Bloat

Use extensions like pgstattuple to analyze table bloat:

Code:

-- Install pgstattuple extension
CREATE EXTENSION pgstattuple;

-- Analyze bloat in a specific table
SELECT * FROM pgstattuple('employees');

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/snippets/postgresql-vacuum-command.php