暂时增加' statement_timeout'对于Rails中的Postgres查询?

时间:2018-05-14 07:39:11

标签: ruby-on-rails database postgresql timeout

我已经在statement_timeout中将database.yml配置为几秒钟,但我的应用程序中存在一些昂贵的查询,这需要更长的查询执行时间。在每个查询级别上实现此目的的推荐方法是什么? 我需要暂时将statement_timeout设置为更大的值,执行查询并将其重置为默认值?或者甚至不需要重置?

2 个答案:

答案 0 :(得分:5)

我认为只有通过更改整个连接的statement_timeout然后还原它才能实现这一目标:

  def execute_expensive_query
    ActiveRecord::Base.connection.execute 'SET statement_timeout = 600000' # 10 minutes
    # DB query with long execution time
  ensure
    ActiveRecord::Base.connection.execute 'SET statement_timeout = 5000' # 5 seconds
  end

在数据库级别,您只能按照this guide为当前交易设置statement_timeout

BEGIN;
SET LOCAL statement_timeout = 250;
...
COMMIT;

答案 1 :(得分:2)

要扩展已接受的答案,这是实现模块DatabaseTimeout的方式,这还可以确保将statement_timeout设置重置为原始值。

# Ruby's `Timeout` doesn't prevent queries from running for a long time.
#
# To prove this, run the following in a console (yes, twice):
#   Timeout.timeout(1.second) { ActiveRecord::Base.connection.execute('SELECT pg_sleep(100);') }
#   Timeout.timeout(1.second) { ActiveRecord::Base.connection.execute('SELECT pg_sleep(100);') }
# => The 2nd call should run for a long time.
#
# DatabaseTimeout's purpose is to enforce that each query doesn't run for more than the given timeout:
#   DatabaseTimeout.timeout(1.second) { ActiveRecord::Base.connection.execute('SELECT pg_sleep(100);') }
#   DatabaseTimeout.timeout(1.second) { ActiveRecord::Base.connection.execute('SELECT pg_sleep(100);') }
# => Both queries are interrupted after 1 second
module DatabaseTimeout
  # Usage: DatabaseTimeout.timeout(10) { run_some_query }
  def self.timeout(nb_seconds)
    original_timeout = ActiveRecord::Base.connection.execute('SHOW statement_timeout').first['statement_timeout']
    ActiveRecord::Base.connection.execute("SET statement_timeout = '#{nb_seconds.to_i}s'")
    yield
  ensure
    if original_timeout
      ActiveRecord::Base.connection.execute("SET statement_timeout = #{original_timeout}")
    end
  end
end