我想为单个迁移设置PostgreSQL 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
我该怎么做呢?
BEGIN test(5432)=> SET LOCAL statement_timeout = 1; select pg_sleep(1); SET ERROR: canceling statement due to statement timeout
当您运行代码时,Postgres日志显示了什么? - Adrian Klaverdisable_ddl_transaction!
),现在它并没有什么意义。你对pg_sleep
的第一个调用确实会引发超时。在同一次execute
调用中发送SET
和pg_sleep
没有任何区别。而且,很可能你的remove_column
足够快,以至于 PG 在检查是否超过你设置的任何超时之前就已经完成了它。 - rafbm