Skip to content

Commit

Permalink
sql: use the correct locking strength for FOR SHARE clause
Browse files Browse the repository at this point in the history
Previously, FOR SHARE and FOR KEY SHARE would use non-locking KV scans.
Now that the lock table supports shared locks, we can use lock.Shared as
the locking strength for KV scans. This patch does that, and in doing
so, wires up SHARED locks end to end.

By default, we turn off this functionality for serializable transactions.
Instead, it's gated behind a session setting called
`enable_shared_locking_for_serializable`.

Informs cockroachdb#91545

Release note (sql change): SELECT FOR SHARE and SELECT FOR UPDATE
previously did not acquire any locks. Users issuing these statements
would expect them to acquire shared locks (multiple readers allowed,
no writers though). This patch switches over the behavior to acquire
such read locks.

For serializable transactions, we default to the old behaviour, unless
the `enable_shared_locking_for_serializable` session setting is set
to true. We'll probably switch this behavior in the future, but for
now, given shared locks are a preview feature, we gate things behind
a session setting.
  • Loading branch information
arulajmani committed Sep 29, 2023
1 parent e8f27c3 commit eeb93c8
Show file tree
Hide file tree
Showing 22 changed files with 265 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3638,6 +3638,10 @@ func (m *sessionDataMutator) SetDurableLockingForSerializable(val bool) {
m.data.DurableLockingForSerializable = val
}

func (m *sessionDataMutator) SetSharedLockingForSerializable(val bool) {
m.data.SharedLockingForSerializable = val
}

// Utility functions related to scrubbing sensitive information on SQL Stats.

// quantizeCounts ensures that the Count field in the
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -5346,6 +5346,7 @@ enable_insert_fast_path on
enable_multiple_modifications_of_table off
enable_multiregion_placement_policy off
enable_seqscan on
enable_shared_locking_for_serializable off
enable_super_regions off
enable_zigzag_join off
enforce_home_region off
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,7 @@ enable_insert_fast_path on N
enable_multiple_modifications_of_table off NULL NULL NULL string
enable_multiregion_placement_policy off NULL NULL NULL string
enable_seqscan on NULL NULL NULL string
enable_shared_locking_for_serializable off NULL NULL NULL string
enable_super_regions off NULL NULL NULL string
enable_zigzag_join off NULL NULL NULL string
enforce_home_region off NULL NULL NULL string
Expand Down Expand Up @@ -2949,6 +2950,7 @@ enable_insert_fast_path on N
enable_multiple_modifications_of_table off NULL user NULL off off
enable_multiregion_placement_policy off NULL user NULL off off
enable_seqscan on NULL user NULL on on
enable_shared_locking_for_serializable off NULL user NULL off off
enable_super_regions off NULL user NULL off off
enable_zigzag_join off NULL user NULL off off
enforce_home_region off NULL user NULL off off
Expand Down Expand Up @@ -3107,6 +3109,7 @@ enable_insert_fast_path NULL NULL NULL
enable_multiple_modifications_of_table NULL NULL NULL NULL NULL
enable_multiregion_placement_policy NULL NULL NULL NULL NULL
enable_seqscan NULL NULL NULL NULL NULL
enable_shared_locking_for_serializable NULL NULL NULL NULL NULL
enable_super_regions NULL NULL NULL NULL NULL
enable_zigzag_join NULL NULL NULL NULL NULL
enforce_home_region NULL NULL NULL NULL NULL
Expand Down
119 changes: 119 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/select_for_share
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# LogicTest: !local-mixed-22.2-23.1

statement ok
CREATE TABLE t(a INT PRIMARY KEY);
INSERT INTO t VALUES(1);
GRANT ALL ON t TO testuser;
CREATE USER testuser2 WITH VIEWACTIVITY;
GRANT SYSTEM MODIFYCLUSTERSETTING TO testuser;
GRANT ALL ON t TO testuser2;

user testuser

statement ok
SET enable_shared_locking_for_serializable = true;

statement ok
BEGIN

query I
SELECT * FROM t WHERE a = 1 FOR SHARE;
----
1

# Start another transaction to show multiple transactions can acquire SHARED
# locks at the same time.

user root

statement ok
SET enable_shared_locking_for_serializable = true;

statement ok
BEGIN

query I
SELECT * FROM t WHERE a = 1 FOR SHARE;
----
1

user testuser2

statement async writeReq count 1
UPDATE t SET a = 2 WHERE a = 1

# TODO(arul): Until https://github.com/cockroachdb/cockroach/issues/107766 is
# addressed, we'll incorrectly report shared locks as having "Exclusive" lock
# strength; We'll also only report a single holder (the other row in there is
# the waiting UPDATE request, not the second shared lock holder). However,
# having this query in here is useful to make sure there are locks and waiters
# on our key, meaning setting the cluster setting above actually did something;
# otherwise, had we used non-locking reads, we'd have failed here.
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
----
database_name schema_name table_name lock_key_pretty lock_strength durability isolation_level granted contended
test public t /Table/106/1/1/0 Exclusive Unreplicated SERIALIZABLE true true
test public t /Table/106/1/1/0 Exclusive Unreplicated SERIALIZABLE false true

# Commit the first transaction and rollback the second.

user testuser

statement ok
COMMIT

user root

statement ok
ROLLBACK

user testuser2

# Now that both the transactions that issued shared lock reads have been
# finalized, the write should be able to proceed.

awaitstatement writeReq

query I
SELECT * FROM t;
----
2

# ------------------------------------------------------------------------------
# Tests to ensure the enable_shared_locking_for_serializable session variable
# works as expected.
# -----------------------------------------------------------------------------

user testuser

statement ok
SET enable_shared_locking_for_serializable = false

statement ok
BEGIN ISOLATION LEVEL SERIALIZABLE

query I
SELECT * FROM t WHERE a = 2 FOR SHARE
----
2

user testuser2

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
----
database_name schema_name table_name lock_key_pretty lock_strength durability isolation_level granted contended

user testuser

statement ok
COMMIT

# TODO(arul): Add a test to show that the session setting doesn't apply to read
# committed transactions. We currently can't issue SELECT FOR SHARE statements
# in read committed transactions because durable locking hasn't been fully
# hooked up.



6 changes: 6 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/select_for_update
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# TODO(arul): remove this before merging.
statement ok
SET enable_shared_locking_for_serializable = true

query I
SELECT 1 FOR UPDATE
----
Expand Down Expand Up @@ -293,6 +297,7 @@ ROLLBACK
statement ok
BEGIN READ ONLY

skipif config local-mixed-22.2-23.1
statement error cannot execute FOR SHARE in a read-only transaction
SELECT * FROM t FOR SHARE

Expand All @@ -302,6 +307,7 @@ ROLLBACK
statement ok
BEGIN READ ONLY

skipif config local-mixed-22.2-23.1
statement error cannot execute FOR KEY SHARE in a read-only transaction
SELECT * FROM t FOR KEY SHARE

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/show_source
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enable_insert_fast_path on
enable_multiple_modifications_of_table off
enable_multiregion_placement_policy off
enable_seqscan on
enable_shared_locking_for_serializable off
enable_super_regions off
enable_zigzag_join off
enforce_home_region off
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/fakedist-disk/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/fakedist-vec-off/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/fakedist/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/local-vec-off/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/local/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/fk
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ vectorized: true
statement ok
SET enable_implicit_fk_locking_for_serializable = true

# We need to enable shared locks for serializable transactions for this to have
# a meaningful effect.
statement ok
SET enable_shared_locking_for_serializable=true

query T
EXPLAIN INSERT INTO child VALUES (1,1), (2,2)
----
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/fk_read_committed
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ insert cookies
statement ok
SET enable_implicit_fk_locking_for_serializable = true

# We als need to enable shared locks for serializable transactions for this to
# have a meaningful effect.
statement ok
SET enable_shared_locking_for_serializable=true

query T
EXPLAIN (OPT) INSERT INTO cookies VALUES (1, 1)
----
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/select_for_update
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ vectorized: true
spans: FULL SCAN
locking strength: for no key update

# By default, SELECT FOR SHARE doesn't acquire any locks.

query T
EXPLAIN (VERBOSE) SELECT * FROM t FOR SHARE
----
distribution: local
vectorized: true
·
• scan
columns: (a, b)
estimated row count: 1,000 (missing stats)
table: t@t_pkey
spans: FULL SCAN

# However, if the session setting is configured to do so, SELECT FOR SHARE will
# acquire shared locks.

statement ok
SET enable_shared_locking_for_serializable = true

query T
EXPLAIN (VERBOSE) SELECT * FROM t FOR SHARE
----
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/opt/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type Memo struct {
useImprovedJoinElimination bool
implicitFKLockingForSerializable bool
durableLockingForSerializable bool
sharedLockingForSerializable bool

// txnIsoLevel is the isolation level under which the plan was created. This
// affects the planning of some locking operations, so it must be included in
Expand Down Expand Up @@ -240,6 +241,7 @@ func (m *Memo) Init(ctx context.Context, evalCtx *eval.Context) {
useImprovedJoinElimination: evalCtx.SessionData().OptimizerUseImprovedJoinElimination,
implicitFKLockingForSerializable: evalCtx.SessionData().ImplicitFKLockingForSerializable,
durableLockingForSerializable: evalCtx.SessionData().DurableLockingForSerializable,
sharedLockingForSerializable: evalCtx.SessionData().SharedLockingForSerializable,
txnIsoLevel: evalCtx.TxnIsoLevel,
}
m.metadata.Init()
Expand Down Expand Up @@ -383,6 +385,7 @@ func (m *Memo) IsStale(
m.useImprovedJoinElimination != evalCtx.SessionData().OptimizerUseImprovedJoinElimination ||
m.implicitFKLockingForSerializable != evalCtx.SessionData().ImplicitFKLockingForSerializable ||
m.durableLockingForSerializable != evalCtx.SessionData().DurableLockingForSerializable ||
m.sharedLockingForSerializable != evalCtx.SessionData().SharedLockingForSerializable ||
m.txnIsoLevel != evalCtx.TxnIsoLevel {
return true, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/optbuilder/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder",
visibility = ["//visibility:public"],
deps = [
"//pkg/clusterversion",
"//pkg/kv/kvserver/concurrency/isolation",
"//pkg/server/telemetry",
"//pkg/settings",
Expand Down
Loading

0 comments on commit eeb93c8

Please sign in to comment.