--- ./old/postgres/src/interfaces/libpq/fe-connect.c	2018-07-19 19:18:14.436766664 +0300
+++ ./new/postgres/src/interfaces/libpq/fe-connect.c	2018-07-20 11:31:42.040486688 +0300
@@ -264,6 +264,10 @@
 		"TCP-Keepalives-Count", "", 10, /* strlen(INT32_MAX) == 10 */
 	offsetof(struct pg_conn, keepalives_count)},
 
+	{"keepalives_user_timeout", NULL, NULL, NULL,
+		"TCP-User-Timeout", "", 10, /* strlen(INT32_MAX) == 10 */
+	offsetof(struct pg_conn, keepalives_user_timeout)},
+
 	{"scram_channel_binding", NULL, DefaultSCRAMChannelBinding, NULL,
 		"SCRAM-Channel-Binding", "D",
 		21,						/* sizeof("tls-server-end-point") == 21 */
@@ -1628,6 +1632,38 @@
 
 	return 1;
 }
+
+/*
+ * Set the RTP_USER_TIMEOUT
+ */
+static int
+setKeepalivesTcpUserTimeout(PGconn *conn)
+{
+	int timeout;
+
+	if (conn->keepalives_user_timeout == NULL)
+		return 1;
+
+	timeout = atoi(conn->keepalives_user_timeout);
+	if (timeout < 0)
+		timeout = 0;
+
+#ifdef TCP_USER_TIMEOUT
+	if (setsockopt(conn->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
+				  (char *) &timeout, sizeof(timeout)) < 0 )
+	{
+		char sebuf[256];
+		appendPQExpBuffer(&conn->errorMessage,
+						  libpq_gettext("seckopt(%s) failed: %s \n"),
+						  "TCP_USER_TIMEOUT",
+						  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
+		return 0;
+	}
+#endif
+
+	return 1;
+}
+
 #else							/* WIN32 */
 #ifdef SIO_KEEPALIVE_VALS
 /*
@@ -1751,7 +1787,7 @@
 				if (ret || !ch->addrlist)
 					appendPQExpBuffer(&conn->errorMessage,
 									  libpq_gettext("could not parse network address \"%s\": %s\n"),
-									  ch->host, gai_strerror(ret));
+									  ch->hostaddr, gai_strerror(ret));
 				break;
 
 			case CHT_UNIX_SOCKET:
@@ -2185,7 +2221,8 @@
 						}
 						else if (!setKeepalivesIdle(conn)
 								 || !setKeepalivesInterval(conn)
-								 || !setKeepalivesCount(conn))
+								 || !setKeepalivesCount(conn)
+								 || !setKeepalivesTcpUserTimeout(conn))
 							err = 1;
 #else							/* WIN32 */
 #ifdef SIO_KEEPALIVE_VALS
