diff --git a/br/pkg/backup/client.go b/br/pkg/backup/client.go index 7cd6f18dc2fa5..19accb9fe496c 100644 --- a/br/pkg/backup/client.go +++ b/br/pkg/backup/client.go @@ -633,6 +633,20 @@ func BuildBackupSchemas( default: if tableInfo.SepAutoInc() { globalAutoID, err = autoIDAccess.IncrementID(tableInfo.Version).Get() + // For a nonclustered table with auto_increment column, both auto_increment_id and _tidb_rowid are required. + // See also https://github.com/pingcap/tidb/issues/46093 + if rowID, err1 := autoIDAccess.RowID().Get(); err1 == nil { + tableInfo.AutoIncIDExtra = rowID + 1 + } else { + // It is possible that the rowid meta key does not exist (i.e. table have auto_increment_id but no _rowid), + // so err1 != nil might be expected. + if globalAutoID == 0 { + // When both auto_increment_id and _rowid are missing, it must be something wrong. + return errors.Trace(err1) + } + // Print a warning in other scenes, should it be a INFO log? + log.Warn("get rowid error", zap.Error(err1)) + } } else { globalAutoID, err = autoIDAccess.RowID().Get() } diff --git a/br/tests/br_autoid/run.sh b/br/tests/br_autoid/run.sh new file mode 100644 index 0000000000000..af0ee46c7f582 --- /dev/null +++ b/br/tests/br_autoid/run.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# +# Copyright 2023 PingCAP, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eu +DB="$TEST_NAME" + +run_sql "create database if not exists ${DB}" +run_sql "create table $DB.issue46093 (a int primary key nonclustered auto_increment, b int) auto_id_cache = 1;" +run_sql "insert into $DB.issue46093 (b) values (1), (2), (3);" +run_sql "show table $DB.issue46093 next_row_id;" +check_contains "NEXT_GLOBAL_ROW_ID: 30001" +check_contains "NEXT_GLOBAL_ROW_ID: 4" + +run_sql "backup table $DB.issue46093 to 'local://$TEST_DIR/$DB'"; +run_sql "drop table $DB.issue46093;" +run_sql "restore table $DB.issue46093 from 'local://$TEST_DIR/$DB';" + +run_sql "show table $DB.issue46093 next_row_id;" +check_contains "NEXT_GLOBAL_ROW_ID: 30001" +check_contains "NEXT_GLOBAL_ROW_ID: 4001" +run_sql "insert into $DB.issue46093 (b) values (4), (5), (6);" +run_sql "insert into $DB.issue46093 (b) values (7), (8), (9);" +run_sql "select * from $DB.issue46093;" +check_contains "a: 1" +check_contains "a: 2" +check_contains "a: 3" +check_contains "a: 4001" +check_contains "a: 4002" +check_contains "a: 4003" +check_contains "a: 4004" +check_contains "a: 4005" +check_contains "a: 4006" +check_contains "b: 4" +check_contains "b: 5" +check_contains "b: 6" +check_contains "b: 7" +check_contains "b: 8" +check_contains "b: 9" diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 4509aa0d954e9..869467926b1fc 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2514,6 +2514,12 @@ func (d *ddl) createTableWithInfoPost( return errors.Trace(err) } } + // For issue https://github.com/pingcap/tidb/issues/46093 + if tbInfo.AutoIncIDExtra != 0 { + if err = d.handleAutoIncID(tbInfo, schemaID, tbInfo.AutoIncIDExtra-1, autoid.RowIDAllocType); err != nil { + return errors.Trace(err) + } + } if tbInfo.AutoRandID > 1 { // Default tableAutoRandID base is 0. // If the first ID is expected to greater than 1, we need to do rebase. diff --git a/parser/model/model.go b/parser/model/model.go index aa48c817f5de1..2a12275cc0545 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -458,14 +458,29 @@ type TableInfo struct { // 1 for the clustered index created > 5.0.0 RC. CommonHandleVersion uint16 `json:"common_handle_version"` - Comment string `json:"comment"` - AutoIncID int64 `json:"auto_inc_id"` - AutoIdCache int64 `json:"auto_id_cache"` //nolint:revive - AutoRandID int64 `json:"auto_rand_id"` - MaxColumnID int64 `json:"max_col_id"` - MaxIndexID int64 `json:"max_idx_id"` - MaxForeignKeyID int64 `json:"max_fk_id"` - MaxConstraintID int64 `json:"max_cst_id"` + Comment string `json:"comment"` + AutoIncID int64 `json:"auto_inc_id"` + + // Only used by BR when: + // 1. SepAutoInc() is true + // 2. The table is nonclustered and has auto_increment column. + // In that case, both auto_increment_id and tidb_rowid need to be backup & recover. + // See also https://github.com/pingcap/tidb/issues/46093 + // + // It should have been named TiDBRowID, but for historial reasons, we do not use separate meta key for _tidb_rowid and auto_increment_id, + // and field `AutoIncID` is used to serve both _tidb_rowid and auto_increment_id. + // If we introduce a TiDBRowID here, it could make furthur misunderstanding: + // in most cases, AutoIncID is _tidb_rowid and TiDBRowID is null + // but in some cases, AutoIncID is auto_increment_id and TiDBRowID is _tidb_rowid + // So let's just use another name AutoIncIDExtra to avoid misconception. + AutoIncIDExtra int64 `json:"auto_inc_id_extra,omitempty"` + + AutoIdCache int64 `json:"auto_id_cache"` //nolint:revive + AutoRandID int64 `json:"auto_rand_id"` + MaxColumnID int64 `json:"max_col_id"` + MaxIndexID int64 `json:"max_idx_id"` + MaxForeignKeyID int64 `json:"max_fk_id"` + MaxConstraintID int64 `json:"max_cst_id"` // UpdateTS is used to record the timestamp of updating the table's schema information. // These changing schema operations don't include 'truncate table' and 'rename table'. UpdateTS uint64 `json:"update_timestamp"`