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

executor: lock duplicated keys on insert-ignore & replace-nothing #42210

Merged
merged 4 commits into from
Mar 15, 2023

Conversation

zyguan
Copy link
Contributor

@zyguan zyguan commented Mar 14, 2023

What problem does this PR solve?

Issue Number: close #42121

Problem Summary: When doing insert-ingore or replace without changes, tidb doesn't lock conflict keys properly.

What is changed and how it works?

Lock keys even when insert-ingnore meets duplicated rows or replace without changes.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

The change may introduce more lock records, which may further lead performance regression like #25659 .

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Mar 14, 2023

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • cfzjywxk
  • you06

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 do-not-merge/needs-triage-completed release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 14, 2023
@@ -1239,6 +1243,10 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
} else {
// If duplicate keys were found in BatchGet, mark row = nil.
e.ctx.GetSessionVars().StmtCtx.AppendWarning(uk.dupErr)
if txnCtx := e.ctx.GetSessionVars().TxnCtx; txnCtx.IsPessimistic {
// lock duplicated unique key on insert-ingore
txnCtx.AddUnchangedRowKey(uk.newKey)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems mysql only locks unique keys as well.

/* init */ select version();
-- init >> +-----------+
-- init    | version() |
-- init    +-----------+
-- init    | 8.0.28    |
-- init    +-----------+
/* init */ drop table if exists t;
-- init >> 0 rows affected
/* init */ create table t (a int primary key, b int unique key, c int);
-- init >> 0 rows affected
/* init */ insert into t values (1, 1, 1);
-- init >> 1 rows affected
/* t1 */ begin;
-- t1 >> 0 rows affected
/* t2 */ begin;
-- t2 >> 0 rows affected
/* t1 */ insert ignore into t values (2, 1, 1);
-- t1 >> 0 rows affected
/* t2 */ select b from t where a = 1 for update;
-- t2 >> +---+
-- t2    | b |
-- t2    +---+
-- t2    | 1 |
-- t2    +---+
/* t2 */ select a from t where b = 1 for update;
-- t2 >> blocked
/* t1 */ commit;
-- t1 >> 0 rows affected
-- t2 >> resumed
-- t2 >> +---+
-- t2    | a |
-- t2    +---+
-- t2    | 1 |
-- t2    +---+
/* t2 */ commit;
-- t2 >> 0 rows affected

Copy link
Contributor

Choose a reason for hiding this comment

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

The behaviors of this part are not described in the documents yet, we could add them there.

Signed-off-by: zyguan <[email protected]>
Copy link
Contributor

@cfzjywxk cfzjywxk left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -1239,6 +1243,10 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
} else {
// If duplicate keys were found in BatchGet, mark row = nil.
e.ctx.GetSessionVars().StmtCtx.AppendWarning(uk.dupErr)
if txnCtx := e.ctx.GetSessionVars().TxnCtx; txnCtx.IsPessimistic {
// lock duplicated unique key on insert-ingore
txnCtx.AddUnchangedRowKey(uk.newKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

The behaviors of this part are not described in the documents yet, we could add them there.

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 14, 2023
@cfzjywxk cfzjywxk added the type/bugfix This PR fixes a bug. label Mar 14, 2023
executor/insert_common.go Outdated Show resolved Hide resolved
executor/insert_common.go Outdated Show resolved Hide resolved
@@ -539,7 +539,7 @@ func TestOptimisticConflicts(t *testing.T) {
tk.MustExec("begin pessimistic")
// This SQL use BatchGet and cache data in the txn snapshot.
// It can be changed to other SQLs that use BatchGet.
tk.MustExec("insert ignore into conflict values (1, 2)")
tk.MustExec("select * from conflict where id in (1, 2, 3)")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this query can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

May I know why the query is unnecessary? IMO, it mean to test the snapshot cache. We cache (1, 2) here and don't want to see the following select-for-update (L547) returns the cached row. Although, it won't read from the cache even when we forget to invalidate the cache because it will read from lock cache firstly now (also, we won't reuse the snapshot with for-update-ts now). I think it's ok to keep the query since the test point still makes sense.

@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 Mar 14, 2023
@zyguan zyguan changed the title executor: lock duplicated keys on insert-ingore & replace-nothing executor: lock duplicated keys on insert-ignore & replace-nothing Mar 14, 2023
@zyguan
Copy link
Contributor Author

zyguan commented Mar 14, 2023

/retest

@zyguan
Copy link
Contributor Author

zyguan commented Mar 15, 2023

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: b4cda16

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 15, 2023
@zyguan
Copy link
Contributor Author

zyguan commented Mar 15, 2023

/retest

1 similar comment
@zyguan
Copy link
Contributor Author

zyguan commented Mar 15, 2023

/retest

@ti-chi-bot ti-chi-bot added needs-cherry-pick-release-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-release-6.0 and removed do-not-merge/needs-triage-completed labels Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.5: #42285.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.6: #42286.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-5.3: #42287.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-5.4: #42288.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.0: #42289.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.1: #42290.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.2: #42291.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.3: #42292.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 15, 2023
you06 added a commit to you06/tidb that referenced this pull request May 19, 2023
@you06 you06 mentioned this pull request May 19, 2023
12 tasks
you06 added a commit to you06/tidb that referenced this pull request May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-release-6.1 Should cherry pick this PR to release-6.1 branch. needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. release-note-none Denotes a PR that doesn't merit a release note. 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. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REPLACE statement has unexpected behavior in concurrent transactions
4 participants