Skip to content

Commit

Permalink
sql/logictest: fix flakes in select_for_update_read_committed
Browse files Browse the repository at this point in the history
The `select_for_update_read_committed` tests were flaking because not
all statements were being run under READ COMMITTED isolation. The logic
test infrastructure does not allow fine-grained control of sessions, and
setting the isolation level in one statement would only apply to a
single session. Subsequent statements are not guaranteed to run in the
same session because they could run in any session in the connection
pool. This commit wraps each statement in an explicitly transaction with
an explicit isolation level to ensure READ COMMITTED is used.

In the future, we should investigate allowing fine-grained and explicit
control of sessions in logic tests.

Fixes #112677

Release note: None
  • Loading branch information
mgartner committed Oct 20, 2023
1 parent fbc0cc7 commit 430cffc
Showing 1 changed file with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
statement ok
SET CLUSTER SETTING sql.txn.read_committed_syntax.enabled = true

statement ok
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED

statement ok
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT, INDEX (b), FAMILY (a, b, c))

Expand All @@ -29,7 +26,7 @@ GRANT ALL on bcd TO testuser
user testuser

statement ok
BEGIN
BEGIN ISOLATION LEVEL READ COMMITTED

query III rowsort
SELECT * FROM abc WHERE a != 3 FOR UPDATE
Expand All @@ -50,14 +47,18 @@ user root
# Normal reads do not block.

query III rowsort
SELECT * FROM abc
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM abc;
COMMIT;
----
1 10 100
2 20 200
3 30 300

query III rowsort
SELECT * FROM bcd
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM bcd;
COMMIT;
----
20 200 2000
30 300 3000
Expand All @@ -66,28 +67,34 @@ SELECT * FROM bcd
# SKIP LOCKED reads do not block.

query III rowsort
SELECT * FROM abc FOR UPDATE SKIP LOCKED
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM abc FOR UPDATE SKIP LOCKED;
COMMIT;
----
3 30 300

query III rowsort
SELECT * FROM bcd FOR UPDATE SKIP LOCKED
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM bcd FOR UPDATE SKIP LOCKED;
COMMIT;
----
20 200 2000
30 300 3000
40 400 4000

# Shared reads block on exclusive locks but not on shared locks.

query III async,rowsort q00
SELECT * FROM abc FOR SHARE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM abc FOR SHARE;
COMMIT;
----
1 11 101
2 21 201
3 30 300

query III rowsort
SELECT * FROM bcd FOR SHARE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM bcd FOR SHARE;
COMMIT;
----
20 200 2000
30 300 3000
Expand All @@ -96,14 +103,18 @@ SELECT * FROM bcd FOR SHARE
# Exclusive reads block on both.

query III async,rowsort q01
SELECT * FROM abc FOR UPDATE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM abc FOR UPDATE;
COMMIT;
----
1 11 101
2 21 201
3 30 300

query III async,rowsort q02
SELECT * FROM bcd FOR UPDATE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM bcd FOR UPDATE;
COMMIT;
----
20 200 2000
30 300 3000
Expand All @@ -112,42 +123,56 @@ SELECT * FROM bcd FOR UPDATE
# Try more exclusive-locking queries.

query I async q03
SELECT a FROM abc WHERE a = 2 FOR UPDATE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT a FROM abc WHERE a = 2 FOR UPDATE;
COMMIT;
----
2

query I async q04
SELECT b FROM abc WHERE a = 2 FOR UPDATE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT b FROM abc WHERE a = 2 FOR UPDATE;
COMMIT;
----
21

query I async q05
SELECT c FROM abc WHERE a = 2 FOR UPDATE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT c FROM abc WHERE a = 2 FOR UPDATE;
COMMIT;
----
201

query I async q06
SELECT c FROM abc ORDER BY a DESC LIMIT 2 FOR UPDATE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT c FROM abc ORDER BY a DESC LIMIT 2 FOR UPDATE;
COMMIT;
----
300
201

query I async,rowsort q07
SELECT a + b + c FROM abc FOR UPDATE
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT a + b + c FROM abc FOR UPDATE;
COMMIT;
----
113
224
333

# Try some joins

query IIIII async q08
SELECT * FROM abc JOIN bcd USING (b) FOR SHARE
query IIIII async,rowsort q08
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM abc JOIN bcd USING (b) FOR SHARE;
COMMIT;
----
30 3 300 300 3000

query IIIII async q09
SELECT * FROM abc JOIN bcd USING (c) FOR UPDATE
query IIIII async,rowsort q09
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT * FROM abc JOIN bcd USING (c) FOR UPDATE;
COMMIT;
----
300 3 30 30 3000

Expand Down

0 comments on commit 430cffc

Please sign in to comment.