Fixing MySQL ERROR 1436: Resolving Thread Stack Overrun

intermediateπŸ—„οΈ MySQL2026-07-11| MySQL 5.7, MySQL 8.0+, MariaDB, Ubuntu/Debian, CentOS, Windows, Docker Containers

Error Message

ERROR 1436 (HY000): Thread stack overrun: 246144 bytes used
#mysql#database#stored-procedure#troubleshooting

The Quick Fix

To clear ERROR 1436, you need to bump up the thread_stack size in your configuration. Most modern database workloads struggle with the old defaults of 192KB or 256KB. Increasing this to 512KB or 1MB usually stops the crashing immediately.

  • Open your configuration file (typically /etc/mysql/my.cnf or /etc/my.cnf).
  • Locate the [mysqld] block.
  • Add or edit this line: thread_stack = 512K
  • Restart the service: sudo systemctl restart mysql

Why This Happens

Think of the thread stack as the "workspace" MySQL gives to every individual connection. This memory handles local variables, function calls, and complex logic. If a query tries to cram more data into that workspace than it can hold, the engine throws a Thread stack overrun to prevent a full system crash.

The error 246144 bytes used is a huge hint. On a system with a 256KB limit (262,144 bytes), using 246KB leaves almost no room for the operating system's own overhead. You're essentially redlining your memory.

Common culprits include:

  • Deep Nesting: Using JSON functions like JSON_EXTRACT on massive, multi-layered documents.
  • Heavy Triggers: A single update that triggers a chain reaction across five other tables.
  • Recursion: Stored procedures that call themselves repeatedly without a clear exit.

Step-by-Step Resolution

1. Check Your Current Limit

Find out exactly what your server is working with before making changes. Run this command in your MySQL terminal:

SHOW VARIABLES LIKE 'thread_stack';

If the result is 262144, you are running on 256KB. For 64-bit systems, this is often too tight for complex queries.

2. Update the Config File

The thread_stack variable is static. You cannot change it while the server is running; a restart is mandatory.

Linux Users (Ubuntu/Debian/CentOS):

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# If that file doesn't exist, try:
sudo nano /etc/my.cnf

Windows Users:

Open my.ini in your installation folder. This is usually found at C:\ProgramData\MySQL\MySQL Server 8.0\my.ini.

Docker Users:

If you use docker-compose.yml, append the setting to your command line:

services:
  db:
    image: mysql:8.0
    command: --thread_stack=512K

3. Apply and Verify

Restart the MySQL daemon to load the new settings:

# Linux
sudo systemctl restart mysql

# Windows
# Restart via 'Services.msc' or the MySQL Notifier

After the restart, run the SHOW VARIABLES command again. You should see 524288 (for 512K) or 1048576 (for 1M).

Proactive Code Optimization

Increasing the stack size is a valid fix, but it can hide bad code. If you raise the stack to 2MB and still see the error, you likely have an infinite loop. This usually happens in stored procedures.

Check for "ping-pong" logic where Trigger A updates Table B, and Trigger B is set to update Table A. This creates a loop that consumes the stack in milliseconds. Also, verify that every recursive procedure has a solid IF statement to break the cycle.

Memory Math: A Warning

Every connection gets its own stack. If you set max_connections = 500 and thread_stack = 1M, MySQL will reserve 500MB of RAM specifically for these stacks. On a small 2GB VPS, this could lead to OOM (Out of Memory) errors. Only increase the stack as much as you actually needβ€”512KB is the "sweet spot" for most enterprise apps.

Further Reading

Related Error Notes