Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl, meta: support altering auto_increment ID to a smaller value #25868

Merged
merged 10 commits into from
Jul 13, 2021

Conversation

tangenta
Copy link
Contributor

@tangenta tangenta commented Jul 1, 2021

What problem does this PR solve?

Issue Number: close #25400

Problem Summary:

For now, the auto ID in TiDB grows only and can never bet set 'back'.

If the auto ID is rebased to a very large value because of either mistake or bug, it is almost impossible to set it back, this can be a headache for the users.

For details, see #25400.

What is changed and how it works?

What's Changed:

This PR introduces a new syntax to force changing the next global auto ID:

ALTER TABLE `t` FORCE AUTO_INCREMENT = 3;
ALTER TABLE `t` /*T![force_auto_inc] FORCE */ AUTO_INCREMENT = 3;
drop table if exists t;
create table t (a bigint primary key auto_increment);
alter table t FORCE auto_increment = 18446744073709551615;
show table t next_row_id;
insert into t values ();
alter table t FORCE auto_increment = 10086;
show table t next_row_id;
insert into t values();
select a from t;
mysql> alter table t FORCE auto_increment = 18446744073709551615;
Query OK, 0 rows affected (0.01 sec)

mysql> show table t next_row_id;
+---------+------------+-------------+----------------------+----------------+
| DB_NAME | TABLE_NAME | COLUMN_NAME | NEXT_GLOBAL_ROW_ID   | ID_TYPE        |
+---------+------------+-------------+----------------------+----------------+
| test    | t          | a           | 18446744073709551615 | AUTO_INCREMENT |
+---------+------------+-------------+----------------------+----------------+
1 row in set (0.00 sec)

mysql> insert into t values ();
ERROR 1467 (HY000): Failed to read auto-increment value from storage engine

mysql> alter table t FORCE auto_increment = 10086;
Query OK, 0 rows affected (0.01 sec)

mysql> show table t next_row_id;
+---------+------------+-------------+--------------------+----------------+
| DB_NAME | TABLE_NAME | COLUMN_NAME | NEXT_GLOBAL_ROW_ID | ID_TYPE        |
+---------+------------+-------------+--------------------+----------------+
| test    | t          | a           |              10086 | AUTO_INCREMENT |
+---------+------------+-------------+--------------------+----------------+
1 row in set (0.00 sec)

mysql> insert into t values();
Query OK, 1 row affected (0.00 sec)

mysql> select a from t;
+-------+
| a     |
+-------+
| 10086 |
+-------+
1 row in set (0.00 sec)

Please attention: the FORCE keyword should be only used to recover metadata. All the write workload on the target table should be stopped and the ID should be the largest existing ID plus one(select max(id)+1 from t). Otherwise, it may lose row data.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch?

Check List

Tests

  • Unit test
  • Integration test

Side effects

NA

Release note

  • Supports forcing the next auto-increment ID to be altered.

@github-actions github-actions bot added the sig/sql-infra SIG: SQL Infra label Jul 1, 2021
@tangenta tangenta requested a review from bb7133 July 1, 2021 10:03
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 1, 2021
Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jul 12, 2021
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 12, 2021
@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • AilinKid
  • bb7133

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jul 13, 2021
@bb7133
Copy link
Member

bb7133 commented Jul 13, 2021

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 75a772f

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jul 13, 2021
@ti-chi-bot ti-chi-bot merged commit a3919e3 into pingcap:master Jul 13, 2021
@bb7133
Copy link
Member

bb7133 commented Jul 13, 2021

Good job! Please update the docs @tangenta

Comment on lines +2647 to +2649
tk.MustExec("insert into t values (3);")
tk.MustExec("insert into t values (3);")
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "3 2"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we better report duplicate errors instead of direct overwrite existing data?

for auto pk handle, it seem will report dup err:

mysql> create table t (a bigint primary key auto_increment);
Query OK, 0 rows affected (0.01 sec)

mysql> alter table t FORCE auto_increment = 20;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t values();
Query OK, 1 row affected (0.00 sec)

mysql> alter table t FORCE auto_increment = 20;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t values();
ERROR 1062 (23000): Duplicate entry '20' for key 'PRIMARY'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/sql-infra SIG: SQL Infra size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ddl: allow altering the auto_increment id 'back' to a small value
5 participants