3290
MariaDBERRORCommonCTEsHIGH confidence

Maximum recursion depth exceeded in recursive CTE

Production Risk

High — unbounded recursive CTEs can exhaust server resources.

How to reproduce
trigger — this will error
trigger — this will error
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte) SELECT * FROM cte LIMIT 2000;

expected output

ERROR 3290 (HY000): Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value.

Fix 1

Add termination condition

Add termination condition
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte WHERE n < 100) SELECT * FROM cte;

Why this works

A WHERE clause on the recursive member stops the recursion at the desired depth.

Fix 2

Increase recursion limit

Increase recursion limit
SET SESSION cte_max_recursion_depth = 5000;

Why this works

Raises the per-session recursion limit for intentionally deep recursions.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3290 ER_CTE_MAX_RECURSION_DEPTH

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors