Skip to content

[Bug]: INSERT IGNORE maps AUTO_INCREMENT gaps to accepted rows differently from MySQL #28238

Description

@Ariznawlll

Summary

In a multi-row INSERT IGNORE, MatrixOne assigns AUTO_INCREMENT candidates by original input position before duplicate rows are removed. MySQL assigns consecutive IDs to the accepted rows within the statement's reserved range and consumes the unused reservation afterward.

Consequently, a successful row receives a different generated primary key depending on where an ignored row appeared in the input list, even though MatrixOne and MySQL agree on the inserted payload rows and the next statement's high watermark.

Environment

  • MatrixOne official main: e7bb0572235ec4bac81eb098eb0ad8900f0065ab
  • Clean local single-CN build and isolated data directory
  • MySQL 8.3 comparison instance
  • Default auto_increment_increment=1, auto_increment_offset=1

Reproduction

CREATE DATABASE insert_ignore_auto_id;
USE insert_ignore_auto_id;

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

INSERT INTO t(u) VALUES (10);              -- id=1
INSERT IGNORE INTO t(u) VALUES (20),(10),(30);
SELECT * FROM t ORDER BY id;

INSERT INTO t(u) VALUES (99);
SELECT * FROM t ORDER BY id;

MatrixOne assigns:

1 | 10
2 | 20
4 | 30
5 | 99

MySQL assigns:

1 | 10
2 | 20
3 | 30
5 | 99

The difference follows the ignored row's position:

Input values               MatrixOne accepted IDs    MySQL accepted IDs    next ID in both
(10),(20),(30)             3,4                       2,3                   5
(20),(10),(30)             2,4                       2,3                   5
(20),(30),(10)             2,3                       2,3                   5
(10),(20),(10),(30)        3,5                       2,3                   6

In every row above, u=10 already exists before the multi-row statement.

Expected behavior

For MySQL-compatible INSERT IGNORE, accepted rows in one multi-row statement should receive the same generated IDs as MySQL. Ignored rows may consume reserved sequence space, as shown by the next statement starting at 5 or 6, but they should not shift the ID attached to a later accepted row inside the same statement.

The current behavior can break replication checks and application-side generated-key mapping even when the non-key payload and final sequence high watermark otherwise agree.

Code observation

AUTO_INCREMENT generation occurs in the pre-insert path before the duplicate-ignore filtering path shrinks the candidate batch. The remaining rows retain candidates associated with original positions, which matches the observed positional gaps.

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. added
    severity/s0
    and removed on Sep 8, 2026
  5. XuPeng-SH commented on Sep 8, 2026

    @XuPeng-SH
    Contributor

    Additional boundary: CHECK/FK-rejected rows also change the generated-ID mapping

    A second, reproducible form of the same INSERT IGNORE contract gap exists when a row is ignored by a CHECK or child FOREIGN KEY constraint, rather than by a duplicate key.

    Reproducer:

    CREATE TABLE checks_case(
      id BIGINT AUTO_INCREMENT PRIMARY KEY,
      uk INT UNIQUE,
      v INT CHECK(v > 0)
    );
    
    INSERT IGNORE INTO checks_case VALUES
      (NULL,1,-1), (NULL,2,1), (NULL,2,1), (NULL,3,1);
    
    SELECT id, uk FROM checks_case ORDER BY id;

    Observed result for (id, uk):

    Endpoint Result
    Exact main at 0b195e4f5a23c06a40552a8db3e7fdf182979a3a (2,2), (4,3)
    PR #28349 head 0254156e80d190a2b7036b99f101ff816629cb60 (2,2), (3,3)
    MySQL 8.4.10 (1,2), (2,3)

    The same result is reproduced with a child-FK violation: the first input row references a missing parent, while later rows reference an existing parent.

    Root cause and scope

    The execution order is currently:

    1. PRE_INSERT allocates AUTO_INCREMENT candidates for the input rows.
    2. INSERT IGNORE applies CHECK/FK row filters.
    3. The ordered PK/UK arbiter assigns the surviving rows.

    Therefore, a candidate belonging to a CHECK/FK-rejected row is removed before the arbiter can reuse it. PR #28349 improves the PK/UK arbitration path, but does not yet carry rejected-row provenance across these independent filters.

    This is an inherited compatibility gap, not a regression newly introduced by PR #28349: the exact main baseline produces 2,4, while the PR improves it to 2,3. It should nevertheless be tracked before claiming that #28238 is fully closed for every INSERT IGNORE rejection path.

    Recommended follow-up

    Extend the acceptance criteria of this issue to cover all rows ignored by INSERT IGNORE, including duplicate-key, CHECK, and FK rejection:

    • preserve candidate provenance until the final accepted-row decision, or evaluate allocation-independent constraints before allocation where that is semantically safe;
    • preserve warning/error ordering and do not evaluate volatile expressions twice;
    • do not roll back the shared allocator or add per-row allocator transactions;
    • cover first/middle/all rejected rows, mixed PK+UK+CHECK/FK cases, non-default session series, batch boundaries, LAST_INSERT_ID(), and base/index final-row identity.

    The separate limitation where a rejected positive explicit ID advances eager allocation remains documented in PR #28349 and should be tracked independently if it is not already covered by another issue.

  6. XuPeng-SH commented on Sep 8, 2026

    @XuPeng-SH
    Contributor

    Scope update after PR #28349

    PR #28349 has been merged into main. It fixes the original duplicate PK/UK INSERT IGNORE case: accepted rows are assigned AUTO_INCREMENT values in accepted-row order, while unused values may still remain reserved for the next statement. Composite and prefix unique-key arbitration, final index row identity, rollback/error cleanup, and multi-batch candidate handling are also covered by the merged tests.

    This issue should remain open for the broader INSERT IGNORE contract. The following boundary is still reproducible and is not closed by the merged change:

    CREATE TABLE checks_case(
      id BIGINT AUTO_INCREMENT PRIMARY KEY,
      uk INT UNIQUE,
      v INT CHECK(v > 0)
    );
    
    INSERT IGNORE INTO checks_case VALUES
      (NULL,1,-1), (NULL,2,1), (NULL,2,1), (NULL,3,1);

    The CHECK/FK rejection occurs before the ordered PK/UK arbiter receives the rows, so the rejected row's candidate provenance is not available for accepted-row assignment. The observed result on the PR validation head was (2,2),(3,3), while MySQL returns (1,2),(2,3). The CHECK cases added to the PR use valid CHECK values and therefore do not cover this rejection path.

    The separate inherited case where a rejected positive explicit ID causes eager allocation/range checking to advance or fail is documented in the PR and also needs independent follow-up. The next fix should cover duplicate, CHECK, FK, and mixed rejection paths without rolling back globally owned reservations or evaluating volatile expressions twice.

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