Skip to content

[Compatibility]: ODKU UPDATE returns the wrong LAST_INSERT_ID #28162

Closed

Description

@Ariznawlll

现象

在 MatrixOne main 99ed717b769e261b842c2f17345bd65865fc1470,ODKU 因主键冲突转为 UPDATE 时,LAST_INSERT_ID() 返回了冲突尝试消耗的下一个自增值,而不是实际更新行的自增主键:

CREATE TABLE t (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  k VARCHAR(32) UNIQUE,
  v INT
);
INSERT INTO t(k,v) VALUES ('x',10); -- id=1

INSERT INTO t(k,v) VALUES ('x',11)
  ON DUPLICATE KEY UPDATE v=VALUES(v);
SELECT LAST_INSERT_ID();

当前结果为 2(no-op ODKU 时为 2,实际更新时下一次返回 3),而被更新行的主键始终是 1。新行插入时 LAST_INSERT_ID() 返回实际新行 id,行为正常。

期望

ODKU 发生 UPDATE 时,LAST_INSERT_ID() 应返回被更新行的自增主键值;自增序列是否因冲突尝试前进,不应改变该函数对当前语句结果的语义。

MySQL 文档明确说明:含 AUTO_INCREMENT 列的 ODKU 插入或更新行时,LAST_INSERT_ID() 返回该 AUTO_INCREMENT 值。

参考:MySQL INSERT ... ON DUPLICATE KEY UPDATE

影响

依赖 LAST_INSERT_ID() 获取 upsert 行 ID 的应用会拿到不存在或尚未插入的自增值,导致后续更新、关联或缓存写入错误。

复现信息

  • MatrixOne main: 99ed717b769e261b842c2f17345bd65865fc1470
  • 重复执行 3 次,结果一致

Activity

  1. added this to the MO-202609 milestone on Sep 4, 2026
  2. added
    kind/bug
    on Sep 4, 2026
  3. added theissue type on Sep 4, 2026
  4. Ariznawlll commented on Sep 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充客户端可见的返回值现象,运行版本为官方 main e7bb0572235ec4bac81eb098eb0ad8900f0065ab,对照 MySQL 8.3.0、Connector/J 8.3.0。

    表定义:

    CREATE TABLE t(id BIGINT AUTO_INCREMENT PRIMARY KEY,k INT UNIQUE,v INT);
    INSERT INTO t(k,v) VALUES(10,1); -- 实际行 id=1
    INSERT INTO t(k,v) VALUES(10,2) ON DUPLICATE KEY UPDATE v=VALUES(v);
    SELECT * FROM t; -- 两端均为 (1,10,2)

    第二条 INSERT 只更新已有的 id=1,并未插入 id=2。此时 MO 的 OK packet insert-id、LAST_INSERT_ID() 返回 2,MySQL 对应返回 1。JDBC getGeneratedKeys() 的首个值也分别是 MO 的 2 和 MySQL 的 1。应用若将 MO 返回的首个 generated key 用于后续查询,会查询到并不存在的行。

    JDBC 分别组合 rewriteBatchedStatements、useServerPrepStmts、useAffectedRows、continueBatchOnError 的两种取值,共 16 种配置、每种重复 3 次;在各配置下,单条重复键更新后 getGeneratedKeys() 都出现上述首值差异,表内数据仍是 id=1。完整驱动返回序列为 MO [2,3]、MySQL [1,2];两端驱动都枚举出两个值,因此不能将“返回两个键”本身单独归为 MO 差异。这里能独立确认的服务端差异是 OK packet 的起始 ID 错误。另以 SQL 客户端重复 3 次得到同样现象。

    这里关注的是返回的 ID 与实际被更新行不对应,不把 AUTO_INCREMENT 预分配或序列空洞本身认定为问题。开启 CLIENT_FOUND_ROWS 后无变化 ODKU 的 affected-rows 问题已独立记录在 #28180。

  5. Ariznawlll commented on Sep 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main(e7bb0572235ec4bac81eb098eb0ad8900f0065ab)仍存在该会话状态错误,并且用不重合的旧会话值可以排除“恰好返回了旧值”的歧义:

    CREATE TABLE marker(id INT AUTO_INCREMENT PRIMARY KEY) AUTO_INCREMENT=500;
    CREATE TABLE target(
      id INT AUTO_INCREMENT PRIMARY KEY,
      u INT UNIQUE,
      v INT
    );
    
    INSERT INTO target(u,v) VALUES(10,1); -- target id=1
    INSERT INTO marker VALUES();          -- session LAST_INSERT_ID becomes 500
    SELECT LAST_INSERT_ID();              -- 500
    
    INSERT INTO target(u,v) VALUES(10,2)
    ON DUPLICATE KEY UPDATE v=VALUES(v);
    
    SELECT id,u,v FROM target;             -- 1,10,2
    SELECT LAST_INSERT_ID();               -- MatrixOne: 2; MySQL 8.3: 500

    MatrixOne 把冲突尝试预分配但并未写入的候选 ID 发布为当前语句的 insert ID。MySQL 在未使用 LAST_INSERT_ID(id) assignment 的普通更新分支中保留此前的会话值;无论采用哪种期望,MO 返回不存在于表中的候选 ID 都会让调用方误认该 ID 已被写入。

    相同场景连续执行时,MO 会继续把 3、4 等未落表候选值写入会话状态,而实际被更新行的主键始终为 1。

  6. added
    severity/s1
    and removed on Sep 16, 2026
  7. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    QA update — PR #28444 synchronized

    The PR is still open and unmerged. Its head advanced from c0cf5f828d64f58e14fb030f98d974ab6cdbd5e5 to 47254e28e441e6cfb3a8b5263f45963a21e1bfcb with a 12-commit main integration. The latest commit is titled “Merge main and preserve ODKU result ownership protocol boundaries.”

    I checked the exact new head: GitHub currently reports no associated workflow runs and no combined commit statuses; no review has been submitted for this head yet. Results recorded for the previous head are not treated as validation of this merge. This workspace has no MatrixOne checkout or Go runtime, so I did not run tests locally.

    The original GC/restart empty-metadata incident remains unresolved/inconclusive, with no attribution or rerun recorded. Issue #28162 stays open and assigned to VioletQwQ-0; no tester handoff until the current head is validated and the fix reaches the merge target.

  8. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    QA update — PR #28444 validation receipt for head 47254e28e441e6cfb3a8b5263f45963a21e1bfcb

    The author’s new PR comment reports this exact head’s normal conflict-maintenance validation: 334 focused named test events, 15 normal owning packages, scoped vet/configured lint, and 466/466 canonical comparisons; three existing owning-test skips were kept separate. The comment says no source/oracle was weakened and no new-head CI pass is claimed.

    I checked GitHub independently: the PR is still open/unmerged, and this SHA currently has no workflow runs or combined status checks. These repository-authored validation receipts are not my local execution; this workspace has no MatrixOne checkout or Go runtime.

    The comment also leaves formal review, representative performance/resource qualification, genuine predecessor/layout testing and deployed QA as separate gates. The original GC/restart empty-metadata incident remains inconclusive and un-attributed. Issue #28162 stays open and assigned to VioletQwQ-0; no tester handoff is appropriate before merge-target verification.

  9. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    QA checkpoint — PR #28444 synchronized to 8030407a39878935365e866b8d27beaa12ced2da

    The PR remains open and unmerged. The new head is a merge of main; comparison from the previous recorded head 47254e28e441e6cfb3a8b5263f45963a21e1bfcb shows 8 commits, so evidence tied to the previous SHA is not treated as validation of this tree.

    For the exact current SHA, GitHub reports no combined commit status checks. The associated PR workflow list has Connector/J Pool Reset E2E in progress and Iceberg Connector E2E successful; neither is an ODKU-specific validation result. I did not run tests locally: this workspace has no MatrixOne checkout or Go runtime.

    The prior ODKU regressions and the separate GC/restart empty-metadata incident remain tracked; the latter is still inconclusive/unattributed. Current-head CI, formal review, representative performance/resource qualification, predecessor/layout testing and deployed QA remain outstanding. Issue #28162 remains open and assigned to VioletQwQ-0; no tester handoff is made before the fix is validated on the merge target.

  10. XuPeng-SH commented on Oct 28, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #28444 (9cf8743ed7b6b4269f50179f1b09664a7582f4e7, batch 043fc9d3b9327bb7e542bc46ec3b73526c637ddc): targeted protobuf roundtrip and six internal process tests passed using a full Go source overlay; the base module graph retained an unused extra dependency. A separate preinsert compile stopped at the memory guard before assertions, without OOM. Actual SQL/OK-packet and JDBC generated-key behavior, distributed retry and mixed-version execution remain unverified locally; please verify against the original contract. No full-system pass is claimed.

  11. Ariznawlll commented on Nov 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。最新 main 在本 issue 的普通 ODKU UPDATE 场景中,将 OK packet 的更新行 ID 与会话 LAST_INSERT_ID() 分开处理;文本、二进制预处理和 JDBC 对照均与 MySQL 一致。

    关键证据

    • 版本:官方 main@1876b2f93e14c03ba538e3806e245e3c3203979a,包含 #28444 的合并提交 9cf8743ed7b6b4269f50179f1b09664a7582f4e7;本地隔离 1 Log / 1 TN / 2 CN、macOS / Apple Silicon,独立 MySQL 8.0.45 对照。
    • Happy case:目标表已有 id=1;用独立 marker 表把当前会话值设为 500 后,ODKU 更新冲突行。MO 与 MySQL 三轮均返回 OK packet insert-id 1、affected rows 2,表中更新的是 id=1,会话 LAST_INSERT_ID() 仍为 500。无变化 ODKU 的 packet id 为 0、会话值仍为 500;后续真正插入新行的 packet/session ID 与落表 ID 一致。文本和二进制预处理协议分别重复 3 轮。
    • JDBC:Connector/J 8.3.0 对 rewriteBatchedStatements、useServerPrepStmts、useAffectedRows、continueBatchOnError 各取两值,共 16 组 × 3 轮 × 两个数据库;每轮核对有变化和 no-op ODKU 的 generated keys、affected rows、会话值及表内容,MO 与 MySQL 全部一致。包含默认配置下有变化更新的 generated keys 序列 [1, 2],首值与实际行 ID 相符;不把驱动枚举两个值本身归为本 issue 的差异。
    • Unhappy / 隔离:两个直接 CN 的独立会话分别维持各自 marker ID,跨 CN 交替执行冲突更新时互不覆盖,重复 3 轮;事务中执行更新后 ROLLBACK,数据和先前会话 ID 保持预期;启用 CLIENT_FOUND_ROWS 的独立会话中 no-op affected rows 为 1,packet id 为 0,且不改该会话 marker。
    • 白盒:frontend 的 session/packet 分离、no-op、新插入和模拟 commit 失败检查各重复 3 次通过;pkg/sql/compile 远程 ODKU 结果协议、终止/取消/重试,以及 pkg/vm/process 会话结果汇总测试使用 -race -count=3 通过。受控失败注入不等同于生产故障演练。
    • 回归:MOTR PR #305 扩展了现有 #28162 用例的 ROLLBACK 和 CLIENT_FOUND_ROWS,在两个 CN 各重复 3 次通过;PR 当前 head 的四项离线 CI 通过。

    待解决点

    • 显式 LAST_INSERT_ID(expr) set-form 在 MO 仍报 20203,属于已有独立 #30802,本次不把该未实现形式计入普通 ODKU UPDATE 的修复通过范围。
    • 本次未执行真实跨版本混部、线上故障重试或完整系统升级;MOTR PR 尚未合入,按约定先关闭 issue,待后续合入回归。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions