-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support syntax SET TRANSACTION READ ONLY AS OF ...
and SELECT ... AS OF
for Stale read
#22765
Comments
CRDB: https://www.cockroachlabs.com/docs/stable/follower-reads.html#run-read-only-transactions-that-use-follower-reads |
SET TRANSACTION READ ONLY WITH EXACT STALENESS '00:00:05';
SET TRANSACTION READ ONLY WITH EXACT TIMESTAMP '2021-01-19 00:00:00';
SET TRANSACTION READ ONLY WITH BOUNDED STALENESS '00:00:05';
SET TRANSACTION READ ONLY WITH BOUNDED TIMESTAMP '2021-01-19 00:00:00'; Would this be better? |
@djshow832 Would like this more. I think it's more clear.
|
@djshow832 yes, I really like this syntax: SET TRANSACTION AS OF SYSTEM TIME follower_read_timestamp(); The function is nice because it means the syntax can be unified between staleness (offset) and timestamp (ts). My understanding is that it also implies |
@morgo I have some concerns:
|
Thanks for the detailed reply :-) On this point:
It comes from System-versioned temporal tables (which MySQL does not support, but MariaDB does). There is an article here for SQL Server which says it is from ANSI SQL 2011. |
relative #18672 |
See also: #22658 |
We need to consider the |
Hi, @djshow832 @morgo
Read-Only transaction:
or
NOTE:default use |
@nolouch I like it a lot. It makes sense to me. Edit: One question. Should it be valid as
|
There is an added MySQL-ism to be aware of (it is minor). You can start a read only transaction with a session var as well: mysql [localhost:5731] {msandbox} ((none)) > SET tx_read_only=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql [localhost:5731] {msandbox} ((none)) > show warnings;
+---------+------+----------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | '@@tx_read_only' is deprecated and will be removed in a future release. Please use '@@transaction_read_only' instead |
+---------+------+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec) If you start a read-only transaction it won't explicitly update this variable though. JDBC drivers are noisy at checking the value of this. |
Hi @morgo Can we only support |
SET TRANSACTION READ ONLY WITH ...
SET TRANSACTION READ ONLY AS OF ...
and SELECT ... AS OF
for Stale read
I do some adjustments to make
I want to use
|
I am looking at how does CockroachDB deal with DDL/stale read compatibility. And from what I have seen, I think they may have |
No. And you are correct. However, the metadata loading process is not versioned on TiDB :( |
Note that the PostgreSQL one doesn't count, it is just a proposal from some random person. The In particular, in MariaDB, the name Therefore, as long as That said, there is a potential syntax ambiguity requiring look-ahead if we decide to use just SELECT * FROM t AS u;
SELECT * FROM t AS OF ....
-- ^ cannot distinguish between table-alias or as-of query until here BTW, CrDB's SELECT * FROM t AS OF SYSTEM TIME (a), u AS OF SYSTEM TIME (b); so let's not follow CrDB's syntax exactly either. |
After discussed with @kennytm for this comment, the conclusion:
This means we only introduced
|
done. |
Background
This is a subtask of #21094.
Starting staleness transactions with extending syntax
START TRANSACTION
makes integrating into Java applications much more difficult, but extending syntaxSET TRANSACTION
will be more acceptable. Besides, the original syntax is too complicated, see #22505 and #22506.Now that TiDB has supported
SET TRANSACTION READ ONLY
(syntax only), we need to extend it toSET TRANSACTION READ ONLY WITH [EXACT] {TIMESTAMP | STALENESS} Expression
.Here are some examples:
SELECT statement:
Read-Only transaction:
or
SET [GLOBAL|SESSION] TRANSACTION variables
is equivalent toSET [GLOBAL|SESSION] variables
in MySQL. However, setting global or session variables for staleness transactions may make junior users confused because there would be too many SQL interfaces to manipulate them. Now we only need to support transaction scope.By default, the
SET TRANSACTION
statement only affects the next transaction and they must be set BEFORE a transaction. E.g.Refer to the MySQL
SET TRANSACTION
document https://dev.mysql.com/doc/refman/8.0/en/set-transaction.html for more details.Furthermore, we are going to support system variables as synonyms for
SET TRANSACTION READ ONLY WITH [EXACT] {TIMESTAMP | STALENESS} Expression
. This is conventional in MySQL. But we don't need to expose these variables to users for now.The system variables are:
transaction_read_only_mode
forWITH [EXACT]
. Available choices are: "strong", "exact_stale", "noexact_stale".transaction_read_only_timestamp
for{TIMESTAMP | STALENESS} Expression
.On the other hand, keep the old syntax
START TRANSACTION READ ONLY WITH ...
for now, in case of requirement changes. But do not expose it to users in any documents.Implementation
Support the new syntax in the parser
Some
VariableAssignment
will be appended invarAssigns
:WITH [EXACT]
corresponds to a stale read flag. Available choices are: strong read, exact stale read, bounded stale read.TIMESTAMP Expression
corresponds to a timestamp or duration.For each
VariableAssignment
,IsGlobal
is false andIsSystem
is true.Set system variables for transactions
Define 2 system variables and add them into
initSynonymsSysVariables
.Note that the other 2 synonyms
transaction_isolation
andtransaction_read_only
don't actually work.For now, system variables only have 3 scopes: global, session, and none. We need to define a next-transaction scope.
Make system variables work
// TODO
The text was updated successfully, but these errors were encountered: