Purpose
This statement locks one or more tables, partitions, or subpartitions in the specified schema.
Locked tables remain locked until the transaction is committed, rolled back, or rolled back to a savepoint before the lock was applied.
Note
The table to be locked must be in the user's own schema, or the user must have the LOCK ANY TABLE system privilege.
Syntax
LOCK TABLE [ schema. ]table_name
[ partition_extension_clause]
[, [ schema. ] table_name
[ partition_extension_clause ]
]...
IN lock_mode MODE
[ NOWAIT
| WAIT integer
] ;
partition_extension_clause:
PARTITION (partition_name, ...)
| SUBPARTITION (subpartition_name, ...)
Syntax
| Field | Description |
|---|---|
| table_name | Specifies the name of the table to be locked. |
| partition_name | Specifies the name of the partition to be locked. |
| subpartition_name | Specifies the name of the subpartition to be locked. |
| lock_mode | Specifies the lock mode. The lock modes supported by OceanBase Database are as follows:
|
| NOWAIT | If a lock conflict occurs when locking the table, partition, or subpartition, specifying NOWAIT allows the database to immediately return control to the user. In this case, the database returns a message indicating that the table, partition, or subpartition is locked by another user. |
| WAIT | If a lock conflict occurs, the statement should wait for the conflicting lock to be released, up to the timeout specified by the user's interger value (in seconds). The value of integer is not limited. |
Examples
The following example creates a subpartitioned table and applies a lock to its partitions.
Create a subpartitioned table named
test. The subpartition names generated bySUBPARTITION TEMPLATEare formed by appending 's' and the subpartition template name to the partition name. For example, thep0partition has subpartitions namedp0ssp0,p0ssp1, andp0ssp2.CREATE TABLE TEST(col1 INT, col2 INT) 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) );Apply an exclusive lock to the entire
testtable.obclient> LOCK TABLE test IN EXCLUSIVE MODE; Query OK, 0 rows affectedApply an exclusive lock to the
p1partition of thetesttable.obclient> LOCK TABLE test PARTITION (p1) IN EXCLUSIVE MODE; Query OK, 0 rows affectedApply an exclusive lock to the
p1ssp1subpartition of thetesttable.obclient> LOCK TABLE test SUBPARTITION (p1ssp1) IN EXCLUSIVE MODE; Query OK, 0 rows affectedApply an exclusive lock to multiple partitions of the
testtable.obclient> LOCK TABLE test PARTITION (p1,p2) IN EXCLUSIVE MODE; Query OK, 0 rows affectedApply an exclusive lock to multiple subpartitions of the
testtable.obclient> LOCK TABLE test SUBPARTITION (p0ssp1,p1ssp2) IN EXCLUSIVE MODE; Query OK, 0 rows affectedApply an exclusive lock to multiple partitions and subpartitions of the
testtable.obclient> LOCK TABLE test PARTITION (p1, p2), test SUBPARTITION (p0ssp0, p0ssp1) IN EXCLUSIVE MODE; Query OK, 0 rows affectedAttempt to apply an exclusive lock to a non-existent partition
p3of thetesttable. In this example,p3does not exist, andp0will also fail to be locked, resulting in a rollback of the entire statement.obclient> LOCK TABLE test PARTITION (p0, p3) IN EXCLUSIVE MODE; OBE-02149: Specified partition does not existLock the
testtable in two client sessions. One session holds an exclusive lock, and the other requests a shared lock. When theLOCK TABLEstatement does not specify theWAIT NorNOWAITkeyword, the timeout is determined by the minimum of the statement timeout and transaction timeout. In this example, the lock conflict error will be reported after 1 second.# conn1 obclient> START TRANSACTION; Query OK, 0 rows affected obclient> LOCK TABLE test IN EXCLUSIVE MODE; Query OK, 0 rows affected # conn2 obclient> START TRANSACTION; Query OK, 0 rows affected obclient> SET ob_trx_timeout = 10000000000; # 10000 seconds Query OK, 0 rows affected obclient> SET ob_query_timeout = 1000000; # 1 second Query OK, 0 rows affected # The requesting client will receive a lock conflict error after 1 second obclient> LOCK TABLE test IN SHARE MODE; OBE-30006: resource busy; acquire with WAIT timeout expiredLock the
testtable in two client sessions. One session holds an exclusive lock, and the other requests a shared lock, using theNOWAITkeyword. When theLOCK TABLEstatement specifies theNOWAITkeyword, a lock conflict will result in an immediate error, with the error message indicating "NOWAIT". This differs from when theWAIT NorNOWAITkeywords are not specified.# conn1 obclient> START TRANSACTION; Query OK, 0 rows affected obclient> LOCK TABLE test IN EXCLUSIVE MODE; Query OK, 0 rows affected # conn2 obclient> START TRANSACTION; Query OK, 0 rows affected obclient> LOCK TABLE test IN SHARE MODE NOWAIT; OBE-00054: resource busy and acquire with NOWAIT specified or timeout expiredLock the
testtable in two client sessions. One session holds an exclusive lock, and the other requests a shared lock, using theWAIT Nkeyword. When theLOCK TABLEstatement specifies theWAIT Nkeyword, a lock conflict will result in a wait of N seconds. If the lock is not released after N seconds, an error will be reported, with the error message indicating "timeout expired". This differs from when theWAIT NorNOWAITkeywords are not specified.# conn1 obclient> START TRANSACTION; Query OK, 0 rows affected obclient> LOCK TABLE test IN EXCLUSIVE MODE; Query OK, 0 rows affected # conn2 obclient> START TRANSACTION; Query OK, 0 rows affected # The requesting client will receive a lock conflict error after 1 second obclient> LOCK TABLE test IN SHARE MODE WAIT 1; OBE-00054: resource busy and acquire with NOWAIT specified or timeout expired