如何为单个迁移设置statement_timeout?

Joh*_*hir 6 postgresql ruby-on-rails database-migration

我想statement_timeout为单独的迁移设置 postgres。我似乎无法做到这一点。这是我的实验:

def change
  execute <<~SQL
    SET LOCAL statement_timeout = 1; -- ms

    -- this does not cause a timeout which is expected, because pg
    -- only applies the timeout to the next protocol message / statement,
    -- and rails sends everthing inside execute in the same statement
    select pg_sleep(1); -- seconds
  SQL

  # if uncommented, this DOES cause a timeout, which is expected
  # execute <<~SQL
  #   select pg_sleep(1); -- seconds
  # SQL

  # this does not cause a timeout, which is unexpected
  remove_column :foos, :bar

  # we do get here, which is unexpected
  raise "we finished"
end
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Joh*_*hir 0

打开log_duration并进行更多调查后,我想我得出的结论是,postgres 不会考虑删除该列以花费超过 1 毫秒,即使记录了如下内容:

statement: ALTER TABLE "foos" DROP COLUMN "bar"
duration: 1.171 ms
duration: 0.068 ms
duration: 0.328 ms
Run Code Online (Sandbox Code Playgroud)

但是按照下面的食谱,

  1. 我能够让另一种类型的查询由于语句超时而失败
  2. 由于锁定超时,我能够使删除列失败(同时在 psql 会话中保持锁定)
statement: ALTER TABLE "foos" DROP COLUMN "bar"
duration: 1.171 ms
duration: 0.068 ms
duration: 0.328 ms
Run Code Online (Sandbox Code Playgroud)