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

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Keep-alive support



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



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group