*** a/src/backend/libpq/pqcomm.c
--- b/src/backend/libpq/pqcomm.c
***************
*** 83,88 ****
--- 83,91 ----
  #ifdef HAVE_UTIME_H
  #include <utime.h>
  #endif
+ #ifdef WIN32
+ #include <mstcpip.h>
+ #endif
  
  #include "libpq/ip.h"
  #include "libpq/libpq.h"
***************
*** 1314,1323 **** pq_endcopyout(bool errorAbort)
   * Support for TCP Keepalive parameters
   */
  
  int
  pq_getkeepalivesidle(Port *port)
  {
! #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return 0;
  
--- 1317,1371 ----
   * Support for TCP Keepalive parameters
   */
  
+ /*
+  * On Windows, we need to set both idle and interval at the same time.
+  * We also cannot reset them to the default (setting to zero will
+  * actually set them to zero, not default), therefor we fallback to
+  * the out-of-the-box default instead.
+  */
+ #ifdef WIN32
+ static int
+ pq_setkeepaliveswin32(Port *port, int idle, int interval)
+ {
+ 	struct tcp_keepalive	ka;
+ 	DWORD					retsize;
+ 
+ 	if (idle <= 0)
+ 		idle = 2 * 60 * 60; /* default = 2 hours */
+ 	if (interval <= 0)
+ 		interval = 1;       /* default = 1 second */
+ 
+ 	ka.onoff = 1;
+ 	ka.keepalivetime = idle * 1000;
+ 	ka.keepaliveinterval = interval * 1000;
+ 
+ 	if (WSAIoctl(port->sock,
+ 				 SIO_KEEPALIVE_VALS,
+ 				 (LPVOID) &ka,
+ 				 sizeof(ka),
+ 				 NULL,
+ 				 0,
+ 				 &retsize,
+ 				 NULL,
+ 				 NULL)
+ 		!= 0)
+ 	{
+ 		elog(LOG, "WSAIoctl(SIO_KEEPALIVE_VALS) failed: %ui",
+ 			 WSAGetLastError());
+ 		return STATUS_ERROR;
+ 	}
+ 	if (port->keepalives_idle != idle)
+ 		port->keepalives_idle = idle;
+ 	if (port->keepalives_interval != interval)
+ 		port->keepalives_interval = interval;
+ 	return STATUS_OK;
+ }
+ #endif
+ 
  int
  pq_getkeepalivesidle(Port *port)
  {
! #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(WIN32)
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return 0;
  
***************
*** 1326,1331 **** pq_getkeepalivesidle(Port *port)
--- 1374,1380 ----
  
  	if (port->default_keepalives_idle == 0)
  	{
+ #ifndef WIN32
  		ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
  
  #ifdef TCP_KEEPIDLE
***************
*** 1344,1350 **** pq_getkeepalivesidle(Port *port)
  			elog(LOG, "getsockopt(TCP_KEEPALIVE) failed: %m");
  			port->default_keepalives_idle = -1; /* don't know */
  		}
! #endif
  	}
  
  	return port->default_keepalives_idle;
--- 1393,1403 ----
  			elog(LOG, "getsockopt(TCP_KEEPALIVE) failed: %m");
  			port->default_keepalives_idle = -1; /* don't know */
  		}
! #endif /* TCP_KEEPIDLE */
! #else /* WIN32 */
! 		/* We can't get the defaults on Windows, so return "don't know" */
! 		port->default_keepalives_idle = -1;
! #endif /* WIN32 */
  	}
  
  	return port->default_keepalives_idle;
***************
*** 1359,1368 **** pq_setkeepalivesidle(int idle, Port *port)
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return STATUS_OK;
  
