← Back to Pull requests

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

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 21, 2026 14:25 UTC
Author

  • The Jira issue number for this PR is: MDEV-42344
  • 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 at COMMIT. 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:

  • rows inserted by a failed INSERT appear in the table,

  • values written by a failed UPDATE replace the committed ones,

  • rows deleted by a failed DELETE disappear.
  • The client gets an error for each statement, and CHECK TABLE reports OK afterwards.

    Cause. online_alter_log_row() called store_prev_position() before every row, so before_stmt_pos pointed 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, but before_stmt_pos is also the position the statement rollback in online_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_pos now stays at the start of the statement. It is already set when the cache is created and at the end of each statement, so online_alter_log_row() sets it only when it is MY_OFF_T_UNDEF, which happens after ROLLBACK TO SAVEPOINT and after a non-transactional cache was written out.

  • The error path of 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.

  • The statement rollback skips a cache whose before_stmt_pos is undefined: the failed statement did not write to it. This is the case for the cache of another table that ROLLBACK TO SAVEPOINT reset. Truncating such a cache to MY_OFF_T_UNDEF made the next write to it fail an assertion in my_seek().
  • | Case (11.4 debug build) | Before | After |
    |---|---|---|
    | The test case from the Jira issue, scaled down to 3000 rows: failed INSERT ... SELECT, UPDATE and DELETE in 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 before COMMIT |
    | The same with XA START ... XA PREPARE / XA COMMIT (failed INSERT ... SELECT of 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 |
    | UPDATE in 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 SAVEPOINT resets the cache of t2, then a statement on t1 fails | Assertion pos != (~(my_off_t) 0) in my_seek() at COMMIT | 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. Only online_alter.cc handles these positions; the binlog's own caches are not touched.

    Release Notes

    Fixed a bug where, while an online ALTER TABLE was 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 failed INSERT appeared and committed rows were changed or deleted.

    How can this PR be tested?

    Three cases were added to main.alter_table_online_debug, all using debug_sync to pause the ALTER while it copies:

    1. A transaction with a failed INSERT ... SELECT, UPDATE and DELETE, each big enough to write several rows events (also with the 64K binlog_row_event_max_size of later versions), followed by SAVEPOINT, a statement, ROLLBACK TO SAVEPOINT and another failed INSERT. The table after the ALTER must be the same as the transaction's own view before COMMIT.
    2. A transaction with an UPDATE that fails because the online ALTER cache is full (online_alter_small_cache), which goes through the error path of online_alter_log_row() and then the statement rollback.
    3. Two tables altered online at the same time, where ROLLBACK TO SAVEPOINT resets 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-protocol and --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 the MY_OFF_T_UNDEF check in online_alter_log_row(), the rows of the failed INSERT after ROLLBACK TO SAVEPOINT are 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:

  • XA PREPARE / XA COMMIT: also fixed by the same change.

  • A failed statement in a stored procedure with a CONTINUE HANDLER: rolled back.

  • A failed statement inside a stored function with a CONTINUE HANDLER: its rows are kept by InnoDB, as before, and they are also in the new table.

  • A failed statement fired by a trigger: rolled back with the outer statement.

  • A failed statement on MyISAM, which is non-transactional: its rows are kept, as before.
  • 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.online and sys_vars.innodb_online_alter_log_max_size_basic.

    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 maintained branch in which the bug can be reproduced.*
  • Online ALTER with LOCK=NONE for copying ALTERs exists since 11.2, so 11.4 is the earliest maintained branch with the bug.

    PR quality check


  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.

  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.
  • This contribution is made under the MariaDB Contributor Agreement (MCA).

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: gkodinov Review duration: 13911m
    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
    0.52
    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%
    46%
    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.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #6190"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 27.7% · Safe to merge
    27.7%
    Repository