云数据库 GAUSSDB-TIMECAPSULE TABLE:示例

时间:2024-09-06 16:59:37

示例

  • 闪回到某个时间点

    需要设置undo_retention_time参数,用于设置旧版本undo的保留时间。

    参数使用请联系管理员处理
    --建表并插入数据。
    gaussdb=# CREATE TABLE tbl_test(c1 int, c2 int);
    gaussdb=# INSERT INTO tbl_test VALUES (1,1),(2,2),(3,3);
    
    --查询当前时间和全局所有节点上的next_csn。
    gaussdb=# SELECT now();
                  now              
    -------------------------------
     2023-11-27 17:06:34.840698+08
    (1 row)
    
    gaussdb=# SELECT int8in(xidout(next_csn)) FROM gs_get_next_xid_csn();
     int8in 
    --------
      25391
    (6 row)
    
    --修改数据。
    gaussdb=# UPDATE tbl_test SET c1=111, c2=222 WHERE c1=1;
    
    --查询数据。
    gaussdb=# SELECT * FROM tbl_test;
     c1  | c2  
    -----+-----
     111 | 222
       2 |   2
       3 |   3
    (3 rows)
    --将数据闪回至修改之前,需要按照实际情况替换时间
    gaussdb=# TIMECAPSULE table tbl_test TO TIMESTAMP to_timestamp('2023-11-27 17:06:34.840698','YYYY-MM-DD HH24:MI:SS.FF');
    
    --也可使使用如下SQL。
    gaussdb=# TIMECAPSULE table tbl_test TO  CS N 25391;
    gaussdb=# SELECT * FROM tbl_test;
     c1 | c2 
    ----+----
      2 |  2
      3 |  3
      1 |  1
    (3 rows)
    
    --删除。
    gaussdb=# DROP TABLE tbl_test;
  • 从回收站中闪回数据

    前提条件:

    • 开启enable_recyclebin参数,启用回收站,参数使用请联系管理员处理
    • recyclebin_retention_time参数用于设置回收站对象保留时间,超过该时间的回收站对象将被自动清理,参数使用请联系管理员处理
    --建表并插入数据。
    gaussdb=# CREATE TABLE tbl_test1(c1 int, c2 varchar(10));
    gaussdb=# INSERT INTO tbl_test1 VALUES (1,'AAA'),(2,'BBB');
    
    --删除表。
    gaussdb=# DROP TABLE tbl_test1;
    
    --闪回至表删除之前。
    gaussdb=# TIMECAPSULE TABLE tbl_test1 TO BEFORE DROP;
    
    --查询数据。
    gaussdb=# SELECT * FROM tbl_test1;
     c1 | c2  
    ----+-----
      1 | AAA
      2 | BBB
    (2 rows)
    
    --删除表(添加PURGE参数,级联删除回收站该表数据)。
    gaussdb=# DROP TABLE tbl_test1 PURGE;
support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-0718.html