【发布时间】:2020-09-21 11:28:51
【问题描述】:
我想为个人迁移设置 postgres statement_timeout。我似乎无法做到这一点。这是我的实验:
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
我该怎么做?
【问题讨论】:
-
这里发生了其他事情:
test(5432)=> begin ; BEGIN test(5432)=> SET LOCAL statement_timeout = 1; select pg_sleep(1); SET ERROR: canceling statement due to statement timeout运行代码时 Postgres 日志显示什么? -
@AdrianKlaver 当我关闭查询日志时,日志中没有任何内容(没有错误)。要查看查询日志吗?
-
我开始怀疑我的“问题”是删除列所需的时间不到 1 毫秒。我在这里探索:dba.stackexchange.com/questions/269251/…
-
@JohnBachir 在您对代码示例进行编辑(删除
disable_ddl_transaction!)之后,现在它真的没有意义了。您对pg_sleep的第一次调用确实会引发超时。在同一个execute调用中发送SET和pg_sleep没有区别。此外,很可能你的remove_column足够快,以至于 PG 甚至在它有机会检查它是否超过你设置的任何超时之前就完成了它。
标签: ruby-on-rails postgresql database-migration