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

INSERT INTO tbl VALUES (subquery) can read the values just inserted

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.5, 10.6, 11.4
    • 11.4
    • Optimizer

    Description

      INSERT statement that inserts multiple rows can read the values it has just inserted.

      create table t10 (a varchar(10), b int);
      insert into t10 values ('original',  0);
      

      If I try to insert into the table I'm reading from, I get an error:

      MariaDB [test]> insert into t10 values ('row1', (select count(*) from t10)), ('row2', (select count(*) from t10)); 
      ERROR 1093 (HY000): Table 't10' is specified twice, both as a target for 'INSERT' and as a separate source for data
      

      but using a table aliases no longer produces an error (not sure why):

      MariaDB [test]> insert into t10 values ('row1', (select count(*) from t10 as T1)), ('row2', (select count(*) from t10 as T2)); 
      Query OK, 2 rows affected (0.001 sec)
      Records: 2  Duplicates: 0  Warnings: 0
      

      The result is:

      MariaDB [test]> select * from t10;
      +----------+------+
      | a        | b    |
      +----------+------+
      | original |    0 |
      | row1     |    1 |
      | row2     |    2 |
      +----------+------+
      

      The "row2-2" is there, because we can already read row1 that we have just inserted. In other databases (except MySQL, I tried 8.4), one would see "row-1" because INSERT is executed as if it could not see the rows it has inserted.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              psergei Sergei Petrunia
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

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