Tidb: How does flashback works

Created on 20 Oct 2020  路  2Comments  路  Source: pingcap/tidb

General Question

I'v read the document of FLASHBACK and had a test.

MySQL [sg]> create table t(id int);                                                                                                                                                            [15/1806]
Query OK, 0 rows affected (0.10 sec)

MySQL [sg]> insert into t values(1);
Query OK, 1 row affected (0.03 sec)

MySQL [sg]> select * from t;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

MySQL [sg]> truncate table t;
Query OK, 0 rows affected (0.13 sec)

MySQL [sg]> flashback table t to t1;
Query OK, 0 rows affected (1.17 sec)

MySQL [sg]> select * from t1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

MySQL [sg]> insert into t values(2);
Query OK, 1 row affected (0.01 sec)

MySQL [sg]> select * from t;
+------+
| id   |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

MySQL [sg]> truncate table t;
Query OK, 0 rows affected (0.12 sec)

MySQL [sg]> flashback table t to t2;
Query OK, 0 rows affected (1.19 sec)

MySQL [sg]> select * from t2;
+------+
| id   |
+------+
|    2 |
+------+
1 row in set (0.01 sec)

MySQL [sg]> admin show ddl jobs where db_name='sg';
+--------+---------+------------+----------------+--------------+-----------+----------+-----------+---------------------+---------------------+--------+
| JOB_ID | DB_NAME | TABLE_NAME | JOB_TYPE       | SCHEMA_STATE | SCHEMA_ID | TABLE_ID | ROW_COUNT | START_TIME          | END_TIME            | STATE  |
+--------+---------+------------+----------------+--------------+-----------+----------+-----------+---------------------+---------------------+--------+
|     85 | sg      | t2         | recover table  | public       |        76 |       80 |         0 | 2020-10-20 10:54:01 | 2020-10-20 10:54:02 | synced |
|     84 | sg      | t          | truncate table | public       |        76 |       80 |         0 | 2020-10-20 10:53:47 | 2020-10-20 10:53:47 | synced |
|     82 | sg      | t1         | recover table  | public       |        76 |       78 |         0 | 2020-10-20 10:53:22 | 2020-10-20 10:53:23 | synced |
|     81 | sg      | t          | truncate table | public       |        76 |       78 |         0 | 2020-10-20 10:52:56 | 2020-10-20 10:52:56 | synced |
|     79 | sg      | t          | create table   | public       |        76 |       78 |         0 | 2020-10-20 10:52:18 | 2020-10-20 10:52:18 | synced |
|     77 | sg      |            | create schema  | public       |        76 |        0 |         0 | 2020-10-20 10:52:10 | 2020-10-20 10:52:10 | synced |
+--------+---------+------------+----------------+--------------+-----------+----------+-----------+---------------------+---------------------+--------+
6 rows in set (0.01 sec)

Then I have some questions:

  1. The document says can't use flashback statement to restore the same deleted table multiple times, but it seems works fine in my test case.
  2. I found the table_id of t changed through admin show ddj jobs, why the table id will change?
typquestion

Most helpful comment

  1. If you execute flashback 2 times without any insertion between them, you may get the error:
"Table 't' already been flashback to 't1', can't be flashback repeatedly"
  1. I think flashback table t to t1 can be understood as follows:

    • The old "dropped" table t is renamed to t1.

    • A new table named t is newly created. When TiDB creates a new table, it also allocates a new table_id.

All 2 comments

  1. If you execute flashback 2 times without any insertion between them, you may get the error:
"Table 't' already been flashback to 't1', can't be flashback repeatedly"
  1. I think flashback table t to t1 can be understood as follows:

    • The old "dropped" table t is renamed to t1.

    • A new table named t is newly created. When TiDB creates a new table, it also allocates a new table_id.

@tangenta got it, thanks for your reply.

Was this page helpful?
0 / 5 - 0 ratings