---
title: LOCK
summary: Acquire table-level locks for PostgreSQL compatibility.
toc: true
docs_area: reference.sql
---
The LOCK statement acquires table-level locks on specified tables. This statement is primarily intended for PostgreSQL compatibility, particularly with pg_dump. When the pg_dump_compatibility cluster setting is enabled, all lock modes are accepted as no-ops since CockroachDB uses Multi-Version Concurrency Control (MVCC) where reads never block. When compatibility mode is disabled, LOCK returns a feature not supported error.
Required privileges
No specific privileges are documented in the implementation. [NEEDS REVIEW: privilege requirements]
Synopsis
LOCK [TABLE] table_name [, ...] [IN lock_mode MODE] [NOWAIT]
Parameters
| Parameter | Description | Required |
|---|---|---|
table_name |
The name of the table(s) to lock. Multiple tables can be specified as a comma-separated list. | Yes |
lock_mode |
The lock mode to acquire. If omitted, defaults to ACCESS EXCLUSIVE. |
No |
NOWAIT |
If specified, the statement will not wait if the lock cannot be acquired immediately. In CockroachDB, this has no effect when pg_dump_compatibility is enabled. |
No |
Lock modes
The following lock modes are supported:
ACCESS SHAREROW SHAREROW EXCLUSIVESHARE UPDATE EXCLUSIVESHARE ROW EXCLUSIVESHAREEXCLUSIVEACCESS EXCLUSIVE(default when no mode specified)
All lock modes are treated as no-ops when pg_dump_compatibility is enabled, since CockroachDB's MVCC concurrency control makes explicit locking unnecessary.
Examples
Basic table locking
SET pg_dump_compatibility = 'postgres';
LOCK TABLE employees IN ACCESS SHARE MODE;
Locking multiple tables
LOCK TABLE employees, departments IN ACCESS SHARE MODE;
Using NOWAIT
LOCK TABLE employees IN ACCESS SHARE MODE NOWAIT;
Default lock mode (ACCESS EXCLUSIVE)
LOCK TABLE employees;
Without TABLE keyword
LOCK employees IN SHARE MODE;
Compatibility notes
LOCK TABLE is only supported when the pg_dump_compatibility cluster setting is enabled. When disabled, all LOCK statements return a "feature not supported" error.
-- Enable pg_dump compatibility
SET pg_dump_compatibility = 'postgres';
-- Now LOCK statements work
LOCK TABLE mytable IN ACCESS SHARE MODE;
-- Disable compatibility
SET pg_dump_compatibility = 'off';
-- This will now fail
LOCK TABLE mytable IN ACCESS SHARE MODE;
-- Error: LOCK TABLE is not supported
See also
SET (session variable)for settingpg_dump_compatibility- PostgreSQL compatibility