Purpose
You can use this statement to lock one or more tables, partitions, or subpartitions in specified mode.
A locked table remains locked until the transaction is committed, rolled back, or rolled back to a savepoint before the table is locked.
Note
You can lock only tables in your own schema, or you must have the LOCK ANY TABLE 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, ...)
Parameters
| Parameter | Description |
|---|---|
| table_name | The name of the table to be locked. |
| partition_name | The name of the partition to be locked. |
| subpartition_name | The name of the subpartition to be locked. |
| lock_mode | The lock mode. OceanBase Database supports the following lock modes:
|
| NOWAIT | This keyword instructs the database to immediately return the control to you if a lock conflict occurs when you attempt to lock a table, partition, or subpartition. In this case, the database returns a message indicating that the table, partition, or subpartition is locked by another user. |
| WAIT | This keyword instructs the database to wait for the release of the lock until timeout, which is specified by integer in seconds, if a lock conflict occurs. The value of integer is not limited. |
Examples
The following examples show how to create a subpartitioned table and lock a partition.
Create a subpartitioned table named
test. Subpartitions generated bySUBPARTITION TEMPLATEare named in the format ofpartition name + 's' + subpartition template name. For example,p0ssp0,p0ssp1, andp0ssp2are subpartitions in thep0partition.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) );Lock the entire
testtable inEXCLUSIVEmode.obclient> LOCK TABLE test IN EXCLUSIVE MODE; Query OK, 0 rows affectedLock the
p1partition of thetesttable inEXCLUSIVEmode.obclient> LOCK TABLE test PARTITION (p1) IN EXCLUSIVE MODE; Query OK, 0 rows affectedLock the
p1ssp1subpartition of thetesttable inEXCLUSIVEmode.obclient> LOCK TABLE test SUBPARTITION (p1ssp1) IN EXCLUSIVE MODE; Query OK, 0 rows affectedLock multiple partitions of the
testtable inEXCLUSIVEmode.obclient> LOCK TABLE test PARTITION (p1, p2) IN EXCLUSIVE MODE; Query OK, 0 rows affectedLock multiple subpartitions of the
testtable inEXCLUSIVEmode.obclient> LOCK TABLE test SUBPARTITION (p0ssp1, p1ssp2) IN EXCLUSIVE MODE; Query OK, 0 rows affectedLock multiple partitions and subpartitions of the
testtable inEXCLUSIVEmode.obclient> LOCK TABLE test PARTITION (p1, p2), test SUBPARTITION (p0ssp0, p0ssp1) IN EXCLUSIVE MODE; Query OK, 0 rows affectedLock a nonexistent partition of the
testtable inEXCLUSIVEmode. In the following example, thep3partition does not exist. Thep0partition will not be locked either, and the entire statement will be rolled back.obclient> LOCK TABLE test PARTITION (p0, p3) IN EXCLUSIVE MODE; OBE-02149: Specified partition does not existLock the
testtable on two clients. On the lock holding client, lock the table inEXCLUSIVEmode. On the lock requesting client, lock the table inSHAREmode. If you do not specify theWAIT NorNOWAITkeyword in theLOCK TABLEstatement, the timeout is the statement timeout or the transaction timeout, whichever is shorter. In this example, a 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; # 10000s Query OK, 0 rows affected obclient> SET ob_query_timeout = 1000000; # 1s Query OK, 0 rows affected # The lock requesting client reports 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 on two clients. On the lock holding client, lock the table inEXCLUSIVEmode. On the lock requesting client, lock the table inSHAREmode with theNOWAITkeyword specified. After theNOWAITkeyword is specified in theLOCK TABLEstatement, an error is reported immediately when a table lock conflict occurs, and the error message containsNOWAIT. The error message differs from the one returned when theWAIT NorNOWAITkeyword is not specified in the statement.# 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 on two clients. On the lock holding client, lock the table inEXCLUSIVEmode. On the lock requesting client, lock the table inSHAREmode with theWAIT Nkeyword specified. After theWAIT Nkeyword is specified in theLOCK TABLEstatement, an error is reported if a table lock conflict occurs and lasts for more than N seconds. The error message containstimeout expired. The error message differs from the one returned when theWAIT NorNOWAITkeyword is not specified in the statement.# 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 lock requesting client reports 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