From 781841dbbccd9192085527971c54c9f3e7ca787f Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Thu, 19 Oct 2023 12:55:05 -0400 Subject: [PATCH] sql/logictest: fix flakes in select_for_update_read_committed 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 --- .../select_for_update_read_committed | 69 +++++++++++++------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/select_for_update_read_committed b/pkg/sql/logictest/testdata/logic_test/select_for_update_read_committed index e8b73e4e66bd..d255de6c77a1 100644 --- a/pkg/sql/logictest/testdata/logic_test/select_for_update_read_committed +++ b/pkg/sql/logictest/testdata/logic_test/select_for_update_read_committed @@ -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)) @@ -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 @@ -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 @@ -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 @@ -112,28 +123,38 @@ 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 @@ -141,13 +162,17 @@ SELECT a + b + c FROM abc FOR UPDATE # 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