【GaussDB】预警复现:CLD-P-A-2026103 过滤条件等值查询与模糊查询结果不一致问题预警
背景
之前收到了华为发的预警CLD-P-A-2026103,说是有些场景下,where主键使用=查询只有一条数据,使用like匹配有两条,数据不一致。这种数据不一致的BUG是客户最怕的。但是我挑了预警文档中的几种简单的复现方式手动去复现,没有复现出来,因此尝试让AI去把问题场景给测出来。
【触发因素】
因素一: GaussDB实例的内核版本为: 仅GaussDB Kernel 506.0.0.SPC0100。
因素二:集中式、分布式
因素三:开启参数batch_insert_index_types='rcr_ubtree';(默认是rcr_ubtree)
因素四:在同一个session里面执行完批量插入之后,又执行了非批量插入,接着插入数据会导致插入重复数据。
批量插入索引的场景:
- 场景1:使用JDBC连接数据库,通过java的批量插入数据接口executeBatch进行批量插入索引;
- 场景2:执行语句insert into t1 select * from t2,其中t1和t2表schema相同,t1表包含索引,此语句支持在t1表上批量插入索引。
- 场景3:执行语句copy t1 from 'file.csv' delimiter ','csv header,其中t1和file.csv文件中的schema相同,t1表包含索引,此语句支持在t1表上批量插入索引。
- 场景4:使用gs_loader、gs_restore等导入导出工具进行批量导入。
注:以上因素全满足才会涉及该问题
AI分析
到 118环境复现一下这个问题 "【CLD-P-A-2026103】华为云Stack&轻量化部署形态 GaussDB 过滤条件等值查询与模糊查询结果不一致问题预警" ,给我一个最简单最容易执行的复现用例
复现成功,并且和预警截图的症象一字不差。用例包在 repro103/(file:///C:/Users/DarkAthena/Documents/Qoder/2026-10-27/cf07c072/repro103),已在 119 上端到端跑通(primary 因为 s17 崩溃从 118 漂到了 119,同一集群同一内核,不影响结论)。
复现序列(同一 session,四步)
| 步 | 动作 | 客户端看到 |
|---|---|---|
| 0 | 单行插入主键 Z0000001 |
INSERT 0 1 |
| 1 | PQexecPreparedBatch 批量插入 2000 行 |
cmdTuples=2000 |
| 2 | 一条唯一键冲突的单行插入,错误被 exception when others then null 吞掉 |
无异常 |
| 3 | 再插一次 Z0000001 |
INSERT 0 1 成功,但索引里没有这条 |
结果:where id = 'Z0000001' 返回 1 行,where id like '%Z0000001%' 返回 2 行(ctid (0,1) first-good、(8,42) second-orphan);reindex index repro103_pkey 报 Failed to create a unique index ------ 索引确实建不回去。5/5 复现,批量行数 10 / 100 / 500 / 1000 / 2000 都行(s14 变体 10/10)。
两个预警里没写、但必须知道的点
- 纯 SQL 造不出来。 场景2
insert into t1 select * from t2、场景3\copy、多值 VALUES、事务内、savepoint 回滚、批内重复、upsert ------ 全部保持索引/表一致(s1~s13)。只有 batch-bind(JDBCexecuteBatch的协议等价物)能触发。gsql发不出 batch-bind,libpq_ce 的PQsendQueryPrepared也不允许多个在途,所以必须用PQexecPreparedBatch。 - "应用吞掉错误"是必要条件。 中间态
invalid tuplesort state只要批量后紧跟非批量就会报(8 个变体全命中),但错误不被捕获时语句正常回滚、索引项不丢(s16 干净)。
分支结局:被吞后插新键 → 静默丢索引项(客户端仍报成功);同一新键插两次 → DN SIGSEGV ,栈是 exec_insert_indextuples → batch_insert_index_callback+0x1c3 → tuplesort_begin_index_btree,RDI=0xbabababa00000818(poison 值),即非批量插入拿着已释放的 BTBatchInsertState 去新建 tuplesort。规避参数 set batch_insert_index_types='' 在 session 级就能挡住。
jdbc驱动在这里,再写个JAVA的用例吧。另外你说的s17是执行的什么,我如果想复现,要怎么做?
Java 用例跑通了,也顺手把 s17 讲清楚。
1. JDBC 用例(已实测,118 主节点,repro103_java 表)
用的就是你给的 gaussdbjdbc.jar(1,695,517B / 2025-04-27,和 SPC0100 同一天构建;驱动类 com.huawei.gaussdb.jdbc.Driver,URL 前缀 jdbc:gaussdb:)。主机上只有 JRE(openjdk 1.8.0_312 Bisheng,没 javac),所以我在 Windows 侧 javac -source 8 -target 8 编好 .class 再拷过去。
bash /tmp/repro103/run103_java.sh pl 2000 repro103_java
step1 executeBatch added=2000 updateCounts=len=2000 rows-affected=1 ... in 126 ms
step2 conflict K0001000 swallowed by the PL handler -> statement returned OK
step3 re-insert Z0000001 -> 1 row(s) ACCEPTED
== detector, fresh session
seeded key Z0000001 index -> 1 heap -> 2
heap row ctid=(0,1) note=first-good
heap row ctid=(8,42) note=second-orphan
where id like '%Z0000001%' -> 2 row(s)
VERDICT: INDEX/HEAP SPLIT REPRODUCED
四个 mode 的对照(J103.java 里 mode = pl|save|raw|wg|dup,phase = write|check|both|fix):
| mode | 结果 |
|---|---|
pl(PL 块吞掉冲突) |
分裂复现,pl 跑了 4 次全中 |
save(JDBC Savepoint+rollback) |
也分裂;而且冲突插入 executeUpdate 返回 1、一个错误都不报------唯一性检查本身已经瞎了 |
raw(错误抛给客户端) |
干净:step2 服务端回 invalid tuplesort state,step3 正确报 Duplicate key value violates unique constraint |
wg(先 set batch_insert_index_types='') |
干净,规避参数在 Java 侧同样有效 |
raw 这条最值钱:Java 收到的中间态错误和 libpq_ce PQexecPreparedBatch 一字不差,说明驱动的 executeBatch 确实走服务端 batch-bind,不是被改写成多值 INSERT 绕过去了。
两个坑:
- 判读必须换连接 。第一版我把 detector 写在同一个 session 里,后端扫到分裂索引当场 SIGSEGV(今天 17:39,119,ffic
plsqlffic_gaussdb-2026-10-29_173951.log),客户端只剩socket is not closed ... EOF,证据全丢。 updateCounts的口径别当结论用:len=2000只有 1 个>0、1999 个是0、无EXECUTE_FAILED,整批按一条命令计数。
顺带把预警【快速恢复方法】在 Java 上验了一遍(phase=fix):reindex index repro103_java_pkey -> FAILED: Failed to create a unique index ... Key (id)=(Z0000001) is duplicated。所以不是"重建索引就好",得先按 ctid 删掉 second-orphan 那行。
2. s17 执行的是什么
s17 就是 C 用例里的 case 17(batch103.c:351(file:///C:/Users/DarkAthena/Documents/Qoder/2026-10-27/cf07c072/repro103/batch103.c#L351)),四步:
PQexecPreparedBatch批量插 2000 行K0000001..K0002000- 一条
insert K0001000(和批量里的键撞)被begin ... exception when others then null; end;吞掉 → 批量遗留的BTBatchInsertState变成脏状态 - 插一个全新 的键
Z0000001,成功 - 同一个
Z0000001再插一次 → 这一步 DN SIGSEGV,主节点掉,漂到另一台
它和 s20 只差一步:s20 的 Z0000001 在批量之前已正常入过索引,第二次插入只是"唯一检查看不见 → 多出一行没有索引项的孤儿"(=你要的症象);s17 这个键此前从没进过索引,第二次插入时非批量路径走进了 batch_insert_index_callback,拿着一块被 poison 填掉的 BTBatchInsertState 去 tuplesort_begin_index_btree(RDI=0xbabababa00000818),直接踩空崩。同一个缺陷,一个是"数据不一致"分支,一个是"core"分支。
3. 你要复现怎么做
先确认谁是主节点(bash /tmp/repro103/role.sh,逐端口看 pg_is_in_recovery(),现在 118 是主):
- 拿症象(安全,推荐):
bash /tmp/repro103/run103_java.sh pl 2000 repro103_java,或 C 侧/tmp/batch103 127.0.0.1 8000 root '密码' postgres 2000 repro103 s20 - 拿 core(会打掉主节点,只在演示集群跑):
/tmp/batch103 127.0.0.1 8000 root '密码' postgres 2000 b17_demo s17,Java 侧对应... J103 127.0.0.1 8000 postgres root 密码 b17j 2000 dup write(崩在 write 阶段的 step4,连接会断,这是预期)。生产主节点不要跑。 - 跑完拿栈:按 coredump 那套只读流程看 ffic / bbox,别动 core、ffic、box 文件本身。
结论
其实就是批量插入和单行插入的交替使用,并结合插入数据冲突后的内核处理不完整,导致数据弄乱的情况。这个BUG会导致两个场景,数据查询结果不对,或者coredump,和官方预警里描述是一致的,而且AI把这两个场景的复现代码也分别写出来了。不过AI没有复现出官方预警里的其他3个场景的问题,当然这并不代表就真的没问题,只是复现条件可能更加苛刻,官方文档没有详细描述。
下面给出可复现程序:
数据不一致
代码
java
import java.sql.*;
/*
curl -O https://repo1.maven.org/maven2/com/huaweicloud/gaussdb/gaussdbjdbc/506.0.0.b058/gaussdbjdbc-506.0.0.b058.jar
mv gaussdbjdbc-506.0.0.b058.jar gaussdbjdbc.jar
yum install java-1.8.0-openjdk-devel -y
javac -cp .:gaussdbjdbc.jar J103min.java
java -cp .:gaussdbjdbc.jar J103min 127.0.0.1 8000 postgres :username :password
*/
public class J103min {
static int ins(Connection c, String k, String v) throws SQLException {
PreparedStatement p = c.prepareStatement("insert into t103min values(?,?)");
p.setString(1, k);
p.setString(2, v);
int n = p.executeUpdate();
p.close();
return n;
}
public static void main(String[] a) throws Exception {
String url = "jdbc:gaussdb://" + a[0] + ":" + a[1] + "/" + a[2];
Class.forName("com.huawei.gaussdb.jdbc.Driver");
Connection c = DriverManager.getConnection(url, a[3], a[4]);
Statement s = c.createStatement();
s.execute("drop table if exists t103min cascade");
s.execute("create table t103min(id varchar(20) primary key, note varchar(50))");
System.out.println("1. create table t103min(id varchar(20) primary key, note varchar(50))");
ins(c, "Z0000001", "first");
System.out.println("2. first insert id = Z0000001" );
PreparedStatement p = c.prepareStatement("insert into t103min values(?,?)");
for (int i = 1; i <= 2000; i++) {
p.setString(1, String.format("K%07d", i));
p.setString(2, "batch");
p.addBatch();
}
p.executeBatch();
p.close();
System.out.println("3. batch insert K0000001 ~ k0002000");
s.execute("begin\n insert into t103min values('K0001000','dup');\nexception\n when others then null;\nend;");
System.out.println("4. plsql subtransaction insert :\nbegin\n insert into t103min values('K0001000','dup');\nexception\n when others then null;\nend; ");
System.out.println("5. re-insert Z0000001 -> " + ins(c, "Z0000001", "orphan") + " row(s)" +" ,why success?");
c.close();
c = DriverManager.getConnection(url, a[3], a[4]);
s = c.createStatement();
System.out.println("6. select count(*) from t103min where id = 'Z0000001'");
ResultSet r = s.executeQuery("select count(*) from t103min where id = 'Z0000001'");
r.next();
System.out.println("id = [Z0000001] -> " + r.getInt(1));
System.out.println("7. select id,count(*) from t103min where id like '%Z0000001%' group by id" );
r = s.executeQuery("select id,count(*) from t103min where id like '%Z0000001%' group by id" );
r.next();
System.out.println(" id like ["+r.getString(1)+"] -> " + r.getInt(2));
c.close();
}
}
执行效果
1. create table t103min(id varchar(20) primary key, note varchar(50))
2. first insert id = Z0000001
3. batch insert K0000001 ~ k0002000
4. plsql subtransaction insert :
begin
insert into t103min values('K0001000','dup');
exception
when others then null;
end;
5. re-insert Z0000001 -> 1 row(s) ,why success?
6. select count(*) from t103min where id = 'Z0000001'
id = [Z0000001] -> 1
7. select id,count(*) from t103min where id like '%Z0000001%' group by id
id like [Z0000001] -> 2
看输出的序号6和7,可以发现这两个查询出来的结果不一致,即主键扫描得到一行Z0000001,而全表扫描得到两行Z0000001。
发现了么,突破了主键唯一性的限制!
coredump
代码
java
import java.sql.*;
/*
curl -O https://repo1.maven.org/maven2/com/huaweicloud/gaussdb/gaussdbjdbc/506.0.0.b058/gaussdbjdbc-506.0.0.b058.jar
mv gaussdbjdbc-506.0.0.b058.jar gaussdbjdbc.jar
yum install java-1.8.0-openjdk-devel
javac -cp .:gaussdbjdbc.jar J103s17.java
java -cp .:gaussdbjdbc.jar J103s17 hostname portno databasename username password
*/
public class J103s17 {
static void say(String m) { System.out.println(m); }
static int ins(Connection c, String k, String v) throws SQLException {
PreparedStatement p = c.prepareStatement("insert into t103s17 values(?,?)");
p.setString(1, k);
p.setString(2, v);
int n = p.executeUpdate();
p.close();
return n;
}
public static void main(String[] a) throws Exception {
String url = "jdbc:gaussdb://" + a[0] + ":" + a[1] + "/" + a[2];
Class.forName("com.huawei.gaussdb.jdbc.Driver");
Connection c = DriverManager.getConnection(url, a[3], a[4]);
Statement s = c.createStatement();
say("[0] connected to " + url + " as " + a[3]);
s.execute("drop table if exists t103s17 cascade");
s.execute("create table t103s17(id varchar(20) primary key, note varchar(50))");
say("[1] create table t103s17(id varchar(20) primary key, note varchar(50))");
PreparedStatement p = c.prepareStatement("insert into t103s17 values(?,?)");
for (int i = 1; i <= 2000; i++) {
p.setString(1, String.format("K%07d", i));
p.setString(2, "batch");
p.addBatch();
}
p.executeBatch();
p.close();
say("[2] addBatch 2000 rows K0000001..K0002000 + executeBatch + close -> batch index state left behind");
s.execute("begin\n insert into t103s17 values('K0001000','dup');\nexception\n when others then null;\nend;");
say("[3] PL block inserts the batch key K0001000, conflict swallowed by 'when others then null' -> block OK");
say("[4] insert a brand new key N0000001, first time ...");
say(" -> accepted, " + ins(c, "N0000001", "first") + " row(s)");
say("[5] insert the SAME key N0000001 again (the unique check should reject it) ...");
boolean died = false;
try {
say(" -> accepted, " + ins(c, "N0000001", "second") + " row(s)");
} catch (SQLException e) {
String m = e.getMessage() == null ? "(null)" : e.getMessage().replace('\n', ' ');
say(" -> connection broke here: " + m.trim());
say(" -> if it says EOF / socket is not closed, the backend crashed on this statement;");
say(" read the newest ffic log on that host and re-run role.sh to find the new primary");
}
if (died) {
say("[6] this session is unusable now; the DN that served it has gone");
return;
}
c.close();
say("[6] no crash on this run: both rows accepted, read them from a new session");
}
}
执行效果
[0] connected to jdbc:gaussdb://127.0.0.1:8000/postgres as root
[1] create table t103s17(id varchar(20) primary key, note varchar(50))
[2] addBatch 2000 rows K0000001..K0002000 + executeBatch + close -> batch index state left behind
[3] PL block inserts the batch key K0001000, conflict swallowed by 'when others then null' -> block OK
[4] insert a brand new key N0000001, first time ...
Exception in thread "main" com.huawei.gaussdb.jdbc.util.PSQLException: [127.0.0.1:36664/127.0.0.1:8000] socket is not closed; Urgent packet sent to backend successfully; An I/O error occurred while sending to the backend.detail:EOF Exception;
at com.huawei.gaussdb.jdbc.core.v3.QueryExecutorImpl.handleIoException(QueryExecutorImpl.java:5046)
at com.huawei.gaussdb.jdbc.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:1354)
at com.huawei.gaussdb.jdbc.core.v3.TracedQueryExecutorImpl.execute(TracedQueryExecutorImpl.java:92)
at com.huawei.gaussdb.jdbc.jdbc.PgStatement.runQueryExecutor(PgStatement.java:737)
at com.huawei.gaussdb.jdbc.jdbc.PgStatement.executeInternal(PgStatement.java:690)
at com.huawei.gaussdb.jdbc.jdbc.PgStatement.execute(PgStatement.java:497)
at com.huawei.gaussdb.jdbc.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:209)
at com.huawei.gaussdb.jdbc.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:165)
at J103s17.ins(J103s17.java:10)
at J103s17.main(J103s17.java:40)
Caused by: java.io.EOFException: EOF Exception
at com.huawei.gaussdb.jdbc.core.PGStream.receiveChar(PGStream.java:409)
at com.huawei.gaussdb.jdbc.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:4151)
at com.huawei.gaussdb.jdbc.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:1322)
... 8 more
执行这个java代码,数据库就coredump了,而且没有产生coredump文件,只产生了ffic_log:
si_signo = 11 (SIGSEGV), si_code = 128, si_addr = (nil)
gaussdb + 0x1a63ff2 tuplesort_begin_common + 0x62
gaussdb + 0x1a692ca tuplesort_begin_index_btree + 0x3a
gaussdb + 0x3379923 batch_insert_index_callback + 0x1c3
gaussdb + 0x18c35a2 exec_insert_indextuples + 0x952
[postgres_cxt] debug_query_string: insert into t103s17 values($1,$2)
batch_insert_index_types = rcr_ubtree
解决方案
- 解决版本:GaussDB Kernel 506.0.0.SPC0500
- 数据修复方案:失效索引,找到重复数据的ctid,选择其中一行,使用ctid作为条件删除,再重建索引
- 临时规避方案:修改参数 batch_insert_index_types='';
详细请联系华为云客服获取预警文档CLD-P-A-2026103
- 本文作者: DarkAthena
- 本文链接: https://www.darkathena.top/archives/gaussdb-alert-reproduction-cld-p-a-2026103-equality-vs-fuzzy-query-filter-condition-inconsistency
- 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处