Skip to content

[Bug]: REPLACE does not publish its generated AUTO_INCREMENT value through LAST_INSERT_ID #28237

Closed

Description

@Ariznawlll

Summary

REPLACE can generate and persist a new AUTO_INCREMENT value without publishing that value through the session's LAST_INSERT_ID(). The stored row and the protocol/session state therefore disagree.

Environment

  • MatrixOne official main: e7bb0572235ec4bac81eb098eb0ad8900f0065ab
  • Clean local single-CN build and isolated data directory
  • MySQL 8.3 comparison instance

Reproduction

CREATE DATABASE replace_last_id;
USE replace_last_id;

CREATE TABLE t (
  id INT AUTO_INCREMENT PRIMARY KEY,
  u INT UNIQUE,
  v INT
);

INSERT INTO t(u,v) VALUES (10,1);
-- stored row: id=1

REPLACE INTO t(u,v) VALUES (10,2);
-- replacement deletes id=1 and inserts id=2

SELECT id,u,v FROM t;
SELECT LAST_INSERT_ID();

MatrixOne returns:

t:                   2 | 10 | 2
LAST_INSERT_ID():    1

MySQL returns:

t:                   2 | 10 | 2
LAST_INSERT_ID():    2

The stale value is not merely the replaced row's ID. If another AUTO_INCREMENT table first sets the session value to 700, then the same replacement inserts a new row but LAST_INSERT_ID() remains 700.

Affected rows remain 2, so the statement is correctly recognized as delete plus insert; only the generated ID is missing from session/protocol state.

Expected behavior

When REPLACE generates an AUTO_INCREMENT value, LAST_INSERT_ID() and the OK packet's insert ID should expose the generated value, matching the row actually inserted and MySQL behavior.

Applications using REPLACE followed by LAST_INSERT_ID() must not receive an unrelated value from an earlier statement.

Scope

Basic REPLACE row semantics are correct here: the conflicting unique-key row is deleted, a new row with the next ID is inserted, and affected rows is 2. This issue is limited to publishing the generated ID.

Activity

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

    @XuPeng-SH
    Contributor

    Resolution update

    PR #28349 has been merged into main and fixes the original reproduction in this issue.

    When REPLACE deletes a conflicting row and inserts a new row with a generated AUTO_INCREMENT value, the generated value is now published through the statement/session LAST_INSERT_ID() path. The fix covers the stale-value case where the session previously held an unrelated insert ID.

    The merged PR includes a real SQL regression covering REPLACE, the inserted row ID, affected rows, and LAST_INSERT_ID(). This resolves the contract described by this issue; broader REPLACE behavior outside generated AUTO_INCREMENT ID publication is not changed by this fix.

  5. added
    severity/s0
    and removed on Sep 8, 2026
  6. Ariznawlll commented on Sep 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:4b43f8c99bb22e6a35c389c93bf64c2dc043feaf
    • 部署方式:本地单 CN / TN / LogService,MySQL text protocol。
    • 测试日期:2026-09-08

    原问题验证

    初始 INSERT 后执行唯一键冲突 REPLACE,3 轮均读回新行 2|10|2 且 LAST_INSERT_ID()=2。

    Happy Path 覆盖

    1. REPLACE 删除冲突行并生成新 id,读回 id 与 LAST_INSERT_ID 一致。
    2. 先向另一 AUTO_INCREMENT 表写入使 session id 为 700,后续 REPLACE 读回 3|10|3 且 LAST_INSERT_ID()=3。

    Unhappy Path 覆盖

    1. 旧 session insert id 不会泄漏到 REPLACE 结果;唯一键冲突后连接可继续使用。

    白盒验证

    pkg/tests/issues 的 TestIssue28349AutoIncrementPublicPaths 覆盖 REPLACE generated id 发布。

    回归测试

    已加入 motr#151:14_issue_regression.issue_28237_28239_auto_increment_session,MOTR 3 轮通过。

    测试证据

    每轮均验证 2/2 与陈旧值控制后的 3/3。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions