Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

LOCK

Updated on 2024-06-03 GMT+08:00

Description

LOCK TABLE obtains a table-level lock.

GaussDB always tries to select the lock mode with minimum constraints when automatically requesting a lock for a statement referenced by a table. Use LOCK if users need a more strict lock mode. For example, suppose an application runs a transaction at the Read Committed isolation level and needs to ensure that data in a table remains stable in the duration of the transaction. To achieve this, you could obtain SHARE lock mode over the table before the query. This will prevent concurrent data changes and ensure subsequent reads of the table see a stable view of committed data. It is because the SHARE lock mode conflicts with the ROW EXCLUSIVE lock acquired by writers, and your LOCK TABLE name IN SHARE MODE statement will wait until any concurrent holders of ROW EXCLUSIVE mode locks commit or roll back. Therefore, once you obtain the lock, there are no uncommitted writes outstanding; furthermore none can begin until you release the lock.

Precautions

  • LOCK TABLE is useless outside a transaction block: the lock would remain held only to the completion of the statement. If LOCK TABLE is out of any transaction block, an error is reported.
  • If no lock mode is specified, then ACCESS EXCLUSIVE, the most restrictive mode, is used.
  • LOCK TABLE ... IN ACCESS SHARE MODE requires the SELECT permission on the target table. All other forms of LOCK require table-level UPDATE and/or the DELETE permission.
  • There is no UNLOCK TABLE statement. Locks are always released at transaction end.
  • LOCK TABLE only deals with table-level locks, and so the mode names involving ROW are all misnomers. These mode names should generally be read as indicating the intention of the user to acquire row-level locks within the locked table. Also, ROW EXCLUSIVE mode is a shareable table lock. Note that all lock modes have the same semantics as long as LOCK TABLE is involved. The only difference lies in whether locks conflict with each other. For details about the rules, see Table 1.
  • If the xc_maintenance_mode parameter is not enabled, an error is reported when an ACCESS EXCLUSIVE lock is applied for a system catalog.

Syntax

LOCK [ TABLE ] {[ ONLY ] name [, ...]| {name [ * ]} [, ...]}
    [ IN {ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE} MODE ]
    [ NOWAIT ];

Parameters

Table 1 Lock mode conflicts

Requested Lock Mode/Current Lock Mode

ACCESS SHARE

ROW SHARE

ROW EXCLUSIVE

SHARE UPDATE EXCLUSIVE

SHARE

SHARE ROW EXCLUSIVE

EXCLUSIVE

ACCESS EXCLUSIVE

ACCESS SHARE

-

-

-

-

-

-

-

X

ROW SHARE

-

-

-

-

-

-

X

X

ROW EXCLUSIVE

-

-

-

-

X

X

X

X

SHARE UPDATE EXCLUSIVE

-

-

-

X

X

X

X

X

SHARE

-

-

X

X

-

X

X

X

SHARE ROW EXCLUSIVE

-

-

X

X

X

X

X

X

EXCLUSIVE

-

X

X

X

X

X

X

X

ACCESS EXCLUSIVE

X

X

X

X

X

X

X

X

