Incident Response

MySQL ERROR 1206: The total number of locks exceeds the lock table size

A single huge UPDATE, DELETE or INSERT ... SELECT ran out of room for its row locks inside the InnoDB buffer pool, and InnoDB rolled the whole transaction back. Here is what the limit really is, how to see it coming, and how to run the same change safely on a big table.

· 13 min read · AG Data Team

ERROR 1206 (HY000): The total number of locks exceeds the lock table size means one InnoDB transaction tried to lock so many rows that its lock structures no longer fit in the memory InnoDB allows for them. There is no separate "lock table size" setting to raise. The locks live in the buffer pool, and the real fix is almost always to make the transaction smaller.

mysql> DELETE FROM events WHERE created_at < '2025-01-01';
ERROR 1206 (HY000): The total number of locks exceeds the lock table size

The server error reference lists it as error 1206, symbol ER_LOCK_TABLE_FULL, SQLSTATE HY000. Two things make it painful. It usually arrives minutes or hours into a long statement, and the work done so far is undone, because InnoDB rolls back the whole transaction, not just the last statement (you can see this in row0mysql.cc, where DB_LOCK_TABLE_FULL is handled the same way as a deadlock).

Where the "lock table" actually lives

InnoDB stores row locks compactly: a record lock structure carries a bitmap for one index page, so a single structure can cover many rows on that page. Those structures are allocated in a per-transaction memory heap, and when the heap grows, its blocks are taken from the buffer pool.

The cut-off is a heuristic in buf0lru.cc, the function buf_LRU_buf_pool_running_out(). It returns true when the pages on the free list plus the LRU list fall below a quarter of a buffer pool instance. In plain words: once lock memory and other internal users of the pool (the adaptive hash index, for example) hold about three quarters of an instance, InnoDB stops granting new row locks to big writers and returns 1206. The source comment states the purpose directly: to prevent huge transactions from eating up the whole buffer pool for their locks.

The check runs when a transaction that already holds more than 10,000 lock structures sets another record lock while reading rows, which is how UPDATE, DELETE and locking reads (SELECT ... FOR UPDATE / FOR SHARE) find their rows. It also runs before inserts and updates that have to reorganise the index tree. That is why the error hits bulk statements and almost never shows up in normal OLTP traffic.

What usually triggers it

How to see it coming

From a second session, while the statement runs, look at information_schema.INNODB_TRX. The documentation describes trx_lock_structs as the number of locks reserved, trx_lock_memory_bytes as the memory taken by the transaction's lock structures, and trx_rows_locked as an approximate count of locked rows.

-- run from a second session while the big statement is still going
SELECT trx_mysql_thread_id,
       trx_started,
       trx_lock_structs,
       ROUND(trx_lock_memory_bytes/1024/1024) AS lock_mb,
       trx_rows_locked,
       trx_rows_modified
  FROM information_schema.INNODB_TRX
 ORDER BY trx_lock_memory_bytes DESC
 LIMIT 5;

-- and compare with the buffer pool size
SELECT @@innodb_buffer_pool_size/1024/1024 AS buffer_pool_mb;

SHOW ENGINE INNODB STATUS shows the same thing in the TRANSACTIONS section, one line per transaction:

-- example output
---TRANSACTION 4211897, ACTIVE 312 sec fetching rows
mysql tables in use 1, locked 1
48211 lock struct(s), heap size 5537912, 9874310 row lock(s)

If the heap size keeps growing and is getting close to three quarters of a buffer pool instance, the statement will fail. Stopping it early saves you the rollback of the part that has not been done yet.

How to fix it

  1. Split the change into batches. This is the fix that works on every server. Each batch is its own short transaction, so its locks are freed at commit. Walk the primary key, keep batches in the low thousands of rows and stop when a batch affects zero rows.
    -- delete by primary key range, one short transaction per batch
    -- (assumes an index on created_at and an AUTO_INCREMENT id)
    SET @max_id := (SELECT MAX(id) FROM events WHERE created_at < '2025-01-01');
    
    DELETE FROM events
     WHERE id <= @max_id
       AND created_at < '2025-01-01'
     ORDER BY id
     LIMIT 5000;
    -- repeat until ROW_COUNT() = 0, with a short pause between batches
  2. Give the statement an index. If the WHERE column is not indexed, InnoDB locks every row it scans. With an index on created_at it only locks the range it reads.
  3. Use READ COMMITTED for the job's session. The isolation level documentation says that under READ COMMITTED, record locks for non-matching rows are released after the WHERE condition is evaluated, and INSERT ... SELECT reads the source table as a consistent read without locks. The catch: only row-based binary logging is supported with READ COMMITTED, so check binlog_format first.
    -- only for this session; requires row-based binary logging
    SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
    INSERT INTO events_archive
    SELECT * FROM events WHERE created_at < '2025-01-01' AND id BETWEEN 1 AND 500000;
  4. Increase the buffer pool, if it is undersized anyway. innodb_buffer_pool_size can be changed at runtime. Per the resizing documentation, the resize waits for active transactions to finish and blocks other threads during some steps, so do it in a quiet period. A bigger pool only moves the limit, so batch the job as well.
    -- dynamic; the value is rounded to a multiple of
    -- innodb_buffer_pool_chunk_size * innodb_buffer_pool_instances
    SET GLOBAL innodb_buffer_pool_size = 8 * 1024 * 1024 * 1024;
    SHOW STATUS LIKE 'Innodb_buffer_pool_resize_status%';

What does not help: searching for a lock table size setting. There is no separate parameter for it. Raising innodb_lock_wait_timeout does nothing either, because 1206 is about memory, not about waiting.

On large production databases

On a small test copy the same statement finishes in seconds and never comes close to the limit, so 1206 tends to appear for the first time in production. A few things matter at that scale:

Key takeaways

MySQL InnoDB Errors Locks
Work with us

A big data change keeps failing with 1206?

We plan and run large MySQL deletes, archives and backfills in safe batches, with locks, undo and replication lag under control.

Get in touch More from the blog