1
<!-- doc/src/sgml/pgstandby.sgml -->
4
<title>pg_standby</title>
6
<indexterm zone="pgstandby">
7
<primary>pg_standby</primary>
11
<application>pg_standby</> supports creation of a <quote>warm standby</>
12
database server. It is designed to be a production-ready program, as well
13
as a customizable template should you require specific modifications.
17
<application>pg_standby</> is designed to be a waiting
18
<varname>restore_command</>, which is needed to turn a standard
19
archive recovery into a warm standby operation. Other
20
configuration is required as well, all of which is described in the main
21
server manual (see <xref linkend="warm-standby">).
25
<application>pg_standby</application> features include:
30
Written in C, so very portable and easy to install
35
Easy-to-modify source code, with specifically designated
36
sections to modify for your own needs
41
Already tested on Linux and Windows
50
To configure a standby
51
server to use <application>pg_standby</>, put this into its
52
<filename>recovery.conf</filename> configuration file:
54
restore_command = 'pg_standby <replaceable>archiveDir</> %f %p %r'
56
where <replaceable>archiveDir</> is the directory from which WAL segment
57
files should be restored.
60
The full syntax of <application>pg_standby</>'s command line is
62
pg_standby <optional> <replaceable>option</> ... </optional> <replaceable>archivelocation</> <replaceable>nextwalfile</> <replaceable>xlogfilepath</> <optional> <replaceable>restartwalfile</> </optional>
64
When used within <varname>restore_command</>, the <literal>%f</> and
65
<literal>%p</> macros should be specified for <replaceable>nextwalfile</>
66
and <replaceable>xlogfilepath</> respectively, to provide the actual file
67
and path required for the restore.
70
If <replaceable>restartwalfile</> is specified, normally by using the
71
<literal>%r</literal> macro, then all WAL files logically preceding this
72
file will be removed from <replaceable>archivelocation</>. This minimizes
73
the number of files that need to be retained, while preserving
74
crash-restart capability. Use of this parameter is appropriate if the
75
<replaceable>archivelocation</> is a transient staging area for this
76
particular standby server, but <emphasis>not</> when the
77
<replaceable>archivelocation</> is intended as a long-term WAL archive area.
80
<application>pg_standby</application> assumes that
81
<replaceable>archivelocation</> is a directory readable by the
82
server-owning user. If <replaceable>restartwalfile</> (or <literal>-k</>)
84
the <replaceable>archivelocation</> directory must be writable too.
87
There are two ways to fail over to a <quote>warm standby</> database server
88
when the master server fails:
92
<term>Smart Failover</term>
95
In smart failover, the server is brought up after applying all WAL
96
files available in the archive. This results in zero data loss, even if
97
the standby server has fallen behind, but if there is a lot of
98
unapplied WAL it can be a long time before the standby server becomes
99
ready. To trigger a smart failover, create a trigger file containing
100
the word <literal>smart</>, or just create it and leave it empty.
105
<term>Fast Failover</term>
108
In fast failover, the server is brought up immediately. Any WAL files
109
in the archive that have not yet been applied will be ignored, and
110
all transactions in those files are lost. To trigger a fast failover,
111
create a trigger file and write the word <literal>fast</> into it.
112
<application>pg_standby</> can also be configured to execute a fast
113
failover automatically if no new WAL file appears within a defined
124
<title><application>pg_standby</> Options</title>
127
<application>pg_standby</application> accepts the following command-line arguments:
132
<term><option>-c</option></term>
135
Use <literal>cp</> or <literal>copy</> command to restore WAL files
136
from archive. This is the only supported behavior so this option is useless.
142
<term><option>-d</option></term>
145
Print lots of debug logging output on <filename>stderr</>.
151
<term><option>-k</option></term>
154
Remove files from <replaceable>archivelocation</replaceable> so that
155
no more than this many WAL files before the current one are kept in the
156
archive. Zero (the default) means not to remove any files from
157
<replaceable>archivelocation</replaceable>.
158
This parameter will be silently ignored if
159
<replaceable>restartwalfile</replaceable> is specified, since that
160
specification method is more accurate in determining the correct
161
archive cut-off point.
162
Use of this parameter is <emphasis>deprecated</> as of
163
<productname>PostgreSQL</> 8.3; it is safer and more efficient to
164
specify a <replaceable>restartwalfile</replaceable> parameter. A too
165
small setting could result in removal of files that are still needed
166
for a restart of the standby server, while a too large setting wastes
173
<term><option>-r</option> <replaceable>maxretries</></term>
176
Set the maximum number of times to retry the copy command if
177
it fails (default 3). After each failure, we wait for
178
<replaceable>sleeptime</> * <replaceable>num_retries</>
179
so that the wait time increases progressively. So by default,
180
we will wait 5 secs, 10 secs, then 15 secs before reporting
181
the failure back to the standby server. This will be
182
interpreted as end of recovery and the standby will come
183
up fully as a result.
189
<term><option>-s</option> <replaceable>sleeptime</></term>
192
Set the number of seconds (up to 60, default 5) to sleep between
193
tests to see if the WAL file to be restored is available in
194
the archive yet. The default setting is not necessarily
195
recommended; consult <xref linkend="warm-standby"> for discussion.
201
<term><option>-t</option> <replaceable>triggerfile</></term>
204
Specify a trigger file whose presence should cause failover.
205
It is recommended that you use a structured file name to
206
avoid confusion as to which server is being triggered
207
when multiple servers exist on the same system; for example
208
<filename>/tmp/pgsql.trigger.5432</>.
214
<term><option>-w</option> <replaceable>maxwaittime</></term>
217
Set the maximum number of seconds to wait for the next WAL file,
218
after which a fast failover will be performed.
219
A setting of zero (the default) means wait forever.
220
The default setting is not necessarily recommended;
221
consult <xref linkend="warm-standby"> for discussion.
232
<title>Examples</title>
234
<para>On Linux or Unix systems, you might use:
237
archive_command = 'cp %p .../archive/%f'
239
restore_command = 'pg_standby -d -s 2 -t /tmp/pgsql.trigger.5442 .../archive %f %p %r 2>>standby.log'
241
recovery_end_command = 'rm -f /tmp/pgsql.trigger.5442'
243
where the archive directory is physically located on the standby server,
244
so that the <varname>archive_command</> is accessing it across NFS,
245
but the files are local to the standby (enabling use of <literal>ln</>).
250
produce debugging output in <filename>standby.log</>
255
sleep for 2 seconds between checks for next WAL file availability
260
stop waiting only when a trigger file called
261
<filename>/tmp/pgsql.trigger.5442</> appears,
262
and perform failover according to its content
267
remove the trigger file when recovery ends
272
remove no-longer-needed files from the archive directory
278
<para>On Windows, you might use:
281
archive_command = 'copy %p ...\\archive\\%f'
283
restore_command = 'pg_standby -d -s 5 -t C:\pgsql.trigger.5442 ...\archive %f %p %r 2>>standby.log'
285
recovery_end_command = 'del C:\pgsql.trigger.5442'
287
Note that backslashes need to be doubled in the
288
<varname>archive_command</>, but <emphasis>not</emphasis> in the
289
<varname>restore_command</> or <varname>recovery_end_command</>.
294
use the <literal>copy</> command to restore WAL files from archive
299
produce debugging output in <filename>standby.log</>
304
sleep for 5 seconds between checks for next WAL file availability
309
stop waiting only when a trigger file called
310
<filename>C:\pgsql.trigger.5442</> appears,
311
and perform failover according to its content
316
remove the trigger file when recovery ends
321
remove no-longer-needed files from the archive directory
328
The <literal>copy</> command on Windows sets the final file size
329
before the file is completely copied, which would ordinarily confuse
330
<application>pg_standby</application>. Therefore
331
<application>pg_standby</application> waits <literal>sleeptime</>
332
seconds once it sees the proper file size. GNUWin32's <literal>cp</>
333
sets the file size only after the file copy is complete.
337
Since the Windows example uses <literal>copy</> at both ends, either
338
or both servers might be accessing the archive directory across the
345
<title>Supported Server Versions</title>
348
<application>pg_standby</application> is designed to work with
349
<productname>PostgreSQL</> 8.2 and later.
352
<productname>PostgreSQL</> 8.3 provides the <literal>%r</literal> macro,
353
which is designed to let <application>pg_standby</application> know the
354
last file it needs to keep. With <productname>PostgreSQL</> 8.2, the
355
<literal>-k</literal> option must be used if archive cleanup is
356
required. This option remains available in 8.3, but its use is deprecated.
359
<productname>PostgreSQL</> 8.4 provides the
360
<varname>recovery_end_command</> option. Without this option
361
a leftover trigger file can be hazardous.
366
<title>Author</title>
369
Simon Riggs <email>simon@2ndquadrant.com</email>