From a5375bf90304d0cc39b6ca9d5bb07c20ff51f2b3 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 21 Jul 2009 21:46:10 +0000 Subject: [PATCH] Make pg_dump/pg_restore --clean options drop large objects too. In passing, make invocations of lo_xxx functions a bit more schema-safe. Itagaki Takahiro --- src/bin/pg_dump/pg_backup_archiver.c | 15 ++++++++++----- src/bin/pg_dump/pg_backup_archiver.h | 4 ++-- src/bin/pg_dump/pg_backup_custom.c | 10 +++++----- src/bin/pg_dump/pg_backup_files.c | 4 ++-- src/bin/pg_dump/pg_backup_null.c | 13 +++++++++---- src/bin/pg_dump/pg_backup_tar.c | 4 ++-- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 16e9bbd74b..c5a75d3488 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.172 2009/06/11 14:49:07 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.173 2009/07/21 21:46:10 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -895,7 +895,7 @@ EndRestoreBlobs(ArchiveHandle *AH) * Called by a format handler to initiate restoration of a blob */ void -StartRestoreBlob(ArchiveHandle *AH, Oid oid) +StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop) { Oid loOid; @@ -906,6 +906,10 @@ StartRestoreBlob(ArchiveHandle *AH, Oid oid) ahlog(AH, 2, "restoring large object with OID %u\n", oid); + if (drop) + ahprintf(AH, "SELECT CASE WHEN EXISTS(SELECT 1 FROM pg_catalog.pg_largeobject WHERE loid = '%u') THEN pg_catalog.lo_unlink('%u') END;\n", + oid, oid); + if (AH->connection) { loOid = lo_create(AH->connection, oid); @@ -919,7 +923,8 @@ StartRestoreBlob(ArchiveHandle *AH, Oid oid) } else { - ahprintf(AH, "SELECT lo_open(lo_create(%u), %d);\n", oid, INV_WRITE); + ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n", + oid, INV_WRITE); } AH->writingBlob = 1; @@ -943,7 +948,7 @@ EndRestoreBlob(ArchiveHandle *AH, Oid oid) } else { - ahprintf(AH, "SELECT lo_close(0);\n\n"); + ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n"); } } @@ -1254,7 +1259,7 @@ dump_lo_buf(ArchiveHandle *AH) /* Hack: turn off writingBlob so ahwrite doesn't recurse to here */ AH->writingBlob = 0; - ahprintf(AH, "SELECT lowrite(0, '%s');\n", str); + ahprintf(AH, "SELECT pg_catalog.lowrite(0, '%s');\n", str); AH->writingBlob = 1; free(str); diff --git a/src/bin/pg_dump/pg_backup_archiver.h b/src/bin/pg_dump/pg_backup_archiver.h index 2cde2dbda3..4f7884862d 100644 --- a/src/bin/pg_dump/pg_backup_archiver.h +++ b/src/bin/pg_dump/pg_backup_archiver.h @@ -17,7 +17,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.79 2009/06/11 14:49:07 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.80 2009/07/21 21:46:10 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -355,7 +355,7 @@ int ReadOffset(ArchiveHandle *, pgoff_t *); size_t WriteOffset(ArchiveHandle *, pgoff_t, int); extern void StartRestoreBlobs(ArchiveHandle *AH); -extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid); +extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop); extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid); extern void EndRestoreBlobs(ArchiveHandle *AH); diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index 5d6cf4de95..a90d19bb3a 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -19,7 +19,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.42 2009/06/11 14:49:07 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.43 2009/07/21 21:46:10 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -54,7 +54,7 @@ static void _StartBlobs(ArchiveHandle *AH, TocEntry *te); static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid); static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid); static void _EndBlobs(ArchiveHandle *AH, TocEntry *te); -static void _LoadBlobs(ArchiveHandle *AH); +static void _LoadBlobs(ArchiveHandle *AH, bool drop); static void _Clone(ArchiveHandle *AH); static void _DeClone(ArchiveHandle *AH); @@ -501,7 +501,7 @@ _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt) break; case BLK_BLOBS: - _LoadBlobs(AH); + _LoadBlobs(AH, ropt->dropSchema); break; default: /* Always have a default */ @@ -622,7 +622,7 @@ _PrintData(ArchiveHandle *AH) } static void -_LoadBlobs(ArchiveHandle *AH) +_LoadBlobs(ArchiveHandle *AH, bool drop) { Oid oid; @@ -631,7 +631,7 @@ _LoadBlobs(ArchiveHandle *AH) oid = ReadInt(AH); while (oid != 0) { - StartRestoreBlob(AH, oid); + StartRestoreBlob(AH, oid, drop); _PrintData(AH); EndRestoreBlob(AH, oid); oid = ReadInt(AH); diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c index 544e48722b..2df535581b 100644 --- a/src/bin/pg_dump/pg_backup_files.c +++ b/src/bin/pg_dump/pg_backup_files.c @@ -20,7 +20,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.35 2009/02/02 20:07:37 adunstan Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.36 2009/07/21 21:46:10 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -382,7 +382,7 @@ _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt) while (oid != 0) { - StartRestoreBlob(AH, oid); + StartRestoreBlob(AH, oid, ropt->dropSchema); _PrintFileData(AH, fname, ropt); EndRestoreBlob(AH, oid); _getBlobTocEntry(AH, &oid, fname); diff --git a/src/bin/pg_dump/pg_backup_null.c b/src/bin/pg_dump/pg_backup_null.c index abff1138fc..a89b012e6c 100644 --- a/src/bin/pg_dump/pg_backup_null.c +++ b/src/bin/pg_dump/pg_backup_null.c @@ -17,7 +17,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_null.c,v 1.20 2009/02/02 20:07:37 adunstan Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_null.c,v 1.21 2009/07/21 21:46:10 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -108,7 +108,7 @@ _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen) if (!str) die_horribly(AH, NULL, "out of memory\n"); - ahprintf(AH, "SELECT lowrite(0, '%s');\n", str); + ahprintf(AH, "SELECT pg_catalog.lowrite(0, '%s');\n", str); free(str); } @@ -149,7 +149,12 @@ _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid) if (oid == 0) die_horribly(AH, NULL, "invalid OID for large object\n"); - ahprintf(AH, "SELECT lo_open(lo_create(%u), %d);\n", oid, INV_WRITE); + if (AH->ropt->dropSchema) + ahprintf(AH, "SELECT CASE WHEN EXISTS(SELECT 1 FROM pg_catalog.pg_largeobject WHERE loid = '%u') THEN pg_catalog.lo_unlink('%u') END;\n", + oid, oid); + + ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n", + oid, INV_WRITE); AH->WriteDataPtr = _WriteBlobData; } @@ -164,7 +169,7 @@ _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid) { AH->WriteDataPtr = _WriteData; - ahprintf(AH, "SELECT lo_close(0);\n\n"); + ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n"); } /* diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c index 9b5a41ac20..8331048877 100644 --- a/src/bin/pg_dump/pg_backup_tar.c +++ b/src/bin/pg_dump/pg_backup_tar.c @@ -16,7 +16,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.65 2009/06/04 19:16:48 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.66 2009/07/21 21:46:10 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -729,7 +729,7 @@ _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt) { ahlog(AH, 1, "restoring large object OID %u\n", oid); - StartRestoreBlob(AH, oid); + StartRestoreBlob(AH, oid, ropt->dropSchema); while ((cnt = tarRead(buf, 4095, th)) > 0) { -- 2.40.0