postgresql批量插入报错Tried to send an out-of-range integer as a 2-byte value: 33048

报错信息:

Caused by: java.io.IOException: Tried to send an out-of-range integer as a 2-byte value: 33048
at org.postgresql.core.PGStream.sendInteger2(PGStream.java:347)
at org.postgresql.core.v3.QueryExecutorImpl.sendParse(QueryExecutorImpl.java:1547)
at org.postgresql.core.v3.QueryExecutorImpl.sendOneQuery(QueryExecutorImpl.java:1872)
at org.postgresql.core.v3.QueryExecutorImpl.sendQuery(QueryExecutorImpl.java:1433)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:315)

postgresql对于sql语句的参数数量是有限制的,最大为32767。
postgresql源码:

public void sendInteger2(int val) throws IOException {
        if (val >= -32768 && val <= 32767) {
            this.int2Buf[0] = (byte)(val >>> 8);
            this.int2Buf[1] = (byte)val;
            this.pgOutput.write(this.int2Buf);
        } else {
            throw new IOException("Tried to send an out-of-range integer as a 2-byte value: " + val);
        }
    }

从源码中可以看到使用2个字节的integer,故其取值范围为[-32768, 32767]。
这意味着sql语句的参数数量,即行数*列数之积必须小于等于32767。
如果一次插入的数据量太多,使得参数数量超过了最大值,只能分批插入。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容