MySQL throws ER_STACK_OVERRUN (error 1119) when a thread exhausts its stack allocation, usually due to deep recursion, large local variables, or a low thread_stack setting.
MySQL Error 1119 ER_STACK_OVERRUN appears when a query or trigger overuses the per-thread stack. Increase thread_stack in my.cnf or refactor recursion to resolve the problem.
Thread stack overrun: Used: %ld of a %ld stack. Use
MySQL raises ER_STACK_OVERRUN when the server detects that the current thread consumed more stack memory than allowed. The error aborts the running statement and protects the server from a crash.
The exception includes the bytes used and the configured maximum, guiding administrators to enlarge the stack or simplify the statement.
Fixing it is critical because repeated stack overruns can destabilize production systems and cause service outages.
The most common trigger is a low thread_stack value in my.cnf.
When the stack allocation is too small, normal stored procedure or trigger execution can exceed the limit.
Deep recursion in stored procedures, functions, or triggers quickly multiplies stack frames and pushes usage beyond the configured threshold.
Large local variables, especially big VARCHAR or BLOB declarations inside routines, occupy significant stack space and lead to overruns during heavy workloads.
Increase thread_stack to at least 512K by editing my.cnf, then restart MySQL to apply the larger allocation.
If recursion is unavoidable, rewrite the algorithm to iterative logic or use temporary tables so that each call consumes minimal stack.
Relocate bulky local variables to heap allocated variables or store them in temporary tables to shrink the per-frame footprint.
A nightly ETL job with nested triggers fails - set thread_stack to 1M and refactor triggers to reduce depth.
An application recursively walks a hierarchy table - convert recursion to a single SQL query using common table expressions or iterative loops.
Legacy stored procedures declare large arrays - move arrays to permanent tables and select as needed.
Always set thread_stack to 256K or higher on production servers running complex routines.
Use LIMIT clauses and iterative loops instead of deep recursive procedures.
Monitor the error log for ER_STACK_OVERRUN entries and alert the team when they occur.
MySQL Error 1436: ER_STACKED_IMAGE_TOO_BIG occurs when RETURNED SQLSTATE stack grows large during nested handlers - increase max_sp_recursion_depth.
MySQL Error 1415: ER_SP_NO_RECURSION prevents self recursion in stored procedures - disable recursion or redesign the routine.
.
512K works for most workloads. For heavy recursion or large routines, 1M provides additional headroom without excessive memory use.
Yes for my.cnf changes. A temporary SET GLOBAL change applies immediately but resets on restart.
The setting allocates memory per connection. Raising from 192K to 512K slightly increases RAM use but has negligible CPU impact.
Galaxy's AI copilot flags deep recursion patterns and large local variables during code review, letting you adjust thread_stack before deploying.