节点PostgreSQL客户端查询超时 [英] Node PostgreSQL timeout a query by the client

查看:404
本文介绍了节点PostgreSQL客户端查询超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node软件包 pg 用于postgres(这里):

I'm using Node package pg for postgres (here):

npm i pg

var pg = require('pg');

我正在查询一个不属于我的大型集群,在某些情况下可能会失败。失败可能是响应较差,易于处理或查询无穷。
请注意,我无法在数据库端进行更改(配置或其他方式)。

I'm querying a large cluster which is not owned by me, and under certain conditions may fail. Failure may be bad response which is easy to handle or endless query. Please note I can not introduce changes [config or otherwise] on the DB side.

是否可以设置查询时间超时?
我希望客户在设定的时间后放弃,并返回超时错误。

Is there any way to set a timeout for query time? I'd like my client to give up after a set time, and return timeout error.

在文档中找不到类似的内容。

Couldn't find anything as such in the docs.

谢谢!

推荐答案

最佳做法是使用初始化查询,以设置会话的查询超时。

Best practice is using an init query, to set query timeout for the session.

    SET statement_timeout TO 15000; # x milliseconds or 0 (turns off limitation)

这需要以ms为单位的超时参数,并且适用于会话。
之后,当查询花费的时间超过指定的值时,您将从服务器收到错误。请注意,这是应用户要求的

This takes an argument of the timeout in ms, and is applied for the session. Afterwards, when a query takes longer than the value specified, you will receive an error from the server. Note this is on user's request:

    ERROR:  Query (150) cancelled on user's request

还请注意,这实际上取消了服务器端的查询,从而减轻了负载。

Also note this actually cancels the query on the server side, reducing load.

这篇关于节点PostgreSQL客户端查询超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