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

Loosen pessimistic transactions: skip acquiring locks for some statements #36579

Closed
ekexium opened this issue Jul 26, 2022 · 3 comments · Fixed by #36473
Closed

Loosen pessimistic transactions: skip acquiring locks for some statements #36579

ekexium opened this issue Jul 26, 2022 · 3 comments · Fixed by #36473
Labels
type/enhancement The issue or PR belongs to an enhancement.

Comments

@ekexium
Copy link
Contributor

ekexium commented Jul 26, 2022

Enhancement

Pessimistic transactions acquire locks for all DMLs. We can selectively skip some locking for better performance, at the cost of a probably lower success rate. Namely support a transaction behavior somewhere between current optimistic and pessimistic transactions.

Example

Consider the workload: there is a table t on which you only do simple INSERT and SELECT. The INSERTs and SELECTs are performed in a transaction that will modify other tables where a pessimistic transaction is preferred. There may be multiple threads running the same workload.
You know you don't have to lock when inserting into t because if a conflicting concurrent insert happens, at most 1 transaction will succeed, while others will fail because of "duplicate key" errors. Skipping the lock of INSERT here and postponing the constraint check (namely the existence check) and conflict check to the prewrite phase can save you some requests for locking. The only downside is that a transaction won't know its INSERT failed until commit (previously it knows at the execution of the INSERT statement).

How

Pessimistic locks basically serve 2 purposes:

  • Constraint check (existence check)
  • Avoiding write conflict

Constraint check can be performed in prewrite as well. And we can perform a write conflict check just like in optimistic transactions for those keys that skipped acquiring pessimistic locks.

@tiancaiamao
Copy link
Contributor

You mean skip the pessimistic lock for insert statement?
It doesn't affect the final correctness, but may break the "pessimistic" semantic.

For insert, we can try something like: optimistic first, and turn to pessimistic on conflict.
Oh, gosh... seems retry is removed

@ekexium
Copy link
Contributor Author

ekexium commented Jul 28, 2022

Yes, it changes the semantics, so we leave the choice to users and do not change the default behavior.

@tiancaiamao
Copy link
Contributor

Get it!

ti-chi-bot added a commit to tikv/tikv that referenced this issue Aug 25, 2022
close #13128, ref pingcap/tidb#36579

Signed-off-by: ekexium <eke@fastmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
TonsnakeLin pushed a commit to TonsnakeLin/tikv that referenced this issue Aug 26, 2022
close tikv#13128, ref pingcap/tidb#36579

Signed-off-by: ekexium <eke@fastmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
TonsnakeLin pushed a commit to TonsnakeLin/tikv that referenced this issue Aug 29, 2022
close tikv#13128, ref pingcap/tidb#36579

Signed-off-by: ekexium <eke@fastmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
Signed-off-by: TonsnakeLin <lpbgytong@163.com>
fengou1 pushed a commit to fengou1/tikv that referenced this issue Aug 30, 2022
close tikv#13128, ref pingcap/tidb#36579

Signed-off-by: ekexium <eke@fastmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
Signed-off-by: fengou1 <feng.ou@pingcap.com>
3pointer pushed a commit to 3pointer/tikv that referenced this issue Sep 6, 2022
close tikv#13128, ref pingcap/tidb#36579

Signed-off-by: ekexium <eke@fastmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
Signed-off-by: 3pointer <luancheng@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants