Concurrency and Locks¶
Locks are used in BLUE to provide consistency when there are multiple concurrent transactions updating the database.
Read only transactions are never blocked, and never block anything. Even if you drop a database while concurrently running a query on it, both will succeed correctly (as long as the query starts running before the drop database commits).
Locking Modes¶
BLUE has two kinds of locks:
exclusive
- this lock mode prevents the resource from being modified by other statements.This lock tells other statements that they’ll have to wait in order to change an object.
DDL operations are always exclusive. They block other DDL operations, and update DML operations such as
INSERT
.inclusive
- ForINSERT
operations, an inclusive lock is obtained on a specific object. This prevents other statements from obtaining an exclusive lock on the object.This lock allows other statements to insert or delete data from a table, but they’ll have to wait in order to run DDL.
When are Locks Obtained?¶
Operation |
DDL |
|||
---|---|---|---|---|
Concurrent |
Concurrent |
Concurrent |
Concurrent |
|
Concurrent |
Concurrent |
Wait |
Wait |
|
DDL |
Concurrent |
Wait |
Wait |
Wait |
Statements that wait will exit with an error if they hit the lock timeout. The default timeout is 3 seconds, see statementLockTimeout
.
Monitoring Locks¶
Monitoring locks across the cluster can be useful when transaction contention takes place, and statements appear “stuck” while waiting for a previous statement to release locks.
The utility command DESCRIBE LOCKS can be used to see the active locks.
DESCRIBE_LOCKS();
Output:
statement_id|username|server |port|locked_object|lock_mode|statement_start_time|lock_start_time |statement_string |
------------+--------+------------+----+-------------+---------+--------------------+--------------------+-----------------------------------------------------------------------------------------------+
287 |sqream |192.168.1.91|5000|database$t |Inclusive| 2019-12-26 00:03:30| 2019-12-26 00:03:30|CREATE OR REPLACE TABLE nba2 AS SELECT "Name" FROM nba WHERE REGEXP_COUNT("Name", '( )+', 8)>1;|
For more information on troubleshooting lock related issues, see Lock Related Issues.