pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: PostgreSQL - JDBC <pgsql-jdbc@postgresql.org>
Subject: Implementing setQueryTimeout()
Date: Mon, 18 Feb 2008 14:40:16 +1300
Message-ID: <47B8E200.2020307@opencloud.com> (raw)

The discussion around TCP keepalives reminded me that I have wanted to 
implement setQueryTimeout() support for a while. Here's my thoughts on 
it so far.

My motivation here is that we have an environment where having threads 
hang for extended periods when doing DB queries is not good. We must 
have them fail the query within a relatively short time, or the entire 
system can grind to a halt due to a lack of threads.

This is the same regardless of the cause of the delay - slow query, or a 
resultset that's taking a long time to transfer, or the server just died 
and TCP hasn't killed the connection yet, or there is a slow query ahead 
of us on the same connection.

In essense the requirement is: calling Statement.execute() should either 
return a result or throw an exception within some relatively short 
period of time. In my case the timeout is on the order of 15 seconds or 
less.

...

There are two main approaches I see:

(1) Map query timeout to server-side statement_timeout. Add a timeout 
parameter to QueryExecutor methods. The protocol layer remembers the 
current setting of statement_timeout and issues an appropriate "SET 
statement_timeout" as necessary before submitting each query.

Advantages: Gives nice failure characteristics (query is cancelled, 
connection remains usable)
Disadvantages: Doesn't help with anything but slow queries on the server 
side, relies on server-side query cancellation due to timeout happening 
reasonably promptly. Client code that sets statement_timeout itself can 
confuse it.

(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.

The assumption here is that if a thread is blocked inside 
Statement.execute(), the eventual cause of that is that something is 
blocked on network I/O. I think that's generally true?

Advantages: Should catch all cases regardless of cause.
Disadvantages: Draconian failure mode - a query timeout means the 
connection is dead.

...

I would like to implement (2) but I can see that killing the connection 
on timeout may not be desirable in all cases.

Independant of approach, I'd also like to implement a connection 
parameter that provides a default query timeout for all statements that 
don't override it (this would include internal driver queries e.g. OID 
lookups, metadata queries).

Any thoughts on this?

-O



view thread (16+ messages)  latest in thread

Message-ID: <47B8E200.2020307@opencloud.com>
Permalink:  ../47B8E200.2020307@opencloud.com/
Also on:    postgresql.org/message-id/47B8E200.2020307@opencloud.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: oliver@opencloud.com
  Subject: Re: Implementing setQueryTimeout()
  In-Reply-To: <47B8E200.2020307@opencloud.com>
  References: <47B8E200.2020307@opencloud.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox