Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id A850E9FB28E for ; Wed, 29 Nov 2006 15:50:58 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 29031-01 for ; Wed, 29 Nov 2006 15:50:49 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from smtp-04.arnet.com.ar (smtp-04.arnet.com.ar [200.45.191.26]) by postgresql.org (Postfix) with SMTP id B44529FB228 for ; Wed, 29 Nov 2006 15:50:48 -0400 (AST) Received: (qmail 28230 invoked from network); 29 Nov 2006 16:52:22 -0000 Received: from unknown (HELO mail.integratech.net) (190.30.4.45) by smtp-04.arnet.com.ar with SMTP; 29 Nov 2006 16:52:21 -0000 Received: from [192.168.0.77] (azazel.integratech.net [192.168.0.77]) by mail.integratech.net (Postfix) with ESMTP id 0974267EF1 for ; Wed, 29 Nov 2006 16:50:44 -0300 (ART) Message-ID: <456DE433.8000407@integratech.com.ar> Date: Wed, 29 Nov 2006 16:49:07 -0300 From: Leandro Lucarella Organization: Integratech User-Agent: IceDove 1.5.0.8 (X11/20061123) MIME-Version: 1.0 To: pgsql-interfaces@postgresql.org Subject: Keep-alive support Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200611/15 X-Sequence-Number: 6330 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