ClickHouse UNDROP TABLE function

Whether you use ClickHouse or other databases, if you delete a table with shaking hands, it is very difficult to restore it. But now ClickHouse supports UNDROP TABLEthe ability, if a table is accidentally deleted due to hand shaking, it can be quickly restored in a short time.

It UNDROP TABLEis still an experimental function, if you want to experience it, you can use the latest v23.3.1.2823-lts branch. Let's experience the whole function below.

Create test table

At present, only some table engines under the Atomic library engine are supported UNDROP TABLE, MergeTree, ReplicatedMergeTree, Distributed, Logetc. are all supported, but temporary tables do not support UNDROP TABLErestoration using . Here you can create a MergeTree table engine under the default default library engine for experience. The table creation statement is as follows:

create table test (id Int32) engine=MergeTree() order by id;

After the table is built, write some test data to facilitate subsequent verification of whether the table is normal after recovery. You can write a few at will:

insert into test values (1),(2),(3),(4),(5);

Verify that the data was written successfully:

select * from test;

┌─id─┐
│  1 │
│  2 │
│  3 │
│  4 │
│  5 │
└────┘

delete table

After creating the table and writing data, execute the delete table operation:

drop table test;

At this point the validation table is deleted:

select * from test;

Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: Table default.test doesn't exist. (UNKNOWN_TABLE)

At this time, if you query the table again, it will prompt that the table does not exist, which proves that the deletion is successful.

The new version provides system.dropped_tablessystem tables to view dropped tables:

select * from system.dropped_tables\G

Row 1:
──────
index:                 0
database:              default
table:                 test
uuid:                  87b7d1d6-f86d-485e-a254-79d9c58c7464
engine:                MergeTree
metadata_dropped_path: /data/ClickHouse/build/programs/data/metadata_dropped/default.test.87b7d1d6-f86d-485e-a254-79d9c58c7464.sql
table_dropped_time:    2023-04-02 12:30:13

This system table is a table that records data that has been deleted but has not been cleaned up, that is, a table that can be restored.

recovery form

Under the default configuration of ClickHouse, the deleted table will start to clear the task after 8 minutes, so if it is accidentally deleted by shaking hands, you can use to UNDROP TABLErestore it.

Instructions

1. UNDROP TABLE {test}

This method will find the default.test table that was deleted recently, because ClickHouse can keep creating and deleting tables with the same name.

2. UNDROP TABLE {test} UUID {uuid}

If you don't want to restore the last deleted table, you can restore it by specifying the table uuid. The specific uuid can be system.dropped_tablesqueried in the system table.

3. UNDROP TABLE {test} ON CLUSTER {cluster}

It doesn't matter if you use the statement when dropping the table ON CLUSTER, it UNDROP TABLEis also supported ON CLUSTER.

recovery test

is currently UNDROP TABLEan experimental feature, so to allow_experimental_undrop_table_queryturn it on:

undrop table test settings allow_experimental_undrop_table_query=1

Query and verify again, the test table has been restored normally.

After the test table is restored, the test table can be deleted again, and can be restored by specifying the uuid:

undrop table test uuid '87b7d1d6-f86d-485e-a254-79d9c58c7464' settings allow_experimental_undrop_table_query=1

Note that the uuid here is queried in my system table and needs to be replaced with my real uuid.

If the default 8 minutes has elapsed after the table is deleted, it cannot be restored, and an error message will be displayed:

undrop table test settings allow_experimental_undrop_table_query=1

Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: The drop task of table default.test is in progress, has been dropped or the database engine doesn't support it. (UNKNOWN_TABLE)

At this point, UNDROP TABLEthe introduction is over, if you are interested in experiencing it.


Welcome to add WeChat: xiedeyantu to discuss technical issues.

Guess you like

Origin blog.csdn.net/weixin_39992480/article/details/129944926