繁体   English   中英

大型查询时出现Java PostgreSQL错误:发送到后端时发生I / O错误

[英]Java PostgreSQL error on large query: An I/O error occurred while sending to the backend

在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有线协议的限制,该协议以16位int传递参数计数(请参阅文档以获取绑定消息定义 )。

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

// 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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM