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
This commit is contained in:
Tom Lane 2009-07-21 21:46:10 +00:00
parent 5dedce6770
commit a5375bf903
6 changed files with 30 additions and 20 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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");
}
/*

View File

@ -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)
{