Bug #105017 Execute a statement with secondary engine, two entries in performance schema
Submitted: 23 Sep 2021 9:10 Modified: 23 Sep 2021 9:17
Reporter: Ze Yang (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.26 OS:Any
Assigned to: CPU Architecture:Any

[23 Sep 2021 9:10] Ze Yang
Description:
Execute a statement with secondary engine, there are two entries in performance schema.

In sql_parse.cc
static void check_secondary_engine_statement(), when statement is restarted in another storage engine the performance schema end the last statement and start a statement again. This make a statement with secondary engine will have two or more entries in performance schema.

```
1460   // Tell performance schema that the statement is restarted.
1461   MYSQL_END_STATEMENT(thd->m_statement_psi, thd->get_stmt_da());
1462   thd->m_statement_psi = MYSQL_START_STATEMENT(
1463       &thd->m_statement_state, com_statement_info[thd->get_command()].m_key,
1464       thd->db().str, thd->db().length, thd->charset(), nullptr);
```

How to repeat:
Add testcase in mysql-test/suite/secondary_engine/t/
secondary_engine_performance.test

./mtr secondary_engine.secondary_engine_performance

```
--disable_query_log
eval INSTALL PLUGIN mock SONAME '$MOCK_PLUGIN';
--enable_query_log

CREATE TABLE t1(id INT PRIMARY KEY, x INT, y INT);
INSERT INTO t1 VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);

ALTER TABLE t1 SECONDARY_ENGINE MOCK;
ALTER TABLE t1 SECONDARY_LOAD;

SELECT * FROM t1;
select SOURCE, SQL_TEXT, DIGEST  from performance_schema.events_statements_history_long where sql_text = "SELECT * FROM t1";

DROP TABLE t1;
--disable_query_log
UNINSTALL PLUGIN mock;
--enable_query_log
```

Result two entries in performance schema.
```

CREATE TABLE t1(id INT PRIMARY KEY, x INT, y INT);
INSERT INTO t1 VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);
ALTER TABLE t1 SECONDARY_ENGINE MOCK;
ALTER TABLE t1 SECONDARY_LOAD;
SELECT * FROM t1;
id	x	y
select SOURCE, SQL_TEXT, DIGEST  from performance_schema.events_statements_history_long where sql_text = "SELECT * FROM t1";
SOURCE	SQL_TEXT	DIGEST
init_net_server_extension.cc:96	SELECT * FROM t1	533c0a9cf0cf92d2c26e7fe8821735eb4a72c409aaca24f9f281d137427bfa9a
sql_parse.cc:1464	SELECT * FROM t1	533c0a9cf0cf92d2c26e7fe8821735eb4a72c409aaca24f9f281d137427bfa9a
DROP TABLE t1;
```

Suggested fix:
Will one statement match one entry in performance schema better?
[23 Sep 2021 9:17] MySQL Verification Team
Hello Ze Yang,

Thank you for the report and feedback.

regards,
Umesh