暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL学习随笔22 利用工具恢复误删除数据2之pg_dirtyread

458

可用工具

pg_recovery 使用简单,默认只有待找回数据;

pg_dirtyread 使用简单,默认返回全部数据;

WalMiner 需要对 walminer 全面掌握,并做好系统预设;

pg_resetwal 需要了解的内容较多;

pg_filedump 需要单独写一些脚本或工具来配合使用;

pageinspect 难度极大。


若无任何准备,如何恢复数据?推荐以下方法:

及时设置 vacuum_defer_cleanup_age

安装 pg_recover 或者 pg_dirtyread

无法安装插件可以采用 pg_resetwal ,无需任何额外工具

上科技

    编译安装
    下载路径 https://github.com/df7cb/pg_dirtyread
    [root@pgexp1 pg_dirtyread-2.4]# make PG_CONFIG=/opt/pgsql/bin/pg_config
    gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I. -I./ -I/opt/pgsql/include/server -I/opt/pgsql/include/internal -D_GNU_SOURCE -c -o pg_dirtyread.o pg_dirtyread.c
    gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I. -I./ -I/opt/pgsql/include/server -I/opt/pgsql/include/internal -D_GNU_SOURCE -c -o dirtyread_tupconvert.o dirtyread_tupconvert.c
    gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -shared -o pg_dirtyread.so pg_dirtyread.o dirtyread_tupconvert.o -L/opt/pgsql/lib -Wl,--as-needed -Wl,-rpath,'/opt/pgsql/lib',--enable-new-dtags


    [root@pgexp1 pg_dirtyread-2.4]# make install PG_CONFIG=/opt/pgsql/bin/pg_config
    gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -shared -o pg_dirtyread.so pg_dirtyread.o dirtyread_tupconvert.o -L/opt/pgsql/lib -Wl,--as-needed -Wl,-rpath,'/opt/pgsql/lib',--enable-new-dtags
    /usr/bin/mkdir -p '/opt/pgsql/lib'
    /usr/bin/mkdir -p '/opt/pgsql/share/extension'
    /usr/bin/mkdir -p '/opt/pgsql/share/extension'
    /usr/bin/install -c -m 755 pg_dirtyread.so '/opt/pgsql/lib/pg_dirtyread.so'
    /usr/bin/install -c -m 644 .//pg_dirtyread.control '/opt/pgsql/share/extension/'
    /usr/bin/install -c -m 644 .//pg_dirtyread--1.0.sql .//pg_dirtyread--1.0--2.sql .//pg_dirtyread--2.sql '/opt/pgsql/share/extension/'


    复制

    上狠活

      加载和使用
      一旦构建并安装了 pg_dirtyread,您就可以将它添加到数据库中。加载 pg_dirtyread 就像以超级用户身份连接到数据库并运行一样简单。
      该pg_dirtyread()函数返回 RECORD,因此有必要附加一个描述表模式的表别名子句。列按名称匹配,因此可以在别名中省略某些列,或重新排列列。


      使用示例
      postgres=# CREATE EXTENSION pg_dirtyread;
      CREATE EXTENSION
      postgres=# \dx
      List of installed extensions
      Name | Version | Schema | Description
      --------------+---------+------------+------------------------------------------
      pg_dirtyread | 2 | public | Read dead but unvacuumed rows from table
      plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
      (2 rows)


      postgres=# -- Create table and disable autovacuum
      postgres=# CREATE TABLE test (bar bigint, baz text);
      CREATE TABLE
      postgres=# ALTER TABLE test SET (autovacuum_enabled = false, toast.autovacuum_enabled = false);
      ALTER TABLE
      postgres=# INSERT INTO test VALUES (1, 'Test'), (2, 'New Test');
      INSERT 0 2
      postgres=# DELETE FROM test WHERE bar = 1;
      DELETE 1
      postgres=# SELECT * FROM pg_dirtyread('test') as t(bar bigint, baz text);
      bar | baz
      -----+----------
      1 | Test
      2 | New Test
      (2 rows)
      复制

      文章转载自CP的PostgreSQL厨房,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论