[MDEV-31214] Recursive CTE execution is interrupted without errors or warnings Created: 2023-05-08  Updated: 2023-07-03  Resolved: 2023-07-03

Status: Closed
Project: MariaDB Server
Component/s: Optimizer - CTE
Affects Version/s: 10.6, 10.7, 10.8, 10.9, 10.10, 10.11, 11.0
Fix Version/s: 10.4.31, 10.5.22, 10.6.15, 10.9.8, 10.10.6, 10.11.5, 11.0.3

Type: Bug Priority: Critical
Reporter: Elena Stepanova Assignee: Sergei Golubchik
Resolution: Fixed Votes: 0
Labels: None


 Description   

In the current implementation, when a recursive CTE reaches max_recursive_iterations, it just stops execution and returns a result set without an error or a warning. It wasn't a very big problem initially, as the default for max_recursive_iterations was 18446744073709551615, so it wouldn't be reachable anyway, and those who changed it presumingly would know what they are doing.

But then there was a patch in 10.6.0 which changed the default of max_recursive_iterations from the old 18446744073709551615 to 1000.

commit 831adb1e5cf5d283a5a727b396ecf75a5b207699
Author: Daniel Black <daniel@mariadb.org>
Date:   Tue Mar 30 09:38:34 2021 +1100
 
    MDEV-17239 default max_recursive_iterations 4G -> 1000

1000 is a very low value, it can be reached easily in many practical use cases. For example, this simple CTE

create table t as with recursive cte as (select 1 as a union select a + 1 as a from cte where a < 10000 ) select * from cte;

is obviously meant to create a table with 10000 values, but instead it now silently creates 1001 and succeeds afterwards.

This change could have caused any number of hidden regressions in user applications, as it's very difficult to notice.

I'm not sure what is the best way to fix it, given that it has already been in GA for a long time. Throwing an error upon reaching max_recursive_iterations is an obvious option, consistent with usual server logic, but it's a fairly big change in behavior, I don't know if it's allowed for post-GA releases.
Another option is increasing the default to a really big value.



 Comments   
Comment by Sergei Golubchik [ 2023-06-30 ]

A warning would be a reasonable behavior, we already do it in similar cases:

Warnings:
Warning	1931	Query execution was interrupted. The query examined at least ### rows, which exceeds LIMIT ROWS EXAMINED (1000). The query result may be incomplete

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