How to Fix MySQL Error 3636 (ER_CTE_MAX_RECURSION_DEPTH)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mysql error 3636 when a recursive query runs too many times and stops. You can fix it by adding a stop condition or raising the recursion depth limit. MySQL uses the symbol ER_CTE_MAX_RECURSION_DEPTH and SQLSTATE HY000 for this failure. The database stops the query after a set number of iterations to save server memory. This safeguard keeps one bad query from crashing your whole database server. What causes mysql error 3636 in your database This error happens when a recursive common

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get mysql error 3636 when a recursive query runs too many times and stops. You can fix it by adding a stop condition or raising the recursion depth limit.

MySQL uses the symbol ER_CTE_MAX_RECURSION_DEPTH and SQLSTATE HY000 for this failure. The database stops the query after a set number of iterations to save server memory. This safeguard keeps one bad query from crashing your whole database server.

What causes mysql error 3636 in your database

This error happens when a recursive common table expression loops past the allowed limit. A recursive query has two main parts that work together. The first part is the seed query that gets the starting rows. The second part is the recursive member that builds on those rows.

Each pass through the recursive part counts as one iteration in the engine. When the count passes the limit in @@cte_max_recursion_depth, MySQL aborts the run. Two main issues cause a query to hit this wall in production.

First, the query logic might lack a proper exit condition in the filter clause. Without a clear stop rule, the query joins rows in an infinite loop. Second, your data might have circular links, like a manager who reports to their own report. Even with good query logic, dirty data will make the loop run forever.

Sometimes the query and data are fine, but the tree is just very deep. For example, generating a long calendar series can hit the cap fast. In that case, the safety limit is simply too low for your legitimate task.

How to diagnose and fix recursive query failures

Do not raise the global depth limit right away when you see this error. Raising the server limit blindly can let a runaway query eat all your CPU and RAM. Instead, test the query in a safe session with a small limit first.

MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Start by checking your data for loops or parent rows that point back to child rows. You can run a simple check on the base table to find self-referencing keys. If the data has loops, clean the bad rows or track visited nodes in your query.

Next, check the join and filter rules inside the recursive part of your query. Make sure each step moves toward a clear end point, such as a date limit. Verify that you use the right join keys between the anchor and recursive parts.

If the logic is sound and the data is clean, check how many rows you need. You can raise the @@cte_max_recursion_depth value for just your current session or query. Setting the limit at the session level keeps the rest of the database safe. Avoid granting broad admin rights to app users just to change system variables.

How to prevent runaway recursive queries in production

Teams often run ad-hoc queries to check deep trees like billing hierarchies or user referrals. A small typo in a join condition can trigger a heavy loop on production data. You should put guardrails in place before engineers run manual queries on live servers.

Set strict session timeouts so bad queries stop before they hurt other users. Review recursive queries in staging with production-sized data shapes before you ship them. Keep global recursion limits low, and only raise them inside specific, reviewed app jobs. Good query review habits stop infinite loops long before they wake up your on-call team.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.

Frequently asked questions

What does the ER_CTE_MAX_RECURSION_DEPTH symbol mean?

It is the official MySQL error symbol for error code 3636 under SQLSTATE HY000. It means a recursive common table expression passed the maximum allowed iteration count and stopped.

Should I set the recursion depth limit to zero or a huge number?

No, removing the cap lets broken queries run until the server runs out of memory. Only raise the limit for the specific session that needs to process a deep tree.

Why did my recursive query work in staging but fail in production?

Production tables usually have deeper trees or circular data links that do not exist in staging. Check your live data for self-referencing rows or trees that exceed the default iteration cap.

The query runs. The emails stay hidden.