More on PostgreSQL

(We marked visited pages blue)
  • How to find the size of PostgreSQL databases and tables
  • Step by Step PostgreSQL installation on Linux
  • Postgresql Where Clause
  • Postgresql View
  • Postgresql Vacuum Statement
  • Postgresql Upper Function
  • Postgresql Update Statement
  • Postgresql Unique Constraints
  • Postgresql Union Operator
  • Postgresql Union All Operator
  • Postgresql Truncate Table Statement
  • Postgresql Trunc Function
  • Postgresql Trim Function
  • Postgresql Translate Function
  • Postgresql To_number Function
  • Postgresql To_date Function
  • Postgresql To_char Function
  • Postgresql Sum Function
  • Postgresql Substring Function
  • Postgresql Subqueries
  • Postgresql Strpos Function
  • Postgresql Sqrt Function
  • Postgresql Sign Function
  • Postgresql Setseed Function
  • Postgresql Select Statement
  • Postgresql Select Limit Statement
  • Postgresql Rtrim Function
  • Postgresql Rpad Function
  • Postgresql Round Function
  • Postgresql Replace Function
  • Postgresql Repeat Function
  • Postgresql Rename A User
  • Postgresql Random Function
  • Postgresql Primary Keys
  • Postgresql Power Function
  • Postgresql Position Function
  • Postgresql Order By Clause
  • Postgresql Or Condition
  • Postgresql Now Function
  • Postgresql Not Condition
  • Postgresql Mod Function
  • Postgresql Min Function
  • Postgresql Max Function
  • Postgresql Ltrim Function
  • Postgresql Lpad Function
  • Postgresql Lower Function
  • Postgresql Localtimestamp Function
  • Postgresql Localtime Function
  • Postgresql Literals
  • Postgresql Like Condition
  • Postgresql Length Function
  • Postgresql Joins
  • Postgresql Is Null Condition
  • Postgresql Is Not Null
  • Postgresql Intersect Operator
  • Postgresql Insert Statement
  • Postgresql Initcap Function
  • Postgresql Indexes
  • Postgresql In Condition
  • Postgresql Having Clause
  • Postgresql Group By Clause
  • Postgresql Grant Revoke Privileges
  • Postgresql From Clause
  • Postgresql Floor Function
  • Postgresql Find Users In Postgresql
  • Postgresql Extract Function
  • Postgresql Exp Function
  • Postgresql Exists Condition
  • Postgresql Except Operator
  • Postgresql Drop User Statement
  • Postgresql Drop Table Statement
  • Postgresql Div Function
  • Postgresql Distinct Clause
  • Postgresql Delete Statement
  • Postgresql Declaring Variables
  • Postgresql Date_part Function
  • Postgresql Data Types
  • Postgresql Current_timestamp Function
  • Postgresql Current_time Function
  • Postgresql Current_date Function
  • Postgresql Create User Statement
  • Postgresql Create Table Statement
  • Postgresql Create Table As Statement
  • Postgresql Count Function
  • Postgresql Comparison Operators
  • Postgresql Comments Within Sql
  • Postgresql Combining The And And Or Conditions
  • Postgresql Character_length Function
  • Postgresql Char_length Function
  • Postgresql Change A User Password
  • Postgresql Ceiling Function
  • Postgresql Ceil Function
  • Postgresql Btrim Function
  • Postgresql Between Condition
  • Postgresql Avg Function
  • Postgresql Autovacuum Daemon
  • Postgresql And Condition
  • Postgresql Alter Table Statement
  • Postgresql Aliases
  • Postgresql Age Function
  • Postgresql Abs Function
  • How to Optimize PostgreSQL Performance with VACUUM, ANALYZE, and REINDEX

    If you have your application running on a PostgreSQL database, there are some commands that can be run to PostgreSQL database performance optimization. Three of these will be introduced in this article: VACUUM, ANALYZE, and REINDEX. In the default PostgreSQL configuration, the AUTOVACUUM daemon is enabled and all required configuration parameters are set as needed. The daemon will run VACUUM and ANALYZE at regular intervals. If you have the damon enabled, these commands can be run to supplement the daemon's work. To confirm whether the autovacuum daemon is running on UNIX, you can check the processlist

    $ ps aux|grep autovacuum|grep -v grep
    postgres           334   0.0  0.0  2654128   1232   ??  Ss   16Mar17   0:05.63 postgres: autovacuum launcher process  
    On UNIX or Windows, you can find the status of autovacuum in the pg_settings database with the query below:
    <code class=" language-sql">select name,setting from pg_settings  where name = 'autovacuum';


    The VACUUM command will reclaim space still used by data that had been updated. In PostgreSQL, updated key-value tuples are not removed from the tables when rows are changed, so the VACUUM command should be run occasionally to do this. VACUUM can be run on its own, or with ANALYZE.


    In the examples below, [tablename] is optional. Without a table specified, VACUUM will be run on available tables in the current schema that the user has access to.
    1. Plain VACUUM: Frees up space for re-use
      VACUUM [tablename]
    2. Full VACUUM: Locks the database table, and reclaims more space than a plain VACUUM
      <code class=" language-sql">VACUUM(FULL)[tablename]
    3. Full VACUUM and ANALYZE: Performs a Full VACUUM and gathers new statistics on query executions paths using ANALYZE
      <code class=" language-sql">VACUUM(FULL,ANALYZE)[tablename]
    4. Verbose Full VACUUM and ANALYZE: Same as #3, but with verbose progress output
      <code class=" language-sql">VACUUM(FULL,ANALYZE,VERBOSE)[tablename]


    ANALYZE gathers statistics for the query planner to create the most efficient query execution paths. Per PostgreSQL documentation, accurate statistics will help the planner to choose the most appropriate query plan, and thereby improve the speed of query processing.


    In the example below, [tablename] is optional. Without a table specified, ANALYZE will be run on available tables in the current schema that the user has access to.
    <code class=" language-sql">ANALYZE VERBOSE [tablename]


    The REINDEX command rebuilds one or more indices, replacing the previous version of the index. REINDEX can be used in many scenarios, including the following (from Postgres documentation):
    • An index has become corrupted, and no longer contains valid data. Although in theory this should never happen, in practice indexes can become corrupted due to software bugs or hardware failures. REINDEX provides a recovery method.
    • An index has become "bloated", that is it contains many empty or nearly-empty pages. This can occur with B-tree indexes in PostgreSQL under certain uncommon access patterns. REINDEX provides a way to reduce the space consumption of the index by writing a new version of the index without the dead pages.
    • You have altered a storage parameter (such as fillfactor) for an index, and wish to ensure that the change has taken full effect.
    • An index build with the CONCURRENTLY option failed, leaving an "invalid" index. Such indexes are useless but it can be convenient to use REINDEX to rebuild them. Note that REINDEX will not perform a concurrent build. To build the index without interfering with production you should drop the index and reissue the CREATE INDEX CONCURRENTLY command.


    Any of these can be forced by adding the keyword FORCE after the command
    1. Recreate a single index, myindex:
      <code class=" language-sql">REINDEX INDEX myindex
    2. Recreate all indices in a table, mytable:
      <code class=" language-sql">REINDEX TABLE mytable
    3. Recreate all indices in schema public:
      REINDEX SCHEMA public
    4. Recreate all indices in database postgres:
      <code class=" language-sql">REINDEX DATABASE postgres
    5. Recreate all indices on system catalogs in database postgres:
      <code class=" language-sql">REINDEX SYSTEM postgres

    Leave a comment or a question in the comment area bellow if you fell like

    Be a sport and


    Loading... Please wait

    Subscribe to our Newsletter

    Be a sport and