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

ALTER IGNORE TABLE ... ADD PRIMARY KEY / UNIQUE removes every row of a duplicate group, not just the extra rows (regression in 10.11.17)

    XMLWordPrintable

Details

    • Q4/2026 Server Development

    Description

          1. How to repeat

      ```sql
      CREATE TABLE t1 (a INT NOT NULL, b CHAR(1)) ENGINE=InnoDB;
      INSERT INTO t1 VALUES (1,'x'),(2,'y'),(3,'p'),(3,'q');
      ALTER IGNORE TABLE t1 ADD PRIMARY KEY (a);
      SELECT * FROM t1 ORDER BY a;
      ```

          1. Expected result (10.11.16, 10.6.28)

      ```
      1 x
      2 y
      3 p
      ```

          1. Actual result (10.11.17 and later)

      ```
      1 x
      2 y
      ```

      Both rows with `a = 3` are gone, and the statement returns no warning.

          1. Further observations
      Variant 10.6.28 10.11.16 10.11.17+
      — — — —
      `ADD PRIMARY KEY (a)`, a NOT NULL keeps `3 p` keeps `3 p` *both rows lost*
      `ADD PRIMARY KEY (a), ALGORITHM=COPY` keeps `3 p` keeps `3 p` *both rows lost*
      `ADD UNIQUE (a)`, a NOT NULL keeps `3 p` keeps `3 p` *both rows lost*
      `ADD UNIQUE (a)`, a NULL keeps `3 p` keeps `3 p` keeps `3 p` (correct)
      rows (3,'p'),(3,'q'),(3,'r') + `ADD PRIMARY KEY` keeps `3 p` keeps `3 p` keeps *`3 r`*

      The three-row case suggests what goes wrong. When the duplicate `q` is rejected, the rollback also undoes the previously inserted row `p`. `r` is then inserted into an empty slot. It looks as if the per-row undo introduced by the MDEV-36436 fix (10.11.17 / 11.4.11 / 11.8.7 /
      12.3.2) rolls back one row too many. The fix discards the undo record after each inserted row.

      The nullable UNIQUE case is unaffected, probably because it doesn't become the clustered index.

          1. Impact

      Silent data loss on a documented statement whose purpose is to keep one row from each duplicate group. Users de-duplicating a table with `ALTER IGNORE` after upgrading to 10.11.17+ lose every duplicated key.

      Attachments

        Activity

          People

            marko Marko Mäkelä
            rcashell Ronan Cashell
            Thirunarayanan Balathandayuthapani Thirunarayanan Balathandayuthapani
            Marko Mäkelä Marko Mäkelä
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - 1h 22m Original Estimate - 1h 22m
                1h 22m
                Remaining:
                Time Spent - 3h Remaining Estimate - 1h 22m
                1h 22m
                Logged:
                Time Spent - 3h Remaining Estimate - 1h 22m
                3h

                Git Integration

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