Skip site navigation (1) Skip section navigation (2)

Re: Keep alive in libpq

From: Andrew Chernow <ac(at)esilo(dot)com>
To: Pavel Golub <pavel(at)gf(dot)microolap(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Keep alive in libpq
Date: 2009-05-27 13:27:44
Message-ID: 4A1D3FD0.6080305@esilo.com (view raw or flat)
Thread:
Lists: pgsql-hackers
Pavel Golub wrote:
> Hello, postgresmen.
> 
> I found solution how to implement keep alive through sockets in
> archive: http://archives.postgresql.org/pgsql-interfaces/2006-11/msg00014.php
> 
> However, it is dated 2006 year and I am wonder if this is for real? 

setsockopt has been around since at least the early 90s.  It is for real.

> At least in Windows environment?
> 
> If not are there any solutions?
> 

Use WSAIoctl(SIO_KEEPALIVE_VALS).  SIO_KEEPALIVE_VALS is supported on 
Windows 2000 and later.

http://msdn.microsoft.com/en-us/library/ms741621(VS.85).aspx
Search the page for "SIO_KEEPALIVE_VALS".

Make sure to test the below because I didn't :)


#include <Winsock2.h>
#include <Mstcpip.h> /* struct tcp_keepalive */

int r;
DWORD dw;
struct tcp_keepalive ka;

/* enable or disable (same as SO_KEEPALIVE) */
ka.onoff = 1;

/* milliseconds (same as TCP_KEEPIDLE) */
ka.keepalivetime = 60000;

/* milliseconds (same as TCP_KEEPINTVL) */
ka.keepaliveinterval = 3000;

/* configure keep-alives for 'conn' */
r = WSAIoctl((SOCKET) PQsocket(conn), SIO_KEEPALIVE_VALS,
   &ka, (DWORD) sizeof(ka), NULL, 0, &dw, NULL, NULL);

if (r == SOCKET_ERROR)
{
   // failed, check WSAGetLastError()
}


Apparently, you can also enable keep-alives using the standard 
setsockopt(...SO_KEEPALIVE...);  Although, the other knobs are only 
exposed through WSAIoctl.  When using SO_KEEPALIVE, the default 
keep-alive settings are used ... keep-alive timeout of 2 hours followed 
by a 1 second probe.  They can still be adjusted via WSAIoctl.

-- 
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

In response to

pgsql-hackers by date

Next:From: Peter EisentrautDate: 2009-05-27 13:33:28
Subject: Re: PostgreSQL Developer meeting minutes up
Previous:From: Bruce MomjianDate: 2009-05-27 13:16:18
Subject: Re: A couple of gripes about the gettext plurals patch

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group