Skip to content

Commit

Permalink
sql: enable the skip-locked wait policy
Browse files Browse the repository at this point in the history
Now that KV support this, we can pass the wait policy through.

Release note (sql change): SELECT ... FOR {UPDATE,SHARE} SKIP LOCKED
is now supported. The option can be used to skip rows that cannot be
immediately locked instead of blocking on contended row-level lock
acquisition.
  • Loading branch information
nvanbenschoten committed Jun 30, 2022
1 parent 05ca68a commit 1c2f2ff
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 23 deletions.
3 changes: 0 additions & 3 deletions pkg/sql/catalog/descpb/locking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ enum ScanLockingWaitPolicy {
BLOCK = 0;

// SKIP_LOCKED represents SKIP LOCKED - skip rows that can't be locked.
//
// NOTE: SKIP_LOCKED is not currently implemented and does not make it out of
// the SQL optimizer without throwing an error.
SKIP_LOCKED = 1;

// ERROR represents NOWAIT - raise an error if a row cannot be locked.
Expand Down
92 changes: 76 additions & 16 deletions pkg/sql/logictest/testdata/logic_test/select_for_update
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,38 @@ SELECT 1 FOR UPDATE OF public.a
query error pgcode 42601 FOR UPDATE must specify unqualified relation names
SELECT 1 FOR UPDATE OF db.public.a

# We don't currently support SKIP LOCKED, since it returns an inconsistent view
# and generally has strange semantics with respect to serializable isolation.
# Support may be added in the future.

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query I
SELECT 1 FOR UPDATE SKIP LOCKED
----
1

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query I
SELECT 1 FOR NO KEY UPDATE SKIP LOCKED
----
1

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query I
SELECT 1 FOR SHARE SKIP LOCKED
----
1

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query I
SELECT 1 FOR KEY SHARE SKIP LOCKED
----
1

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query error pgcode 42P01 relation "a" in FOR UPDATE clause not found in FROM clause
SELECT 1 FOR UPDATE OF a SKIP LOCKED

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query error pgcode 42P01 relation "a" in FOR UPDATE clause not found in FROM clause
SELECT 1 FOR UPDATE OF a SKIP LOCKED FOR NO KEY UPDATE OF b SKIP LOCKED

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query error pgcode 42P01 relation "a" in FOR UPDATE clause not found in FROM clause
SELECT 1 FOR UPDATE OF a SKIP LOCKED FOR NO KEY UPDATE OF b NOWAIT

query error pgcode 42P01 relation "a" in FOR UPDATE clause not found in FROM clause
SELECT 1 FOR UPDATE OF a SKIP LOCKED FOR SHARE OF b, c SKIP LOCKED FOR NO KEY UPDATE OF d SKIP LOCKED FOR KEY SHARE OF e, f SKIP LOCKED

query I
SELECT 1 FOR UPDATE NOWAIT
----
Expand Down Expand Up @@ -105,18 +112,24 @@ query error pgcode 42P01 relation "a" in FOR UPDATE clause not found in FROM cla
SELECT 1 FOR UPDATE OF a NOWAIT FOR NO KEY UPDATE OF b NOWAIT

query error pgcode 42P01 relation "a" in FOR UPDATE clause not found in FROM clause
SELECT 1 FOR UPDATE OF a NOWAIT FOR SHARE OF b, c NOWAIT FOR NO KEY UPDATE OF d NOWAIT FOR KEY SHARE OF e, f NOWAIT
SELECT 1 FOR UPDATE OF a NOWAIT FOR SHARE OF b, c NOWAIT FOR NO KEY UPDATE OF d NOWAIT FOR KEY SHARE OF e, f NOWAIT

# Locking clauses both inside and outside of parenthesis are handled correctly.

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query I
((SELECT 1)) FOR UPDATE SKIP LOCKED
----
1

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query I
((SELECT 1) FOR UPDATE SKIP LOCKED)
----
1

query error unimplemented: SKIP LOCKED lock wait policy is not supported
query I
((SELECT 1 FOR UPDATE SKIP LOCKED))
----
1

# FOR READ ONLY is ignored, like in Postgres.
query I
Expand Down Expand Up @@ -295,7 +308,7 @@ SELECT * FROM t FOR KEY SHARE
statement ok
ROLLBACK

# The NOWAIT wait policy returns error when conflicting lock is encountered.
# The NOWAIT wait policy returns error when a conflicting lock is encountered.

statement ok
INSERT INTO t VALUES (1, 1)
Expand Down Expand Up @@ -372,3 +385,50 @@ user root

statement ok
ROLLBACK

# The SKIP LOCKED wait policy skip rows when a conflicting lock is encountered.

statement ok
INSERT INTO t VALUES (2, 2), (3, 3), (4, 4)

statement ok
BEGIN; UPDATE t SET v = 3 WHERE k = 2

user testuser

statement ok
BEGIN

query II
SELECT * FROM t FOR UPDATE SKIP LOCKED
----
1 1
3 3
4 4

statement ok
UPDATE t SET v = 4 WHERE k = 3

query II
SELECT * FROM t FOR UPDATE SKIP LOCKED
----
1 1
3 4
4 4

user root

query II
SELECT * FROM t FOR UPDATE SKIP LOCKED
----
2 3

statement ok
ROLLBACK

user testuser

statement ok
ROLLBACK

user root
Loading

0 comments on commit 1c2f2ff

Please sign in to comment.