PostgreSQL异常:An IO error occurred while sending to the backend

在使用PostgreSQL数据库批量写入数据的时候,遇到了一个问题,异常内容如下:

复制代码
Cause: org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
报错内容
报错提示1
复制代码
Caused 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)
报错提示2
复制代码
Caused 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_VALUEShort.MAX_VALUE 之间。如果超出范围,会抛出一个 IOException,指明试图发送的值过大或过小,无法用两个字节表示。

上面的错误提示,能确定就是在这块抛出的了,接下来重点查看代码中批量写入数据的代码。

问题发现

拿到接口执行的SQL以后,发现是一个批量写入的SQL,执行语句特别长,在Navicat中执行就直接报错了;修改批量写入的数据条数,发现是可以正常执行的;

伪代码如下:

复制代码
// 从一个响应中获取一个名为"list"的数据集合
List<CustomEntity> entityList = ExtractListFromResponse(response, "list");

// 使用一个数据访问层组件,将数据插入到数据库中的指定表中
DataMapper.persistResponseData(entityList, targetTableName);
问题解决
将大集合进行拆分
复制代码
// 使用org.apache.commons.collections4提供的ListUtils工具类,将大集合按照每组1000条进行拆分
List<List<CustomEntity>> partition = ListUtils.partition(entityList,1000);
// 循环遍历拆分之后的集合,分批写入数据
for (List<CustomEntity> customEntityList : partition){
    // 使用一个数据访问层组件,将数据插入到数据库中的指定表中
    DataMapper.persistResponseData(customEntityList, targetTableName);
}
使用MybatisPlus的批量写入

请确保自己的项目当中使用了MybatisPlus

复制代码
// 从一个响应中获取一个名为"list"的数据集合
List<CustomEntity> entityList = ExtractListFromResponse(response, "list");
// 使用MybatisPlus的批量写入,指定每批写入条数
customService.saveBatch(entityList,1000);
相关推荐
憧憬成为web高手16 小时前
皮卡丘靶场速通--sql 2
数据库·sql·mybatis
段一凡-华北理工大学16 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
oradh18 小时前
Oracle XTTS实现跨版本迁移和升级(Oracle 11g单库升级至19C RAC集群)
数据库·oracle·11g升级19c·xtts跨版本迁移和升级
z1234567898619 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
程序猿DD19 小时前
一个 API Key,统一调用大模型、生图和联网搜索
数据库·网关
TlSfoward20 小时前
抓包代理链路下的 TLS 指纹变化分析 TLSFOWARD抓包工具
数据库·爬虫·网络协议·搜索引擎·php
数智化管理手记20 小时前
账龄分析手工统计易遗漏?自动账龄分析工具怎么搭建
大数据·网络·数据库·人工智能·数据挖掘
LabVIEW开发21 小时前
NI Package Manager安装LabVIEW时InternalServerError错误
网络·数据库·labview·labview知识·labview功能·labview程序
SelectDB21 小时前
PostgreSQL 实时同步到 Apache Doris:一站式 CDC 解决方案
数据库
欲速不达882021 小时前
Mysql并发DML锁等待的优化
数据库·mysql