# LogicTest: local local-opt local-parallel-stmts fakedist fakedist-opt fakedist-metadata

subtest automatic_retry

# On an implicit transaction, we retry automatically and the function
# eventually returns a result.
query I
SELECT crdb_internal.force_retry('50ms':::INTERVAL)
----
0

subtest automatic_retry

statement ok
BEGIN TRANSACTION; SAVEPOINT cockroach_restart

# The SELECT 1 is necessary to take the session out of the AutoRetry state,
# otherwise the statement below would be retries automatically.
statement ok
SELECT 1

query error restart transaction: TransactionRetryWithProtoRefreshError: forced by crdb_internal.force_retry()
SELECT crdb_internal.force_retry('500ms':::INTERVAL)

statement ok
ROLLBACK TO SAVEPOINT cockroach_restart

# wait until the transaction is at least 1 second
sleep 1s

# Ensure that ident case rules are used.
statement error pq: unimplemented: SAVEPOINT not supported except for cockroach_restart
SAVEPOINT "COCKROACH_RESTART"

# Ensure that ident case rules are used.
statement ok
SAVEPOINT COCKROACH_RESTART

query I
SELECT crdb_internal.force_retry('500ms':::INTERVAL)
----
0

statement ok
COMMIT

subtest schema_chage_with_rollback

# Test that creating a table repeatedly across restarts doesn't leave dangling
# rows behind (the rows are  associated with the correct descriptor).
# See #24785.

statement ok
BEGIN

statement ok
SAVEPOINT cockroach_restart

statement ok
CREATE TABLE t (
id INT PRIMARY KEY
)

statement ok
ROLLBACK TO SAVEPOINT cockroach_restart

# The following CREATE shouldn't be necessary. This test would like to just run
# the next insert (or a select) and check that it fails to resolve the table
# name. However, that doesn't currently work because of #24885.
statement ok
CREATE TABLE t (
id INT PRIMARY KEY
)

statement ok
INSERT INTO t (id) VALUES (1);

statement ok
COMMIT

query I
SELECT id FROM t
----
1

subtest rename_savepoint

query T
show session force_savepoint_restart
----
off

statement ok
SET force_savepoint_restart = true

query T
show session force_savepoint_restart
----
on

# We can now use anything that we want.
statement ok
BEGIN TRANSACTION; SAVEPOINT something_else; COMMIT

# Ensure that we can't mix-and-match names.
statement ok
BEGIN TRANSACTION; SAVEPOINT foo

statement error pq: SAVEPOINT "foo" is in use
ROLLBACK TO SAVEPOINT bar

# Verify we're doing the right thing for non-quoted idents.
statement ok
ROLLBACK TO SAVEPOINT FOO

# Verify use of quoted idents.
statement ok
SAVEPOINT "Foo Bar"

statement error pq: SAVEPOINT "Foo Bar" is in use
ROLLBACK TO SAVEPOINT FooBar

# Verify case-sensitivity of quoted idents.
statement error pq: SAVEPOINT "Foo Bar" is in use
ROLLBACK TO SAVEPOINT "foo bar"

statement ok
ROLLBACK TO SAVEPOINT "Foo Bar"

# Verify case-sensitivity of quoted vs. unquoted idents.
statement ok
SAVEPOINT "UpperCase"

statement error pq: SAVEPOINT "UpperCase" is in use
ROLLBACK TO SAVEPOINT UpperCase

statement ok
ABORT

statement ok
RESET force_savepoint_restart

query T
show session force_savepoint_restart
----
off
