Site icon Database Tutorials

PostGre SQL Database Maintenance

Backup and Restore

  1. Plain-Text Format. A plain-text script file containing SQL statements and commands that can be executed by the psql command line terminal program to recreate the database objects and load the table data. Use the psql program to restore from a plain-text backup file.
  2. Custom Archive Format. A binary file that allows for restoration of all or only selected database objects from the backup file. Use pgAdmin to restore from a custom archive backup file.
  3. Tar Archive Format. A tar archive file that allows for restoration of all or only selected  database objects from the backup file. Use pgAdmin to restore from a tar archive backup file.

Re-Indexing

Rebuild a single index:

REINDEX INDEX my_index;

Rebuild all the indexes on the table my_table:

REINDEX TABLE my_table;

Vacuum

  1. VACUUM reclaims storage occupied by dead tuples. In normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a VACUUM is done. Therefore it’s necessary to do VACUUM periodically, especially on frequently-updated tables.
  2. With no parameter, VACUUM processes every table in the current database that the current user has permission to vacuum. With a parameter, VACUUM processes only that table.
  3. VACUUM ANALYZE performs a VACUUM and then an ANALYZE for each selected table. This is a handy combination form for routine maintenance scripts.
  4. Plain VACUUM (without FULL) simply reclaims space and makes it available for re-use. This form of the command can operate in parallel
    with normal reading and writing of the table, as an exclusive lock is not obtained. However, extra space is not returned to the operating
    system (in most cases); it’s just kept available for re-use within the same table. VACUUM FULL rewrites the entire contents of the table
    into a new disk file with no extra space, allowing unused space to be returned to the operating system. This form is much slower and
    requires an exclusive lock on each table while it is being processed.
  5. To recover or reuse disk space occupied by updated or deleted rows.
  6. To update data statistics used by the PostgreSQL query planner.
  7. To protect against loss of very old data due to transaction ID wraparound.
  8. Each of these reasons dictates performing VACUUM operations of varying frequency and scope, as explained in the following subsections.
  9. There are two variants of VACUUM:Standard VACUUM and VACUUM FULL.
  1. VACUUM FULL can reclaim more disk space but runs much more slowly. Also, the standard form of VACUUM can run in parallel with production
    database operations. (Commands such as SELECT, INSERT, UPDATE, and DELETE will continue to function normally, though you will not be able to modify the definition of a table with commands such as ALTER TABLE while it is being vacuumed.)
  2. VACUUM FULLrequires exclusive lock on the table it
    is working on, and therefore cannot be done in parallel with
    other use of the table. Generally, therefore, administrators
    should strive to use standard VACUUM and avoid VACUUM FULL.

Exit mobile version