! #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
  	if (idle == port->keepalives_idle)
  		return STATUS_OK;
  
  	if (port->default_keepalives_idle <= 0)
  	{
  		if (pq_getkeepalivesidle(port) < 0)
--- 1412,1422 ----
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return STATUS_OK;
  
! #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(WIN32)
  	if (idle == port->keepalives_idle)
  		return STATUS_OK;
  
+ #ifndef WIN32
  	if (port->default_keepalives_idle <= 0)
  	{
  		if (pq_getkeepalivesidle(port) < 0)
***************
*** 1394,1414 **** pq_setkeepalivesidle(int idle, Port *port)
  #endif
  
  	port->keepalives_idle = idle;
! #else
  	if (idle != 0)
  	{
  		elog(LOG, "setting the keepalive idle time is not supported");
  		return STATUS_ERROR;
  	}
  #endif
- 
  	return STATUS_OK;
  }
  
  int
  pq_getkeepalivesinterval(Port *port)
  {
! #ifdef TCP_KEEPINTVL
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return 0;
  
--- 1448,1470 ----
  #endif
  
  	port->keepalives_idle = idle;
! #else /* WIN32 */
! 	return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
! #endif
! #else /* TCP_KEEPIDLE || WIN32 */
  	if (idle != 0)
  	{
  		elog(LOG, "setting the keepalive idle time is not supported");
  		return STATUS_ERROR;
  	}
  #endif
  	return STATUS_OK;
  }
  
  int
  pq_getkeepalivesinterval(Port *port)
  {
! #if defined(TCP_KEEPIDLE) || defined(WIN32)
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return 0;
  
***************
*** 1417,1422 **** pq_getkeepalivesinterval(Port *port)
--- 1473,1479 ----
  
  	if (port->default_keepalives_interval == 0)
  	{
+ #ifndef WIN32
  		ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
  
  		if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
***************
*** 1426,1431 **** pq_getkeepalivesinterval(Port *port)
--- 1483,1492 ----
  			elog(LOG, "getsockopt(TCP_KEEPINTVL) failed: %m");
  			port->default_keepalives_interval = -1;		/* don't know */
  		}
+ #else
+ 		/* We can't get the defaults on Windows, so return "don't know" */
+ 		port->default_keepalives_interval = -1;
+ #endif /* WIN32 */
  	}
  
  	return port->default_keepalives_interval;
***************
*** 1440,1449 **** pq_setkeepalivesinterval(int interval, Port *port)
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return STATUS_OK;
  
! #ifdef TCP_KEEPINTVL
  	if (interval == port->keepalives_interval)
  		return STATUS_OK;
  
  	if (port->default_keepalives_interval <= 0)
  	{
  		if (pq_getkeepalivesinterval(port) < 0)
--- 1501,1511 ----
  	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
  		return STATUS_OK;
  
! #if defined(TCP_KEEPINTVL) || defined (WIN32)
  	if (interval == port->keepalives_interval)
  		return STATUS_OK;
  
+ #ifndef WIN32
  	if (port->default_keepalives_interval <= 0)
  	{
  		if (pq_getkeepalivesinterval(port) < 0)
***************
*** 1466,1471 **** pq_setkeepalivesinterval(int interval, Port *port)
--- 1528,1536 ----
  	}
  
  	port->keepalives_interval = interval;
+ #else /* WIN32 */
+ 	return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
+ #endif
  #else
  	if (interval != 0)
  	{
*** a/src/interfaces/libpq/fe-connect.c
--- b/src/interfaces/libpq/fe-connect.c
***************
*** 38,43 ****
--- 38,44 ----
  #endif
  #define near
  #include <shlobj.h>
+ #include <mstcpip.h>
  #else
  #include <sys/socket.h>
  #include <netdb.h>
***************
*** 982,987 **** useKeepalives(PGconn *conn)
--- 983,989 ----
  	return val != 0 ? 1 : 0;
  }
  
+ #ifndef WIN32
  /*
   * Set the keepalive idle timer.
   */
***************
*** 1090,1095 **** setKeepalivesCount(PGconn *conn)
--- 1092,1143 ----
  	return 1;
  }
  
+ #else /* Win32 */
+ /*
+  * Enable keepalives and set the keepalive values on Win32,
+  * where they are always set in one batch.
+  */
+ static int
+ setKeepalivesWin32(PGconn *conn)
+ {
+ 	struct tcp_keepalive 	ka;
+ 	DWORD					retsize;
+ 	int						idle = 0;
+ 	int						interval = 0;
+ 
+ 	if (conn->keepalives_idle)
+ 		idle = atoi(conn->keepalives_idle);
+ 	if (idle <= 0)
+ 		idle = 2 * 60 * 60; /* 2 hours = default */
+ 
+ 	if (conn->keepalives_interval)
+ 		interval = atoi(conn->keepalives_interval);
+ 	if (interval <= 0)
+ 		interval = 1; /* 1 second = default */
+ 
+ 	ka.onoff = 1;
+ 	ka.keepalivetime = idle * 1000;
+ 	ka.keepaliveinterval = interval * 1000;
+ 
+ 	if (WSAIoctl(conn->sock,
+ 				 SIO_KEEPALIVE_VALS,
+ 				 (LPVOID) &ka,
+ 				 sizeof(ka),
+ 				 NULL,
+ 				 0,
+ 				 &retsize,
+ 				 NULL,
+ 				 NULL)
+ 		!= 0)
+ 	{
+ 		appendPQExpBuffer(&conn->errorMessage,
+ 						  libpq_gettext("WSAIoctl(SIO_KEEPALIVE_VALS) failed: %ui\n"),
+ 						  WSAGetLastError());
+ 		return 0;
+ 	}
+ 	return 1;
+ }
+ #endif /* WIN32 */
  
  /* ----------
   * connectDBStart -
***************
*** 1492,1497 **** keep_going:						/* We will come back to here until there is
--- 1540,1546 ----
  						{
  							/* Do nothing */
  						}
+ #ifndef WIN32
  						else if (setsockopt(conn->sock,
  											SOL_SOCKET, SO_KEEPALIVE,
  											(char *) &on, sizeof(on)) < 0)
***************
*** 1505,1510 **** keep_going:						/* We will come back to here until there is
--- 1554,1563 ----
  								 || !setKeepalivesInterval(conn)
  								 || !setKeepalivesCount(conn))
  							err = 1;
+ #else /* WIN32 */
+ 						else if (!setKeepalivesWin32(conn))
+ 							err = 1;
+ #endif /* WIN32 */
  
  						if (err)
  						{