且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PostgreSQL - 意外删除数据文件后恢复函数的代码

更新时间:2023-02-02 21:01:43

如果您可以重建足够的数据目录在单用户模式下启动postgres,你可能会转储pg_proc。但这似乎不太可能。



否则,如果您真幸运,您将能够找到 pg_proc 及其对应的 pg_toast 关系。后者通常会包含压缩的文本,因此搜索你知道出现在函数体中的变量部分可能无法帮助你。



任何内嵌在 pg_proc 将是短的函数,显着小于8k长。为了解码你必须解压缩页面以获得烘烤块,然后重新组装它们并解压它们(如果压缩的话)。

如果必须这样做,我可能会创建一个与 pg_proc 完全相同的模式的表相同版本的新postgres实例。然后,我会使用relfilenode映射文件(如果它幸存下来)或模式匹配和猜测找到 pg_catalog.pg_proc 的relfilenode(s)和它的toast表。我将替换用恢复的表创建的新表的空关系文件,重新启动postgres,如果我是对的,我可以从表中选择



不容易。我建议您阅读 postgres的存储格式,因为您需要了解它。



您可以考虑 https://www.postgresql.org/support/professional_support/ 。 (免责声明,我为其中一家上市公司工作)。

So, I am (well... I was) running PostgreSQL within a container (Ubuntu 14.04LTS with all the recent updates, back-end storage is "dir" because of convince).

To cut the long story short, the container folder got deleted. Following the use of extundelete and ext4magic, I have managed to extract some of the database physical files (it appears as if most of the files are there... but not 100% sure if and what is missing).

I have two copies of the database files. One from 9.5.3 (which appears to be more complete) and one from 9.6 (I upgraded the container very recently to 9.6, however it appears to be missing datafiles).

All I am after is to attempt and extract the SQL code the relates to the user defined functions. Is anyone aware of an approach that I could try?

P.S.: Last backup is a bit dated (due to bad practices really) so it would be last resort if the task of extracting the needed information is "reasonable" and "successful".

Regards, G

Update - 20/4/2017 I was hoping for a "quick fix" by somehow extracting the function body text off the recovered data files... however, nothing's free in this life :)

Starting from the old-ish backup along with the recovered logs, we managed to cover a lot of ground into bringing the DB back to life.

Lessons learned:
1. Do implement a good backup/restore strategy
2. Do not store backups on the same physical machine
3. Hardware failure can be disruptive... Human error can be disastrous!

If you can reconstruct enough of a data directory to start postgres in single user mode you might be able to dump pg_proc. But this seems unlikely.

Otherwise, if you're really lucky you'll be able to find the relation for pg_proc and its corresponding pg_toast relation. The latter will often contain compressed text, so searches for parts of variables you know appear in function bodies may not help you out.

Anything stored inline in pg_proc will be short functions, significantly less than 8k long. Everything else will be in the toast relation.

To decode that you have to unpack the pages to get the toast hunks, then reassemble them and uncompress them (if compressed).

If I had to do this, I would probably create a table with the exact same schema as pg_proc in a new postgres instance of the same version. I would then find the relfilenode(s) for pg_catalog.pg_proc and its toast table using the relfilenode map file (if it survived) or by pattern matching and guesswork. I would replace the empty relation files for the new table I created with the recovered ones, restart postgres, and if I was right, I'd be able to select from the tables.

Not easy.

I suggest reading up on postgres's storage format as you'll need to understand it.

You may consider https://www.postgresql.org/support/professional_support/ . (Disclaimer, I work for one of the listed companies).