Issue of Auto-Increment Value Being Less Than the Maximum Value in the Table

This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 出现自增值小于表中数据最大值问题

| username: zhanggame1

[TiDB Usage Environment] Production Environment / Testing / PoC
[TiDB Version] 7.5
[Reproduction Path] What operations were performed when the issue occurred
[Encountered Issue: Issue Phenomenon and Impact]
A table test reported that data could not be inserted, citing a primary key conflict. The primary key used the default auto-increment value. Upon inspection, the maximum id value in the table was 122131, while the auto-increment value was 83853, resulting in a primary key conflict.
Under what circumstances would this situation occur? Normally, the auto-increment value should be greater than the maximum id value in the table.


| username: dba远航 | Original post link

It’s also normal to be smaller, possibly because a specific large value was specified during insertion. Additionally, the auto-increment value can also be modified.

| username: zhanggame1 | Original post link

When a specific large value is specified during insertion, I tested it and found no impact. For example, if the auto-increment value was originally 100 and you insert a specific value of 1000, the next auto-increment value will be 1001.

| username: 小龙虾爱大龙虾 | Original post link

Did you perform any operations on the table, like recovery or something?

| username: TiDB_C罗 | Original post link

How about explicitly inserting a primary key?

| username: TIDB-Learner | Original post link

It is estimated that some operations, such as bulk inserts followed by database table restoration, caused some bug.

| username: tidb菜鸟一只 | Original post link

It’s not normal. When you specify a large value during insertion, the table’s auto_increment value will default to this large value +1. Additionally, the auto_increment value can indeed be changed, but it can only be increased, not decreased, to prevent this issue.

| username: Jayjlchen | Original post link

It should be the scenario of explicitly and implicitly cross-inserting auto-increment column values, right? Executing an alter table xx auto_increment = 0 can trigger the tidbserver to reload the ID.

| username: dba远航 | Original post link

Also, check the PD, as it maintains the global auto-increment.

| username: zhanggame1 | Original post link

Auto-increment is maintained by the TiDB server itself, right? Here, I am using auto_id_cache=1.

| username: zhanggame1 | Original post link

The command “ALTER TABLE xx AUTO_INCREMENT = 0” in TiDB is useless.

| username: TiDBer_小阿飞 | Original post link

Have you tried restarting? I remember that after restarting MySQL, it has an impact and won’t use the previous auto-increment ID value but instead uses max ID + 1 because the auto-increment ID value is stored in memory and needs to be recalculated after a restart! I’m not sure if TiDB has the same issue.

| username: zhanggame1 | Original post link

I haven’t restarted it. Theoretically, it should be normal after a restart.

| username: gcworkerishungry | Original post link

Check the source of the maximum value.

| username: zhanggame1 | Original post link

The auto-increment value is 83854, and the current maximum value in the table is 122131. Then, I manually inserted 122132 successfully. After that, I tested inserting without specifying the primary key, and this time the insertion was successful. The auto-increment value was correctly set to the maximum value +1.

| username: TiDBer_jYQINSnf | Original post link

Still having the same issue after further operations? Following…

| username: WinterLiu | Original post link

Although I don’t know why this problem occurs, I know how to solve it, which is not bad either :grin:

| username: zhanggame1 | Original post link

I don’t know how this bug came about. If it happens in production, it will be a big headache.

| username: forever | Original post link

Have you tried creating a table for analysis?

| username: zhanggame1 | Original post link

No, theoretically it shouldn’t have any impact.