node.js - 在 postgresql 有线协议(protocol)中,如何从部分执行的选择中到达 ReadyForQuery?

标签 node.js postgresql postgresql-9.3

我正在尝试针对 postgresql 服务器(版本 9.3)调试 node.js 中“node-pg-cursor”模块的问题

此模块允许在一个选择中顺序读取 N 行,并通过发送来工作

cur.read(N): 'Execute' on portal=unnamed, rows=N

此命令最多获取 N 行,我们可以继续增量获取行,直到最后,我们收到

CommandComplete
ReadyForQuery

现在我的问题是我想在获取所有行并到达执行序列的末尾之前退出扩展命令:我想增量获取 N 行,N 行,N 行,..一分决定我够不够。

当我这样做时(停止通过 Execute 获取),查询似乎永远不会到达 CommandComplete 或 ReadyForQuery。这看起来很正常,因为没有任何信息告诉扩展查询我永远不会再从中询问行。

除了关闭连接之外,是否有一个命令可以在不从门户获取所有行的情况下到达 CommandComplete 或 ReadyForQuery?

我尝试发送 Close 并收到 CloseComplete,但它没有转到 ReadyForQuery。

如果我通过在协议(protocol)上发送垃圾来强制执行 ErrorResponse,我会到达 ReadyForQuery 但这看起来不是很干净......

最佳答案

我想你指的是这个,in the documentation :

If Execute terminates before completing the execution of a portal (due to reaching a nonzero result-row count), it will send a PortalSuspended message; the appearance of this message tells the frontend that another Execute should be issued against the same portal to complete the operation. The CommandComplete message indicating completion of the source SQL command is not sent until the portal's execution is completed. Therefore, an Execute phase is always terminated by the appearance of exactly one of these messages: CommandComplete, EmptyQueryResponse (if the portal was created from an empty query string), ErrorResponse, or PortalSuspended.

据推测,您正在获取 PortalSuspended 并且您想要丢弃门户而不执行任何更多的门户或使用更多的结果。

如果是这样,我想你可以发送一条Sync消息:

At completion of each series of extended-query messages, the frontend should issue a Sync message. This parameterless message causes the backend to close the current transaction if it's not inside a BEGIN/COMMIT transaction block ("close" meaning to commit if no error, or roll back if error). Then a ReadyForQuery response is issued.

您可能希望首先对门户发出关闭:

The Close message closes an existing prepared statement or portal and releases resources.

所以我认为您需要做的是,在消息流方面:

  • 解析
  • 绑定(bind)一个命名的门户
  • 描述
  • 循环:
    • 执行并限制行数以获取一些行
    • 如果不需要更多的行;然后
      • 关闭门户
      • 跳出循环
    • 如果 CommandComplete 收到:
      • 跳出循环
  • 同步
  • 等待ReadyForQuery

关于node.js - 在 postgresql 有线协议(protocol)中,如何从部分执行的选择中到达 ReadyForQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25900914/

相关文章:

PostgreSQL pgAdmin III SSL 连接到 AWS RDS 实例

postgresql - Openshift:远程公开 postgresql

sql - 如何在两列的postgres中使用ILIKE

postgresql - "Create database if not exists"在 postgres

node.js - 比较 Cloud Functions 中的两个 Firestore 时间戳

mysql - Join 不适用于sequelize 关联belongsToMany

kubernetes - 在本地在 kubernetes 中安装 postgresql 持久卷时面临的问题

postgresql - @@ROWCOUNT 在 PostgreSQL 9.3 中

javascript - 选择某些内容后,Angular ngOptions 列表空白

javascript - Node.js 如何进行条件回调?