How to time out a query

Hi,

I’ve got an activerecord query which takes longer than 100 seconds. I’m
using rails 2.2 with postgresql 8.3.
http://www.postgresql.org/docs/8.3/static/runtime-config-client.html
mentions an option called “statement_timeout” which when set aborts a
query which takes longer than x milli seconds.

How do I pass this option to AR so that it in turn passes this to
postgresql?

cheers,
skar.

The life so short, the craft so long to learn.

You’ve probably already figured it out, but this looks workable:

See this page for some of the statement-related settings:

http://www.postgresql.org/docs/8.3/static/runtime-config-client.html
def setStatementTimeout milliseconds
query = “SET statement_timeout = #{milliseconds};”
ActiveRecord::Base.connection().execute query
end

Another alternative would be to just wrap your long-running query in a
timeout, something like:

require ‘timeout’

begin
status = Timeout::timeout(60) do
# some long-running action …
end
rescue Timeout::Error => te

end

Jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs