From 9c6f8be2b14c170b589fdbd48e3db42c45efb2cc Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Wed, 4 Jul 2012 17:36:50 -0400
Subject: [PATCH] Have pg_dump in binary-upgrade mode properly drop
 user-created extensions that might exist in the new empty cluster databases,
 like plpgsql.

Backpatch to 9.2.
---
 src/bin/pg_dump/pg_dump.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index afb28a870e6..7d672878ed3 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -7331,6 +7331,16 @@ dumpExtension(Archive *fout, ExtensionInfo *extinfo)
 		int			n;
 
 		appendPQExpBuffer(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
+
+		/*
+		 *	We unconditionally create the extension, so we must drop it if it
+		 *	exists.  This could happen if the user deleted 'plpgsql' and then
+		 *	readded it, causing its oid to be greater than FirstNormalObjectId.
+		 *	The FirstNormalObjectId test was kept to avoid repeatedly dropping
+		 *	and recreating extensions like 'plpgsql'.
+		 */
+		appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
+
 		appendPQExpBuffer(q,
 						  "SELECT binary_upgrade.create_empty_extension(");
 		appendStringLiteralAH(q, extinfo->dobj.name, fout);
-- 
GitLab