The following commands produce an error:

    createdb src
    createdb dst
    psql src -c "select lo_from_bytea(1, '\xff')"
    pg_dump -Ft -f d.tar src
    mkdir ex
    tar xf d.tar -C ex
    pg_restore -Fd -d dst ex

    pg_restore: error: could not open large object TOC file "ex/2966.dat" for 
input: No such file or directory

This is because the tar format puts NNN.dat instead of blobs_NNN.toc into
the BLOBS TOC entry.  That was harmless until commit a45c78e328 (v17)
taught the directory format's restore code to use the file name in the TOC
entry.  Commit 166b4f4560 fixed a similar problem for directory-format
dumps made by versions before v12.

The attached patch fixes it by constructing the file name instead of using
the one in the TOC entry.  Given the lack of reports, I'm guessing few
users are using extracted tar archives like this, or even using the tar
format at all.  IMHO we should seriously consider putting the tar format on
a path to deprecation, but I'll save that discussion for later.

-- 
nathan
>From a1f54aa34f39756173de63e8528ac248c340c6bb Mon Sep 17 00:00:00 2001
From: Nathan Bossart <[email protected]>
Date: Thu, 19 Nov 2026 15:14:58 -0600
Subject: [PATCH v1 1/1] pg_restore: Fix incompatibility with extracted tar
 dumps.

pg_restore fails to restore large objects from a directory-format
archive that was created by extracting a tar-format archive.
That's because the tar format puts the wrong file name into the
BLOBS TOC entries.  This was harmless before v17, because we
ignored the file name field.  Commit 166b4f4560 fixed the same
problem for directory-format archives made by versions before v12.

To fix, construct the file name instead of using the one in the
TOC entry, as we already do for those older archives.  This commit
doesn't change the name recorded by the tar format, as that
wouldn't help with existing archives.

Oversight in commit a45c78e328.

Backpatch-through: 17
---
 src/bin/pg_dump/pg_backup_directory.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_dump/pg_backup_directory.c 
b/src/bin/pg_dump/pg_backup_directory.c
index 34b3b6ba7d7..e14369181f4 100644
--- a/src/bin/pg_dump/pg_backup_directory.c
+++ b/src/bin/pg_dump/pg_backup_directory.c
@@ -399,7 +399,6 @@ _LoadLOs(ArchiveHandle *AH, TocEntry *te)
 {
        Oid                     oid;
        lclContext *ctx = (lclContext *) AH->formatData;
-       lclTocEntry *tctx = (lclTocEntry *) te->formatData;
        CompressFileHandle *CFH;
        char            tocfname[MAXPGPATH];
        char            line[MAXPGPATH];
@@ -412,11 +411,20 @@ _LoadLOs(ArchiveHandle *AH, TocEntry *te)
         * was always "blobs.toc" before v16, the value of tctx->filename did 
not
         * match that before commit 548e50976 fixed it.  For simplicity we 
assume
         * it must be "blobs.toc" in all archives before v16.
+        *
+        * Likewise, the tar format puts the wrong filename into tctx->filename,
+        * which matters when reading an extracted tar archive.  For simplicity 
we
+        * assume it must be "blobs_<dumpId>.toc" in all newer archives.
         */
        if (AH->version < K_VERS_1_16)
                setFilePath(AH, tocfname, "blobs.toc");
        else
-               setFilePath(AH, tocfname, tctx->filename);
+       {
+               char            fname[MAXPGPATH];
+
+               snprintf(fname, MAXPGPATH, "blobs_%d.toc", te->dumpId);
+               setFilePath(AH, tocfname, fname);
+       }
 
        CFH = ctx->LOsTocFH = InitDiscoverCompressFileHandle(tocfname, 
PG_BINARY_R);
 
-- 
2.55.0

Reply via email to