Skip to content

Commit 094f154

Browse files
authored
Merge pull request #9109 from soyeric128/flash-back
docs: flashback
2 parents 40a4db9 + d2f0c84 commit 094f154

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: RESTORE TABLE
3+
---
4+
5+
Restores a table to an earlier version with a snapshot ID or timestamp.
6+
7+
By the snapshot ID or timestamp you specify in the command, Databend restores the table to a prior state where the snapshot was created. To retrieve snapshot IDs and timestamps of a table, use [FUSE_SNAPSHOT](../../../15-sql-functions/111-system-functions/fuse_snapshot.md).
8+
9+
The capability to restore a table is subject to these conditions:
10+
11+
- The command only restores existing tables to their prior states. To recover a dropped table, use [UNDROP TABLE](21-ddl-undrop-table.md).
12+
13+
- Restoring a table is part of Databend's time travel feature. Before using the command, make sure the table you want to restore is eligible for time travel. For example, the command doesn't work for transient tables because Databend does not create or store snapshots for such tables.
14+
15+
- You cannot roll back after restoring a table to a prior state, but you can restore the table again to an earlier state.
16+
17+
- Databend recommends this command for emergency recovery only. To query the history data of a table, use the [AT](../../20-query-syntax/dml-at.md) clause.
18+
19+
## Syntax
20+
21+
```sql
22+
-- Restore with a snapshot ID
23+
ALTER TABLE <table> FLASHBACK TO (SNAPSHOT => '<snapshot-id>');
24+
25+
-- Restore with a snapshot timestamp
26+
ALTER TABLE <table> FLASHBACK TO (TIMESTAMP => '<timestamp>'::TIMESTAMP);
27+
```
28+
29+
## Examples
30+
31+
```sql
32+
CREATE TABLE mytable(a int);
33+
34+
INSERT INTO mytable VALUES(1);
35+
INSERT INTO mytable VALUES(2);
36+
INSERT INTO mytable VALUES(3);
37+
38+
SELECT * FROM mytable;
39+
40+
a|
41+
-+
42+
3|
43+
2|
44+
1|
45+
46+
-- Retrieve snapshot information
47+
SELECT snapshot_id, timestamp FROM FUSE_SNAPSHOT('default','mytable');
48+
49+
snapshot_id |timestamp |
50+
--------------------------------+-----------------------------+
51+
2648bee0e85044f9879e71f1d37f453b|2022-12-06 20:50:53.324485000|
52+
7e5d4f7ebdbc44d08116771193533346|2022-12-06 20:50:23.623331000|
53+
41c94c7ea47a432388770ada0a66b6c0|2022-12-06 20:49:56.125918000|
54+
55+
-- Restore with a snapshot ID
56+
ALTER TABLE mytable FLASHBACK TO (SNAPSHOT => '7e5d4f7ebdbc44d08116771193533346');
57+
58+
SELECT * FROM mytable;
59+
60+
a|
61+
-+
62+
2|
63+
1|
64+
65+
-- Restore with a snapshot timestamp
66+
ALTER TABLE mytable FLASHBACK TO (TIMESTAMP => '2022-12-06 20:49:56.125918000'::TIMESTAMP);
67+
68+
SELECT * FROM mytable;
69+
70+
a|
71+
-+
72+
1|
73+
```

0 commit comments

Comments
 (0)