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

Time:06-03

We have a simple table my_table like :

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

Unfortunately, a contractor of us have run lo_unlink on the IDs of the large objects from the column object_id of the table my_table.

The Large Object ID are still in the table, but the associated Large Objects not exist anymore :

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

We do backup with pg_dump every night.

How we can restore only the Large Object with pg_restore ?

Indeed, we don't need to restore all the tables, even not the table with LOB ID (because the ID are still defined), but reload only the Large Object into the database from the dump.

The pg_restore has an option :

--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.

but we don't want to restore table data or sequence values. Just large objects.

Thank you

CodePudding user response:

To restore only the large objects from a dump that is not in plain text format, you can proceed as follows:

  • create a "table of contants" file from the dump:

    pg_restore --list --file=tocfile dumpfile
    
  • edit tocfile and remove all lines except the one that contains BLOBS, for example

    4353; 0 0 BLOBS - BLOBS
    
  • now you can restore only the large objects:

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

That will overwrite all large objects in the database. If you only want to restore certain large objects, there is no easy way. You can run the final pg_restore to write SQL statements to a file by using --file=lobs.sql instead of --dbname=proddb, which will create an SQL script with all large objects in it. You'd have to edit that file manually to extract the objects you want :^/

  • Related