Skip to content

MDEV-40388 Optimizer context records unusable DDL for SEQUENCE tables#5432

Closed
LukeYL026 wants to merge 1 commit into
MariaDB:mainfrom
LukeYL026:mdev-40388-opt-context-records-unusable-sequence-ddl
Closed

MDEV-40388 Optimizer context records unusable DDL for SEQUENCE tables#5432
LukeYL026 wants to merge 1 commit into
MariaDB:mainfrom
LukeYL026:mdev-40388-opt-context-records-unusable-sequence-ddl

Conversation

@LukeYL026

@LukeYL026 LukeYL026 commented Jul 21, 2026

Copy link
Copy Markdown

Description

This is a draft PR including MTR regression test and example of adding flag to identify sequence tables, in case it may be of use.

With optimizer_record_context enabled, the optimizer trace records a
CREATE TABLE statement for every base table referenced by a
SELECT/INSERT/DELETE/UPDATE query, so that an Optimizer Context Replay server
(MDEV-39368) can recreate the query's context. The recording code emitted a
CREATE TABLE ... ENGINE=SEQUENCE for the auto-generated SEQUENCE-engine
helper tables (seq_<from>_to_<to>[_step_<step>]).

Replaying that DDL fails:

ERROR 1050 (42S01): Table 'seq_15_to_1_step_12345' already exists

The SEQUENCE engine discovers such tables purely from their name pattern — the
table exists the moment it is referenced and can never be created with an
explicit CREATE TABLE (ha_seq::create() returns HA_ERR_WRONG_COMMAND), so
the recorded DDL is unusable on replay.

Root cause

is_base_table() in sql/opt_trace_ddl_info.cc records the DDL of every table
in the TABLE_CATEGORY_USER category. It did not exclude engines that determine
a table's existence from its name alone, so SEQUENCE tables were treated like
ordinary base tables and their (unusable) DDL was recorded.

Fix

  • sql/handler.h: add a handlerton capability flag
    HTON_TABLE_EXISTS_BY_NAME (1 << 4). An engine sets it to declare that its
    tables are discovered from the table name alone and cannot be created with an
    explicit CREATE TABLE.
  • storage/sequence/sequence.cc: the SEQUENCE storage engine sets
    HTON_TABLE_EXISTS_BY_NAME in its init().
  • sql/opt_trace_ddl_info.cc: is_base_table() now also returns false when
    the table's engine has HTON_TABLE_EXISTS_BY_NAME set, so no DDL is recorded
    for such tables. All other base tables (InnoDB, MyISAM, views, ...) are
    unaffected.

Using a declared handlerton flag (rather than inferring the property from the
engine's discover_table_existence hook) states the intent explicitly and is
opt-in: only engines that set the flag are skipped. The flag occupies a
previously unused bit and defaults to 0 (handlertons are zero-filled at
allocation), so no other engine's behaviour changes. PERFORMANCE_SCHEMA tables
remain excluded earlier in is_base_table() by the table-category check.

How this PR Can Be Tested

New MTR test main.opt_trace_store_ddls_sequence:

  • A query over seq_15_to_1_step_12345 records no DDL (the list_ddls array is
    empty).
  • A join of a regular MyISAM table t1 with seq_1_to_10 records the DDL for
    t1 only, confirming normal tables are still captured while the sequence
    table is skipped.

Verification performed on a Debug build of main:

  • Unfixed base: main.opt_trace_store_ddls_sequence fails — the trace records
    CREATE TABLE ... ENGINE=SEQUENCE for both seq_15_to_1_step_12345 and
    seq_1_to_10, matching the JIRA report.
  • With this fix: main.opt_trace_store_ddls_sequence passes.
  • Regression: main.opt_trace_store_ddls (pre-existing), sequence.simple, and
    sequence.group_by all pass.

Basing the PR against the correct MariaDB version

Based on main. The optimizer-context-recording feature (opt_trace_ddl_info.cc,
optimizer_record_context) is not present in any released LTS (10.6, 10.11,
11.4, 11.8); it lives only on main, so there is no older affected branch to
base on.

Release Notes

The optimizer context (optimizer_record_context) no longer records an
unusable CREATE TABLE ... ENGINE=SEQUENCE statement for auto-generated
SEQUENCE tables, so recorded contexts referencing sequence tables can be
replayed.

All new code of the whole pull request, including one or several files that are
either new files or modified ones, are contributed under the BSD-new license. I
am contributing on behalf of my employer Amazon Web Services, Inc.

With optimizer_record_context enabled, the optimizer trace records a
CREATE TABLE statement for every base table a query references, so an
Optimizer Context Replay server (MDEV-39368) can rebuild the query's
context. For SEQUENCE-engine helper tables (seq_<from>_to_<to>[_step_<step>])
this recorded a CREATE TABLE ... ENGINE=SEQUENCE that can never be
replayed: the SEQUENCE engine discovers such tables from their name alone
and ha_seq::create() rejects CREATE TABLE, so replay fails with
"Table '...' already exists".

Add a handlerton capability flag HTON_TABLE_EXISTS_BY_NAME that an engine
sets to declare that its tables are discovered from the table name alone
and cannot be created with an explicit CREATE TABLE. The SEQUENCE storage
engine sets it in init(). is_base_table() in opt_trace_ddl_info.cc skips
tables whose engine has the flag, so their DDL is not recorded. The flag
uses a previously unused bit and defaults to 0 (handlertons are
zero-filled), so no other engine is affected. PERFORMANCE_SCHEMA tables
remain excluded earlier by the table-category check.

Test main.opt_trace_store_ddls_sequence: a query over a sequence table
records no DDL, and a join of a regular table with a sequence table
records only the regular table's DDL.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Jul 23, 2026
@LukeYL026

Copy link
Copy Markdown
Author

Closing this PR - PR 5427 is a simpler fix for the issue that takes advantage of the fact that we already have a means of identifying sequence tables, and can avoid the introduction of flags in my draft.

@LukeYL026 LukeYL026 closed this Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

2 participants