MDEV-40860 INSERT/REPLACE ... SELECT into temporary table may cause silent master/slave divergence #6240
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 #6240"
general
weight: 0.35
gkodinov
Description
With
binlog_format=MIXEDandcreate_tmp_table_binlog_formatsincludingMIXED,CREATE TEMPORARY TABLEis written to the binary log, and so are later changes to the temporary table.REPLACE ... SELECTis 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 exampleINSERT INTO t1 SELECT FROM tmp, was logged as a statement, and the replica computed it from its own stale copy of the temporary table.t1then differed between master and replica, with no error on the replica.INSERT IGNORE ... SELECTandINSERT ... SELECT ... ON DUPLICATE KEY UPDATEbehave 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()andmysql_insert()do mark it in that case. The check inTHD::mark_tmp_table_as_free_for_reuse(), which marks temporary tables that were changed but not logged, did not catch it either:~select_insert()callsha_reset()before that check, andha_reset()clearsmark_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 conditionabort_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|INSERTlogged asQuery. Mastert1: (1,102) (2,203) (3,304), replicat1: (1,10) (2,20) |INSERTlogged asWrite_rows,t1is the same on both ||
INSERT IGNORE ... SELECT| Mastert1: (1,10) (2,20) (3,304), replicat1: (1,10) (2,20) |t1is the same on both ||
INSERT ... SELECT ... ON DUPLICATE KEY UPDATE| Mastert1: (1,102) (2,203) (3,304), replicat1: (1,10) (2,20) |t1is the same on both |Side effects: behaviour changes only for a temporary table whose
CREATEwas logged and whose changes byINSERT/REPLACE ... SELECTwere not logged. Later statements that use that table are row logged, as MDEV-36099 intends. Whenprepare_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. ForCREATE 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=MIXEDwhen a logged temporary table was changed withREPLACE ... SELECT,INSERT IGNORE ... SELECTorINSERT ... 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 logsCREATE TEMPORARY TABLE. For each of the three statements, a new temporary table is changed with that statement,INSERT INTO t1 SELECT FROM tmpis run, anddiff_tablescomparest1on master and replica. For the first statement the binary log is shown, and it must containWrite_rowsfort1. A last case runs anINSERT IGNORE ... SELECTthat changes nothing: the table stays up to date, and the nextINSERTis still logged as a statement. Without the fix, the test fails at the firstdiff_tables(3 rows on master, 2 on the replica), and each of the three statements gives a differentt1on master and replica. With the fix, the test passes, also with--repeat=3,--ps-protocoland--cursor-protocol. The test case from the Jira issue also passes.These cases were also checked, by comparing
t1on master and replica after reading the temporary table into it:REPLACE ... SELECTin a stored procedure, in a transaction, and a safeINSERT ... SELECTthat reads a temporary table that is already not up to date.CREATE TEMPORARY TABLE ... SELECTwith and withoutIGNORE,binlog_format=STATEMENTandROW,REPLACE ... SELECTin a stored function, andUPDATE/DELETE ... LIMITon the temporary table.The 32 tests changed by MDEV-36099 pass (41 runs). In the full
binlogandrplsuites, 1534 tests pass, 74 are skipped and none fail. Inbinlog_encryption,binlog_in_engineandmulti_source, 228 tests pass, 9 are skipped and none fail. In themainsuite, 1429 tests pass, 70 are skipped and two fail:main.opt_trace_store_ddlsandmain.sp-security. Both fail the same way on this macOS machine without this change.Basing the PR against the correct MariaDB version
mainbranch.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
This contribution is made under the MariaDB Contributor Agreement (MCA).