diff --git a/as-of-timestamp.md b/as-of-timestamp.md index ce63be0ff055e..4f2c82293757d 100644 --- a/as-of-timestamp.md +++ b/as-of-timestamp.md @@ -7,15 +7,6 @@ summary: Learn how to read historical data using the `AS OF TIMESTAMP` statement This document describes how to perform the [Stale Read](/stale-read.md) feature using the `AS OF TIMESTAMP` clause to read historical data in TiDB, including specific usage examples and strategies for saving historical data. -> **Warning:** -> -> Currently, you cannot use Stale Read together with TiFlash. If your SQL query contains the `AS OF TIMESTAMP` clause and TiDB might read data from TiFlash replicas, you might encounter an error with a message like `ERROR 1105 (HY000): stale requests require tikv backend`. -> -> To fix the problem, disable TiFlash replicas for your Stale Read query. To do that, perform one of the following operations: -> -> - Use the `set session tidb_isolation_read_engines='tidb,tikv'` variable. -> - Use the [hint](/optimizer-hints.md#read_from_storagetiflasht1_name--tl_name--tikvt2_name--tl_name-) to enforce TiDB to read data from TiKV. - TiDB supports reading historical data through a standard SQL interface, which is the `AS OF TIMESTAMP` SQL clause, without the need for special clients or drivers. After data is updated or deleted, you can read the historical data before the update or deletion using this SQL interface. > **Note:** diff --git a/develop/dev-guide-use-stale-read.md b/develop/dev-guide-use-stale-read.md index 28eea22369e09..a7ac8868174b3 100644 --- a/develop/dev-guide-use-stale-read.md +++ b/develop/dev-guide-use-stale-read.md @@ -457,7 +457,7 @@ Enable Stale Read in a session: SET @@tidb_read_staleness="-5"; ``` -For example, if the value is set to `-5` and TiKV has the corresponding historical data, TiDB selects a timestamp as new as possible within a 5-second time range. +For example, if the value is set to `-5` and TiKV or TiFlash has the corresponding historical data, TiDB selects a timestamp as new as possible within a 5-second time range. Disable Stale Read in the session: diff --git a/stale-read.md b/stale-read.md index 60b0a8c35afe3..b11b0b959a144 100644 --- a/stale-read.md +++ b/stale-read.md @@ -36,3 +36,35 @@ TiDB provides the methods of performing Stale Read at the statement level, the s - Specifying a time range: In a session, if you need TiDB to read the data as new as possible within a time range in subsequent queries without violating the isolation level, you can specify the time range by setting the `tidb_read_staleness` system variable. For detailed usage, refer to [`tidb_read_staleness`](/tidb-read-staleness.md). Besides, TiDB provides a way to specify an exact point in time by setting the [`tidb_external_ts`](/system-variables.md#tidb_external_ts-new-in-v640) system variable on session or global level. For detailed usage, refer to [Perform Stale Read Using `tidb_external_ts`](/tidb-external-ts.md). + +## Restrictions + +When a Stale Read query for a table is pushed down to TiFlash, the query will return an error if this table has newer DDL operations executed after the read timestamp specified by the query. This is because TiFlash only supports reading data from the tables with the latest schemas. + +Take the following table as an example: + +```sql +create table t1(id int); +alter table t1 set tiflash replica 1; +``` + +Execute the following DDL operation after one minute: + +```sql +alter table t1 add column c1 int not null; +``` + +Then, use Stale Read to query the data from one minute ago: + +```sql +set @@session.tidb_enforce_mpp=1; +select * from t1 as of timestamp NOW() - INTERVAL 1 minute; +``` + +TiFlash will report an error as follows: + +``` +ERROR 1105 (HY000): other error for mpp stream: From MPP,task_id:1>: Code: 0, e.displayText() = DB::TiFlashException: Table 323 schema version 104 newer than query schema version 100, e.what() = DB::TiFlashException, +``` + +To avoid this error, you can change the read timestamp specified by Stale Read to the time after the DDL operation. \ No newline at end of file diff --git a/tidb-external-ts.md b/tidb-external-ts.md index a66058b09179b..104422ad16f14 100644 --- a/tidb-external-ts.md +++ b/tidb-external-ts.md @@ -7,15 +7,6 @@ summary: Learn how to read historical data using the `tidb_external_ts` variable To support reading the historical data, TiDB v6.4.0 introduces a system variable [`tidb_external_ts`](/system-variables.md#tidb_external_ts-new-in-v640). This document describes how to read historical data through this system variable, including detailed usage examples. -> **Warning:** -> -> Currently, you cannot use Stale Read together with TiFlash. If you enable [`tidb_enable_external_ts_read`](/system-variables.md#tidb_enable_external_ts_read-new-in-v640) in a query and TiDB might read data from TiFlash replicas, you might encounter the error message `ERROR 1105 (HY000): stale requests require tikv backend`. -> -> To fix this problem, disable TiFlash replicas for your Stale Read query by performing one of the following operations: -> -> - Use the `tidb_isolation_read_engines` variable to disable TiFlash replicas: `SET SESSION tidb_isolation_read_engines='tidb,tikv'`. -> - Use the [READ_FROM_STORAGE](/optimizer-hints.md#read_from_storagetiflasht1_name--tl_name--tikvt2_name--tl_name-) hint to enforce TiDB to read data from TiKV. - ## Scenarios Read historical data from a specified point in time is very useful for data replication tools such as TiCDC. After the data replication tool completes the data replication before a certain point in time, you can set the `tidb_external_ts` system variable of the downstream TiDB to read the data before that point in time. This prevents the data inconsistency caused by data replication.