Table locking is the most basic locking strategy. OceanBase Database allows you to lock a single table, multiple tables, or multiple partitions and subpartitions of a table at a time.
A database is a shared resource that can be simultaneously accessed by multiple users. When multiple users concurrently access the database, it is likely that the same data is operated by multiple transactions at the same time. Uncontrolled concurrent operations on the database may result in data inconsistency. Locking is key to the control of database concurrency. The locking mechanism is introduced to ensure resource security (or data integrity and consistency) by controlling concurrent data operations of multiple transactions.
After a table is locked, it remains in the locked state until you commit a transaction or roll back a transaction to a savepoint before the table is locked. In other words, the table lock is released after you commit or roll back a transaction. For more information about transaction control statements, see Transaction control statements.
Table locking modes
OceanBase Database supports the following locking modes:
- ROW SHARE: allows concurrent access to the locked table but prohibits other users from locking the table for exclusive access. This means that other users are not allowed to lock the table in
EXCLUSIVEmode. - ROW EXCLUSIVE: prohibits users from locking the table in
SHAREor a higher mode (SHARE,ROW SHARE EXCLUSIVE, orEXCLUSIVE). WhenUPDATE,INSERT,DELETE, orSELECT FOR UPDATEoperations are performed on a table, the table is automatically locked inROW EXCLUSIVEmode. - SHARE: allows concurrent queries but prohibits updates on the locked table.
UPDATE,DELETE,INSERT, orSELECT FOR UPDATEoperations on table rows are not allowed, and other users cannot lock the table inSHARE ROW EXCLUSIVEorEXCLUSIVEmode. - SHARE ROW EXCLUSIVE: allows other users to view rows of the locked table but prohibits the users from performing the
UPDATE,DELETE, orINSERToperation on table rows, usingSELECT FOR UPDATEto query table rows, or locking the table in any mode other thanROW SHARE. - EXCLUSIVE: allows other users to query the locked table but prohibits the users from performing any Data Manipulation Language (DML) operations on the table or locking the table in any mode.
The following table describes the conflicts between different locking modes.
| Requested locking mode | Current locking mode | |||||
|---|---|---|---|---|---|---|
| ROW SHARE | ROW EXCLUSIVE | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ||
| ROW SHARE | Not conflicting | Not conflicting | Not conflicting | Not conflicting | Conflicting | |
| ROW EXCLUSIVE | Not conflicting | Not conflicting | Conflicting | Conflicting | Conflicting | |
| SHARE | Not conflicting | Conflicting | Not conflicting | Conflicting | Conflicting | |
| SHARE ROW EXCLUSIVE | Not conflicting | Conflicting | Conflicting | Conflicting | Conflicting | |
| EXCLUSIVE | Conflicting | Conflicting | Conflicting | Conflicting | Conflicting | |
Required privileges
You can lock only tables in your own schema, or you must have the LOCK ANY TABLE privilege.
Lock an entire table
The SQL syntax for locking an entire table is as follows:
LOCK TABLE [schema.]table_name[,[schema.]table_name ...] IN lockmode MODE [NOWAIT | WAIT integer];
where:
table_namespecifies the name of the table to be locked. Separate multiple tables with commas (,).lockmodespecifies the table locking mode.NOWAIT | WAIT integerspecifies the action to take if a lock conflict occurs.If
NOWAITis specified and a lock conflict occurs when you lock the target table, the system immediately returns the right of control to you and returns an error message.If
WAIT integeris specified and a lock conflict occurs when you lock the target table, the system waits for the conflicting table lock to be released until the specified timeout period for statement execution expires. If the conflicting table lock is not released within the timeout period, the system returns an error message. The unit of the value ofintegeris seconds, and the value is not limited.Notice
If
WAIT integeris specified, the timeout period for statement execution is subject to the values ofinteger, ob_query_timeout, and ob_trx_timeout. The smallest value among the three is used as the actual timeout period for statement execution. For example, ifWAIT 10is specified and the default values are retained forob_query_timeoutandob_trx_timeout, the timeout period for waiting for the release of a conflicting table lock is 1,000,000 us (1s).If neither
NOWAITnorWAIT integeris specified, the smaller value between ob_query_timeout and ob_trx_timeout is used as the timeout period for statement execution.
Example: Lock the tbl1 table and set the locking mode to EXCLUSIVE.
LOCK TABLE tbl1 IN EXCLUSIVE MODE NOWAIT;
In this example, after an EXCLUSIVE lock is set for the tbl1 table, other users can only query this table and cannot perform any DML operations or set other types of locks on this table.
Lock partitions of a table
The SQL syntax for locking partitions of a table is as follows:
LOCK TABLE
{
[ schema.]table_name
[ PARTITION '('partition_name ...')' | SUBPARTITION '(' subpartition_name ...')' ] ...
}
IN lockmode MODE
[ NOWAIT | WAIT integer] ;
where:
table_namespecifies the name of the partitioned table whose partitions are to be locked.partition_namespecifies the name of the partition to be locked. You can lock multiple partitions at a time. To do so, separate the partition names with commas (,).subpartition_namespecifies the name of the subpartition to be locked. You can lock multiple subpartitions at a time. To do so, separate the subpartition names with commas (,).lockmodespecifies the table locking mode.NOWAIT | WAIT integerspecifies the action to take if a lock conflict occurs.If
NOWAITis specified and a lock conflict occurs when you lock the target table, the system immediately returns the right of control to you and returns an error message.If
WAIT integeris specified and a lock conflict occurs when you lock the target table, the system waits for the conflicting table lock to be released until the specified timeout period for statement execution expires. If the conflicting table lock is not released within the timeout period, the system returns an error message. The unit of the value ofintegeris seconds, and the value is not limited.Notice
If
WAIT integeris specified, the timeout period for statement execution is subject to the values ofinteger, ob_query_timeout, and ob_trx_timeout. The smallest value among the three is used as the actual timeout period for statement execution. For example, ifWAIT 10is specified and the default values are retained forob_query_timeoutandob_trx_timeout, the timeout period for waiting for the release of a conflicting table lock is 1,000,000 us (1s).If neither
NOWAITnorWAIT integeris specified, the timeout period for statement execution is determined by ob_query_timeout or ob_trx_timeout, whichever is smaller.
Here provides an example. Assume that a template-based subpartitioned table named tbl2 exists in the current database. The statement for creating this table is as follows:
CREATE TABLE tbl2(col1 NUMBER, col2 NUMBER)
PARTITION BY RANGE (col1)
SUBPARTITION BY RANGE (col2)
SUBPARTITION TEMPLATE
(
SUBPARTITION sp0 VALUES LESS THAN (3),
SUBPARTITION sp1 VALUES LESS THAN (6),
SUBPARTITION sp2 VALUES LESS THAN (9)
)
(
PARTITION p0 VALUES LESS THAN (100),
PARTITION p1 VALUES LESS THAN (200),
PARTITION p2 VALUES LESS THAN (300)
);
According to the partition naming rules of a template-based subpartitioned table, the subpartitions are named p0ssp0, p0ssp1, and p0ssp2. For more information about partition naming rules, see Overview.
The following examples show how to perform specific locking operations on the table:
Set an EXCLUSIVE lock for the
p1partition of thetbl2table.LOCK TABLE tbl2 PARTITION (p1) IN EXCLUSIVE MODE NOWAIT;Set an EXCLUSIVE lock for the
p1ssp1subpartition of thetbl2table.LOCK TABLE tbl2 SUBPARTITION (p1ssp1) IN EXCLUSIVE MODE WAIT 60;Set a SHARE lock for the
p1andp2partitions and thep2ssp0andp2ssp1subpartitions of thetbl2table.LOCK TABLE tbl2 PARTITION (p1, p2), tbl2 SUBPARTITION (p2ssp0, p2ssp1) IN SHARE MODE;
View table locks
You can query the GV$OB_LOCKS and V$OB_LOCKS views for the locks held and requested for each table of the current user.
SELECT * FROM GV$OB_LOCKS;
Sample query results are as follows:
+----------------+----------+-----------+----------+------+--------+------+-------+---------+-----------+-------+
| SVR_IP | SVR_PORT | TENANT_ID | TRANS_ID | TYPE | ID1 | ID2 | LMODE | REQUEST | CTIME | BLOCK |
+----------------+----------+-----------+----------+------+--------+------+-------+---------+-----------+-------+
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 500003 | NULL | RX | NONE | 462723786 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 500003 | NULL | RS | NONE | 6690958 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200005 | NULL | X | NONE | 462718854 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200006 | NULL | X | NONE | 462710583 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200007 | NULL | X | NONE | 462701111 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200005 | NULL | S | NONE | 6687863 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200006 | NULL | S | NONE | 6679696 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200007 | NULL | S | NONE | 6671395 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200008 | NULL | S | NONE | 6661725 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200009 | NULL | S | NONE | 6653562 | 0 |
| xx.xx.xx.xx | 2882 | 1004 | 1344711 | TM | 200010 | NULL | S | NONE | 6645387 | 0 |
+----------------+----------+-----------+----------+------+--------+------+-------+---------+-----------+-------+
11 rows in set
The following table describes the fields in the query results.
| Field | Description |
|---|---|
| SRV_IP | The IP address of the OBServer node that holds or requests the lock. |
| SRV_PORT | The port number of the OBServer node that holds or requests the lock. |
| TENANT_ID | The ID of the tenant that holds or requests the lock. |
| TRANS_ID | The ID of the transaction that holds or requests the lock. |
| TYPE | The type of the lock. Valid values:
|
| ID1 | Lock identifier 1.
|
| ID2 | Lock identifier 2.
|
| LMODE | The lock holding mode. Valid values:
|
| REQUEST | The lock requesting mode. Valid values:
|
| CTIME | The time to hold or wait for a lock, in microseconds. |
| BLOCK | Indicates whether the lock blocks other processes. Valid values:
|
More information
For more information about the LOCK TABLES statement, see LOCK TABLES.