EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Vacuum
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL Vacuum

By Priya PedamkarPriya Pedamkar

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, that records are not physically deleted and are still present in the memory and consume the space required by them. These tuples are removed, and space is available for another usage only after VACUUM is done in PostgreSQL that reclaims the storage space occupied by dead tuples. Hence, it is required that you perform the VACUUM frequently, especially on the most often used and manipulated tables on which most of the operations are performed.

If we perform a vacuum without specifying any parameter, then all the tables present in that database are vacuumed if the user is a superuser or only those tables that the current user is permitted to perform vacuum on it. If the parameter containing the table name is specified, then only that particular table is vacuumed. The vacuum is not present in SQL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The vacuum is often accompanied by analyzing that helps in maintaining statistics about the database that is further stored in the pg_statistic table. These statistics are used for finding out the execution plans that are most efficient for performance. Analyze can be done only for specific columns if you want and can specify the list of the columns in which you want to perform the analysis. Statistics of only those columns are collected that is specified in analyze’s parameter.

Syntax and Parameters

Syntax and parameters of postgresql vacuum are given below:

Syntax:

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

Parameters:

There are other parameters that 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: This is the names of columns for which you can perform vacuum and analyze and are optional in nature. The default value evaluates all the table columns to be analyzed when no particular column name is specified.
  • FULL: This is the optional parameter which, when not specified, the vacuum done frees up space for some other users in the same table. It does not block reading and writing operations on the table on which vacuum is being performed, and tables are not locked. The operating system is not returned with an empty space.
    However, when we mention FULL and perform full vacuum at that time, the tables on which we are performing vacuum are completely locked, and no operations are allowed on them. The whole directory or file is written to a new location with only saved changes and permanently deleting the dead tuples; the operating system is returned with extra space as compared to a non-full vacuum. But this process is slow in execution and needs to be done only when no operations are being performed on the database, usually at midnight when users do not access the system.
    Performing vacuum operation with FULL involves too many I/O operations being performed. So the user may find the performance of the application slower and cause problems while using the application. Hence, it is not recommended to perform FULL vacuum operation in a daily routine. However, in some special cases, like when the operating system requires more memory, then the full vacuum can be done as it shrinks the memory usage and memory occupied by the database and some frequently used/manipulated tables.
  • FREEZE: Whenever we specify the freeze option, the tuples are aggressively frozen, and this is the same in functionality as specifying the vacuum_freeze_min_age to zero.
  • VERBOSE: Information about the tables and vacuum related details for all the tables for which operation is being performed is printed on the console if the VERBOSE option is used. In another case, only the names of the table on which operation is being done are printed.
  • ANALYZE: It updates the statistics in pg_statistic that are required and used for building 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.

PostgreSQL Vacuum-1.1

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

\dt

PostgreSQL Vacuum-1.2

Now, suppose we want to perform vacuum on 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

No row was found containing a dead tuple. Hence 0 dead rows were there, and There was 0 unused item identifiers message gets printed along with other details.

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 can be seen, one row was deleted. This deleted row is not deleted from the physical memory unless we perform a vacuum operation on it. If we perform vacuum operation on the educba table, then the tuple with javascript as technology_name is an unused and dead tuple. Hence, that will be cleaned and removed from the physical memory of the database. Performing the following query now results in the following output –

VACUUM VERBOSE educba;

PostgreSQL Vacuum-1.6

Now, it says removed 1-row versions in 1 page that stands for dead tuple that we deleted. Other information related to the performed vacuum operation also gets displayed.

Recommended Articles

This is a guide to PostgreSQL Vacuum. Here we also discuss the introduction and Syntax, and parameters of postgresql vacuum along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. PostgreSQL Variables
  2. PostgreSQL Like
  3. PostgreSQL round
  4. PostgreSQL Inner Join
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more