Skip to content

Commit 0d6909e

Browse files
committed
Teach pg_dump to dump comments attached to the columns of a composite type.
Taro Minowa (Higepon)
1 parent adfa042 commit 0d6909e

File tree

1 file changed

+115
-1
lines changed

1 file changed

+115
-1
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.541 2009/07/20 20:53:40 momjian Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.542 2009/07/23 22:59:40 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -134,6 +134,7 @@ static void dumpBaseType(Archive *fout, TypeInfo *tinfo);
134134
static void dumpEnumType(Archive *fout, TypeInfo *tinfo);
135135
static void dumpDomain(Archive *fout, TypeInfo *tinfo);
136136
static void dumpCompositeType(Archive *fout, TypeInfo *tinfo);
137+
static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tinfo);
137138
static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
138139
static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
139140
static void dumpFunc(Archive *fout, FuncInfo *finfo);
@@ -6755,6 +6756,119 @@ dumpCompositeType(Archive *fout, TypeInfo *tinfo)
67556756
destroyPQExpBuffer(q);
67566757
destroyPQExpBuffer(delq);
67576758
destroyPQExpBuffer(query);
6759+
6760+
/* Dump any per-column comments */
6761+
dumpCompositeTypeColComments(fout, tinfo);
6762+
}
6763+
6764+
/*
6765+
* dumpCompositeTypeColComments
6766+
* writes out to fout the queries to recreate comments on the columns of
6767+
* a user-defined stand-alone composite type
6768+
*/
6769+
static void
6770+
dumpCompositeTypeColComments(Archive *fout, TypeInfo *tinfo)
6771+
{
6772+
CommentItem *comments;
6773+
int ncomments;
6774+
PGresult *res;
6775+
PQExpBuffer query;
6776+
PQExpBuffer target;
6777+
Oid pgClassOid;
6778+
int i;
6779+
int ntups;
6780+
int i_attname;
6781+
int i_attnum;
6782+
6783+
query = createPQExpBuffer();
6784+
6785+
/* We assume here that remoteVersion must be at least 70300 */
6786+
appendPQExpBuffer(query,
6787+
"SELECT c.tableoid, a.attname, a.attnum "
6788+
"FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
6789+
"WHERE c.oid = '%u' AND c.oid = a.attrelid "
6790+
" AND NOT a.attisdropped "
6791+
"ORDER BY a.attnum ",
6792+
tinfo->typrelid);
6793+
6794+
/* Fetch column attnames */
6795+
res = PQexec(g_conn, query->data);
6796+
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
6797+
6798+
/* Expecting at least a single result */
6799+
ntups = PQntuples(res);
6800+
if (ntups < 1)
6801+
{
6802+
write_msg(NULL, "query returned no rows: %s\n", query->data);
6803+
exit_nicely();
6804+
}
6805+
6806+
pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
6807+
6808+
/* Search for comments associated with type's pg_class OID */
6809+
ncomments = findComments(fout,
6810+
pgClassOid,
6811+
tinfo->typrelid,
6812+
&comments);
6813+
6814+
/* If no comments exist, we're done */
6815+
if (ncomments <= 0)
6816+
{
6817+
PQclear(res);
6818+
destroyPQExpBuffer(query);
6819+
return;
6820+
}
6821+
6822+
/* Build COMMENT ON statements */
6823+
target = createPQExpBuffer();
6824+
6825+
i_attnum = PQfnumber(res, "attnum");
6826+
i_attname = PQfnumber(res, "attname");
6827+
while (ncomments > 0)
6828+
{
6829+
const char *attname;
6830+
6831+
attname = NULL;
6832+
for (i = 0; i < ntups; i++)
6833+
{
6834+
if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
6835+
{
6836+
attname = PQgetvalue(res, i, i_attname);
6837+
break;
6838+
}
6839+
}
6840+
if (attname) /* just in case we don't find it */
6841+
{
6842+
const char *descr = comments->descr;
6843+
6844+
resetPQExpBuffer(target);
6845+
appendPQExpBuffer(target, "COLUMN %s.",
6846+
fmtId(tinfo->dobj.name));
6847+
appendPQExpBuffer(target, "%s",
6848+
fmtId(attname));
6849+
6850+
resetPQExpBuffer(query);
6851+
appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
6852+
appendStringLiteralAH(query, descr, fout);
6853+
appendPQExpBuffer(query, ";\n");
6854+
6855+
ArchiveEntry(fout, nilCatalogId, createDumpId(),
6856+
target->data,
6857+
tinfo->dobj.namespace->dobj.name,
6858+
NULL, tinfo->rolname,
6859+
false, "COMMENT", SECTION_NONE,
6860+
query->data, "", NULL,
6861+
&(tinfo->dobj.dumpId), 1,
6862+
NULL, NULL);
6863+
}
6864+
6865+
comments++;
6866+
ncomments--;
6867+
}
6868+
6869+
PQclear(res);
6870+
destroyPQExpBuffer(query);
6871+
destroyPQExpBuffer(target);
67586872
}
67596873

67606874
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy