<?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; Postgres truncate恢复</title>
	<atom:link href="https://www.xifenfei.com/tag/postgres-truncate%e6%81%a2%e5%a4%8d/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>Postgres数据库truncate表无有效备份恢复</title>
		<link>https://www.xifenfei.com/2025/09/pg-truncate-recovery.html</link>
		<comments>https://www.xifenfei.com/2025/09/pg-truncate-recovery.html#comments</comments>
		<pubDate>Sun, 28 Sep 2025 15:12:07 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[pdu工具]]></category>
		<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[pg truncate 恢复]]></category>
		<category><![CDATA[Postgres truncate恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11661</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/09/pg-truncate-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/09/pg-truncate-recovery.html" target="_blank">Postgres数据库truncate表无有效备份恢复</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>创建一个Postgres表,并插入数据</p>
<pre class="brush: sql; title: ; notranslate">
postgres=# CREATE TABLE &quot;PeisInterfaceLog&quot;(
postgres(#         &quot;PeisInterfaceLogId&quot; text,
postgres(#         &quot;PeisDepartmentId&quot; text,
postgres(#         &quot;PeisDepartmentName&quot; text,
postgres(#         &quot;PeisInterfaceSubjectType&quot; text,
postgres(#         &quot;PeisInterfaceSubjectId&quot; text,
postgres(#         &quot;PeisInterfaceNo&quot; text,
postgres(#         &quot;PeisInterfaceName&quot; text,
postgres(#         &quot;PeisInterfaceDirection&quot; text,
postgres(#         &quot;PeisInterfaceCallAddress&quot; text,
postgres(#         &quot;PeisInterfaceLogStartTime&quot; timestamp,
postgres(#         &quot;PeisInterfaceInputContent&quot; json,
postgres(#         &quot;PeisInterfaceInputTranscodeContent&quot; json,
postgres(#         &quot;PeisInterfaceOutContent&quot; json,
postgres(#         &quot;PeisInterfaceOutTranscodeContent&quot; json,
postgres(#         &quot;PeisInterfaceSuccessSign&quot; int4,
postgres(#         &quot;PeisInterfaceLogStatusCode&quot; text,
postgres(#         &quot;PeisInterfaceLogNote&quot; text,
postgres(#         &quot;PeisInterfaceLogTimestamp&quot; timestamp,
postgres(#         &quot;PeisInterfaceLogInfo&quot; text,
postgres(#         &quot;PeisPatientRegisterId&quot; text
postgres(# );
CREATE TABLE
postgres=# \i /postgres/COPY/public_copy.sql 
SET
COPY 722957
postgres=# select count(1) from &quot;PeisInterfaceLog&quot;;
 count  
--------
 722957
(1 row)
</pre>
<p>验证truncate操作,引起Postgres中oid的变化</p>
<pre class="brush: sql; title: ; notranslate">
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 &quot;PeisInterfaceLog&quot;;
TRUNCATE TABLE
postgres=# select count(1) from  &quot;PeisInterfaceLog&quot;;
 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)
</pre>
<p>使用工具进行初始化字典信息</p>
<pre class="brush: bash; title: ; notranslate">
PDU.public=# b;

开始初始化...
 -pg_database:&lt;/pgdata/global/1262&gt;

数据库:postgres 
      -pg_schema:&lt;/pgdata/base/13676/2615&gt;
      -pg_class:&lt;/pgdata/base/13676/1259&gt; 共88行
      -pg_attribute:&lt;/pgdata/base/13676/1249&gt; 共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

</pre>
<p>配置软件磁盘扫描操作（pdu.ini中配置）</p>
<pre class="brush: bash; title: ; notranslate">
#dropScan需要扫描的磁盘
DISK_PATH=/data/test.dd
#dropScan时跳跃的数据块数量，数值越小覆盖磁盘越全面，速度越慢
BLOCK_INTERVAL=5
</pre>
<p>启用磁盘扫描操作</p>
<pre class="brush: bash; title: ; notranslate">
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';
</pre>
<p>把恢复数据导入到Postgres数据中</p>
<pre class="brush: sql; title: ; notranslate">
[root@xifenfei ~]# su - postgres 
[postgres@xifenfei ~]$ psql
psql (12.8)
Type &quot;help&quot; for help.

postgres=# \i /restore/dropscan/PeisInterfaceLog/COPY.sql
COPY 336787
COPY 330416
COPY 55744
postgres=# select count(1) from &quot;PeisInterfaceLog&quot;;
 count  
--------
 722947
(1 row)
</pre>
<p>经过上述扫描测试证明该工具实现了在Postgres中truncate数据的绝大部分数据恢复(这里的乱码不是由于没有扫描到数据,主要是由于个别字符串由于类型判断关系识别不对导致乱码抛弃).<br />
如果你遇到Postgres 数据库由于drop/truncate等误操作，而且无有效备份进行恢复，面临数据丢失风险,请第一时间保护现场(数据文件所在分区尽可能不要有写入操作)，联系我们提供专业恢复技术支持:<br />
<strong><font color="red" size=4>电话/微信:17813235971 &nbsp;&nbsp;   Q Q:107644445<a target="_blank" href="https://wpa.qq.com/msgrd?v=3&amp;uin=107644445&amp;site=qq&amp;menu=yes"><img id="xffqqimg" src="//www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"></a>  &nbsp;&nbsp;  E-Mail:dba@xifenfei.com</font></strong></p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/04/pg-createdb-gbk.html" rel="bookmark" class="crp_title">pg创建gbk字符集库</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/2018/08/postgresql-archive_mode.html" rel="bookmark" class="crp_title">PostgreSQL修改归档模式</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/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/2026/01/pg-readme_to_recover.html" rel="bookmark" class="crp_title">注意:PostgreSQL库出现readme_to_recover勒索</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/pg-tablespace-database-schema.html" rel="bookmark" class="crp_title">PostgreSQL的表空间、数据库、用户之间的关系</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2024/04/pg_filedump_batch.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:pg_filedump批量处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2024/04/pg_filedump-batch-recovery.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:pg_filedump恢复字典构造</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/03/navicat-postgresql-datlastsysoid.html" rel="bookmark" class="crp_title">Navicat连接postgresql时出现column &#8220;datlastsysoid&#8221; does not exist错误解决</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></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/09/pg-truncate-recovery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
