Bug #121976 InnoDB: UPDATE on a table with a multi-valued index leaks memory into prebuilt->heap until the handle is closed
Description:
memory/innodb/memory grows without bound on a table with a multi-valued index that is updated continuously.
In production (Temporal's MySQL 8 visibility schema) it reached 8 GB on a 16 GB host in about 12 days, ~0.7 GB/day.
FLUSH TABLES <t> returns all of it immediately (8,090 MB -> 13.7 MB). The memory is not attributed to any
thread in memory_summary_by_thread_by_event_name, and it stays after the client sessions end, because the
table handles stay in table_open_cache.
Trigger: an UPDATE (or INSERT ... ON DUPLICATE KEY UPDATE) that changes the multi-valued value of a row in
which some other indexed virtual column is NULL and is not part of the update.
Cause (storage/innobase/row/row0upd.cc, same in 8.4.13 and trunk):
row_upd_store_row() -> row_upd_store_v_row(): for an indexed virtual column that is not in the update vector
and whose old value is NULL, it calls row_upd_dup_v_new_vals(update) before innobase_get_computed_value().
row_upd_dup_v_new_vals() copies every virtual new_val (dfield_dup / dfield_multi_value_dup) into update->heap.
On the SQL handler path update->heap is prebuilt->heap:
row_get_prebuilt_update_vector() -> row_create_update_node_for_mysql(table, prebuilt->heap) -> upd_create(n, heap).
prebuilt->heap is never emptied; it is freed only by row_prebuilt_free() from ha_innobase::close().
So every such UPDATE leaves its copies behind for as long as the handle stays open.
The same pattern was fixed in calc_row_difference() for Bug #105861 (8.0.30) by allocating from
update->per_stmt_heap, which row_prebuilt_t::end_stmt() empties. row_upd_dup_v_new_vals() was not changed.
Found and measured on Percona Server 8.4.5 and 8.4.11; the code involved is identical in MySQL 8.4.13 and trunk
How to repeat:
CREATE DATABASE mv_leak;
USE mv_leak;
CREATE TABLE t (
id INT PRIMARY KEY,
j JSON,
b VARCHAR(64) GENERATED ALWAYS AS (j->>'$.b') VIRTUAL,
KEY k_b (b),
KEY k_mv ((CAST(j->'$.a' AS CHAR(64) ARRAY)))
) ENGINE=InnoDB;
DELIMITER //
CREATE PROCEDURE load_rows(IN n INT, IN doc JSON)
BEGIN
DECLARE i INT DEFAULT 0;
TRUNCATE t;
WHILE i < n DO INSERT INTO t (id, j) VALUES (i, doc); SET i = i + 1; END WHILE;
END//
CREATE PROCEDURE update_rows(IN n INT, IN doc JSON)
BEGIN
DECLARE i INT DEFAULT 0;
WHILE i < n DO UPDATE t SET j = doc WHERE id = i; SET i = i + 1; END WHILE;
END//
DELIMITER ;
-- b is NULL, the update changes the multi-valued array
CALL load_rows(20000, '{"a": ["x"]}');
FLUSH TABLES t;
CALL update_rows(20000, '{"a": ["y", "z"]}');
SELECT current_number_of_bytes_used INTO @before FROM performance_schema.memory_summary_global_by_event_name
WHERE event_name = 'memory/innodb/memory';
FLUSH TABLES t;
SELECT (@before - current_number_of_bytes_used) DIV 1024 AS kb_held_by_handle
FROM performance_schema.memory_summary_global_by_event_name WHERE event_name = 'memory/innodb/memory';
Repeat with other inputs. Memory held by the handle until FLUSH:
b NULL, array changed, 20,000 rows 2,397 KB
b NULL, array changed, 40,000 rows 4,781 KB (linear, ~120 bytes per row)
b NOT NULL ('{"a":["x"],"b":"k"}' -> '{"a":["y","z"],"b":"k"}') 14 KB
b NULL, no indexed value changed ('{"a":["x"],"c":1}' -> '{"a":["x"],"c":2}') 14 KB
Suggested fix:
static void row_upd_dup_v_new_vals(upd_t *update) {
ut_ad(update != nullptr);
mem_heap_t *heap =
update->per_stmt_heap != nullptr ? update->per_stmt_heap : update->heap;
for (ulint j = 0; j < upd_get_n_fields(update); j++) {
upd_field_t *upd_field = upd_get_nth_field(update, j);
if (upd_field->new_val.type.is_virtual()) {
if (dfield_is_multi_value(&upd_field->new_val)) {
dfield_multi_value_dup(&upd_field->new_val, heap);
} else {
dfield_dup(&upd_field->new_val, heap);
}
}
}
}
The copies are only needed while the current row's indexes are updated. Paths without per_stmt_heap
(cascade update vectors) keep the current behaviour. With this change, 5,000 such updates leave less than
256 KB held by the handle (without it, the same test fails); the innodb and gcol virtual / multi_value tests pass.
Same change submitted to Percona Server: https://github.com/percona/percona-server/pull/6366 (PS-12266).
Workaround: periodic FLUSH TABLES <t>.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.041 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.