]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix pg_dump to dump shell types.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 4 Aug 2015 23:34:12 +0000 (19:34 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 4 Aug 2015 23:34:12 +0000 (19:34 -0400)
Per discussion, it really ought to do this.  The original choice to
exclude shell types was probably made in the dark ages before we made
it harder to accidentally create shell types; but that was in 7.3.

Also, cause the standard regression tests to leave a shell type behind,
for convenience in testing the case in pg_dump and pg_upgrade.

Back-patch to all supported branches.

src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dump.h
src/test/regress/expected/create_type.out
src/test/regress/sql/create_type.sql

index 710b779c7eee30e58a736a21590f7b4f453f3a10..a7d106d44b2f4550a5ed286b432bdfb2c7dbc5ef 100644 (file)
@@ -163,6 +163,7 @@ static void dumpExtension(Archive *fout, ExtensionInfo *extinfo);
 static void dumpType(Archive *fout, TypeInfo *tyinfo);
 static void dumpBaseType(Archive *fout, TypeInfo *tyinfo);
 static void dumpEnumType(Archive *fout, TypeInfo *tyinfo);
+static void dumpUndefinedType(Archive *fout, TypeInfo *tyinfo);
 static void dumpDomain(Archive *fout, TypeInfo *tyinfo);
 static void dumpCompositeType(Archive *fout, TypeInfo *tyinfo);
 static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo);
@@ -1184,11 +1185,6 @@ selectDumpableType(TypeInfo *tyinfo)
        /* dump only types in dumpable namespaces */
        if (!tyinfo->dobj.namespace->dobj.dump)
                tyinfo->dobj.dump = false;
-
-       /* skip undefined placeholder types */
-       else if (!tyinfo->isDefined)
-               tyinfo->dobj.dump = false;
-
        else
                tyinfo->dobj.dump = true;
 }
@@ -3118,7 +3114,7 @@ getTypes(int *numTypes)
                        }
                }
 
-               if (strlen(tyinfo[i].rolname) == 0 && tyinfo[i].isDefined)
+               if (strlen(tyinfo[i].rolname) == 0)
                        write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",
                                          tyinfo[i].dobj.name);
        }
@@ -7408,6 +7404,11 @@ dumpType(Archive *fout, TypeInfo *tyinfo)
                dumpCompositeType(fout, tyinfo);
        else if (tyinfo->typtype == TYPTYPE_ENUM)
                dumpEnumType(fout, tyinfo);
+       else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
+               dumpUndefinedType(fout, tyinfo);
+       else
+               write_msg(NULL, "WARNING: typtype of data type \"%s\" appears to be invalid\n",
+                                 tyinfo->dobj.name);
 }
 
 /*
@@ -7531,6 +7532,67 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo)
        destroyPQExpBuffer(query);
 }
 
+/*
+ * dumpUndefinedType
+ *       writes out to fout the queries to recreate a !typisdefined type
+ *
+ * This is a shell type, but we use different terminology to distinguish
+ * this case from where we have to emit a shell type definition to break
+ * circular dependencies.  An undefined type shouldn't ever have anything
+ * depending on it.
+ */
+static void
+dumpUndefinedType(Archive *fout, TypeInfo *tyinfo)
+{
+       PQExpBuffer q = createPQExpBuffer();
+       PQExpBuffer delq = createPQExpBuffer();
+       PQExpBuffer labelq = createPQExpBuffer();
+       char       *qtypname;
+
+       qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
+
+       /*
+        * DROP must be fully qualified in case same name appears in pg_catalog.
+        */
+       appendPQExpBuffer(delq, "DROP TYPE %s.",
+                                         fmtId(tyinfo->dobj.namespace->dobj.name));
+       appendPQExpBuffer(delq, "%s;\n",
+                                         qtypname);
+
+       if (binary_upgrade)
+               binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
+
+       appendPQExpBuffer(q, "CREATE TYPE %s;\n",
+                                         qtypname);
+
+       appendPQExpBuffer(labelq, "TYPE %s", qtypname);
+
+       if (binary_upgrade)
+               binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
+
+       ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
+                                tyinfo->dobj.name,
+                                tyinfo->dobj.namespace->dobj.name,
+                                NULL,
+                                tyinfo->rolname, false,
+                                "TYPE", SECTION_PRE_DATA,
+                                q->data, delq->data, NULL,
+                                NULL, 0,
+                                NULL, NULL);
+
+       /* Dump Type Comments and Security Labels */
+       dumpComment(fout, labelq->data,
+                               tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
+                               tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
+       dumpSecLabel(fout, labelq->data,
+                                tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
+                                tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
+
+       destroyPQExpBuffer(q);
+       destroyPQExpBuffer(delq);
+       destroyPQExpBuffer(labelq);
+}
+
 /*
  * dumpBaseType
  *       writes out to fout the queries to recreate a user-defined base type
index 828b6cf32256ceeab005003f05275990e6e264fc..90296ca7384d6abae25e5636e3fb64dcadd87b3a 100644 (file)
@@ -169,7 +169,7 @@ typedef struct _typeInfo
        char            typtype;                /* 'b', 'c', etc */
        bool            isArray;                /* true if auto-generated array type */
        bool            isDefined;              /* true if typisdefined */
-       /* If it's a dumpable base type, we create a "shell type" entry for it */
+       /* If needed, we'll create a "shell type" entry for it; link that here: */
        struct _shellTypeInfo *shellType;       /* shell-type entry, or NULL */
        /* If it's a domain, we store links to its constraints here: */
        int                     nDomChecks;
index 6dfe9169854dcbdf884e92b28faab2cbfebb359a..09459adc3fecc9e12e2acb2da13a268a888c177f 100644 (file)
@@ -29,6 +29,8 @@ ERROR:  type "shell" already exists
 DROP TYPE shell;
 DROP TYPE shell;     -- fail, type not exist
 ERROR:  type "shell" does not exist
+-- also, let's leave one around for purposes of pg_dump testing
+CREATE TYPE myshell;
 --
 -- Test type-related default values (broken in releases before PG 7.2)
 --
index a4906b64e10bfd91b9f5b916fed3aeb932b4aa66..62bd53cfbae431cf75084323d745e4fa3fbceedd 100644 (file)
@@ -31,6 +31,9 @@ CREATE TYPE shell;   -- fail, type already present
 DROP TYPE shell;
 DROP TYPE shell;     -- fail, type not exist
 
+-- also, let's leave one around for purposes of pg_dump testing
+CREATE TYPE myshell;
+
 --
 -- Test type-related default values (broken in releases before PG 7.2)
 --