Bug #121940 ALTER TABLE progress: WORK_ESTIMATED equals WORK_COMPLETED (100%) when more than one index is built

Submitted: 26 Nov 3:03
Reporter: geonhwan park Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S3 (Non-critical)
Version:26.10.0, 9.7.2, 8.4.13, 8.0.46 OS:Any
Assigned to: CPU Architecture:Any

[26 Nov 3:03] geonhwan park
Description:
The manual describes monitoring ALTER TABLE progress with
performance_schema.events_stages_current (WORK_COMPLETED / WORK_ESTIMATED).

Since parallel DDL (WL#14283, 8.0.27), this ratio is no longer usable when the
ALTER builds more than one index, which includes every table rebuild of a table
that has secondary indexes. WORK_ESTIMATED starts at the value for a single
index, WORK_COMPLETED passes it early in the "read PK and internal sort" stage,
and from then on WORK_ESTIMATED is simply raised to equal WORK_COMPLETED. The
progress reads 100% for most of the operation.

Sampling events_stages_current every 0.25 s on a 1,000,000-row table
(PK + 2 secondary indexes), statement: ALTER TABLE t1 ENGINE=InnoDB, ALGORITHM=INPLACE

26.10.0 (first samples, session thread):

    stage                          WORK_COMPLETED  WORK_ESTIMATED   pct
    (read PK and internal sort)               204           70317    0.3
    (read PK and internal sort)             13212           70317   18.8
    (read PK and internal sort)             27912           70317   39.7
    (read PK and internal sort)             38988           70317   55.4
    (read PK and internal sort)             54324           70317   77.3
    (read PK and internal sort)             72648           72648  100.0
    (read PK and internal sort)             92160           92160  100.0
    (read PK and internal sort)            108876          108876  100.0
    ... stays at 100.0 until the end (last sample 267710 / 267710)

    40 of 45 samples show 100.0%.

8.0.26 (before WL#14283), same table and statement:

    (read PK and internal sort)               420          257829    0.2
    (read PK and internal sort)             63096          257829   24.5
    (read PK and internal sort)            107538          257829   41.7
    (insert)                               186310          401749   46.4
    (merge sort)                           270443          401749   67.3
    (insert)                               348852          401749   86.8
    (flush)                                410502          410502  100.0

Number of samples where every stage row shows 100.0%:

    version   add 1 index   add 2 indexes   rebuild (PK + 2 indexes)
    8.0.26        0 / 41        5 / 15          23 / 70   (only at the very end: last insert + flush samples)
    8.0.46        0 / 56       11 / 13         131 / 139
    8.4.13        0 / 15        7 / 8           41 / 47
    9.7.2         0 / 13        6 / 8           41 / 46
    26.10.0       0 / 14       11 / 15          40 / 45

Note the initial WORK_ESTIMATED: with two indexes 26.10.0 starts at 70317, the
same value as for one index. 8.0.26 starts at 117195 for two indexes and 257829
for the rebuild.

Also, during the rebuild several threads have their own row in
events_stages_current (the session thread plus DDL worker threads), each with
WORK_COMPLETED == WORK_ESTIMATED, so there is no single row to follow either.

How to repeat:
Start the server with the stage instruments and consumers enabled:

    --performance-schema-instrument='stage/innodb/alter%=ON'
    --performance-schema-consumer-events-stages-current=ON

Session 1:

    CREATE DATABASE r;
    CREATE TABLE r.t1 (
      id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      c1 INT, c2 TEXT, c3 INT,
      KEY idx_1 (c1), KEY idx_3 (c3)
    ) ENGINE=InnoDB;
    SET SESSION cte_max_recursion_depth = 10000000;
    INSERT INTO r.t1 (c1, c2, c3)
    WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM s WHERE n < 1000000)
    SELECT n % 1000, REPEAT('x', 200), n % 777 FROM s;
    ANALYZE TABLE r.t1;

    ALTER TABLE r.t1 ENGINE=InnoDB, ALGORITHM=INPLACE;
    -- or: ALTER TABLE r.t1 ADD INDEX idx_a (c1,c3), ADD INDEX idx_b (c3,c1), ALGORITHM=INPLACE;

Session 2, repeatedly while the ALTER runs:

    SELECT THREAD_ID, EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED,
           ROUND(100*WORK_COMPLETED/WORK_ESTIMATED, 1) AS pct
    FROM performance_schema.events_stages_current
    WHERE EVENT_NAME LIKE 'stage/innodb/alter%';

A script that does this against the official Docker images (poll.sh) and the raw sample output will be attached.

Suggested fix:
Analysis from reading the source, not verified with a patch:

- storage/innobase/ddl/ddl0builder.cc, Builder::init(): every Builder (one per
  index) creates its own copy of Alter_stage and calls
  m_local_stage->begin_phase_read_pk(1), so each copy estimates the work for
  one index only.
- The copies report into the stage progress of the same session thread, so
  WORK_COMPLETED accumulates the work of all indexes.
- Alter_stage::reestimate() in storage/innobase/include/ut0stage.h ends with
  mysql_stage_set_work_estimated(progress, std::max(estimate, completed)),
  which is why the two columns become equal once the one-index estimate is
  exceeded.

The estimate should account for all indexes being built by the statement, as it
did before 8.0.27.

Possibly related: Bug #117176 (duplicated ALTER stage rows in
events_stages_history_long since WL#14283). Same per-builder Alter_stage
design, different symptom.

I would like to work on a patch for this and submit it as a pull request
against trunk. Please let me know if someone is already working on it or
if you prefer a different approach.
[27 Nov 0:48] geonhwan park
poll.sh (samples events_stages_current every 0.25 s while ALTER runs, official Docker images) and its output on 26.10.0 and 8.0.26

Attachment: bug121940-repro.zip (application/zip, text), 3.38 KiB.

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

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