云数据库 GAUSSDB-修复账本数据库:恢复用户表和用户历史表名称

时间:2024-11-13 10:05:07

恢复用户表和用户历史表名称

已通过enable_recyclebin参数和recyclebin_retention_time参数开启闪回DROP功能,恢复用户表和用户历史表名称。示例如下:

  • DROP用户表,对用户表执行闪回DROP。使用ledger_hist_repair对用户表、用户历史表进行表名恢复。
    -- 对用户表执行闪回drop,使用ledger_hist_repair对用户历史表进行表名恢复。
    gaussdb=# CREATE TABLE ledgernsp.tab2(a int, b text);
    CREATE TABLE
    gaussdb=# DROP TABLE ledgernsp.tab2;
    DROP TABLE
    gaussdb=# SELECT rcyrelid, rcyname, rcyoriginname FROM gs_recyclebin;
     rcyrelid |           rcyname            |    rcyoriginname
    ----------+------------------------------+---------------------
        16717 | BIN$38242338414D$42EB978==$0 | tab2
        16725 | BIN$382423384155$42EC678==$0 | gs_hist_tab2_index
        16722 | BIN$382423384152$42ECC30==$0 | ledgernsp_tab2_hist
        16720 | BIN$382423384150$42ED3E0==$0 | pg_toast_16717
    (4 rows)
    -- 对用户表执行闪回drop。
    gaussdb=# TIMECAPSULE TABLE ledgernsp.tab2 TO BEFORE DROP;
    TimeCapsule Table
    -- 使用ledger_hist_repair恢复用户历史表表名。
    gaussdb=# SELECT ledger_hist_repair('ledgernsp', 'tab2');
     ledger_hist_repair
    --------------------
     0000000000000000
    (1 row)
    gaussdb=# TIMECAPSULE TABLE ledgernsp.tab2 TO BEFORE DROP;
    TimeCapsule Table
    gaussdb=# SELECT ledger_hist_repair('ledgernsp', 'tab2');
     ledger_hist_repair
    --------------------
     0000000000000000
    (1 row)
    gaussdb=#  \d+ ledgernsp.tab2;
                              Table "ledgernsp.tab2"
       Column    |  Type   | Modifiers | Storage  | Stats target | Description
    -------------+---------+-----------+----------+--------------+-------------
     a           | integer |           | plain    |              |
     b           | text    |           | extended |              |
     hash_1d2d14 | hash16  |           | plain    |              |
    Has OIDs: no
    Options: orientation=row, compression=no, storage_type=USTORE, segment=off, toast.storage_type=USTORE, toast.toast_storage_type=enhanced_toast
    History table name: ledgernsp_tab2_hist
    
    -- 对用户表执行闪回drop,使用ledger_hist_repair对用户表进行表名恢复。
    gaussdb=# CREATE TABLE ledgernsp.tab3(a int, b text);
    CREATE TABLE
    gaussdb=# DROP TABLE ledgernsp.tab3;
    DROP TABLE
    gaussdb=# SELECT rcyrelid, rcyname, rcyoriginname FROM gs_recyclebin;
     rcyrelid |           rcyname            |    rcyoriginname
    ----------+------------------------------+---------------------
        17574 | BIN$44A4233844A6$B18E7A0==$0 | tab3
        17582 | BIN$44A4233844AE$B18F488==$0 | gs_hist_tab3_index
        17579 | BIN$44A4233844AB$B18FA40==$0 | ledgernsp_tab3_hist
        17577 | BIN$44A4233844A9$B190208==$0 | pg_toast_17574
    (4 rows)
    -- 对用户历史表执行闪回drop。
    gaussdb=# TIMECAPSULE TABLE blockchain.ledgernsp_tab3_hist TO BEFORE DROP;
    TimeCapsule Table
    -- 拿到回收站中用户表对应的rcyname,使用ledger_hist_repair恢复用户表表名。
    gaussdb=# SELECT ledger_hist_repair('ledgernsp', 'BIN$44A4233844A6$B18E7A0==$0');
     ledger_hist_repair
    --------------------
     0000000000000000
    (1 row)
    
    gaussdb=# \d+ ledgernsp.tab3;
                              Table "ledgernsp.tab3"
       Column    |  Type   | Modifiers | Storage  | Stats target | Description
    -------------+---------+-----------+----------+--------------+-------------
     a           | integer |           | plain    |              |
     b           | text    |           | extended |              |
     hash_7a0c87 | hash16  |           | plain    |              |
    Has OIDs: no
    Options: orientation=row, compression=no, storage_type=USTORE, segment=off, toast.storage_type=USTORE, toast.toast_storage_type=enhanced_toast
    History table name: ledgernsp_tab3_hist
    
    -- 删除表。
    gaussdb=# DROP TABLE ledgernsp.tab2 PURGE;
    DROP TABLE
    gaussdb=# DROP TABLE ledgernsp.tab3 PURGE;
    DROP TABLE
support.huaweicloud.com/fg-gaussdb-cent-v8/gaussdb-48-0031.html