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 MVCC (i will tell you more details below) - multi-version of lines. When you update or delete data, older versions of strings do not disappear immediately. They remain in the table files as "dead cargo". Over time, such rows become numerous, tables inflate, indices lose their effectiveness, and queries begin to work more slowly.
To keep the base in a healthy state, PostgreSQL uses a process VACUUM. It removes unused versions of strings, frees up space, and helps update statistics for the query scheduler. This is a kind of cleaning that almost does not need to be started manually, because automatic autovacuum usually works.
There is also VACUUM FULL - more radical version. It not only cleans the "garbage", but also physically rearranges the table, returning space to the file system. Price — full table lock for operation time. Therefore, FULL is rarely and mainly used in night windows or after mass removals.
Without VACUUM, PostgreSQL gradually degrades: tables grow, indexes lose accuracy, queries become slow, and autovacuum may start working too aggressively. Therefore, VACUUM is not just a technical tool, but a basic mechanism that allows PostgreSQL to maintain performance for many years.

MVCC (Multi-Version Concurrency Control) 

MVCC (Multi-Version Concurrency Control) this is the way PostgreSQL and other DBMSs allow many clients to read and modify data simultaneously without work-paralyzing locks.
In conventional non-MVCC databases, simultaneous writing and reading can create conflicts. MVCC decides this as follows: when you update a string, the base does not rewrite it on top of the old one, but creates a new version. The old one remains available for those transactions that still "see" it. Each transaction works with its own snapshot of data without disturbing the others.
The advantage is simple - readers do not block the recording, and the recording does not block readers. That is why PostgreSQL can handle a large number of parallel operations without significant delays.
The drawback is also obvious - old versions accumulate. And it is VACUUM that is responsible for getting these "dead" strings to be removed and keeping the base clean.

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

The variables in Ruby are @, @@ and class instance variable
30 Oct 20:54

The variables in Ruby are @, @@ and class instance variable

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

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

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

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

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

What is 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 B-Tree (Balanced Tree)?
22 Nov 12:58

What is B-Tree (Balanced Tree)?

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