Sophie

Sophie

distrib > Mandriva > 2006.0 > x86_64 > by-pkgid > b8f4049de69feba5041d49ed4382e582 > files > 30

postgresql-docs-8.0.11-0.1.20060mdk.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>On-line backup and point-in-time recovery (PITR)</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.0.11 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Backup and Restore"
HREF="backup.html"><LINK
REL="PREVIOUS"
TITLE="File system level backup"
HREF="backup-file.html"><LINK
REL="NEXT"
TITLE="Migration Between Releases"
HREF="migration.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-02-02T03:57:22"></HEAD
><BODY
CLASS="SECT1"
><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.0.11 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="backup-file.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="backup.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 22. Backup and Restore</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="migration.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="BACKUP-ONLINE"
>22.3. On-line backup and point-in-time recovery (PITR)</A
></H1
><A
NAME="AEN21482"
></A
><A
NAME="AEN21484"
></A
><A
NAME="AEN21486"
></A
><P
>   At all times, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> maintains a
   <I
CLASS="FIRSTTERM"
>write ahead log</I
> (WAL) in the <TT
CLASS="FILENAME"
>pg_xlog/</TT
>
   subdirectory of the cluster's data directory. The log describes
   every change made to the database's data files.  This log exists
   primarily for crash-safety purposes: if the system crashes, the
   database can be restored to consistency by <SPAN
CLASS="QUOTE"
>"replaying"</SPAN
> the
   log entries made since the last checkpoint.  However, the existence
   of the log makes it possible to use a third strategy for backing up
   databases: we can combine a file-system-level backup with backup of
   the WAL files.  If recovery is needed, we restore the backup and
   then replay from the backed-up WAL files to bring the backup up to
   current time.  This approach is more complex to administer than
   either of the previous approaches, but it has some significant
   benefits:
  <P
></P
></P><UL
><LI
><P
>     We do not need a perfectly consistent backup as the starting point.
     Any internal inconsistency in the backup will be corrected by log
     replay (this is not significantly different from what happens during
     crash recovery).  So we don't need file system snapshot capability,
     just <SPAN
CLASS="APPLICATION"
>tar</SPAN
> or a similar archiving tool.
    </P
></LI
><LI
><P
>     Since we can string together an indefinitely long sequence of WAL files
     for replay, continuous backup can be achieved simply by continuing to archive
     the WAL files.  This is particularly valuable for large databases, where
     it may not be convenient to take a full backup frequently.
    </P
></LI
><LI
><P
>     There is nothing that says we have to replay the WAL entries all the
     way to the end.  We could stop the replay at any point and have a
     consistent snapshot of the database as it was at that time.  Thus,
     this technique supports <I
CLASS="FIRSTTERM"
>point-in-time recovery</I
>: it is
     possible to restore the database to its state at any time since your base
     backup was taken.
    </P
></LI
><LI
><P
>     If we continuously feed the series of WAL files to another
     machine that has been loaded with the same base backup file, we
     have a <SPAN
CLASS="QUOTE"
>"hot standby"</SPAN
> system: at any point we can bring up
     the second machine and it will have a nearly-current copy of the
     database.
    </P
></LI
></UL
><P>
  </P
><P
>   As with the plain file-system-backup technique, this method can only
   support restoration of an entire database cluster, not a subset.
   Also, it requires a lot of archival storage: the base backup may be bulky,
   and a busy system will generate many megabytes of WAL traffic that
   have to be archived.  Still, it is the preferred backup technique in
   many situations where high reliability is needed.
  </P
><P
>   To recover successfully using an on-line backup, you need a continuous
   sequence of archived WAL files that extends back at least as far as the
   start time of your backup.  So to get started, you should set up and test
   your procedure for archiving WAL files <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>before</I
></SPAN
> you take your
   first base backup.  Accordingly, we first discuss the mechanics of
   archiving WAL files.
  </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="BACKUP-ARCHIVING-WAL"
>22.3.1. Setting up WAL archiving</A
></H2
><P
>    In an abstract sense, a running <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> system
    produces an indefinitely long sequence of WAL records.  The system
    physically divides this sequence into WAL <I
CLASS="FIRSTTERM"
>segment
    files</I
>, which are normally 16MB apiece (although the size can be
    altered when building <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>).  The segment
    files are given numeric names that reflect their position in the
    abstract WAL sequence.  When not using WAL archiving, the system
    normally creates just a few segment files and then
    <SPAN
CLASS="QUOTE"
>"recycles"</SPAN
> them by renaming no-longer-needed segment files
    to higher segment numbers.  It's assumed that a segment file whose
    contents precede the checkpoint-before-last is no longer of
    interest and can be recycled.
   </P
><P
>    When archiving WAL data, we want to capture the contents of each segment
    file once it is filled, and save that data somewhere before the segment
    file is recycled for reuse.  Depending on the application and the
    available hardware, there could be many different ways of <SPAN
CLASS="QUOTE"
>"saving
    the data somewhere"</SPAN
>: we could copy the segment files to an NFS-mounted
    directory on another machine, write them onto a tape drive (ensuring that
    you have a way of restoring the file with its original file name), or batch
    them together and burn them onto CDs, or something else entirely.  To
    provide the database administrator with as much flexibility as possible,
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> tries not to make any assumptions about how 
    the archiving will be done.  Instead, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> lets
    the administrator specify a shell command to be executed to copy a
    completed segment file to wherever it needs to go.  The command could be
    as simple as a <SPAN
CLASS="APPLICATION"
>cp</SPAN
>, or it could invoke a complex shell
    script &mdash; it's all up to you.
   </P
><P
>    The shell command to use is specified by the <A
HREF="runtime-config.html#GUC-ARCHIVE-COMMAND"
>archive_command</A
> configuration parameter, which in practice
    will always be placed in the <TT
CLASS="FILENAME"
>postgresql.conf</TT
> file.
    In this string,
    any <TT
CLASS="LITERAL"
>%p</TT
> is replaced by the absolute path of the file to
    archive, while any <TT
CLASS="LITERAL"
>%f</TT
> is replaced by the file name only.
    Write <TT
CLASS="LITERAL"
>%%</TT
> if you need to embed an actual <TT
CLASS="LITERAL"
>%</TT
>
    character in the command.  The simplest useful command is something
    like
</P><PRE
CLASS="PROGRAMLISTING"
>archive_command = 'cp -i %p /mnt/server/archivedir/%f &lt;/dev/null'</PRE
><P>
    which will copy archivable WAL segments to the directory
    <TT
CLASS="FILENAME"
>/mnt/server/archivedir</TT
>.  (This is an example, not a 
    recommendation, and may not work on all platforms.)
   </P
><P
>    The archive command will be executed under the ownership of the same
    user that the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> server is running as.  Since
    the series of WAL files being archived contains effectively everything
    in your database, you will want to be sure that the archived data is
    protected from prying eyes; for example, archive into a directory that
    does not have group or world read access.
   </P
><P
>    It is important that the archive command return zero exit status if and
    only if it succeeded.  Upon getting a zero result,
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> will assume that the WAL segment file has been
    successfully archived, and will remove or recycle it.
    However, a nonzero status tells
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> that the file was not archived; it will try
    again periodically until it succeeds.
   </P
><P
>    The archive command should generally be designed to refuse to overwrite
    any pre-existing archive file.  This is an important safety feature to
    preserve the integrity of your archive in case of administrator error
    (such as sending the output of two different servers to the same archive
    directory).
    It is advisable to test your proposed archive command to ensure that it
    indeed does not overwrite an existing file, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>and that it returns
    nonzero status in this case</I
></SPAN
>.  We have found that <TT
CLASS="LITERAL"
>cp -i</TT
> does
    this correctly on some platforms but not others.  If the chosen command
    does not itself handle this case correctly, you should add a command
    to test for pre-existence of the archive file.  For example, something
    like
</P><PRE
CLASS="PROGRAMLISTING"
>archive_command = 'test ! -f .../%f &amp;&amp; cp %p .../%f'</PRE
><P>
    works correctly on most Unix variants.
   </P
><P
>    While designing your archiving setup, consider what will happen if
    the archive command fails repeatedly because some aspect requires 
    operator intervention or the archive runs out of space. For example, this
    could occur if you write to tape without an autochanger; when the tape 
    fills, nothing further can be archived until the tape is swapped.
    You should ensure that any error condition or request to a human operator
    is reported appropriately so that the situation can be 
    resolved relatively quickly. The <TT
CLASS="FILENAME"
>pg_xlog/</TT
> directory will
    continue to fill with WAL segment files until the situation is resolved.
   </P
><P
>    The speed of the archiving command is not important, so long as it can keep up
    with the average rate at which your server generates WAL data.  Normal
    operation continues even if the archiving process falls a little behind.
    If archiving falls significantly behind, this will increase the amount of
    data that would be lost in the event of a disaster. It will also mean that
    the <TT
CLASS="FILENAME"
>pg_xlog/</TT
> directory will contain large numbers of
    not-yet-archived segment files, which could eventually exceed available
    disk space. You are advised to monitor the archiving process to ensure that
    it is working as you intend.
   </P
><P
>    If you are concerned about being able to recover right up to the
    current instant, you may want to take additional steps to ensure that
    the current, partially-filled WAL segment is also copied someplace.
    This is particularly important if your server generates only little WAL
    traffic (or has slack periods where it does so), since it could take a
    long time before a WAL segment file is completely filled and ready to
    archive.  One possible way to handle this is to set up a
    <SPAN
CLASS="APPLICATION"
>cron</SPAN
> job that periodically (once a minute, perhaps)
    identifies the current WAL segment file and saves it someplace safe.
    Then the combination of the archived WAL segments and the saved current
    segment will be enough to ensure you can always restore to within a
    minute of current time.  This behavior is not presently built into
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> because we did not want to complicate the
    definition of the <A
HREF="runtime-config.html#GUC-ARCHIVE-COMMAND"
>archive_command</A
> by requiring it
    to keep track of successively archived, but different, copies of the
    same WAL file.  The <A
HREF="runtime-config.html#GUC-ARCHIVE-COMMAND"
>archive_command</A
> is only
    invoked on completed WAL segments. Except in the case of retrying a
    failure, it will be called only once for any given file name.
   </P
><P
>    In writing your archive command, you should assume that the filenames to
    be archived may be up to 64 characters long and may contain any
    combination of ASCII letters, digits, and dots.  It is not necessary to
    remember the original full path (<TT
CLASS="LITERAL"
>%p</TT
>) but it is necessary to
    remember the file name (<TT
CLASS="LITERAL"
>%f</TT
>).
   </P
><P
>    Note that although WAL archiving will allow you to restore any
    modifications made to the data in your <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> database
    it will not restore changes made to configuration files (that is,
    <TT
CLASS="FILENAME"
>postgresql.conf</TT
>, <TT
CLASS="FILENAME"
>pg_hba.conf</TT
> and
    <TT
CLASS="FILENAME"
>pg_ident.conf</TT
>), since those are edited manually rather
    than through SQL operations.
    You may wish to keep the configuration files in a location that will
    be backed up by your regular file system backup procedures.  See
    <A
HREF="runtime-config.html#RUNTIME-CONFIG-FILE-LOCATIONS"
>Section 16.4.1</A
> for how to relocate the
    configuration files.
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="BACKUP-BASE-BACKUP"
>22.3.2. Making a Base Backup</A
></H2
><P
>    The procedure for making a base backup is relatively simple:
  <P
></P
></P><OL
TYPE="1"
><LI
><P
>     Ensure that WAL archiving is enabled and working.
    </P
></LI
><LI
><P
>     Connect to the database as a superuser, and issue the command
</P><PRE
CLASS="PROGRAMLISTING"
>SELECT pg_start_backup('label');</PRE
><P>
     where <TT
CLASS="LITERAL"
>label</TT
> is any string you want to use to uniquely
     identify this backup operation.  (One good practice is to use the
     full path where you intend to put the backup dump file.)
     <CODE
CLASS="FUNCTION"
>pg_start_backup</CODE
> creates a <I
CLASS="FIRSTTERM"
>backup label</I
> file,
     called <TT
CLASS="FILENAME"
>backup_label</TT
>, in the cluster directory with
     information about your backup.
    </P
><P
>     It does not matter which database within the cluster you connect to to 
     issue this command.  You can ignore the result returned by the function;
     but if it reports an error, deal with that before proceeding.
    </P
></LI
><LI
><P
>     Perform the backup, using any convenient file-system-backup tool
     such as <SPAN
CLASS="APPLICATION"
>tar</SPAN
> or <SPAN
CLASS="APPLICATION"
>cpio</SPAN
>.  It is neither
     necessary nor desirable to stop normal operation of the database
     while you do this.
    </P
></LI
><LI
><P
>     Again connect to the database as a superuser, and issue the command
</P><PRE
CLASS="PROGRAMLISTING"
>SELECT pg_stop_backup();</PRE
><P>
     This should return successfully.
    </P
></LI
><LI
><P
>     Once the WAL segment files used during the backup are archived as part
     of normal database activity, you are done.
    </P
></LI
></OL
><P>
   </P
><P
>    It is not necessary to be very concerned about the amount of time elapsed
    between <CODE
CLASS="FUNCTION"
>pg_start_backup</CODE
> and the start of the actual backup,
    nor between the end of the backup and <CODE
CLASS="FUNCTION"
>pg_stop_backup</CODE
>; a
    few minutes' delay won't hurt anything.  You
    must however be quite sure that these operations are carried out in
    sequence and do not overlap.
   </P
><P
>    Be certain that your backup dump includes all of the files underneath
    the database cluster directory (e.g., <TT
CLASS="FILENAME"
>/usr/local/pgsql/data</TT
>).
    If you are using tablespaces that do not reside underneath this directory,
    be careful to include them as well (and be sure that your backup dump
    archives symbolic links as links, otherwise the restore will mess up
    your tablespaces).
   </P
><P
>    You may, however, omit from the backup dump the files within the
    <TT
CLASS="FILENAME"
>pg_xlog/</TT
> subdirectory of the cluster directory.  This
    slight complication is worthwhile because it reduces the risk
    of mistakes when restoring.  This is easy to arrange if
    <TT
CLASS="FILENAME"
>pg_xlog/</TT
> is a symbolic link pointing to someplace outside
    the cluster directory, which is a common setup anyway for performance
    reasons.
   </P
><P
>    To make use of this backup, you will need to keep around all the WAL
    segment files generated during and after the file system backup.
    To aid you in doing this, the <CODE
CLASS="FUNCTION"
>pg_stop_backup</CODE
> function
    creates a <I
CLASS="FIRSTTERM"
>backup history file</I
> that is immediately
    stored into the WAL archive area. This file is named after the first
    WAL segment file that you need to have to make use of the backup.
    For example, if the starting WAL file is
    <TT
CLASS="LITERAL"
>0000000100001234000055CD</TT
> the backup history file will be
    named something like
    <TT
CLASS="LITERAL"
>0000000100001234000055CD.007C9330.backup</TT
>. (The second
    number in the file name stands for an exact position within the WAL
    file, and can ordinarily be ignored.) Once you have safely archived
    the file system backup and the WAL segment files used during the
    backup (as specified in the backup history file), all archived WAL
    segments with names numerically less are no longer needed to recover
    the file system backup and may be deleted. However, you should
    consider keeping several backup sets to be absolutely certain that
    you are can recover your data. Keep in mind that only completed WAL
    segment files are archived, so there will be delay between running
    <CODE
CLASS="FUNCTION"
>pg_stop_backup</CODE
> and the archiving of all WAL segment
    files needed to make the file system backup consistent.
   </P
><P
>    The backup history file is just a small text file. It contains the
    label string you gave to <CODE
CLASS="FUNCTION"
>pg_start_backup</CODE
>, as well as
    the starting and ending times of the backup. If you used the label
    to identify where the associated dump file is kept, then the
    archived history file is enough to tell you which dump file to
    restore, should you need to do so.
   </P
><P
>    Since you have to keep around all the archived WAL files back to your
    last base backup, the interval between base backups should usually be
    chosen based on how much storage you want to expend on archived WAL
    files.  You should also consider how long you are prepared to spend
    recovering, if recovery should be necessary &mdash; the system will have to
    replay all those WAL segments, and that could take awhile if it has
    been a long time since the last base backup.
   </P
><P
>    It's also worth noting that the <CODE
CLASS="FUNCTION"
>pg_start_backup</CODE
> function
    makes a file named <TT
CLASS="FILENAME"
>backup_label</TT
> in the database cluster
    directory, which is then removed again by <CODE
CLASS="FUNCTION"
>pg_stop_backup</CODE
>.
    This file will of course be archived as a part of your backup dump file.
    The backup label file includes the label string you gave to
    <CODE
CLASS="FUNCTION"
>pg_start_backup</CODE
>, as well as the time at which
    <CODE
CLASS="FUNCTION"
>pg_start_backup</CODE
> was run, and the name of the starting WAL
    file.  In case of confusion it will
    therefore be possible to look inside a backup dump file and determine
    exactly which backup session the dump file came from.
   </P
><P
>    It is also possible to make a backup dump while the postmaster is
    stopped.  In this case, you obviously cannot use
    <CODE
CLASS="FUNCTION"
>pg_start_backup</CODE
> or <CODE
CLASS="FUNCTION"
>pg_stop_backup</CODE
>, and
    you will therefore be left to your own devices to keep track of which
    backup dump is which and how far back the associated WAL files go.
    It is generally better to follow the on-line backup procedure above.
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="BACKUP-PITR-RECOVERY"
>22.3.3. Recovering with an On-line Backup</A
></H2
><P
>    Okay, the worst has happened and you need to recover from your backup.
    Here is the procedure:
  <P
></P
></P><OL
TYPE="1"
><LI
><P
>     Stop the postmaster, if it's running.
    </P
></LI
><LI
><P
>     If you have the space to do so,
     copy the whole cluster data directory and any tablespaces to a temporary 
     location in case you need them later. Note that this precaution will
     require that you have enough free space on your system to hold two
     copies of your existing database. If you do not have enough space, 
     you need at the least to copy the contents of the <TT
CLASS="FILENAME"
>pg_xlog</TT
>
     subdirectory of the cluster data directory, as it may contain logs which
     were not archived before the system went down.
    </P
></LI
><LI
><P
>     Clean out all existing files and subdirectories under the cluster data
     directory and under the root directories of any tablespaces you are using.
    </P
></LI
><LI
><P
>     Restore the database files from your backup dump.  Be careful that they
     are restored with the right ownership (the database system user, not
     root!) and with the right permissions.  If you are using tablespaces,
     you may want to verify that the symbolic links in <TT
CLASS="FILENAME"
>pg_tblspc/</TT
>
     were correctly restored.
    </P
></LI
><LI
><P
>     Remove any files present in <TT
CLASS="FILENAME"
>pg_xlog/</TT
>; these came from the
     backup dump and are therefore probably obsolete rather than current.
     If you didn't archive <TT
CLASS="FILENAME"
>pg_xlog/</TT
> at all, then re-create it,
     and be sure to re-create the subdirectory
    <TT
CLASS="FILENAME"
>pg_xlog/archive_status/</TT
> as well.
    </P
></LI
><LI
><P
>     If you had unarchived WAL segment files that you saved in step 2,
     copy them into <TT
CLASS="FILENAME"
>pg_xlog/</TT
>.  (It is best to copy them,
     not move them, so that you still have the unmodified files if a
     problem occurs and you have to start over.)
    </P
></LI
><LI
><P
>     Create a recovery command file <TT
CLASS="FILENAME"
>recovery.conf</TT
> in the cluster
     data directory (see <A
HREF="backup-online.html#RECOVERY-CONFIG-SETTINGS"
>Recovery Settings</A
>). You may 
     also want to temporarily modify <TT
CLASS="FILENAME"
>pg_hba.conf</TT
> to prevent 
     ordinary users from connecting until you are sure the recovery has worked.
    </P
></LI
><LI
><P
>     Start the postmaster.  The postmaster will go into recovery mode and
     proceed to read through the archived WAL files it needs.  Upon completion
     of the recovery process, the postmaster will rename
     <TT
CLASS="FILENAME"
>recovery.conf</TT
> to <TT
CLASS="FILENAME"
>recovery.done</TT
> (to prevent
     accidentally re-entering recovery mode in case of a crash later) and then
     commence normal database operations.
    </P
></LI
><LI
><P
>     Inspect the contents of the database to ensure you have recovered to
     where you want to be.  If not, return to step 1.  If all is well,
     let in your users by restoring <TT
CLASS="FILENAME"
>pg_hba.conf</TT
> to normal.
    </P
></LI
></OL
><P>
   </P
><P
>    The key part of all this is to set up a recovery command file that
    describes how you want to recover and how far the recovery should
    run.  You can use <TT
CLASS="FILENAME"
>recovery.conf.sample</TT
> (normally
    installed in the installation <TT
CLASS="FILENAME"
>share/</TT
> directory) as a
    prototype.  The one thing that you absolutely must specify in
    <TT
CLASS="FILENAME"
>recovery.conf</TT
> is the <TT
CLASS="VARNAME"
>restore_command</TT
>,
    which tells <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> how to get back archived
    WAL file segments.  Like the <TT
CLASS="VARNAME"
>archive_command</TT
>, this is
    a shell command string.  It may contain <TT
CLASS="LITERAL"
>%f</TT
>, which is
    replaced by the name of the desired log file, and <TT
CLASS="LITERAL"
>%p</TT
>,
    which is replaced by the absolute path to copy the log file to.
    Write <TT
CLASS="LITERAL"
>%%</TT
> if you need to embed an actual <TT
CLASS="LITERAL"
>%</TT
>
    character in the command.  The simplest useful command is
    something like
</P><PRE
CLASS="PROGRAMLISTING"
>restore_command = 'cp /mnt/server/archivedir/%f %p'</PRE
><P>
    which will copy previously archived WAL segments from the directory
    <TT
CLASS="FILENAME"
>/mnt/server/archivedir</TT
>.  You could of course use something
    much more complicated, perhaps even a shell script that requests the
    operator to mount an appropriate tape.
   </P
><P
>    It is important that the command return nonzero exit status on failure.
    The command <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>will</I
></SPAN
> be asked for log files that are not present
    in the archive; it must return nonzero when so asked.  This is not an
    error condition.  Be aware also that the base name of the <TT
CLASS="LITERAL"
>%p</TT
>
    path will be different from <TT
CLASS="LITERAL"
>%f</TT
>; do not expect them to be
    interchangeable.
   </P
><P
>    WAL segments that cannot be found in the archive will be sought in
    <TT
CLASS="FILENAME"
>pg_xlog/</TT
>; this allows use of recent un-archived segments.
    However segments that are available from the archive will be used in
    preference to files in <TT
CLASS="FILENAME"
>pg_xlog/</TT
>.  The system will not
    overwrite the existing contents of <TT
CLASS="FILENAME"
>pg_xlog/</TT
> when retrieving
    archived files.
   </P
><P
>    Normally, recovery will proceed through all available WAL segments,
    thereby restoring the database to the current point in time (or as
    close as we can get given the available WAL segments).  But if you want
    to recover to some previous point in time (say, right before the junior
    DBA dropped your main transaction table), just specify the required
    stopping point in <TT
CLASS="FILENAME"
>recovery.conf</TT
>.  You can specify the stop
    point, known as the <SPAN
CLASS="QUOTE"
>"recovery target"</SPAN
>, either by date/time or
    by completion of a specific transaction ID.  As of this writing only
    the date/time option is very usable, since there are no tools to help
    you identify with any accuracy which transaction ID to use.
   </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>      The stop point must be after the ending time of the base backup (the
      time of <CODE
CLASS="FUNCTION"
>pg_stop_backup</CODE
>).  You cannot use a base backup
      to recover to a time when that backup was still going on.  (To
      recover to such a time, you must go back to your previous base backup
      and roll forward from there.)
     </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="RECOVERY-CONFIG-SETTINGS"
>22.3.3.1. Recovery Settings</A
></H3
><P
>      These settings can only be made in the <TT
CLASS="FILENAME"
>recovery.conf</TT
>
      file, and apply only for the duration of the recovery. They must be
      reset for any subsequent recovery you wish to perform. They cannot be
      changed once recovery has begun.
     </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><A
NAME="RESTORE-COMMAND"
></A
><TT
CLASS="VARNAME"
>restore_command</TT
> (<TT
CLASS="TYPE"
>string</TT
>)</DT
><DD
><P
>        The shell command to execute to retrieve an archived segment of
        the WAL file series. This parameter is required.
        Any <TT
CLASS="LITERAL"
>%f</TT
> in the string is
        replaced by the name of the file to retrieve from the archive,
        and any <TT
CLASS="LITERAL"
>%p</TT
> is replaced by the absolute path to copy
        it to on the server.
        Write <TT
CLASS="LITERAL"
>%%</TT
> to embed an actual <TT
CLASS="LITERAL"
>%</TT
> character
        in the command. 
       </P
><P
>        It is important for the command to return a zero exit status if and
        only if it succeeds.  The command <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>will</I
></SPAN
> be asked for file
        names that are not present in the archive; it must return nonzero
        when so asked.  Examples:
</P><PRE
CLASS="PROGRAMLISTING"
>restore_command = 'cp /mnt/server/archivedir/%f "%p"'
restore_command = 'copy /mnt/server/archivedir/%f "%p"'  # Windows</PRE
><P>
       </P
></DD
><DT
><A
NAME="RECOVERY-TARGET-TIME"
></A
><TT
CLASS="VARNAME"
>recovery_target_time</TT
> 
           (<TT
CLASS="TYPE"
>timestamp</TT
>)</DT
><DD
><P
>        This parameter specifies the time stamp up to which recovery
        will proceed.
        At most one of <TT
CLASS="VARNAME"
>recovery_target_time</TT
> and
        <A
HREF="backup-online.html#RECOVERY-TARGET-XID"
>recovery_target_xid</A
> can be specified.
        The default is to recover to the end of the WAL log.
        The precise stopping point is also influenced by 
        <A
HREF="backup-online.html#RECOVERY-TARGET-INCLUSIVE"
>recovery_target_inclusive</A
>.
       </P
></DD
><DT
><A
NAME="RECOVERY-TARGET-XID"
></A
><TT
CLASS="VARNAME"
>recovery_target_xid</TT
> (<TT
CLASS="TYPE"
>string</TT
>)</DT
><DD
><P
>        This parameter specifies the transaction ID up to which recovery
        will proceed. Keep in mind 
        that while transaction IDs are assigned sequentially at transaction 
        start, transactions can complete in a different numeric order.
        The transactions that will be recovered are those that committed
        before (and optionally including) the specified one.
        At most one of <TT
CLASS="VARNAME"
>recovery_target_xid</TT
> and
        <A
HREF="backup-online.html#RECOVERY-TARGET-TIME"
>recovery_target_time</A
> can be specified.
        The default is to recover to the end of the WAL log.
        The precise stopping point is also influenced by 
        <A
HREF="backup-online.html#RECOVERY-TARGET-INCLUSIVE"
>recovery_target_inclusive</A
>.
       </P
></DD
><DT
><A
NAME="RECOVERY-TARGET-INCLUSIVE"
></A
><TT
CLASS="VARNAME"
>recovery_target_inclusive</TT
> 
        (<TT
CLASS="TYPE"
>boolean</TT
>)</DT
><DD
><P
>        Specifies whether we stop just after the specified recovery target
        (<TT
CLASS="LITERAL"
>true</TT
>), or just before the recovery target 
        (<TT
CLASS="LITERAL"
>false</TT
>).
        Applies to both <A
HREF="backup-online.html#RECOVERY-TARGET-TIME"
>recovery_target_time</A
>
        and <A
HREF="backup-online.html#RECOVERY-TARGET-XID"
>recovery_target_xid</A
>, whichever one is
        specified for this recovery.  This indicates whether transactions
        having exactly the target commit time or ID, respectively, will
        be included in the recovery.  Default is <TT
CLASS="LITERAL"
>true</TT
>.
       </P
></DD
><DT
><A
NAME="RECOVERY-TARGET-TIMELINE"
></A
><TT
CLASS="VARNAME"
>recovery_target_timeline</TT
> 
        (<TT
CLASS="TYPE"
>string</TT
>)</DT
><DD
><P
>        Specifies recovering into a particular timeline.  The default is
        to recover along the same timeline that was current when the
        base backup was taken.  You would only need to set this parameter
        in complex re-recovery situations, where you need to return to
        a state that itself was reached after a point-in-time recovery.
        See <A
HREF="backup-online.html#BACKUP-TIMELINES"
>Section 22.3.4</A
> for discussion.
       </P
></DD
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="BACKUP-TIMELINES"
>22.3.4. Timelines</A
></H2
><A
NAME="AEN21722"
></A
><P
>    The ability to restore the database to a previous point in time creates
    some complexities that are akin to science-fiction stories about time
    travel and parallel universes.  In the original history of the database,
    perhaps you dropped a critical table at 5:15PM on Tuesday evening.
    Unfazed, you get out your backup, restore to the point-in-time 5:14PM
    Tuesday evening, and are up and running.  In <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>this</I
></SPAN
> history of
    the database universe, you never dropped the table at all.  But suppose
    you later realize this wasn't such a great idea after all, and would like
    to return to some later point in the original history.  You won't be able
    to if, while your database was up-and-running, it overwrote some of the
    sequence of WAL segment files that led up to the time you now wish you
    could get back to.  So you really want to distinguish the series of
    WAL records generated after you've done a point-in-time recovery from
    those that were generated in the original database history.
   </P
><P
>    To deal with these problems, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> has a notion
    of <I
CLASS="FIRSTTERM"
>timelines</I
>.  Each time you recover to a point-in-time
    earlier than the end of the WAL sequence, a new timeline is created
    to identify the series of WAL records generated after that recovery.
    (If recovery proceeds all the way to the end of WAL, however, we do not
    start a new timeline: we just extend the existing one.)  The timeline
    ID number is part of WAL segment file names, and so a new timeline does
    not overwrite the WAL data generated by previous timelines.  It is
    in fact possible to archive many different timelines.  While that might
    seem like a useless feature, it's often a lifesaver.  Consider the
    situation where you aren't quite sure what point-in-time to recover to,
    and so have to do several point-in-time recoveries by trial and error
    until you find the best place to branch off from the old history.  Without
    timelines this process would soon generate an unmanageable mess.  With
    timelines, you can recover to <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>any</I
></SPAN
> prior state, including
    states in timeline branches that you later abandoned.
   </P
><P
>    Each time a new timeline is created, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> creates
    a <SPAN
CLASS="QUOTE"
>"timeline history"</SPAN
> file that shows which timeline it branched
    off from and when.  These history files are necessary to allow the system
    to pick the right WAL segment files when recovering from an archive that
    contains multiple timelines.  Therefore, they are archived into the WAL
    archive area just like WAL segment files.  The history files are just
    small text files, so it's cheap and appropriate to keep them around
    indefinitely (unlike the segment files which are large).  You can, if
    you like, add comments to a history file to make your own notes about
    how and why this particular timeline came to be.  Such comments will be
    especially valuable when you have a thicket of different timelines as
    a result of experimentation.
   </P
><P
>    The default behavior of recovery is to recover along the same timeline
    that was current when the base backup was taken.  If you want to recover
    into some child timeline (that is, you want to return to some state that
    was itself generated after a recovery attempt), you need to specify the
    target timeline ID in <TT
CLASS="FILENAME"
>recovery.conf</TT
>.  You cannot recover into
    timelines that branched off earlier than the base backup.
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="BACKUP-ONLINE-CAVEATS"
>22.3.5. Caveats</A
></H2
><P
>    At this writing, there are several limitations of the on-line backup
    technique.  These will probably be fixed in future releases:

  <P
></P
></P><UL
><LI
><P
>     Operations on non-B-tree indexes (hash, R-tree, and GiST indexes) are
     not presently WAL-logged, so replay will not update these index types.
     The recommended workaround is to manually <TT
CLASS="COMMAND"
>REINDEX</TT
> each
     such index after completing a recovery operation.
    </P
></LI
><LI
><P
>     If a <TT
CLASS="COMMAND"
>CREATE DATABASE</TT
> command is executed while a base
     backup is being taken, and then the template database that the
     <TT
CLASS="COMMAND"
>CREATE DATABASE</TT
> copied is modified while the base backup
     is still in progress, it is possible that recovery will cause those
     modifications to be propagated into the created database as well.
     This is of course undesirable.  To avoid this risk, it is best not to
     modify any template databases while taking a base backup.
    </P
></LI
><LI
><P
>     <TT
CLASS="COMMAND"
>CREATE TABLESPACE</TT
> commands are WAL-logged with the literal
     absolute path, and will therefore be replayed as tablespace creations
     with the same absolute path.  This might be undesirable if the log is
     being replayed on a different machine.  It can be dangerous even if
     the log is being replayed on the same machine, but into a new data
     directory: the replay will still overwrite the contents of the original
     tablespace.  To avoid potential gotchas of this sort, the best practice
     is to take a new base backup after creating or dropping tablespaces.
    </P
></LI
></UL
><P>
   </P
><P
>    It should also be noted that the present <ACRONYM
CLASS="ACRONYM"
>WAL</ACRONYM
>
    format is extremely bulky since it includes many disk page
    snapshots.  This is appropriate for crash recovery purposes,
    since we may need to fix partially-written disk pages.  It is not
    necessary to store so many page copies for PITR operations, however.
    An area for future development is to compress archived WAL data by
    removing unnecessary page copies.  In the meantime, administrators
    may wish to reduce the number of page snapshots included in WAL by
    increasing the checkpoint interval parameters as much as feasible.
   </P
></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="backup-file.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="migration.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>File system level backup</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="backup.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Migration Between Releases</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>