>From 90e3bb7a389cfa02901dd71ae42ff7c89d9fd56a Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <daniel@yesql.se>
Date: Fri, 26 May 2017 14:15:58 +0200
Subject: [PATCH] Fix broken SQL when dumping empty opclass

Defining an empty operator class is valid SQL (albeit perhaps
not useful), but pg_dump fails to dump valid SQL for it. The
following operator class:

  CREATE OPERATOR CLASS foo FOR TYPE uuid USING hash
  AS STORAGE uuid;

is dumped like this:

  CREATE OPERATOR CLASS alt_opc3
  FOR TYPE uuid USING hash FAMILY alt_opc1 AS
  ;

Fix by dumping the STORAGE clause in that case even if it
redundant. Also skip dropping the SCHEMA in the regression
tests which contains empty operator classes to expose this
to pg_upgrade testing.
---
 src/bin/pg_dump/pg_dump.c                   | 9 +++++++++
 src/test/regress/expected/alter_generic.out | 5 -----
 src/test/regress/sql/alter_generic.sql      | 7 -------
 3 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 053ae0e417..2b4ce14a15 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -12674,6 +12674,15 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
 
 	PQclear(res);
 
+	/*
+	 * If we reach here with needComma being false it means we haven't added
+	 * neither STORAGE nor any OPERATOR/FUNCTION(s).  Append the STORAGE with
+	 * the intype, since thats syntactically correct, to avoid rendering
+	 * broken SQL.
+	 */
+	if (!needComma)
+		appendPQExpBuffer(q, "STORAGE %s", opcintype);
+
 	appendPQExpBufferStr(q, ";\n");
 
 	appendPQExpBuffer(labelq, "OPERATOR CLASS %s",
diff --git a/src/test/regress/expected/alter_generic.out b/src/test/regress/expected/alter_generic.out
index 62347bc47e..5574d35eb2 100644
--- a/src/test/regress/expected/alter_generic.out
+++ b/src/test/regress/expected/alter_generic.out
@@ -678,8 +678,3 @@ DROP FOREIGN DATA WRAPPER alt_fdw2 CASCADE;
 DROP FOREIGN DATA WRAPPER alt_fdw3 CASCADE;
 DROP LANGUAGE alt_lang2 CASCADE;
 DROP LANGUAGE alt_lang3 CASCADE;
-DROP SCHEMA alt_nsp1 CASCADE;
-DROP SCHEMA alt_nsp2 CASCADE;
-DROP USER regress_alter_user1;
-DROP USER regress_alter_user2;
-DROP USER regress_alter_user3;
diff --git a/src/test/regress/sql/alter_generic.sql b/src/test/regress/sql/alter_generic.sql
index 342f82856e..382b447df1 100644
--- a/src/test/regress/sql/alter_generic.sql
+++ b/src/test/regress/sql/alter_generic.sql
@@ -580,10 +580,3 @@ DROP FOREIGN DATA WRAPPER alt_fdw3 CASCADE;
 
 DROP LANGUAGE alt_lang2 CASCADE;
 DROP LANGUAGE alt_lang3 CASCADE;
-
-DROP SCHEMA alt_nsp1 CASCADE;
-DROP SCHEMA alt_nsp2 CASCADE;
-
-DROP USER regress_alter_user1;
-DROP USER regress_alter_user2;
-DROP USER regress_alter_user3;
-- 
2.13.0.rc0.45.ge2cb6ab.dirty

