gpt4 book ai didi

ruby-on-rails - Rails 4.2 Postgres 9.4.4 statement_timeout 不起作用

转载 作者:行者123 更新时间:2023-12-03 15:15:05 27 4
gpt4 key购买 nike

我正在尝试设置 statement_timeout。我在 database.yml 中尝试了这两种设置像这样的文件

variables:
statement_timeout: 1000

和这个
ActiveRecord::Base.connection.execute("SET statement_timeout = 1000")

测试过
ActiveRecord::Base.connection.execute("select pg_sleep(4)")

而且两者都没有任何作用。

我在本地运行 postgres 10 并且 statement_timeouts 工作正常。但是在我运行 postgres 9.4.4 的服务器上,它根本没有做任何事情。

我查了 Postgres' doc for 9.4并且 statement_timeout 可用。任何人都可以透露一些信息吗?

最佳答案

我无法使用以下方法在本地复制此问题:Postgresql 9.4.26。但是分享我尝试过的内容以及围绕服务器问题的一些想法可能会很有用。
这是我尝试过的(有用的一点可能是从 rails 验证 PG 版本的查询):

# Confirming I am executing against 9.4.x PG:

irb(main):002:0> ActiveRecord::Base.connection.execute("select version()")
(10.8ms) select version()
=> #<PG::Result:0x00007ff74782e060 status=PGRES_TUPLES_OK ntuples=1 nfields=1 cmd_tuples=1>
irb(main):003:0> _.first
=> {"version"=>"PostgreSQL 9.4.26 on x86_64-apple-darwin18.7.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit"}


# Set timeout:

irb(main):004:0> ActiveRecord::Base.connection.execute("SET statement_timeout = 1000")
(0.4ms) SET statement_timeout = 1000
=> #<PG::Result:0x00007ff7720a3d88 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>


# Confirm it works - it is ~1s and also stacktrace is pretty explicit about it:

irb(main):005:0> ActiveRecord::Base.connection.execute("select pg_sleep(4)")
(1071.2ms) select pg_sleep(4)
.... (stacktrace hidden)
ActiveRecord::StatementInvalid (PG::QueryCanceled: ERROR: canceling statement due to statement timeout)
: select pg_sleep(4)
这是尝试的内容
由于该问题仅发生在服务器上,而且自 statement_timeout适用于其他次要版本和本地,我想到的一件事是缺乏更新权限 statement_timeout从哪里尝试。也许用于建立数据库连接的 rails pg login 不允许更新该设置。
最好的方法是通过服务器上的 rails 控制台验证:
irb(main):004:0> ActiveRecord::Base.connection.execute("SET statement_timeout = 1000")
irb(main):004:0> irb(main):003:0> ActiveRecord::Base.connection.execute("show statement_timeout").first
(0.2ms) show statement_timeout
=> {"statement_timeout"=>"1s"}
或者,它可以通过 psql 控制台直接检查(某些部署也允许这样做):
psql myserveruser # if this was heroku's pg: heroku pg:psql

postgres=# set statement_timeout = 1000;
SET
postgres=# select pg_sleep(4);
ERROR: canceling statement due to statement timeout
Time: 1068.067 ms (00:01.068)
其他要记住的事情(取自 https://dba.stackexchange.com/a/83035/90903 ):

The way statement_timeout works, the time starts counting when theserver receives a new command from the client...

And if a function does SET statement_timeout = 100; it will have an effect only starting atthe next command from the client.

关于ruby-on-rails - Rails 4.2 Postgres 9.4.4 statement_timeout 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160535/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com