*** a/doc/src/sgml/ref/pg_basebackup.sgml
--- b/doc/src/sgml/ref/pg_basebackup.sgml
***************
*** 386,391 **** PostgreSQL documentation
--- 386,411 ----
+
+
+
+
+ time that receiver waits for communication from server (in seconds).
+
+
+
+
+
+
+
+
+
+ time that client wait for connection to establish with server (in seconds).
+
+
+
+
+
*** a/doc/src/sgml/ref/pg_receivexlog.sgml
--- b/doc/src/sgml/ref/pg_receivexlog.sgml
***************
*** 164,169 **** PostgreSQL documentation
--- 164,189 ----
+
+
+
+
+ time that receiver waits for communication from server (in seconds).
+
+
+
+
+
+
+
+
+
+ time that client wait for connection to establish with server (in seconds).
+
+
+
+
+
*** a/src/bin/pg_basebackup/pg_basebackup.c
--- b/src/bin/pg_basebackup/pg_basebackup.c
***************
*** 41,46 **** bool includewal = false;
--- 41,50 ----
bool streamwal = false;
bool fastcheckpoint = false;
int standby_message_timeout = 10 * 1000; /* 10 sec = default */
+ int standby_recv_timeout = 60*1000; /* 60 sec = default */
+ char *standby_connect_timeout = NULL;
+
+ #define NAPTIME_PER_CYCLE 100 /* max sleep time between cycles (100ms) */
/* Progress counters */
static uint64 totalsize;
***************
*** 119,124 **** usage(void)
--- 123,132 ----
printf(_(" -p, --port=PORT database server port number\n"));
printf(_(" -s, --status-interval=INTERVAL\n"
" time between status packets sent to server (in seconds)\n"));
+ printf(_(" -r, --recvtimeout=INTERVAL time that receiver waits for communication from\n"
+ " server (in seconds)\n"));
+ printf(_(" -t, --conntimeout=INTERVAL time that client wait for connection to establish\n"
+ " with server (in seconds)\n"));
printf(_(" -U, --username=NAME connect as specified database user\n"));
printf(_(" -w, --no-password never prompt for password\n"));
printf(_(" -W, --password force password prompt (should happen automatically)\n"));
***************
*** 231,238 **** LogStreamerMain(logstreamer_param *param)
{
if (!ReceiveXlogStream(param->bgconn, param->startptr, param->timeline,
param->sysidentifier, param->xlogdir,
! reached_end_position, standby_message_timeout,
! true))
/*
* Any errors will already have been reported in the function process,
--- 239,246 ----
{
if (!ReceiveXlogStream(param->bgconn, param->startptr, param->timeline,
param->sysidentifier, param->xlogdir,
! reached_end_position, standby_message_timeout,
! standby_recv_timeout, true))
/*
* Any errors will already have been reported in the function process,
***************
*** 284,291 **** StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier)
}
#endif
! /* Get a second connection */
! param->bgconn = GetConnection();
if (!param->bgconn)
/* Error message already written in GetConnection() */
exit(1);
--- 292,300 ----
}
#endif
! /* Get a second connection. Sending connect_timeout
! * as configured, there is no need for rw_timeout.*/
! param->bgconn = GetConnection(standby_connect_timeout);
if (!param->bgconn)
/* Error message already written in GetConnection() */
exit(1);
***************
*** 461,466 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
--- 470,476 ----
char filename[MAXPGPATH];
char *copybuf = NULL;
FILE *tarfile = NULL;
+ int64 last_recv_timestamp;
#ifdef HAVE_LIBZ
gzFile ztarfile = NULL;
***************
*** 578,586 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
--- 588,599 ----
disconnect_and_exit(1);
}
+ /* Set the last reply timestamp */
+ last_recv_timestamp = localGetCurrentTimestamp();
while (1)
{
int r;
+ int64 now;
if (copybuf != NULL)
{
***************
*** 588,594 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
copybuf = NULL;
}
! r = PQgetCopyData(conn, ©buf, 0);
if (r == -1)
{
/*
--- 601,662 ----
copybuf = NULL;
}
! r = PQgetCopyData(conn, ©buf, 1);
! if (r == 0)
! {
! /*
! * In async mode, and no data available. We block on reading but
! * not more than the specified timeout, so that we can send a
! * response back to the client.
! */
! fd_set input_mask;
! struct timeval timeout;
!
! FD_ZERO(&input_mask);
! FD_SET(PQsocket(conn), &input_mask);
! timeout.tv_sec = 0;
! timeout.tv_usec = NAPTIME_PER_CYCLE*1000;
!
! r = select(PQsocket(conn) + 1, &input_mask, NULL, NULL, &timeout);
! if (r == 0 || (r < 0 && errno == EINTR))
! {
! /*
! * Got a timeout or signal. Before Continuing the loop, check for timeout.
! */
! if (standby_recv_timeout > 0)
! {
! now = localGetCurrentTimestamp();
! if (localTimestampDifferenceExceeds(last_recv_timestamp, now, standby_recv_timeout))
! {
! fprintf(stderr, _("%s: terminating DB File receive due to timeout\n"),
! progname);
! disconnect_and_exit(1);
! }
! }
!
! continue;
! }
! else if (r < 0)
! {
! fprintf(stderr, _("%s: select() failed: %s\n"),
! progname, strerror(errno));
! disconnect_and_exit(1);
! }
! /* Else there is actually data on the socket */
! if (PQconsumeInput(conn) == 0)
! {
! fprintf(stderr,
! _("%s: could not receive data from WAL Sender: %s"),
! progname, PQerrorMessage(conn));
! disconnect_and_exit(1);
! }
!
! /* Set the last reply timestamp */
! last_recv_timestamp = localGetCurrentTimestamp();
!
! /* Some data is received, so go back read them in buffer*/
! continue;
! }
if (r == -1)
{
/*
***************
*** 659,664 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
--- 727,735 ----
disconnect_and_exit(1);
}
+ /* Set the last reply timestamp */
+ last_recv_timestamp = localGetCurrentTimestamp();
+
#ifdef HAVE_LIBZ
if (ztarfile != NULL)
{
***************
*** 708,713 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
--- 779,785 ----
int current_padding = 0;
char *copybuf = NULL;
FILE *file = NULL;
+ int64 last_recv_timestamp;
if (PQgetisnull(res, rownum, 0))
strcpy(current_path, basedir);
***************
*** 725,733 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
--- 797,809 ----
disconnect_and_exit(1);
}
+ /* Set the last reply timestamp */
+ last_recv_timestamp = localGetCurrentTimestamp();
while (1)
{
int r;
+ int64 now;
+
if (copybuf != NULL)
{
***************
*** 735,742 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
copybuf = NULL;
}
! r = PQgetCopyData(conn, ©buf, 0);
if (r == -1)
{
/*
--- 811,872 ----
copybuf = NULL;
}
! r = PQgetCopyData(conn, ©buf, 1);
! if (r == 0)
! {
! /*
! * In async mode, and no data available. We block on reading but
! * not more than the specified timeout, so that we can send a
! * response back to the client.
! */
! fd_set input_mask;
! struct timeval timeout;
!
! FD_ZERO(&input_mask);
! FD_SET(PQsocket(conn), &input_mask);
! timeout.tv_sec = 0;
! timeout.tv_usec = NAPTIME_PER_CYCLE*1000;
+ r = select(PQsocket(conn) + 1, &input_mask, NULL, NULL, &timeout);
+ if (r == 0 || (r < 0 && errno == EINTR))
+ {
+ /*
+ * Got a timeout or signal. Before Continuing the loop, check for timeout.
+ */
+ if (standby_recv_timeout > 0)
+ {
+ now = localGetCurrentTimestamp();
+ if (localTimestampDifferenceExceeds(last_recv_timestamp, now, standby_recv_timeout))
+ {
+ fprintf(stderr, _("%s: terminating DB File receive due to timeout\n"),
+ progname);
+ disconnect_and_exit(1);
+ }
+ }
+
+ continue;
+ }
+ else if (r < 0)
+ {
+ fprintf(stderr, _("%s: select() failed: %s\n"),
+ progname, strerror(errno));
+ disconnect_and_exit(1);
+ }
+ /* Else there is actually data on the socket */
+ if (PQconsumeInput(conn) == 0)
+ {
+ fprintf(stderr,
+ _("%s: could not receive data from WAL Sender: %s"),
+ progname, PQerrorMessage(conn));
+ disconnect_and_exit(1);
+ }
+
+ /* Set the last reply timestamp */
+ last_recv_timestamp = localGetCurrentTimestamp();
+
+ /* Some data is received, so go back read them in buffer*/
+ continue;
+ }
if (r == -1)
{
/*
***************
*** 749,759 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
}
else if (r == -2)
{
fprintf(stderr, _("%s: could not read COPY data: %s"),
progname, PQerrorMessage(conn));
disconnect_and_exit(1);
}
!
if (file == NULL)
{
int filemode;
--- 879,892 ----
}
else if (r == -2)
{
+ fprintf(stderr, "\n");
fprintf(stderr, _("%s: could not read COPY data: %s"),
progname, PQerrorMessage(conn));
disconnect_and_exit(1);
}
!
! /* Set the last reply timestamp */
! last_recv_timestamp = localGetCurrentTimestamp();
if (file == NULL)
{
int filemode;
***************
*** 947,955 **** BaseBackup(void)
char xlogend[64];
/*
! * Connect in replication mode to the server
*/
! conn = GetConnection();
if (!conn)
/* Error message already written in GetConnection() */
exit(1);
--- 1080,1088 ----
char xlogend[64];
/*
! * Connect in replication mode to the server. Sending connect_timeout.
*/
! conn = GetConnection(standby_connect_timeout);
if (!conn)
/* Error message already written in GetConnection() */
exit(1);
***************
*** 1248,1253 **** main(int argc, char **argv)
--- 1381,1388 ----
{"no-password", no_argument, NULL, 'w'},
{"password", no_argument, NULL, 'W'},
{"status-interval", required_argument, NULL, 's'},
+ {"recvtimeout", required_argument, NULL, 'r'},
+ {"conntimeout", required_argument, NULL, 't'},
{"verbose", no_argument, NULL, 'v'},
{"progress", no_argument, NULL, 'P'},
{NULL, 0, NULL, 0}
***************
*** 1274,1280 **** main(int argc, char **argv)
}
}
! while ((c = getopt_long(argc, argv, "D:F:xX:l:zZ:c:h:p:U:s:wWvP",
long_options, &option_index)) != -1)
{
switch (c)
--- 1409,1415 ----
}
}
! while ((c = getopt_long(argc, argv, "D:F:xX:l:zZ:c:h:p:U:s:r:t:wWvP",
long_options, &option_index)) != -1)
{
switch (c)
***************
*** 1386,1391 **** main(int argc, char **argv)
--- 1521,1546 ----
exit(1);
}
break;
+ case 'r':
+ standby_recv_timeout = atoi(optarg)*1000;
+ if (standby_recv_timeout < 0)
+ {
+ fprintf(stderr, _("%s: invalid recv timeout \"%s\"\n"),
+ progname, optarg);
+ exit(1);
+ }
+
+ break;
+ case 't':
+ if (atoi(optarg) < 0)
+ {
+ fprintf(stderr, _("%s: invalid connect timeout \"%s\"\n"),
+ progname, optarg);
+ exit(1);
+ }
+
+ standby_connect_timeout = pg_strdup(optarg);
+ break;
case 'v':
verbose++;
break;
*** a/src/bin/pg_basebackup/pg_receivexlog.c
--- b/src/bin/pg_basebackup/pg_receivexlog.c
***************
*** 35,40 **** char *basedir = NULL;
--- 35,42 ----
int verbose = 0;
int noloop = 0;
int standby_message_timeout = 10 * 1000; /* 10 sec = default */
+ int standby_recv_timeout = 60*1000; /* 60 sec = default */
+ char *standby_connect_timeout = NULL;
volatile bool time_to_abort = false;
***************
*** 63,68 **** usage(void)
--- 65,74 ----
printf(_(" -p, --port=PORT database server port number\n"));
printf(_(" -s, --status-interval=INTERVAL\n"
" time between status packets sent to server (in seconds)\n"));
+ printf(_(" -r, --recvtimeout=INTERVAL time that receiver waits for communication from\n"
+ " server (in seconds)\n"));
+ printf(_(" -t, --conntimeout=INTERVAL time that client wait for connection to establish\n"
+ " with server (in seconds)\n"));
printf(_(" -U, --username=NAME connect as specified database user\n"));
printf(_(" -w, --no-password never prompt for password\n"));
printf(_(" -W, --password force password prompt (should happen automatically)\n"));
***************
*** 218,226 **** StreamLog(void)
lo;
/*
! * Connect in replication mode to the server
*/
! conn = GetConnection();
if (!conn)
/* Error message already written in GetConnection() */
return;
--- 224,233 ----
lo;
/*
! * Connect in replication mode to the server, Sending connect_timeout
! * as configured, there is no need for rw_timeout.
*/
! conn = GetConnection(standby_connect_timeout);
if (!conn)
/* Error message already written in GetConnection() */
return;
***************
*** 274,280 **** StreamLog(void)
timeline);
ReceiveXlogStream(conn, startpos, timeline, NULL, basedir,
! stop_streaming, standby_message_timeout, false);
PQfinish(conn);
}
--- 281,288 ----
timeline);
ReceiveXlogStream(conn, startpos, timeline, NULL, basedir,
! stop_streaming, standby_message_timeout,
! standby_recv_timeout, false);
PQfinish(conn);
}
***************
*** 306,311 **** main(int argc, char **argv)
--- 314,321 ----
{"no-password", no_argument, NULL, 'w'},
{"password", no_argument, NULL, 'W'},
{"status-interval", required_argument, NULL, 's'},
+ {"recvtimeout", required_argument, NULL, 'r'},
+ {"conntimeout", required_argument, NULL, 't'},
{"verbose", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
***************
*** 331,337 **** main(int argc, char **argv)
}
}
! while ((c = getopt_long(argc, argv, "D:h:p:U:s:nwWv",
long_options, &option_index)) != -1)
{
switch (c)
--- 341,347 ----
}
}
! while ((c = getopt_long(argc, argv, "D:h:p:U:s:r:t:nwWv",
long_options, &option_index)) != -1)
{
switch (c)
***************
*** 369,374 **** main(int argc, char **argv)
--- 379,404 ----
exit(1);
}
break;
+ case 'r':
+ standby_recv_timeout = atoi(optarg)*1000;
+ if (standby_recv_timeout < 0)
+ {
+ fprintf(stderr, _("%s: invalid recv timeout \"%s\"\n"),
+ progname, optarg);
+ exit(1);
+ }
+ break;
+ case 't':
+ if (atoi(optarg) < 0)
+ {
+ fprintf(stderr, _("%s: invalid connect timeout \"%s\"\n"),
+ progname, optarg);
+ exit(1);
+ }
+
+ standby_connect_timeout = pg_strdup(optarg);
+ break;
+
case 'n':
noloop = 1;
break;
*** a/src/bin/pg_basebackup/receivelog.c
--- b/src/bin/pg_basebackup/receivelog.c
***************
*** 181,187 **** close_walfile(char *basedir, char *walname, bool segment_complete)
* backend code. The protocol always uses integer timestamps, regardless of
* server setting.
*/
! static int64
localGetCurrentTimestamp(void)
{
int64 result;
--- 181,187 ----
* backend code. The protocol always uses integer timestamps, regardless of
* server setting.
*/
! int64
localGetCurrentTimestamp(void)
{
int64 result;
***************
*** 201,207 **** localGetCurrentTimestamp(void)
* Local version of TimestampDifference(), since we are not linked with
* backend code.
*/
! static void
localTimestampDifference(int64 start_time, int64 stop_time,
long *secs, int *microsecs)
{
--- 201,207 ----
* Local version of TimestampDifference(), since we are not linked with
* backend code.
*/
! void
localTimestampDifference(int64 start_time, int64 stop_time,
long *secs, int *microsecs)
{
***************
*** 223,229 **** localTimestampDifference(int64 start_time, int64 stop_time,
* Local version of TimestampDifferenceExceeds(), since we are not
* linked with backend code.
*/
! static bool
localTimestampDifferenceExceeds(int64 start_time,
int64 stop_time,
int msec)
--- 223,229 ----
* Local version of TimestampDifferenceExceeds(), since we are not
* linked with backend code.
*/
! bool
localTimestampDifferenceExceeds(int64 start_time,
int64 stop_time,
int msec)
***************
*** 333,339 **** bool
ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
char *sysidentifier, char *basedir,
stream_stop_callback stream_stop,
! int standby_message_timeout, bool rename_partial)
{
char query[128];
char current_walfile_name[MAXPGPATH];
--- 333,340 ----
ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
char *sysidentifier, char *basedir,
stream_stop_callback stream_stop,
! int standby_message_timeout,
! int standby_recv_timeout, bool rename_partial)
{
char query[128];
char current_walfile_name[MAXPGPATH];
***************
*** 341,346 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
--- 342,349 ----
char *copybuf = NULL;
int64 last_status = -1;
XLogRecPtr blockpos = InvalidXLogRecPtr;
+ int64 last_recv_timestamp;
+ bool ping_sent = false;
if (sysidentifier != NULL)
{
***************
*** 394,399 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
--- 397,406 ----
}
PQclear(res);
+ /* Set the last reply timestamp */
+ last_recv_timestamp = localGetCurrentTimestamp();
+ ping_sent = false;
+
/*
* Receive the actual xlog data
*/
***************
*** 477,486 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
if (r == 0 || (r < 0 && errno == EINTR))
{
/*
! * Got a timeout or signal. Continue the loop and either
! * deliver a status packet to the server or just go back into
* blocking.
*/
continue;
}
else if (r < 0)
--- 484,522 ----
if (r == 0 || (r < 0 && errno == EINTR))
{
/*
! * Got a timeout or signal. Before Continuing the loop, check for timeout.
! * and then either deliver a status packet to the server or just go back into
* blocking.
*/
+ if (standby_recv_timeout > 0)
+ {
+ now = localGetCurrentTimestamp();
+ if (localTimestampDifferenceExceeds(last_recv_timestamp, now, standby_recv_timeout))
+ {
+ fprintf(stderr, _("%s: terminating XLogStream receiver due to timeout\n"),
+ progname);
+ goto error;
+ }
+
+ /*
+ * We didn't receive anything new, for half of receiver
+ * replication timeout. Ping the server, if not already done.
+ */
+ if (!ping_sent)
+ {
+ if (localTimestampDifferenceExceeds(last_recv_timestamp, now, (standby_recv_timeout/2)))
+ {
+ if (!sendFeedback(conn, blockpos, now, true))
+ {
+ goto error;
+ }
+
+ last_status = now;
+ ping_sent = true;
+ }
+ }
+ }
+
continue;
}
else if (r < 0)
***************
*** 497,502 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
--- 533,543 ----
progname, PQerrorMessage(conn));
goto error;
}
+
+ /* Set the last reply timestamp */
+ last_recv_timestamp = localGetCurrentTimestamp();
+ ping_sent = false;
+
continue;
}
if (r == -1)
***************
*** 509,514 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
--- 550,559 ----
goto error;
}
+ /* Set the last reply timestamp */
+ last_recv_timestamp = localGetCurrentTimestamp();
+ ping_sent = false;
+
/* Check the message type. */
if (copybuf[0] == 'k')
{
*** a/src/bin/pg_basebackup/receivelog.h
--- b/src/bin/pg_basebackup/receivelog.h
***************
*** 7,16 ****
typedef bool (*stream_stop_callback) (XLogRecPtr segendpos, uint32 timeline, bool segment_finished);
extern bool ReceiveXlogStream(PGconn *conn,
! XLogRecPtr startpos,
! uint32 timeline,
! char *sysidentifier,
! char *basedir,
! stream_stop_callback stream_stop,
! int standby_message_timeout,
! bool rename_partial);
--- 7,24 ----
typedef bool (*stream_stop_callback) (XLogRecPtr segendpos, uint32 timeline, bool segment_finished);
extern bool ReceiveXlogStream(PGconn *conn,
! XLogRecPtr startpos,
! uint32 timeline,
! char *sysidentifier,
! char *basedir,
! stream_stop_callback stream_stop,
! int standby_message_timeout,
! int standby_recv_timeout,
! bool rename_partial);
!
! extern int64 localGetCurrentTimestamp(void);
! extern void localTimestampDifference(int64 start_time, int64 stop_time,
! long *secs, int *microsecs);
! extern bool localTimestampDifferenceExceeds(int64 start_time,
! int64 stop_time,
! int msec);
*** a/src/bin/pg_basebackup/streamutil.c
--- b/src/bin/pg_basebackup/streamutil.c
***************
*** 66,74 **** pg_malloc0(size_t size)
* Connect to the server. Returns a valid PGconn pointer if connected,
* or NULL on non-permanent error. On permanent error, the function will
* call exit(1) directly.
*/
PGconn *
! GetConnection(void)
{
PGconn *tmpconn;
int argcount = 4; /* dbname, replication, fallback_app_name,
--- 66,76 ----
* Connect to the server. Returns a valid PGconn pointer if connected,
* or NULL on non-permanent error. On permanent error, the function will
* call exit(1) directly.
+ * Set conn_timeout to PGconn structure if their value
+ * is not NULL.
*/
PGconn *
! GetConnection(char *conn_timeout)
{
PGconn *tmpconn;
int argcount = 4; /* dbname, replication, fallback_app_name,
***************
*** 85,90 **** GetConnection(void)
--- 87,94 ----
argcount++;
if (dbport)
argcount++;
+ if (conn_timeout)
+ argcount++;
keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
values = pg_malloc0((argcount + 1) * sizeof(*values));
***************
*** 114,119 **** GetConnection(void)
--- 118,129 ----
values[i] = dbport;
i++;
}
+ if (conn_timeout != NULL)
+ {
+ keywords[i] = "connect_timeout";
+ values[i] = conn_timeout;
+ i++;
+ }
while (true)
{
*** a/src/bin/pg_basebackup/streamutil.h
--- b/src/bin/pg_basebackup/streamutil.h
***************
*** 19,22 **** extern PGconn *conn;
extern char *pg_strdup(const char *s);
extern void *pg_malloc0(size_t size);
! extern PGconn *GetConnection(void);
--- 19,22 ----
extern char *pg_strdup(const char *s);
extern void *pg_malloc0(size_t size);
! PGconn *GetConnection(char *conn_timeout);