commit 68ba9d3b682c6e56b08d623ebc58e351ce1a6c55
Author: Pavel Stehule <pavel.stehule@gooddata.com>
Date:   Fri Aug 15 13:41:24 2014 +0200

    heikki-lo-if-exists-fix

diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 3aebac8..0049be7 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -429,60 +429,100 @@ RestoreArchive(Archive *AHX)
 					}
 					else
 					{
-						char		buffer[40];
-						char	   *mark;
-						char	   *dropStmt = pg_strdup(te->dropStmt);
-						char	   *dropStmtPtr = dropStmt;
-						PQExpBuffer ftStmt = createPQExpBuffer();
-
 						/*
-						 * Need to inject IF EXISTS clause after ALTER TABLE
-						 * part in ALTER TABLE .. DROP statement
+						 * LO doesn't use DDL stmts, instead it uses lo_unlink
+						 * functions. But it should be injected too.
 						 */
-						if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
+						if (strncmp(te->desc, "BLOB", 4) == 0)
 						{
-							appendPQExpBuffer(ftStmt,
-											  "ALTER TABLE IF EXISTS");
-							dropStmt = dropStmt + 11;
+							Oid	   loid = InvalidOid;
+							char	   *mark;
+
+							/* find and take foid */
+							if (strncmp(te->dropStmt, "SELECT pg_catalog.lo_unlink('", 29) == 0)
+							{
+								unsigned long cvt;
+								char	   *endptr;
+							
+								mark = te->dropStmt + 29;
+
+								if (*mark != '\0')
+								{
+									errno = 0;
+									cvt = strtoul(mark, &endptr, 10);
+
+									/* Ensure we have a valid Oid */
+									if (errno == 0 && mark != endptr && *endptr == '\'')
+										loid = (Oid) cvt;
+								}
+							}
+
+							if (loid == InvalidOid)
+								exit_horribly(modulename, "cannot to identify oid of large object\n");
+
+							/*
+							 * Now we have valid foid and we can generate
+							 * fault tolerant (not existency) SQL statement.
+							 */
+							DropBlobIfExists(AH, loid);
 						}
-
-						/*
-						 * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does not
-						 * support the IF EXISTS clause, and therefore we
-						 * simply emit the original command for such objects.
-						 * For other objects, we need to extract the first
-						 * part of the DROP which includes the object type.
-						 * Most of the time this matches te->desc, so search
-						 * for that; however for the different kinds of
-						 * CONSTRAINTs, we know to search for hardcoded "DROP
-						 * CONSTRAINT" instead.
-						 */
-						if (strcmp(te->desc, "DEFAULT") == 0)
-							appendPQExpBuffer(ftStmt, "%s", dropStmt);
 						else
 						{
-							if (strcmp(te->desc, "CONSTRAINT") == 0 ||
-								strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
-								strcmp(te->desc, "FK CONSTRAINT") == 0)
-								strcpy(buffer, "DROP CONSTRAINT");
+							char		buffer[40];
+							char	   *mark;
+							char	   *dropStmt = pg_strdup(te->dropStmt);
+							char	   *dropStmtPtr = dropStmt;
+							PQExpBuffer ftStmt = createPQExpBuffer();
+
+							/*
+							 * Need to inject IF EXISTS clause after ALTER TABLE
+							 * part in ALTER TABLE .. DROP statement
+							 */
+							if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
+							{
+								appendPQExpBuffer(ftStmt,
+												  "ALTER TABLE IF EXISTS");
+								dropStmt = dropStmt + 11;
+							}
+
+							/*
+							 * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does not
+							 * support the IF EXISTS clause, and therefore we
+							 * simply emit the original command for such objects.
+							 * For other objects, we need to extract the first
+							 * part of the DROP which includes the object type.
+							 * Most of the time this matches te->desc, so search
+							 * for that; however for the different kinds of
+							 * CONSTRAINTs, we know to search for hardcoded "DROP
+							 * CONSTRAINT" instead.
+							 */
+							if (strcmp(te->desc, "DEFAULT") == 0)
+								appendPQExpBuffer(ftStmt, "%s", dropStmt);
 							else
-								snprintf(buffer, sizeof(buffer), "DROP %s",
-										 te->desc);
+							{
+								if (strcmp(te->desc, "CONSTRAINT") == 0 ||
+									strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
+									strcmp(te->desc, "FK CONSTRAINT") == 0)
+									strcpy(buffer, "DROP CONSTRAINT");
+								else
+									snprintf(buffer, sizeof(buffer), "DROP %s",
+											 te->desc);
 
-							mark = strstr(dropStmt, buffer);
-							Assert(mark != NULL);
+								mark = strstr(dropStmt, buffer);
+								Assert(mark != NULL);
 
-							*mark = '\0';
-							appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
-											  dropStmt, buffer,
-											  mark + strlen(buffer));
-						}
+								*mark = '\0';
+								appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
+												  dropStmt, buffer,
+												  mark + strlen(buffer));
+							}
 
-						ahprintf(AH, "%s", ftStmt->data);
+							ahprintf(AH, "%s", ftStmt->data);
 
-						destroyPQExpBuffer(ftStmt);
+							destroyPQExpBuffer(ftStmt);
 
-						pg_free(dropStmtPtr);
+							pg_free(dropStmtPtr);
+						}
 					}
 				}
 			}
