Keep-alive support - Mailing list pgsql-interfaces

From Leandro Lucarella
Subject Keep-alive support
Date
Msg-id 456DE433.8000407@integratech.com.ar
Whole thread Raw
Responses Re: Keep-alive support  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Keep-alive support  (Tomasz Myrta <jasiek@klaster.net>)
List pgsql-interfaces
Is there any keep alive support in libpq? I'm not really using libpq 
directly, I'm using libpqxx and there is no keep-alive support there, so 
I'm trying to use TCP's own keep-alive support, but I have a problem: 
libpq seems to reconnect the socket when the connection is lost.

What I do is this:

/* Connect libpq as in the Example 1 of the manual. */

/* Before any query is sent (linux 2.6.18) */

int sd = PQsocket(conn);
// Use TCP keep-alive feature
setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, 1);
// Maximum keep-alive probes before asuming the connection is lost
setsockopt(sd, IPPROTO_TCP, TCP_KEEPCNT, 5);
// Interval (in seconds) between keep-alive probes
setsockopt(sd, IPPROTO_TCP, TCP_KEEPINTVL, 2);
// Maximum idle time (in seconds) before start sending keep-alive probes
setsockopt(sd, IPPROTO_TCP, TCP_KEEPIDLE, 10);
(see set_sock_opt() above, but is just a simple setsockopt wrapper)

then I so a sleep(10) and continue with the Example 1 of the manual 
(which makes a simple transaction query). In the sleep time I unplug the 
network cable and monitor the TCP connection using netstat -pano, and 
found all the TCP keep-alive timers times out perfectly, closing the 
connection, but inmediatly I see a new connection (and without the 
keep-alive parameters, so it take forever to timeout again). So I guess 
libpq is re-opening the socket. This is making my life a nightmare =)

Is there any way to avoid this behavior? Please tell me it is =)

PS: This thread was originated in libpqxx's mailing list, but I'm moving 
it here because it looks like a libpq issue, if you want take a look to 
the original thread, you can find it here:
http://gborg.postgresql.org/pipermail/libpqxx-general/2006-November/001511.html

TIA

--------8<--------8<--------8<--------8<--------8<--------8<--------

void set_sock_opt(int sd, int level, int name, int val)
{        if (setsockopt(sd, level, name, &val, sizeof(val)) == -1)        {                perror("setsockopt");
       abort();        }
 
}

-------->8-------->8-------->8-------->8-------->8-------->8--------

-- 
Leandro Lucarella
Integratech S.A.
4571-5252


pgsql-interfaces by date:

Previous
From: "Pawel Hudak"
Date:
Subject: ECPGttype/OID
Next
From: Tom Lane
Date:
Subject: Re: Keep-alive support