Skip to content

Commit

Permalink
Update stale_read doc for the support of TiFlash (#12369)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiancai authored Feb 10, 2023
1 parent 8931f7c commit 9a44af1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
9 changes: 0 additions & 9 deletions as-of-timestamp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
2 changes: 1 addition & 1 deletion develop/dev-guide-use-stale-read.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
32 changes: 32 additions & 0 deletions stale-read.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<query:<query_ts:1673950975508472943, local_query_id:18, server_id:111947, start_ts:438816196526080000>,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.
9 changes: 0 additions & 9 deletions tidb-external-ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9a44af1

Please sign in to comment.