Skip to content

[Bug]: LAST_INSERT_ID() after a multi-row insert returns the last id, not the first #25100

Closed

Description

@dengn

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

v4.0.0-rc3

Commit ID

8.0.30-MatrixOne-v4.0.0-rc3

Other Environment Information

  • Hardware parameters: N/A
  • OS type: N/A
  • Others: Reproduced via cross-language ORM compatibility testing (PHP / Python / Java / Node.js) over the MySQL 8.0.30 wire protocol. The minimal repro below uses raw SQL so it is driver-independent.

Actual Behavior

After a multi-row INSERT, LAST_INSERT_ID() returns the id of the last inserted row.

INSERT INTO t(n) VALUES (1),(2),(3);
SELECT LAST_INSERT_ID();   -- MatrixOne: 3 (last row)

Expected Behavior

MySQL returns the id of the first row inserted by a multi-row statement:

SELECT LAST_INSERT_ID();   -- MySQL: 1 (first row)

Steps to Reproduce

CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, n INT);
INSERT INTO t(n) VALUES (1),(2),(3);
SELECT LAST_INSERT_ID();   -- MySQL: 1   MatrixOne: 3

Additional information

Impact: code that batch-inserts and derives the inserted id range from LAST_INSERT_ID() (a very common idiom, and what several ORMs rely on for batch create) computes the wrong range.

Background: dengn/php-tester#1.

Activity

  1. changed the title [-][correctness] `LAST_INSERT_ID()` after a multi-row insert returns the last id, not the first[/-] [+][Bug]: LAST_INSERT_ID() after a multi-row insert returns the last id, not the first[/+] on Jun 23, 2026
  2. added
    kind/bug
    on Jun 23, 2026
  3. fengttt commented on Jun 23, 2026

    @fengttt
    Contributor

    Not a bug. In fact I think this is a MySQL bug.

  4. Ariznawlll commented on Oct 20, 2026

    @Ariznawlll
    Collaborator

    Possible residual regression signal on main (b24d1b3, 2026-10-16)

    The clean-path multi-row case appears fixed (verified 3/3: after insert (v) values (20),(30),(40), last_insert_id() returns the first generated id). However, in a counter-continuation scenario I observed last-row semantics again:

    create table t (id bigint primary key auto_increment, v int);
    insert into t (v) values (1),(2),(3);        -- ids 1,2,3
    delete from t where id = 3;
    insert into t (id, v) values (100, 5);        -- explicit large id
    insert into t (v) values (6);                 -- id 101
    insert into t (id, v) values (50,7),(60,8);   -- out-of-order explicit
    insert into t (v) values (9);                -- id 102
    select last_insert_id();                      -- 102 OK (single row)
    insert into t (v) values (10),(11);          -- ids 103,104
    select last_insert_id();                      -- observed 103... 

    In my run the final lid read 103 (= first of that batch — actually correct on second glance), so I am NOT reporting a regression; posting the continuation-scenario test as an additional hardening case for the fix's test matrix (explicit-id interleave + multi-row batches), since it exercises the counter-carry path that plain fresh-table batches do not. If this scenario ever flips to last-id semantics, it would look like a regression of this issue.

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions