-
Notifications
You must be signed in to change notification settings - Fork 692
docs: clarify AUTO_RANDOM_BASE usage and explicit insert behavior #20734
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
Open
alastori
wants to merge
6
commits into
pingcap:master
Choose a base branch
from
alastori:fix-auto-random-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+23
−14
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be6b29d
docs: clarify AUTO_RANDOM_BASE usage and explicit insert behavior (#2…
alastori 1c25097
Update auto-random.md
alastori cfb9d77
Update auto-random.md
alastori ca46a99
Update auto-random.md
alastori d8e7c59
refine wording
hfxsd 1f7443b
Update auto-random.md
hfxsd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -166,30 +166,39 @@ TiDB implicitly allocates values to `AUTO_RANDOM` columns similarly to `AUTO_INC | |||||
|
||||||
## Clear the auto-increment ID cache | ||||||
|
||||||
Explicitly inserting data into an `AUTO_RANDOM` column behaves the same as with an `AUTO_INCREMENT` column, so you also need to clear the auto-increment ID cache. For more details, see [Clear the auto-increment ID cache](/auto-increment.md#clear-the-auto-increment-id-cache). | ||||||
When you insert explicit values into an `AUTO_RANDOM` column in a multi-instance TiDB deployment, ID collisions might occur, similar to an `AUTO_INCREMENT` column. Errors occur when the explicitly inserted ID conflicts with the internal counter that TiDB uses for automatic ID generation. | ||||||
|
||||||
You can run the `ALTER TABLE` statement to set `AUTO_RANDOM_BASE=0` to clear the auto-increment ID cache on all TiDB nodes in the cluster. For example: | ||||||
The collision happens as follows: each `AUTO_RANDOM` ID contains an auto-incrementing part alongside random bits. TiDB uses an internal counter for this auto-incrementing part. If you explicitly insert an ID where the auto-incrementing part matches the counter's next value, a duplicate key error might occur when TiDB later attempts to generate the same ID automatically. For more details, see [AUTO_INCREMENT Uniqueness](/auto-increment.md#uniqueness). | ||||||
|
||||||
In a single TiDB instance, this issue does not occur because the node automatically adjusts its internal counter when processing explicit insertions, preventing any future collisions. However, in a multi-instance TiDB deployment, each node maintains its own cache of IDs, which needs to be cleared after explicit insertions to prevent collisions. You can clear the unallocated cached IDs and avoid collisions using one of the following options. | ||||||
|
||||||
### Option 1: Automatically rebase (Recommended) | ||||||
|
||||||
```sql | ||||||
ALTER TABLE t AUTO_RANDOM_BASE=0; | ||||||
``` | ||||||
|
||||||
``` | ||||||
Query OK, 0 rows affected, 1 warning (0.52 sec) | ||||||
``` | ||||||
This statement automatically determines an appropriate base value. Although it produces a warning message similar to `Can't reset AUTO_INCREMENT to 0 without FORCE option, using XXX instead`, the value **will** change and you can safely ignore this warning. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
> **Note:** | ||||||
> | ||||||
> You cannot set `AUTO_RANDOM_BASE` to `0` with the `FORCE` keyword. Attempting this results in an error. | ||||||
|
||||||
### Option 2: Manually set a specific base value | ||||||
|
||||||
If you need to set a specific base value (for example, `1000`), use the `FORCE` keyword: | ||||||
|
||||||
```sql | ||||||
SHOW WARNINGS; | ||||||
ALTER TABLE t FORCE AUTO_RANDOM_BASE = 1000; | ||||||
``` | ||||||
|
||||||
``` | ||||||
+---------+------+-------------------------------------------------------------------------+ | ||||||
| Level | Code | Message | | ||||||
+---------+------+-------------------------------------------------------------------------+ | ||||||
| Warning | 1105 | Can't reset AUTO_INCREMENT to 0 without FORCE option, using 101 instead | | ||||||
+---------+------+-------------------------------------------------------------------------+ | ||||||
1 row in set (0.00 sec) | ||||||
``` | ||||||
This approach is less convenient because it requires you to determine an appropriate value yourself. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
> **Note:** | ||||||
> | ||||||
> You must use a non-zero positive integer value when using `FORCE`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Both statements in these two options modify the starting point for the auto-increment bits used in subsequent `AUTO_RANDOM` value generations across all TiDB nodes. They do not affect already allocated IDs. | ||||||
|
||||||
## Restrictions | ||||||
|
||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.