Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

AUTO_INCREMENT ids jump after INSERT ... ON DUPLICATE KEY UPDATE

Your error

Ids go 1, 2, 7, 8 although no rows were deleted2 more lines it can show

Fix it fast

Most common cause, 1 of 2

The upsert updated a row but still took an id

Treat ids as identifiers, not as a row count.

Fix
ALTER TABLE tags MODIFY id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;

-- update first, insert only when no row matched
UPDATE tags SET hits = hits + 1 WHERE name = 'red';
-- if the affected-row count was 0:
INSERT INTO tags (name, hits) VALUES ('red', 1);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

InnoDB counts INSERT ... ON DUPLICATE KEY UPDATE as a mixed-mode insert: it can allocate an AUTO_INCREMENT value before it finds the duplicate, and that value is never reused, even when the row is only updated or the transaction rolls back. The gaps are by design. Widen the id column, or UPDATE first and INSERT only new rows. MySQL 8.4.

  • Applies to MySQL 8.4 LTS, InnoDB, innodb_autoinc_lock_mode 2 (default)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Note the highest id, run the upsert for a key that already exists, then add one genuinely new row and compare its id with the highest id from before.

Run this first
SELECT MAX(id) FROM tags;
INSERT INTO tags (name, hits) VALUES ('red', 1)
  ON DUPLICATE KEY UPDATE hits = hits + 1;
INSERT INTO tags (name, hits) VALUES ('probe-new-row', 1);
SELECT LAST_INSERT_ID();

Then find the line you see in this list and open its fix:

  1. 1The upsert updated a row but still took an idThe client reports 2 rows affected for the upsert, and the new row's id is higher than MAX(id) + 1
  2. 2The upsert ran in a transaction that rolled back, or in a statement that failedGaps line up with batches that the application log shows as failed or rolled back, and the Com_rollback counter keeps growing

What does the error look like?

  • Ids go 1, 2, 7, 8 although no rows were deleted
  • Query OK, 2 rows affected
  • The AUTO_INCREMENT counter in SHOW CREATE TABLE climbs after upserts that only updated existing rows

Cause 1 of 2

The upsert updated a row but still took an id

You seeThe client reports 2 rows affected for the upsert, and the new row's id is higher than MAX(id) + 1

Check

The gaps come from how InnoDB handles this statement. The manual lists INSERT ... ON DUPLICATE KEY UPDATE as a mixed-mode insert that in the worst case acts as an INSERT followed by an UPDATE, where the allocated AUTO_INCREMENT value may or may not be used during the update. A value, once generated, is not handed out again, so each upsert that ends as an update can leave a hole. Interleaved lock mode 2 is the default, and the mode is configured at server startup.

Check
SELECT @@GLOBAL.innodb_autoinc_lock_mode;

Fix

Treat ids as identifiers, not as a row count. Give upsert-heavy tables a BIGINT UNSIGNED id so the extra consumption never reaches the type's maximum. When most calls hit existing rows, run the UPDATE first and insert only if it matched nothing: an UPDATE adds no row, so it needs no new AUTO_INCREMENT value. If two sessions race, the second INSERT fails with a duplicate-key error; catch it and repeat the UPDATE.

Fix
ALTER TABLE tags MODIFY id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;

-- update first, insert only when no row matched
UPDATE tags SET hits = hits + 1 WHERE name = 'red';
-- if the affected-row count was 0:
INSERT INTO tags (name, hits) VALUES ('red', 1);

Verify

A repeat of the first step shows the new row taking MAX(id) + 1 when the update-first path is used.

Verify
SELECT MAX(id) FROM tags;

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 2

The upsert ran in a transaction that rolled back, or in a statement that failed

You seeGaps line up with batches that the application log shows as failed or rolled back, and the Com_rollback counter keeps growing

Check

In all three lock modes, values generated by a statement are lost when its transaction rolls back, and they are lost whether or not the statement completed. A batch of upserts that fails halfway still uses up the ids it reserved.

Check
SHOW GLOBAL STATUS LIKE 'Com_rollback';

Fix

Nothing in MySQL needs repair. If a gap-free number matters to the business, such as an invoice number, keep it in its own column filled by the application, and leave the AUTO_INCREMENT id as an internal key.

Fix
ALTER TABLE invoices ADD COLUMN invoice_no INT UNSIGNED NULL,
  ADD UNIQUE KEY uq_invoice_no (invoice_no);

Verify

Reports and users see the business number, and gaps in the id column no longer matter to anyone.

Verify
SELECT id, invoice_no FROM invoices ORDER BY invoice_no DESC LIMIT 5;

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Pick BIGINT UNSIGNED for the id of any table written mostly by upserts.
  • Never show AUTO_INCREMENT ids to users as a count or a gap-free sequence.
  • Avoid ON DUPLICATE KEY UPDATE on tables with more than one unique index, as the manual advises.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.