Rails ActiveRecord/Postgres单次查询超时?

Rails ActiveRecord/Postgres single query timeout?

我有一个记录查询(一个简单的 INSERT),它会在每个请求上发生。

仅针对此请求(每次加载页面时发生的请求),我想将限制设置为 500ms,以防数据库 locked/slow/down 不会影响网站,站点在等待 connect/write.

时挂起的位置

有没有一种方法可以在每个查询的基础上以某种方式指定超时,如果花费的时间太长,我可以中止 LoggedRequest.create!

我不想在我的配置中设置它,因为我有许多其他查询不应该有那么低的超时。

我正在使用 Postgres 11.7

我也不知道我对为整个会话设置超时有什么感觉,因为我不希望该连接从池中与其他不能有该超时的查询共享。

Rails 6 introduces event based triggers for notifications, logging etc that comes in very handy, provided you are using/can afford to migrate to Rails 6. Here'a useful post that demonstrates creating event based triggers for notifications/logging: https://pramodbshinde.wordpress.com/2020/03/20/custom-events-tracking-with-activesupportnotifications-and-audited/

如果由于某种原因您无法使用 Rails6,也许这篇文章可以帮助您找到一些答案:https://evilmartians.com/chronicles/the-silence-of-the-ruby-exceptions-a-rails-postgresql-database-transaction-thriller

如果我是你,我也可以考虑使用 AJAX 对服务器的 logging/whatever 的即发即弃 API 请求,这对服务器的正常运行并不重要申请。