*** a/src/bin/pg_dump/pg_backup_archiver.c
--- b/src/bin/pg_dump/pg_backup_archiver.c
***************
*** 329,335 **** RestoreArchive(Archive *AHX, RestoreOptions *ropt)
  			AH->currentTE = te;
  
  			reqs = _tocEntryRequired(te, ropt, false /* needn't drop ACLs */ );
! 			if (((reqs & REQ_SCHEMA) != 0) && te->dropStmt)
  			{
  				/* We want the schema */
  				ahlog(AH, 1, "dropping %s %s\n", te->desc, te->tag);
--- 329,335 ----
  			AH->currentTE = te;
  
  			reqs = _tocEntryRequired(te, ropt, false /* needn't drop ACLs */ );
! 			if (((reqs & (REQ_SCHEMA|REQ_DATA)) != 0) && te->dropStmt)
  			{
  				/* We want the schema */
  				ahlog(AH, 1, "dropping %s %s\n", te->desc, te->tag);
***************
*** 381,387 **** RestoreArchive(Archive *AHX, RestoreOptions *ropt)
  		/* Work out what, if anything, we want from this entry */
  		reqs = _tocEntryRequired(te, ropt, true);
  
! 		if ((reqs & REQ_SCHEMA) != 0)	/* We want the schema */
  		{
  			ahlog(AH, 1, "setting owner and privileges for %s %s\n",
  				  te->desc, te->tag);
--- 381,388 ----
  		/* Work out what, if anything, we want from this entry */
  		reqs = _tocEntryRequired(te, ropt, true);
  
! 		/* Access privileges for both of schema and data */
! 		if ((reqs & (REQ_SCHEMA|REQ_DATA)) != 0)
  		{
  			ahlog(AH, 1, "setting owner and privileges for %s %s\n",
  				  te->desc, te->tag);
***************
*** 520,525 **** restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
--- 521,527 ----
  				_printTocEntry(AH, te, ropt, true, false);
  
  				if (strcmp(te->desc, "BLOBS") == 0 ||
+ 					strcmp(te->desc, "BLOB DATA") == 0 ||
  					strcmp(te->desc, "BLOB COMMENTS") == 0)
  				{
  					ahlog(AH, 1, "restoring %s\n", te->desc);
***************
*** 903,909 **** EndRestoreBlobs(ArchiveHandle *AH)
   * Called by a format handler to initiate restoration of a blob
   */
  void
! StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
  {
  	Oid			loOid;
  
--- 905,911 ----
   * Called by a format handler to initiate restoration of a blob
   */
  void
! StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop, bool compat)
  {
  	Oid			loOid;
  
***************
*** 919,937 **** StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
  
  	if (AH->connection)
  	{
! 		loOid = lo_create(AH->connection, oid);
! 		if (loOid == 0 || loOid != oid)
! 			die_horribly(AH, modulename, "could not create large object %u\n",
! 						 oid);
! 
  		AH->loFd = lo_open(AH->connection, oid, INV_WRITE);
  		if (AH->loFd == -1)
  			die_horribly(AH, modulename, "could not open large object\n");
  	}
  	else
  	{
! 		ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
! 				 oid, INV_WRITE);
  	}
  
  	AH->writingBlob = 1;
--- 921,944 ----
  
  	if (AH->connection)
  	{
! 		if (compat)
! 		{
! 			loOid = lo_create(AH->connection, oid);
! 			if (loOid == 0 || loOid != oid)
! 				die_horribly(AH, modulename, "could not create large object %u\n",
! 							 oid);
! 		}
  		AH->loFd = lo_open(AH->connection, oid, INV_WRITE);
  		if (AH->loFd == -1)
  			die_horribly(AH, modulename, "could not open large object\n");
  	}
  	else
  	{
! 		if (compat)
! 			ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n", oid, INV_WRITE);
! 		else
! 			ahprintf(AH, "SELECT pg_catalog.lo_open(%u, %d);\n",
! 					 oid, INV_WRITE);
  	}
  
  	AH->writingBlob = 1;
***************
*** 1940,1946 **** WriteDataChunks(ArchiveHandle *AH)
  			AH->currToc = te;
  			/* printf("Writing data for %d (%x)\n", te->id, te); */
  
! 			if (strcmp(te->desc, "BLOBS") == 0)
  			{
  				startPtr = AH->StartBlobsPtr;
  				endPtr = AH->EndBlobsPtr;
--- 1947,1954 ----
  			AH->currToc = te;
  			/* printf("Writing data for %d (%x)\n", te->id, te); */
  
! 			if (strcmp(te->desc, "BLOBS") == 0 ||
! 				strcmp(te->desc, "BLOB DATA") == 0)
  			{
  				startPtr = AH->StartBlobsPtr;
  				endPtr = AH->EndBlobsPtr;
***************
*** 2077,2082 **** ReadToc(ArchiveHandle *AH)
--- 2085,2091 ----
  				te->section = SECTION_NONE;
  			else if (strcmp(te->desc, "TABLE DATA") == 0 ||
  					 strcmp(te->desc, "BLOBS") == 0 ||
+ 					 strcmp(te->desc, "BLOB DATA") == 0 ||
  					 strcmp(te->desc, "BLOB COMMENTS") == 0)
  				te->section = SECTION_DATA;
  			else if (strcmp(te->desc, "CONSTRAINT") == 0 ||
***************
*** 2286,2294 **** _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls)
  	if (!te->hadDumper)
  	{
  		/*
! 		 * Special Case: If 'SEQUENCE SET' then it is considered a data entry
  		 */
! 		if (strcmp(te->desc, "SEQUENCE SET") == 0)
  			res = res & REQ_DATA;
  		else
  			res = res & ~REQ_DATA;
--- 2295,2308 ----
  	if (!te->hadDumper)
  	{
  		/*
! 		 * Special Case: If 'SEQUENCE SET', 'BLOB ITEM' or 'ACL' for large
! 		 * objects, then it is considered a data entry
! 		 *
! 		 * XXX - we assume te->tag is not numeric except for large objects.
  		 */
! 		if (strcmp(te->desc, "SEQUENCE SET") == 0 ||
! 			strcmp(te->desc, "BLOB ITEM") == 0 ||
! 			(strcmp(te->desc, "ACL") == 0 && atooid(te->tag) > 0))
  			res = res & REQ_DATA;
  		else
  			res = res & ~REQ_DATA;
***************
*** 2713,2718 **** _getObjectDescription(PQExpBuffer buf, TocEntry *te, ArchiveHandle *AH)
--- 2727,2739 ----
  		return;
  	}
  
+ 	/* Use ALTER LARGE OBJECT for BLOB ITEM */
+ 	if (strcmp(type, "BLOB ITEM") == 0)
+ 	{
+ 		appendPQExpBuffer(buf, "LARGE OBJECT %s", te->tag);
+ 		return;
+ 	}
+ 
  	write_msg(modulename, "WARNING: don't know how to set owner for object type %s\n",
  			  type);
  }
***************
*** 2824,2829 **** _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
--- 2845,2851 ----
  		strlen(te->owner) > 0 && strlen(te->dropStmt) > 0)
  	{
  		if (strcmp(te->desc, "AGGREGATE") == 0 ||
+ 			strcmp(te->desc, "BLOB ITEM") == 0 ||
  			strcmp(te->desc, "CONVERSION") == 0 ||
  			strcmp(te->desc, "DATABASE") == 0 ||
  			strcmp(te->desc, "DOMAIN") == 0 ||
*** a/src/bin/pg_dump/pg_backup_archiver.h
--- b/src/bin/pg_dump/pg_backup_archiver.h
***************
*** 359,365 **** int			ReadOffset(ArchiveHandle *, pgoff_t *);
  size_t		WriteOffset(ArchiveHandle *, pgoff_t, int);
  
  extern void StartRestoreBlobs(ArchiveHandle *AH);
! extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop);
  extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid);
  extern void EndRestoreBlobs(ArchiveHandle *AH);
  
--- 359,365 ----
  size_t		WriteOffset(ArchiveHandle *, pgoff_t, int);
  
  extern void StartRestoreBlobs(ArchiveHandle *AH);
! extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop, bool compat);
  extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid);
  extern void EndRestoreBlobs(ArchiveHandle *AH);
  
*** a/src/bin/pg_dump/pg_backup_custom.c
--- b/src/bin/pg_dump/pg_backup_custom.c
***************
*** 54,60 **** 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, bool drop);
  static void _Clone(ArchiveHandle *AH);
  static void _DeClone(ArchiveHandle *AH);
  
--- 54,60 ----
  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, bool drop, bool compat);
  static void _Clone(ArchiveHandle *AH);
  static void _DeClone(ArchiveHandle *AH);
  
***************
*** 498,504 **** _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
  			break;
  
  		case BLK_BLOBS:
! 			_LoadBlobs(AH, ropt->dropSchema);
  			break;
  
  		default:				/* Always have a default */
--- 498,507 ----
  			break;
  
  		case BLK_BLOBS:
! 			if (strcmp(te->desc, "BLOBS") == 0)
! 				_LoadBlobs(AH, ropt->dropSchema, true);
! 			else
! 				_LoadBlobs(AH, false, false);
  			break;
  
  		default:				/* Always have a default */
***************
*** 619,625 **** _PrintData(ArchiveHandle *AH)
  }
  
  static void
! _LoadBlobs(ArchiveHandle *AH, bool drop)
  {
  	Oid			oid;
  
--- 622,628 ----
  }
  
  static void
! _LoadBlobs(ArchiveHandle *AH, bool drop, bool compat)
  {
  	Oid			oid;
  
***************
*** 628,634 **** _LoadBlobs(ArchiveHandle *AH, bool drop)
  	oid = ReadInt(AH);
  	while (oid != 0)
  	{
! 		StartRestoreBlob(AH, oid, drop);
  		_PrintData(AH);
  		EndRestoreBlob(AH, oid);
  		oid = ReadInt(AH);
--- 631,637 ----
  	oid = ReadInt(AH);
  	while (oid != 0)
  	{
! 		StartRestoreBlob(AH, oid, drop, compat);
  		_PrintData(AH);
  		EndRestoreBlob(AH, oid);
  		oid = ReadInt(AH);
*** a/src/bin/pg_dump/pg_backup_db.c
--- b/src/bin/pg_dump/pg_backup_db.c
***************
*** 12,17 ****
--- 12,18 ----
  
  #include "pg_backup_db.h"
  #include "dumputils.h"
+ #include "libpq/libpq-fs.h"
  
  #include <unistd.h>
  
***************
*** 656,672 **** void
  DropBlobIfExists(ArchiveHandle *AH, Oid oid)
  {
  	/* Call lo_unlink only if exists to avoid not-found error. */
! 	if (PQserverVersion(AH->connection) >= 80500)
! 	{
! 		ahprintf(AH, "SELECT pg_catalog.lo_unlink(oid) "
! 					 "FROM pg_catalog.pg_largeobject_metadata "
! 					 "WHERE oid = %u;\n", oid);
! 	}
! 	else
! 	{
! 		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);
! 	}
  }
  
  static bool
--- 657,670 ----
  DropBlobIfExists(ArchiveHandle *AH, Oid oid)
  {
  	/* Call lo_unlink only if exists to avoid not-found error. */
! 	if (AH->connection &&
! 		PQserverVersion(AH->connection) < 80500)
! 		die_horribly(AH, NULL,
! 					 "could not restore large object into older server");
! 
! 	ahprintf(AH, "SELECT pg_catalog.lo_unlink(oid) "
! 			 "FROM pg_catalog.pg_largeobject_metadata "
! 			 "WHERE oid = %u;\n", oid);
  }
  
  static bool
*** a/src/bin/pg_dump/pg_backup_files.c
--- b/src/bin/pg_dump/pg_backup_files.c
***************
*** 66,72 **** typedef struct
  } lclTocEntry;
  
  static const char *modulename = gettext_noop("file archiver");
! static void _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt);
  static void _getBlobTocEntry(ArchiveHandle *AH, Oid *oid, char *fname);
  
  /*
--- 66,72 ----
  } lclTocEntry;
  
  static const char *modulename = gettext_noop("file archiver");
! static void _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt, bool compat);
  static void _getBlobTocEntry(ArchiveHandle *AH, Oid *oid, char *fname);
  
  /*
***************
*** 330,336 **** _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
  		return;
  
  	if (strcmp(te->desc, "BLOBS") == 0)
! 		_LoadBlobs(AH, ropt);
  	else
  		_PrintFileData(AH, tctx->filename, ropt);
  }
--- 330,338 ----
  		return;
  
  	if (strcmp(te->desc, "BLOBS") == 0)
! 		_LoadBlobs(AH, ropt, true);
! 	else if (strcmp(te->desc, "BLOB DATA") == 0)
! 		_LoadBlobs(AH, ropt, false);
  	else
  		_PrintFileData(AH, tctx->filename, ropt);
  }
***************
*** 365,374 **** _getBlobTocEntry(ArchiveHandle *AH, Oid *oid, char fname[K_STD_BUF_SIZE])
  }
  
  static void
! _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt)
  {
  	Oid			oid;
  	lclContext *ctx = (lclContext *) AH->formatData;
  	char		fname[K_STD_BUF_SIZE];
  
  	StartRestoreBlobs(AH);
--- 367,377 ----
  }
  
  static void
! _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt, bool compat)
  {
  	Oid			oid;
  	lclContext *ctx = (lclContext *) AH->formatData;
+ 	char		drop = (compat ? ropt->dropSchema : false);
  	char		fname[K_STD_BUF_SIZE];
  
  	StartRestoreBlobs(AH);
***************
*** 382,388 **** _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt)
  
  	while (oid != 0)
  	{
! 		StartRestoreBlob(AH, oid, ropt->dropSchema);
  		_PrintFileData(AH, fname, ropt);
  		EndRestoreBlob(AH, oid);
  		_getBlobTocEntry(AH, &oid, fname);
--- 385,391 ----
  
  	while (oid != 0)
  	{
! 		StartRestoreBlob(AH, oid, drop, compat);
  		_PrintFileData(AH, fname, ropt);
  		EndRestoreBlob(AH, oid);
  		_getBlobTocEntry(AH, &oid, fname);
*** a/src/bin/pg_dump/pg_backup_null.c
--- b/src/bin/pg_dump/pg_backup_null.c
***************
*** 147,160 **** _StartBlobs(ArchiveHandle *AH, TocEntry *te)
  static void
  _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
  {
  	if (oid == 0)
  		die_horribly(AH, NULL, "invalid OID for large object\n");
  
! 	if (AH->ropt->dropSchema)
  		DropBlobIfExists(AH, oid);
  
! 	ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
! 			 oid, INV_WRITE);
  
  	AH->WriteDataPtr = _WriteBlobData;
  }
--- 147,165 ----
  static void
  _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
  {
+ 	bool	compat = (strcmp(te->desc, "BLOBS") == 0 ? true : false);
+ 
  	if (oid == 0)
  		die_horribly(AH, NULL, "invalid OID for large object\n");
  
! 	if (compat && AH->ropt->dropSchema)
  		DropBlobIfExists(AH, oid);
  
! 	if (compat)
! 		ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
! 				 oid, INV_WRITE);
! 	else
! 		ahprintf(AH, "SELECT pg_catalog.lo_open(%u, %d);\n", oid, INV_WRITE);
  
  	AH->WriteDataPtr = _WriteBlobData;
  }
***************
*** 195,206 **** _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
  	{
  		AH->currToc = te;
  
! 		if (strcmp(te->desc, "BLOBS") == 0)
  			_StartBlobs(AH, te);
  
  		(*te->dataDumper) ((Archive *) AH, te->dataDumperArg);
  
! 		if (strcmp(te->desc, "BLOBS") == 0)
  			_EndBlobs(AH, te);
  
  		AH->currToc = NULL;
--- 200,213 ----
  	{
  		AH->currToc = te;
  
! 		if (strcmp(te->desc, "BLOBS") == 0 ||
! 			strcmp(te->desc, "BLOB DATA") == 0)
  			_StartBlobs(AH, te);
  
  		(*te->dataDumper) ((Archive *) AH, te->dataDumperArg);
  
! 		if (strcmp(te->desc, "BLOBS") == 0 ||
! 			strcmp(te->desc, "BLOB DATA") == 0)
  			_EndBlobs(AH, te);
  
  		AH->currToc = NULL;
*** a/src/bin/pg_dump/pg_backup_tar.c
--- b/src/bin/pg_dump/pg_backup_tar.c
***************
*** 100,106 **** typedef struct
  
  static const char *modulename = gettext_noop("tar archiver");
  
! static void _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt);
  
  static TAR_MEMBER *tarOpen(ArchiveHandle *AH, const char *filename, char mode);
  static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH);
--- 100,106 ----
  
  static const char *modulename = gettext_noop("tar archiver");
  
! static void _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt, bool compat);
  
  static TAR_MEMBER *tarOpen(ArchiveHandle *AH, const char *filename, char mode);
  static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH);
***************
*** 696,714 **** _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
  	}
  
  	if (strcmp(te->desc, "BLOBS") == 0)
! 		_LoadBlobs(AH, ropt);
  	else
  		_PrintFileData(AH, tctx->filename, ropt);
  }
  
  static void
! _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt)
  {
  	Oid			oid;
  	lclContext *ctx = (lclContext *) AH->formatData;
  	TAR_MEMBER *th;
  	size_t		cnt;
  	bool		foundBlob = false;
  	char		buf[4096];
  
  	StartRestoreBlobs(AH);
--- 696,717 ----
  	}
  
  	if (strcmp(te->desc, "BLOBS") == 0)
! 		_LoadBlobs(AH, ropt, true);
! 	else if (strcmp(te->desc, "BLOB DATA") == 0)
! 		_LoadBlobs(AH, ropt, false);
  	else
  		_PrintFileData(AH, tctx->filename, ropt);
  }
  
  static void
! _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt, bool compat)
  {
  	Oid			oid;
  	lclContext *ctx = (lclContext *) AH->formatData;
  	TAR_MEMBER *th;
  	size_t		cnt;
  	bool		foundBlob = false;
+ 	bool		drop = (compat ? ropt->dropSchema : false);
  	char		buf[4096];
  
  	StartRestoreBlobs(AH);
***************
*** 725,731 **** _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt)
  			{
  				ahlog(AH, 1, "restoring large object OID %u\n", oid);
  
! 				StartRestoreBlob(AH, oid, ropt->dropSchema);
  
  				while ((cnt = tarRead(buf, 4095, th)) > 0)
  				{
--- 728,734 ----
  			{
  				ahlog(AH, 1, "restoring large object OID %u\n", oid);
  
! 				StartRestoreBlob(AH, oid, drop, compat);
  
  				while ((cnt = tarRead(buf, 4095, th)) > 0)
  				{
*** a/src/bin/pg_dump/pg_dump.c
--- b/src/bin/pg_dump/pg_dump.c
***************
*** 190,198 **** static void selectSourceSchema(const char *schemaName);
  static char *getFormattedTypeName(Oid oid, OidOptions opts);
  static char *myFormatType(const char *typname, int32 typmod);
  static const char *fmtQualifiedId(const char *schema, const char *id);
! static bool hasBlobs(Archive *AH);
! static int	dumpBlobs(Archive *AH, void *arg);
! static int	dumpBlobComments(Archive *AH, void *arg);
  static void dumpDatabase(Archive *AH);
  static void dumpEncoding(Archive *AH);
  static void dumpStdStrings(Archive *AH);
--- 190,198 ----
  static char *getFormattedTypeName(Oid oid, OidOptions opts);
  static char *myFormatType(const char *typname, int32 typmod);
  static const char *fmtQualifiedId(const char *schema, const char *id);
! static void getBlobs(Archive *AH);
! static void dumpBlobItem(Archive *AH, BlobInfo *binfo);
! static int  dumpBlobData(Archive *AH, void *arg);
  static void dumpDatabase(Archive *AH);
  static void dumpEncoding(Archive *AH);
  static void dumpStdStrings(Archive *AH);
***************
*** 701,725 **** main(int argc, char **argv)
  			getTableDataFKConstraints();
  	}
  
! 	if (outputBlobs && hasBlobs(g_fout))
! 	{
! 		/* Add placeholders to allow correct sorting of blobs */
! 		DumpableObject *blobobj;
! 		DumpableObject *blobcobj;
! 
! 		blobobj = (DumpableObject *) malloc(sizeof(DumpableObject));
! 		blobobj->objType = DO_BLOBS;
! 		blobobj->catId = nilCatalogId;
! 		AssignDumpId(blobobj);
! 		blobobj->name = strdup("BLOBS");
! 
! 		blobcobj = (DumpableObject *) malloc(sizeof(DumpableObject));
! 		blobcobj->objType = DO_BLOB_COMMENTS;
! 		blobcobj->catId = nilCatalogId;
! 		AssignDumpId(blobcobj);
! 		blobcobj->name = strdup("BLOB COMMENTS");
! 		addObjectDependency(blobcobj, blobobj->dumpId);
! 	}
  
  	/*
  	 * Collect dependency data to assist in ordering the objects.
--- 701,708 ----
  			getTableDataFKConstraints();
  	}
  
! 	if (outputBlobs)
! 		getBlobs(g_fout);
  
  	/*
  	 * Collect dependency data to assist in ordering the objects.
***************
*** 1938,1980 **** dumpStdStrings(Archive *AH)
  
  
  /*
!  * hasBlobs:
   *	Test whether database contains any large objects
   */
! static bool
! hasBlobs(Archive *AH)
  {
! 	bool		result;
! 	const char *blobQry;
! 	PGresult   *res;
  
  	/* Make sure we are in proper schema */
  	selectSourceSchema("pg_catalog");
  
  	/* Check for BLOB OIDs */
  	if (AH->remoteVersion >= 80500)
! 		blobQry = "SELECT oid FROM pg_largeobject_metadata LIMIT 1";
  	else if (AH->remoteVersion >= 70100)
! 		blobQry = "SELECT loid FROM pg_largeobject LIMIT 1";
  	else
! 		blobQry = "SELECT oid FROM pg_class WHERE relkind = 'l' LIMIT 1";
  
! 	res = PQexec(g_conn, blobQry);
! 	check_sql_result(res, g_conn, blobQry, PGRES_TUPLES_OK);
  
! 	result = PQntuples(res) > 0;
  
  	PQclear(res);
  
! 	return result;
  }
  
  /*
!  * dumpBlobs:
!  *	dump all blobs
   */
  static int
! dumpBlobs(Archive *AH, void *arg)
  {
  	const char *blobQry;
  	const char *blobFetchQry;
--- 1921,2066 ----
  
  
  /*
!  * getBlobs:
   *	Test whether database contains any large objects
   */
! static void
! getBlobs(Archive *AH)
  {
! 	PQExpBuffer		blobQry = createPQExpBuffer();
! 	BlobInfo	   *binfo;
! 	DumpableObject *bdata;
! 	PGresult	   *res;
! 	int				i;
! 
! 	/* Verbose message */
! 	if (g_verbose)
! 		write_msg(NULL, "reading binary large objects\n");
  
  	/* Make sure we are in proper schema */
  	selectSourceSchema("pg_catalog");
  
  	/* Check for BLOB OIDs */
  	if (AH->remoteVersion >= 80500)
! 		appendPQExpBuffer(blobQry,
! 						  "SELECT oid, (%s lomowner), lomacl,"
! 						  " obj_description(oid, 'pg_largeobject')"
! 						  " FROM pg_largeobject_metadata",
! 						  username_subquery);
! 	else if (AH->remoteVersion >= 70200)
! 		appendPQExpBuffer(blobQry,
! 						  "SELECT DISTINCT loid, NULL, NULL,"
! 						  " obj_description(loid, 'pg_largeobject')"
! 						  " FROM pg_largeobject");
  	else if (AH->remoteVersion >= 70100)
! 		appendPQExpBuffer(blobQry,
! 						  "SELECT DISTINCT loid, NULL, NULL,"
! 						  " obj_description(loid)"
! 						  " FROM pg_largeobject");
  	else
! 		appendPQExpBuffer(blobQry,
! 						  "SELECT DISTINCT oid, NULL, NULL,"
! 						  " obj_description(oid)"
! 						  " FROM pg_class WHERE relkind = 'l'");
  
! 	res = PQexec(g_conn, blobQry->data);
! 	check_sql_result(res, g_conn, blobQry->data, PGRES_TUPLES_OK);
! 
! 	/*
! 	 * Now, a large object has its own "BLOB ITEM" section to
! 	 * declare itself.
! 	 */
! 	for (i = 0; i < PQntuples(res); i++)
! 	{
! 		binfo = (BlobInfo *) malloc(sizeof(BlobInfo));
! 		binfo->dobj.objType = DO_BLOB_ITEM;
! 		binfo->dobj.catId = nilCatalogId;
! 		AssignDumpId(&binfo->dobj);
  
! 		binfo->dobj.name = strdup(PQgetvalue(res, i, 0));
! 		binfo->rolname = strdup(PQgetvalue(res, i, 1));
! 		binfo->blobacl = strdup(PQgetvalue(res, i, 2));
! 		binfo->blobdescr = strdup(PQgetvalue(res, i, 3));
! 	}
! 
! 	/*
! 	 * If we have a large object at least, "BLOB DATA" section
! 	 * is also necessary.
! 	 */
! 	if (PQntuples(res) > 0)
! 	{
! 		bdata = (DumpableObject *) malloc(sizeof(DumpableObject));
! 		bdata->objType = DO_BLOB_DATA;
! 		bdata->catId = nilCatalogId;
! 		AssignDumpId(bdata);
! 		bdata->name = strdup("BLOBS");
! 	}
  
  	PQclear(res);
+ }
  
! /*
!  * dumpBlobItem
!  *
!  * dump a definition of the given large object
!  */
! static void
! dumpBlobItem(Archive *AH, BlobInfo *binfo)
! {
! 	PQExpBuffer		aquery = createPQExpBuffer();
! 	PQExpBuffer		bquery = createPQExpBuffer();
! 	PQExpBuffer		dquery = createPQExpBuffer();
! 
! 	/*
! 	 * Cleanup a large object
! 	 */
! 	appendPQExpBuffer(dquery, "SELECT lo_unlink(%s);\n", binfo->dobj.name);
! 
! 	/*
! 	 * Create an empty large object
! 	 */
! 	appendPQExpBuffer(bquery, "SELECT lo_create(%s);\n", binfo->dobj.name);
! 
! 	/*
! 	 * Create a comment on large object, if necessary
! 	 */
! 	if (strlen(binfo->blobdescr) > 0)
! 	{
! 		appendPQExpBuffer(bquery, "\nCOMMENT ON LARGE OBJECT %s IS ",
! 						  binfo->dobj.name);
! 		appendStringLiteralAH(bquery, binfo->blobdescr, AH);
! 		appendPQExpBuffer(bquery, ";\n");
! 	}
! 
! 	ArchiveEntry(AH, binfo->dobj.catId, binfo->dobj.dumpId,
! 				 binfo->dobj.name,
! 				 NULL, NULL,
! 				 binfo->rolname, false,
! 				 "BLOB ITEM", SECTION_DATA,
! 				 bquery->data, dquery->data, NULL,
! 				 binfo->dobj.dependencies, binfo->dobj.nDeps,
! 				 NULL, NULL);
! 
! 	/*
! 	 * Dump access privileges, if necessary
! 	 */
! 	dumpACL(AH, binfo->dobj.catId, binfo->dobj.dumpId,
! 			"LARGE OBJECT",
! 			binfo->dobj.name, NULL,
! 			binfo->dobj.name, NULL,
! 			binfo->rolname, binfo->blobacl);
! 
! 	destroyPQExpBuffer(aquery);
! 	destroyPQExpBuffer(bquery);
! 	destroyPQExpBuffer(dquery);
  }
  
  /*
!  * dumpBlobData:
!  *	dump all the data contents of large object
   */
  static int
! dumpBlobData(Archive *AH, void *arg)
  {
  	const char *blobQry;
  	const char *blobFetchQry;
***************
*** 2022,2028 **** dumpBlobs(Archive *AH, void *arg)
  			loFd = lo_open(g_conn, blobOid, INV_READ);
  			if (loFd == -1)
  			{
! 				write_msg(NULL, "dumpBlobs(): could not open large object: %s",
  						  PQerrorMessage(g_conn));
  				exit_nicely();
  			}
--- 2108,2114 ----
  			loFd = lo_open(g_conn, blobOid, INV_READ);
  			if (loFd == -1)
  			{
! 				write_msg(NULL, "dumpBlobData(): could not open large object: %s",
  						  PQerrorMessage(g_conn));
  				exit_nicely();
  			}
***************
*** 2035,2041 **** dumpBlobs(Archive *AH, void *arg)
  				cnt = lo_read(g_conn, loFd, buf, LOBBUFSIZE);
  				if (cnt < 0)
  				{
! 					write_msg(NULL, "dumpBlobs(): error reading large object: %s",
  							  PQerrorMessage(g_conn));
  					exit_nicely();
  				}
--- 2121,2127 ----
  				cnt = lo_read(g_conn, loFd, buf, LOBBUFSIZE);
  				if (cnt < 0)
  				{
! 					write_msg(NULL, "dumpBlobData(): error reading large object: %s",
  							  PQerrorMessage(g_conn));
  					exit_nicely();
  				}
***************
*** 2054,2187 **** dumpBlobs(Archive *AH, void *arg)
  	return 1;
  }
  
- /*
-  * dumpBlobComments
-  *	dump all blob properties.
-  *  It has "BLOB COMMENTS" tag due to the historical reason, but note
-  *  that it is the routine to dump all the properties of blobs.
-  *
-  * Since we don't provide any way to be selective about dumping blobs,
-  * there's no need to be selective about their comments either.  We put
-  * all the comments into one big TOC entry.
-  */
- static int
- dumpBlobComments(Archive *AH, void *arg)
- {
- 	const char *blobQry;
- 	const char *blobFetchQry;
- 	PQExpBuffer cmdQry = createPQExpBuffer();
- 	PGresult   *res;
- 	int			i;
- 
- 	if (g_verbose)
- 		write_msg(NULL, "saving large object properties\n");
- 
- 	/* Make sure we are in proper schema */
- 	selectSourceSchema("pg_catalog");
- 
- 	/* Cursor to get all BLOB comments */
- 	if (AH->remoteVersion >= 80500)
- 		blobQry = "DECLARE blobcmt CURSOR FOR SELECT oid, "
- 			"obj_description(oid, 'pg_largeobject'), "
- 			"pg_get_userbyid(lomowner), lomacl "
- 			"FROM pg_largeobject_metadata";
- 	else if (AH->remoteVersion >= 70300)
- 		blobQry = "DECLARE blobcmt CURSOR FOR SELECT loid, "
- 			"obj_description(loid, 'pg_largeobject'), NULL, NULL "
- 			"FROM (SELECT DISTINCT loid FROM "
- 			"pg_description d JOIN pg_largeobject l ON (objoid = loid) "
- 			"WHERE classoid = 'pg_largeobject'::regclass) ss";
- 	else if (AH->remoteVersion >= 70200)
- 		blobQry = "DECLARE blobcmt CURSOR FOR SELECT loid, "
- 			"obj_description(loid, 'pg_largeobject'), NULL, NULL "
- 			"FROM (SELECT DISTINCT loid FROM pg_largeobject) ss";
- 	else if (AH->remoteVersion >= 70100)
- 		blobQry = "DECLARE blobcmt CURSOR FOR SELECT loid, "
- 			"obj_description(loid), NULL, NULL "
- 			"FROM (SELECT DISTINCT loid FROM pg_largeobject) ss";
- 	else
- 		blobQry = "DECLARE blobcmt CURSOR FOR SELECT oid, "
- 			"	( "
- 			"		SELECT description "
- 			"		FROM pg_description pd "
- 			"		WHERE pd.objoid=pc.oid "
- 			"	), NULL, NULL "
- 			"FROM pg_class pc WHERE relkind = 'l'";
- 
- 	res = PQexec(g_conn, blobQry);
- 	check_sql_result(res, g_conn, blobQry, PGRES_COMMAND_OK);
- 
- 	/* Command to fetch from cursor */
- 	blobFetchQry = "FETCH 100 IN blobcmt";
- 
- 	do
- 	{
- 		PQclear(res);
- 
- 		/* Do a fetch */
- 		res = PQexec(g_conn, blobFetchQry);
- 		check_sql_result(res, g_conn, blobFetchQry, PGRES_TUPLES_OK);
- 
- 		/* Process the tuples, if any */
- 		for (i = 0; i < PQntuples(res); i++)
- 		{
- 			Oid			blobOid = atooid(PQgetvalue(res, i, 0));
- 			char	   *lo_comment = PQgetvalue(res, i, 1);
- 			char	   *lo_owner = PQgetvalue(res, i, 2);
- 			char	   *lo_acl = PQgetvalue(res, i, 3);
- 			char		lo_name[32];
- 
- 			resetPQExpBuffer(cmdQry);
- 
- 			/* comment on the blob */
- 			if (!PQgetisnull(res, i, 1))
- 			{
- 				appendPQExpBuffer(cmdQry,
- 								  "COMMENT ON LARGE OBJECT %u IS ", blobOid);
- 				appendStringLiteralAH(cmdQry, lo_comment, AH);
- 				appendPQExpBuffer(cmdQry, ";\n");
- 			}
- 
- 			/* dump blob ownership, if necessary */
- 			if (!PQgetisnull(res, i, 2))
- 			{
- 				appendPQExpBuffer(cmdQry,
- 								  "ALTER LARGE OBJECT %u OWNER TO %s;\n",
- 								  blobOid, lo_owner);
- 			}
- 
- 			/* dump blob privileges, if necessary */
- 			if (!PQgetisnull(res, i, 3) &&
- 				!dataOnly && !aclsSkip)
- 			{
- 				snprintf(lo_name, sizeof(lo_name), "%u", blobOid);
- 				if (!buildACLCommands(lo_name, NULL, "LARGE OBJECT",
- 									  lo_acl, lo_owner, "",
- 									  AH->remoteVersion, cmdQry))
- 				{
- 					write_msg(NULL, "could not parse ACL (%s) for "
- 							  "large object %u", lo_acl, blobOid);
- 					exit_nicely();
- 				}
- 			}
- 
- 			if (cmdQry->len > 0)
- 			{
- 				appendPQExpBuffer(cmdQry, "\n");
- 				archputs(cmdQry->data, AH);
- 			}
- 		}
- 	} while (PQntuples(res) > 0);
- 
- 	PQclear(res);
- 
- 	archputs("\n", AH);
- 
- 	destroyPQExpBuffer(cmdQry);
- 
- 	return 1;
- }
- 
  static void
  binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer,
  											   Oid pg_type_oid)
--- 2140,2145 ----
***************
*** 6524,6544 **** dumpDumpableObject(Archive *fout, DumpableObject *dobj)
  		case DO_DEFAULT_ACL:
  			dumpDefaultACL(fout, (DefaultACLInfo *) dobj);
  			break;
! 		case DO_BLOBS:
! 			ArchiveEntry(fout, dobj->catId, dobj->dumpId,
! 						 dobj->name, NULL, NULL, "",
! 						 false, "BLOBS", SECTION_DATA,
! 						 "", "", NULL,
! 						 dobj->dependencies, dobj->nDeps,
! 						 dumpBlobs, NULL);
  			break;
! 		case DO_BLOB_COMMENTS:
  			ArchiveEntry(fout, dobj->catId, dobj->dumpId,
  						 dobj->name, NULL, NULL, "",
! 						 false, "BLOB COMMENTS", SECTION_DATA,
  						 "", "", NULL,
  						 dobj->dependencies, dobj->nDeps,
! 						 dumpBlobComments, NULL);
  			break;
  	}
  }
--- 6482,6497 ----
  		case DO_DEFAULT_ACL:
  			dumpDefaultACL(fout, (DefaultACLInfo *) dobj);
  			break;
! 		case DO_BLOB_ITEM:
! 			dumpBlobItem(fout, (BlobInfo *) dobj);
  			break;
! 		case DO_BLOB_DATA:
  			ArchiveEntry(fout, dobj->catId, dobj->dumpId,
  						 dobj->name, NULL, NULL, "",
! 						 false, "BLOB DATA", SECTION_DATA,
  						 "", "", NULL,
  						 dobj->dependencies, dobj->nDeps,
! 						 dumpBlobData, NULL);
  			break;
  	}
  }
***************
*** 10394,10401 **** dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
  {
  	PQExpBuffer sql;
  
! 	/* Do nothing if ACL dump is not enabled */
! 	if (dataOnly || aclsSkip)
  		return;
  
  	sql = createPQExpBuffer();
--- 10347,10359 ----
  {
  	PQExpBuffer sql;
  
! 	/*
! 	 * Do nothing if ACL dump is not enabled
! 	 *
! 	 * Note that the caller has to check necessity to dump ACLs
! 	 * depending on --data-only / --schema-only.
! 	 */
! 	if (aclsSkip)
  		return;
  
  	sql = createPQExpBuffer();
*** a/src/bin/pg_dump/pg_dump.h
--- b/src/bin/pg_dump/pg_dump.h
***************
*** 115,122 **** typedef enum
  	DO_FDW,
  	DO_FOREIGN_SERVER,
  	DO_DEFAULT_ACL,
! 	DO_BLOBS,
! 	DO_BLOB_COMMENTS
  } DumpableObjectType;
  
  typedef struct _dumpableObject
--- 115,122 ----
  	DO_FDW,
  	DO_FOREIGN_SERVER,
  	DO_DEFAULT_ACL,
! 	DO_BLOB_DATA,
! 	DO_BLOB_ITEM,
  } DumpableObjectType;
  
  typedef struct _dumpableObject
***************
*** 443,448 **** typedef struct _defaultACLInfo
--- 443,456 ----
  	char	   *defaclacl;
  } DefaultACLInfo;
  
+ typedef struct _blobInfo
+ {
+ 	DumpableObject	dobj;
+ 	char	   *rolname;
+ 	char	   *blobacl;
+ 	char	   *blobdescr;
+ } BlobInfo;
+ 
  /* global decls */
  extern bool force_quotes;		/* double-quotes for identifiers flag */
  extern bool g_verbose;			/* verbose flag */
*** a/src/bin/pg_dump/pg_dump_sort.c
--- b/src/bin/pg_dump/pg_dump_sort.c
***************
*** 92,99 **** static const int newObjectTypePriority[] =
  	14,							/* DO_FDW */
  	15,							/* DO_FOREIGN_SERVER */
  	27,							/* DO_DEFAULT_ACL */
! 	20,							/* DO_BLOBS */
! 	21							/* DO_BLOB_COMMENTS */
  };
  
  
--- 92,99 ----
  	14,							/* DO_FDW */
  	15,							/* DO_FOREIGN_SERVER */
  	27,							/* DO_DEFAULT_ACL */
! 	21,							/* DO_BLOB_DATA */
! 	20,							/* DO_BLOB_ITEM */
  };
  
  
***************
*** 1146,1159 **** describeDumpableObject(DumpableObject *obj, char *buf, int bufsize)
  					 "DEFAULT ACL %s  (ID %d OID %u)",
  					 obj->name, obj->dumpId, obj->catId.oid);
  			return;
! 		case DO_BLOBS:
  			snprintf(buf, bufsize,
! 					 "BLOBS  (ID %d)",
  					 obj->dumpId);
  			return;
! 		case DO_BLOB_COMMENTS:
  			snprintf(buf, bufsize,
! 					 "BLOB COMMENTS  (ID %d)",
  					 obj->dumpId);
  			return;
  	}
--- 1146,1159 ----
  					 "DEFAULT ACL %s  (ID %d OID %u)",
  					 obj->name, obj->dumpId, obj->catId.oid);
  			return;
! 		case DO_BLOB_DATA:
  			snprintf(buf, bufsize,
! 					 "BLOB DATA  (ID %d)",
  					 obj->dumpId);
  			return;
! 		case DO_BLOB_ITEM:
  			snprintf(buf, bufsize,
! 					 "BLOB ITEM  (ID %d)",
  					 obj->dumpId);
  			return;
  	}
