Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id B60892E0033 for ; Sun, 17 Feb 2008 21:58:37 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 57388-03 for ; Sun, 17 Feb 2008 21:58:33 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 24BF42E0030 for ; Sun, 17 Feb 2008 21:58:32 -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 m1I1wNSX005351; Sun, 17 Feb 2008 20:58:23 -0500 (EST) To: Oliver Jowett cc: PostgreSQL - JDBC Subject: Re: Implementing setQueryTimeout() In-reply-to: <47B8E200.2020307@opencloud.com> References: <47B8E200.2020307@opencloud.com> Comments: In-reply-to Oliver Jowett message dated "Mon, 18 Feb 2008 14:40:16 +1300" Date: Sun, 17 Feb 2008 20:58:23 -0500 Message-ID: <5350.1203299903@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/116 X-Sequence-Number: 18323 Oliver Jowett writes: > (2) Run a separate timer thread. Start a timer in Statement.execute() > before submitting the query to the protocol layer. If the timer expires, > close the low-level DB connection (from the timer thread) which should > cause an IOException in the guts of the protocol layer where the query > executing thread is blocked on network I/O, eventually propagating up as > a fatal SQLException to the caller. > I would like to implement (2) but I can see that killing the connection > on timeout may not be desirable in all cases. That seems pretty darn horrid, actually. If the reason for the slow response is server overload, this technique will make things rapidly *worse*. In the first place it does nothing to prevent the server from continuing to compute the too-slow query (and perhaps even committing it). In the second place, having to establish a new connection will eat a lot of cycles you really don't want to waste. In the third place, once you do establish a new connection it will be competing for cycles with the still-running query in the original backend. Iterate a few times and you'll have a self-inflicted denial of service. I agree with having a timer thread, I think, just not with what you want to do when the timer fires. Can't you do something like sending a query cancel request when you time out? It might be that you need to decouple queries from connections a bit more, so that a query can fail and "let go" of a connection, while the connection object has to wait for its query to be cancelled before returning to the pool of available connections. regards, tom lane