All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is VACUUM in PostgreSQL?

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
PostgreSQL has a mechanism called MVCC (I will explain in more detail below) - multi-version concurrency control. When you update or delete data, old versions of rows do not disappear immediately. They remain in the table files as "dead weight." Over time, these rows accumulate, tables bloat, indexes lose efficiency, and queries start to run slower.
To keep the database in a healthy state, PostgreSQL uses a process called VACUUM. It removes unused versions of rows, frees up space, and helps update statistics for the query planner. This is a kind of cleanup that usually does not need to be run manually, as the automatic autovacuum typically takes care of it.
There is also VACUUM FULL - a more radical version. It not only cleans up the "garbage" but also physically rebuilds the table, returning space to the file system. The cost is a complete lock on the table during the operation. Therefore, FULL is used rarely and mainly during night windows or after mass deletions.
Without VACUUM, PostgreSQL gradually degrades: tables grow, indexes lose accuracy, queries become slow, and autovacuum may start to operate too aggressively. Thus, VACUUM is not just a technical tool but a fundamental mechanism that allows PostgreSQL to maintain performance for many years.

MVCC (Multi-Version Concurrency Control) 

MVCC (Multi-Version Concurrency Control) is the way PostgreSQL and other DBMS allow many clients to read and modify data simultaneously without blocking that paralyzes operations.
In regular databases without MVCC, simultaneous writes and reads can create conflicts. MVCC resolves this by: when you update a row, the database does not overwrite it on top of the old one but creates a new version. The old one remains accessible to those transactions that still "see" it. Each transaction works with its own snapshot of the data, without interfering with others.
The advantage is simple - readers do not block writes, and writes do not block readers. This is why PostgreSQL can handle a large number of parallel operations without significant delays.
The downside is also obvious - old versions accumulate. And it is VACUUM that is responsible for removing these "dead" rows and keeping the database in a clean state.

This post doesn't have any additions from the author yet.

Variables in Ruby: @, @@ and class instance variable
30 Oct 20:54

Variables in Ruby: @, @@ and class instance variable

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
The difference between blank?, present?, empty? and nil? in Ruby
30 Oct 21:06

The difference between blank?, present?, empty? and nil? in Ruby

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Middleware in Ruby on Rails and when is it used
04 Nov 10:39

What is Middleware in Ruby on Rails and when is it used

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is the Vanilla Rails approach?
14 Nov 16:48

What is the Vanilla Rails approach?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Elasticsearch and how does it work?
22 Nov 12:35

What is Elasticsearch and how does it work?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is a time-series database?
22 Nov 12:42

What is a time-series database?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is a B-Tree (Balanced Tree)?
22 Nov 12:58

What is a B-Tree (Balanced Tree)?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Fix for the issue of installing Ruby 3.4.3 (and not only) via RVM on macOS (Apple Silicon)
30 Dec 14:05

Fix for the issue of installing Ruby 3.4.3 (and not only) via RVM on macOS (Apple Silicon)

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Thundering Herd Problem: what it is and why it breaks production
15 Jan 10:14

Thundering Herd Problem: what it is and why it breaks production

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Exponential Backoff and Random Jitter?
15 Jan 15:24

What is Exponential Backoff and Random Jitter?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Connecting the Elasticsearch service to a Rails application (Coolify in the cloud, server on Hetzner).
15 Feb 13:45

Connecting the Elasticsearch service to a Rails application (Coolify in the cloud, server on Hetzner).

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
"No space left on device" - when Docker has consumed the entire disk
15 Feb 19:57

"No space left on device" - when Docker has consumed the entire disk

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska