PostgreSQL:是否可以使用 PostgreSQL 仅恢复大对象?

PostgreSQL : it is possible to restore only Large Object with PostgreSQL?

我们有一个简单的 table my_table 比如:

id (int) -> primary key
object_id (Large Object)

不幸的是,我们的承包商在 table my_tableobject_id 列的大型对象的 ID 上有 运行 lo_unlink

大对象 ID 仍在 table,但关联的大对象不再存在:

ERROR:  large object 10268782 does not exist
SQL state: 42704

我们每天晚上用 pg_dump 进行备份。

我们如何才能只恢复具有 pg_restore 的大对象?

的确,我们不需要恢复所有的table,甚至不需要恢复带有LOB ID的table(因为ID仍然被定义),只需要重新加载大对象到来自转储的数据库。

pg_restore有一个选项:

--data-only
Restore only the data, not the schema (data definitions). Table data, large objects, and sequence values are restored, if present in the archive.

但我们不想恢复 table 数据或序列值。只是大对象。

谢谢

要仅从 纯文本格式的转储中恢复大对象,您可以按以下步骤进行:

  • 从转储创建一个“table of contants”文件:

    pg_restore --list --file=tocfile dumpfile
    
  • 编辑 tocfile 并删除除包含 BLOBS 的行以外的所有行,例如

    4353; 0 0 BLOBS - BLOBS
    
  • 现在您只能恢复大对象:

    pg_restore --use-list=tocfile --dbname=proddb dumpfile
    

这将覆盖数据库中的所有 个大对象。如果您只想恢复某些大对象,没有简单的方法。您可以 运行 最后 pg_restore 将 SQL 语句写入文件,方法是使用 --file=lobs.sql 而不是 --dbname=proddb,这将创建一个 SQL 脚本里面有所有大物体。您必须手动编辑该文件才能提取所需的对象:^/