Skip to content

Commit 5da713f

Browse files
committed
Fix pg_dump to dump shell types.
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.
1 parent 8bd45a3 commit 5da713f

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ static void dumpType(Archive *fout, TypeInfo *tyinfo);
175175
static void dumpBaseType(Archive *fout, TypeInfo *tyinfo);
176176
static void dumpEnumType(Archive *fout, TypeInfo *tyinfo);
177177
static void dumpRangeType(Archive *fout, TypeInfo *tyinfo);
178+
static void dumpUndefinedType(Archive *fout, TypeInfo *tyinfo);
178179
static void dumpDomain(Archive *fout, TypeInfo *tyinfo);
179180
static void dumpCompositeType(Archive *fout, TypeInfo *tyinfo);
180181
static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo);
@@ -1311,11 +1312,6 @@ selectDumpableType(TypeInfo *tyinfo)
13111312
/* dump only types in dumpable namespaces */
13121313
if (!tyinfo->dobj.namespace->dobj.dump)
13131314
tyinfo->dobj.dump = false;
1314-
1315-
/* skip undefined placeholder types */
1316-
else if (!tyinfo->isDefined)
1317-
tyinfo->dobj.dump = false;
1318-
13191315
else
13201316
tyinfo->dobj.dump = true;
13211317
}
@@ -3425,7 +3421,7 @@ getTypes(Archive *fout, int *numTypes)
34253421
}
34263422
}
34273423

3428-
if (strlen(tyinfo[i].rolname) == 0 && tyinfo[i].isDefined)
3424+
if (strlen(tyinfo[i].rolname) == 0)
34293425
write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",
34303426
tyinfo[i].dobj.name);
34313427
}
@@ -8008,6 +8004,8 @@ dumpType(Archive *fout, TypeInfo *tyinfo)
80088004
dumpEnumType(fout, tyinfo);
80098005
else if (tyinfo->typtype == TYPTYPE_RANGE)
80108006
dumpRangeType(fout, tyinfo);
8007+
else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
8008+
dumpUndefinedType(fout, tyinfo);
80118009
else
80128010
write_msg(NULL, "WARNING: typtype of data type \"%s\" appears to be invalid\n",
80138011
tyinfo->dobj.name);
@@ -8275,6 +8273,73 @@ dumpRangeType(Archive *fout, TypeInfo *tyinfo)
82758273
destroyPQExpBuffer(query);
82768274
}
82778275

8276+
/*
8277+
* dumpUndefinedType
8278+
* writes out to fout the queries to recreate a !typisdefined type
8279+
*
8280+
* This is a shell type, but we use different terminology to distinguish
8281+
* this case from where we have to emit a shell type definition to break
8282+
* circular dependencies. An undefined type shouldn't ever have anything
8283+
* depending on it.
8284+
*/
8285+
static void
8286+
dumpUndefinedType(Archive *fout, TypeInfo *tyinfo)
8287+
{
8288+
PQExpBuffer q = createPQExpBuffer();
8289+
PQExpBuffer delq = createPQExpBuffer();
8290+
PQExpBuffer labelq = createPQExpBuffer();
8291+
char *qtypname;
8292+
8293+
qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
8294+
8295+
/*
8296+
* DROP must be fully qualified in case same name appears in pg_catalog.
8297+
*/
8298+
appendPQExpBuffer(delq, "DROP TYPE %s.",
8299+
fmtId(tyinfo->dobj.namespace->dobj.name));
8300+
appendPQExpBuffer(delq, "%s;\n",
8301+
qtypname);
8302+
8303+
if (binary_upgrade)
8304+
binary_upgrade_set_type_oids_by_type_oid(fout,
8305+
q, tyinfo->dobj.catId.oid);
8306+
8307+
appendPQExpBuffer(q, "CREATE TYPE %s;\n",
8308+
qtypname);
8309+
8310+
appendPQExpBuffer(labelq, "TYPE %s", qtypname);
8311+
8312+
if (binary_upgrade)
8313+
binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
8314+
8315+
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
8316+
tyinfo->dobj.name,
8317+
tyinfo->dobj.namespace->dobj.name,
8318+
NULL,
8319+
tyinfo->rolname, false,
8320+
"TYPE", SECTION_PRE_DATA,
8321+
q->data, delq->data, NULL,
8322+
NULL, 0,
8323+
NULL, NULL);
8324+
8325+
/* Dump Type Comments and Security Labels */
8326+
dumpComment(fout, labelq->data,
8327+
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8328+
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8329+
dumpSecLabel(fout, labelq->data,
8330+
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8331+
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8332+
8333+
dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
8334+
qtypname, NULL, tyinfo->dobj.name,
8335+
tyinfo->dobj.namespace->dobj.name,
8336+
tyinfo->rolname, tyinfo->typacl);
8337+
8338+
destroyPQExpBuffer(q);
8339+
destroyPQExpBuffer(delq);
8340+
destroyPQExpBuffer(labelq);
8341+
}
8342+
82788343
/*
82798344
* dumpBaseType
82808345
* writes out to fout the queries to recreate a user-defined base type

src/bin/pg_dump/pg_dump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ typedef struct _typeInfo
161161
char typtype; /* 'b', 'c', etc */
162162
bool isArray; /* true if auto-generated array type */
163163
bool isDefined; /* true if typisdefined */
164-
/* If it's a dumpable base type, we create a "shell type" entry for it */
164+
/* If needed, we'll create a "shell type" entry for it; link that here: */
165165
struct _shellTypeInfo *shellType; /* shell-type entry, or NULL */
166166
/* If it's a domain, we store links to its constraints here: */
167167
int nDomChecks;

src/test/regress/expected/create_type.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ ERROR: type "shell" already exists
2929
DROP TYPE shell;
3030
DROP TYPE shell; -- fail, type not exist
3131
ERROR: type "shell" does not exist
32+
-- also, let's leave one around for purposes of pg_dump testing
33+
CREATE TYPE myshell;
3234
--
3335
-- Test type-related default values (broken in releases before PG 7.2)
3436
--

src/test/regress/sql/create_type.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ CREATE TYPE shell; -- fail, type already present
3131
DROP TYPE shell;
3232
DROP TYPE shell; -- fail, type not exist
3333

34+
-- also, let's leave one around for purposes of pg_dump testing
35+
CREATE TYPE myshell;
36+
3437
--
3538
-- Test type-related default values (broken in releases before PG 7.2)
3639
--

0 commit comments

Comments
 (0)