如何在rails中设置postgresql命令超时

时间:2013-02-07 00:43:16

标签: ruby-on-rails postgresql heroku

我正在使用heroku和heroku postgresql。如何设置db命令超时,以便在sql命令超过10秒时出现异常?

4 个答案:

答案 0 :(得分:18)

像这样配置你的database.yml,关键位是变量hash:

defaults: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  min_messages: warning
  variables:
    statement_timeout: 5000

答案 1 :(得分:5)

我不知道ruby,但是在PostgreSQL中你可以简单地运行它:

SET statement_timeout = '10s'

之后,此连接(会话)中的所有查询都将严格限制运行时。

您也可以在postgresql.conf中将其更改为全局默认值,或者甚至将其设置为给定数据库或给定用户的默认值:

ALTER DATABASE web SET statement_timeout = '10s';
ALTER USER bad_user SET statement_timeout = '10s';

答案 2 :(得分:5)

我在这里找到了一个解决方案:Ruby on Rails: How to set a database timeout in application configuration?

添加

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

environment.rb文件的末尾。

有没有人看到更好的方法?

答案 3 :(得分:3)

添加

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

environment.rb文件的末尾对我不起作用。

有用的是添加

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

到初始化文件。

相关问题