What is Vacuum On PostgreSQL

 

When a delete operation is performed in Postgres, the deleted data is not deleted directly from the disk. The visibility of the rows disappears.

Vacuum puts a pointer to the visibility mapping. This pointer shows the block which the data was deleted from which block.

It then actually deletes the rows in the visibility mapping.

In order to use these blank spaces(it comes from deleted rows) again, it writes these blank space in free space mapping (fsm).

When a space is needed in the database, the space in the free space mapping are used.

If we run the vacuum with full parameters, the empty spaces are shrinked.

If you want to see how the vacuum works, you can disable the auto vacuum in the table with the following commands and insert it to see the table size.

 

With this command we are doing the vacuum operation for the table.

 

With this command We update the statistics on the table

 

Later, with the command pg_size_pretty again, when we check the size of the table we will see that the table has become smaller.