Repair Corrupt database postgresql

≯℡__Kan透↙ 提交于 2019-11-29 13:10:11
wildplasser

Before you do anything else, http://wiki.postgresql.org/wiki/Corruption and act on the instructions. Failure to do so risks making the problem worse.


There are two configuration parameters listed in the Fine Manual that might be of use: ignore_system_indexes and zero_damaged_pages. I have never used them, but I would if I were desparate ...

I don't know if they help against toast-tables. In any case, if setting them causes your database(s) to become usable again, I would {backup + drop + restore} to get all tables and catalogs into newborn shape again. Success!

If you have backups, just restore from them.

If not - you've just learned why you need regular backups. There's nothing PostgreSQL can do if hardware misbehaves.

In addition, if you ever find yourself in this situation again, first stop PostgreSQL and take a complete file-level backup of everything - all tablespaces, WAL etc. That way you have a known starting point.

So - if you still want to recover some data.

  1. Try dumping individual tables. Get what you can this way.
  2. Drop indexes if they cause problems
  3. Dump sections of tables (id=0..9999, 1000..19999 etc) - that way you can identify where some rows may be corrupted and dump ever-smaller sections to recover what's still good.
  4. Try dumping just certain columns - large text values are stored out-of-line (in toast tables) so avoiding them might get the rest of your data out.
  5. If you've got corrupted system tables then you're getting into a lot of work.

That's a lot of work, and then you'll need to go through and audit what you've recovered and try to figure out what's missing/incorrect.

There are more things you can do (creating empty blocks in some cases can let you dump partial data) but they're all more complicated and fiddly and unless the data is particularly valuable not worth the effort.

Key message to take away from this - make sure you take regular backups, and make sure they work.

Before you do ANYTHING ELSE, take a complete file-system-level copy of the damaged database.

http://wiki.postgresql.org/wiki/Corruption

Failure to do so destroys evidence about what caused the corruption, and means that if your repair efforts go badly and make things worse you can't undo them.

Copy it now!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!