Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 65A671337B47 for ; Sat, 22 Jan 2011 23:44:14 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 18636-02 for ; Sun, 23 Jan 2011 03:44:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 04E4013364DE for ; Sat, 22 Jan 2011 23:44:06 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p0N3i4se001951; Sat, 22 Jan 2011 22:44:04 -0500 (EST) To: Robert Haas cc: "Murray S. Kucherawy" , pgsql-bugs@postgresql.org Subject: Re: BUG #5837: PQstatus() fails to report lost connection In-reply-to: References: <201101140136.p0E1aTFb017852@wwwmaster.postgresql.org> Comments: In-reply-to Robert Haas message dated "Sat, 22 Jan 2011 21:51:33 -0500" Date: Sat, 22 Jan 2011 22:44:04 -0500 Message-ID: <1950.1295754244@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201101/157 X-Sequence-Number: 29179 Robert Haas writes: > On Thu, Jan 13, 2011 at 8:36 PM, Murray S. Kucherawy wrote: >> 1) establish a connection to postgresql >> 2) initiate a query, collect results, etc.; all normal >> 3) while client is idle, restart the server >> 4) initiate the very same query as before >> 5) call PQgetResult(), returns non-NULL >> 6) call PQresultStatus(), returns PGRES_FATAL_ERROR >> 7) call PQstatus(), returns CONNECTION_OK > I can reproduce this by hacking up src/test/examples/testlibpq to > loop, but I'm not totally sure what's causing the behavior. What did you do exactly? testlibpq.c just uses PQexec(), and AFAICS the connection status does end up BAD if the backend is terminated before a PQexec starts. > I think > the problem may be that libpq only reads enough from the connection to > get the FATAL error. It doesn't keep reading to see whether there's > an EOF afterward, and thus doesn't immediately realize that the > connection has been closed. I think the OP's mistake is to assume that the first PQgetResult ought to set this. As you say, it hasn't discovered the EOF condition at the time it returns that error-message result, and we are certainly not going to add another kernel call to every query cycle to check for EOF. The reason I don't see a problem when using PQexec is that PQexec will internally do another PQgetResult, and it's the second one that will fail and reset the connection status. In a non-connection-termination situation, the second internal PQgetResult call consumes the ReadyForQuery message, and at that point we fall out of PQexec (without any extra kernel call). Here, though, there won't be a ReadyForQuery, and it's the read() to try to collect one that discovers the loss of connection. In short: I don't think there's a bug here, just failure to understand proper use of PQgetResult. regards, tom lane