Bug #121902 ON DELETE SET NULL cascade during online table rebuild corrupts index on virtual column

Submitted: 24 Nov 9:01
Reporter: Tomas Ulin Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S3 (Non-critical)
Version:8.4.11, 9.7.4, trunk (26.10.0) OS:Any
Assigned to: CPU Architecture:Any

[24 Nov 9:01] Tomas Ulin
Description:
A child table has a secondary index on a virtual column that is computed from
a foreign key column, and the foreign key is ON DELETE SET NULL. Deleting
parent rows while OPTIMIZE TABLE rebuilds the child table online makes the
rebuild fail with:

  Incorrect key file for table 'c'; try to repair it

On 8.4.11, 19 of 20 OPTIMIZE TABLE runs failed this way. CHECK TABLE on the
child table reports OK afterwards.

On 9.7.4 and trunk the bug shows only with --innodb-native-foreign-keys=ON.
With the default (cascades run by the server layer), all 20 OPTIMIZE TABLE
runs succeed.

With --innodb-native-foreign-keys=ON:
- 9.7.4 release build: 16 of 20 runs fail with "Incorrect key file", then the
  server stops with
    Assertion failure: row0ins.cc:271:!cursor->index->is_committed()
- trunk debug build: the server stops on the first run with
    Assertion failure: row0log.cc:2366

On 8.4.11 the bug does not show without the index on the virtual column, or
without the virtual column.

Related: Bug#93670 (index on a virtual column not updated by ON UPDATE
CASCADE, fixed in 5.7.27, no online rebuild involved) and Bug#85605.

How to repeat:
Start a server, then run this in one session:

CREATE DATABASE repro;
USE repro;

CREATE TABLE p (id INT PRIMARY KEY);

CREATE TABLE c (
  id INT PRIMARY KEY,
  fk INT,
  g INT AS (fk + 1) VIRTUAL,
  KEY k_g (g),
  FOREIGN KEY (fk) REFERENCES p (id) ON DELETE SET NULL
);

INSERT INTO p
  WITH RECURSIVE s (k) AS (SELECT 1 UNION ALL SELECT k + 1 FROM s WHERE k < 1000)
  SELECT k FROM s;
INSERT INTO c (id, fk) SELECT id, id FROM p;

DELIMITER //
CREATE PROCEDURE churn(IN n INT)
BEGIN
  DECLARE k INT;
  WHILE n > 0 DO
    SET k = 1 + FLOOR(RAND() * 1000);
    DELETE FROM p WHERE id = k;
    INSERT INTO p VALUES (k);
    UPDATE c SET fk = k WHERE id = k;
    SET n = n - 1;
  END WHILE;
END //
DELIMITER ;

CALL churn(1000000);

While the CALL runs, run this in a second session, about 20 times:

OPTIMIZE TABLE repro.c;

On 8.4.11 most runs return:

repro.c  optimize  error   Incorrect key file for table 'c'; try to repair it
repro.c  optimize  status  Operation failed
On 9.7.4 and trunk, start the server with --innodb-native-foreign-keys=ON.

The attached minimal.sh does all of this:
  BASEDIR=/path/to/mysql ./minimal.sh [mysqld options]
[24 Nov 9:03] Tomas Ulin
minimal.sh

Attachment: minimal.sh (application/x-sh, text), 1.26 KiB.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.052 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.