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

INSERT ... SELECT ... ON DUPLICATE KEY UPDATE with a no-op update reuses a stale auto-increment id: updates rows of other sessions and loses rows

    XMLWordPrintable

Details

    • Can result in data loss

    Description

      With innodb_autoinc_lock_mode=2, INSERT ... SELECT ... ON DUPLICATE KEY UPDATE into a table with an AUTO_INCREMENT primary key and a separate UNIQUE key, running concurrently with other inserts, updates rows of other sessions and does not insert some of its own rows. It happens when the ODKU update does not change the row.

      Cause. write_record() (sql/sql_insert.cc) first restores the auto-increment cursor to prev_insert_id, which is next_insert_id at the start of the row and may be stale. It is corrected later by restore_auto_increment(prev_insert_id_for_cur_row), but prev_insert_id_for_cur_row is assigned only inside if (different_records). For a no-op update the cursor stays stale. The next row reuses an id that another session already took, gets a duplicate on PRIMARY, and ODKU updates that session's row.

      MySQL fixed the same problem as Bug#30194841 (bug #96578, commit cbd87693). MariaDB took only the test, main.insert_debug. It passes because its ODKU always changes the row, and it is skipped on release builds.

      Test case. Runs on release builds: GET_LOCK() pauses the statement after its first row instead of DEBUG_SYNC.
      -master.opt: --innodb_autoinc_lock_mode=2

      --source include/have_innodb.inc
       
      CREATE TABLE t1 (c1 VARCHAR(10) NOT NULL, c2 VARCHAR(10) NOT NULL) ENGINE=InnoDB;
      INSERT INTO t1 VALUES ('A1','B1'),('A2','B2'),('A3','B3'),('A4','B4'),('A5','B5'),('A6','B6'),('A7','B7');
      CREATE TABLE t2 (c1 VARCHAR(10) NOT NULL, c2 VARCHAR(10) NOT NULL) ENGINE=InnoDB;
      INSERT INTO t2 VALUES ('A3','B3'),('A2','B2'),('A4','B4'),('A5','B5');
       
      --connect (con1,localhost,root,,)
      --connection default
       
      --let $i= 2
      while ($i)
      {
        if ($i == 2)
        {
          --echo # ODKU changes the row (works)
          --let $odku= c2=VALUES(c2), c3=CONCAT(VALUES(c3),'u')
        }
        if ($i == 1)
        {
          --echo # ODKU is a no-op (fails)
          --let $odku= c2=VALUES(c2), c3=VALUES(c3)
        }
        CREATE TABLE result (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, c1 VARCHAR(10) NOT NULL,
          c2 VARCHAR(10), c3 VARCHAR(10), PRIMARY KEY (id), UNIQUE KEY (c1)) ENGINE=InnoDB;
       
        SELECT GET_LOCK('pause_after_first_row', 0);
       
        --connection con1
        # Writes A1 (id 1), then blocks while evaluating the WHERE of A2
        --send_eval INSERT INTO result (c1, c2, c3) SELECT c1, c2, 'X' FROM t1 WHERE IF(c1 = 'A2', GET_LOCK('pause_after_first_row', 300), 1) ON DUPLICATE KEY UPDATE $odku
       
        --connection default
        let $wait_condition= SELECT COUNT(*) = 1 FROM information_schema.processlist
          WHERE state = 'User lock' AND info LIKE 'INSERT INTO result%';
        --source include/wait_condition.inc
        # Takes ids 2..5 while con1 owns only id 1
        INSERT INTO result (c1, c2, c3) SELECT c1, c2, 'X' FROM t2;
        SELECT RELEASE_LOCK('pause_after_first_row');
       
        --connection con1
        --reap
        SELECT RELEASE_LOCK('pause_after_first_row');
       
        --connection default
        SELECT c1, c2 FROM result ORDER BY c1;
        DROP TABLE result;
        --dec $i
      }
       
      --disconnect con1
      DROP TABLE t1, t2;
      

      The no-op case on 10.11.11 fails in 5 of 5 runs:

       c1	c2
       A1	B1
       A2	B2
      -A3	B3
      +A3	B7
       A4	B4
       A5	B5
      -A6	B6
      -A7	B7
      

      Row A3 was inserted by con2, and A7's ODKU value was written into it. A6 and A7 are not inserted. The case where ODKU changes the row returns all 7 rows correctly.

      The bug also reproduces on 10.11.20 under concurrent load: 2 sessions repeat INSERT ... SELECT ... ON DUPLICATE KEY UPDATE value=VALUES(value), and 4 sessions insert new rows. Rows of the other sessions end up with the ODKU value. There is no corruption with innodb_autoinc_lock_mode=1, or when the unique key is the primary key and there is no AUTO_INCREMENT column.

      We hit this in production on 10.11.11: a value from one row appeared in an unrelated row.

      Suggested fix: set prev_insert_id_for_cur_row for every duplicate-key row, not only when different_records is true.

      Possibly related: MDEV-24613.

      Attachments

        1. insert_update_autoinc_noop.result
          2 kB
          Aleksandr Kholodov
        2. insert_update_autoinc_noop.test
          2 kB
          Aleksandr Kholodov
        3. insert_update_autoinc_noop-master.opt
          0.0 kB
          Aleksandr Kholodov

        Activity

          People

            sanja Oleksandr Byelkin
            alho Aleksandr Kholodov
            Oleksandr Byelkin Oleksandr Byelkin
            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.