Commit a5375bf9 authored by Tom Lane's avatar Tom Lane

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
parent 5dedce67
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -895,7 +895,7 @@ EndRestoreBlobs(ArchiveHandle *AH)
* Called by a format handler to initiate restoration of a blob * Called by a format handler to initiate restoration of a blob
*/ */
void void
StartRestoreBlob(ArchiveHandle *AH, Oid oid) StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
{ {
Oid loOid; Oid loOid;
...@@ -906,6 +906,10 @@ StartRestoreBlob(ArchiveHandle *AH, Oid oid) ...@@ -906,6 +906,10 @@ StartRestoreBlob(ArchiveHandle *AH, Oid oid)
ahlog(AH, 2, "restoring large object with OID %u\n", 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) if (AH->connection)
{ {
loOid = lo_create(AH->connection, oid); loOid = lo_create(AH->connection, oid);
...@@ -919,7 +923,8 @@ StartRestoreBlob(ArchiveHandle *AH, Oid oid) ...@@ -919,7 +923,8 @@ StartRestoreBlob(ArchiveHandle *AH, Oid oid)
} }
else 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; AH->writingBlob = 1;
...@@ -943,7 +948,7 @@ EndRestoreBlob(ArchiveHandle *AH, Oid oid) ...@@ -943,7 +948,7 @@ EndRestoreBlob(ArchiveHandle *AH, Oid oid)
} }
else 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) ...@@ -1254,7 +1259,7 @@ dump_lo_buf(ArchiveHandle *AH)
/* Hack: turn off writingBlob so ahwrite doesn't recurse to here */ /* Hack: turn off writingBlob so ahwrite doesn't recurse to here */
AH->writingBlob = 0; AH->writingBlob = 0;
ahprintf(AH, "SELECT lowrite(0, '%s');\n", str); ahprintf(AH, "SELECT pg_catalog.lowrite(0, '%s');\n", str);
AH->writingBlob = 1; AH->writingBlob = 1;
free(str); free(str);
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
* *
* *
* IDENTIFICATION * 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 *); ...@@ -355,7 +355,7 @@ int ReadOffset(ArchiveHandle *, pgoff_t *);
size_t WriteOffset(ArchiveHandle *, pgoff_t, int); size_t WriteOffset(ArchiveHandle *, pgoff_t, int);
extern void StartRestoreBlobs(ArchiveHandle *AH); 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 EndRestoreBlob(ArchiveHandle *AH, Oid oid);
extern void EndRestoreBlobs(ArchiveHandle *AH); extern void EndRestoreBlobs(ArchiveHandle *AH);
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
* *
* *
* IDENTIFICATION * 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); ...@@ -54,7 +54,7 @@ static void _StartBlobs(ArchiveHandle *AH, TocEntry *te);
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid); static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid); static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te); 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 _Clone(ArchiveHandle *AH);
static void _DeClone(ArchiveHandle *AH); static void _DeClone(ArchiveHandle *AH);
...@@ -501,7 +501,7 @@ _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt) ...@@ -501,7 +501,7 @@ _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
break; break;
case BLK_BLOBS: case BLK_BLOBS:
_LoadBlobs(AH); _LoadBlobs(AH, ropt->dropSchema);
break; break;
default: /* Always have a default */ default: /* Always have a default */
...@@ -622,7 +622,7 @@ _PrintData(ArchiveHandle *AH) ...@@ -622,7 +622,7 @@ _PrintData(ArchiveHandle *AH)
} }
static void static void
_LoadBlobs(ArchiveHandle *AH) _LoadBlobs(ArchiveHandle *AH, bool drop)
{ {
Oid oid; Oid oid;
...@@ -631,7 +631,7 @@ _LoadBlobs(ArchiveHandle *AH) ...@@ -631,7 +631,7 @@ _LoadBlobs(ArchiveHandle *AH)
oid = ReadInt(AH); oid = ReadInt(AH);
while (oid != 0) while (oid != 0)
{ {
StartRestoreBlob(AH, oid); StartRestoreBlob(AH, oid, drop);
_PrintData(AH); _PrintData(AH);
EndRestoreBlob(AH, oid); EndRestoreBlob(AH, oid);
oid = ReadInt(AH); oid = ReadInt(AH);
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -382,7 +382,7 @@ _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt)
while (oid != 0) while (oid != 0)
{ {
StartRestoreBlob(AH, oid); StartRestoreBlob(AH, oid, ropt->dropSchema);
_PrintFileData(AH, fname, ropt); _PrintFileData(AH, fname, ropt);
EndRestoreBlob(AH, oid); EndRestoreBlob(AH, oid);
_getBlobTocEntry(AH, &oid, fname); _getBlobTocEntry(AH, &oid, fname);
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -108,7 +108,7 @@ _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen)
if (!str) if (!str)
die_horribly(AH, NULL, "out of memory\n"); 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); free(str);
} }
...@@ -149,7 +149,12 @@ _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid) ...@@ -149,7 +149,12 @@ _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
if (oid == 0) if (oid == 0)
die_horribly(AH, NULL, "invalid OID for large object\n"); 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; AH->WriteDataPtr = _WriteBlobData;
} }
...@@ -164,7 +169,7 @@ _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid) ...@@ -164,7 +169,7 @@ _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
{ {
AH->WriteDataPtr = _WriteData; AH->WriteDataPtr = _WriteData;
ahprintf(AH, "SELECT lo_close(0);\n\n"); ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
} }
/* /*
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -729,7 +729,7 @@ _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt)
{ {
ahlog(AH, 1, "restoring large object OID %u\n", oid); 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) while ((cnt = tarRead(buf, 4095, th)) > 0)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment