| Categories: | PostgreSQL® |
|---|---|
| Tags: | PostgreSQL® |
VACUUM in PostgreSQL® has always been surrounded by myths and misinformation. A particularly common belief seems to be that VACUUM FULL helps as a preventive measure. In many cases, the exact opposite is true.
Since the introduction of MVCC (Multi Version Concurrency Control) in PostgreSQL® 6.5 in 1999, the VACUUM command has existed. This command defragments the so-called heap, i.e., the files that contain the table data, and frees up unused disk space. This is necessary because PostgreSQL® does not physically delete rows on UPDATE or DELETE; instead, it creates a new version of the row or simply marks the row as deleted. The old version must be retained as long as there are transactions that can still “see” that row version. If a table is heavily used for UPDATE or DELETE/INSERT, and VACUUM runs too infrequently (for example, because autovacuum is not used), the so-called “dead” space in a table can grow significantly.
Many administrators therefore believe that, for this reason, it is appropriate to prevent table growth in advance by running nightly VACUUM FULL jobs. This is a poor strategy for several reasons:
While most disadvantages can be avoided with a maintenance window, the drawbacks of running VACUUM FULL very frequently are more serious. PostgreSQL® versions up to and including 8.4 are particularly affected. To understand this, you need to look at how the VACUUM FULL command works in these versions:
The main problem is reordering rows into the freed space. This causes massive I/O on the storage system. Even more serious, however, is the fact that the index must also be updated during reordering. If this happens very frequently, the index itself can become heavily fragmented. In that case, the index grows as well—this is known as index bloat. It may therefore be necessary to run a REINDEX on the tables directly after VACUUM FULL, especially if tables were heavily fragmented and many tuples were reordered. For very large tables, all of this also results in very long runtimes. Starting with PostgreSQL® 9.0, VACUUM FULL behaves like the CLUSTER command, i.e., the table is read sequentially and rebuilt completely in parallel. The advantage is that only active rows are read, while “dead” rows are left out. The indexes are then recreated. This eliminates many disadvantages of the old algorithm, but it does not remove the need for exclusive table locks. In addition, in the worst case, reorganizing the table requires as much additional disk space as the table currently being processed.
VACUUM and autovacuum are designed for daily or continuous maintenance of PostgreSQL® databases.
VACUUM FULL is a command that is not designed for daily maintenance. If the proverbial damage has already been done and a table is heavily bloated, then depending on the PostgreSQL® version, freeing disk space with VACUUM FULL is unavoidable. With older PostgreSQL® versions, administrators should consider using the CLUSTER command instead, especially when the table requires a great deal of disk space. If you still want to use VACUUM FULL, then on older PostgreSQL® versions you should also recreate the indexes with REINDEX. More information on this topic can be found in the PostgreSQL® Wiki.
All blog posts on PostgreSQL® are also available as the PostgreSQL® category with its own feed. We are also happy to help with support and services for PostgreSQL®.
This post was originally written by Bernd Helmle.
| Categories: | PostgreSQL® |
|---|---|
| Tags: | PostgreSQL® |
About the author
about the person
Dieser Account dient als Sammelpunkt für die wertvollen Beiträge ehemaliger Mitarbeiter von credativ. Wir bedanken uns für ihre großartigen Inhalte, die das technische Wissen in unserem Blog über die Jahre hinweg bereichert haben. Ihre Artikel bleiben hier weiterhin für unsere Leser zugänglich.
You need to load content from reCAPTCHA to submit the form. Please note that doing so will share data with third-party providers.
More InformationYou are currently viewing a placeholder content from Brevo. To access the actual content, click the button below. Please note that doing so will share data with third-party providers.
More InformationYou need to load content from reCAPTCHA to submit the form. Please note that doing so will share data with third-party providers.
More InformationYou need to load content from Turnstile to submit the form. Please note that doing so will share data with third-party providers.
More InformationYou need to load content from reCAPTCHA to submit the form. Please note that doing so will share data with third-party providers.
More InformationYou are currently viewing a placeholder content from Turnstile. To access the actual content, click the button below. Please note that doing so will share data with third-party providers.
More Information