EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Vacuum
 

PostgreSQL Vacuum

Priya Pedamkar
Article byPriya Pedamkar

Updated May 11, 2023

PostgreSQL Vacuum

 

 

Introduction to PostgreSQL Vacuum

In PostgreSQL, whenever we perform a delete operation or update the records that lead to obsolete dead tuple formation, then in reality, those records are not physically deleted and are still present in the memory and consume the space required by them. PostgreSQL reclaims storage space used by dead tuples only after performing VACUUM. VACUUM removes these tuples, freeing up space for other uses. It is recommended to execute VACUUM frequently, especially on tables that are frequently used and manipulated and where most of the operations occur.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

If you perform a vacuum operation without specifying any parameters, the database will vacuum all tables if you are a superuser or only the tables that you have permission to vacuum if you are not a superuser. However, if you specify a table name as a parameter, only that table will be vacuumed.The vacuum is not present in SQL.

Along with vacuuming the database, it is common to also perform an analyze operation. Maintaining statistics requires analyzing the database and storing the results in the pg_statistic table.These statistics are essential for determining the most efficient execution plans for optimal performance. If you want to perform an analysis on specific columns, you can specify a list of columns to be analyzed. When you use the ‘analyze’ command with a list of columns as a parameter, only statistics for those columns will be collected.

Syntax and Parameters

Below are the syntax and parameters of PostgreSQL VACUUM:

Syntax:

VACUUM [({FULL | FREEZE | VERBOSE | ANALYZE}[, ...])] [tablename [(columnname [,...])]]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ tablename ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ tablename [ (columnname [, ...] ) ] ]

Parameters:

Other parameters are specified in the vacuum method.

  • Tablename: It is an optional parameter. It is the name of the table on which you want to perform a vacuum or analyze. The default value is all tables in the current database.
  • Columnname: These are the names of columns for which you can perform vacuum and analyze and are optional. When no specific column name is specified, the default value analyzes all the columns of a table.
  • FULL: This is the optional parameter which, when not specified, the vacuum done frees up space for some other users in the same table. A VACUUM operation allows reading and writing operations to continue on the table being vacuumed and does not impose any locks on the table. Nevertheless, the VACUUM operation does not immediately return the reclaimed space to the operating system.
    When performing a FULL VACUUM operation on a table, the entire table becomes locked, which prevents any operations from being performed until the vacuum operation is complete. During a FULL VACUUM operation, the system rewrites the entire directory or file containing the table data to a new location, preserving only the saved changes. During a FULL VACUUM operation, the database permanently deletes dead tuples, resulting in more available space for the operating system compared to a non-full vacuum operation. Executing the analyze operation can be slow, and you should only perform it when there are no active operations being executed on the database. Scheduling vacuum operations at midnight when users are not accessing the system is a common recommendation. However, performing vacuum operations with the FULL option involves a high volume of I/O operations.So the user may find the application performance slower and cause problems while using the application. Therefore, it is not recommended to perform a FULL vacuum operation as a daily routine. In some special cases, such as when the operating system needs additional memory, performing a full vacuum can be beneficial because it reduces both the memory usage of the database and the memory occupied by frequently used or manipulated tables.
  • FREEZE: When the “freeze” option is specified, it aggressively freezes tuples in the table, which is equivalent in functionality to setting “vacuum_freeze_min_age” to zero.
  • VERBOSE: The VERBOSE option can be used with the vacuum command to print information about the tables and vacuum-related details for all the tables for which the operation is being performed. If the VERBOSE option is not specified, the VACUUM operation only prints the names of the tables where it is being executed.
  • ANALYZE: Updating the statistics in pg_statistic is necessary to build and use the most efficient execution strategies.

Examples of PostgreSQL Vacuum

Let us login using my Postgres user and then open my psql command-prompt using the following commands –

sudo su – postgres

Enter the password of the superuser if you are logging in using the superuser. In my case, Postgres is my superuser.

psql

Enter the password if set by you while accessing the psql.

The output will be somewhat like the following if your default database is Postgres.

default database Postgres

Let us see all the tables present in my postgres database using \dt command that results in the following output –

\dt

postgres database using \dt command

Now, suppose we want to perform a vacuum on the educba table and print all the vacuum operation related details in the report format then we will use the following command-prompt

VACUUM VERBOSE educba;

PostgreSQL Vacuum-1.3

The code did not find a row that contains a dead tuple.As a result, the system displays the message “There were 0 unused item identifiers” along with other details, indicating that there are no dead rows.

Now, let us check the contents of the table educba using the following query statement –

select * from educba;

PostgreSQL Vacuum-1.4

Now, we will delete the row with course_duration as 30 and technology_name as javascript using the following query statement –

delete from educba where course_duration=30;

PostgreSQL Vacuum-1.5

As you can see, the statement successfully deleted one row. However, the deleted row remains in the physical memory until a vacuum operation is performed. Performing a VACUUM operation on the “educba” table will remove the tuple with “javascript” as the “technology_name” from the physical memory of the database, as it is dead and no longer in use. This will help to reclaim storage space that was previously occupied by this tuple. Performing the following query now results in the following output –

VACUUM VERBOSE educba;

PostgreSQL Vacuum-1.6

Now, it says it removed 1-row versions in 1 page that stands for dead tuple that we deleted. The VACUUM operation also displays other information related to its execution.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Vacuum” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Variables
  2. Like PostgreSQL
  3. PostgreSQL round
  4. PostgreSQL Inner Join
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW