SQL Keywords

SQL VACUUM

What does the SQL VACUUM command do?

VACUUM reclaims unused disk space and updates table statistics to keep the database efficient.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL VACUUM: PostgreSQL (full support), Amazon Redshift (subset), SQLite (file rebuild variant), Greenplum (based on PostgreSQL). Not in MySQL, SQL Server, or Oracle.

SQL VACUUM Full Explanation

VACUUM is a maintenance command that scans heap tables, removes dead tuples left by UPDATE and DELETE operations, and marks the freed pages as available for reuse. In PostgreSQL it can also update visibility maps, freeze transaction IDs to prevent wraparound, and optionally gather statistics (ANALYZE) so the query planner stays accurate. A standard VACUUM runs in the background with minimal locks, while VACUUM FULL rewrites the entire table, fully compacting it but requiring an exclusive lock. AUTOVACUUM generally handles routine cleanup, yet manual VACUUM remains essential after bulk loads, mass deletes, disabling autovacuum, or before creating critical backups. SQLite supports a different but related VACUUM that rebuilds the entire database file to defragment and shrink it.

SQL VACUUM Syntax

-- Basic
VACUUM;

-- Vacuum a specific table
VACUUM [FULL] [FREEZE] [VERBOSE] [ANALYZE]
       [table_name];

-- PostgreSQL 12+ option syntax
VACUUM [ ( option [, ...] ) ] [table_name [ ( column [, ...] ) ] ];

SQL VACUUM Parameters

  • • FULL (keyword) - Perform a full table rewrite, reclaiming maximum space but taking an exclusive lock.
  • • FREEZE (keyword) - Aggressively freeze tuples to avoid transaction ID wraparound.
  • • VERBOSE (keyword) - Output detailed progress messages.
  • • ANALYZE (keyword) - Run ANALYZE on the table after vacuuming.
  • • table_name (identifier) - Optional table (and column list) to target; omit for all tables.
  • - INDEX_CLEANUP { ON - OFF }
  • - PARALLEL integer - Number of parallel workers.
  • - SKIP_LOCKED - Skip tables that cannot be locked immediately.

Example Queries Using SQL VACUUM

-- Reclaim space across the entire database
VACUUM;

-- Show progress while vacuuming every table
VACUUM VERBOSE;

-- Compact a single table and freeze its tuples
VACUUM FULL FREEZE public.orders;

-- Vacuum and analyze with parallel workers
VACUUM (ANALYZE, PARALLEL 4) users;

Expected Output Using SQL VACUUM

  • Dead rows are removed, page space is marked reusable, visibility maps are updated, and (if ANALYZE) planner statistics are refreshed
  • VACUUM returns an OK message and, with VERBOSE, a per-table summary of pages scanned, tuples removed, and CPU time

Use Cases with SQL VACUUM

  • After bulk DELETE or TRUNCATE operations to immediately free disk space.
  • Before major version upgrades or pg_dump to minimize backup size.
  • Prevent transaction ID wraparound on very busy systems via FREEZE.
  • Improve query performance by updating statistics when autovacuum is disabled or lagging.
  • In SQLite, shrink the .db file after massive data churn.

Common Mistakes with SQL VACUUM

  • Assuming autovacuum is always enough - heavy workloads may still need manual runs.
  • Using VACUUM FULL on large production tables during peak hours, causing long locks.
  • Forgetting to ANALYZE after disabling autovacuum, leading to poor planner choices.
  • Confusing PostgreSQL VACUUM with SQLite VACUUM; the options differ.

Related Topics

ANALYZE, AUTOVACUUM, REINDEX, CLUSTER, CHECKPOINT

First Introduced In

PostgreSQL 6.5 (1999); SQLite 1.0 (2000)

Frequently Asked Questions

Why do I need to run VACUUM if autovacuum exists?

Autovacuum works in the background but may lag after mass deletes or when disabled. A manual VACUUM ensures space is reclaimed immediately.

What is the difference between VACUUM and VACUUM FULL?

VACUUM cleans pages in place with minimal locking. VACUUM FULL rewrites the table, fully reclaiming space but blocking access.

Does VACUUM affect query performance?

Yes. By removing bloat and updating statistics, VACUUM can improve index efficiency and planner accuracy, leading to faster queries.

Can I VACUUM specific columns?

PostgreSQL allows a column list only when ANALYZE is requested, letting you refresh statistics for selected columns without scanning the entire table.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!