diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index d49b228c46677..d8e96e056d206 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1807,6 +1807,11 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6 if err != nil { return errors.Trace(err) } + // If newBase < autoIncID, we need to do a rebase before returning. + // Assume there are 2 TiDB servers: TiDB-A with allocator range of 0 ~ 30000; TiDB-B with allocator range of 30001 ~ 60000. + // If the user sends SQL `alter table t1 auto_increment = 100` to TiDB-B, + // and TiDB-B finds 100 < 30001 but returns without any handling, + // then TiDB-A may still allocate 99 for auto_increment column. This doesn't make sense for the user. newBase = mathutil.MaxInt64(newBase, autoIncID) job := &model.Job{ SchemaID: schema.ID, diff --git a/ddl/table.go b/ddl/table.go index 225a11208a62b..cec8c7ca07a8e 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -535,6 +535,7 @@ func onRebaseAutoID(store kv.Storage, t *meta.Meta, job *model.Job) (ver int64, job.State = model.JobStateCancelled return ver, errors.Trace(err) } + // No need to check `newBase` again, because `RebaseAutoID` will do this check. tblInfo.AutoIncID = newBase tbl, err := getTable(store, schemaID, tblInfo) if err != nil {