Skip to content

Commit 9f20765

Browse files
committed
Adds domain dumping support to pg_dump.
Rod Taylor
1 parent 811f7df commit 9f20765

File tree

2 files changed

+131
-4
lines changed

2 files changed

+131
-4
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.245 2002/04/05 00:31:31 tgl Exp $
25+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.246 2002/04/05 11:51:12 momjian Exp $
2626
*
2727
*-------------------------------------------------------------------------
2828
*/
@@ -77,6 +77,7 @@ typedef enum _formatLiteralOptions
7777
static void dumpComment(Archive *fout, const char *target, const char *oid,
7878
const char *classname, int subid,
7979
const char *((*deps)[]));
80+
static void dumpOneDomain(Archive *fout, TypeInfo *tinfo);
8081
static void dumpSequence(Archive *fout, TableInfo tbinfo, const bool schemaOnly, const bool dataOnly);
8182
static void dumpACL(Archive *fout, TableInfo tbinfo);
8283
static void dumpTriggers(Archive *fout, const char *tablename,
@@ -91,6 +92,7 @@ static Oid findLastBuiltinOid_V71(const char *);
9192
static Oid findLastBuiltinOid_V70(void);
9293
static void setMaxOid(Archive *fout);
9394

95+
9496
static void AddAcl(char *aclbuf, const char *keyword);
9597
static char *GetPrivileges(Archive *AH, const char *s);
9698

@@ -1352,6 +1354,7 @@ getTypes(int *numTypes)
13521354
int i_typisdefined;
13531355
int i_usename;
13541356
int i_typedefn;
1357+
int i_typtype;
13551358

13561359
/* find all base types */
13571360

@@ -1368,7 +1371,7 @@ getTypes(int *numTypes)
13681371
"typinput, typoutput, typreceive, typsend, typelem, typdelim, "
13691372
"typdefault, typrelid, typalign, 'p'::char as typstorage, typbyval, typisdefined, "
13701373
"(select usename from pg_user where typowner = usesysid) as usename, "
1371-
"typname as typedefn "
1374+
"typname as typedefn, typtype "
13721375
"from pg_type");
13731376
}
13741377
else
@@ -1377,7 +1380,7 @@ getTypes(int *numTypes)
13771380
"typinput, typoutput, typreceive, typsend, typelem, typdelim, "
13781381
"typdefault, typrelid, typalign, typstorage, typbyval, typisdefined, "
13791382
"(select usename from pg_user where typowner = usesysid) as usename, "
1380-
"format_type(pg_type.oid, NULL) as typedefn "
1383+
"format_type(pg_type.oid, NULL) as typedefn, typtype "
13811384
"from pg_type");
13821385
}
13831386

@@ -1412,6 +1415,7 @@ getTypes(int *numTypes)
14121415
i_typisdefined = PQfnumber(res, "typisdefined");
14131416
i_usename = PQfnumber(res, "usename");
14141417
i_typedefn = PQfnumber(res, "typedefn");
1418+
i_typtype = PQfnumber(res, "typtype");
14151419

14161420
for (i = 0; i < ntups; i++)
14171421
{
@@ -1435,6 +1439,7 @@ getTypes(int *numTypes)
14351439
tinfo[i].typstorage = strdup(PQgetvalue(res, i, i_typstorage));
14361440
tinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
14371441
tinfo[i].typedefn = strdup(PQgetvalue(res, i, i_typedefn));
1442+
tinfo[i].typtype = strdup(PQgetvalue(res, i, i_typtype));
14381443

14391444
if (strlen(tinfo[i].usename) == 0)
14401445
write_msg(NULL, "WARNING: owner of data type %s appears to be invalid\n",
@@ -3188,6 +3193,88 @@ dumpDBComment(Archive *fout)
31883193
destroyPQExpBuffer(query);
31893194
}
31903195

3196+
/*
3197+
* dumpOneDomain
3198+
* wites out to fout the queries to recrease a user-defined domains
3199+
* as requested by dumpTypes
3200+
*/
3201+
void
3202+
dumpOneDomain(Archive *fout, TypeInfo *tinfo)
3203+
{
3204+
PQExpBuffer q = createPQExpBuffer();
3205+
PQExpBuffer delq = createPQExpBuffer();
3206+
3207+
PGresult *res;
3208+
PQExpBuffer query = createPQExpBuffer();
3209+
int ntups;
3210+
const char *((*deps)[]);
3211+
int depIdx = 0;
3212+
3213+
3214+
deps = malloc(sizeof(char *) * 10);
3215+
3216+
/* Fetch domain specific details */
3217+
resetPQExpBuffer(query);
3218+
appendPQExpBuffer(query, "SELECT typnotnull, "
3219+
"format_type(typbasetype, typtypmod) as typdefn, "
3220+
"typbasetype "
3221+
"FROM pg_type "
3222+
"WHERE typname = '%s'",
3223+
tinfo->typname);
3224+
3225+
res = PQexec(g_conn, query->data);
3226+
if (!res ||
3227+
PQresultStatus(res) != PGRES_TUPLES_OK)
3228+
{
3229+
write_msg(NULL, "query to obtain domain information failed: %s", PQerrorMessage(g_conn));
3230+
exit_nicely();
3231+
}
3232+
3233+
/* Expecting a single result only */
3234+
ntups = PQntuples(res);
3235+
if (ntups != 1)
3236+
write_msg(NULL, "Domain %s non-existant.", fmtId(tinfo->typname, force_quotes));
3237+
3238+
3239+
/* Drop the old copy */
3240+
resetPQExpBuffer(delq);
3241+
appendPQExpBuffer(delq, "DROP DOMAIN %s RESTRICT;\n", fmtId(tinfo->typname, force_quotes));
3242+
3243+
resetPQExpBuffer(q);
3244+
appendPQExpBuffer(q,
3245+
"CREATE DOMAIN %s AS %s",
3246+
fmtId(tinfo->typname, force_quotes),
3247+
PQgetvalue(res, 0, PQfnumber(res, "typdefn"))
3248+
);
3249+
3250+
/* Depends on the base type */
3251+
(*deps)[depIdx++] = strdup(PQgetvalue(res, 0, PQfnumber(res, "typbasetype")));
3252+
3253+
if (PQgetvalue(res, 0, PQfnumber(res, "typnotnull"))[0] == 't')
3254+
appendPQExpBuffer(q, " NOT NULL");
3255+
3256+
if (tinfo->typdefault)
3257+
{
3258+
appendPQExpBuffer(q,
3259+
" DEFAULT %s",
3260+
tinfo->typdefault);
3261+
}
3262+
3263+
appendPQExpBuffer(q, ";\n");
3264+
3265+
3266+
(*deps)[depIdx++] = NULL; /* End of List */
3267+
3268+
ArchiveEntry(fout, tinfo->oid, tinfo->typname, "DOMAIN", deps,
3269+
q->data, delq->data, "", tinfo->usename, NULL, NULL);
3270+
3271+
/*** Dump Domain Comments ***/
3272+
resetPQExpBuffer(q);
3273+
3274+
appendPQExpBuffer(q, "DOMAIN %s", fmtId(tinfo->typname, force_quotes));
3275+
dumpComment(fout, q->data, tinfo->oid, "pg_type", 0, NULL);
3276+
}
3277+
31913278
/*
31923279
* dumpTypes
31933280
* writes out to fout the queries to recreate all the user-defined types
@@ -3223,6 +3310,13 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
32233310
(strcmp(tinfo[i].typinput, "array_in") == 0))
32243311
continue;
32253312

3313+
/* Dump out domains as we run across them */
3314+
if (strcmp(tinfo[i].typtype, "d") == 0) {
3315+
dumpOneDomain(fout, &tinfo[i]);
3316+
continue;
3317+
}
3318+
3319+
32263320
deps = malloc(sizeof(char *) * 10);
32273321
depIdx = 0;
32283322

@@ -3318,6 +3412,8 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
33183412
ArchiveEntry(fout, tinfo[i].oid, tinfo[i].typname, "TYPE", deps,
33193413
q->data, delq->data, "", tinfo[i].usename, NULL, NULL);
33203414

3415+
3416+
33213417
/*** Dump Type Comments ***/
33223418

33233419
resetPQExpBuffer(q);
@@ -4294,6 +4390,36 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
42944390
tblinfo[i].check_expr[k]);
42954391
}
42964392

4393+
/* Primary Key */
4394+
if (tblinfo[i].pkIndexOid != NULL)
4395+
{
4396+
PQExpBuffer consDef;
4397+
4398+
/* Find the corresponding index */
4399+
for (k = 0; k < numIndexes; k++)
4400+
{
4401+
if (strcmp(indinfo[k].indexreloid,
4402+
tblinfo[i].pkIndexOid) == 0)
4403+
break;
4404+
}
4405+
4406+
if (k >= numIndexes)
4407+
{
4408+
write_msg(NULL, "dumpTables(): failed sanity check, could not find index (%s) for primary key constraint\n",
4409+
tblinfo[i].pkIndexOid);
4410+
exit_nicely();
4411+
}
4412+
4413+
consDef = getPKconstraint(&tblinfo[i], &indinfo[k]);
4414+
4415+
if ((actual_atts + tblinfo[i].ncheck) > 0)
4416+
appendPQExpBuffer(q, ",\n\t");
4417+
4418+
appendPQExpBuffer(q, "%s", consDef->data);
4419+
4420+
destroyPQExpBuffer(consDef);
4421+
}
4422+
42974423
/*
42984424
* Primary Key: In versions of PostgreSQL prior to 7.2, we
42994425
* needed to include the primary key in the table definition.

src/bin/pg_dump/pg_dump.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_dump.h,v 1.79 2002/04/05 00:31:32 tgl Exp $
9+
* $Id: pg_dump.h,v 1.80 2002/04/05 11:51:13 momjian Exp $
1010
*
1111
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
1212
*
@@ -53,6 +53,7 @@ typedef struct _typeInfo
5353
char *typstorage;
5454
char *usename;
5555
char *typedefn;
56+
char *typtype;
5657
int passedbyvalue;
5758
int isArray;
5859
int isDefined;

0 commit comments

Comments
 (0)