Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-42204

Optimizer context script fails on a table with a foreign key: ERROR 1005 errno 150

    XMLWordPrintable

Details

    Description

      Likely cause: commit 85595ce69c7252aa402f45762fbe62fc537da668 (MDEV-36523), in Optimizer_context_recorder::dump_sql_script(). It is not a regression: in CS 12.3 and 13.0, optimizer_record_context puts the same CREATE TABLE in the optimizer trace, and it fails in the same way when it is run.

      --source include/have_innodb.inc
      --source include/not_embedded.inc
       
      CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB;
      CREATE TABLE child (id INT PRIMARY KEY, pid INT, FOREIGN KEY (pid) REFERENCES parent (id)) ENGINE=InnoDB;
      INSERT INTO parent VALUES (1);
      INSERT INTO child VALUES (1,1);
      SET optimizer_record_context=ON;
      SELECT * FROM child;
      SET optimizer_record_context=OFF;
      --eval SELECT context INTO DUMPFILE '$MYSQLTEST_VARDIR/tmp/ctx.sql' FROM information_schema.OPTIMIZER_CONTEXT
      DROP TABLE child, parent;
      --error 0,1
      --exec $MYSQL test < $MYSQLTEST_VARDIR/tmp/ctx.sql > $MYSQLTEST_VARDIR/tmp/ctx.out 2>&1
      SELECT COUNT(*) INTO @c FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 'child';
      --let $c= query_get_value(SELECT @c AS c, c, 1)
      if ($c != 1)
      {
        --die The script from the optimizer context does not create the table child, which has a foreign key to a table the query does not use
      }
      DROP TABLE child;
      --remove_file $MYSQLTEST_VARDIR/tmp/ctx.sql
      --remove_file $MYSQLTEST_VARDIR/tmp/ctx.out

      Leads to:

      CS 13.1.2 87e62ca17270e163a6d41b3fc8d6f4d440f1bb88 (Debug, Clang 22.1.8-20260622) Build 07/11/2026

      mysqltest: At line 19: The script from the optimizer context does not create the table child, which has a foreign key to a table the query does not use

      The script stops at the CREATE TABLE for child:

      CS 13.1.2 87e62ca17270e163a6d41b3fc8d6f4d440f1bb88 (Debug, Clang 22.1.8-20260622) Build 07/11/2026

      ERROR 1005 (HY000) at line 85: Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed")

      Optimized builds give the same result. With SELECT * FROM parent JOIN child ON parent.id = child.pid, the script creates child before parent, and the same error happens. MySQL has no optimizer context.

      From the code it seems that Optimizer_context_recorder::dump_sql_script() in sql/opt_context_store_replay.cc writes the CREATE TABLE statements, foreign keys included, in the reverse order of the tables in the query. The script does not set foreign_key_checks. The test main.opt_context_store_ddls has a case with foreign keys, under the comment "context result should have the ddls in correct order". It runs on MyISAM, which ignores foreign keys, and its result lists the child t2 before the parent t1. The TODO list of MDEV-36483 notes that the DDL list cannot be replayed yet when the tables have foreign keys.

      Attachments

        Issue Links

          Activity

            People

              bsrikanth Srikanth Bondalapati
              Roel Roel Van de Paar
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - 0d
                  0d
                  Remaining:
                  Time Spent - 1h Remaining Estimate - 1.5d
                  1.5d
                  Logged:
                  Time Spent - 1h Remaining Estimate - 1.5d
                  1h

                  Git Integration

                    Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.