resetting AUTO_INCREMENT value in MySQL avoiding race condition

Udo G

I know that I can reset the AUTO_INCREMENT value in MySQL using

ALTER TABLE fooo AUTO_INCREMENT = 1;

However, can I avoid changing the value when the table is not empty? Alternatively, can I automatically and atomically set it to the current maximum value + 1?

Background

I have a queue table that is filled by various processes with simple data that is processed by another, single process. The latter deletes items it has processed. It is likely that the table becomes completely empty, although new data will come in quickly.

Currently the auto increment value is over 1,000,000,000 and sooner or later this will lead to nasty integer overflows in the processes. This is why I'm looking for a way to reset the autoinc value for a busy table (within the process that consumes/deletes the rows in the table).

NOTE: As this is a queue, I must absolutely avoid to set the autoinc value to a value lower than currently existing data.

Marcus Adams

With MyISAM, simply reset to 1 with ALTER, and it will automatically use the next highest.

For InnoDB, to reset and use the next highest value atomically:

LOCK TABLES table WRITE;
SELECT COALESCE(MAX(id), 1) FROM table;
// Use value from SELECT in the ALTER
ALTER TABLE AUTO_INCREMENT = 99;
UNLOCK TABLES;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Increment field value in different records avoiding race condition in Golang

Avoiding a race condition in erlang

Avoiding race condition in OpenMP?

Vue resetting variables race condition

Avoiding race condition on Nodejs Api

Avoiding race condition in postgres updates using slick

Avoiding race condition while using ThreadPoolExecutor

Avoiding React Race condition with AbortController not working

Reset Auto_Increment Value After Data Delete MySQL

Set AUTO_INCREMENT value through variable in MySql

how to reset the value of a AUTO_INCREMENT row in a mysql table

MYSQL - Use CONCAT to create a variable with contains an auto_increment value

MySQL IF Statement Race Condition

MySQL, AUTO_INCREMENT, and NOT NULL

Hibernate auto_increment MySQL

Avoiding race condition in enum-based Finite State Machine

concurrently, how to manage values/states and avoiding a race condition

Django Assigning ID from Count: Avoiding Race Condition

Mysql Irregular Auto_Increment increment

return id AUTO_INCREMENT value in mysql DB immediately after insert

Retrieve auto_increment value of multiple rows from MySQL in batch mode

How to set AUTO_INCREMENT default value and rate of increase in MySQL 5.6

Use auto_increment value in INSERT

initial AUTO_INCREMENT value for TypeORM PrimaryGeneratedColumn

Nagios: monitor mysql auto_increment overflow

How to reset AUTO_INCREMENT in MySQL?

MySQL ALTER TABLE AUTO_INCREMENT and ORDER BY

AUTO_INCREMENT in mysql can be signed by default?

How to find if a column is auto_increment in MySQL