MDEV-42344 Online ALTER TABLE (ALGORITHM=COPY, LOCK=NONE, the default): a statement that fails in a concurrent transaction is rolled back in InnoDB, but the rows it had already logged are applied to the new table at COMMIT; committed rows get deleted #6190
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 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.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #6190"
general
weight: 0.35
gkodinov
Description
While an online
ALTER TABLE(ALGORITHM=COPY, LOCK=NONE) runs, the DML of other sessions is written to a per-transaction online ALTER cache, which is applied to the new table atCOMMIT. When a statement fails inside a multi-statement transaction (duplicate key, lock wait timeout, ...), InnoDB rolls the statement back and the transaction goes on, but the rows events that the statement had already written to the cache were kept and applied to the new table:INSERTappear in the table,UPDATEreplace the committed ones,DELETEdisappear.The client gets an error for each statement, and
CHECK TABLEreports OK afterwards.Cause.
online_alter_log_row()calledstore_prev_position()before every row, sobefore_stmt_pospointed to the current end of the cache instead of the start of the statement. This was added in MDEV-31646 so that a failed write could undo a partly written event, butbefore_stmt_posis also the position the statement rollback inonline_alter_end_trans()truncates the cache to. Rows are collected in a pending rows event that is written to the cache when it is full (binlog_row_event_max_size), so a statement rollback only dropped the pending event, and every complete event of the failed statement stayed in the cache.Fix.
before_stmt_posnow stays at the start of the statement. It is already set when the cache is created and at the end of each statement, soonline_alter_log_row()sets it only when it isMY_OFF_T_UNDEF, which happens afterROLLBACK TO SAVEPOINTand after a non-transactional cache was written out.online_alter_log_row()keeps the position before the row in a local variable and still undoes only the write of that row, as before.before_stmt_posis undefined: the failed statement did not write to it. This is the case for the cache of another table thatROLLBACK TO SAVEPOINTreset. Truncating such a cache toMY_OFF_T_UNDEFmade the next write to it fail an assertion inmy_seek().| Case (11.4 debug build) | Before | After |
|---|---|---|
| The test case from the Jira issue, scaled down to 3000 rows: failed
INSERT ... SELECT,UPDATEandDELETEin a transaction during the ALTER | 5516 rows after the ALTER: 2997 from the failed INSERT, 1964 with the failed UPDATE's value, 481 committed rows deleted | 3000 rows, the same as the transaction saw beforeCOMMIT|| The same with
XA START...XA PREPARE/XA COMMIT(failedINSERT ... SELECTof 2000 rows) | 1998 rows of the failed INSERT are applied | rolled back || Failed statement after
ROLLBACK TO SAVEPOINT| its complete rows events are applied | rolled back ||
UPDATEin a transaction that fails because the online ALTER cache is full (online_alter_small_cache) | 12 rows get the failed UPDATE's value | rolled back || Two tables altered online,
ROLLBACK TO SAVEPOINTresets the cache of t2, then a statement on t1 fails |Assertion pos != (~(my_off_t) 0)inmy_seek()atCOMMIT| correct contents in both tables |Side effects: statements that succeed log exactly as before. The error path of
online_alter_log_row()and the handling of non-transactional engines are unchanged. Onlyonline_alter.cchandles these positions; the binlog's own caches are not touched.Release Notes
Fixed a bug where, while an online
ALTER TABLEwas running, a statement that failed inside a concurrent transaction could still have its changes applied to the altered table when the transaction committed, so that rows of a failedINSERTappeared and committed rows were changed or deleted.How can this PR be tested?
Three cases were added to
main.alter_table_online_debug, all usingdebug_syncto pause the ALTER while it copies:1. A transaction with a failed
INSERT ... SELECT,UPDATEandDELETE, each big enough to write several rows events (also with the 64Kbinlog_row_event_max_sizeof later versions), followed bySAVEPOINT, a statement,ROLLBACK TO SAVEPOINTand another failedINSERT. The table after the ALTER must be the same as the transaction's own view beforeCOMMIT.2. A transaction with an
UPDATEthat fails because the online ALTER cache is full (online_alter_small_cache), which goes through the error path ofonline_alter_log_row()and then the statement rollback.3. Two tables altered online at the same time, where
ROLLBACK TO SAVEPOINTresets the cache of one table and a statement on the other table then fails.The test passes in both binlog combinations, with
--ps-protocol,--cursor-protocoland--view-protocol(the new section disables the view protocol, like the existing transaction tests in this file), and with--repeat=5.Without the fix, case 1 makes the ALTER fail with a duplicate key error on rows that were rolled back, also with
--binlog-row-event-max-size=65536, case 2 applies 12 rows of the failed UPDATE, and case 3 crashes the server. Without theMY_OFF_T_UNDEFcheck inonline_alter_log_row(), the rows of the failedINSERTafterROLLBACK TO SAVEPOINTare applied (5000 rows instead of 3000).These paths were checked separately with the fix, and in each the table after the ALTER matched what the transaction saw before
COMMIT:CONTINUE HANDLER: rolled back.CONTINUE HANDLER: its rows are kept by InnoDB, as before, and they are also in the new table.The other tests that use online ALTER pass in all combinations:
main.alter_table,main.partition_alter,main.innodb_mysql_sync,main.sp2,innodb.innodb-alter,innodb.innodb-index,innodb.innodb-table-online,innodb.innodb-wl5980-alter,innodb.autoinc_persist,innodb.innodb_stats_fetch,parts.alter_table,rpl.rpl_alter_extra_persistent,rpl.rpl_alter_online_debug,versioning.onlineandsys_vars.innodb_online_alter_log_max_size_basic.Basing the PR against the correct MariaDB version
Online ALTER with
LOCK=NONEfor copying ALTERs exists since 11.2, so 11.4 is the earliest maintained branch with the bug.PR quality check
This contribution is made under the MariaDB Contributor Agreement (MCA).