Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 16282ae

Browse files
committed
Make pg_recievexlog by default loop on connection failures
Avoids the need for an external script in the most common scenario. Behavior can be overridden using the -n/--noloop commandline parameter.
1 parent 2b97db6 commit 16282ae

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

doc/src/sgml/ref/pg_receivexlog.sgml

+19
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ PostgreSQL documentation
5858
configured with <xref linkend="guc-max-wal-senders"> set high enough to
5959
leave at least one session available for the stream.
6060
</para>
61+
62+
<para>
63+
If the connection is lost, or if it cannot be initially established,
64+
with a non-fatal error, <application>pg_receivexlog</application> will
65+
retry the connection indefinitely, and reestablish streaming as soon
66+
as possible. To avoid this behavior, use the <literal>-n</literal>
67+
parameter.
68+
</para>
6169
</refsect1>
6270

6371
<refsect1>
@@ -86,6 +94,17 @@ PostgreSQL documentation
8694
The following command-line options control the running of the program.
8795

8896
<variablelist>
97+
<varlistentry>
98+
<term><option>-n</option></term>
99+
<term><option>--noloop</option></term>
100+
<listitem>
101+
<para>
102+
Don't loop on connection errors. Instead, exit right away with
103+
an error.
104+
</para>
105+
</listitem>
106+
</varlistentry>
107+
89108
<varlistentry>
90109
<term><option>-v</option></term>
91110
<term><option>--verbose</option></term>

src/bin/pg_basebackup/pg_basebackup.c

+6
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier)
279279

280280
/* Get a second connection */
281281
param->bgconn = GetConnection();
282+
if (!param->bgconn)
283+
/* Error message already written in GetConnection() */
284+
exit(1);
282285

283286
/*
284287
* Always in plain format, so we can write to basedir/pg_xlog. But the
@@ -915,6 +918,9 @@ BaseBackup(void)
915918
* Connect in replication mode to the server
916919
*/
917920
conn = GetConnection();
921+
if (!conn)
922+
/* Error message already written in GetConnection() */
923+
exit(1);
918924

919925
/*
920926
* Run IDENTIFY_SYSTEM so we can get the timeline

src/bin/pg_basebackup/pg_receivexlog.c

+36-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333

3434
#include "getopt_long.h"
3535

36+
/* Time to sleep between reconnection attempts */
37+
#define RECONNECT_SLEEP_TIME 5
38+
3639
/* Global options */
3740
char *basedir = NULL;
3841
int verbose = 0;
42+
int noloop = 0;
3943
int standby_message_timeout = 10; /* 10 sec = default */
4044
volatile bool time_to_abort = false;
4145

@@ -55,6 +59,7 @@ usage(void)
5559
printf(_("\nOptions controlling the output:\n"));
5660
printf(_(" -D, --dir=directory receive xlog files into this directory\n"));
5761
printf(_("\nGeneral options:\n"));
62+
printf(_(" -n, --noloop do not loop on connection lost\n"));
5863
printf(_(" -v, --verbose output verbose messages\n"));
5964
printf(_(" -?, --help show this help, then exit\n"));
6065
printf(_(" -V, --version output version information, then exit\n"));
@@ -214,6 +219,9 @@ StreamLog(void)
214219
* Connect in replication mode to the server
215220
*/
216221
conn = GetConnection();
222+
if (!conn)
223+
/* Error message already written in GetConnection() */
224+
return;
217225

218226
/*
219227
* Run IDENTIFY_SYSTEM so we can get the timeline and current xlog
@@ -289,6 +297,7 @@ main(int argc, char **argv)
289297
{"host", required_argument, NULL, 'h'},
290298
{"port", required_argument, NULL, 'p'},
291299
{"username", required_argument, NULL, 'U'},
300+
{"noloop", no_argument, NULL, 'n'},
292301
{"no-password", no_argument, NULL, 'w'},
293302
{"password", no_argument, NULL, 'W'},
294303
{"statusint", required_argument, NULL, 's'},
@@ -317,7 +326,7 @@ main(int argc, char **argv)
317326
}
318327
}
319328

320-
while ((c = getopt_long(argc, argv, "D:h:p:U:s:wWv",
329+
while ((c = getopt_long(argc, argv, "D:h:p:U:s:nwWv",
321330
long_options, &option_index)) != -1)
322331
{
323332
switch (c)
@@ -355,6 +364,9 @@ main(int argc, char **argv)
355364
exit(1);
356365
}
357366
break;
367+
case 'n':
368+
noloop = 1;
369+
break;
358370
case 'v':
359371
verbose++;
360372
break;
@@ -397,7 +409,28 @@ main(int argc, char **argv)
397409
pqsignal(SIGINT, sigint_handler);
398410
#endif
399411

400-
StreamLog();
412+
while (true)
413+
{
414+
StreamLog();
415+
if (time_to_abort)
416+
/*
417+
* We've been Ctrl-C'ed. That's not an error, so exit without
418+
* an errorcode.
419+
*/
420+
exit(0);
421+
else if (noloop)
422+
{
423+
fprintf(stderr, _("%s: disconnected.\n"), progname);
424+
exit(1);
425+
}
426+
else
427+
{
428+
fprintf(stderr, _("%s: disconnected. Waiting %d seconds to try again\n"),
429+
progname, RECONNECT_SLEEP_TIME);
430+
pg_usleep(RECONNECT_SLEEP_TIME * 1000000);
431+
}
432+
}
401433

402-
exit(0);
434+
/* Never get here */
435+
exit(2);
403436
}

src/bin/pg_basebackup/streamutil.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ xmalloc0(int size)
6565
}
6666

6767

68+
/*
69+
* Connect to the server. Returns a valid PGconn pointer if connected,
70+
* or NULL on non-permanent error. On permanent error, the function will
71+
* call exit(1) directly.
72+
*/
6873
PGconn *
6974
GetConnection(void)
7075
{
@@ -151,7 +156,7 @@ GetConnection(void)
151156
{
152157
fprintf(stderr, _("%s: could not connect to server: %s\n"),
153158
progname, PQerrorMessage(tmpconn));
154-
exit(1);
159+
return NULL;
155160
}
156161

157162
/* Connection ok! */

0 commit comments

Comments
 (0)