5

I'd like to set my database to timeout requests that do not complete within a set amount of time, in order to prevent outlier requests from monopolizing the entire application.

Is there anything I can add to my Rails configuration files in order to set this?

I tried to add a line I saw often online of timeout: 5000 to my database.yml, but that didn't seem to have any effect.

I tried to make calls to ActiveRecord::Base.connection.execute('set statement_timeout to 5000') in the environment.rb but that causes Rails to error out.

I'm running a Postgres database on Heroku, where I do not have direct database access, hence I cannot do this with database configuration directly. Even if I remotely execute that command from the Heroku console, they can restart my application at any time, and if this isn't re-executed as the application starts, my change gets lost.

3 Answers 3

9

database.yml:

defaults: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  min_messages: warning
  variables:
    statement_timeout: 5000
Sign up to request clarification or add additional context in comments.

2 Comments

is that for 5 seconds or 5000 seconds?
5000 means 5 seconds.
5

Got this working, just needed to include the line in environment.rb at the very end, rather than in the beginning or in the block.

Comments

2

Try this syntax:

SET statement_timeout = 5000;

1 Comment

No, the problem isn't the SQL, the error I get when I stick the ActiveRecord::Base.connection.execute into my environment.rb is uninitialized constant ActiveRecord.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.