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

# This example session documents that a SERIALIZABLE transaction is
# not immediately poisoned when it revisits a Range on which one of
# its intents has had its timestamp pushed. This allows it to continue
# laying down intents in a single pass, despite the possibility that it
# will restart on commit. A SNAPSHOT transaction can always proceed and
# commit with its new timestamp.
#
# Note that ORDER BY id is done on selects which expect more than a
# single result, to account for the distsql config, which randomly
# splits ranges. This can cause table scans to return tuples in any
# arbitrary order.

statement ok
CREATE TABLE t (id INT PRIMARY KEY)

statement ok
INSERT INTO t VALUES (1)

statement ok
GRANT ALL ON t TO testuser

statement ok
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE, PRIORITY LOW

statement ok
INSERT INTO t VALUES (2)

# Switch users and push the above insert to a higher timestamp.
user testuser

statement ok
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE, PRIORITY HIGH

# This pushes the intent.
query I
SELECT * FROM t
----
1

statement ok
COMMIT

# Switch back and observe that we can still read our data - the txn is still
# operational and will continue to lay down its intents in the first pass.
user root

query I
SELECT * FROM t ORDER BY id
----
1
2

# On commit, there were no key spans that require updating, so the txn
# coordinator should handle the retry automatically and succeed.
statement ok
COMMIT

# The same type of session for a SNAPSHOT transaction shouldn't be poisoned.
statement ok
BEGIN TRANSACTION ISOLATION LEVEL SNAPSHOT, PRIORITY LOW

statement ok
INSERT INTO t VALUES (3)

user testuser

statement ok
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE, PRIORITY HIGH

# This pushes the intent.
query I
SELECT * FROM t ORDER BY id
----
1
2

statement ok
COMMIT

user root

query I
SELECT * FROM t ORDER BY id
----
1
2
3

statement ok
COMMIT
