← Back to Pull requests

MDEV-40860 INSERT/REPLACE ... SELECT into temporary table may cause silent master/slave divergence #6240

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 26, 2026 09:08 UTC
Author

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

    With binlog_format=MIXED and create_tmp_table_binlog_formats including MIXED, CREATE TEMPORARY TABLE is written to the binary log, and so are later changes to the temporary table. REPLACE ... SELECT is unsafe, so MIXED switches it to row logging, and changes to a temporary table are not row logged. The statement is therefore not logged at all. MDEV-36099 requires the temporary table to be marked as not up to date in that case, so that later statements that use it are row logged. This did not happen. The next statement that read the temporary table, for example INSERT INTO t1 SELECT FROM tmp, was logged as a statement, and the replica computed it from its own stale copy of the temporary table. t1 then differed between master and replica, with no error on the replica. INSERT IGNORE ... SELECT and INSERT ... SELECT ... ON DUPLICATE KEY UPDATE behave the same way.

    Cause. select_insert::prepare_eof() marked the table as not up to date only when it did not try to log the statement at all. When it tried and nothing was written (binlog_query() returns -1 under row logging), nothing marked the table. select_insert::abort_result_set() and mysql_insert() do mark it in that case. The check in THD::mark_tmp_table_as_free_for_reuse(), which marks temporary tables that were changed but not logged, did not catch it either: ~select_insert() calls ha_reset() before that check, and ha_reset() clears mark_trx_read_write_done.

    Fix. In select_insert::prepare_eof(), mark the table as not up to date whenever it was changed and the change was not logged. This is the condition abort_result_set() already uses.

    | Statement on the temporary table, followed by INSERT INTO t1 SELECT FROM tmp (12.3 debug build) | Before | After |
    |---|---|---|
    | REPLACE ... SELECT | INSERT logged as Query. Master t1: (1,102) (2,203) (3,304), replica t1: (1,10) (2,20) | INSERT logged as Write_rows, t1 is the same on both |
    | INSERT IGNORE ... SELECT | Master t1: (1,10) (2,20) (3,304), replica t1: (1,10) (2,20) | t1 is the same on both |
    | INSERT ... SELECT ... ON DUPLICATE KEY UPDATE | Master t1: (1,102) (2,203) (3,304), replica t1: (1,10) (2,20) | t1 is the same on both |

    Side effects: behaviour changes only for a temporary table whose CREATE was logged and whose changes by INSERT/REPLACE ... SELECT were not logged. Later statements that use that table are row logged, as MDEV-36099 intends. When prepare_eof() does not try to log the statement (binary log closed, or the table is already not up to date), the behaviour is the same as before. For CREATE TEMPORARY TABLE ... SELECT, the new table is not yet marked as logged at this point, so nothing changes.

    Release Notes

    Fixed silent replica divergence with binlog_format=MIXED when a logged temporary table was changed with REPLACE ... SELECT, INSERT IGNORE ... SELECT or INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, and the temporary table was then used to change another table.

    How can this PR be tested?

    A test was added to rpl.rpl_temp_table_mix_row, which runs with MIXED and logs CREATE TEMPORARY TABLE. For each of the three statements, a new temporary table is changed with that statement, INSERT INTO t1 SELECT FROM tmp is run, and diff_tables compares t1 on master and replica. For the first statement the binary log is shown, and it must contain Write_rows for t1. A last case runs an INSERT IGNORE ... SELECT that changes nothing: the table stays up to date, and the next INSERT is still logged as a statement. Without the fix, the test fails at the first diff_tables (3 rows on master, 2 on the replica), and each of the three statements gives a different t1 on master and replica. With the fix, the test passes, also with --repeat=3, --ps-protocol and --cursor-protocol. The test case from the Jira issue also passes.

    These cases were also checked, by comparing t1 on master and replica after reading the temporary table into it:

  • Fixed by this change: a MyISAM temporary table, REPLACE ... SELECT in a stored procedure, in a transaction, and a safe INSERT ... SELECT that reads a temporary table that is already not up to date.

  • Same result with and without the change: CREATE TEMPORARY TABLE ... SELECT with and without IGNORE, binlog_format=STATEMENT and ROW, REPLACE ... SELECT in a stored function, and UPDATE/DELETE ... LIMIT on the temporary table.
  • The 32 tests changed by MDEV-36099 pass (41 runs). In the full binlog and rpl suites, 1534 tests pass, 74 are skipped and none fail. In binlog_encryption, binlog_in_engine and multi_source, 228 tests pass, 9 are skipped and none fail. In the main suite, 1429 tests pass, 70 are skipped and two fail: main.opt_trace_store_ddls and main.sp-security. Both fail the same way on this macOS machine without this change.

    Basing the PR against the correct MariaDB version


  • This is a new feature or a refactoring, and the PR is based against the main branch.

  • This is a bug fix, and the PR is based against the earliest maintained branch in which the bug can be reproduced.*
  • The code comes from MDEV-36099, first released in 12.0.1, so 12.3 is the earliest maintained branch with the bug. Community 11.8 does not have MDEV-36099. The reporter also reproduced it on an 11.8 Enterprise build with a backport of MDEV-36099.

    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: 7028m
    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 #6240"
    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