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

Keep-alive support

From: Leandro Lucarella <llucarella(at)integratech(dot)com(dot)ar>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Keep-alive support
Date: 2006-11-29 19:49:07
Message-ID: 456DE433.8000407@integratech.com.ar (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-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

Responses

pgsql-hackers by date

Next:From: Tom LaneDate: 2006-11-29 19:50:12
Subject: Re: More vcbuild
Previous:From: Stefan KaltenbrunnerDate: 2006-11-29 19:47:26
Subject: -HEAD configure issue with libedit on linux

pgsql-interfaces by date

Next:From: Tom LaneDate: 2006-11-29 20:18:37
Subject: Re: Keep-alive support
Previous:From: Pawel HudakDate: 2006-11-28 10:15:49
Subject: ECPGttype/OID

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