Tre*_*reg 4 java postgresql limits jdbc
在 Java 中,使用java.sql.PreparedStatement,我试图提交一个相当大的查询,其中包含(VALUES (?), (?), (?)...)用于有效连接的常量表达式。
有 cca 250K 值,所以我也设置了 250K 参数。
在 Java 中,我得到
org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
Run Code Online (Sandbox Code Playgroud)
在我的服务器 PostgreSQL 日志中,有一行关于该错误:
incomplete message from client
Run Code Online (Sandbox Code Playgroud)
关于我可以在任何地方更改以使我的大型查询工作的某些设置的任何想法?
JDBC 驱动程序可以传递给后端的最大参数数是 32767。这受到 v3 线路协议的限制,它以 16 位 int 形式传递参数计数(有关 Bind 消息的定义,请参阅文档)。
您可以通过在数组中传递值并在服务器上取消嵌套来解决此问题:
// 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));
Run Code Online (Sandbox Code Playgroud)