<!-- $PostgreSQL: pgsql/doc/src/sgml/maintenance.sgml,v 1.95.2.1 2009/08/07 20:55:04 alvherre Exp $ -->

<chapter id="maintenance">
 <title>Routine Database Maintenance Tasks</title>

 <indexterm zone="maintenance">
  <primary>maintenance</primary>
 </indexterm>

 <indexterm zone="maintenance">
  <primary>routine maintenance</primary>
 </indexterm>

  <para>
   <productname>PostgreSQL</>, like any database software, requires that certain tasks
   be performed regularly to achieve optimum performance. The tasks
   discussed here are <emphasis>required</emphasis>, but they
   are repetitive in nature and can easily be automated using standard
   tools such as <application>cron</application> scripts or
   Windows' <application>Task Scheduler</>.  But it is the database
   administrator's responsibility to set up appropriate scripts, and to
   check that they execute successfully.
  </para>

  <para>
   One obvious maintenance task is creation of backup copies of the data on a
   regular schedule.  Without a recent backup, you have no chance of recovery
   after a catastrophe (disk failure, fire, mistakenly dropping a critical
   table, etc.).  The backup and recovery mechanisms available in
   <productname>PostgreSQL</productname> are discussed at length in
   <xref linkend="backup">.
  </para>

  <para>
   The other main category of maintenance task is periodic <quote>vacuuming</>
   of the database.  This activity is discussed in
   <xref linkend="routine-vacuuming">.  Closely related to this is updating
   the statistics that will be used by the query planner, as discussed in
   <xref linkend="vacuum-for-statistics">.
  </para>

  <para>
   Another task that might need periodic attention is log file management.
   This is discussed in <xref linkend="logfile-maintenance">.
  </para>

  <para>
   <ulink
   url="http://bucardo.org/check_postgres/"><application>check_postgres.pl</></ulink>
   is available for monitoring database health and reporting unusual
   conditions.  <application>check_postgres.pl</> integrates with
   Nagios and MRTG, but can be run standalone too.
  </para>

  <para>
   <productname>PostgreSQL</productname> is low-maintenance compared
   to some other database management systems.  Nonetheless,
   appropriate attention to these tasks will go far towards ensuring a
   pleasant and productive experience with the system.
  </para>

 <sect1 id="routine-vacuuming">
  <title>Routine Vacuuming</title>

  <indexterm zone="routine-vacuuming">
   <primary>vacuum</primary>
  </indexterm>

  <para>
   <productname>PostgreSQL</productname> databases require periodic
   maintenance known as <firstterm>vacuuming</>.  For many installations, it
   is sufficient to let vacuuming be performed by the <firstterm>autovacuum
   daemon</>, which is described in <xref linkend="autovacuum">.  You might
   need to adjust the autovacuuming parameters described there to obtain best
   results for your situation.  Some database administrators will want to
   supplement or replace the daemon's activities with manually-managed
   <command>VACUUM</> commands, which typically are executed according to a
   schedule by <application>cron</application> or <application>Task
   Scheduler</> scripts.  To set up manually-managed vacuuming properly,
   it is essential to understand the issues discussed in the next few
   subsections.  Administrators who rely on autovacuuming may still wish
   to skim this material to help them understand and adjust autovacuuming.
  </para>

  <sect2 id="vacuum-basics">
   <title>Vacuuming Basics</title>

   <para>
    <productname>PostgreSQL</productname>'s
    <xref linkend="sql-vacuum" endterm="sql-vacuum-title"> command has to
    process each table on a regular basis for several reasons:

    <orderedlist>
     <listitem>
      <simpara>To recover or reuse disk space occupied by updated or deleted
      rows.</simpara>
     </listitem>

     <listitem>
      <simpara>To update data statistics used by the
      <productname>PostgreSQL</productname> query planner.</simpara>
     </listitem>

     <listitem>
      <simpara>To protect against loss of very old data due to
      <firstterm>transaction ID wraparound</>.</simpara>
     </listitem>
    </orderedlist>

    Each of these reasons dictates performing <command>VACUUM</> operations
    of varying frequency and scope, as explained in the following subsections.
   </para>

   <para>
    There are two variants of <command>VACUUM</>: standard <command>VACUUM</>
    and <command>VACUUM FULL</>.  <command>VACUUM FULL</> can reclaim more
    disk space but runs much more slowly.  Also,
    the standard form of <command>VACUUM</> can run in parallel with production
    database operations.  (Commands such as <command>SELECT</command>,
    <command>INSERT</command>, <command>UPDATE</command>, and
    <command>DELETE</command> will continue to function as normal, though you
    will not be able to modify the definition of a table with commands such as
    <command>ALTER TABLE</command> while it is being vacuumed.)
    <command>VACUUM FULL</> requires exclusive lock on the table it is
    working on, and therefore cannot be done in parallel with other use
    of the table.  Another disadvantage of <command>VACUUM FULL</> is that
    while it reduces table size, it does not reduce index size proportionally;
    in fact it can make indexes <emphasis>larger</>.  Generally, therefore,
    administrators should strive to use standard <command>VACUUM</> and
    avoid <command>VACUUM FULL</>.
   </para>

   <para>
    <command>VACUUM</command> creates a substantial amount of I/O
    traffic, which can cause poor performance for other active sessions.
    There are configuration parameters that can be adjusted to reduce the
    performance impact of background vacuuming &mdash; see
    <xref linkend="runtime-config-resource-vacuum-cost">.
   </para>
  </sect2>

  <sect2 id="vacuum-for-space-recovery">
   <title>Recovering Disk Space</title>

   <indexterm zone="vacuum-for-space-recovery">
    <primary>disk space</primary>
   </indexterm>

   <para>
    In <productname>PostgreSQL</productname>, an
    <command>UPDATE</> or <command>DELETE</> of a row does not
    immediately remove the old version of the row.
    This approach is necessary to gain the benefits of multiversion
    concurrency control (see <xref linkend="mvcc">): the row version
    must not be deleted while it is still potentially visible to other
    transactions. But eventually, an outdated or deleted row version is no
    longer of interest to any transaction. The space it occupies must then be
    reclaimed for reuse by new rows, to avoid infinite growth of disk
    space requirements. This is done by running <command>VACUUM</>.
   </para>

   <para>
    The standard form of <command>VACUUM</command> removes dead row
    versions in tables and indexes and marks the space available for
    future reuse.  However, it will not return the space to the operating
    system, except in the special case where one or more pages at the
    end of a table become entirely free and an exclusive table lock can be
    easily obtained.  In contrast, <command>VACUUM FULL</> actively compacts
    tables by moving row versions to earlier pages.  It is thus able to
    force pages at the end of the table to become entirely free, whereupon
    it will return them to the operating system.  However, if many rows
    must be moved, this can take a long time.  Also, moving a row requires
    transiently making duplicate index entries for it (the entry pointing
    to its new location must be made before the old entry can be removed);
    so moving a lot of rows this way causes severe index bloat.
   </para>

   <para>
    The usual goal of routine vacuuming is to do standard <command>VACUUM</>s
    often enough to avoid needing <command>VACUUM FULL</>.  The
    autovacuum daemon attempts to work this way, and in fact will
    never issue <command>VACUUM FULL</>.  In this approach, the idea
    is not to keep tables at their minimum size, but to maintain steady-state
    usage of disk space: each table occupies space equivalent to its
    minimum size plus however much space gets used up between vacuumings.
    Although <command>VACUUM FULL</> can be used to shrink a table back
    to its minimum size and return the disk space to the operating system,
    there is not much point in this if the table will just grow again in the
    future.  Thus, moderately-frequent standard <command>VACUUM</> runs are a
    better approach than infrequent <command>VACUUM FULL</> runs for
    maintaining heavily-updated tables.
   </para>

   <para>
    Some administrators prefer to schedule vacuuming themselves, for example
    doing all the work at night when load is low.
    The difficulty with doing vacuuming according to a fixed schedule
    is that if a table has an unexpected spike in update activity, it may
    get bloated to the point that <command>VACUUM FULL</> is really necessary
    to reclaim space.  Using the autovacuum daemon alleviates this problem,
    since the daemon schedules vacuuming dynamically in response to update
    activity.  It is unwise to disable the daemon completely unless you
    have an extremely predictable workload.  One possible compromise is
    to set the daemon's parameters so that it will only react to unusually
    heavy update activity, thus keeping things from getting out of hand,
    while scheduled <command>VACUUM</>s are expected to do the bulk of the
    work when the load is typical.
   </para>

   <para>
    For those not using autovacuum, a typical approach is to schedule a
    database-wide <command>VACUUM</> once a day during a low-usage period,
    supplemented by more frequent vacuuming of heavily-updated tables as
    necessary. (Some installations with extremely high update rates vacuum
    their busiest tables as often as once every few minutes.) If you have
    multiple databases in a cluster, don't forget to
    <command>VACUUM</command> each one; the program <xref
    linkend="app-vacuumdb" endterm="app-vacuumdb-title"> might be helpful.
   </para>

   <tip>
   <para>
    Neither form of <command>VACUUM</> is entirely satisfactory when
    a table contains large numbers of dead row versions as a result of
    massive update or delete activity.  If you have such a table and
    you need to reclaim the excess disk space it occupies, the best
    way is to use <xref linkend="sql-cluster" endterm="sql-cluster-title">
    or one of the table-rewriting variants of
    <xref linkend="sql-altertable" endterm="sql-altertable-title">.
    These commands rewrite an entire new copy of the table and build
    new indexes for it.  Like <command>VACUUM FULL</>, they require
    exclusive lock.  Note that they also temporarily use extra disk
    space, since the old copies of the table and indexes can't be
    released until the new ones are complete.  In the worst case where
    your disk is nearly full, <command>VACUUM FULL</> may be the only
    workable alternative.
   </para>
   </tip>

   <tip>
   <para>
    If you have a table whose entire contents are deleted on a periodic
    basis, consider doing it with
    <xref linkend="sql-truncate" endterm="sql-truncate-title"> rather
    than using <command>DELETE</command> followed by
    <command>VACUUM</command>. <command>TRUNCATE</command> removes the
    entire content of the table immediately, without requiring a
    subsequent <command>VACUUM</command> or <command>VACUUM
    FULL</command> to reclaim the now-unused disk space.
    The disadvantage is that strict MVCC semantics are violated.
   </para>
   </tip>
  </sect2>

  <sect2 id="vacuum-for-statistics">
   <title id="vacuum-for-statistics-title">Updating Planner Statistics</title>

   <indexterm zone="vacuum-for-statistics">
    <primary>statistics</primary>
    <secondary>of the planner</secondary>
   </indexterm>

   <indexterm zone="vacuum-for-statistics">
    <primary>ANALYZE</primary>
   </indexterm>

   <para>
    The <productname>PostgreSQL</productname> query planner relies on
    statistical information about the contents of tables in order to
    generate good plans for queries.  These statistics are gathered by
    the <xref linkend="sql-analyze" endterm="sql-analyze-title"> command,
    which can be invoked by itself or
    as an optional step in <command>VACUUM</>.  It is important to have
    reasonably accurate statistics, otherwise poor choices of plans might
    degrade database performance.
   </para>

   <para>
    The autovacuum daemon, if enabled, will automatically issue
    <command>ANALYZE</> commands whenever the content of a table has
    changed sufficiently.  However, administrators might prefer to rely
    on manually-scheduled <command>ANALYZE</> operations, particularly
    if it is known that update activity on a table will not affect the
    statistics of <quote>interesting</> columns.  The daemon schedules
    <command>ANALYZE</> strictly as a function of the number of rows
    inserted or updated; it has no knowledge of whether that will lead
    to meaningful statistical changes.
   </para>

   <para>
    As with vacuuming for space recovery, frequent updates of statistics
    are more useful for heavily-updated tables than for seldom-updated
    ones. But even for a heavily-updated table, there might be no need for
    statistics updates if the statistical distribution of the data is
    not changing much. A simple rule of thumb is to think about how much
    the minimum and maximum values of the columns in the table change.
    For example, a <type>timestamp</type> column that contains the time
    of row update will have a constantly-increasing maximum value as
    rows are added and updated; such a column will probably need more
    frequent statistics updates than, say, a column containing URLs for
    pages accessed on a website. The URL column might receive changes just
    as often, but the statistical distribution of its values probably
    changes relatively slowly.
   </para>

   <para>
    It is possible to run <command>ANALYZE</> on specific tables and even
    just specific columns of a table, so the flexibility exists to update some
    statistics more frequently than others if your application requires it.
    In practice, however, it is usually best to just analyze the entire
    database, because it is a fast operation.  <command>ANALYZE</> uses a
    statistical random sampling of the rows of a table rather than reading
    every single row.
   </para>

   <tip>
    <para>
     Although per-column tweaking of <command>ANALYZE</> frequency might not be
     very productive, you might well find it worthwhile to do per-column
     adjustment of the level of detail of the statistics collected by
     <command>ANALYZE</>.  Columns that are heavily used in <literal>WHERE</>
     clauses and have highly irregular data distributions might require a
     finer-grain data histogram than other columns.  See <command>ALTER TABLE
     SET STATISTICS</>, or change the database-wide default using the <xref
     linkend="guc-default-statistics-target"> configuration parameter.
    </para>
   </tip>
  </sect2>

  <sect2 id="vacuum-for-wraparound">
   <title>Preventing Transaction ID Wraparound Failures</title>

   <indexterm zone="vacuum-for-wraparound">
    <primary>transaction ID</primary>
    <secondary>wraparound</secondary>
   </indexterm>

   <para>
    <productname>PostgreSQL</productname>'s MVCC transaction semantics
    depend on being able to compare transaction ID (<acronym>XID</>)
    numbers: a row version with an insertion XID greater than the current
    transaction's XID is <quote>in the future</> and should not be visible
    to the current transaction.  But since transaction IDs have limited size
    (32 bits at this writing) a cluster that runs for a long time (more
    than 4 billion transactions) would suffer <firstterm>transaction ID
    wraparound</>: the XID counter wraps around to zero, and all of a sudden
    transactions that were in the past appear to be in the future &mdash; which
    means their outputs become invisible.  In short, catastrophic data loss.
    (Actually the data is still there, but that's cold comfort if you cannot
    get at it.)  To avoid this, it is necessary to vacuum every table
    in every database at least once every two billion transactions.
   </para>

   <para>
    The reason that periodic vacuuming solves the problem is that
    <productname>PostgreSQL</productname> distinguishes a special XID
    <literal>FrozenXID</>.  This XID is always considered older
    than every normal XID. Normal XIDs are
    compared using modulo-2<superscript>32</> arithmetic. This means
    that for every normal XID, there are two billion XIDs that are
    <quote>older</> and two billion that are <quote>newer</>; another
    way to say it is that the normal XID space is circular with no
    endpoint. Therefore, once a row version has been created with a particular
    normal XID, the row version will appear to be <quote>in the past</> for
    the next two billion transactions, no matter which normal XID we are
    talking about. If the row version still exists after more than two billion
    transactions, it will suddenly appear to be in the future. To
    prevent data loss, old row versions must be reassigned the XID
    <literal>FrozenXID</> sometime before they reach the
    two-billion-transactions-old mark. Once they are assigned this
    special XID, they will appear to be <quote>in the past</> to all
    normal transactions regardless of wraparound issues, and so such
    row versions will be good until deleted, no matter how long that is.
    This reassignment of old XIDs is handled by <command>VACUUM</>.
   </para>

   <para>
    <xref linkend="guc-vacuum-freeze-min-age">
    controls how old an XID value has to be before it's replaced with
    <literal>FrozenXID</>.  Larger values of this setting
    preserve transactional information longer, while smaller values increase
    the number of transactions that can elapse before the table must be
    vacuumed again.
   </para>

   <para>
    <command>VACUUM</> normally skips pages that don't have any dead row
    versions, but those pages might still have row versions with old XID
    values.  To ensure all old XIDs have been replaced by
    <literal>FrozenXID</>, a scan of the whole table is needed.
    <xref linkend="guc-vacuum-freeze-table-age"> controls when
    <command>VACUUM</> does that: a whole table sweep is forced if
    the table hasn't been fully scanned for <varname>vacuum_freeze_table_age</>
    minus <varname>vacuum_freeze_min_age</> transactions. Setting it to 0
    forces <command>VACUUM</> to always scan all pages, effectively ignoring
    the visibility map.
   </para>

   <para>
    The maximum time that a table can go unvacuumed is two billion
    transactions minus the <varname>vacuum_freeze_min_age</> that was used
    when <command>VACUUM</> last scanned the whole table.  If it were to go
    unvacuumed for longer than
    that, data loss could result.  To ensure that this does not happen,
    autovacuum is invoked on any table that might contain XIDs older than the
    age specified by the configuration parameter <xref
    linkend="guc-autovacuum-freeze-max-age">.  (This will happen even if
    autovacuum is otherwise disabled.)
   </para>

   <para>
    This implies that if a table is not otherwise vacuumed,
    autovacuum will be invoked on it approximately once every
    <varname>autovacuum_freeze_max_age</> minus
    <varname>vacuum_freeze_min_age</> transactions.
    For tables that are regularly vacuumed for space reclamation purposes,
    this is of little importance.  However, for static tables
    (including tables that receive inserts, but no updates or deletes),
    there is no need for vacuuming for space reclamation, and so it can
    be useful to try to maximize the interval between forced autovacuums
    on very large static tables.  Obviously one can do this either by
    increasing <varname>autovacuum_freeze_max_age</> or by decreasing
    <varname>vacuum_freeze_min_age</>.
   </para>

   <para>
    The effective maximum for <varname>vacuum_freeze_table_age</> is 0.95 *
    <varname>autovacuum_freeze_max_age</>; a setting higher than that will be
    capped to the maximum. A value higher than
    <varname>autovacuum_freeze_max_age</> wouldn't make sense because an
    anti-wraparound autovacuum would be triggered at that point anyway, and
    the 0.95 multiplier leaves some breathing room to run a manual
    <command>VACUUM</> before that happens.  As a rule of thumb,
    <command>vacuum_freeze_table_age</> should be set to a value somewhat
    below <varname>autovacuum_freeze_max_age</>, leaving enough gap so that
    a regularly scheduled <command>VACUUM</> or an autovacuum triggered by
    normal delete and update activity is run in that window.  Setting it too
    close could lead to anti-wraparound autovacuums, even though the table
    was recently vacuumed to reclaim space, whereas lower values lead to more
    frequent whole-table scans.
   </para>

   <para>
    The sole disadvantage of increasing <varname>autovacuum_freeze_max_age</>
    (and <varname>vacuum_freeze_table_age</> along with it)
    is that the <filename>pg_clog</> subdirectory of the database cluster
    will take more space, because it must store the commit status for all
    transactions back to the <varname>autovacuum_freeze_max_age</> horizon.
    The commit status uses two bits per transaction, so if
    <varname>autovacuum_freeze_max_age</> has its maximum allowed
    value of two billion, <filename>pg_clog</> can be expected to
    grow to about half a gigabyte.  If this is trivial compared to your
    total database size, setting <varname>autovacuum_freeze_max_age</> to
    its maximum allowed value is recommended.  Otherwise, set it depending
    on what you are willing to allow for <filename>pg_clog</> storage.
    (The default, 200 million transactions, translates to about 50MB of
    <filename>pg_clog</> storage.)
   </para>

   <para>
    One disadvantage of decreasing <varname>vacuum_freeze_min_age</> is that
    it might cause <command>VACUUM</> to do useless work: changing a table row's
    XID to <literal>FrozenXID</> is a waste of time if the row is modified
    soon thereafter (causing it to acquire a new XID).  So the setting should
    be large enough that rows are not frozen until they are unlikely to change
    any more.  Another disadvantage of decreasing this setting is
    that details about exactly which transaction inserted or modified a
    row will be lost sooner.  This information sometimes comes in handy,
    particularly when trying to analyze what went wrong after a database
    failure.  For these two reasons, decreasing this setting is not
    recommended except for completely static tables.
   </para>

   <para>
    To track the age of the oldest XIDs in a database,
    <command>VACUUM</> stores XID
    statistics in the system tables <structname>pg_class</> and
    <structname>pg_database</>.  In particular,
    the <structfield>relfrozenxid</> column of a table's
    <structname>pg_class</> row contains the freeze cutoff XID that was used
    by the last whole-table <command>VACUUM</> for that table.  All normal
    XIDs older than this cutoff XID are guaranteed to have been replaced by
    <literal>FrozenXID</> within the table.  Similarly,
    the <structfield>datfrozenxid</> column of a database's
    <structname>pg_database</> row is a lower bound on the normal XIDs
    appearing in that database &mdash; it is just the minimum of the
    per-table <structfield>relfrozenxid</> values within the database.
    A convenient way to
    examine this information is to execute queries such as:

<programlisting>
SELECT c.oid::regclass as table_name,
       greatest(age(c.relfrozenxid),age(t.relfrozenxid)) as age
FROM pg_class c
LEFT JOIN pg_class t ON c.reltoastrelid = t.oid
WHERE c.relkind = 'r';

SELECT datname, age(datfrozenxid) FROM pg_database;
</programlisting>

    The <literal>age</> column measures the number of transactions from the
    cutoff XID to the current transaction's XID.
   </para>

   <para>
    <command>VACUUM</> normally
    only scans pages that have been modified since the last vacuum, but
    <structfield>relfrozenxid</> can only be advanced when the whole table is
    scanned. The whole table is scanned when <structfield>relfrozenxid</> is
    more than <varname>vacuum_freeze_table_age</> transactions old, when the
    <command>VACUUM FREEZE</> command is used, or when all pages happen to
    require vacuuming to remove dead row versions. When <command>VACUUM</>
    scans the whole table, after it's finished <literal>age(relfrozenxid)</>
    should be a little more than the <varname>vacuum_freeze_min_age</> setting
    that was used (more by the number of transactions started since the
    <command>VACUUM</> started).  If no whole-table-scanning <command>VACUUM</>
    is issued on the table until <varname>autovacuum_freeze_max_age</> is
    reached, an autovacuum will soon be forced for the table.
   </para>

   <para>
    If for some reason autovacuum fails to clear old XIDs from a table,
    the system will begin to emit warning messages like this when the
    database's oldest XIDs reach ten million transactions from the wraparound
    point:

<programlisting>
WARNING:  database "mydb" must be vacuumed within 177009986 transactions
HINT:  To avoid a database shutdown, execute a database-wide VACUUM in "mydb".
</programlisting>

    (A manual <command>VACUUM</> should fix the problem, as suggested by the
    hint; but note that the <command>VACUUM</> must be performed by a
    superuser, else it will fail to process system catalogs and thus not
    be able to advance the database's <structfield>datfrozenxid</>.)
    If these warnings are
    ignored, the system will shut down and refuse to execute any new
    transactions once there are fewer than 1 million transactions left
    until wraparound:

<programlisting>
ERROR:  database is not accepting commands to avoid wraparound data loss in database "mydb"
HINT:  Stop the postmaster and use a standalone backend to VACUUM in "mydb".
</programlisting>

    The 1-million-transaction safety margin exists to let the
    administrator recover without data loss, by manually executing the
    required <command>VACUUM</> commands.  However, since the system will not
    execute commands once it has gone into the safety shutdown mode,
    the only way to do this is to stop the server and use a single-user
    backend to execute <command>VACUUM</>.  The shutdown mode is not enforced
    by a single-user backend.  See the <xref linkend="app-postgres"> reference
    page for details about using a single-user backend.
   </para>

  </sect2>

  <sect2 id="autovacuum">
   <title id="autovacuum-title">The Autovacuum Daemon</title>

   <indexterm>
    <primary>autovacuum</primary>
    <secondary>general information</secondary>
   </indexterm>
   <para>
    <productname>PostgreSQL</productname> has an optional but highly
    recommended feature called <firstterm>autovacuum</firstterm>,
    whose purpose is to automate the execution of
    <command>VACUUM</command> and <command>ANALYZE </command> commands.
    When enabled, autovacuum checks for
    tables that have had a large number of inserted, updated or deleted
    tuples.  These checks use the statistics collection facility;
    therefore, autovacuum cannot be used unless <xref
    linkend="guc-track-counts"> is set to <literal>true</literal>.
    In the default configuration, autovacuuming is enabled and the related
    configuration parameters are appropriately set.
   </para>

   <para>
    The <quote>autovacuum daemon</> actually consists of multiple processes.
    There is a persistent daemon process, called the
    <firstterm>autovacuum launcher</firstterm>, which is in charge of starting
    <firstterm>autovacuum worker</firstterm> processes for all databases. The
    launcher will distribute the work across time, attempting to start one
    worker on each database every <xref linkend="guc-autovacuum-naptime">
    seconds. One worker will be launched for each database, with a maximum
    of <xref linkend="guc-autovacuum-max-workers"> processes running at the
    same time. If there are more than
    <xref linkend="guc-autovacuum-max-workers"> databases to be processed,
    the next database will be processed as soon as the first worker finishes.
    Each worker process will check each table within its database and
    execute <command>VACUUM</> and/or <command>ANALYZE</> as needed.
   </para>

   <para>
    The <xref linkend="guc-autovacuum-max-workers"> setting limits how many
    workers may be running at any time. If several large tables all become
    eligible for vacuuming in a short amount of time, all autovacuum workers
    may become occupied with vacuuming those tables for a long period.
    This would result
    in other tables and databases not being vacuumed until a worker became
    available. There is not a limit on how many workers might be in a
    single database, but workers do try to avoid repeating work that has
    already been done by other workers. Note that the number of running
    workers does not count towards the <xref linkend="guc-max-connections"> nor
    the <xref linkend="guc-superuser-reserved-connections"> limits.
   </para>

   <para>
    Tables whose <structfield>relfrozenxid</> value is more than
    <xref linkend="guc-autovacuum-freeze-max-age"> transactions old are always
    vacuumed (this also applies to those tables whose freeze max age has
    been modified via storage parameters; see below).  Otherwise, if the
    number of tuples obsoleted since the last
    <command>VACUUM</command> exceeds the <quote>vacuum threshold</quote>, the
    table is vacuumed.  The vacuum threshold is defined as:
<programlisting>
vacuum threshold = vacuum base threshold + vacuum scale factor * number of tuples
</programlisting>
    where the vacuum base threshold is
    <xref linkend="guc-autovacuum-vacuum-threshold">,
    the vacuum scale factor is
    <xref linkend="guc-autovacuum-vacuum-scale-factor">,
    and the number of tuples is
    <structname>pg_class</structname>.<structfield>reltuples</structfield>.
    The number of obsolete tuples is obtained from the statistics
    collector; it is a semi-accurate count updated by each
    <command>UPDATE</command> and <command>DELETE</command> operation.  (It
    is only semi-accurate because some information might be lost under heavy
    load.)  If the <structfield>relfrozenxid</> value of the table is more
    than <varname>vacuum_freeze_table_age</> transactions old, the whole
    table is scanned to freeze old tuples and advance
    <structfield>relfrozenxid</>, otherwise only pages that have been modified
    since the last vacuum are scanned.
   </para>

   <para>
    For analyze, a similar condition is used: the threshold, defined as:
<programlisting>
analyze threshold = analyze base threshold + analyze scale factor * number of tuples
</programlisting>
    is compared to the total number of tuples inserted or updated
    since the last <command>ANALYZE</command>.
   </para>

   <para>
    The default thresholds and scale factors are taken from
    <filename>postgresql.conf</filename>, but it is possible to override them
    on a table-by-table basis; see
    <xref linkend="sql-createtable-storage-parameters"
    endterm="sql-createtable-storage-parameters-title"> for more information.
    If a setting
    has been changed via storage parameters, that value is used; otherwise the
    global settings are used. See <xref linkend="runtime-config-autovacuum"> for
    more details on the global settings.
   </para>

   <para>
    Besides the base threshold values and scale factors, there are six
    more autovacuum parameters that can be set for each table via
    storage parameters.
    The first parameter, <literal>autovacuum_enabled</>,
    can be set to <literal>false</literal> to instruct the autovacuum daemon
    to skip that particular table entirely.  In this case
    autovacuum will only touch the table if it must do so
    to prevent transaction ID wraparound.
    Another two parameters,
    <literal>autovacuum_vacuum_cost_delay</literal> and
    <literal>autovacuum_vacuum_cost_limit</literal>, are used to set
    table-specific values for the
    <xref linkend="runtime-config-resource-vacuum-cost"
    endterm="runtime-config-resource-vacuum-cost-title">
    feature.
    <literal>autovacuum_freeze_min_age</literal>,
    <literal>autovacuum_freeze_max_age</literal> and
    <literal>autovacuum_freeze_table_age</literal> are used to set
    values for <xref linkend="guc-vacuum-freeze-min-age">,
    <xref linkend="guc-autovacuum-freeze-max-age"> and
    <xref linkend="guc-vacuum-freeze-table-age"> respectively.
   </para>

   <para>
    When multiple workers are running, the cost delay parameters are
    <quote>balanced</quote> among all the running workers, so that the
    total I/O impact on the system is the same regardless of the number
    of workers actually running.  However, any workers processing tables whose
    <literal>autovacuum_vacuum_cost_delay</> or
    <literal>autovacuum_vacuum_cost_limit</> have been set are not considered
    in the balancing algorithm.
   </para>
  </sect2>
 </sect1>


 <sect1 id="routine-reindex">
  <title>Routine Reindexing</title>

  <indexterm zone="routine-reindex">
   <primary>reindex</primary>
  </indexterm>

  <para>
   In some situations it is worthwhile to rebuild indexes periodically
   with the <xref linkend="sql-reindex" endterm="sql-reindex-title">
   command.
  </para>

  <para>
   In <productname>PostgreSQL</> releases before 7.4, periodic reindexing
   was frequently necessary to avoid <quote>index bloat</>, due to lack of
   internal space reclamation in B-tree indexes.  Any situation in which the
   range of index keys changed over time &mdash; for example, an index on
   timestamps in a table where old entries are eventually deleted &mdash;
   would result in bloat, because index pages for no-longer-needed portions
   of the key range were not reclaimed for re-use.  Over time, the index size
   could become indefinitely much larger than the amount of useful data in it.
  </para>

  <para>
   In <productname>PostgreSQL</> 7.4 and later, index pages that have become
   completely empty are reclaimed for re-use.  There is still a possibility
   for inefficient use of space: if all but a few index keys on a page have
   been deleted, the page remains allocated.  So a usage pattern in which all
   but a few keys in each range are eventually deleted will see poor use of
   space.  For such usage patterns, periodic reindexing is recommended.
  </para>

  <para>
   The potential for bloat in non-B-tree indexes has not been well
   characterized.  It is a good idea to keep an eye on the index's physical
   size when using any non-B-tree index type.
  </para>

  <para>
   Also, for B-tree indexes a freshly-constructed index is somewhat faster to
   access than one that has been updated many times, because logically
   adjacent pages are usually also physically adjacent in a newly built index.
   (This consideration does not currently apply to non-B-tree indexes.)  It
   might be worthwhile to reindex periodically just to improve access speed.
  </para>
 </sect1>


 <sect1 id="logfile-maintenance">
  <title>Log File Maintenance</title>

  <indexterm zone="logfile-maintenance">
   <primary>server log</primary>
   <secondary>log file maintenance</secondary>
  </indexterm>

  <para>
   It is a good idea to save the database server's log output
   somewhere, rather than just routing it to <filename>/dev/null</>.
   The log output is invaluable when it comes time to diagnose
   problems.  However, the log output tends to be voluminous
   (especially at higher debug levels) and you won't want to save it
   indefinitely.  You need to <quote>rotate</> the log files so that
   new log files are started and old ones removed after a reasonable
   period of time.
  </para>

  <para>
   If you simply direct the <systemitem>stderr</> of
   <command>postgres</command> into a
   file, you will have log output, but
   the only way to truncate the log file is to stop and restart
   the server. This might be OK if you are using
   <productname>PostgreSQL</productname> in a development environment,
   but few production servers would find this behavior acceptable.
  </para>

  <para>
   A better approach is to send the server's
   <systemitem>stderr</> output to some type of log rotation program.
   There is a built-in log rotation program, which you can use by
   setting the configuration parameter <literal>logging_collector</> to
   <literal>true</> in <filename>postgresql.conf</>.  The control
   parameters for this program are described in <xref
   linkend="runtime-config-logging-where">. You can also use this approach
   to capture the log data in machine readable CSV format.
  </para>

  <para>
   Alternatively, you might prefer to use an external log rotation
   program, if you have one that you are already using with other
   server software. For example, the <application>rotatelogs</application>
   tool included in the <productname>Apache</productname> distribution
   can be used with <productname>PostgreSQL</productname>.  To do this,
   just pipe the server's
   <systemitem>stderr</> output to the desired program.
   If you start the server with
   <command>pg_ctl</>, then <systemitem>stderr</>
   is already redirected to <systemitem>stdout</>, so you just need a
   pipe command, for example:

<programlisting>
pg_ctl start | rotatelogs /var/log/pgsql_log 86400
</programlisting>
  </para>

  <para>
   Another production-grade approach to managing log output is to
   send it all to <application>syslog</> and let
   <application>syslog</> deal with file rotation. To do this, set the
   configuration parameter <literal>log_destination</> to <literal>syslog</>
   (to log to <application>syslog</> only) in
   <filename>postgresql.conf</>. Then you can send a <literal>SIGHUP</literal>
   signal to the <application>syslog</> daemon whenever you want to force it
   to start writing a new log file.  If you want to automate log
   rotation, the <application>logrotate</application> program can be
   configured to work with log files from
   <application>syslog</application>.
  </para>

  <para>
   On many systems, however, <application>syslog</> is not very reliable,
   particularly with large log messages; it might truncate or drop messages
   just when you need them the most.  Also, on <productname>Linux</>,
   <application>syslog</> will sync each message to disk, yielding poor
   performance.  (You can use a <literal>-</> at the start of the file name
   in the <application>syslog</> configuration file to disable syncing.)
  </para>

  <para>
   Note that all the solutions described above take care of starting new
   log files at configurable intervals, but they do not handle deletion
   of old, no-longer-interesting log files.  You will probably want to set
   up a batch job to periodically delete old log files.  Another possibility
   is to configure the rotation program so that old log files are overwritten
   cyclically.
  </para>
 </sect1>
</chapter>
