From 1a041da9bbd03e5cc1b3b46ace1817a8a37c26db Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 8 Dec 2021 10:25:56 +0800 Subject: [PATCH] Update the description of pessimistic transaction #7247 (#6980) (#7122) --- benchmark/benchmark-tidb-using-sysbench.md | 2 +- best-practices/tidb-best-practices.md | 4 ++-- mysql-compatibility.md | 2 +- optimistic-transaction.md | 2 +- pessimistic-transaction.md | 10 +++++----- releases/release-3.0-ga.md | 4 ++-- releases/release-3.0.0-rc.2.md | 2 +- releases/release-3.0.8.md | 2 +- sql-statements/sql-statement-begin.md | 2 +- sql-statements/sql-statement-select.md | 2 +- system-variables.md | 2 +- transaction-isolation-levels.md | 2 +- transaction-overview.md | 2 +- troubleshoot-write-conflicts.md | 2 +- whats-new-in-tidb-4.0.md | 2 +- 15 files changed, 21 insertions(+), 21 deletions(-) diff --git a/benchmark/benchmark-tidb-using-sysbench.md b/benchmark/benchmark-tidb-using-sysbench.md index ee865acc066e3..da372fea0dd1b 100644 --- a/benchmark/benchmark-tidb-using-sysbench.md +++ b/benchmark/benchmark-tidb-using-sysbench.md @@ -89,7 +89,7 @@ db-driver=mysql > **Note:** > -> If you enable the optimistic transaction model (TiDB uses the pessimistic transaction model by default), TiDB rolls back transactions when a concurrency conflict is found. Setting `tidb_disable_txn_auto_retry` to `off` turns on the automatic retry mechanism after meeting a transaction conflict, which can prevent Sysbench from quitting because of the transaction conflict error. +> If you enable the optimistic transaction model (TiDB uses the pessimistic transaction mode by default), TiDB rolls back transactions when a concurrency conflict is found. Setting `tidb_disable_txn_auto_retry` to `off` turns on the automatic retry mechanism after meeting a transaction conflict, which can prevent Sysbench from quitting because of the transaction conflict error. Before importing the data, it is necessary to make some settings to TiDB. Execute the following command in MySQL client: diff --git a/best-practices/tidb-best-practices.md b/best-practices/tidb-best-practices.md index 8a5f5c0391a42..a2b6d1c06792f 100644 --- a/best-practices/tidb-best-practices.md +++ b/best-practices/tidb-best-practices.md @@ -41,9 +41,9 @@ TiDB provides complete distributed transactions and the model has some optimizat Assume that the database is used as a counter. High access concurrency might lead to severe conflicts, resulting in multiple retries or even timeouts. Therefore, in the scenario of severe conflicts, it is recommended to use the pessimistic transaction mode or to solve problems at the system architecture level, such as placing counter in Redis. Nonetheless, the optimistic transaction model is efficient if the access conflict is not very severe. -* Pessimistic transaction model +* Pessimistic transaction mode - In TiDB, the pessimistic transaction model has almost the same behavior as in MySQL. The transaction applies a lock during the execution phase, which avoids retries in conflict situations and ensures a higher success rate. By applying the pessimistic locking, you can also lock data in advance using `SELECT FOR UPDATE`. + In TiDB, the pessimistic transaction mode has almost the same behavior as in MySQL. The transaction applies a lock during the execution phase, which avoids retries in conflict situations and ensures a higher success rate. By applying the pessimistic locking, you can also lock data in advance using `SELECT FOR UPDATE`. However, if the application scenario has fewer conflicts, the optimistic transaction model has better performance. diff --git a/mysql-compatibility.md b/mysql-compatibility.md index c36b430fbaceb..b9b4064b47dd4 100644 --- a/mysql-compatibility.md +++ b/mysql-compatibility.md @@ -15,7 +15,7 @@ However, some features of MySQL are not supported. This could be because there i > **Note:** > -> This page refers to general differences between MySQL and TiDB. Refer to the dedicated pages for [Security](/security-compatibility-with-mysql.md) and [Pessimistic Transaction Model](/pessimistic-transaction.md#difference-with-mysql-innodb) compatibility. +> This page refers to general differences between MySQL and TiDB. Refer to the dedicated pages for [Security](/security-compatibility-with-mysql.md) and [Pessimistic Transaction Mode](/pessimistic-transaction.md#difference-with-mysql-innodb) compatibility. ## Unsupported features diff --git a/optimistic-transaction.md b/optimistic-transaction.md index 0a66178d7fb9b..569d5b5d02741 100644 --- a/optimistic-transaction.md +++ b/optimistic-transaction.md @@ -11,7 +11,7 @@ Before enabling optimistic transactions, make sure that your application correct > **Note:** > -> Starting from v3.0.8, TiDB uses the [pessimistic transaction model](/pessimistic-transaction.md) by default. However, this does not affect your existing cluster if you upgrade it from v3.0.7 or earlier to v3.0.8 or later. In other words, **only newly created clusters default to using the pessimistic transaction model**. +> Starting from v3.0.8, TiDB uses the [pessimistic transaction mode](/pessimistic-transaction.md) by default. However, this does not affect your existing cluster if you upgrade it from v3.0.7 or earlier to v3.0.8 or later. In other words, **only newly created clusters default to using the pessimistic transaction mode**. ## Principles of optimistic transactions diff --git a/pessimistic-transaction.md b/pessimistic-transaction.md index c457ba2228119..3e8a04b3fbf2d 100644 --- a/pessimistic-transaction.md +++ b/pessimistic-transaction.md @@ -1,15 +1,15 @@ --- -title: TiDB Pessimistic Transaction Model -summary: Learn the pessimistic transaction model in TiDB. +title: TiDB Pessimistic Transaction Mode +summary: Learn the pessimistic transaction mode in TiDB. --- -# TiDB Pessimistic Transaction Model +# TiDB Pessimistic Transaction Mode -To make the usage of TiDB closer to traditional databases and reduce the cost of migration, starting from v3.0, TiDB supports the pessimistic transaction model on top of the optimistic transaction model. This document describes the features of the TiDB pessimistic transaction model. +To make the usage of TiDB closer to traditional databases and reduce the cost of migration, starting from v3.0, TiDB supports the pessimistic transaction mode on top of the optimistic transaction model. This document describes the features of the TiDB pessimistic transaction mode. > **Note:** > -> Starting from v3.0.8, newly created TiDB clusters use the pessimistic transaction model by default. However, this does not affect your existing cluster if you upgrade it from v3.0.7 or earlier to v3.0.8 or later. In other words, **only newly created clusters default to using the pessimistic transaction model**. +> Starting from v3.0.8, newly created TiDB clusters use the pessimistic transaction mode by default. However, this does not affect your existing cluster if you upgrade it from v3.0.7 or earlier to v3.0.8 or later. In other words, **only newly created clusters default to using the pessimistic transaction mode**. ## Switch transaction mode diff --git a/releases/release-3.0-ga.md b/releases/release-3.0-ga.md index 09f285106e173..12fd644d4d837 100644 --- a/releases/release-3.0-ga.md +++ b/releases/release-3.0-ga.md @@ -78,7 +78,7 @@ On June 28, 2019, TiDB 3.0 GA is released. The corresponding TiDB Ansible versio - Add a feature to use `SHARD_ROW_ID_BITS` to scatter row IDs when the column contains an AUTO_INCREMENT attribute to relieve hotspot issues - Optimize the lifetime of invalid DDL metadata to speed up recovering the normal execution of DDL operations after upgrading the TiDB cluster + Transactions - - Support the pessimistic transaction model (**Experimental**) + - Support the pessimistic transaction mode (**Experimental**) - Optimize transaction processing logics to adapt to more scenarios: - Change the default value `tidb_disable_txn_auto_retry` to `on`, which means non-auto committed transactions will not be retried - Add the `tidb_batch_commit` system variable to split a transaction into multiple ones to be executed concurrently @@ -152,7 +152,7 @@ On June 28, 2019, TiDB 3.0 GA is released. The corresponding TiDB Ansible versio + Support batch receiving and sending Raft messages, improving TPS by 7% for write intensive scenarios + Support checking RocksDB Level 0 files before applying snapshots to avoid write stall + Introduce Titan, a key-value plugin that improves write performance for scenarios with value sizes greater than 1KiB, and relieves write amplification in certain degrees -+ Support the pessimistic transaction model (**Experimental**) ++ Support the pessimistic transaction mode (**Experimental**) + Support getting monitoring information via HTTP + Modify the semantics of `Insert` to allow Prewrite to succeed only when there is no Key + Develop a unified log format specification with restructured log system to facilitate collection and analysis by tools diff --git a/releases/release-3.0.0-rc.2.md b/releases/release-3.0.0-rc.2.md index 2af344325457a..c69fd7009f6dc 100644 --- a/releases/release-3.0.0-rc.2.md +++ b/releases/release-3.0.0-rc.2.md @@ -39,7 +39,7 @@ On May 28, 2019, TiDB 3.0.0-rc.2 is released. The corresponding TiDB Ansible ver - Add the `tidb_back_off_weight` variable to control the backoff time [#10266](https://github.com/pingcap/tidb/pull/10266) - Prohibit TiDB retrying non-automatically committed transactions in default conditions by setting the default value of `tidb_disable_txn_auto_retry` to `on` [#10266](https://github.com/pingcap/tidb/pull/10266) - Fix the database privilege judgment of `role` in `RBAC` [#10261](https://github.com/pingcap/tidb/pull/10261) - - Support the pessimistic transaction model (experimental) [#10297](https://github.com/pingcap/tidb/pull/10297) + - Support the pessimistic transaction mode (experimental) [#10297](https://github.com/pingcap/tidb/pull/10297) - Reduce the wait time for handling lock conflicts in some cases [#10006](https://github.com/pingcap/tidb/pull/10006) - Make the Region cache able to visit follower nodes when a fault occurs in the leader node [#10256](https://github.com/pingcap/tidb/pull/10256) - Add the `tidb_low_resolution_tso` variable to control the number of TSOs obtained in batches and reduce the times of transactions obtaining TSO to adapt for scenarios where data consistency is not so strictly required [#10428](https://github.com/pingcap/tidb/pull/10428) diff --git a/releases/release-3.0.8.md b/releases/release-3.0.8.md index 8adfd628a50b5..5004e1583c26d 100644 --- a/releases/release-3.0.8.md +++ b/releases/release-3.0.8.md @@ -64,7 +64,7 @@ TiDB Ansible version: 3.0.8 + Transaction - Update the default value of the `tidb_txn_mode` variable from `""` to `"pessimistic"` when a new cluster is created [#14171](https://github.com/pingcap/tidb/pull/14171) - Fix the issue that the lock waiting time is too long for a pessimistic transaction because the lock waiting time for a single statement is not reset when a transaction is retried [#13990](https://github.com/pingcap/tidb/pull/13990) - - Fix the issue that wrong data might be read because unmodified data is unlocked for the pessimistic transaction model [#14050](https://github.com/pingcap/tidb/pull/14050) + - Fix the issue that wrong data might be read because unmodified data is unlocked for the pessimistic transaction mode [#14050](https://github.com/pingcap/tidb/pull/14050) - Fix repeated insert value restriction checks because transaction types are not distinguished when prewrite is performed in mocktikv [#14175](https://github.com/pingcap/tidb/pull/14175) - Fix the panic because transactions are not correctly handled when `session.TxnState` is `Invalid` [#13988](https://github.com/pingcap/tidb/pull/13988) - Fix the issue that the `ErrConfclit` structure in mocktikv does not contain `ConflictCommitTS` [#14080](https://github.com/pingcap/tidb/pull/14080) diff --git a/sql-statements/sql-statement-begin.md b/sql-statements/sql-statement-begin.md index c1ba8cc1a728e..ad78ec7921e0c 100644 --- a/sql-statements/sql-statement-begin.md +++ b/sql-statements/sql-statement-begin.md @@ -43,4 +43,4 @@ TiDB supports the syntax extension of `BEGIN PESSIMISTIC` or `BEGIN OPTIMISTIC`. * [ROLLBACK](/sql-statements/sql-statement-rollback.md) * [START TRANSACTION](/sql-statements/sql-statement-start-transaction.md) * [TiDB optimistic transaction model](/optimistic-transaction.md) -* [TiDB pessimistic transaction model](/pessimistic-transaction.md) \ No newline at end of file +* [TiDB pessimistic transaction mode](/pessimistic-transaction.md) \ No newline at end of file diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index 1226f8f8e6e76..bea4465f4c0be 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -104,7 +104,7 @@ TableList ::= |`ORDER BY` | The `ORDER BY` clause is used to sort the data in ascending or descending order, based on columns, expressions or items in the `select_expr` list.| |`LIMIT` | The `LIMIT` clause can be used to constrain the number of rows. `LIMIT` takes one or two numeric arguments. With one argument, the argument specifies the maximum number of rows to return, the first row to return is the first row of the table by default; with two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. TiDB also supports the `FETCH FIRST/NEXT n ROW/ROWS ONLY` syntax, which has the same effect as `LIMIT n`. You can omit `n` in this syntax and its effect is the same as `LIMIT 1`. | |`Window window_definition`| This is the syntax for window function, which is usually used to do some analytical computation. For more information, refer to [Window Function](/functions-and-operators/window-functions.md). | -| `FOR UPDATE` | The `SELECT FOR UPDATE` clause locks all the data in the result sets to detect concurrent updates from other transactions. Data that match the query conditions but do not exist in the result sets are not read-locked, such as the row data written by other transactions after the current transaction is started. TiDB uses the [Optimistic Transaction Model](/optimistic-transaction.md). The transaction conflicts are not detected in the statement execution phase. Therefore, the current transaction does not block other transactions from executing `UPDATE`, `DELETE` or `SELECT FOR UPDATE` like other databases such as PostgreSQL. In the committing phase, the rows read by `SELECT FOR UPDATE` are committed in two phases, which means they can also join the conflict detection. If write conflicts occur, the commit fails for all transactions that include the `SELECT FOR UPDATE` clause. If no conflict is detected, the commit succeeds. And a new version is generated for the locked rows, so that write conflicts can be detected when other uncommitted transactions are being committed later. When using pessimistic transaction model, the behavior is basically the same as other databases. Refer to [Difference with MySQL InnoDB](/pessimistic-transaction.md#difference-with-mysql-innodb) to see the details. TiDB supports the `NOWAIT` modifier for `FOR UPDATE`. See [TiDB Pessimistic Transaction Model](/pessimistic-transaction.md) for details. | +| `FOR UPDATE` | The `SELECT FOR UPDATE` clause locks all the data in the result sets to detect concurrent updates from other transactions. Data that match the query conditions but do not exist in the result sets are not read-locked, such as the row data written by other transactions after the current transaction is started. TiDB uses the [Optimistic Transaction Model](/optimistic-transaction.md). The transaction conflicts are not detected in the statement execution phase. Therefore, the current transaction does not block other transactions from executing `UPDATE`, `DELETE` or `SELECT FOR UPDATE` like other databases such as PostgreSQL. In the committing phase, the rows read by `SELECT FOR UPDATE` are committed in two phases, which means they can also join the conflict detection. If write conflicts occur, the commit fails for all transactions that include the `SELECT FOR UPDATE` clause. If no conflict is detected, the commit succeeds. And a new version is generated for the locked rows, so that write conflicts can be detected when other uncommitted transactions are being committed later. When using pessimistic transaction mode, the behavior is basically the same as other databases. Refer to [Difference with MySQL InnoDB](/pessimistic-transaction.md#difference-with-mysql-innodb) to see the details. TiDB supports the `NOWAIT` modifier for `FOR UPDATE`. See [TiDB Pessimistic Transaction Mode](/pessimistic-transaction.md) for details. | |`LOCK IN SHARE MODE` | To guarantee compatibility, TiDB parses these three modifiers, but will ignore them. | ## Examples diff --git a/system-variables.md b/system-variables.md index d35a27a68d79d..701fdb15612c6 100644 --- a/system-variables.md +++ b/system-variables.md @@ -1014,7 +1014,7 @@ For a system upgraded to v5.0 from an earlier version, if you have not modified - `user`: The current session user. - `schemaVersion`: The current schema version. - `txnStartTS`: The timestamp at which the current transaction starts. - - `forUpdateTS`: In the pessimistic transactional model, `forUpdateTS` is the current timestamp of the SQL statement. When a write conflict occurs in the pessimistic transaction, TiDB retries the SQL statement currently being executed and updates this timestamp. You can configure the number of retries via [`max-retry-count`](/tidb-configuration-file.md#max-retry-count). In the optimistic transactional model, `forUpdateTS` is equivalent to `txnStartTS`. + - `forUpdateTS`: In the pessimistic transactional mode, `forUpdateTS` is the current timestamp of the SQL statement. When a write conflict occurs in the pessimistic transaction, TiDB retries the SQL statement currently being executed and updates this timestamp. You can configure the number of retries via [`max-retry-count`](/tidb-configuration-file.md#max-retry-count). In the optimistic transactional model, `forUpdateTS` is equivalent to `txnStartTS`. - `isReadConsistency`: Indicates whether the current transactional isolation level is Read Committed (RC). - `current_db`: The name of the current database. - `txn_mode`: The transactional mode. Value options are `OPTIMISTIC` and `PESSIMISTIC`. diff --git a/transaction-isolation-levels.md b/transaction-isolation-levels.md index 8f01218665ea2..7ce643a86d3b5 100644 --- a/transaction-isolation-levels.md +++ b/transaction-isolation-levels.md @@ -22,7 +22,7 @@ TiDB implements Snapshot Isolation (SI) consistency, which it advertises as `REP > > In TiDB v3.0, the automatic retry of transactions is disabled by default. It is not recommended to enable the automatic retry because it might **break the transaction isolation level**. Refer to [Transaction Retry](/optimistic-transaction.md#automatic-retry) for details. > -> Starting from TiDB [v3.0.8](/releases/release-3.0.8.md#tidb), newly created TiDB clusters use the [pessimistic transaction model](/pessimistic-transaction.md) by default. The current read (`for update` read) is **non-repeatable read**. Refer to [pessimistic transaction mode](/pessimistic-transaction.md) for details. +> Starting from TiDB [v3.0.8](/releases/release-3.0.8.md#tidb), newly created TiDB clusters use the [pessimistic transaction mode](/pessimistic-transaction.md) by default. The current read (`for update` read) is **non-repeatable read**. Refer to [pessimistic transaction mode](/pessimistic-transaction.md) for details. ## Repeatable Read isolation level diff --git a/transaction-overview.md b/transaction-overview.md index 75ffeaa891c2c..8a59b98431895 100644 --- a/transaction-overview.md +++ b/transaction-overview.md @@ -5,7 +5,7 @@ summary: Learn transactions in TiDB. # Transactions -TiDB supports distributed transactions using either [pessimistic](/pessimistic-transaction.md) or [optimistic](/optimistic-transaction.md) transaction models. Starting from TiDB 3.0.8, TiDB uses the pessimistic transaction model by default. +TiDB supports distributed transactions using either [pessimistic](/pessimistic-transaction.md) or [optimistic](/optimistic-transaction.md) transaction mode. Starting from TiDB 3.0.8, TiDB uses the pessimistic transaction mode by default. This document introduces commonly used transaction-related statements, explicit and implicit transactions, isolation levels, lazy check for constraints, and transaction sizes. diff --git a/troubleshoot-write-conflicts.md b/troubleshoot-write-conflicts.md index adea5ee563210..a6f7f58de2ee5 100644 --- a/troubleshoot-write-conflicts.md +++ b/troubleshoot-write-conflicts.md @@ -89,4 +89,4 @@ You can use `indexID` and the table name to find the name of the related index: SELECT * FROM INFORMATION_SCHEMA.TIDB_INDEXES WHERE TABLE_SCHEMA='{db_name}' AND TABLE_NAME='{table_name}' AND INDEX_ID={indexID}; ``` -In addition, in TiDB v3.0.8 and later versions, the pessimistic transaction becomes the default model. The pessimistic transaction model can avoid write conflicts during the transaction prewrite stage, so you do not need to modify the application any more. In the pessimistic transaction mode, each DML statement writes a pessimistic lock to the related keys during execution. This pessimistic lock can prevent other transactions from modifying the same keys, thus ensuring no write conflicts exist in the `prewrite` stage of the transaction 2PC. +In addition, in TiDB v3.0.8 and later versions, the pessimistic transaction becomes the default mode. The pessimistic transaction mode can avoid write conflicts during the transaction prewrite stage, so you do not need to modify the application any more. In the pessimistic transaction mode, each DML statement writes a pessimistic lock to the related keys during execution. This pessimistic lock can prevent other transactions from modifying the same keys, thus ensuring no write conflicts exist in the `prewrite` stage of the transaction 2PC. diff --git a/whats-new-in-tidb-4.0.md b/whats-new-in-tidb-4.0.md index 47bc49c7be554..ec6e4a7ef5a2c 100644 --- a/whats-new-in-tidb-4.0.md +++ b/whats-new-in-tidb-4.0.md @@ -39,7 +39,7 @@ TiUP is a new package manager tool introduced in v4.0 that is used to manage all ## Transaction -- The pessimistic transaction is now provided for general availability as the default transaction mode. Support the Read Committed isolation level and the `SELECT FOR UPDATE NOWAIT` syntax. See [Pessimistic Transaction Model](/pessimistic-transaction.md) for details. +- The pessimistic transaction is now provided for general availability as the default transaction mode. Support the Read Committed isolation level and the `SELECT FOR UPDATE NOWAIT` syntax. See [Pessimistic Transaction Mode](/pessimistic-transaction.md) for details. - Support large transactions. Increase the upper limit on transaction size from 10 MB to 10 GB. Support both the pessimistic transaction and optimistic transaction. See [Transaction size limit](/transaction-overview.md#transaction-size-limit) for details. ## SQL features