<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>惜分飞 &#187; pdu工具</title>
	<atom:link href="https://www.xifenfei.com/tag/pdu%e5%b7%a5%e5%85%b7/feed" rel="self" type="application/rss+xml" />
	<link>https://www.xifenfei.com</link>
	<description>提供7*24专业数据库(Oracle,SQL Server,MySQL,PostgreSQL等)恢复和技术支持@Tel:17813235971</description>
	<lastBuildDate>Wed, 04 Nov 2026 15:33:30 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>PostgreSQL表文件损坏恢复&#8212;pdu恢复损坏的表文件</title>
		<link>https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html</link>
		<comments>https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html#comments</comments>
		<pubDate>Tue, 04 Mar 2025 14:11:13 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[pdu工具]]></category>
		<category><![CDATA[ERROR: invalid page in block]]></category>
		<category><![CDATA[pdu]]></category>
		<category><![CDATA[pdu恢复]]></category>
		<category><![CDATA[pg坏块恢复]]></category>
		<category><![CDATA[pg恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11277</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<div id="xifenfeicopyright">
<p>联系：<a href="https://www.xifenfei.com/about_xifenfei" target="_blank">手机/微信(+86 17813235971)   QQ(107644445)</a><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&#038;uin=107644445&#038;site=qq&#038;menu=yes"><img src="https://www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"/></a></p>
<p>标题：<a href="https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html" target="_blank">PostgreSQL表文件损坏恢复&#8212;pdu恢复损坏的表文件</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>在某些情况下,由于PostgreSQL表文件损坏导致无法正常访问,可以通过pdu把好的block中的数据恢复出来<br />
<strong>准备一张测试表,里面有97条记录</strong></p>
<pre class="brush: sql; title: ; notranslate">
his5_dms=# \d hiscrm.t_sys_oper_log;
                                            Table &quot;hiscrm.t_sys_oper_log&quot;
       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)
</pre>
<p><strong>查询表对应的具体文件</strong></p>
<pre class="brush: sql; title: ; notranslate">
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)

