Postgres数据库truncate表无有效备份恢复

联系:手机/微信(+86 17813235971) QQ(107644445)QQ咨询惜分飞

标题:Postgres数据库truncate表无有效备份恢复

作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]

创建一个Postgres表,并插入数据

postgres=# CREATE TABLE "PeisInterfaceLog"(
postgres(#         "PeisInterfaceLogId" text,
postgres(#         "PeisDepartmentId" text,
postgres(#         "PeisDepartmentName" text,
postgres(#         "PeisInterfaceSubjectType" text,
postgres(#         "PeisInterfaceSubjectId" text,
postgres(#         "PeisInterfaceNo" text,
postgres(#         "PeisInterfaceName" text,
postgres(#         "PeisInterfaceDirection" text,
postgres(#         "PeisInterfaceCallAddress" text,
postgres(#         "PeisInterfaceLogStartTime" timestamp,
postgres(#         "PeisInterfaceInputContent" json,
postgres(#         "PeisInterfaceInputTranscodeContent" json,
postgres(#         "PeisInterfaceOutContent" json,
postgres(#         "PeisInterfaceOutTranscodeContent" json,
postgres(#         "PeisInterfaceSuccessSign" int4,
postgres(#         "PeisInterfaceLogStatusCode" text,
postgres(#         "PeisInterfaceLogNote" text,
postgres(#         "PeisInterfaceLogTimestamp" timestamp,
postgres(#         "PeisInterfaceLogInfo" text,
postgres(#         "PeisPatientRegisterId" text
postgres(# );
CREATE TABLE
postgres=# \i /postgres/COPY/public_copy.sql 
SET
COPY 722957
postgres=# select count(1) from "PeisInterfaceLog";
 count  
--------
 722957
(1 row)

验证truncate操作,引起Postgres中oid的变化

postgres=# checkpoint;
CHECKPOINT
postgres=#  show data_directory;
 data_directory 
----------------
 /pgdata
(1 row)
postgres=# select oid, datname from pg_database ;
  oid  |  datname  
-------+-----------
 13676 | postgres
     1 | template1
 13675 | template0
(3 rows)
postgres=# select relname, relowner, relfilenode from pg_class where relowner = 10 and relname like '%PeisInterfaceLog%';
     relname      | relowner | relfilenode 
------------------+----------+-------------
 PeisInterfaceLog |       10 |       16384
(1 row)

postgres=# truncate table "PeisInterfaceLog";
TRUNCATE TABLE
postgres=# select count(1) from  "PeisInterfaceLog";
 count 
-------
     0
(1 row)

postgres=# select relname, relowner, relfilenode from pg_class where relowner = 10 and relname like '%PeisInterfaceLog%';
     relname      | relowner | relfilenode 
------------------+----------+-------------
 PeisInterfaceLog |       10 |       16394
(1 row)

使用工具进行初始化字典信息

PDU.public=# b;

开始初始化...
 -pg_database:</pgdata/global/1262>

数据库:postgres 
      -pg_schema:</pgdata/base/13676/2615>
      -pg_class:</pgdata/base/13676/1259> 共88行
      -pg_attribute:</pgdata/base/13676/1249> 共2950行
      模式:
        ▌ public 1张表

PDU.public=# use postgres;

┌────────────────────────────────────────┐
│          模式             │  表数量    │
├────────────────────────────────────────┤
│    public                 │  1         │
└────────────────────────────────────────┘
postgres.public=# set public;

┌──────────────────────────────────────────────────┐
│               表名                  │  表大小    │
├──────────────────────────────────────────────────┤
│    PeisInterfaceLog                 │  0         │
└──────────────────────────────────────────────────┘
        仅显示表大小排名前 1 的表名
postgres.public=# \d PeisInterfaceLog;

┌──────────────────────────────────────────────────────────────┐
│                            列类型                            │
└──────────────────────────────────────────────────────────────┘
text,text,text,text,text,text,text,text,text,timestamp,json,json,json,json,int4,text,text,timestamp,text,text

配置软件磁盘扫描操作(pdu.ini中配置)

#dropScan需要扫描的磁盘
DISK_PATH=/data/test.dd
#dropScan时跳跃的数据块数量,数值越小覆盖磁盘越全面,速度越慢
BLOCK_INTERVAL=5

启用磁盘扫描操作

PDU.public=# p idxmode off;

┌─────────────────────────────────────────────────────────────────┐
│              参数                │             当前值           │
├─────────────────────────────────────────────────────────────────┤
│    startwal                      │                              │
│    endwal                        │                              │
│    startlsnt                     │                              │
│    endlsnt                       │                              │
│    starttime                     │                              │
│    endtime                       │                              │
│    resmode(Data Restore Mode)    │              TIME            │
│    exmode(Data Export Mode)      │              CSV             │
│    encoding                      │              UTF8            │
│    restype(Data Restore Type)    │              DELETE          │
          ----------------------DropScan----------------------
│    dsoff(DropScan startOffset)   │              0               │
│    blkiter(Block Intervals)      │              5               │
│    itmpcsv(Items Per Csv)        │              100             │
│    idxmode                       │              off             │
└─────────────────────────────────────────────────────────────────┘
PDU.public=# ds;

 ▌全量扫描恢复模式 

 ▌数据文件扫描 
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
          表名           │                                   结果                                    
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 PeisInterfaceLog           99.976 %(21469593600)   数据页: 71158      成功: 722947    (疑似乱码: 2809      ) 失败: 0 

耗时 15.28 秒
└────────────────────────────────────────────────────────────────────────────────────────────────────┘
扫描完毕,文件目录如下: 
        restore/dropscan/PeisInterfaceLog
PDU.public=# ds copy;
已导出: 
/restore/dropscan/PeisInterfaceLog/COPY.sql
PDU.public=# 

[root@xifenfei PeisInterfaceLog]# more COPY.sql 
[root@xifenfei PeisInterfaceLog]# cat /restore/dropscan/PeisInterfaceLog/COPY.sql
COPY PeisInterfaceLog FROM '/restore/dropscan/PeisInterfaceLog/09-28-21:29:55_226738176_32760blks_336787items.csv';
COPY PeisInterfaceLog FROM '/restore/dropscan/PeisInterfaceLog/09-28-21:30:01_595968000_32767blks_330416items.csv';
COPY PeisInterfaceLog FROM '/restore/dropscan/PeisInterfaceLog/09-28-21:30:04_1116061696_5631blks_55744items.csv';

把恢复数据导入到Postgres数据中

[root@xifenfei ~]# su - postgres 
[postgres@xifenfei ~]$ psql
psql (12.8)
Type "help" for help.

postgres=# \i /restore/dropscan/PeisInterfaceLog/COPY.sql
COPY 336787
COPY 330416
COPY 55744
postgres=# select count(1) from "PeisInterfaceLog";
 count  
--------
 722947
(1 row)

经过上述扫描测试证明该工具实现了在Postgres中truncate数据的绝大部分数据恢复(这里的乱码不是由于没有扫描到数据,主要是由于个别字符串由于类型判断关系识别不对导致乱码抛弃).
如果你遇到Postgres 数据库由于drop/truncate等误操作,而且无有效备份进行恢复,面临数据丢失风险,请第一时间保护现场(数据文件所在分区尽可能不要有写入操作),联系我们提供专业恢复技术支持:
电话/微信:17813235971    Q Q:107644445QQ咨询惜分飞    E-Mail:dba@xifenfei.com

pdu完美支持金仓数据库恢复(KingbaseES)

联系:手机/微信(+86 17813235971) QQ(107644445)QQ咨询惜分飞

标题:pdu完美支持金仓数据库恢复(KingbaseES)

作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]

最近朋友对pdu进行了一些完善,让其可以比较好的支持电科金仓数据库(KingbaseES)的v8/v9版本
使用pdu直接unload数据

[kingbase@xifenfei ~]$ isql -Usystem test
Password for user system: 
isql (V9.0)
Licesen Type: SALES-企业版.
Type "help" for help.

test=# select version();
         version         
-------------------------
 KingbaseES V009R002C010
(1 row)

test=# \dt
        List of relations
 Schema | Name  | Type  | Owner  
--------+-------+-------+--------
 public | t_xff | table | system
(1 row)

test=# drop table walminer_contents;
DROP TABLE
test=# select * from t_xff;
 id |       name       
----+------------------
  1 | 汉字
  2 | xff测试
  3 | 惜分飞测试
  4 | www.xifenfei.com
(4 rows)

test=# insert into t_xff values(5,'www.xifenfie.com
test'# www.orasos.com');
INSERT 0 1
test=# select * from t_xff;
 id |       name       
----+------------------
  1 | 汉字
  2 | xff测试
  3 | 惜分飞测试
  4 | www.xifenfei.com
  5 | www.xifenfie.com+
    | www.orasos.com
(5 rows)

test=# checkpoint;
CHECKPOINT

pdu直接unload恢复

test.public=# b; 
开始初始化...
 -pg_database:</data/kingbase/v9/data/global/1262>

数据库:test 
      -pg_schema:</data/kingbase/v9/data/base/13856/2615>
      -pg_class:</data/kingbase/v9/data/base/13856/1259> 共132行
      -pg_attribute:</data/kingbase/v9/data/base/13856/1249> 共8863行
      模式:
        -->public 1张表

数据库:kingbase 
      -pg_schema:</data/kingbase/v9/data/base/13857/2615>
      -pg_class:</data/kingbase/v9/data/base/13857/1259> 共131行
      -pg_attribute:</data/kingbase/v9/data/base/13857/1249> 共8855行
      模式:
        -->public 0张表
PDU.public=# use test;    
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  1         |
|----------------------------------------|
test.public=# set public;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_xff                            |  8.00 KB   |
|--------------------------------------------------|

        仅显示表大小排名前 1 的表名
test.public=# param exmode sql;
-------------------------------------------------------------------
|              参数                |             当前值           |
-------------------------------------------------------------------
|    startwal                      |                              |
|    endwal                        |                              |
|    starttime                     |                              |
|    endtime                       |                              |
|    resmode(Data Restore Mode)    |              TX              |
|    exmode(Data Export Mode)      |              SQL             |
------------------------------------------------------------------
test.public=# unload tab t_xff;
正在解析表 <t_xff>. 已解析数据页: 1, 已解析数据: 5 条
表名<t_xff>-</data/kingbase/v9/data/base/13856/16384> 解析完成, 1 个数据页 ,共计 5 条数据. 成功 5 条; 失败 0 条 
COPY文件路径为:<test/public/t_xff.sql>

test.public=# unload ddl;

DDL导出完成. 文件路径: test/DDL/public_ddl.sql, 共计 1 张表

查看恢复结果,证明可以完美恢复金仓中表的数据

[root@xifenfei pdu]# cat test/public/t_xff.sql
INSERT INTO t_xff VALUES(1,'汉字');
INSERT INTO t_xff VALUES(2,'xff测试');
INSERT INTO t_xff VALUES(3,'惜分飞测试');
INSERT INTO t_xff VALUES(4,'www.xifenfei.com');
INSERT INTO t_xff VALUES(5,'www.xifenfie.com
www.orasos.com');
[root@xifenfei pdu]# cat test/DDL/public_ddl.sql
CREATE SCHEMA public;
set search_path to public;
CREATE TABLE t_xff(
        id int,
        name varchar
);

pdu恢复金仓数据库delete数据
删除数据尝试恢复

test=# delete from t_xff where id in(4,2);
DELETE 2
test=# checkpoint;
CHECKPOINT
test=# select * from t_xff;
 id |       name       
----+------------------
  1 | 汉字
  3 | 惜分飞测试
  5 | www.xifenfie.com+
    | www.orasos.com
(3 rows)

使用pdu恢复被删除数据

PDU.public=# use test;
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  1         |
|----------------------------------------|
test.public=# set public;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_xff                            |  8.00 KB   |
|--------------------------------------------------|

        仅显示表大小排名前 1 的表名
test.public=# scan t_xff;

正在扫描表<t_xff>的删除记录...

开始扫描归档目录
|-起始文件<000000010000000000000005>
|-终点文件<000000010000000000000009>

正在扫描中

-------------------------------------------------------------------
当前为 事务号恢复 模式,扫描结果将以 事务号 为单位进行输出
---------------------------------------------------------------------

读取到最后的日志段: 0/504EC68: 


扫描结束,当前扫描的时间段为
|-开始时间:2025-04-06 19:13:15.280234 CST
|-结束时间:2025-04-08 01:45:59.493354 CST

---------------------------------------------------------------------------------------------------------
 时间戳:2025-04-08 01:45:59.493354 CST | 事务号:1120 | 数据文件oid:16384 | toast文件oid:0 |待恢复条数:2 
---------------------------------------------------------------------------------------------------------
test.public=# restore del 1120;
开始扫描归档目录
|-起始文件<000000010000000000000005>
|-终点文件<000000010000000000000009>

正在扫描中
读取到最后的日志段: 0/504EC68: 
|-已解析出2条数据读取到最后的日志段: 0/504EC68: 


━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
解析结果:成功 2, 失败 0 (即对应FPW未找到), 共计2条数据
恢复出的csv文件路径为<restore/public/t_xff_1120.csv>
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

扫描结束,当前扫描的时间段为
|-开始时间:2025-04-06 19:13:15.280234 CST
|-结束时间:2025-04-08 01:45:59.493354 CST

restore完成
test.public=# exit;
[root@xifenfei pdu]# cat restore/public/t_xff_1120.csv
2       xff测试
4       www.xifenfei.com

通过上述测试,证明pdu工具可以完美的恢复金仓中刚刚被删除的delete数据.

pg单个数据库目录恢复-pdu恢复单个数据库目录数据

联系:手机/微信(+86 17813235971) QQ(107644445)QQ咨询惜分飞

标题:pg单个数据库目录恢复-pdu恢复单个数据库目录数据

作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]

在某些情况下,无法获取pg完整的PGDATA目录中的所有数据库文件目录和文件,只能恢复出来一个数据库目录,对于这种情况,可以通过pdu进行直接恢复,比如有一个目录16805

[postgres@localhost 16805]$ pwd
/tmp/16805
[postgres@localhost 16805]$ ls 
112        16841     2613      2682      3079      3596      4159
113        174       2615      2683      3079_fsm  3597      4160
1247       175       2615_fsm  2684      3079_vm   3598      4163
1247_fsm   2187      2615_vm   2685      3080      3599      4164
1247_vm    2224      2616      2686      3081      3600      4165
1249       2228      2616_fsm  2687      3085      3600_fsm  4166
1249_fsm   2328      2616_vm   2688      3118      3600_vm   4167
1249_vm    2336      2617      2689      3119      3601      4168
1255       2337      2617_fsm  2690      3164      3601_fsm  4169
1255_fsm   2579      2617_vm   2691      3256      3601_vm   4170
1255_vm    2600      2618      2692      3257      3602      4171
1259       2600_fsm  2618_fsm  2693      3258      3602_fsm  4172
1259_fsm   2600_vm   2618_vm   2696      3350      3602_vm   4173
1259_vm    2601      2619      2699      3351      3603      4174
13362      2601_fsm  2619_fsm  2701      3379      3603_fsm  5002
13362_fsm  2601_vm   2619_vm   2702      3380      3603_vm   548
13362_vm   2602      2620      2703      3381      3604      549
13365      2602_fsm  2650      2704      3394      3605      6102
13366      2602_vm   2651      2753      3394_fsm  3606      6104
13367      2603      2652      2753_fsm  3394_vm   3607      6106
13367_fsm  2603_fsm  2653      2753_vm   3395      3608      6110
13367_vm   2603_vm   2654      2754      3429      3609      6111
13370      2604      2655      2755      3430      3712      6112
13371      2605      2656      2756      3431      3764      6113
13372      2605_fsm  2657      2757      3433      3764_fsm  6116
13372_fsm  2605_vm   2658      2830      3439      3764_vm   6117
13372_vm   2606      2659      2831      3440      3766      6175
13375      2606_fsm  2660      2832      3455      3767      6176
13376      2606_vm   2661      2833      3456      3997      6228
13377      2607      2662      2834      3456_fsm  4143      6229
13377_fsm  2607_fsm  2663      2835      3456_vm   4144      6237
13377_vm   2607_vm   2664      2836      3466      4145      6238
13380      2608      2665      2836_fsm  3467      4146      6239
13381      2608_fsm  2666      2836_vm   3468      4147      826
1417       2608_vm   2667      2837      3501      4148      827
1418       2609      2668      2838      3502      4149      828
16806      2609_fsm  2669      2838_fsm  3503      4150      pg_filenode.map
16806_fsm  2609_vm   2670      2838_vm   3534      4151      pg_internal.init
16806_vm   2610      2673      2839      3541      4152      PG_VERSION
16809      2610_fsm  2674      2840      3541_fsm  4153
16810      2610_vm   2675      2840_fsm  3541_vm   4154
16833      2611      2678      2840_vm   3542      4155
16833_fsm  2612      2679      2841      3574      4156
16833_vm   2612_fsm  2680      2995      3575      4157
16840      2612_vm   2681      2996      3576      4158
[postgres@localhost 16805]$ 

利用pdu进行恢复

PDU.public=# restore db xifenfei /tmp/16805;
      -pg_schema:</tmp/16805/2615>
      -pg_class:</tmp/16805/1259>,共86行
      -pg_attribute:</tmp/16805/1249>,共3274行
      模式:
        -->public,2张表
PDU.public=# use xifenfei;
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  2         |
|----------------------------------------|
xifenfei.public=# set public;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_cas_paymentbill                |  8.24 MB   |
|    t_auth                           |  80.00 KB  |
|--------------------------------------------------|

        仅显示表大小排名前 2 的表名
xifenfei.public=# \dt;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_cas_paymentbill                |  8.24 MB   |
|    t_auth                           |  80.00 KB  |
|--------------------------------------------------|

        共计 2 张表
xifenfei.public=# unload sch public;
正在解析表 <t_cas_paymentbill>. 已解析数据页: 1055, 已解析数据: 9636 条
表名<t_cas_paymentbill>-</tmp/16805//16806> 解析完成, 1055 个数据页 ,共计 9636 条数据. 成功 9636 条; 失败【0】条 
COPY文件路径为:<xifenfei/public/t_cas_paymentbill.csv>

正在解析表 <t_auth>. 已解析数据页: 10, 已解析数据: 129 条
表名<t_auth>-</tmp/16805//16833> 解析完成, 10 个数据页 ,共计 129 条数据. 成功 129 条; 失败【0】条 
COPY文件路径为:<xifenfei/public/t_auth.csv>



模式<public>共 2 张表。成功:2, 失败【0】
日志路径:log/log/xifenfei_unload_schema_public_err.txt 

COPY命令导出完成, 文件路径: xifenfei/COPY/public_copy.sql,共找到2个csv文件

DDL导出完成. 文件路径: xifenfei/DDL/public_ddl.sql, 共计 2 张表
xifenfei.public=# 
xifenfei.public=# unload ddl;

DDL导出完成. 文件路径: xifenfei/DDL/public_ddl.sql, 共计 2 张表

通过pdu解析和恢复,该目录中一共两个业务表,均正常恢复出来,创建新库,导入数据测试

postgres=# create database xifenfei;
CREATE DATABASE
postgres=# \c xifenfei;
You are now connected to database "xifenfei" as user "postgres".
xifenfei=# \i /data/tools/pdu/xifenfei/DDL/public_ddl.sql
psql:/data/tools/pdu/xifenfei/DDL/public_ddl.sql:1: ERROR:  schema "public" already exists
SET
CREATE TABLE
CREATE TABLE
xifenfei=# \i /data/tools/pdu/xifenfei/COPY/public_copy.sql
SET
COPY 9636
COPY 129
xifenfei=# select count(1) from t_auth;
 count 
-------
   129
(1 row)

xifenfei=# select count(1) from t_cas_paymentbill;
 count 
-------
  9636
(1 row)

pg删除数据恢复—pdu恢复pg delete数据

联系:手机/微信(+86 17813235971) QQ(107644445)QQ咨询惜分飞

标题:pg删除数据恢复—pdu恢复pg delete数据

作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]

误操作,在应用端选择每页50条记录然后点击删除,类似这样误操作删除了近10000条记录,现在希望对其进行恢复.对于这种表被误删除数据的,可以使用pdu进行解析wal然后进行恢复.
1. 配置pdu.ini指定wal日志路径

PGDATA=/data/pg/16/data
ARCHIVE_DEST=/data/arch

2. 初始化字典信息

PDU.public=# b;
开始初始化...
 -pg_database:</data/pg/16/data/global/1262>
    【postgres】
      -pg_schema:</data/pg/16/data/base/5/2615>
      -pg_class:</data/pg/16/data/base/5/1259>,共86行
      -pg_attribute:</data/pg/16/data/base/5/1249>,共3273行
      模式:
        -->public,2张表

3. 关于删除恢复相关帮助

**误删数据恢复**
scan t1;                                │ 扫描被误删的表
restore del <Tx Number>;                │ 通过 事务号 恢复被误删的数据
restore del all;                        │ 通过 时间区间 恢复被误删的数据
--------------------------------------------------------------------------------------------------
add <filenode> <tablename> <attibutes>; │ 将特定表信息手动添加到restore库中
例如: <add 12345 t1 varchar,varchar,timestamp,varchar,numeric,varchar,varchar,varchar,numeric;>
--------------------------------------------------------------------------------------------------
restore db <dbname> <DB Path>;          │ 初始化特定的数据库目录 (例: restore db xmandb /home/postgres/data/base/290113;)

**参数设置**
param startwal 000000010000000800000008;│ 设置scan扫描的起始WAL文件,如果未设置则默认是归档目录的第一个文件
param endwal 000000010000000800000009;  │ 设置scan扫描的结束WAL文件,如果未设置则默认是归档目录的最后一个文件
param resmode tx|time;                  │ 设置restore恢复的模式,选择按照【事务号/TX】或【时间区间/TIME】进行恢复
param starttime 2025-01-01_00:00:00;    │ 设置scan扫描的起始时间 
param endtime 2025-01-01_00:00:00;      │ 设置scan扫描的结束时间 
reset <param name>                      │ 重置某个参数 
show;                                   │ 查看当前所有参数

4.可以通过param相关参数设置扫描wal日志范围,时间,恢复模式等.我们先使用事务模式确认开始时间

postgres.public=# scan t_delete;

正在扫描表<t_delete>的删除记录...

开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
当前为 事务号恢复 模式,扫描结果将以【事务号】为单位进行输出
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━



扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:05:38.006348 CST
|-结束时间:2025-03-05 19:00:19.515825 CST

--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:06:20.420863 CST | 事务号:1360118630 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:38:06.749572 CST | 事务号:1360430105 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.947320 CST | 事务号:1360532435 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948734 CST | 事务号:1360532430 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948754 CST | 事务号:1360532434 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948781 CST | 事务号:1360532432 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
……………………
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:56.872903 CST | 事务号:1360536441 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:57.125587 CST | 事务号:1360536457 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:36 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:56:24.931723 CST | 事务号:1360596835 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:59:38.314220 CST | 事务号:1360621790 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
postgres.public=# 

5.基于上述数据,事务情况,可以确认第一次发生删除时间为2025-03-05 18:48:41.947320,最后删除时间为:2025-03-05 18:48:57.125587,重新设置基于时间的恢复模式

postgres.public=# param starttime 2025-03-05_18:48:40; 
OK! starttime=2025-03-05_18:48:40
postgres.public=# param endtime 2025-03-05_18:49:00;
OK! endtime=2025-03-05_18:49:00
postgres.public=# param resmode time;
OK! restoreMode=time
postgres.public=# show;
---------------------------------------------------
|       参数       |             当前值           |
---------------------------------------------------
|startwal          |                              |
|endwal            |                              |
|starttime         |2025-03-05 18:48:40.000000 CST|
|endtime           |2025-03-05 18:49:00.000000 CST|
|resmode           |             TIME             |
---------------------------------------------------

6.重新扫描需要恢复表

postgres.public=# scan t_delete;

正在扫描表<t_delete>的删除记录...

开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
当前为 时间区间恢复 模式,将输出该表在时间区间内的所有删除结果
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━



扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:48:40.000000 CST
|-结束时间:2025-03-05 18:49:00.000000 CST

----------------------------------------------------------------------------------------------------------------------------------------------------
 开始时间:2025-03-05 18:48:40.000000 CST | 结束时间:2025-03-05 18:49:00.000000 CST | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:9636 
----------------------------------------------------------------------------------------------------------------------------------------------------
postgres.public=# 

7.确认删除的记录9636条全部被扫描出来,现在对其进行恢复

postgres.public=# restore del all; 
开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中
|-已解析出9636条数据 

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
解析结果:成功 9636,【失败 0】(即对应FPW未找到), 共计9636条数据
恢复出的csv文件路径为<restore/public/t_delete_2025-03-05 18:48:40.000000 CST_2025-03-05 18:49:00.000000 CST.csv>
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:48:40.000000 CST
|-结束时间:2025-03-05 18:49:00.000000 CST

restore完成
postgres.public=#

8.恢复数据导入库中

[postgres@localhost pdu]$ psql -f postgres/COPY/public_copy.sql
SET
COPY 9636

QQ20250315-235527

PostgreSQL表文件损坏恢复—pdu恢复损坏的表文件

联系:手机/微信(+86 17813235971) QQ(107644445)QQ咨询惜分飞

标题:PostgreSQL表文件损坏恢复—pdu恢复损坏的表文件

作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]

在某些情况下,由于PostgreSQL表文件损坏导致无法正常访问,可以通过pdu把好的block中的数据恢复出来
准备一张测试表,里面有97条记录

his5_dms=# \d hiscrm.t_sys_oper_log;
                                            Table "hiscrm.t_sys_oper_log"
       Column       |          Type          | Collation | Nullable |Default                     
--------------------+------------------------+-----------+----------+------------
 id                 | bigint                 |           | not null | 
 module             | character varying(50)  |           |          | 
 title              | character varying(50)  |           |          | 
 alias              | character varying(50)  |           |          | 
 business_type      | integer                |           |          | 0
 method             | character varying(200) |           |          | 
 request_method     | character varying(10)  |           |          | 
 operator_type      | integer                |           |          | 0
 oper_name          | character varying(50)  |           |          | 
 dept_name          | character varying(50)  |           |          | 
 oper_url           | character varying(255) |           |          | 
 oper_ip            | character varying(50)  |           |          | 
 oper_location      | character varying(255) |           |          | 
 oper_param         | text                   |           |          | 
 json_result        | text                   |           |          | 
 status             | integer                |           |          | 0
 error_msg          | text                   |           |          | 
 oper_time          | date                   |           |          | 
 create_id          | bigint                 |           |          | 
 create_time        | bigint                 |           |          | 
 clinic_id          | bigint                 |           |          | 
 group_id           | bigint                 |           |          | 
 patient_id         | bigint                 |           |          | 
 is_patient_related | integer                |           |          | 
 business_content   | json                   |           |          | 

his5_dms=# select count(1) from hiscrm.t_sys_oper_log;
 count 
-------
    97
(1 row)

查询表对应的具体文件

his5_dms=# SELECT oid,relfilenode FROM pg_class WHERE relname='t_sys_oper_log';
  oid  | relfilenode 
-------+-------------
 16850 |       16850
(1 row)

his5_dms=# SELECT pg_relation_filepath('hiscrm.t_sys_oper_log');
 pg_relation_filepath 
----------------------
 base/16386/16850
(1 row)

his5_dms=# SHOW data_directory;
     data_directory     
------------------------
 /var/lib/pgsql/12/data
(1 row)

使用dd对文件进行破坏

[postgres@xifenfeidg ~]$ ls -l  /var/lib/pgsql/12/data/base/16386/16850
-rw-------. 1 postgres postgres 90112 Sep  5 20:26 /var/lib/pgsql/12/data/base/16386/16850
[postgres@xifenfeidg ~]$ dd if=/dev/zero of=/var/lib/pgsql/12/data/base/16386/16850 bs=512 count=1 conv=notrunc
1+0 records in
1+0 records out
512 bytes copied, 0.000158756 s, 3.2 MB/s

重启pg库

[postgres@xifenfeidg bin]$ ./pg_ctl -m fast -D /var/lib/pgsql/12/data/ stop
waiting for server to shut down.... done
server stopped
[postgres@xifenfeidg bin]$ ./pg_ctl -D /var/lib/pgsql/12/data/ start
waiting for server to start....2025-03-02 19:02:11.395 HKT [64515] LOG:  
  starting PostgreSQL 12.20 on x86_64-pc-linux-gnu, 
  compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22), 64-bit
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on IPv6 address "::1", port 5432
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2025-03-02 19:02:11.397 HKT [64515] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2025-03-02 19:02:11.403 HKT [64515] LOG:  redirecting log output to logging collector process
2025-03-02 19:02:11.403 HKT [64515] HINT:  Future log output will appear in directory "log".
 done
server started

查询数据报错

[postgres@xifenfeidg bin]$ psql
psql (16.8, server 12.20)
Type "help" for help.

postgres=# \c his5_dms;
psql (16.8, server 12.20)
You are now connected to database "his5_dms" as user "postgres".
his5_dms=#  select count(1) from hiscrm.t_sys_oper_log;
ERROR:  invalid page in block 0 of relation base/16386/16850

通过pdu进行恢复
跳过了坏块,把好的block中数据均恢复出来

his5_dms.hiscrm=# unload tab t_sys_oper_log;
正在解析表 <t_sys_oper_log>. 已解析数据页: 0, 已解析数据: 0 条
        |-块号0 空页面或页面已损坏,已跳过
正在解析表 <t_sys_oper_log>. 已解析数据页: 11, 已解析数据: 86 条
表名<t_sys_oper_log>-</var/lib/pgsql/12/data/base/16386/16850> 
    解析完成, 11 个数据页 ,共计 86 条数据. 成功 86 条; 失败【0】条 
COPY文件路径为:<his5_dms/hiscrm/t_sys_oper_log.csv>

导入pg库中

his5_dms=# truncate table hiscrm.t_sys_oper_log;
TRUNCATE TABLE
his5_dms=# \i his5_dms/COPY/hiscrm_copy.sql
SET
COPY 86
his5_dms=#