subtest automatic_retry

statement ok
CREATE SEQUENCE s

# On an implicit transaction, we retry automatically and the function
# eventually returns a result.
query I
SELECT IF(nextval('s')<3, crdb_internal.force_retry('1h':::INTERVAL), 0)
----
0

# Demonstrate that the txn was indeed retried.
query I
SELECT currval('s')
----
3

statement ok
DROP SEQUENCE s

subtest automatic_retry

statement ok
CREATE SEQUENCE s;

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

skipif config local-read-committed
query error restart transaction: crdb_internal.force_retry\(\): TransactionRetryWithProtoRefreshError: forced by crdb_internal.force_retry\(\)
SELECT crdb_internal.force_retry('1h':::INTERVAL)

onlyif config local-read-committed
query error restart transaction: read committed retry limit exceeded; set by max_retries_for_read_committed=10: crdb_internal.force_retry\(\): TransactionRetryWithProtoRefreshError: forced by crdb_internal.force_retry\(\)
SELECT crdb_internal.force_retry('1h':::INTERVAL)

statement ok
ROLLBACK TO SAVEPOINT cockroach_restart

query I
SELECT IF(nextval('s')<3, crdb_internal.force_retry('1h':::INTERVAL), 0)
----
0

# Demonstrate that the txn was indeed retried.
query I
SELECT currval('s')
----
3

statement ok
COMMIT

# subtest savepoint_name

# Check that releasing the special cockroach_restart savepoint moves us to CommitWait.

statement ok
BEGIN

statement ok
SAVEPOINT cockroach_restart

query T
SHOW TRANSACTION STATUS
----
Open

statement ok
RELEASE SAVEPOINT cockroach_restart

query T
SHOW TRANSACTION STATUS
----
CommitWait

statement ok
ROLLBACK

# Ensure that ident case rules are used.
statement ok
BEGIN

statement ok
SAVEPOINT "COCKROACH_RESTART"

query T
SHOW TRANSACTION STATUS
----
Open

statement ok
RELEASE SAVEPOINT "COCKROACH_RESTART"

query T
SHOW TRANSACTION STATUS
----
Open

statement ok
ROLLBACK

subtest schema_change_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 "bar" does not exist
ROLLBACK TO SAVEPOINT bar

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

statement ok
ABORT; BEGIN TRANSACTION

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

statement error pq: savepoint "foobar" does not exist
ROLLBACK TO SAVEPOINT FooBar

# Verify case-sensitivity of quoted idents.
statement error pq: savepoint "foo bar" does not exist
ROLLBACK TO SAVEPOINT "foo bar"

statement ok
ROLLBACK TO SAVEPOINT "Foo Bar"

query TB colnames
SHOW SAVEPOINT STATUS
----
savepoint_name is_initial_savepoint
Foo Bar  true

statement ok
ABORT; BEGIN TRANSACTION

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

statement error pq: savepoint "uppercase" does not exist
ROLLBACK TO SAVEPOINT UpperCase

query TB colnames
SHOW SAVEPOINT STATUS
----
savepoint_name is_initial_savepoint
UpperCase  true

statement ok
ABORT

statement ok
RESET force_savepoint_restart

query T
show session force_savepoint_restart
----
off
