<?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; PostgreSQL恢复</title>
	<atom:link href="https://www.xifenfei.com/category/database/postgresql/postgresql%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>Sun, 22 Nov 2026 15:21:14 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>记录一次国产数据库被rm -rf /*删除的救援过程</title>
		<link>https://www.xifenfei.com/2026/01/hgdb-rm-rf-recovery.html</link>
		<comments>https://www.xifenfei.com/2026/01/hgdb-rm-rf-recovery.html#comments</comments>
		<pubDate>Fri, 02 Jan 2026 16:46:40 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[pdu恢复]]></category>
		<category><![CDATA[rm -rf / 故障恢复]]></category>
		<category><![CDATA[rm -rf 国产库恢复]]></category>
		<category><![CDATA[rm 数据库恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11872</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2026/01/hgdb-rm-rf-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/2026/01/hgdb-rm-rf-recovery.html" target="_blank">记录一次国产数据库被rm -rf /*删除的救援过程</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>有一套国产数据库运行在arm平台的Kylin Linux上</p>
<pre class="brush: bash; title: ; notranslate">
NAME=&amp;amp;quot;Kylin Linux Advanced Server&amp;amp;quot;
VERSION=&amp;amp;quot;V10 (Lance)&amp;amp;quot;
ID=&amp;amp;quot;kylin&amp;amp;quot;
VERSION_ID=&amp;amp;quot;V10&amp;amp;quot;
PRETTY_NAME=&amp;amp;quot;Kylin Linux Advanced Server V10 (Lance)&amp;amp;quot;
ANSI_COLOR=&amp;amp;quot;0;31&amp;amp;quot;
</pre>
<p>由于数据库归档日志导致磁盘空间满,在清理归档日志的时候不慎执行了rm -rf /*命令<br />
<a href="https://www.xifenfei.com/wp-content/uploads/2026/01/rm.png"><img src="https://www.xifenfei.com/wp-content/uploads/2026/01/rm.png" alt="rm" width="268" height="321" class="alignleft size-full wp-image-11873" /></a><br />
<hr />
导致系统无法正常启动,客户尝试通过把磁盘挂载到其他机器上启动,但是无法恢复需要数据.由于客户这个是ssd盘的虚拟化环境,担心可能触发trim和进一步破坏的风险,建议客户尽快把虚拟磁盘下载到本地,然后通过工具分析,发现大量数据已经被删除,特别是数据库目录只剩下部分文件<br />
<a href="https://www.xifenfei.com/wp-content/uploads/2026/01/1.png"><img src="https://www.xifenfei.com/wp-content/uploads/2026/01/1.png" alt="1" width="239" height="353" class="alignleft size-full wp-image-11874" /></a><br />
<hr />
通过分析data/global/1262文件(pg_database)的出来数据库名字对应关系,然后根据应用提供的需要恢复的数据库名称.在data目录中尝试恢复该目录中oid文件,发现最核心的几个字典文件全部异常(元数据异常)<br />
<a href="https://www.xifenfei.com/wp-content/uploads/2026/01/3.png"><img src="https://www.xifenfei.com/wp-content/uploads/2026/01/3.png" alt="3" width="1077" height="448" class="alignleft size-full wp-image-11875" /></a><br />
<hr />
<pre class="brush: bash; title: ; notranslate">
pg_class	1259	存储数据库中所有关系（表、索引、视图、序列等）的元数据
pg_attribute	1249	存储表的列（属性）信息，每个字段对应一行
pg_namespace	2615	存储模式（Schema）信息
pg_type	        1247	存储数据类型信息（内置类型、自定义类型）
</pre>
<p>对于这种问题,普通的恢复工具无法解决,协调专业的文件系统恢复专家对其进行处理,比较完美的恢复出来的该目录下面绝大部分文件(包括字典文件)<br />
<a href="https://www.xifenfei.com/wp-content/uploads/2026/01/4.png"><img src="https://www.xifenfei.com/wp-content/uploads/2026/01/4.png" alt="4" width="602" height="532" class="alignleft size-full wp-image-11877" /></a><br />
<hr />
有了比较完美的恢复出来了需要的数据库文件,然后pdu专家进行该国产库的块结构分析和调整程序并恢复出来数据<br />
<a href="https://www.xifenfei.com/wp-content/uploads/2026/01/5.png"><img src="https://www.xifenfei.com/wp-content/uploads/2026/01/5.png" alt="5" width="741" height="755" class="alignleft size-full wp-image-11878" /></a><br />
<a href="https://www.xifenfei.com/wp-content/uploads/2026/01/6.png"><img src="https://www.xifenfei.com/wp-content/uploads/2026/01/6.png" alt="6" width="1598" height="716" class="alignleft size-full wp-image-11879" /></a><br />
<hr />
完成数据恢复之后,把数据提供给客户,由客户那边安排厂商进行后续应用层面工作(比如和历史数据整合,应用调试等),总体算比较完美帮忙客户完成了本次rm -rf /* 故障救援</p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/06/deepseek_oracle_recovery_major_fault.html" rel="bookmark" class="crp_title">使用deepseek进行Oracle恢复,引起重大故障</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/01/dd-asm-recovery.html" rel="bookmark" class="crp_title">在生产环境错误执行dd命令破坏asm磁盘故障恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/04/orafhr-oracle-wman.html" rel="bookmark" class="crp_title">OraFHR快速open被勒索加密破坏的Oracle数据库</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/03/wman-mysql-recovery.html" rel="bookmark" class="crp_title">.wman扩展名勒索mysql数据库恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/03/orafhr.html" rel="bookmark" class="crp_title">Oracle数据库被勒索加密一键open工具&#8211;OraFHR</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/01/aix-nfs-combehin.html" rel="bookmark" class="crp_title">aix环境写入大文件设置combehin提高效率</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/10/bres-dmp-recovery.html" rel="bookmark" class="crp_title">.bres勒索病毒加密数据库dmp文件可恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/07/obet-2026-07.html" rel="bookmark" class="crp_title">Oracle Block Edit Tool (obet) 功能增强&#8211;2026.07</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/03/win-rm-datafile-recovery.html" rel="bookmark" class="crp_title">记录一次win删除数据文件完美恢复案例</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/08/obet-forcecopy-ioerror.html" rel="bookmark" class="crp_title">obet forcecopy功能抢救硬件故障中的数据文件</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/12/sql-bak-0-lost-data.html" rel="bookmark" class="crp_title">模拟sql server故障备份完成恢复实现数据0丢失</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/08/redo-corrupt-recovery.html" rel="bookmark" class="crp_title">不太常见的10.2.0.1的oracle redo损坏恢复</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2026/01/hgdb-rm-rf-recovery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQL oid文件替换实现数据访问</title>
		<link>https://www.xifenfei.com/2025/12/pg-replace-oid-recovery.html</link>
		<comments>https://www.xifenfei.com/2025/12/pg-replace-oid-recovery.html#comments</comments>
		<pubDate>Fri, 12 Dec 2025 13:00:21 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[oid恢复]]></category>
		<category><![CDATA[pg恢复]]></category>
		<category><![CDATA[replace oid recovery]]></category>
		<category><![CDATA[替换oid恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11847</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/12/pg-replace-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/12/pg-replace-oid-recovery.html" target="_blank">PostgreSQL oid文件替换实现数据访问</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>对于postgresql数据库,我们都知道他的数据在独立的oid文件里面,而字典主要在pg_type/1247,pg_class/1259,pg_attribute/1260等对象中,而数据是存储在独立的oid和toast oid文件中.如果通过提供oid文件,可以实现狸猫换太子的恢复数据.这里做一个简单测试,大概思路是这样的库里面有一个表a,然后创建一个空表b,然后把a表相关的oid文件复制成b表的oid文件,然后验证数据是否可以正常查询<br />
创建一个空的b表,结构和a表一致</p>
<pre class="brush: sql; title: ; notranslate">
[postgres@xifenfei 13676]$ psql
psql (12.8)
Type &quot;help&quot; for help.

postgres=# CREATE TABLE &quot;public&quot;.&quot;AO_6384AB_DISCOVERED1&quot; (
postgres(#   &quot;DATE&quot; &quot;pg_catalog&quot;.&quot;timestamp&quot;,
postgres(#   &quot;ID&quot; &quot;pg_catalog&quot;.&quot;int4&quot;,
postgres(#   &quot;KEY&quot; &quot;pg_catalog&quot;.&quot;varchar&quot; COLLATE &quot;pg_catalog&quot;.&quot;default&quot;,
postgres(#   &quot;PLUGIN_KEY&quot; &quot;pg_catalog&quot;.&quot;varchar&quot; COLLATE &quot;pg_catalog&quot;.&quot;default&quot;,
postgres(#   &quot;USER_KEY&quot; &quot;pg_catalog&quot;.&quot;varchar&quot; COLLATE &quot;pg_catalog&quot;.&quot;default&quot;
postgres(# )
postgres-# ;
CREATE TABLE
</pre>
<p>检查a/b的情况</p>
<pre class="brush: sql; title: ; notranslate">
postgres=# select count(1) from &quot;AO_6384AB_DISCOVERED&quot;;
 count 
-------
   544
(1 row)

postgres=# select count(1) from &quot;AO_6384AB_DISCOVERED1&quot;;
 count 
-------
     0
(1 row)

postgres=# \d &quot;AO_6384AB_DISCOVERED&quot;
                    Table &quot;public.AO_6384AB_DISCOVERED&quot;
   Column   |            Type             | Collation | Nullable | Default 
------------+-----------------------------+-----------+----------+---------
 DATE       | timestamp without time zone |           |          | 
 ID         | integer                     |           |          | 
 KEY        | character varying           |           |          | 
 PLUGIN_KEY | character varying           |           |          | 
 USER_KEY   | character varying           |           |          | 

postgres=# \d &quot;AO_6384AB_DISCOVERED1&quot;
                   Table &quot;public.AO_6384AB_DISCOVERED1&quot;
   Column   |            Type             | Collation | Nullable | Default 
------------+-----------------------------+-----------+----------+---------
 DATE       | timestamp without time zone |           |          | 
 ID         | integer                     |           |          | 
 KEY        | character varying           |           |          | 
 PLUGIN_KEY | character varying           |           |          | 
 USER_KEY   | character varying           |           |          | 
</pre>
<p>存在数据的a/b表相关oid信息</p>
<pre class="brush: sql; title: ; notranslate">


postgres=# select oid,relname,relfilenode from pg_class where relname like '%AO_6384AB_DISCOVERED%';
  oid  |        relname        | relfilenode 
-------+-----------------------+-------------
 16516 | AO_6384AB_DISCOVERED  |       16516
 17069 | AO_6384AB_DISCOVERED1 |       17069
(2 rows)

postgres=# select oid,relname,relfilenode from pg_class where relname like '%16516%' or  relname like '%17069%'  ;
  oid  |       relname        | relfilenode 
-------+----------------------+-------------
 16519 | pg_toast_16516       |       16519
 16521 | pg_toast_16516_index |       16521
 17072 | pg_toast_17069       |       17072
 17074 | pg_toast_17069_index |       17074
(4 rows)
</pre>
<p>关闭数据库把a表相关的oid文件拷贝替换b表的oid文件</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@xifenfei 13676]$ pg_ctl stop -D /pgdata
waiting for server to shut down....2025-12-12 20:00:58.804 HKT [21445] LOG:  received fast shutdown request
2025-12-12 20:00:58.805 HKT [21445] LOG:  aborting any active transactions
2025-12-12 20:00:58.805 HKT [21471] FATAL:  terminating connection due to administrator command
2025-12-12 20:00:58.805 HKT [21473] FATAL:  terminating connection due to administrator command
2025-12-12 20:00:58.805 HKT [21856] FATAL:  terminating connection due to administrator command
2025-12-12 20:00:58.806 HKT [21472] FATAL:  terminating connection due to administrator command
2025-12-12 20:00:58.806 HKT [21445] LOG: background worker&quot;logical replication launcher&quot;(PID 252)exited with exit code 1
2025-12-12 20:00:58.807 HKT [21447] LOG:  shutting down
2025-12-12 20:00:58.811 HKT [21445] LOG:  database system is shut down
 done
server stopped
[postgres@xifenfei 13676]$ cp  16516 17069
[postgres@xifenfei 13676]$ cp  16519 17072
[postgres@xifenfei 13676]$ cp  16521 17074
[postgres@xifenfei 13676]$ pg_ctl start -D /pgdata
waiting for server to start....2025-12-12 20:02:05.985 HKT [22241] LOG:  
starting PostgreSQL 12.8 on x86_64-pc-linux-gnu, 
compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-26.0.1), 64-bit
2025-12-12 20:02:05.985 HKT [22241] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
2025-12-12 20:02:05.986 HKT [22241] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
2025-12-12 20:02:05.986 HKT [22241] LOG:  listening on Unix socket &quot;/tmp/.s.PGSQL.5432&quot;
2025-12-12 20:02:05.993 HKT [22242] LOG:  database system was shut down at 2025-12-12 20:00:58 HKT
2025-12-12 20:02:05.995 HKT [22241] LOG:  database system is ready to accept connections
 done
server started
</pre>
<p>查询a/b表数据</p>
<pre class="brush: sql; title: ; notranslate">
[postgres@xifenfei 13676]$ psql
psql (12.8)
Type &quot;help&quot; for help.

postgres=# select count(1) from &quot;AO_6384AB_DISCOVERED1&quot;;
 count 
-------
   544
(1 row)

postgres=# select count(1) from &quot;AO_6384AB_DISCOVERED&quot;;
 count 
-------
   544
(1 row)
</pre>
<p>通过上述从测试,证明创建一个新的表结构,完全可以访问老的oid文件中的数据</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/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/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/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/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/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/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/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/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/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_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/2010/08/fckeditor%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e.html" rel="bookmark" class="crp_title">fckeditor使用说明</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/12/pg-replace-oid-recovery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>由于空间满导致PostgreSQL数据库异常处理</title>
		<link>https://www.xifenfei.com/2025/07/no-space-left-on-device-postgresql.html</link>
		<comments>https://www.xifenfei.com/2025/07/no-space-left-on-device-postgresql.html#comments</comments>
		<pubDate>Tue, 29 Jul 2025 15:36:09 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[pg异常处理]]></category>
		<category><![CDATA[pg恢复支持]]></category>
		<category><![CDATA[postgres恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11562</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/07/no-space-left-on-device-postgresql.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/07/no-space-left-on-device-postgresql.html" target="_blank">由于空间满导致PostgreSQL数据库异常处理</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>朋友和我反馈pg库异常,通过查看日志确认最初故障是由于磁盘空间满，导出出现类似:无法扩展文件 &#8220;pg_tblspc/16394/PG_13_202007201/16395/5055912.143&#8243;: No space left on device错误</p>
<pre class="brush: bash; title: ; notranslate">
2025-07-28 09:06:02.703 HKT [15352] 错误:  无法扩展文件 &quot;pg_tblspc/16394/PG_13_202007201/16395/5055912.143&quot;: No space left on device
2025-07-28 09:06:02.703 HKT [15352] 提示:  检查空闲磁盘控件.
2025-07-28 09:06:02.703 HKT [15352] 语句:  insert into 语句
2025-07-28 09:06:02.703 HKT [576] 错误:  无法扩展文件 &quot;pg_tblspc/16394/PG_13_202007201/16395/4477723.73&quot;: No space left on device
2025-07-28 09:06:02.703 HKT [576] 提示:  检查空闲磁盘控件.
2025-07-28 09:06:02.703 HKT [576] 语句:  update 语句
2025-07-28 09:06:02.706 HKT [11940] 错误:  无法扩展文件 &quot;pg_tblspc/16394/PG_13_202007201/16395/5055912.143&quot;: No space left on device
2025-07-28 09:06:02.706 HKT [11940] 提示:  检查空闲磁盘控件.
2025-07-28 09:06:02.706 HKT [11940] 语句:  insert into 语句
</pre>
<p><a href="https://www.xifenfei.com/wp-content/uploads/2025/07/pg-nofree.png"><img src="https://www.xifenfei.com/wp-content/uploads/2025/07/pg-nofree.png" alt="pg-nofree" width="846" height="481" class="alignleft size-full wp-image-11563" /></a><br />
<hr />
后续对d盘空间进行了清理,pg报无法打开文件&#8221;pg_tblspc/16394/PG_13_202007201/16395/4477706.16&#8243;(目标数据块5544906): Permission denied</p>
<pre class="brush: bash; title: ; notranslate">
2025-07-28 10:53:10.435 HKT [11920] 语句:  insert into语句
2025-07-28 10:53:10.435 HKT [18300] 错误:  无法打开文件&quot;pg_tblspc/16394/PG_13_202007201/16395/4477706.16&quot;(目标数据块5544906): Permission denied
</pre>
<p>和报:警告:  无法写入pg_tblspc/16394/PG_13_202007201/16395/4477706的块5545387错误</p>
<pre class="brush: bash; title: ; notranslate">
2025-07-28 11:05:11.008 HKT [5380] 警告:  无法写入pg_tblspc/16394/PG_13_202007201/16395/4477706的块5545387
2025-07-28 11:05:11.008 HKT [5380] 详细信息:  多次失败 --- 写错误可能是永久性的
2025-07-28 11:05:11.015 HKT [12072] 错误:  无法打开文件&quot;pg_tblspc/16394/PG_13_202007201/16395/4477706.17&quot;（目标块5545398）：上一段只有952个块
2025-07-28 11:05:11.015 HKT [12072] 上下文:  写入关系pg_tblspc/16394/PG_13_202007201/16395/4477706的块5545398
2025-07-28 11:05:11.015 HKT [12072] 语句:  select 语句
2025-07-28 11:05:11.016 HKT [12072] 警告:  无法写入pg_tblspc/16394/PG_13_202007201/16395/4477706的块5545398
2025-07-28 11:05:11.016 HKT [12072] 详细信息:  多次失败 --- 写错误可能是永久性的
</pre>
<p>查看4477706相关文件情况,初步看部分文件大小不对,结合上述报错,可以断定该表异常<br />
<a href="https://www.xifenfei.com/wp-content/uploads/2025/07/pgsize.png"><img src="https://www.xifenfei.com/wp-content/uploads/2025/07/pgsize.png" alt="pgsize" width="507" height="323" class="alignleft size-full wp-image-11564" /></a><br />
<hr />
通过查询pg字典确认该表具体名称</p>
<pre class="brush: sql; title: ; notranslate">
xifenfei=# select oid,relname,relnamespace from pg_class where  relfilenode=4477706;
  oid  |     relname      | relnamespace
-------+------------------+--------------
 16880 | xifenfei01_log   |         2200
(1 行记录)
</pre>
<p>尝试pg_dump导入数据报错：多次失败 &#8212; 写错误可能是永久性的</p>
<p>这次运气比较好,损坏的对象是一个日志表,可以直接清理掉,对该日志表进行清理,然后正常导出数据,如果遇到的损坏表是业务需要的表,可以使用pdu(<a href="https://www.xifenfei.com/2025/03/pdu-recovery-corrupt-block.html" title="PostgreSQL表文件损坏恢复—pdu恢复损坏的表文件" rel="noopener" target="_blank">PostgreSQL表文件损坏恢复—pdu恢复损坏的表文件</a>)进行恢复</p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/12/sql-bak-0-lost-data.html" rel="bookmark" class="crp_title">模拟sql server故障备份完成恢复实现数据0丢失</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/10/linux7-8-single-user.html" rel="bookmark" class="crp_title">redhat系列7/8进入单用户模式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pg-delete-recovery.html" rel="bookmark" class="crp_title">pg误删除数据恢复（PostgreSQL delete数据恢复）</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/10/grubx64-efi-not-found.html" rel="bookmark" class="crp_title">Failed to open \EFI\redhat\grubx64.efi &#8211; Not Found 故障处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/01/ar8mswin1256.html" rel="bookmark" class="crp_title">中文环境显示AR8MSWIN1256（阿拉伯语字符集）</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/11/prvg-11975.html" rel="bookmark" class="crp_title">PRVG-11975 : The I/O scheduler parameter of device &#8220;/dev/sdm&#8221; did not match the expected value on nodes</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/09/delete-df-some-overwrit-recovery.html" rel="bookmark" class="crp_title">删除数据库文件并部分覆盖情况下Oracle恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/01/fio-disk-destruction-recovery.html" rel="bookmark" class="crp_title">fio测试io,导致磁盘文件系统损坏故障恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/12/ufs-recovery-lvm.html" rel="bookmark" class="crp_title">linux异常磁盘lvm恢复操作演示</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/04/sql-tempdb-error.html" rel="bookmark" class="crp_title">可能是 tempdb 空间用尽或某个系统表不一致故障处理</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/2025/01/first-oracle-21c-recovery-ask.html" rel="bookmark" class="crp_title">第一例Oracle 21c恢复咨询</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/07/no-space-left-on-device-postgresql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>pg_control丢失/损坏处理</title>
		<link>https://www.xifenfei.com/2025/03/pg_control-rebuild.html</link>
		<comments>https://www.xifenfei.com/2025/03/pg_control-rebuild.html#comments</comments>
		<pubDate>Tue, 11 Mar 2025 00:38:35 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[pg_control丢失恢复]]></category>
		<category><![CDATA[pg_control异常恢复]]></category>
		<category><![CDATA[pg_control强制启动]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11321</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/03/pg_control-rebuild.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/pg_control-rebuild.html" target="_blank">pg_control丢失/损坏处理</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p><strong>-l ：设置下一个wal日志名</strong><br />
可以通过 $PGDATA/pg_wal 目录中查找数值最新的WAL段的文件名+1</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost pg_wal]$ ls
00000001000000000000008E  archive_status

-l 00000001000000000000008F
</pre>
<p><strong>-m：设置下一个和最旧的事务ID</strong><br />
mxid1：下一个多事务ID的安全值可以通过在 $PGDATA/pg_multixact/offsets目录中查找数值最大的文件名+1，然后乘以65536（0&#215;10000）来确定。<br />
mxid2：最旧的事务ID的安全值可以通过$PGDATA/pg_multixact/offsets目录中数字最小的文件名+1，乘以65536（0&#215;10000）来确定。文件名是十六进制</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost pg_wal]$ cd ../pg_multixact
[postgres@localhost pg_multixact]$ ls
members  offsets
[postgres@localhost pg_multixact]$ cd offsets/
[postgres@localhost offsets]$ ls
0000

-m 0x10000,0x10000
</pre>
<p><strong>-O：设置下一个多事务处理偏移量</strong><br />
安全值可以通过在 $PGDATA/pg_multixact/members 目录中查找数值最大的文件名，+1，然后乘以52352（0xCC80）来确定。文件名为十六进制</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost members]$ ls
0000

-O 0xCC80
</pre>
<p><strong>-x：设置下一个事务ID</strong><br />
安全值可以通过在$PGDATA/pg_xact目录中查找数值最大的文件名，+1，然后乘以1048576（0&#215;100000）来确定。请注意，文件名是十六进制</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost pg_xact]$ ls -ltr
total 504
-rw------- 1 postgres postgres 262144 Mar  8 01:53 0000
-rw------- 1 postgres postgres 253952 Mar  9 10:54 0001

-x 0x200000
</pre>
<p><strong>删除postmaster.pid文件</strong><br />
如果正常关闭库,该文件会被自动删除,异常关闭的才需要处理</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost pg_wal]$ pg_resetwal -l 00000001000000000000008F -m 0x10000,0x10000 -O 0xCC80 -x 0x200000 -f $PGDATA
pg_resetwal: error: lock file &quot;postmaster.pid&quot; exists
pg_resetwal: hint: Is a server running?  If not, delete the lock file and try again.
[postgres@localhost pg_wal]$ cd ../
[postgres@localhost data]$ ls -l postmaster.pid
-rw------- 1 postgres postgres 75 Mar  9 11:02 postmaster.pid
[postgres@localhost data]$ rm -rf postmaster.pid
</pre>
<p><strong>touch pg_control文件</strong></p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost data]$ pg_resetwal -l 00000001000000000000008F -m 0x10000,0x10000 -O 0xCC80 -x 0x200000 -f $PGDATA
pg_resetwal: error: could not open file &quot;global/pg_control&quot; for reading: No such file or directory
pg_resetwal: hint: If you are sure the data directory path is correct, execute
  touch global/pg_control
and try again.
[postgres@localhost data]$ touch global/pg_control
</pre>
<p><strong>重建pg_control</strong></p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost data]$ pg_resetwal -l 00000001000000000000008F -m 0x10000,0x10000 -O 0xCC80 -x 0x200000 -f $PGDATA
pg_resetwal: warning: pg_control exists but is broken or wrong version; ignoring it
Write-ahead log reset
[postgres@localhost data]$ pg_ctl start
waiting for server to start....
 done
server started
</pre>
<div id="crp_related"><ul><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/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/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/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/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/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/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/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/2024/04/postgresql-walminer.html" rel="bookmark" class="crp_title">PostgreSQL解析wal日志之&#8212;walminer</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/08/pg_wal_filename-logseq-mismatch.html" rel="bookmark" class="crp_title">pg_wal中文件的名称中的logseq和实际文件中的logseq不匹配</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></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/03/pg_control-rebuild.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>pg启动报invalid checkpoint record处理</title>
		<link>https://www.xifenfei.com/2025/03/invalid-checkpoint-record.html</link>
		<comments>https://www.xifenfei.com/2025/03/invalid-checkpoint-record.html#comments</comments>
		<pubDate>Sun, 09 Mar 2025 15:36:03 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[invalid checkpoint record]]></category>
		<category><![CDATA[pg启动异常处理]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11318</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/03/invalid-checkpoint-record.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/invalid-checkpoint-record.html" target="_blank">pg启动报invalid checkpoint record处理</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>pg库启动报PANIC:  could not locate a valid checkpoint record错误</p>
<pre class="brush: bash; title: ; notranslate">
2025-03-09 10:59:10.365 EDT [73013] LOG:  starting PostgreSQL 16.8 on x86_64-pc-linux-gnu, 
                                          compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
2025-03-09 10:59:10.365 EDT [73013] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
2025-03-09 10:59:10.365 EDT [73013] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
2025-03-09 10:59:10.367 EDT [73013] LOG:  listening on Unix socket &quot;/tmp/.s.PGSQL.5432&quot;
2025-03-09 10:59:10.401 EDT [73018] LOG:  database system was interrupted; last known up at 2025-03-09 10:54:43 EDT
2025-03-09 10:59:11.506 EDT [73018] LOG:  invalid checkpoint record
2025-03-09 10:59:11.508 EDT [73018] PANIC:  could not locate a valid checkpoint record
2025-03-09 10:59:12.004 EDT [73013] LOG:  startup process (PID 73018) was terminated by signal 6: Aborted
2025-03-09 10:59:12.004 EDT [73013] LOG:  aborting startup due to startup process failure
2025-03-09 10:59:12.006 EDT [73013] LOG:  database system is shut down
</pre>
<p>从报错信息中看,是有无法读取到有效的checkpoint记录导致,初步怀疑是wal异常,检查pg_wal目录,发现wal日志为空</p>
<pre class="brush: bash; title: ; notranslate">
[root@localhost pg_wal]# ls -ltr
total 16388
drwx------  2 postgres postgres        6 Mar  6 08:10 archive_status
[root@localhost pg_wal]# 
</pre>
<p>进一步检查系统操作命令rm删除wal日志命令</p>
<pre class="brush: bash; title: ; notranslate">
cd pg_wal
rm -rf 0000000*
</pre>
<p>初步确认是由于wal日志被意外删除导致pg库无法启动,尝试重置wal,由于库不是干净关闭,无法直接重置成功</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost log]$ pg_resetwal $PGDATA
The database server was not shut down cleanly.
Resetting the write-ahead log might cause data to be lost.
If you want to proceed anyway, use -f to force reset.
</pre>
<p>使用pg_resetwal  -f强制重置</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost log]$ pg_resetwal -f $PGDATA
Write-ahead log reset
</pre>
<p>尝试启动pg库成功</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost log]$ pg_ctl start 
waiting for server to start....2025-03-09 11:02:02.609 EDT [73088] LOG:  
redirecting log output to logging collector process
2025-03-09 11:02:02.609 EDT [73088] HINT:  Future log output will appear in directory &quot;log&quot;.
 done
server started
</pre>
<pre class="brush: bash; title: ; notranslate">
2025-03-09 11:02:02.609 EDT [73088] LOG:  starting PostgreSQL 16.8 on x86_64-pc-linux-gnu, 
                                          compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
2025-03-09 11:02:02.609 EDT [73088] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
2025-03-09 11:02:02.609 EDT [73088] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
2025-03-09 11:02:02.610 EDT [73088] LOG:  listening on Unix socket &quot;/tmp/.s.PGSQL.5432&quot;
2025-03-09 11:02:02.645 EDT [73092] LOG:  database system was shut down at 2025-03-09 11:01:53 EDT
2025-03-09 11:02:02.650 EDT [73088] LOG:  database system is ready to accept connections
</pre>
<div id="crp_related"><ul><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/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/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/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/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/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/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/08/pg_wal_filename-logseq-mismatch.html" rel="bookmark" class="crp_title">pg_wal中文件的名称中的logseq和实际文件中的logseq不匹配</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/07/no-space-left-on-device-postgresql.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-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/02/connect-pdb-sid-use_sid_as_service_listener.html" rel="bookmark" class="crp_title">使用sid方式直接访问pdb(USE_SID_AS_SERVICE_LISTENER)</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/03/invalid-checkpoint-record.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>pg误删除数据恢复（PostgreSQL delete数据恢复）</title>
		<link>https://www.xifenfei.com/2025/03/pg-delete-recovery.html</link>
		<comments>https://www.xifenfei.com/2025/03/pg-delete-recovery.html#comments</comments>
		<pubDate>Thu, 06 Mar 2025 15:24:52 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=11281</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2025/03/pg-delete-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/pg-delete-recovery.html" target="_blank">pg误删除数据恢复（PostgreSQL delete数据恢复）</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>应用维护人员通过应用程序误删除了一些数据,希望对其进行恢复.通过咨询确认是PostgreSQL数据,wal和arch文件都在,取客户删除数据时间点相关wal文件<br />
<img src="https://www.xifenfei.com/wp-content/uploads/2025/03/QQ20250306-231208.png" alt="QQ20250306-231208" width="733" height="300" class="alignleft size-full wp-image-11283" /><br />
<hr />
然后让客户提供具体涉及误删除的相关的库和表信息<br />
<img src="https://www.xifenfei.com/wp-content/uploads/2025/03/QQ20250306-230846.png" alt="QQ20250306-230846" width="660" height="481" class="alignleft size-full wp-image-11282" /><br />
<hr />
通过工具解析wal日志,比较顺利的获取到了undo sql语句</p>
<pre class="brush: bash; title: ; notranslate">
Switch wal to 0000000300000525000000B0 on time 2025-03-05 22:23:14.263661+08
Switch wal to 0000000300000525000000B1 on time 2025-03-05 22:23:14.423082+08
Switch wal to 0000000300000525000000B2 on time 2025-03-05 22:23:15.983833+08
Switch wal to 0000000300000525000000B3 on time 2025-03-05 22:23:17.802107+08
Switch wal to 0000000300000525000000B4 on time 2025-03-05 22:23:18.942125+08
Switch wal to 0000000300000525000000B5 on time 2025-03-05 22:23:20.293585+08
Switch wal to 0000000300000525000000B6 on time 2025-03-05 22:23:21.531484+08
Switch wal to 0000000300000525000000B7 on time 2025-03-05 22:23:24.217501+08
Switch wal to 0000000300000525000000B8 on time 2025-03-05 22:23:27.504164+08
Switch wal to 0000000300000525000000B9 on time 2025-03-05 22:23:29.260754+08
Switch wal to 0000000300000525000000BA on time 2025-03-05 22:23:33.544486+08
Switch wal to 0000000300000525000000BB on time 2025-03-05 22:23:35.568116+08
Switch wal to 0000000300000525000000BC on time 2025-03-05 22:23:37.329659+08
Switch wal to 0000000300000525000000BD on time 2025-03-05 22:23:38.971834+08
Switch wal to 0000000300000525000000BE on time 2025-03-05 22:23:39.922353+08
Switch wal to 0000000300000525000000BF on time 2025-03-05 22:23:40.897294+08
</pre>
<p><img src="https://www.xifenfei.com/wp-content/uploads/2025/03/QQ20250306-231657.png" alt="QQ20250306-231657" width="1262" height="636" class="alignleft size-full wp-image-11284" /><br />
<hr />
然后把这些sql语句反向插入到生产库,完成这些误操作数据的恢复<br />
<img src="https://www.xifenfei.com/wp-content/uploads/2025/03/QQ20250306-231935.png" alt="QQ20250306-231935" width="567" height="235" class="alignleft size-full wp-image-11285" /><br />
<hr />
如果此类的数据库(oracle,mysql,sql server,PostgreSQL)等恢复请求,需要专业恢复技术支持，请联系我们:<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/03/vmdk-recovery.html" rel="bookmark" class="crp_title">VMware勒索加密恢复(vmdk勒索恢复)</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/12/sql-bak-0-lost-data.html" rel="bookmark" class="crp_title">模拟sql server故障备份完成恢复实现数据0丢失</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/11/mysql-drop-database-recovery.html" rel="bookmark" class="crp_title">mysql drop database 恢复思路</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/10/linux7-8-single-user.html" rel="bookmark" class="crp_title">redhat系列7/8进入单用户模式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/09/mysql-recovery-2-case.html" rel="bookmark" class="crp_title">docker回收和mysql备份导入导致数据丢失恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/04/sql-tempdb-error.html" rel="bookmark" class="crp_title">可能是 tempdb 空间用尽或某个系统表不一致故障处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/01/ar8mswin1256.html" rel="bookmark" class="crp_title">中文环境显示AR8MSWIN1256（阿拉伯语字符集）</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/12/ufs-recovery-lvm.html" rel="bookmark" class="crp_title">linux异常磁盘lvm恢复操作演示</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/10/grubx64-efi-not-found.html" rel="bookmark" class="crp_title">Failed to open \EFI\redhat\grubx64.efi &#8211; Not Found 故障处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/01/fio-disk-destruction-recovery.html" rel="bookmark" class="crp_title">fio测试io,导致磁盘文件系统损坏故障恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/02/sql-server-recovering.html" rel="bookmark" class="crp_title">sql server数据库“正在恢复”故障处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/09/delete-df-some-overwrit-recovery.html" rel="bookmark" class="crp_title">删除数据库文件并部分覆盖情况下Oracle恢复</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2025/03/pg-delete-recovery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQL解析wal日志之&#8212;walminer</title>
		<link>https://www.xifenfei.com/2024/04/postgresql-walminer.html</link>
		<comments>https://www.xifenfei.com/2024/04/postgresql-walminer.html#comments</comments>
		<pubDate>Thu, 25 Apr 2024 15:32:31 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[pg解析wal]]></category>
		<category><![CDATA[pg解析日志]]></category>
		<category><![CDATA[walminer]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=10718</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2024/04/postgresql-walminer.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/2024/04/postgresql-walminer.html" target="_blank">PostgreSQL解析wal日志之&#8212;walminer</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>在oracle数据库中可以通过logminer实现对归档日志的解析，从而分析执行sql语句和undo sql,可以实现某些情况下数据库一些操作的定位（比如日志突然增加，数据突然丢失）以及一些故障的恢复（比如需要把update/delete执行的数据找回）等。在PostgreSQL数据库中walminer可以实现该需求，对pg的预写式日志(wal)的解析,具体见官网:https://gitee.com/movead/XLogMiner/<br />
<strong>walminer安装</strong></p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost tmp]$ ls -l walminer_x86_64_centos_v4.6.0.tar.gz
-rw-r--r--. 1 root root 3866437 Apr 18 10:08 walminer_x86_64_centos_v4.6.0.tar.gz
[postgres@localhost tmp]$ tar xzvf walminer_x86_64_centos_v4.6.0.tar.gz 
walminer_x86_64_centos_v4.6.0/
walminer_x86_64_centos_v4.6.0/bin/
walminer_x86_64_centos_v4.6.0/bin/walminer
walminer_x86_64_centos_v4.6.0/lib/
walminer_x86_64_centos_v4.6.0/lib/libpq.so.5.15
walminer_x86_64_centos_v4.6.0/lib/libpq.so.5
walminer_x86_64_centos_v4.6.0/lib/libpq.so
walminer_x86_64_centos_v4.6.0/share/
…………
[root@localhost ~]# mkdir -p /usr/local/walminer/
[root@localhost ~]# chown postgres:postgres  /usr/local/walminer/
[root@localhost ~]# cp /tmp/walminer/walminer.license /usr/local/walminer/
[postgres@localhost bin]$ cd /tmp/walminer
[postgres@localhost walminer]$ cp -rp * /usr/local/walminer/
[postgres@localhost bin]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/walminer/
[postgres@localhost bin]$ cd /usr/local/walminer/bin
[postgres@localhost bin]$ ./walminer  help
walminer [command] [options]
COMMANDS
---------
#wal2sql
  options
    -D dic file for miner
    -a out detail info for catalog change
    -w wal file path to miner
    -t dest of miner result(1 stdout, 2 file, 3 db)(stdout default)
    -k boundary kind(1 all, 2 lsn, 3 time, 4 xid)(all default)
    -m miner mode(0 nomal miner, 1 accurate miner)(nomal default) if k=2
    -r the relname for single table miner 
    -s start location if k=2 or k=3, or xid if k = 4 
          if k=2 default the min lsn of input wals   
          if k=3 or k=4 you need input this
    -e end wal location if k=2 or k=3
          if k=2 default the max lsn of input wals   
          if k=3 you need input this
    -f file to store miner result if t = 2
    -d target database name if t=3(default postgres)
    -h target database host if t=3(default localhost)
    -p target database port if t=3(default 5432)
    -u target database user if t=3(default postgres)
    -W target user password if t=3
---------
#builtdic
  options
    -d target database name for connect(default postgres)
    -h target database host(default localhost)
    -p target database port(default 5432)
    -u target database user(default postgres)
    -W target user password
    -D dic produce path
    -f rewrite walminer dic if exists
    -s only database pointed by -d
---------
#showdic
  options
    -D dic file to show
---------
#avatardic
  options
    -r avatar rel that new created
    -n avatared relfilenode
    -D avatared walminer dic path
    -b target database name which contain rel pointed by -r
---------
#regress(not support for user)
  options
    -w test database wal path(default postgres)
    -d test database name(default postgres)
    -h test database host(default localhost)
    -p test database port(default 5432)
    -u test database user(default postgres)
    -P apply database port 
    -W test user password
---------
#fosync
  options
    -D dic file for miner
    -w wal file path to miner
    -t dest of miner result(1 stdout, 2 file, 3 db, 4 apply)(stdout default)
    -f file to store miner result if t = 2
    -l lsn it start fync
    -d target database name if t=3 or 4(default postgres)
    -h target database host if t=3 or 4(default localhost)
    -p target database port if t=3 or 4(default 5432)
    -u target database user if t=3 or 4(default postgres)
    -W target user password if t=3 or 4
---------
#pgto
  options
    -c configure path
    -i to init a CDC configure
    -r to run a CDC configure
    Below is needed when -i
    -d source database name(default postgres)
    -h source database host(default localhost)
    -p source database port(default 5432)
    -u source database user(default postgres)
    -w source user password
    -D target database name
    -H target database host
    -P target database port
    -U target database user
    -W target user password
    -K target database type(1 postgres) (support postgres only currently)
    -s slot name need for CDC
---------
#waldump
  options
    -D dic file for miner
    -w wal file path to dump
    -t dest of miner result(1 stdout, 2 file)(stdout default)
    -s start lsn to dump
    -e end lsn to dump
    -f file to store miner result if t = 2
    -v verbose
---------
#################################################

[postgres@localhost bin]$ 
</pre>
<p><strong>postgresql创建测试表和插入数据</strong></p>
<pre class="brush: sql; title: ; notranslate">
[postgres@localhost ~]$ psql
psql (16.2)
Type &quot;help&quot; for help.

postgres=# select now();
              now              
-------------------------------
 2024-04-25 10:48:00.602067-04
(1 row)

postgres=# 
postgres=# create table t_walminer(id int,name varchar(100));
CREATE TABLE
postgres=# insert into t_walminer values(1,'www.xifenfei.com');
INSERT 0 1
postgres=# insert into t_walminer values(2,'www.orasos.com');
INSERT 0 1
postgres=# insert into t_walminer values(3,'xifenfei');
INSERT 0 1
postgres=# select * from t_walminer;
 id |       name       
----+------------------
  1 | www.xifenfei.com
  2 | www.orasos.com
  3 | xifenfei
(3 rows)

postgres=# select now();
              now              
-------------------------------
 2024-04-25 10:49:47.036881-04
(1 row)
postgres=# select pg_switch_wal();
 pg_switch_wal 
---------------
 0/D5023E8
(1 row)
</pre>
<p><strong>walminer 生成字典</strong></p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost bin]$ ./walminer builtdic -D /usr/local/walminer/xifenfei.dic
#################################################
Walminer for PostgreSQL wal
Contact Author by mail 'lchch1990@sina.cn'
Persional License for posgress
#################################################
DIC INFO#
sysid:7357852038421105818 timeline:1 dbversion:160002 walminer:4.6
</pre>
<p>walminer解析这个时间段wal操作</p>
<pre class="brush: bash; title: ; notranslate">
[postgres@localhost bin]$  ./walminer wal2sql -D /usr/local/walminer/xifenfei.dic -w /pg/database/data/pg_arch \
 -k 3 -s 2024-04-24 -e 2024-04-26
#################################################
Walminer for PostgreSQL wal
Contact Author by mail 'lchch1990@sina.cn'
Persional License for posgress
#################################################
Switch wal to /pg/database/data/pg_arch/000000010000000000000001 on time 2024-04-25 23:27:07.42721+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000002 on time 2024-04-25 23:27:07.45369+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000003 on time 2024-04-25 23:27:07.453891+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000004 on time 2024-04-25 23:27:07.486403+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000005 on time 2024-04-25 23:27:07.513144+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000006 on time 2024-04-25 23:27:07.538212+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000007 on time 2024-04-25 23:27:07.561455+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000008 on time 2024-04-25 23:27:07.584488+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000009 on time 2024-04-25 23:27:07.606598+08
Switch wal to /pg/database/data/pg_arch/00000001000000000000000A on time 2024-04-25 23:27:07.609195+08
Switch wal to /pg/database/data/pg_arch/00000001000000000000000B on time 2024-04-25 23:27:07.609344+08
Switch wal to /pg/database/data/pg_arch/00000001000000000000000C on time 2024-04-25 23:27:07.609364+08
Switch wal to /pg/database/data/pg_arch/00000001000000000000000D on time 2024-04-25 23:27:07.66233+08
Switch wal to /pg/database/data/pg_arch/00000001000000000000000E on time 2024-04-25 23:27:07.684666+08
Switch wal to /pg/database/data/pg_arch/00000001000000000000000F on time 2024-04-25 23:27:07.684877+08
Switch wal to /pg/database/data/pg_arch/000000010000000000000001 on time 2024-04-25 23:27:07.684899+08
Get start lsn 0/d4eb380 for time range
Switch wal to /pg/database/data/pg_arch/00000001000000000000000D on time 2024-04-25 23:27:07.694947+08
[XID]=425507, [TOPXID]=0
[SQLNO]=1
[SQL]=INSERT INTO public.t_walminer(id ,name) VALUES(1 ,'www.xifenfei.com')
[UNDO]=DELETE FROM public.t_walminer WHERE id=1 AND name='www.xifenfei.com'
[database]=postgres
[COMPLETE]=true
[LSN]=0/d5021c8
[COMMITLSN]=0/d502218
[COMMITTIME]=2024-04-25 22:48:55.775279+08
------------------------------------------------------
[XID]=425508, [TOPXID]=0
[SQLNO]=1
[SQL]=INSERT INTO public.t_walminer(id ,name) VALUES(2 ,'www.orasos.com')
[UNDO]=DELETE FROM public.t_walminer WHERE id=2 AND name='www.orasos.com'
[database]=postgres
[COMPLETE]=true
[LSN]=0/d502278
[COMMITLSN]=0/d5022c8
[COMMITTIME]=2024-04-25 22:49:10.769752+08
------------------------------------------------------
[XID]=425509, [TOPXID]=0
[SQLNO]=1
[SQL]=INSERT INTO public.t_walminer(id ,name) VALUES(3 ,'xifenfei')
[UNDO]=DELETE FROM public.t_walminer WHERE id=3 AND name='xifenfei'
[database]=postgres
[COMPLETE]=true
[LSN]=0/d502328
[COMMITLSN]=0/d502370
[COMMITTIME]=2024-04-25 22:49:23.382642+08
------------------------------------------------------
Switch wal to /pg/database/data/pg_arch/00000001000000000000000E on time 2024-04-25 23:27:07.696041+08
Switch wal to /pg/database/data/pg_arch/00000001000000000000000F on time 2024-04-25 23:27:07.696062+08
[postgres@localhost bin]$ 
</pre>
<p>通过上述测试证明walminer可以非常好的解析pg的wal日志</p>
<div id="crp_related"><ul><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/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/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/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/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/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/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/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/2024/04/postgresql-16-source-install.html" rel="bookmark" class="crp_title">PostgreSQL 16 源码安装</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/2024/04/pg_filedump_batch.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:pg_filedump批量处理</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2024/04/postgresql-walminer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQL恢复系列:pg_filedump批量处理</title>
		<link>https://www.xifenfei.com/2024/04/pg_filedump_batch.html</link>
		<comments>https://www.xifenfei.com/2024/04/pg_filedump_batch.html#comments</comments>
		<pubDate>Thu, 18 Apr 2024 13:11:35 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[pg_filedump]]></category>
		<category><![CDATA[pg_filedump恢复]]></category>
		<category><![CDATA[pg_filedump批量恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=10712</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2024/04/pg_filedump_batch.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/2024/04/pg_filedump_batch.html" target="_blank">PostgreSQL恢复系列:pg_filedump批量处理</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>pg_filedump工具使用起来比较麻烦,主要存在问题：<br />
1. 需要人工一个个枚举各个列类型无法实现批量恢复,参考以前写的<a href="https://www.xifenfei.com/2022/06/pg_filedump-recovery.html" title="PostgreSQL恢复系列:pg_filedump基本使用" rel="noopener" target="_blank">PostgreSQL恢复系列:pg_filedump基本使用</a><br />
2. 特别是在pg库无法正常运行的情况下,如果没有业务提供表创建语句,恢复基本上无法正常进行.<br />
基于这两个问题,在以前的文章中写过<a href="https://www.xifenfei.com/2024/04/pg_filedump-batch-recovery.html" title="PostgreSQL恢复系列:pg_filedump恢复字典构造" rel="noopener" target="_blank">PostgreSQL恢复系列:pg_filedump恢复字典构造</a>,为了解决上述的两个,弄了一个pg_filedump_batch脚本实现批量恢复需求<br />
<br />
在测试的pg库中创建了一些测试表,并查看部分表数据,便于对比后续恢复效果</p>
<pre class="brush: sql; title: ; notranslate">
postgres=# \d
             List of relations
 Schema |      Name      | Type  |  Owner   
--------+----------------+-------+----------
 public | t_tbs          | table | postgres
 public | t_xff          | table | postgres
 public | t_xff2         | table | postgres
 public | t_xff3         | table | postgres
 public | t_xff4         | table | postgres
 public | t_xifenfei     | table | postgres
 public | tab_attribute  | table | postgres
 public | tab_class      | table | postgres
 public | tab_database   | table | postgres
 public | tab_namespace  | table | postgres
 public | tab_tablespace | table | postgres
 public | tab_type       | table | postgres
(12 rows)

postgres=# select * from tab_database;
  oid  |   datname   | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoi
d | datfrozenxid | datminmxid | dattablespace 
-------+-------------+--------+----------+-------------+-------------+---------------+--------------+--------------+-------------
--+--------------+------------+---------------
 14187 | postgres    |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         1418
6 |          479 |          1 |          1663
 16403 | db_xff      |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         1418
6 |          479 |          1 |          1663
     1 | template1   |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | t            |           -1 |         1418
6 |          479 |          1 |          1663
 14186 | template0   |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | f            |           -1 |         1418
6 |          479 |          1 |          1663
 16407 | db_xifenfei |  16405 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         1418
6 |          479 |          1 |         16406
(5 rows)
postgres=# select count(1) from tab_class;
 count 
-------
   407
(1 row)

postgres=# select *from pg_tablespace;
  oid  |   spcname    | spcowner | spcacl | spcoptions 
-------+--------------+----------+--------+------------
  1663 | pg_default   |       10 |        | 
  1664 | pg_global    |       10 |        | 
 16406 | tbs_xifenfei |    16405 |        | 
(3 rows)
</pre>
<p>使用pg_filedump_bath脚本来实现批量恢复</p>
<pre class="brush: bash; title: ; notranslate">
[root@xifenfei tmp]# ./pg_filedump_batch recover --database-oid=14187  \
 --output-directory=/data/recovery --pgdata=/var/lib/pgsql/12/data
Recover tables in database with oid: 14187
LOG: starting to process table tab_attribute
LOG: starting to process table tab_class
LOG: starting to process table tab_database
LOG: starting to process table tab_namespace
LOG: starting to process table tab_tablespace
LOG: starting to process table tab_type
LOG: starting to process table t_tbs
LOG: starting to process table t_xff
LOG: starting to process table t_xff2
LOG: starting to process table t_xff3
LOG: starting to process table t_xff4
LOG: starting to process table t_xifenfei
Check dumps in /data/recovery
</pre>
<p>参考数据恢复</p>
<pre class="brush: bash; title: ; notranslate">
[root@xifenfei tmp]# cd /data/recovery/
[root@xifenfei recovery]# ls -ltr
total 156
-rw-r--r-- 1 root root 82797 Apr 18 20:35 recovered-14187-tab_attribute.csv
-rw-r--r-- 1 root root 31129 Apr 18 20:35 recovered-14187-tab_class.csv
-rw-r--r-- 1 root root   343 Apr 18 20:35 recovered-14187-tab_database.csv
-rw-r--r-- 1 root root   118 Apr 18 20:35 recovered-14187-tab_namespace.csv
-rw-r--r-- 1 root root    50 Apr 18 20:35 recovered-14187-tab_tablespace.csv
-rw-r--r-- 1 root root  7907 Apr 18 20:35 recovered-14187-tab_type.csv
-rw-r--r-- 1 root root     0 Apr 18 20:35 recovered-14187-t_tbs.csv
-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff.csv
-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff2.csv
-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff3.csv
-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff4.csv
-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xifenfei.csv
[root@xifenfei recovery]# cat recovered-14187-tab_database.csv
14187   postgres        10      6       en_US.UTF-8     en_US.UTF-8     f       t       -1      14186   479     1       1663
16403   db_xff  10      6       en_US.UTF-8     en_US.UTF-8     f       t       -1      14186   479     1       1663
1       template1       10      6       en_US.UTF-8     en_US.UTF-8     t       t       -1      14186   479     1       1663
14186   template0       10      6       en_US.UTF-8     en_US.UTF-8     t       f       -1      14186   479     1       1663
16407   db_xifenfei     16405   6       en_US.UTF-8     en_US.UTF-8     f       t       -1      14186   479     1       16406
[root@xifenfei recovery]# cat recovered-14187-tab_class.csv|wc -l
407
[root@xifenfei recovery]# cat recovered-14187-tab_tablespace.csv
1663    pg_default
1664    pg_global
16406   tbs_xifenfei
</pre>
<p>把pg_class恢复数据导入库中进行对比,证明恢复的数据完全正确</p>
<pre class="brush: sql; title: ; notranslate">
postgres=# COPY tab_class_new FROM '/data/recovery/recovered-14187-tab_class.csv';
COPY 407

postgres=# select count(1) from tab_class;
 count 
-------
   407
(1 row)

 count 
-------
   407
(1 row)

postgres=# select count(1) from tab_class_new;
 count 
-------
   407
(1 row)

postgres=# select * from tab_class_new 
postgres-# EXCEPT
postgres-# select * from tab_class;
 oid | relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | rel
allvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind 
-----+---------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+----
-----------+---------------+-------------+-------------+----------------+---------
(0 rows)

postgres=# select * from tab_class
postgres-# EXCEPT
postgres-# select * from tab_class_new;
 oid | relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | rel
allvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind 
-----+---------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+----
-----------+---------------+-------------+-------------+----------------+---------
(0 rows)

</pre>
<p>通过上述操作证明:<br />
1. 在没有人工列出列类型的情况下实现批量pg_filedump恢复功能<br />
2. 在pg库没有启动的情况下直接解析字典实现恢复功能<br />
3. 实现pg数据库的批量恢复<br />
如果有PostgreSQL的数据库故障,自行无法解决,请联系我们提供专业数据库恢复技术支持:<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/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/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/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/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/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/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/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/2024/04/postgresql-walminer.html" rel="bookmark" class="crp_title">PostgreSQL解析wal日志之&#8212;walminer</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/postgresql-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/2022/05/pg-wal-lost-recovery.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:wal日志丢失恢复</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2024/04/pg_filedump_batch.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQL恢复系列:pg_filedump恢复字典构造</title>
		<link>https://www.xifenfei.com/2024/04/pg_filedump-batch-recovery.html</link>
		<comments>https://www.xifenfei.com/2024/04/pg_filedump-batch-recovery.html#comments</comments>
		<pubDate>Mon, 15 Apr 2024 14:19:42 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL恢复]]></category>
		<category><![CDATA[pg_filedump恢复]]></category>
		<category><![CDATA[pg_filedump批量恢复]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=10703</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2024/04/pg_filedump-batch-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/2024/04/pg_filedump-batch-recovery.html" target="_blank">PostgreSQL恢复系列:pg_filedump恢复字典构造</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>pg_filedump是在pg数据库极端情况下直接解析数据库文件的利器,但是由于是开源软件,本身难以实现批量处理,通过对底层基表分析,可以实现批量处理功能<br />
<strong>分析PostgreSQL库中数据库信息</strong></p>
<pre class="brush: bash; title: ; notranslate">
--数据库查询结果
postgres=# select oid,datname,datdba,dattablespace from pg_database;
  oid  |   datname   | datdba | dattablespace 
-------+-------------+--------+---------------
 14187 | postgres    |     10 |          1663
 16403 | db_xff      |     10 |          1663
     1 | template1   |     10 |          1663
 14186 | template0   |     10 |          1663
 16407 | db_xifenfei |  16405 |         16406
(5 rows)

--通过dump 该文件解析数据
&lt;Data&gt; -----
 Item   1 -- Length:    0  Offset:    5 (0x0005)  Flags: REDIRECT
 Item   2 -- Length:    0  Offset:    6 (0x0006)  Flags: REDIRECT
 Item   3 -- Length:  260  Offset: 7320 (0x1c98)  Flags: NORMAL
COPY: 14187     postgres
 Item   4 -- Length:  260  Offset: 7056 (0x1b90)  Flags: NORMAL
COPY: 16403     db_xff
 Item   5 -- Length:  297  Offset: 7888 (0x1ed0)  Flags: NORMAL
COPY: 1 template1
 Item   6 -- Length:  297  Offset: 7584 (0x1da0)  Flags: NORMAL
COPY: 14186     template0
 Item   7 -- Length:  260  Offset: 6792 (0x1a88)  Flags: NORMAL
COPY: 16407     db_xifenfei
</pre>
<p><strong>分析PostgreSQL 表空间信息</strong></p>
<pre class="brush: bash; title: ; notranslate">
--sql查询表空间信息
postgres=# select * from pg_tablespace;
  oid  |   spcname    | spcowner | spcacl | spcoptions 
-------+--------------+----------+--------+------------
  1663 | pg_default   |       10 |        | 
  1664 | pg_global    |       10 |        | 
 16406 | tbs_xifenfei |    16405 |        | 
(3 rows)


--通过dump 该文件解析数据
&lt;Data&gt; -----
 Item   1 -- Length:   96  Offset: 8096 (0x1fa0)  Flags: NORMAL
COPY: 1663      pg_default
 Item   2 -- Length:   96  Offset: 8000 (0x1f40)  Flags: NORMAL
COPY: 1664      pg_global
 Item   3 -- Length:   96  Offset: 7904 (0x1ee0)  Flags: NORMAL
COPY: 16406     tbs_xifenfei
</pre>
<p><strong>分析PostgreSQL 对象id、name、path对应关系</strong></p>
<pre class="brush: bash; title: ; notranslate">
--对象信息查询
postgres=# select oid ,relname,relnamespace,reltype,reloftype,relowner,relam,relfilenode,
           reltablespace from pg_class where relname like 't_t%' or relname like 't_x%';
  oid  |  relname   | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace 
-------+------------+--------------+---------+-----------+----------+-------+-------------+---------------
 16387 | t_xifenfei |         2200 |   16389 |         0 |       10 |     2 |       16390 |             0
 16391 | t_xff      |         2200 |   16393 |         0 |       10 |     2 |       16391 |             0
 16394 | t_xff2     |         2200 |   16396 |         0 |       10 |     2 |       16394 |             0
 16397 | t_xff3     |         2200 |   16399 |         0 |       10 |     2 |       16397 |             0
 16400 | t_xff4     |         2200 |   16402 |         0 |       10 |     2 |       16400 |             0
 16408 | t_tbs      |         2200 |   16410 |         0 |       10 |     2 |       16408 |         16406
(6 rows)

--通过dump 该文件解析数据（显示部分）
COPY: 16394     t_xff2  2200    16396   0       10      2       16394   0       0       0.000000000000  0       0       f       f       p       r
 Item  29 -- Length:    0  Offset:   31 (0x001f)  Flags: REDIRECT
 Item  30 -- Length:  172  Offset: 2592 (0x0a20)  Flags: NORMAL
COPY: 16397     t_xff3  2200    16399   0       10      2       16397   0       0       0.000000000000  0       0       f       f       p       r
 Item  31 -- Length:  205  Offset: 3376 (0x0d30)  Flags: NORMAL
COPY: 12093     pg_shadow       11      12094   0       10      0       0       0       0       0.000000000000  0       0       f       f       p       v
 Item  32 -- Length:  172  Offset: 2416 (0x0970)  Flags: NORMAL
COPY: 16400     t_xff4  2200    16402   0       10      2       16400   0       0       0.000000000000  0       0       f       f       p       r
</pre>
<p>这个里面获取到pg_class.reltablespace是表空间的id值,根据自定义表空间的规则:在pgdata/pg_tblspc创建link指向创建表空间时候的文件夹路径</p>
<pre class="brush: bash; title: ; notranslate">
-bash-4.2$ pwd
/var/lib/pgsql/12/data/pg_tblspc
-bash-4.2$ ls -ltr
total 0
lrwxrwxrwx 1 postgres postgres 30 Apr 15 20:13 16406 -&gt; /var/lib/pgsql/12/data/tbs_xff
</pre>
<p>结合上述的pg_database,pg_tablespace,pg_class信息,可以获取到每个表对应实际的存储路径<br />
<strong>分析PostgreSQL 模式信息</strong></p>
<pre class="brush: bash; title: ; notranslate">
--sql查询模式信息
postgres=# select * from pg_namespace;
  oid  |      nspname       | nspowner |               nspacl                
-------+--------------------+----------+-------------------------------------
    99 | pg_toast           |       10 | 
 12314 | pg_temp_1          |       10 | 
 12315 | pg_toast_temp_1    |       10 | 
    11 | pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}
  2200 | public             |       10 | {postgres=UC/postgres,=UC/postgres}
 13887 | information_schema |       10 | {postgres=UC/postgres,=U/postgres}
 16404 | u_xifenfei         |       10 | 
(7 rows)


--通过dump 该文件解析数据
&lt;Data&gt; -----
 Item   1 -- Length:    0  Offset:    6 (0x0006)  Flags: REDIRECT
 Item   2 -- Length:   96  Offset: 8096 (0x1fa0)  Flags: NORMAL
COPY: 99        pg_toast
 Item   3 -- Length:    0  Offset:    7 (0x0007)  Flags: REDIRECT
 Item   4 -- Length:   96  Offset: 8000 (0x1f40)  Flags: NORMAL
COPY: 12314     pg_temp_1
 Item   5 -- Length:   96  Offset: 7904 (0x1ee0)  Flags: NORMAL
COPY: 12315     pg_toast_temp_1
 Item   6 -- Length:  141  Offset: 7760 (0x1e50)  Flags: NORMAL
COPY: 11        pg_catalog
 Item   7 -- Length:  141  Offset: 7616 (0x1dc0)  Flags: NORMAL
COPY: 2200      public
 Item   8 -- Length:    0  Offset:    9 (0x0009)  Flags: REDIRECT
 Item   9 -- Length:  141  Offset: 7472 (0x1d30)  Flags: NORMAL
COPY: 13887     information_schema
 Item  10 -- Length:   96  Offset: 7376 (0x1cd0)  Flags: NORMAL
COPY: 16404     u_xifenfei
</pre>
<p>通过pg_namespace，pg_class信息，可以获取到对象所属的模式关系，基于上述汇总,可以获取到某个模式下面,所有表id和实际存储路径,现在使用pg_filedump进行恢复,还缺少表的列类型信息,通过pg_type和pg_attribute来获取。<br />
<br />
<strong>获取PostgreSQL表的列名称和类型[编号]信息</strong></p>
<pre class="brush: bash; title: ; notranslate">
--sql查询列信息
postgres=# \d t_tbs
               Table &quot;public.t_tbs&quot;
  Column  | Type | Collation | Nullable | Default 
----------+------+-----------+----------+---------
 oid      | oid  |           |          | 
 spcname  | name |           |          | 
 spcowner | oid  |           |          | 
Tablespace: &quot;tbs_xifenfei&quot;

postgres=# select attrelid,attname,atttypid,attstattarget,attlen,attnum from pg_attribute where attrelid=16408;
 attrelid | attname  | atttypid | attstattarget | attlen | attnum 
----------+----------+----------+---------------+--------+--------
    16408 | tableoid |       26 |             0 |      4 |     -6
    16408 | cmax     |       29 |             0 |      4 |     -5
    16408 | xmax     |       28 |             0 |      4 |     -4
    16408 | cmin     |       29 |             0 |      4 |     -3
    16408 | xmin     |       28 |             0 |      4 |     -2
    16408 | ctid     |       27 |             0 |      6 |     -1
    16408 | oid      |       26 |            -1 |      4 |      1
    16408 | spcname  |       19 |            -1 |     64 |      2
    16408 | spcowner |       26 |            -1 |      4 |      3
(9 rows)

--dump 内容（截取部分）
 Item  11 -- Length:  144  Offset: 1424 (0x0590)  Flags: NORMAL
COPY: 16408     oid     26      -1      4       1
 Item  12 -- Length:  144  Offset: 1280 (0x0500)  Flags: NORMAL
COPY: 16408     spcname 19      -1      64      2
 Item  13 -- Length:  144  Offset: 1136 (0x0470)  Flags: NORMAL
COPY: 16408     spcowner        26      -1      4       3
 Item  14 -- Length:  144  Offset:  992 (0x03e0)  Flags: NORMAL
COPY: 16408     ctid    27      0       6       -1
 Item  15 -- Length:  144  Offset:  848 (0x0350)  Flags: NORMAL
COPY: 16408     xmin    28      0       4       -2
 Item  16 -- Length:  144  Offset:  704 (0x02c0)  Flags: NORMAL
COPY: 16408     cmin    29      0       4       -3
 Item  17 -- Length:  144  Offset:  560 (0x0230)  Flags: NORMAL
COPY: 16408     xmax    28      0       4       -4
 Item  18 -- Length:  144  Offset:  416 (0x01a0)  Flags: NORMAL
COPY: 16408     cmax    29      0       4       -5
</pre>
<p><strong>PostgreSQL获取类型编号和实际类型名称对应关系</strong></p>
<pre class="brush: bash; title: ; notranslate">
--查询类型编号和实际类型关系
postgres=# select oid,typname from pg_type;
  oid  |                typname                
-------+---------------------------------------
    16 | bool
    17 | bytea
    18 | char
    19 | name
    20 | int8
    21 | int2
    22 | int2vector
    23 | int4
    24 | regproc
    25 | text
    26 | oid
    27 | tid
    28 | xid
    29 | cid
……

--dump 内容（截取部分）
 Item   1 -- Length:  176  Offset: 8016 (0x1f50)  Flags: NORMAL
COPY: 16        bool
 Item   2 -- Length:  176  Offset: 7840 (0x1ea0)  Flags: NORMAL
COPY: 17        bytea
 Item   3 -- Length:  176  Offset: 7664 (0x1df0)  Flags: NORMAL
COPY: 18        char
 Item   4 -- Length:  176  Offset: 7488 (0x1d40)  Flags: NORMAL
COPY: 19        name
 Item   5 -- Length:  176  Offset: 7312 (0x1c90)  Flags: NORMAL
COPY: 20        int8
 Item   6 -- Length:  176  Offset: 7136 (0x1be0)  Flags: NORMAL
COPY: 21        int2
 Item   7 -- Length:  176  Offset: 6960 (0x1b30)  Flags: NORMAL
COPY: 22        int2vector
 Item   8 -- Length:  176  Offset: 6784 (0x1a80)  Flags: NORMAL
COPY: 23        int4
 Item   9 -- Length:  176  Offset: 6608 (0x19d0)  Flags: NORMAL
COPY: 24        regproc
 Item  10 -- Length:  176  Offset: 6432 (0x1920)  Flags: NORMAL
COPY: 25        text
 Item  11 -- Length:  176  Offset: 6256 (0x1870)  Flags: NORMAL
COPY: 26        oid
 Item  12 -- Length:  176  Offset: 6080 (0x17c0)  Flags: NORMAL
COPY: 27        tid
 Item  13 -- Length:  176  Offset: 5904 (0x1710)  Flags: NORMAL
COPY: 28        xid
 Item  14 -- Length:  176  Offset: 5728 (0x1660)  Flags: NORMAL
COPY: 29        cid
 Item  15 -- Length:  176  Offset: 5552 (0x15b0)  Flags: NORMAL
COPY: 30        oidvector
</pre>
<p>通过pg_class、pg_type和pg_attribute可以获取对象的表的列名称，数据类型等信息。通过以上几个对象，即可获取到pg_filedmp处理所需要的所有信息,然后可以实现批量处理</p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2022/06/pg_filedump-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/2010/08/flash%e5%b9%bb%e7%81%af%e7%89%87.html" rel="bookmark" class="crp_title">flash幻灯片</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/2019/11/imp-00098-internal-error-impgst2.html" rel="bookmark" class="crp_title">IMP-00098: INTERNAL ERROR: impgst2</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-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/2013/05/shell%e8%84%9a%e6%9c%ac%e8%8e%b7%e5%be%97extents%e5%88%86%e5%b8%83.html" rel="bookmark" class="crp_title">shell脚本获得extents分布</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/09/dul%e5%ae%9e%e7%8e%b0expdp-dump%e6%96%87%e4%bb%b6%e8%bd%ac%e6%8d%a2sqlldr%e6%a0%bc%e5%bc%8f.html" rel="bookmark" class="crp_title">密码保护：dul实现expdp dump文件转换sqlldr格式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2024/12/ora-15411.html" rel="bookmark" class="crp_title">ORA-15411: Failure groups in disk group DATA have different number of disks.</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/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/09/ora-704-ora-604-ora-1426.html" rel="bookmark" class="crp_title">ORA-704 ORA-604 ORA-1426故障分析处理</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2024/04/pg_filedump-batch-recovery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
