MDEV-42658 ODKU no-op update reuses a stale auto-increment id #6289
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
gkodinov
Description
With
innodb_autoinc_lock_mode=2,INSERT ... SELECT ... ON DUPLICATE KEY UPDATEinto 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 toprev_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 byrestore_auto_increment(prev_insert_id_for_cur_row), butprev_insert_id_for_cur_rowwas 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_rowon 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):
With the fix, all seven rows are present and no row of the other session is modified:
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 withGET_LOCK()evaluated in the WHERE clause, lets a second connection insert conflicting rows, then resumes. Unlikemain.insert_debugit 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):
main.insert_odku_noop_autoincfails with the wrong-row output above; the row-changing control case passes.insert(9 tests), mainauto_increment(3), innodbautoinc(3) andinnodb-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
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.