how to set postgresql command timeout in rails

后端 未结 4 734
梦谈多话
梦谈多话 2021-02-07 08:11

I\'m using heroku and heroku postgresql. How do I set db command timeout so that I get an exception when a sql command takes longer than 10 seconds?

相关标签:
4条回答
  • 2021-02-07 08:37

    I don't know ruby, but in PostgreSQL you can simply run this:

    SET statement_timeout = '10s'
    

    and afterwards, all queries in this connection (session) will have strict limit on runtime.

    You can also change it in postgresql.conf as global default, or even make it default in given database or for given user like:

    ALTER DATABASE web SET statement_timeout = '10s';
    ALTER USER bad_user SET statement_timeout = '10s';
    
    0 讨论(0)
  • 2021-02-07 08:52

    Configure your database.yml like this, the key bit being the variables hash:

    defaults: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      min_messages: warning
      variables:
        statement_timeout: 5000
    
    0 讨论(0)
  • 2021-02-07 08:53

    Adding

    ActiveRecord::Base.connection.execute('set statement_timeout to 10000')
    

    to the end of the environment.rb file did not work for me.

    What worked was adding

    ActiveRecord::Base.connection.execute("SET statement_timeout = '10s'")
    

    to an initialize file.

    0 讨论(0)
  • 2021-02-07 08:54

    I found a solution here: Ruby on Rails: How to set a database timeout in application configuration?

    Add

    ActiveRecord::Base.connection.execute('set statement_timeout to 10000')
    

    in the end of the environment.rb file.

    Does anyone see a better way?

    0 讨论(0)
提交回复
热议问题