[MDEV-14222] Unnecessary 'cascade' memory allocation for every updated row when there is no FOREIGN KEY Created: 2017-10-30  Updated: 2020-08-25  Resolved: 2018-02-07

Status: Closed
Project: MariaDB Server
Component/s: Storage Engine - InnoDB
Affects Version/s: 10.2.2, 10.3.0
Fix Version/s: 10.2.13, 10.3.5

Type: Bug Priority: Major
Reporter: Marko Mäkelä Assignee: Marko Mäkelä
Resolution: Fixed Votes: 0
Labels: memory, performance, upstream

Issue Links:
Blocks
is blocked by MDEV-15219 FOREIGN KEY CASCADE or SET NULL opera... Closed
Duplicate
is duplicated by MDEV-15199 Triangular FKs - Cascade delete cause... Closed
Relates
relates to MDEV-14958 Merge new release of InnoDB MySQL 5.7... Closed
relates to MDEV-15611 Due to the failure of foreign key det... Closed

 Description   

MySQL 5.7.2 introduced heap memory allocations for every updated row:

	if (node->cascade_heap) {
		mem_heap_empty(node->cascade_heap);
	} else {
		node->cascade_heap = mem_heap_create(128);
	}
 
	mem_heap_allocator<upd_node_t*> mem_heap_ator(node->cascade_heap);
 
	cascade_upd_nodes = new
		(mem_heap_ator.allocate(sizeof(upd_cascade_t)))
		upd_cascade_t(deque_mem_heap_t(mem_heap_ator));
 
	new_upd_nodes = new
		(mem_heap_ator.allocate(sizeof(upd_cascade_t)))
		upd_cascade_t(deque_mem_heap_t(mem_heap_ator));
 
	processed_cascades = new
		(mem_heap_ator.allocate(sizeof(upd_cascade_t)))
		upd_cascade_t(deque_mem_heap_t(mem_heap_ator));

If the mem_heap_create(128) was replaced with a larger parameter, then these allocations could reuse the same memory heap for every updated row. It would still be unnecessary to allocate the memory when there are no FOREIGN KEY ON (UPDATE|DELETE) (SET NULL|CASCADE) constraints, but we would avoid the multiple calls to mem_heap_create_block_func() on every updated row.



 Comments   
Comment by Marko Mäkelä [ 2018-01-24 ]

As noted in MDEV-14958, MySQL 5.7.21 removes this code and reverts back to call stack recursion. To compensate for the increased stack memory usage, it limits the maximum depth of FOREIGN KEY constraints from 255 to 15.
Bug #26191879 FOREIGN KEY CASCADES USE EXCESSIVE MEMORY

Comment by Marko Mäkelä [ 2018-02-06 ]

The MySQL 5.7.21 fix also appears to fix MDEV-15199. Thus, we do not seem to have any other choice than to merge the fix from MySQL.
But, we can try to reduce the stack memory usage for each iteration.

Generated at Thu Feb 08 08:11:53 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.