gpt4 book ai didi

大型查询 : An I/O error occurred while sending to the backend 上的 Java PostgreSQL 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:36 24 4
gpt4 key购买 nike

在 Java 中,我尝试使用 java.sql.PreparedStatement 提交一个相当大的查询,其中包含常量 (VALUES (?), (?), (?)。 ..) 用于高效连接的表达式。

有 cca​​ 250K 的值,所以我也设置了 250K 的参数。

在 Java 中,我得到了

org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.

在我的服务器 PostgreSQL 日志中,有一行关于该错误:

incomplete message from client

关于我可以在任何地方更改以使我的大型查询正常工作的某些设置有什么想法吗?

最佳答案

JDBC 驱动程序可以传递给后端的参数的最大数量是 32767。这受到 v3 有线协议(protocol)的限制,该协议(protocol)以 16 位 int 传递参数计数(请参阅 definition of the Bind message 的文档)。

您可以通过在数组中传递值并在服务器上解除它们的嵌套来解决此问题:

// Normally this would be one too many parameters
Integer[] ids = new Integer[Short.MAX_VALUE + 1];
Arrays.setAll(ids, i -> i);
// Pass them in an array as a single parameter and unnest it
PreparedStatement stmt = con.prepareStatement(
"WITH ids (id) AS (SELECT unnest (?)) " +
"SELECT f.* FROM foo f JOIN ids i ON i.id = f.id"
);
stmt.setArray(1, con.createArrayOf("INT", ids));

关于大型查询 : An I/O error occurred while sending to the backend 上的 Java PostgreSQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41204543/

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