-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
*: Support LOCK/UNLOCK TABLES feature (#10343) #16772
Conversation
/run-all-tests |
/rebuild |
/build |
Ci will happy after the parser PR merged, pingcap/parser#827 @bb7133 PTAL |
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.
LGTM
Signed-off-by: crazycs <crazycs520@gmail.com>
/run-all-tests |
/rebuild |
1 similar comment
/rebuild |
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.
LGTM
/merge |
/run-all-tests |
What problem does this PR solve?
cherry-pick #10343
conflict file
What problem does this PR solve?
Background
Currently, we only support LOCK TABLES/UNLOCK TABLES syntax, but actually do nothing.
TiDB only supports Optimistic Lock currently. A large write transaction maybe failed caused by other transaction has 1 insert. Large transaction retries are expensive, so we want to use LOCK TABLES to avoid this expensive retry.
Proposal
LOCK TABLES
explicitly acquires table locks for the current client session. Table locks can be only acquired for base tables, not support for views. You must have the LOCK TABLES privilege, and the SELECT privilege for each table to be locked.UNLOCK TABLES
explicitly releases any table locks held by the current session.LOCK TABLES
implicitly releases any table locks held by the current session before acquiring new locks.While the locks thus obtained are held, the session can access only the locked tables. Tables in the
INFORMATION_SCHEMA
database are an exception. They can be accessed without being locked explicitly.LOCK TABLES or UNLOCK TABLES, when applied to a partitioned table, always locks or unlocks the entire table; these statements do not support partition lock pruning.
Principles
The Difference with Mysql
LOCK TABLE
You can't implement transaction with
LOCK TABLES
andUNLOCK TABLES
in TiDB. If you need transaction, you should also executebegin
afterLOCK TABLES
.Table locks of mysql is metadata locks, and user can use table locks to implement transaction when the mysql storage engine is nontransactional.
Because MySQL execute
select/insert/update
will automatically to acquire the related table locks first. If session 1 is executing aselect * from t1
, and session 2 executelock tables t1 write
, then session 2 won't aquire the t1 table lock untill the session 1 execute finish. Details as follows chart:Select * from t1
t1
table lock.Lock tables t1 write
Select * from t1
) FinishLock tables t1 write
) sucess.But in TiDB, session2 will aquire the t1 table lock soon. Because session 1 won't acquire the
t1
table lock. In TiDB, It is very expensive to acquire the related table locks when execute DML likeselect/insert
. Details in TiDB as follows chart:Select * from t1
Lock tables t1 write
Lock tables t1 write
) sucess.Select * from t1
) FinishMySQL session1 execute
LOCK TABLES t1 write
, if other session2 obtains the lock oft1
, session 1 will wait until the session2 releaset1
lock. In TiDB, session 1 will return error like:Table 't1' was locked by server xx session xx.
Lock tables t1 read
Lock tables t1 write
Lock tables t1 write
Error: Table 't1' was locked by server xx session 1
Lock tables t1 write
sucess.TiDB table lock not support
alias
name currently. TiDB will lock thetable.ID
instead oftable.Name
What is changed and how it works?
TableLockInfo
inTableInfo
.LOCK TABLES
andUNLOCK TABLES
. So add 2 DDL job type:ActionLockTable
,ActionUnlockTable
.lockedTables
map insession
.lockedTables
use to record the table locks hold by the session.CheckTableLock
will use thevisitInfo
to check table locked. This check was add after theCheckPrivilege
.Related parser PR: pingcap/parser#305
Check List
Tests
Code changes
Side effects
Release note