successfully archived, and will remove or recycle it. However, a nonzero
status tells <productname>PostgreSQL</productname> that the file was not archived;
it will try again periodically until it succeeds.
</para>
<para>
Another way to archive is to use a custom archive module as the
<varname>archive_library</varname>. Since such modules are written in
<literal>C</literal>, creating your own may require considerably more effort
than writing a shell command. However, archive modules can be more
performant than archiving via shell, and they will have access to many
useful server resources. For more information about archive modules, see
<xref linkend="archive-modules"/>.
</para>
<para>
When the archive command is terminated by a signal (other than
<systemitem>SIGTERM</systemitem> that is used as part of a server
shutdown) or an error by the shell with an exit status greater than
125 (such as command not found), or if the archive function emits an
<literal>ERROR</literal> or <literal>FATAL</literal>, the archiver process
aborts and gets restarted by the postmaster. In such cases, the failure is
not reported in <xref linkend="pg-stat-archiver-view"/>.
</para>
<para>
Archive commands and libraries 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 library to ensure
that it does not overwrite an existing file.
</para>
<para>
In rare cases, <productname>PostgreSQL</productname> may attempt to
re-archive a WAL file that was previously archived. For example, if the
system crashes before the server makes a durable record of archival
success, the server will attempt to archive the file again after
restarting (provided archiving is still enabled). When an archive command or library
encounters a pre-existing file, it should return a zero status or <literal>true</literal>, respectively,
if the WAL file has identical contents to the pre-existing archive and the
pre-existing archive is fully persisted to storage. If a pre-existing
file contains different contents than the WAL file being archived, the
archive command or library <emphasis>must</emphasis> return a nonzero status or
<literal>false</literal>, respectively.
</para>
<para>
The example command above for Unix avoids overwriting a pre-existing archive
by including a separate
<command>test</command> step. On some Unix platforms, <command>cp</command> has
switches such as <option>-i</option> that can be used to do the same thing
less verbosely, but you should not rely on these without verifying that
the right exit status is returned. (In particular, GNU <command>cp</command>
will return status zero when <option>-i</option> is used and the target file
already exists, which is <emphasis>not</emphasis> the desired behavior.)
</para>
<para>
While designing your archiving setup, consider what will happen if
the archive command or library 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 reasonably quickly. The <filename>pg_wal/</filename> directory will
continue to fill with WAL segment files until the situation is resolved.
(If the file system containing <filename>pg_wal/</filename> fills up,
<productname>PostgreSQL</productname>