VACUUM (Reclaim Table Space)

Last updated: 2026-11-04

VACUUM (Reclaim Table Space)

VACUUM reclaims the space held by dead tuples left behind by UPDATE and DELETE, and refreshes the table's visibility map so index scans get faster. Under PostgreSQL's MVCC, old row versions are not removed immediately; autovacuum cleans up in the background, and you can also run VACUUM by hand after a large delete or update.

Kindcommand
Example LanguageSQL

📝 Syntax

VACUUM [ ( <option> [, ...] ) ] [TABLE] <table> [, ...]

-- common options
-- FULL       rewrite the table and return space to the OS
-- ANALYZE    also update statistics
-- VERBOSE    print per-table progress
-- FREEZE     freeze sufficiently old tuples

⚙️ Parameters

FULLRewrites the table and returns space to the OS; costly and locks
ANALYZEAlso refreshes statistics
VERBOSEPrints per-table progress
FREEZEFreezes sufficiently old tuples for long-term archiving

Returns:No return value. After it finishes, the space held by dead tuples is marked reusable.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

VACUUM

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhy does VACUUM report cannot run inside a transaction block?
AVACUUM has to run outside a transaction: it cannot sit inside a BEGIN/COMMIT block or inside a stored function. Commit it as a standalone statement.
QDoes VACUUM lock the table?
AA plain VACUUM takes only SHARE UPDATE EXCLUSIVE, which does not block normal reads and writes; VACUUM FULL rewrites the table and blocks all access.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