Bug #121855 Consistent read of a secondary index skips or repeats rows under concurrent updates

Submitted: 22 Nov 10:04 Modified: 22 Nov 10:10
Reporter: Tomas Ulin Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S2 (Serious)
Version:8.4.11, 9.7.4, trunk 06bcb0eb0ad OS:Any
Assigned to: CPU Architecture:Any

[22 Nov 10:04] Tomas Ulin
Description:
In a REPEATABLE READ transaction started WITH CONSISTENT SNAPSHOT, a scan of a
secondary index sometimes returns a row twice, or misses a row, while other
sessions update the table. A scan of the primary key in the same snapshot
returns the correct rows.

Table:

    CREATE TABLE t (pk INT PRIMARY KEY, i INT NOT NULL, v VARCHAR(400) NOT NULL,
                    n INT NOT NULL, KEY k_n (n)) ENGINE=InnoDB;

300 rows. Writers run, in a loop:

    START TRANSACTION;
    UPDATE t SET v = <16 to 320 random characters>, n = LENGTH(v) % 97, i = i + 1
      WHERE i % 11 = FLOOR(RAND() * 11);
    COMMIT; -- or ROLLBACK, 50/50

Readers run, in a loop, in one snapshot:

    SELECT pk, n FROM t FORCE INDEX (k_n) WHERE n >= 0;
    SELECT pk, n FROM t FORCE INDEX (PRIMARY) WHERE pk >= 0;

The first query is a covering index scan of k_n. Its result sometimes differs
from the second. A row (pk, n) that exists in the snapshot is returned 0 or
2 times. The value of n returned is always the one in the snapshot.

The scan of k_n also waits 0.1 ms per row, with SLEEP in the WHERE clause.
This only keeps the scan open longer, which makes the bug much more frequent.
Without the wait it still happens.

Results, 120 seconds, 4 writers, 4 readers, default server settings:

    8.4.11                     10480 comparisons, 21 wrong
    9.7.4                      10310 comparisons, 23 wrong
    trunk 06bcb0eb0ad          9825 comparisons, 23 wrong (VERSION() 26.10.0)
    9.7.4, no wait (SLEEP=0)   121259 comparisons, 10 wrong

What the bug needs, from narrowing down the workload. Most of these runs used
a self join through k_n as the reader (SELECT ... FROM t a JOIN t c ON
a.n = c.n), which also returns wrong results, and are 120 seconds on 9.7.4:

- The UPDATE changes both the indexed column n and the length of the row.
  With a column of fixed length and n changed: 0 wrong results in 21.8k
  comparisons. With only the length changed: 0 in 69k.
- The column whose length changes can be VARCHAR, VARBINARY or LONGBLOB,
  stored inline. When the indexed column is itself a VARBINARY whose length
  changes, it also happens: 3 in 35.4k.
- Wrong results become frequent with many row changes: about 1M committed
  row changes in 120 seconds. Writers that update one row per statement made
  about 220k row changes and gave 2 wrong results in 20.6k.
- The read must scan the index. Single lookups (WHERE n = <constant>) gave 0
  in 1.1M. Non-covering index reads also return wrong results.
- Not needed: purge (holding it back with an old open snapshot does not
  prevent the bug), virtual columns, off-page storage, DDL.

How to repeat:
### Deterministic, debug build

The MTR test below uses the existing debug sync point
row_search_before_mtr_restart_for_extra_clust to pause the reader in the
middle of the scan, and the existing debug point do_page_reorganize. The first
case returns a row twice, the second misses a row. The recorded result shows
the wrong rows; the comments in the test give the expected ones. Every run on
trunk 06bcb0eb0ad debug gave the same wrong results.

mysql-test/suite/innodb/t/cons_read_cached_old_vers.test:

### Release build, under load

repro.sql:

Run it with:

    MYSQL="mysql -uroot -S /path/to/mysql.sock" ./repro.sh

The first result is the number of comparisons and of wrong ones. The second
lists, for each wrong comparison, the rows (pk, n) the two scans return a
different number of times.

Output on 9.7.4, 120 seconds:

    VERSION()   compared        mismatched
    9.7.4       10310   23
    id  pk      n       by_index        by_pk
    1   265     95      2       1
    2   173     79      0       1
    3   168     78      0       1
    4   39      96      0       1
    5   110     64      0       1
    6   200     32      0       1
    7   259     95      0       1
    8   193     94      2       1
    9   112     31      0       1
[22 Nov 10:07] Tomas Ulin
test

Attachment: cons_read_cached_old_vers.test (application/octet-stream, text), 3.14 KiB.

[22 Nov 10:08] Tomas Ulin
sql for release

Attachment: repro.sql (application/octet-stream, text), 2.43 KiB.

[22 Nov 10:08] Tomas Ulin
repro for release

Attachment: repro.sh (application/x-sh, text), 1.14 KiB.

[22 Nov 10:10] Tomas Ulin
cause from AI:

## Cause

Row_sel_get_clust_rec_for_mysql::operator() in row0sel.cc caches the last
clustered record it looked up and the old version it built for it. When the
next lookup finds a clustered record at the same address, it uses the cached
old version and does not build one:

    if (clust_rec != cached_clust_rec) {
      ...row_sel_build_prev_vers_for_mysql(...)...
      cached_clust_rec = clust_rec;
      cached_old_vers = old_vers;
    } else {
      ...
      old_vers = cached_old_vers;

(row0sel.cc line 3298 in 8.4.11, 3310 in 9.7.4, 3316 in trunk 06bcb0eb0ad.)

The cache lives for one call of row_search_mvcc(). Within that call, after a
secondary index record needed a clustered index lookup, the scan commits the
mini-transaction and starts a new one before it reads the next record
(mtr_has_extra_clust_latch, line 5823 in 8.4.11, 5821 in 9.7.4, 5827 in
trunk). This releases the page latches. Other sessions can then change the
clustered index page, and a different record can end up at the cached
address:

- An UPDATE that changes the size of a record deletes it and inserts it
  again, which frees its old space. A later insert into the page can reuse
  that space.
- A page reorganize moves all records on the page.

The next lookup then gets the old version of a different row. If its columns
match the secondary index record, that version is returned: the other row is
returned a second time. If they do not match, the secondary index record is
skipped: its row is missing.

This explains what the bug needs: records only move when their size changes,
the cache is only used for rows that are newer than the read view, and a
longer scan does more of these mini-transaction restarts.

The cache was added by the fix for Bug #25540277 (Bug #84958), commit
647a3814a91, first released in 8.0.13.

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

Page generated in 0.040 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.