From e3fd808a66f7516c03797fb0d2fb77efa4f7d264 Mon Sep 17 00:00:00 2001 From: Arul Ajmani Date: Thu, 20 Nov 2025 17:58:41 -0500 Subject: [PATCH] concurrency: use the correct lock strength for waiting requests Previously, we incorrectly assumed every locking request waiting in the lock table was doing so with Exclusive lock strength when returning output for the cluster locks table. Epic: none Release note: None --- .../testdata/logic_test/cluster_locks_tenant | 2 +- pkg/kv/kvserver/concurrency/lock_table.go | 2 +- .../testdata/logic_test/cluster_locks | 53 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/pkg/ccl/logictestccl/testdata/logic_test/cluster_locks_tenant b/pkg/ccl/logictestccl/testdata/logic_test/cluster_locks_tenant index 25cd13916be3..d176bd9f4a3f 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/cluster_locks_tenant +++ b/pkg/ccl/logictestccl/testdata/logic_test/cluster_locks_tenant @@ -203,7 +203,7 @@ query TTTTTTTBB colnames,rowsort SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, isolation_level, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn4' ---- database_name schema_name table_name lock_key_pretty lock_strength durability isolation_level granted contended -test public t /Table/106/1/"b"/0 Exclusive Unreplicated SERIALIZABLE false true +test public t /Table/106/1/"b"/0 Intent Unreplicated SERIALIZABLE false true query I SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't' diff --git a/pkg/kv/kvserver/concurrency/lock_table.go b/pkg/kv/kvserver/concurrency/lock_table.go index 2bc0d0793f67..4068feb10426 100644 --- a/pkg/kv/kvserver/concurrency/lock_table.go +++ b/pkg/kv/kvserver/concurrency/lock_table.go @@ -2069,7 +2069,7 @@ func (kl *keyLocks) lockStateInfo(now time.Time, rangeID roachpb.RangeID) []roac lockWaiters = append(lockWaiters, lock.Waiter{ WaitingTxn: g.txnMeta(), ActiveWaiter: qg.active, - Strength: lock.Exclusive, + Strength: qg.mode.Strength, WaitDuration: now.Sub(g.mu.curLockWaitStart), }) g.mu.Unlock() diff --git a/pkg/sql/logictest/testdata/logic_test/cluster_locks b/pkg/sql/logictest/testdata/logic_test/cluster_locks index a3e257a4f555..02dc9a051451 100644 --- a/pkg/sql/logictest/testdata/logic_test/cluster_locks +++ b/pkg/sql/logictest/testdata/logic_test/cluster_locks @@ -268,7 +268,7 @@ query TTTTTTTBB colnames SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, isolation_level, granted, contended FROM crdb_internal.cluster_locks WHERE range_id=$r1 AND txn_id='$txn4' ---- database_name schema_name table_name lock_key_pretty lock_strength durability isolation_level granted contended -test public t /Table/106/1/"b"/0 Exclusive Unreplicated SERIALIZABLE false true +test public t /Table/106/1/"b"/0 Intent Unreplicated SERIALIZABLE false true query TTTTTTTBB colnames,rowsort SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, isolation_level, granted, contended FROM crdb_internal.cluster_locks WHERE range_id=$r2 AND txn_id='$txn3' @@ -431,3 +431,54 @@ awaitstatement iso2 statement ok COMMIT + +user root + +statement ok +BEGIN; +SELECT * FROM t WHERE k = 'a' FOR UPDATE; + +user testuser + +statement ok +BEGIN; +SET enable_shared_locking_for_serializable = true + +statement async share1 +SELECT * FROM t WHERE k = 'a' FOR SHARE; + +user testuser 2 + +statement ok +BEGIN; + +statement async put1 +DELETE FROM t WHERE k = 'a'; + +user root + +# Verify that the waiting transaction reports Shared strength, not Exclusive. +query TTTTTTTBB colnames,retry,rowsort +SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, isolation_level, granted, contended FROM crdb_internal.cluster_locks WHERE table_name = 't' +---- +database_name schema_name table_name lock_key_pretty lock_strength durability isolation_level granted contended +test public t /Table/106/1/"a"/0 Exclusive Unreplicated SERIALIZABLE true true +test public t /Table/106/1/"a"/0 Shared Unreplicated SERIALIZABLE false true +test public t /Table/106/1/"a"/0 Intent Unreplicated READ COMMITTED false true + +statement ok +COMMIT + +user testuser + +awaitstatement share1 + +statement ok +COMMIT + +user testuser2 + +awaitstatement put1 + +statement ok +ROLLBACK