</pre>
<p><strong>使用dd对文件进行破坏</strong></p>
<pre class="brush: bash; title: ; notranslate">
[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
</pre>
<p><strong>重启pg库</strong></p>
<pre class="brush: bash; title: ; notranslate">
[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 &quot;::1&quot;, port 5432
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on IPv4 address &quot;127.0.0.1&quot;, port 5432
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
2025-03-02 19:02:11.397 HKT [64515] LOG:  listening on Unix socket &quot;/tmp/.s.PGSQL.5432&quot;
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 &quot;log&quot;.
 done
server started
</pre>
<p><strong>查询数据报错</strong></p>
<pre class="brush: bash; title: ; notranslate">
[postgres@xifenfeidg bin]$ psql
psql (16.8, server 12.20)
Type &quot;help&quot; for help.

postgres=# \c his5_dms;
psql (16.8, server 12.20)
You are now connected to database &quot;his5_dms&quot; as user &quot;postgres&quot;.
his5_dms=#  select count(1) from hiscrm.t_sys_oper_log;
ERROR:  invalid page in block 0 of relation base/16386/16850
</pre>
<p><strong>通过pdu进行恢复</strong><br />
跳过了坏块,把好的block中数据均恢复出来</p>
<pre class="brush: bash; title: ; notranslate">
his5_dms.hiscrm=# unload tab t_sys_oper_log;
正在解析表 &lt;t_sys_oper_log&gt;. 已解析数据页: 0, 已解析数据: 0 条
        |-块号0 空页面或页面已损坏，已跳过
正在解析表 &lt;t_sys_oper_log&gt;. 已解析数据页: 11, 已解析数据: 86 条
表名&lt;t_sys_oper_log&gt;-&lt;/var/lib/pgsql/12/data/base/16386/16850&gt; 
    解析完成, 11 个数据页 ,共计 86 条数据. 成功 86 条; 失败【0】条 
COPY文件路径为:&lt;his5_dms/hiscrm/t_sys_oper_log.csv&gt;
</pre>
<p><strong>导入pg库中</strong></p>
<pre class="brush: sql; title: ; notranslate">
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=# 
</pre>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/02/pdu-threshold.html" rel="bookmark" class="crp_title">PostgreSQL恢复工具&#8212;pdu工具介绍</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/12/pg-replace-oid-recovery.html" rel="bookmark" class="crp_title">PostgreSQL oid文件替换实现数据访问</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pdu-single-oid-recovery.html" rel="bookmark" class="crp_title">PostgreSQL恢复工具&#8212;pdu恢复单个表文件</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/invalid-checkpoint-record.html" rel="bookmark" class="crp_title">pg启动报invalid checkpoint record处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/psql-fatal-no-pg_hba-conf-entry-for-host.html" rel="bookmark" class="crp_title">psql: FATAL:  no pg_hba.conf entry for host</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2022/05/pg-wal-lost-recovery.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:wal日志丢失恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/09/pg-truncate-recovery.html" rel="bookmark" class="crp_title">Postgres数据库truncate表无有效备份恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/pg_rman_backup_restore.html" rel="bookmark" class="crp_title">pg_rman 备份还原测试</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2015/06/%e5%9c%a8linux%e4%b8%8a%e5%ae%89%e8%a3%85postgresql-9-4%e5%b9%b6%e5%90%af%e5%8a%a8%e5%92%8c%e5%85%b3%e9%97%ad%e6%95%b0%e6%8d%ae%e5%ba%93.html" rel="bookmark" class="crp_title">在linux上安装PostgreSQL 9.4并启动和关闭数据库</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2022/05/pg-pg_control-lost-recovery.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:pg_control异常恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pg_control-rebuild.html" rel="bookmark" class="crp_title">pg_control丢失/损坏处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/postgresql-archive_mode.html" rel="bookmark" class="crp_title">PostgreSQL修改归档模式</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQL恢复工具&#8212;pdu恢复单个表文件</title>
		<link>https://www.xifenfei.com/2025/03/pdu-single-oid-recovery.html</link>
		<comments>https://www.xifenfei.com/2025/03/pdu-single-oid-recovery.html#comments</comments>
		<pubDate>Sun, 02 Mar 2025 13:51:12 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[pdu工具]]></category>
		<category><![CDATA[pdu]]></category>
		<category><![CDATA[pdu恢复]]></category>
		<category><![CDATA[pg恢复]]></category>
		<category><![CDATA[单个pg文件恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11265</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/03/pdu-single-oid-recovery.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<div id="xifenfeicopyright">
<p>联系：<a href="https://www.xifenfei.com/about_xifenfei" target="_blank">手机/微信(+86 17813235971)   QQ(107644445)</a><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&#038;uin=107644445&#038;site=qq&#038;menu=yes"><img src="https://www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"/></a></p>
<p>标题：<a href="https://www.xifenfei.com/2025/03/pdu-single-oid-recovery.html" target="_blank">PostgreSQL恢复工具&#8212;pdu恢复单个表文件</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>在某些情况下,比如我们需要对单个的PostgreSQL库的表文件进行恢复(比如文件系统损坏,drop库/表,truncate表等原因,然后找到了部分oid文件),可以使用pdu对其进行完美恢复（相比pg_filedump也方便很多）,具体操作步骤:<br />
<strong>1. 由于只有单个表文件,无法获取字典信息,因此需要应用厂商/客户提供具体表创建语句</strong></p>
<pre class="brush: sql; title: ; notranslate">
his5_dms=#    CREATE TABLE t_xff (
his5_dms(#         id                       bigint,
his5_dms(#         hospital_id              bigint,
his5_dms(#         parent_id                bigint,
his5_dms(#         disease_code             varchar(60),
his5_dms(#         disease_name             varchar(60),
his5_dms(#         type                     smallint,
his5_dms(#         py                       varchar(60),
his5_dms(#         wb                       varchar(60),
his5_dms(#         sc                       varchar(20),
his5_dms(#         order_no                 int,
his5_dms(#         state                    smallint,
his5_dms(#         create_datetime          timestamp(6),
his5_dms(#         create_id                bigint,
his5_dms(#         edit_datetime            timestamp(6),
his5_dms(#         edit_id                  bigint,
his5_dms(#         search_path              varchar(300),
his5_dms(#         diagnosis_sort           int,
his5_dms(#         category_name            varchar(40),
his5_dms(#         input_option             varchar(40),
his5_dms(#         category_class           smallint,
his5_dms(#         memo1                    varchar(300),
his5_dms(#         memo2                    varchar(300),
his5_dms(#         other_code               varchar(60),
his5_dms(#         other_name               varchar(60),
his5_dms(#         special_disease_flag     smallint
his5_dms(#    );
CREATE TABLE
</pre>
<p><strong>2. 把oid文件pdu放到restore库中</strong></p>
<pre class="brush: bash; title: ; notranslate">
[root@xifenfeidg public]# pwd
/tmp/pdu/restore/public
[root@xifenfeidg public]# ls -l
total 7144
-rw-r--r--. 1 root root 7315456 Mar  2 21:04 123456
[root@xifenfeidg public]# 
</pre>
<p><strong>3. 使用add语句在pdu加载数据类型</strong></p>
<pre class="brush: bash; title: ; notranslate">
restore.public=# add 123456 t_xff bigint,bigint,bigint,varchar,varchar,smallint,varchar,varchar,varchar,
int,smallint,timestamp,bigint,timestamp,bigint,varchar,int,varchar,varchar,
smallint,varchar,varchar,varchar,varchar,smallint;
添加完成，请用\dt;查看可unload的表
restore.public=# \dt;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_xff                            |  6.98 MB   |
|--------------------------------------------------|

        仅显示表大小排名前 1 的表名
</pre>
<p><strong>4.使用pdu恢复表数据</strong></p>
<pre class="brush: bash; title: ; notranslate">
restore.public=# unload t_xff;
正在解析表 &lt;t_xff&gt;. 已解析数据页: 893, 已解析数据: 46998 条
&lt;t_xff&gt;-&lt;restore/public/123456&gt; 解析完成, 894 个数据页 ,共计 46998 条数据. 成功 46998 条; 失败【0】条 
 COPY文件路径为:&lt;restore/public/t_xff.csv&gt;
restore.public=# unload COPY;

COPY命令导出完成, 文件路径: restore/COPY/public_copy.sql
</pre>
<p><strong>5.导入数据到pg库中</strong></p>
<pre class="brush: sql; title: ; notranslate">
his5_dms=# \i restore/COPY/public_copy.sql
SET
COPY 46998
his5_dms=# select count(1) from t_xff;
 count 
-------
 46998
(1 row)
his5_dms=# \x
Expanded display is on.
his5_dms=# select * from t_xff limit 1;
-[ RECORD 1 ]--------+---------------------------
id                   | 323839
hospital_id          | 0
parent_id            | 301
disease_code         | 57.8900x003
disease_name         | 腹腔镜下膀胱颈悬吊术
type                 | 2
py                   | fqjxpgjxds
wb                   | eeqgeeceks
sc                   | 
order_no             | 0
state                | 1
create_datetime      | 2022-09-29 15:22:58.588492
create_id            | 
edit_datetime        | 
edit_id              | 
search_path          | 301,
diagnosis_sort       | 
category_name        | 
input_option         | 
category_class       | 3
memo1                | 
memo2                | 
other_code           | 
other_name           | 
special_disease_flag | 0
</pre>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/02/pdu-threshold.html" rel="bookmark" class="crp_title">PostgreSQL恢复工具&#8212;pdu工具介绍</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html" rel="bookmark" class="crp_title">PostgreSQL表文件损坏恢复&#8212;pdu恢复损坏的表文件</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2010/08/%e5%b8%b8%e8%a7%81%e6%95%b0%e6%8d%ae%e5%ba%93%e5%af%b9%e6%97%a5%e6%9c%9f%e6%97%b6%e9%97%b4%e6%a0%bc%e5%bc%8f%e5%8c%96.html" rel="bookmark" class="crp_title">常见数据库对日期时间格式化</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/04/pdu-support-kingbasees-recovery.html" rel="bookmark" class="crp_title">pdu完美支持金仓数据库恢复(KingbaseES)</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2009/12/%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%ae%9e%e7%8e%b0%ef%bc%9a%e6%9b%b4%e6%96%b0%e3%80%81%e6%b1%87%e6%80%bb%e3%80%81%e5%a4%8d%e5%88%b6%e5%8a%9f%e8%83%bd.html" rel="bookmark" class="crp_title">存储过程实现：更新、汇总、复制功能</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2009/12/%e5%8a%a8%e6%80%81%e5%a4%8d%e5%88%b6%e4%b8%80%e4%b8%aa%e8%a1%a8%e4%b8%ad%e4%b8%80%e6%9d%a1%e6%95%b0%e6%8d%ae%e5%88%b0%e5%8f%a6%e4%b8%80%e4%b8%aa%e8%a1%a8%e4%b8%ad%ef%bc%88%e5%88%97%e4%b8%8d%e5%ae%8c.html" rel="bookmark" class="crp_title">动态复制一个表中一条数据到另一个表中（列不完全相同）（存储过程实现）</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pdu-pg-delete-recovery.html" rel="bookmark" class="crp_title">pg删除数据恢复&#8212;pdu恢复pg delete数据</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2015/08/mysql%e6%81%a2%e5%a4%8d%e6%95%b0%e6%8d%ae%e5%ad%97%e5%85%b8%e8%a1%a8%e8%ae%b2%e8%a7%a3.html" rel="bookmark" class="crp_title">[MySQL异常恢复]恢复数据字典表讲解</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/02/ms-sql-server%e4%b8%ad%e7%9a%84-convert-%e6%97%a5%e6%9c%9f%e6%97%b6%e9%97%b4-%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%a4%a7%e5%85%a8.html" rel="bookmark" class="crp_title">MS SQL Server中的 CONVERT 日期时间 格式化大全</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/04/db2%e4%b8%ad%e4%ba%a7%e7%94%9f%e5%94%af%e4%b8%80%e5%80%bc%e4%b8%89%e7%a7%8d%e6%96%b9%e5%bc%8f.html" rel="bookmark" class="crp_title">DB2中产生唯一值三种方式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2010/08/%e7%b2%be%e5%bd%a9sql%e8%af%ad%e5%8f%a5.html" rel="bookmark" class="crp_title">精彩SQL语句</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/12/mysql%e6%9f%a5%e8%af%a2%e8%a7%86%e5%9b%be%ef%bc%9aerror-1449-hy000.html" rel="bookmark" class="crp_title">Mysql查询视图：ERROR 1449 (HY000)</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/03/pdu-single-oid-recovery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQL恢复工具&#8212;pdu工具介绍</title>
		<link>https://www.xifenfei.com/2025/02/pdu-threshold.html</link>
		<comments>https://www.xifenfei.com/2025/02/pdu-threshold.html#comments</comments>
		<pubDate>Fri, 28 Feb 2025 15:11:31 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[pdu工具]]></category>
		<category><![CDATA[pdu]]></category>
		<category><![CDATA[pdu恢复]]></category>
		<category><![CDATA[pg恢复]]></category>
		<category><![CDATA[PostgreSQL恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11260</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/02/pdu-threshold.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<div id="xifenfeicopyright">
<p>联系：<a href="https://www.xifenfei.com/about_xifenfei" target="_blank">手机/微信(+86 17813235971)   QQ(107644445)</a><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&#038;uin=107644445&#038;site=qq&#038;menu=yes"><img src="https://www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"/></a></p>
<p>标题：<a href="https://www.xifenfei.com/2025/02/pdu-threshold.html" target="_blank">PostgreSQL恢复工具&#8212;pdu工具介绍</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>张晨同学开发了一个PostgreSQL数据恢复工具PDU(PDU: Postgresql Data Rescue Tool),我这边配合做一些测试<br />
<strong>使用帮助命令</strong></p>
<pre class="brush: bash; title: ; notranslate">
PDU.public=# ;

PDU数据拯救工具 | 命令帮助
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
**基础操作**
b;                                      │ 初始化数据库元信息
exit;                                   │ 退出工具

**数据库切换**
use &lt;db&gt;;                               │ 指定目标数据库 (例: use logs;)
set &lt;schema&gt;;                           │ 指定操作模式 (例: set recovery;)

**元数据展示**
\l;                                     │ 列出所有数据库
\dn;                                    │ 显示当前数据库模式
\dt;                                    │ 列出当前模式下的表
\d+ &lt;table&gt;;                            │ 查看表结构详情 (例: \d+ users;)
\d &lt;table&gt;;                             │ 查看表列类型 (例: \d users;)

**数据导出**
unload &lt;table&gt;;                         │ 导出表数据 → ./&lt;表名&gt;.csv (例: unload orders;)
unload SCH;                             │ 导出当前模式所有数据
unload DDL;                             │ 生成模式结构定义文件
unload COPY;                            │ 生成PSQL COPY语句脚本

**误删数据恢复**
scan t1;                                │ 扫描被误删的表
restore del &lt;Tx Number&gt;;                │ 通过事务号恢复被误删的数据
--------------------------------------------------------------------------------------
scan drop;                              │ 扫描wal日志中的drop事务
restore drop &lt;Tx Number&gt;;               │ 通过事务号恢复被drop的表
add &lt;oid&gt; &lt;tablename&gt; &lt;attibutes&gt;;      │ 将表信息手动添加到restore库中
例如: &lt;add 12345 t1 varchar,varchar,timestamp,varchar,numeric,varchar,varchar,varchar,numeric;&gt;

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
语法规则
◈ 指令后缀必须带 `;`
</pre>
<p><strong>加载PostgreSQL元数据</strong></p>
<pre class="brush: bash; title: ; notranslate">
PDU.public=# b;
开始初始化...
 -pg_database:&lt;/var/lib/pgsql/12/data/global/1262&gt;
    【postgres】
      -pg_schema:&lt;/var/lib/pgsql/12/data/base/14399/2615&gt;
      -pg_class:&lt;/var/lib/pgsql/12/data/base/14399/1259&gt;，共55行
      -pg_attribute:&lt;/var/lib/pgsql/12/data/base/14399/1249&gt;，共2913行
      模式:
        --&gt;public，2张表
    【his5_dms】
      -pg_schema:&lt;/var/lib/pgsql/12/data/base/16386/2615&gt;
      -pg_class:&lt;/var/lib/pgsql/12/data/base/16386/1259&gt;，共793行
      -pg_attribute:&lt;/var/lib/pgsql/12/data/base/16386/1249&gt;，共31329行
      模式:
        --&gt;public，660张表
        --&gt;hiscrm，55张表
        --&gt;pgagent，8张表
        --&gt;report，7张表
        --&gt;statistics，10张表
</pre>
<p><strong>查看当前有哪些库</strong></p>
<pre class="brush: bash; title: ; notranslate">
PDU.public=# \l;
|------------------|
|     数据库名     |
|------------------|
|    postgres      |
|    template1     |
|    template0     |
|    his5_dms      |
|    restore       |
|------------------|

      5 rows selected
</pre>
<p><strong>进入某个库</strong></p>
<pre class="brush: bash; title: ; notranslate">
PDU.public=# use his5_dms;
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  660       |
|    hiscrm                 |  55        |
|    pgagent                |  8         |
|    report                 |  7         |
|    statistics             |  10        |
|----------------------------------------|
</pre>
<p><strong>进入某个模式</strong></p>
<pre class="brush: bash; title: ; notranslate">
his5_dms.public=# set hiscrm;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_patient_other                  |  600.00 KB |
|    t_sys_oper_log                   |  88.00 KB  |
|    t_auth                           |  88.00 KB  |
|    t_setting_user                   |  56.00 KB  |
|    t_field_define                   |  32.00 KB  |
|    t_oper_log                       |  16.00 KB  |
|    t_role                           |  16.00 KB  |
|    t_sys_login_log                  |  8.00 KB   |
|    t_appointment_item               |  8.00 KB   |
|    t_clinic                         |  8.00 KB   |
|    t_dept                           |  8.00 KB   |
|    t_employee                       |  8.00 KB   |
|    t_menu                           |  8.00 KB   |
|    t_patient_label                  |  8.00 KB   |
|    t_patient_label_detail_tpl       |  8.00 KB   |
|    t_patient_source_ref             |  8.00 KB   |
|    t_return_visit_tpl               |  8.00 KB   |
|    t_setting_clinic                 |  8.00 KB   |
|    t_setting_notify                 |  8.00 KB   |
|    t_sms_template_category          |  8.00 KB   |
|--------------------------------------------------|

        仅显示表大小排名前 50 的表名
</pre>
<p><strong>显示部分表</strong></p>
<pre class="brush: bash; title: ; notranslate">
his5_dms.hiscrm=# \dt;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_patient_other                  |  600.00 KB |
|    t_sys_oper_log                   |  88.00 KB  |
|    t_auth                           |  88.00 KB  |
|    t_setting_user                   |  56.00 KB  |
|    t_field_define                   |  32.00 KB  |
|    t_oper_log                       |  16.00 KB  |
|    t_role                           |  16.00 KB  |
|    t_sys_login_log                  |  8.00 KB   |
|    t_appointment_item               |  8.00 KB   |
|    t_clinic                         |  8.00 KB   |
|    t_dept                           |  8.00 KB   |
|    t_employee                       |  8.00 KB   |
|    t_menu                           |  8.00 KB   |
|    t_patient_label                  |  8.00 KB   |
|    t_patient_label_detail_tpl       |  8.00 KB   |
|    t_patient_source_ref             |  8.00 KB   |
|    t_return_visit_tpl               |  8.00 KB   |
|    t_setting_clinic                 |  8.00 KB   |
|    t_setting_notify                 |  8.00 KB   |
|    t_sms_template_category          |  8.00 KB   |
|--------------------------------------------------|

        仅显示表大小排名前 50 的表名
</pre>
<p><strong>显示某个表的信息</strong></p>
<pre class="brush: bash; title: ; notranslate">
his5_dms.hiscrm=# \d+ t_auth;
----------------------------------------------------------------
|                            建表语句                           |
----------------------------------------------------------------
   CREATE TABLE t_auth (
        id                       bigint,
        clinic_id                bigint,
        group_id                 bigint,
        parient_id               varchar(64),
        menu_id                  varchar(64),
        auth_key                 varchar(60),
        auth_name                varchar(64),
        uris                     varchar,
        rely                     varchar(255),
        state                    bigint,
        sort                     bigint,
        tag                      bigint,
        explain                  varchar(255),
        desc                     varchar(255)
   );
----------------------------------------------------------------
|                                                              |
----------------------------------------------------------------
his5_dms.hiscrm=# \d t_auth;
----------------------------------------------------------------
|                            列类型                             |
----------------------------------------------------------------
bigint,bigint,bigint,varchar,varchar,varchar,varchar,varchar,varchar,bigint,bigint,bigint,varchar,varchar
</pre>
<p><strong>恢复表数据</strong></p>
<pre class="brush: bash; title: ; notranslate">
his5_dms.hiscrm=# unload t_auth;
正在解析表 &lt;t_auth&gt;. 已解析数据页: 11, 已解析数据: 492 条
&lt;t_auth&gt;-&lt;/var/lib/pgsql/12/data/base/16386/16895&gt; 解析完成, 12 个数据页 ,共计 492 条数据. 成功 492 条; 失败【0】条 
 COPY文件路径为:&lt;his5_dms/hiscrm/t_auth.csv&gt;
</pre>
<p><strong>确认恢复表的数据情况</strong></p>
<pre class="brush: bash; title: ; notranslate">
[root@xifenfeidg hiscrm]# wc -l t_auth.csv
492 t_auth.csv
</pre>
<p><img src="https://www.xifenfei.com/wp-content/uploads/2025/02/QQ20250228-224837.png" alt="QQ20250228-224837" width="1637" height="321" class="alignleft size-full wp-image-11261" /><br />
<hr />
<strong>PostgreSQL中查询表实际数据情况</strong></p>
<pre class="brush: sql; title: ; notranslate">
his5_dms=# select count(1) from hiscrm.t_auth;
 count 
-------
   492
(1 row)
is5_dms=# \d hiscrm.t_auth;
                                Table &quot;hiscrm.t_auth&quot;
   Column   |          Type          | Collation | Nullable |         Default         
------------+------------------------+-----------+----------+-------------------------
 id         | bigint                 |           | not null | 
 clinic_id  | bigint                 |           |          | 
 group_id   | bigint                 |           |          | 
 parient_id | character varying(64)  |           | not null | 
 menu_id    | character varying(64)  |           | not null | 
 auth_key   | character varying(60)  |           | not null | 
 auth_name  | character varying(64)  |           | not null | 
 uris       | text                   |           | not null | 
 rely       | character varying(255) |           |          | NULL::character varying
 state      | bigint                 |           | not null | 
 sort       | bigint                 |           | not null | 
 tag        | bigint                 |           |          | '0'::bigint
 explain    | character varying(255) |           |          | NULL::character varying
 desc       | character varying(255) |           |          | NULL::character varying
</pre>
<p><strong>truncate表</strong></p>
<pre class="brush: sql; title: ; notranslate">
his5_dms=# truncate table hiscrm.t_auth;
TRUNCATE TABLE
his5_dms=# select count(1) from hiscrm.t_auth;
 count 
-------
     0
(1 row)
</pre>
<p><strong>导入数据,并验证恢复效果</strong></p>
<pre class="brush: sql; title: ; notranslate">
his5_dms=# \i /tmp/pdu/his5_dms/COPY/hiscrm_copy.sql 
SET
COPY 492
his5_dms=# select count(1) from  hiscrm.t_auth;
 count 
-------
   492
(1 row)
</pre>
<p>通过验证pdu可以在数据库离线的情况下，恢复PostgreSQL数据库中表的数据，更加方便和灵活的实现替代pg_filedump功能，而且pdu还在不断完善和新功能更新中</p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html" rel="bookmark" class="crp_title">PostgreSQL表文件损坏恢复&#8212;pdu恢复损坏的表文件</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pdu-single-oid-recovery.html" rel="bookmark" class="crp_title">PostgreSQL恢复工具&#8212;pdu恢复单个表文件</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2015/08/mysql%e6%81%a2%e5%a4%8d%e6%95%b0%e6%8d%ae%e5%ad%97%e5%85%b8%e8%a1%a8%e8%ae%b2%e8%a7%a3.html" rel="bookmark" class="crp_title">[MySQL异常恢复]恢复数据字典表讲解</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/12/pg-replace-oid-recovery.html" rel="bookmark" class="crp_title">PostgreSQL oid文件替换实现数据访问</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/04/db2%e4%b8%ad%e4%ba%a7%e7%94%9f%e5%94%af%e4%b8%80%e5%80%bc%e4%b8%89%e7%a7%8d%e6%96%b9%e5%bc%8f.html" rel="bookmark" class="crp_title">DB2中产生唯一值三种方式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/04/pdu-support-kingbasees-recovery.html" rel="bookmark" class="crp_title">pdu完美支持金仓数据库恢复(KingbaseES)</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2010/08/%e5%b8%b8%e8%a7%81%e6%95%b0%e6%8d%ae%e5%ba%93%e5%af%b9%e6%97%a5%e6%9c%9f%e6%97%b6%e9%97%b4%e6%a0%bc%e5%bc%8f%e5%8c%96.html" rel="bookmark" class="crp_title">常见数据库对日期时间格式化</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2009/12/%e5%8a%a8%e6%80%81%e5%a4%8d%e5%88%b6%e4%b8%80%e4%b8%aa%e8%a1%a8%e4%b8%ad%e4%b8%80%e6%9d%a1%e6%95%b0%e6%8d%ae%e5%88%b0%e5%8f%a6%e4%b8%80%e4%b8%aa%e8%a1%a8%e4%b8%ad%ef%bc%88%e5%88%97%e4%b8%8d%e5%ae%8c.html" rel="bookmark" class="crp_title">动态复制一个表中一条数据到另一个表中（列不完全相同）（存储过程实现）</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pdu-pg-delete-recovery.html" rel="bookmark" class="crp_title">pg删除数据恢复&#8212;pdu恢复pg delete数据</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/06/format-disk-mysql-recovery.html" rel="bookmark" class="crp_title">文件系统格式化MySQL数据库恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/02/ms-sql-server%e4%b8%ad%e7%9a%84-convert-%e6%97%a5%e6%9c%9f%e6%97%b6%e9%97%b4-%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%a4%a7%e5%85%a8.html" rel="bookmark" class="crp_title">MS SQL Server中的 CONVERT 日期时间 格式化大全</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2009/12/%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%ae%9e%e7%8e%b0%ef%bc%9a%e6%9b%b4%e6%96%b0%e3%80%81%e6%b1%87%e6%80%bb%e3%80%81%e5%a4%8d%e5%88%b6%e5%8a%9f%e8%83%bd.html" rel="bookmark" class="crp_title">存储过程实现：更新、汇总、复制功能</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/02/pdu-threshold.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
