Bug #122072 util.loadDump in two passes (loadData: false, then loadDdl: false) fires the dump's triggers on every loaded row

Submitted: 5 Dec 2:49
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:Shell Dump & Load Severity:S2 (Serious)
Version:MySQL Shell 26.10.0.1, server 26.10.0.1 OS:Any
Assigned to: CPU Architecture:Any

[5 Dec 2:49] Ke Han
Description:
The manual offers loading a dump in two passes: "The dump loading utility
also lets you separately apply the DDL files and data files from a regular
dump that contains both sorts of files", and loadIndexes "can be useful if
you are loading the DDL files and data files separately, and if you want to
make changes to the table structure after loading the DDL files".

If the dump has triggers, the DDL pass (loadData: false) creates them on the
empty tables, so the data pass (loadDdl: false) fires them on every row it
loads:
- BEFORE INSERT triggers rewrite the dumped values: an original created_at
  becomes the load time, and a stored amount gets the tax added again;
- AFTER INSERT triggers write rows: an audit table gets a second entry for
  every loaded row.
Both passes report "0 warnings were reported during the load." A
single-pass load of the same dump is identical to the source, because the
loader creates triggers only after the data of the run.

Cause, in modules/util/load/dump_loader.cc (mysql-shell 1161d07bad16):
the loader keeps triggers away from the data by running their DDL at the
end of a run. Dump_loader::on_dump_end() (line 2396) calls on_schema_end()
for every schema after all data of the run is loaded, and on_schema_end()
(line 2489) executes each table's triggers script when
m_options.load_ddl() is set. A loadData: false run loads no data, so the
triggers are created right after the tables. The following loadDdl: false
run skips all DDL and loads every chunk with LOAD DATA into tables whose
triggers are active (INSERT triggers fire for LOAD DATA). Nothing in the
data-only run checks for, disables, or warns about the dump's triggers.

How to repeat:
One server with local_infile enabled (util.loadDump needs it). In the mysql
client:

SET GLOBAL local_infile = ON;
CREATE DATABASE shop;
USE shop;
CREATE TABLE orders (id INT PRIMARY KEY, amount DECIMAL(10,2), created_at DATETIME(6), note VARCHAR(20));
CREATE TABLE orders_audit (id INT AUTO_INCREMENT PRIMARY KEY, order_id INT, action VARCHAR(10));
CREATE TRIGGER orders_bi BEFORE INSERT ON orders FOR EACH ROW
  SET NEW.created_at = NOW(6), NEW.amount = ROUND(NEW.amount * 1.20, 2);
CREATE TRIGGER orders_ai AFTER INSERT ON orders FOR EACH ROW
  INSERT INTO orders_audit (order_id, action) VALUES (NEW.id, 'insert');
SET TIMESTAMP = UNIX_TIMESTAMP('2024-03-01 09:00:00');
INSERT INTO orders VALUES (1, 10.00, NULL, 'first'), (2, 25.50, NULL, 'second');
SET TIMESTAMP = DEFAULT;
SELECT * FROM orders;
SELECT * FROM orders_audit;

  orders:       1  12.00  2024-03-01 09:00:00.000000  first
                2  30.60  2024-03-01 09:00:00.000000  second
  orders_audit: (1, 1, insert), (2, 2, insert)

Then in an interactive mysqlsh session (mysqlsh root@localhost --js), dump
the schema, drop it, and load the dump in two passes:

util.dumpSchemas(['shop'], '/tmp/shopdump')
\sql DROP DATABASE shop;
util.loadDump('/tmp/shopdump', {loadData: false})
util.loadDump('/tmp/shopdump', {loadDdl: false})
\sql SELECT * FROM shop.orders;
\sql SELECT * FROM shop.orders_audit;

Result with MySQL Shell 26.10.0.1 (each loadDump ends with "0 warnings were
reported during the load."):
+----+--------+----------------------------+--------+
| id | amount | created_at                 | note   |
+----+--------+----------------------------+--------+
|  1 |  14.40 | 2026-11-11 22:39:03.209665 | first  |
|  2 |  36.72 | 2026-11-11 22:39:03.209665 | second |
+----+--------+----------------------------+--------+
+----+----------+--------+
| id | order_id | action |
+----+----------+--------+
|  1 |        1 | insert |
|  2 |        2 | insert |
|  3 |        1 | insert |
|  4 |        2 | insert |
+----+----------+--------+

Control: \sql DROP DATABASE shop; then util.loadDump('/tmp/shopdump',
{resetProgress: true}) (one pass) gives the source values: 12.00 and 30.60
from 2024-03-01 09:00:00, and 2 audit rows.

Expected: the two-pass load gives the same tables as the source and as the
one-pass load.

Suggested fix:
Create the triggers in the run that loads the data: skip trigger DDL in a
run with loadData: false (or record it as pending in the progress file) and
execute it at the end of the run that loads the data, as a single-pass load
does. At least, print a warning when a data-only run loads into tables that
already have the dump's triggers.

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

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