Details
-
Task
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
None
-
None
-
None
Description
The Optimizer Context capture (thd->opt_ctx_recorder) and replay (thd->opt_ctx_replay) hooks are scattered across the SQL layer:
- hook_records_in_range() in sql/opt_range.cc
- the multi_range_read_info_const() call site in sql/opt_range.cc
- cost_for_index_read() in sql/sql_select.cc
- set_statistics_for_table() in sql/sql_statistics.cc
Only handler::ha_scan_time() is hooked inside the handler. Because the hooks live at individual call sites, any new optimizer call to the handler that bypasses them breaks record/replay silently. The opt_range.cc code is also cluttered with if (opt_ctx_replay) and if (opt_ctx_recorder) blocks.
Proposal
Intercept at the handler interface so that record and replay work for all storage engines without per-engine code.
1. Add non-virtual wrappers in sql/handler.
{h,cc}:
- ha_records_in_range()
- ha_multi_range_read_info_const()
- ha_scan_time() (add the recorder call)
- ha_index_read_cost()
- ha_info_stats(), which infuses or records table stats right after info(), so the engine cannot overwrite infused values
2. Each wrapper calls the engine virtual, then infuses (replay) and records. In replay, the engine call may be skipped when the key matches.
3. Add a re-entrancy guard so only the outermost call intercepts. Internal calls must not intercept: - handler::multi_range_read_info_const() calling this->records_in_range()
- ha_partition calling m_file[i] sub-handlers (mark the children non-intercepting)
4. Gate on the optimizing phase so DML-time callers do not record or replay.
5. Keep thd->opt_ctx_replay and thd->opt_ctx_recorder as the per-statement state holders. The wrappers reach them via table->in_use.
6. Remove the old SQL-layer hooks.
Out of scope / stays in the SQL layer
- cost_for_index_read() glue, unless ha_index_read_cost() replaces it
- subquery and row capture (record_subquery_exec, record_table_row)
- the table_records clamp and the max_index_blocks / max_row_blocks handling in opt_range.cc. The replayed values are returned from the wrapper through an out struct.
Engine impact
- No engine changes are needed. MyISAM, InnoDB and Aria route MRR through ds_mrr.dsmrr_info_const() into handler::multi_range_read_info_const().
- ha_partition needs the guard described above.
- Spider, Connect and Videx override MRR and must be checked.
Risks
- A re-entrancy bug makes replay mismatch silently.
- The info() ordering has to be right.
- handler.cc is shared by all engines, so the blast radius is large.