|
Server : Apache/2.2.2 (Fedora) System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686 User : apache ( 48) PHP Version : 5.2.9 Disable Function : NONE Directory : /proc/self/root/usr/share/doc/postgresql-8.1.9/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Routine Database Maintenance Tasks</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.1.9 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Server Administration"
HREF="admin.html"><LINK
REL="PREVIOUS"
TITLE="Character Set Support"
HREF="multibyte.html"><LINK
REL="NEXT"
TITLE="Routine Reindexing"
HREF="routine-reindex.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2007-04-20T04:40:08"></HEAD
><BODY
CLASS="CHAPTER"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.1.9 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="multibyte.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="charset.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="backup.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="routine-reindex.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="MAINTENANCE"
></A
>Chapter 22. Routine Database Maintenance Tasks</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
>22.1. <A
HREF="maintenance.html#ROUTINE-VACUUMING"
>Routine Vacuuming</A
></DT
><DD
><DL
><DT
>22.1.1. <A
HREF="maintenance.html#VACUUM-FOR-SPACE-RECOVERY"
>Recovering disk space</A
></DT
><DT
>22.1.2. <A
HREF="maintenance.html#VACUUM-FOR-STATISTICS"
>Updating planner statistics</A
></DT
><DT
>22.1.3. <A
HREF="maintenance.html#VACUUM-FOR-WRAPAROUND"
>Preventing transaction ID wraparound failures</A
></DT
><DT
>22.1.4. <A
HREF="maintenance.html#AUTOVACUUM"
>The auto-vacuum daemon</A
></DT
></DL
></DD
><DT
>22.2. <A
HREF="routine-reindex.html"
>Routine Reindexing</A
></DT
><DT
>22.3. <A
HREF="logfile-maintenance.html"
>Log File Maintenance</A
></DT
></DL
></DIV
><A
NAME="AEN22931"
></A
><P
> There are a few routine maintenance chores that must be performed on
a regular basis to keep a <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
server running smoothly. The tasks discussed here are repetitive
in nature and can easily be automated using standard Unix tools such
as <SPAN
CLASS="APPLICATION"
>cron</SPAN
> scripts. But it is the database
administrator's responsibility to set up appropriate scripts, and to
check that they execute successfully.
</P
><P
> 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
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> are discussed at length in
<A
HREF="backup.html"
>Chapter 23</A
>.
</P
><P
> The other main category of maintenance task is periodic <SPAN
CLASS="QUOTE"
>"vacuuming"</SPAN
>
of the database. This activity is discussed in
<A
HREF="maintenance.html#ROUTINE-VACUUMING"
>Section 22.1</A
>.
</P
><P
> Something else that might need periodic attention is log file management.
This is discussed in <A
HREF="logfile-maintenance.html"
>Section 22.3</A
>.
</P
><P
> <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 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.
</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="ROUTINE-VACUUMING"
>22.1. Routine Vacuuming</A
></H1
><A
NAME="AEN22948"
></A
><P
> <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>'s <TT
CLASS="COMMAND"
>VACUUM</TT
> command
must be run on a regular basis for several reasons:
<P
></P
></P><OL
TYPE="1"
><LI
><P
>To recover disk space occupied by updated or deleted
rows.</P
></LI
><LI
><P
>To update data statistics used by the
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> query planner.</P
></LI
><LI
><P
>To protect against loss of very old data due to
<I
CLASS="FIRSTTERM"
>transaction ID wraparound</I
>.</P
></LI
></OL
><P>
The frequency and scope of the <TT
CLASS="COMMAND"
>VACUUM</TT
> operations
performed for each of these reasons will vary depending on the
needs of each site. Therefore, database administrators must
understand these issues and develop an appropriate maintenance
strategy. This section concentrates on explaining the high-level
issues; for details about command syntax and so on, see the <A
HREF="sql-vacuum.html"
><I
>VACUUM</I
></A
> reference page.
</P
><P
> Beginning in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 7.2, the standard form
of <TT
CLASS="COMMAND"
>VACUUM</TT
> can run in parallel with normal database operations
(selects, inserts, updates, deletes, but not changes to table definitions).
Routine vacuuming is therefore not nearly as intrusive as it was in prior
releases, and it is not as critical to try to schedule it at low-usage
times of day.
</P
><P
> Beginning in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 8.0, there are
configuration parameters that can be adjusted to further reduce the
performance impact of background vacuuming. See
<A
HREF="runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST"
>Section 17.4.4</A
>.
</P
><P
> An automated mechanism for performing the necessary <TT
CLASS="COMMAND"
>VACUUM</TT
>
operations has been added in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 8.1.
See <A
HREF="maintenance.html#AUTOVACUUM"
>Section 22.1.4</A
>.
</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="VACUUM-FOR-SPACE-RECOVERY"
>22.1.1. Recovering disk space</A
></H2
><A
NAME="AEN22976"
></A
><P
> In normal <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> operation, an
<TT
CLASS="COMMAND"
>UPDATE</TT
> or <TT
CLASS="COMMAND"
>DELETE</TT
> 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 <A
HREF="mvcc.html"
>Chapter 12</A
>): 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 be
reclaimed for reuse by new rows, to avoid infinite growth of disk
space requirements. This is done by running <TT
CLASS="COMMAND"
>VACUUM</TT
>.
</P
><P
> Clearly, a table that receives frequent updates or deletes will need
to be vacuumed more often than tables that are seldom updated. It
may be useful to set up periodic <SPAN
CLASS="APPLICATION"
>cron</SPAN
> tasks that
<TT
CLASS="COMMAND"
>VACUUM</TT
> only selected tables, skipping tables that are known not to
change often. This is only likely to be helpful if you have both
large heavily-updated tables and large seldom-updated tables — the
extra cost of vacuuming a small table isn't enough to be worth
worrying about.
</P
><P
> There are two variants of the <TT
CLASS="COMMAND"
>VACUUM</TT
>
command. The first form, known as <SPAN
CLASS="QUOTE"
>"lazy vacuum"</SPAN
> or
just <TT
CLASS="COMMAND"
>VACUUM</TT
>, marks expired data in tables and
indexes for future reuse; it does <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> attempt
to reclaim the space used by this expired data unless the space is
at the end of the table and an exclusive table lock can be easily
obtained. Unused space at the start or middle of the file does
not result in the file being shortened and space returned to the
operating system. This variant of <TT
CLASS="COMMAND"
>VACUUM</TT
> can be
run concurrently with normal database operations.
</P
><P
> The second form is the <TT
CLASS="COMMAND"
>VACUUM FULL</TT
>
command. This uses a more aggressive algorithm for reclaiming the
space consumed by expired row versions. Any space that is freed by
<TT
CLASS="COMMAND"
>VACUUM FULL</TT
> is immediately returned to the
operating system. Unfortunately, this variant of the
<TT
CLASS="COMMAND"
>VACUUM</TT
> command acquires an exclusive lock on
each table while <TT
CLASS="COMMAND"
>VACUUM FULL</TT
> is processing
it. Therefore, frequently using <TT
CLASS="COMMAND"
>VACUUM FULL</TT
> can
have an extremely negative effect on the performance of concurrent
database queries.
</P
><P
> The standard form of <TT
CLASS="COMMAND"
>VACUUM</TT
> is best used with the goal
of maintaining a fairly level steady-state usage of disk space. If
you need to return disk space to the operating system you can use
<TT
CLASS="COMMAND"
>VACUUM FULL</TT
> — but what's the point of releasing disk
space that will only have to be allocated again soon? Moderately
frequent standard <TT
CLASS="COMMAND"
>VACUUM</TT
> runs are a better approach
than infrequent <TT
CLASS="COMMAND"
>VACUUM FULL</TT
> runs for maintaining
heavily-updated tables.
</P
><P
> Recommended practice for most sites is to schedule a database-wide
<TT
CLASS="COMMAND"
>VACUUM</TT
> once a day at a low-usage time of day,
supplemented by more frequent vacuuming of heavily-updated tables
if necessary. (Some installations with an extremely high
rate of data modification <TT
CLASS="COMMAND"
>VACUUM</TT
> busy tables as
often as once every few minutes.) If you have multiple databases
in a cluster, don't forget to <TT
CLASS="COMMAND"
>VACUUM</TT
> each one;
the program <A
HREF="app-vacuumdb.html"
><I
><I
>vacuumdb</I
></I
></A
>
may be helpful.
</P
><P
> <TT
CLASS="COMMAND"
>VACUUM FULL</TT
> is recommended for cases where you know
you have deleted the majority of rows in a table, so that the
steady-state size of the table can be shrunk substantially with
<TT
CLASS="COMMAND"
>VACUUM FULL</TT
>'s more aggressive approach. Use plain
<TT
CLASS="COMMAND"
>VACUUM</TT
>, not <TT
CLASS="COMMAND"
>VACUUM FULL</TT
>, for routine
vacuuming for space recovery.
</P
><P
> If you have a table whose contents are deleted on a periodic
basis, consider doing it with <TT
CLASS="COMMAND"
>TRUNCATE</TT
> rather
than using <TT
CLASS="COMMAND"
>DELETE</TT
> followed by
<TT
CLASS="COMMAND"
>VACUUM</TT
>. <TT
CLASS="COMMAND"
>TRUNCATE</TT
> removes the
entire content of the table immediately, without requiring a
subsequent <TT
CLASS="COMMAND"
>VACUUM</TT
> or <TT
CLASS="COMMAND"
>VACUUM
FULL</TT
> to reclaim the now-unused disk space.
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="VACUUM-FOR-STATISTICS"
>22.1.2. Updating planner statistics</A
></H2
><A
NAME="AEN23023"
></A
><A
NAME="AEN23026"
></A
><P
> The <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 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 <TT
CLASS="COMMAND"
>ANALYZE</TT
> command, which can be invoked by itself or
as an optional step in <TT
CLASS="COMMAND"
>VACUUM</TT
>. It is important to have
reasonably accurate statistics, otherwise poor choices of plans may
degrade database performance.
</P
><P
> 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 may 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 <TT
CLASS="TYPE"
>timestamp</TT
> 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 may receive changes just
as often, but the statistical distribution of its values probably
changes relatively slowly.
</P
><P
> It is possible to run <TT
CLASS="COMMAND"
>ANALYZE</TT
> 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, the usefulness of this feature is doubtful.
Beginning in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 7.2,
<TT
CLASS="COMMAND"
>ANALYZE</TT
> is a fairly fast operation even on large tables,
because it uses a statistical random sampling of the rows of a table
rather than reading every single row. So it's probably much simpler
to just run it over the whole database every so often.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> Although per-column tweaking of <TT
CLASS="COMMAND"
>ANALYZE</TT
> frequency may not be
very productive, you may well find it worthwhile to do per-column
adjustment of the level of detail of the statistics collected by
<TT
CLASS="COMMAND"
>ANALYZE</TT
>. Columns that are heavily used in <TT
CLASS="LITERAL"
>WHERE</TT
> clauses
and have highly irregular data distributions may require a finer-grain
data histogram than other columns. See <TT
CLASS="COMMAND"
>ALTER TABLE SET
STATISTICS</TT
>.
</P
></BLOCKQUOTE
></DIV
><P
> Recommended practice for most sites is to schedule a database-wide
<TT
CLASS="COMMAND"
>ANALYZE</TT
> once a day at a low-usage time of day; this can
usefully be combined with a nightly <TT
CLASS="COMMAND"
>VACUUM</TT
>. However,
sites with relatively slowly changing table statistics may find that
this is overkill, and that less-frequent <TT
CLASS="COMMAND"
>ANALYZE</TT
> runs
are sufficient.
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="VACUUM-FOR-WRAPAROUND"
>22.1.3. Preventing transaction ID wraparound failures</A
></H2
><A
NAME="AEN23050"
></A
><P
> <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>'s MVCC transaction semantics
depend on being able to compare transaction ID (<ACRONYM
CLASS="ACRONYM"
>XID</ACRONYM
>)
numbers: a row version with an insertion XID greater than the current
transaction's XID is <SPAN
CLASS="QUOTE"
>"in the future"</SPAN
> 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 <I
CLASS="FIRSTTERM"
>transaction ID
wraparound</I
>: the XID counter wraps around to zero, and all of a sudden
transactions that were in the past appear to be in the future — which
means their outputs become invisible. In short, catastrophic data loss.
(Actually the data is still there, but that's cold comfort if you can't
get at it.)
</P
><P
> Prior to <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 7.2, the only defense
against XID wraparound was to re-<TT
CLASS="COMMAND"
>initdb</TT
> at least every 4
billion transactions. This of course was not very satisfactory for
high-traffic sites, so a better solution has been devised. The new
approach allows a server to remain up indefinitely, without
<TT
CLASS="COMMAND"
>initdb</TT
> or any sort of restart. The price is this
maintenance requirement: <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>every table in the database must
be vacuumed at least once every billion transactions</I
></SPAN
>.
</P
><P
> In practice this isn't an onerous requirement, but since the
consequences of failing to meet it can be complete data loss (not
just wasted disk space or slow performance), some special provisions
have been made to help database administrators avoid disaster.
For each database in the cluster, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
keeps track of the time of the last database-wide <TT
CLASS="COMMAND"
>VACUUM</TT
>.
When any database approaches the billion-transaction danger level,
the system begins to emit warning messages. If nothing is done, it
will eventually shut down normal operations until appropriate
manual maintenance is done. The remainder of this
section gives the details.
</P
><P
> The new approach to XID comparison distinguishes two special XIDs,
numbers 1 and 2 (<TT
CLASS="LITERAL"
>BootstrapXID</TT
> and
<TT
CLASS="LITERAL"
>FrozenXID</TT
>). These two XIDs are always considered older
than every normal XID. Normal XIDs (those greater than 2) are
compared using modulo-2<SUP
>31</SUP
> arithmetic. This means
that for every normal XID, there are two billion XIDs that are
<SPAN
CLASS="QUOTE"
>"older"</SPAN
> and two billion that are <SPAN
CLASS="QUOTE"
>"newer"</SPAN
>; 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 <SPAN
CLASS="QUOTE"
>"in the past"</SPAN
> 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
<TT
CLASS="LITERAL"
>FrozenXID</TT
> sometime before they reach the
two-billion-transactions-old mark. Once they are assigned this
special XID, they will appear to be <SPAN
CLASS="QUOTE"
>"in the past"</SPAN
> 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 XID is handled by <TT
CLASS="COMMAND"
>VACUUM</TT
>.
</P
><P
> <TT
CLASS="COMMAND"
>VACUUM</TT
>'s normal policy is to reassign <TT
CLASS="LITERAL"
>FrozenXID</TT
>
to any row version with a normal XID more than one billion transactions in the
past. This policy preserves the original insertion XID until it is not
likely to be of interest anymore. (In fact, most row versions will probably
live and die without ever being <SPAN
CLASS="QUOTE"
>"frozen"</SPAN
>.) With this policy,
the maximum safe interval between <TT
CLASS="COMMAND"
>VACUUM</TT
> runs on any table
is exactly one billion transactions: if you wait longer, it's possible
that a row version that was not quite old enough to be reassigned last time
is now more than two billion transactions old and has wrapped around
into the future — i.e., is lost to you. (Of course, it'll reappear
after another two billion transactions, but that's no help.)
</P
><P
> Since periodic <TT
CLASS="COMMAND"
>VACUUM</TT
> runs are needed anyway for the reasons
described earlier, it's unlikely that any table would not be vacuumed
for as long as a billion transactions. But to help administrators ensure
this constraint is met, <TT
CLASS="COMMAND"
>VACUUM</TT
> stores transaction ID
statistics in the system table <TT
CLASS="LITERAL"
>pg_database</TT
>. In particular,
the <TT
CLASS="LITERAL"
>datfrozenxid</TT
> column of a database's
<TT
CLASS="LITERAL"
>pg_database</TT
> row is updated at the completion of any
database-wide <TT
CLASS="COMMAND"
>VACUUM</TT
> operation (i.e.,
<TT
CLASS="COMMAND"
>VACUUM</TT
> that does not
name a specific table). The value stored in this field is the freeze
cutoff XID that was used by that <TT
CLASS="COMMAND"
>VACUUM</TT
> command. All normal
XIDs older than this cutoff XID are guaranteed to have been replaced by
<TT
CLASS="LITERAL"
>FrozenXID</TT
> within that database. A convenient way to
examine this information is to execute the query
</P><PRE
CLASS="PROGRAMLISTING"
>SELECT datname, age(datfrozenxid) FROM pg_database;</PRE
><P>
The <TT
CLASS="LITERAL"
>age</TT
> column measures the number of transactions from the
cutoff XID to the current transaction's XID.
</P
><P
> With the standard freezing policy, the <TT
CLASS="LITERAL"
>age</TT
> column will start
at one billion for a freshly-vacuumed database. When the <TT
CLASS="LITERAL"
>age</TT
>
approaches two billion, the database must be vacuumed again to avoid
risk of wraparound failures. Recommended practice is to <TT
CLASS="COMMAND"
>VACUUM</TT
> each
database at least once every half-a-billion (500 million) transactions,
so as to provide plenty of safety margin. To help meet this rule,
each database-wide <TT
CLASS="COMMAND"
>VACUUM</TT
> automatically delivers a warning
if there are any <TT
CLASS="LITERAL"
>pg_database</TT
> entries showing an
<TT
CLASS="LITERAL"
>age</TT
> of more than 1.5 billion transactions, for example:
</P><PRE
CLASS="PROGRAMLISTING"
>play=# VACUUM;
WARNING: database "mydb" must be vacuumed within 177009986 transactions
HINT: To avoid a database shutdown, execute a full-database VACUUM in "mydb".
VACUUM</PRE
><P>
</P
><P
> If the warnings emitted by <TT
CLASS="COMMAND"
>VACUUM</TT
> go ignored, then
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> will begin to emit a warning
like the above on every transaction start once there are fewer than 10
million transactions left until wraparound. If those warnings also 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:
</P><PRE
CLASS="PROGRAMLISTING"
>play=# select 2+2;
ERROR: database is shut down to avoid wraparound data loss in database "mydb"
HINT: Stop the postmaster and use a standalone backend to VACUUM in "mydb".</PRE
><P>
The 1-million-transaction safety margin exists to let the
administrator recover without data loss, by manually executing the
required <TT
CLASS="COMMAND"
>VACUUM</TT
> 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 postmaster and use a standalone
backend to execute <TT
CLASS="COMMAND"
>VACUUM</TT
>. The shutdown mode is not enforced
by a standalone backend. See the <A
HREF="app-postgres.html"
><SPAN
CLASS="APPLICATION"
>postgres</SPAN
></A
> reference
page for details about using a standalone backend.
</P
><P
> <TT
CLASS="COMMAND"
>VACUUM</TT
> with the <TT
CLASS="COMMAND"
>FREEZE</TT
> option uses a more
aggressive freezing policy: row versions are frozen if they are old enough
to be considered good by all open transactions. In particular, if a
<TT
CLASS="COMMAND"
>VACUUM FREEZE</TT
> is performed in an otherwise-idle
database, it is guaranteed that <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>all</I
></SPAN
> row versions in that
database will be frozen. Hence, as long as the database is not
modified in any way, it will not need subsequent vacuuming to avoid
transaction ID wraparound problems. This technique is used by
<TT
CLASS="COMMAND"
>initdb</TT
> to prepare the <TT
CLASS="LITERAL"
>template0</TT
> database.
It should also be used to prepare any user-created databases that
are to be marked <TT
CLASS="LITERAL"
>datallowconn</TT
> = <TT
CLASS="LITERAL"
>false</TT
> in
<TT
CLASS="LITERAL"
>pg_database</TT
>, since there isn't any convenient way to
<TT
CLASS="COMMAND"
>VACUUM</TT
> a database that you can't connect to.
</P
><DIV
CLASS="WARNING"
><P
></P
><TABLE
CLASS="WARNING"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Warning</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
> A database that is marked <TT
CLASS="LITERAL"
>datallowconn</TT
> = <TT
CLASS="LITERAL"
>false</TT
>
in <TT
CLASS="LITERAL"
>pg_database</TT
> is assumed to be properly frozen; the
automatic warnings and wraparound protection shutdown do not take
such databases into account. Therefore it's up to you to ensure
you've correctly frozen a database before you mark it with
<TT
CLASS="LITERAL"
>datallowconn</TT
> = <TT
CLASS="LITERAL"
>false</TT
>.
</P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AUTOVACUUM"
>22.1.4. The auto-vacuum daemon</A
></H2
><A
NAME="AEN23128"
></A
><P
> Beginning in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL </SPAN
> 8.1, there is a
separate optional server process called the <I
CLASS="FIRSTTERM"
>autovacuum
daemon</I
>, whose purpose is to automate the execution of
<TT
CLASS="COMMAND"
>VACUUM</TT
> and <TT
CLASS="COMMAND"
>ANALYZE </TT
> commands.
When enabled, the autovacuum daemon runs periodically and checks for
tables that have had a large number of inserted, updated or deleted
tuples. These checks use the row-level statistics collection facility;
therefore, the autovacuum daemon cannot be used unless <A
HREF="runtime-config-statistics.html#GUC-STATS-START-COLLECTOR"
>stats_start_collector</A
> and <A
HREF="runtime-config-statistics.html#GUC-STATS-ROW-LEVEL"
>stats_row_level</A
> are set to <TT
CLASS="LITERAL"
>true</TT
>. Also,
it's important to allow a slot for the autovacuum process when choosing
the value of <A
HREF="runtime-config-connection.html#GUC-SUPERUSER-RESERVED-CONNECTIONS"
>superuser_reserved_connections</A
>.
</P
><P
> The autovacuum daemon, when enabled, runs every <A
HREF="runtime-config-autovacuum.html#GUC-AUTOVACUUM-NAPTIME"
>autovacuum_naptime</A
> seconds and determines which database
to process. Any database which is close to transaction ID wraparound
is immediately processed. In this case, autovacuum issues a
database-wide <TT
CLASS="COMMAND"
>VACUUM</TT
> call, or <TT
CLASS="COMMAND"
>VACUUM
FREEZE</TT
> if it's a template database, and then terminates. If
no database fulfills this criterion, the one that was least recently
processed by autovacuum is chosen. In this case each table in
the selected database is checked, and individual <TT
CLASS="COMMAND"
>VACUUM</TT
>
or <TT
CLASS="COMMAND"
>ANALYZE</TT
> commands are issued as needed.
</P
><P
> For each table, two conditions are used to determine which operation(s)
to apply. If the number of obsolete tuples since the last
<TT
CLASS="COMMAND"
>VACUUM</TT
> exceeds the <SPAN
CLASS="QUOTE"
>"vacuum threshold"</SPAN
>, the
table is vacuumed. The vacuum threshold is defined as:
</P><PRE
CLASS="PROGRAMLISTING"
>vacuum threshold = vacuum base threshold + vacuum scale factor * number of tuples</PRE
><P>
where the vacuum base threshold is
<A
HREF="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-THRESHOLD"
>autovacuum_vacuum_threshold</A
>,
the vacuum scale factor is
<A
HREF="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-SCALE-FACTOR"
>autovacuum_vacuum_scale_factor</A
>,
and the number of tuples is
<TT
CLASS="STRUCTNAME"
>pg_class</TT
>.<TT
CLASS="STRUCTFIELD"
>reltuples</TT
>.
The number of obsolete tuples is obtained from the statistics
collector; it is a semi-accurate count updated by each
<TT
CLASS="COMMAND"
>UPDATE</TT
> and <TT
CLASS="COMMAND"
>DELETE</TT
> operation. (It
is only semi-accurate because some information may be lost under heavy
load.) For analyze, a similar condition is used: the threshold, defined as
</P><PRE
CLASS="PROGRAMLISTING"
>analyze threshold = analyze base threshold + analyze scale factor * number of tuples</PRE
><P>
is compared to the total number of tuples inserted, updated, or deleted
since the last <TT
CLASS="COMMAND"
>ANALYZE</TT
>.
</P
><P
> The default thresholds and scale factors are taken from
<TT
CLASS="FILENAME"
>postgresql.conf</TT
>, but it is possible to override them
on a table-by-table basis by making entries in the system catalog
<A
HREF="catalog-pg-autovacuum.html"
><TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
></A
>.
If a <TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
> row exists for a particular
table, the settings it specifies are applied; otherwise the global
settings are used. See <A
HREF="runtime-config-autovacuum.html"
>Section 17.9</A
> for
more details on the global settings.
</P
><P
> Besides the base threshold values and scale factors, there are three
more parameters that can be set for each table in
<TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
>.
The first, <TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
>.<TT
CLASS="STRUCTFIELD"
>enabled</TT
>,
can be set to <TT
CLASS="LITERAL"
>false</TT
> to instruct the autovacuum daemon
to skip that particular table entirely. In this case
autovacuum will only touch the table when it vacuums the entire database
to prevent transaction ID wraparound.
The other two parameters, the vacuum cost delay
(<TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
>.<TT
CLASS="STRUCTFIELD"
>vac_cost_delay</TT
>)
and the vacuum cost limit
(<TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
>.<TT
CLASS="STRUCTFIELD"
>vac_cost_limit</TT
>),
are used to set table-specific values for the
<A
HREF="runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST"
><I
>Cost-Based Vacuum Delay</I
></A
>
feature.
</P
><P
> If any of the values in <TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
>
are set to a negative number, or if a row is not present at all in
<TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
> for any particular table, the
corresponding values from <TT
CLASS="FILENAME"
>postgresql.conf</TT
> are used.
</P
><P
> There is not currently any support for making
<TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
> entries, except by doing
manual <TT
CLASS="COMMAND"
>INSERT</TT
>s into the catalog. This feature will be
improved in future releases, and it is likely that the catalog
definition will change.
</P
><DIV
CLASS="CAUTION"
><P
></P
><TABLE
CLASS="CAUTION"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Caution</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
> The contents of the <TT
CLASS="STRUCTNAME"
>pg_autovacuum</TT
> system
catalog are currently not saved in database dumps created by
the tools <TT
CLASS="COMMAND"
>pg_dump</TT
> and <TT
CLASS="COMMAND"
>pg_dumpall</TT
>.
If you want to preserve them across a dump/reload cycle, make sure you
dump the catalog manually.
</P
></TD
></TR
></TABLE
></DIV
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="multibyte.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="routine-reindex.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Character Set Support</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="admin.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Routine Reindexing</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>