← Back to Pull requests

MDEV-42658 ODKU no-op update reuses a stale auto-increment id #6289

Open
unknown opened this pull request with 432 lines diff across 5 files
View on GitHub
unknown unknown commented on Dec 01, 2026 01:01 UTC
Author

  • The Jira issue number for this PR is: MDEV-42658
  • Description

    With innodb_autoinc_lock_mode=2, INSERT ... SELECT ... ON DUPLICATE KEY UPDATE into a table with an AUTO_INCREMENT primary key and a separate UNIQUE key can update rows belonging to another session and lose its own rows, when the ODKU update does not change the row.

    Write_record::insert_on_duplicate_update() restores the auto-increment cursor to prev_insert_id, the value from before the row. A concurrent session can take ids while the statement runs, so that value may be stale. The cursor is corrected later by restore_auto_increment(prev_insert_id_for_cur_row), but prev_insert_id_for_cur_row was only assigned when the update changed the row. For a no-op update the cursor stayed stale: the next row reused an id another session had already taken, got a duplicate on PRIMARY, and ODKU updated that session's row instead of inserting.

    The fix assigns prev_insert_id_for_cur_row on the no-op path as well, so the id generated for the failed insert (which was never used) is restored for the next row. The row-changing path is unchanged. This matches the MySQL fix for Bug#30194841; MariaDB had taken only its test, main.insert_debug, whose ODKU always changes the row and which is skipped on release builds.

    Before the fix, the no-op case of the new test returns (row A3 was inserted by the other session; A6 and A7 are lost):

    c1	c2	c3
    A1	B1	X
    A2	B2	X
    A3	B7	X
    A4	B4	X
    A5	B5	X
    

    With the fix, all seven rows are present and no row of the other session is modified:

    c1	c2	c3
    A1	B1	X
    A2	B2	X
    A3	B3	X
    A4	B4	X
    A5	B5	X
    A6	B6	X
    A7	B7	X
    

    Side effects: only the no-op ODKU path changes, and it now behaves like the row-changing path. LAST_INSERT_ID() semantics are untouched. Auto-increment values consumed after a no-op ODKU update can differ from before (the unused id is reused instead of a stale one), which is the corrected behavior.

    How can this PR be tested?

    New automated test main.insert_odku_noop_autoinc (with -master.opt --innodb_autoinc_lock_mode=2). It pauses the inserting statement after its first row with GET_LOCK() evaluated in the WHERE clause, lets a second connection insert conflicting rows, then resumes. Unlike main.insert_debug it needs no DEBUG_SYNC, so it runs on release builds. It covers both a row-changing ODKU update (control) and a no-op update.

    Verified on a Release build of 10.11 (commit 91678a49de5e067f49626fbea29ed4a703bee80c):

  • Without the fix, main.insert_odku_noop_autoinc fails with the wrong-row output above; the row-changing control case passes.

  • With the fix, the test passes.

  • Regressions with the fix, all passing: main insert (9 tests), main auto_increment (3), innodb autoinc (3) and innodb-autoinc (7). The two debug-only tests (main.insert_debug, innodb.autoinc_debug) are skipped on release builds.
  • Basing the PR against the correct MariaDB version


  • This is a new feature and the PR is based against the latest MariaDB development branch

  • This is a bug fix and the PR is based against the earliest branch in which the bug can be reproduced
  • The bug reproduces on 10.11 (reported on 10.11.11 and 10.11.20); 10.11 is the earliest maintained branch.

    Backward compatibility

    No on-disk format change. A datadir created before the fix works unchanged. The only user-visible difference is the corrected auto-increment behavior described above.

    Diff: 432 lines (+324 / -108) across 5 files Reviewer: gkodinov Review duration: 1342m
    All Vouch review confidence checks passed
    Residual risk 0.29 = Change Risk (0.50) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 432 lines diff across 5 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.363
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #6289"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 28.6% · Safe to merge
    28.6%
    Repository