LOCK parameters are as follows:

  • name

    Specifies the name (optionally schema-qualified) of an existing table to lock.

    Tables are locked one-by-one in the order specified in the LOCK TABLE statement.

    Value range: an existing table name

    NOTE:

    You can use database links to perform operations on remote tables. For details, see DATABASE LINK.

  • ONLY

    If ONLY is specified, only that table is locked. If it is not specified, the table and all its sub-tables are locked.

  • ACCESS SHARE

    Conflicts with the ACCESS EXCLUSIVE lock mode only.

    The SELECT statement acquires a lock of this mode on referenced tables. Typically, any command that reads a table without modifying it acquires this lock mode.

  • ROW SHARE

    It conflicts with the EXCLUSIVE and ACCESS EXCLUSIVE lock modes.

    SELECT FOR UPDATE and SELECT FOR SHARE automatically acquire the ROW SHARE lock on the target table and add the ACCESS SHARE lock to other referenced tables except FOR SHARE and FOR UPDATE.

  • ROW EXCLUSIVE

    Allows concurrent read of a table but does not allow modification of data in the table. ROW EXCLUSIVE is the same as ROW SHARE. UPDATE, DELETE, and INSERT automatically acquire this lock on the target table and add the ACCESS SHARE lock to other referenced tables. Generally, all statements that modify table data acquire the ROW EXCLUSIVE lock for tables.

  • SHARE UPDATE EXCLUSIVE

    Protects a table against concurrent schema changes and VACUUM runs.

    The VACUUM (without FULL), ANALYZE, and CREATE INDEX CONCURRENTLY statements automatically request this lock.

  • SHARE

    Allows concurrent queries of a table but does not allow modification of the table.

    Acquired by CREATE INDEX (without CONCURRENTLY).

  • SHARE ROW EXCLUSIVE

    Protects a table against concurrent data changes, and is self-exclusive so that only one session can hold it at a time.

    No SQL statements automatically acquire this lock mode.

  • EXCLUSIVE

    Allows concurrent queries of the target table but does not allow any other operations.

    This mode allows only concurrent ACCESS SHARE locks; that is, only reads from the table can proceed in parallel with a transaction holding this lock mode.

    No SQL statements automatically acquire this lock mode on user tables. However, it will be acquired on some system catalogs in case of some operations.

  • ACCESS EXCLUSIVE

    Guarantees that the holder is the only transaction accessing the table in any way.

    The ALTER TABLE, DROP TABLE, TRUNCATE and REINDEX statements automatically request this lock.

    This is also the default lock mode for LOCK TABLE statements that do not specify a mode explicitly.

  • NOWAIT

    Specifies that LOCK TABLE should not wait for any conflicting locks to be released: if the specified lock(s) cannot be acquired immediately without waiting, the transaction is aborted.

    If a table-level lock is obtained without specifying NOWAIT and other mutex locks exist, the system waits for other locks to be released.

Examples

  • SHARE ROW EXCLUSIVE
    -- Create a schema.
    gaussdb=# CREATE SCHEMA tpcds;
    
    -- Create the tpcds.reason table.
    gaussdb=# CREATE TABLE tpcds.reason (
    r_reason_sk         INTEGER      NOT NULL,
    r_reason_id         CHAR(16)     NOT NULL,
    r_reason_desc       INTEGER
    );
    
    -- Insert multiple records into the table.
    gaussdb=# INSERT INTO tpcds.reason VALUES (1, 'AAAAAAAABAAAAAAA', '18'),(5, 'AAAAAAAACAAAAAAA', '362'),(7, 'AAAAAAAADAAAAAAA', '585');
    
    -- Create a table named reason_t1.
    gaussdb=# CREATE TABLE tpcds.reason_t1 AS TABLE tpcds.reason;
    
    -- Start a transaction.
    gaussdb=# START TRANSACTION;
    
    -- Use the SHARE ROW EXCLUSIVE MODE lock mode to lock a table.
    gaussdb=# LOCK TABLE tpcds.reason_t1 IN SHARE ROW EXCLUSIVE MODE;
    
    -- If you perform the DELETE operation on another device, the operation is blocked.
    gaussdb=# DELETE FROM tpcds.reason_t1 WHERE r_reason_sk = 7; 
    
    -- End the transaction and release the lock.
    gaussdb=# COMMIT;
    
  • ROW EXCLUSIVE
    -- Start a transaction.
    gaussdb=# START TRANSACTION;
    
    -- After the UPDATE statement is executed, request a ROW EXCLUSIVE lock on the referenced table.
    gaussdb=# UPDATE tpcds.reason_t1 SET r_reason_desc=180 WHERE r_reason_sk=1;
    
    -- When the ALTER TABLE statement is executed on another device, the operation is blocked.
    gaussdb=# ALTER TABLE tpcds.reason_t1 ADD r_reason_asc int;
    
    -- End the transaction and release the lock.
    gaussdb=# COMMIT;
  • ACCESS EXCLUSIVE
    -- Start a transaction.
    gaussdb=# START TRANSACTION;
    
    -- After the TRUNCATE statement is executed, request an ACCESS SHARE lock on the referenced table.
    gaussdb=# TRUNCATE tpcds.reason_t1;
    
    -- When the SELECT statement is executed on another device, the operation is blocked.
    gaussdb=# SELECT * FROM tpcds.reason_t1;
    
    -- End the transaction and release the lock.
    gaussdb=# COMMIT;
    
    -- Delete the tpcds.reason_t1 table.
    gaussdb=# DROP TABLE tpcds.reason_t1;
    
    -- Delete the tpcds.reason table.
    gaussdb=# DROP TABLE tpcds.reason;
    
    -- Delete a schema.
    gaussdb=# DROP SCHEMA tpcds CASCADE;

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback