Back to Yugabyte Db

Explicit locking

docs/content/v2.20/explore/transactions/explicit-locking.md

2026.1.0.0-b253.7 KB
Original Source
<ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../explicit-locking/" class="nav-link active"> <i class="icon-postgres" aria-hidden="true"></i> YSQL </a> </li> </ul>

Explicit locking in YugabyteDB gives you granular control over concurrency and data integrity within your transactions. While YugabyteDB automatically handles the ACID properties through its underlying distributed transaction protocol, explicit locking allows you to prevent conflicts that might otherwise occur, or to implement custom concurrency patterns that extend beyond the default isolation levels.

By using explicit locks, you can guarantee exclusive or shared access to specific data, ensuring that subsequent transactions are appropriately blocked or permitted. This is particularly useful for complex business logic, long-running transactions, or scenarios where you require custom serializability guarantees.

Row-level locks

YugabyteDB's YSQL supports explicit row-level locking, similar to PostgreSQL. Explicit row-locks ensure that two transactions can never hold conflicting locks on the same row. When two transactions try to acquire conflicting lock modes, the semantics are dictated by YugabyteDB's concurrency control policies.

The following types of row locks are supported:

  • FOR UPDATE
  • FOR NO KEY UPDATE
  • FOR SHARE
  • FOR KEY SHARE

The following example uses the FOR UPDATE row lock with the fail-on-conflict concurrency control policy. First, a row is selected for update, thereby locking it, and subsequently updated. A concurrent transaction should not be able to abort this transaction by updating the value of that row after the row is locked.

{{% explore-setup-single %}}

Create a sample table and populate it with sample data, as follows:

sql
yugabyte=# CREATE TABLE t (k VARCHAR, v VARCHAR);
yugabyte=# INSERT INTO t VALUES ('k1', 'v1');

Next, connect to the universe using two independent ysqlsh instances. You can connect both session ysqlsh instances to the same server or to different servers.

Begin a transaction in the first session and perform a SELECT FOR UPDATE on the row in the table t. This locks the row for an update as a part of a transaction that has a very high priority (that is, in the high priority bucket, as explained in Transaction priorities):

sql
yugabyte=# BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
output
BEGIN
sql
yugabyte=# SELECT * from t WHERE k='k1' FOR UPDATE;
output
 k  | v
----+----
 k1 | v1
(1 row)

Before completing the transaction, try to update the same key in your other session using a basic update statement, as follows:

sql
yugabyte=# UPDATE t SET v='v1.1' WHERE k='k1';
output
ERROR:  All transparent retries exhausted. Operation failed. Try again: bb3aace4-5de2-41f9-981e-d9ca06671419 Conflicts with higher priority transaction: d4dadbf8-ca81-4bbd-b68c-067023f8ee6b

This operation fails because it conflicts with the row-level lock and as per Fail-on-Conflict concurrency control policy, the transaction aborts itself because it has a lower priority.

Note that the error message appears after all best-effort statement retries have been exhausted.

Finally, in the first session, update the row and commit the transaction, as follows:

sql
yugabyte=# UPDATE t SET v='v1.2' WHERE k='k1';
output
UPDATE 1
sql
yugabyte=# COMMIT;
output
COMMIT

This should succeed.