PostgreSQL异常:An IO error occurred while sending to the backend
PostgreSQL异常:An IO error occurred while sending to the backend
📅 发布时间:2026/7/11 19:15:11👁️ 浏览次数:
在使用PostgreSQL数据库批量写入数据的时候遇到了一个问题异常内容如下Cause: org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.报错内容报错提示1Caused by: org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend. at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:336) at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:446) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:370) at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:149) at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:138)报错提示2Caused by: java.io.IOException: Tried to send an out-of-range integer as a 2-byte value: 1847252 at org.postgresql.core.PGStream.sendInteger2(PGStream.java:252) at org.postgresql.core.v3.QueryExecutorImpl.sendParse(QueryExecutorImpl.java:1470) at org.postgresql.core.v3.QueryExecutorImpl.sendOneQuery(QueryExecutorImpl.java:1793) at org.postgresql.core.v3.QueryExecutorImpl.sendQuery(QueryExecutorImpl.java:1356) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:301) ... 117 common frames omitted问题分析从首个错误信息中可以看出在PostgreSQL运行SQL语句时遭遇了输入/输出I/O异常这意味着所执行的SQL语句存在某种问题或数据库文件系统有潜在故障。而第二条错误信息则揭示PostgreSQL在向客户端传输数据时数据量超过了预设的限制。这通常表明执行中的SQL查询结果过于庞大超出了系统默认的传输容量可能是因为查询返回了过多的数据行或单行数据包含大量信息所致。查看源码/** * Sends a 2-byte integer (short) to the back end. * * param val the integer to be sent * throws IOException if an I/O error occurs or {code val} cannot be encoded in 2 bytes */ public void sendInteger2(int val) throws IOException { if (val Short.MIN_VALUE || val Short.MAX_VALUE) { throw new IOException(Tried to send an out-of-range integer as a 2-byte value: val); } int2Buf[0] (byte) (val 8); int2Buf[1] (byte) val; pgOutput.write(int2Buf); }sendInteger2方法的作用是将一个整型值转换成短整型的二进制表示并通过输出流发送给后端。如果输入的值超出短整型的范围或者在写入过程中出现I/O错误则会抛出IOException。在方法内部首先进行了一次范围检查确保传入的整数值在短整型short的表示范围内即介于Short.MIN_VALUE和Short.MAX_VALUE之间。如果超出范围会抛出一个IOException指明试图发送的值过大或过小无法用两个字节表示。上面的错误提示能确定就是在这块抛出的了接下来重点查看代码中批量写入数据的代码。问题发现拿到接口执行的SQL以后发现是一个批量写入的SQL执行语句特别长在Navicat中执行就直接报错了修改批量写入的数据条数发现是可以正常执行的伪代码如下// 从一个响应中获取一个名为list的数据集合 ListCustomEntity entityList ExtractListFromResponse(response, list); // 使用一个数据访问层组件将数据插入到数据库中的指定表中 DataMapper.persistResponseData(entityList, targetTableName);问题解决将大集合进行拆分// 使用org.apache.commons.collections4提供的ListUtils工具类将大集合按照每组1000条进行拆分 ListListCustomEntity partition ListUtils.partition(entityList,1000); // 循环遍历拆分之后的集合分批写入数据 for (ListCustomEntity customEntityList : partition){ // 使用一个数据访问层组件将数据插入到数据库中的指定表中 DataMapper.persistResponseData(customEntityList, targetTableName); }使用MybatisPlus的批量写入请确保自己的项目当中使用了MybatisPlus// 从一个响应中获取一个名为list的数据集合 ListCustomEntity entityList ExtractListFromResponse(response, list); // 使用MybatisPlus的批量写入指定每批写入条数 customService.saveBatch(entityList,1000);
告别Mac视频预览烦恼:3步解锁专业级媒体管理体验 【免费下载链接】QuickLookVideo This package allows macOS Finder to display thumbnails, static QuickLook previews, cover art and metadata for most types of video files. 项目地址: https://gitcode.com…