pg_dump Add dumping of comments on index columns

Started by higeponalmost 17 years ago13 messages
#1higepon
higepon@gmail.com

Hi.
I found a TODO item "Add dumping of comments on index columns and
composite type columns" for pg_dump
and want to write a patch for it.

But I'm not sure if I understand the problem correctly.
Does "Add dumping of comments on index columns" mean that
pg_dump should dump out COMMENT statements like following?

COMMENT ON COLUMN some_index.index_column1 IS 'Hello column1';

Can anybody give me some advice on this?

Best regards,

-----
Taro Minowa(Higepon)

http://www.monaos.org/
http://code.google.com/p/mosh-scheme/

#2Bruce Momjian
bruce@momjian.us
In reply to: higepon (#1)
Re: pg_dump Add dumping of comments on index columns

higepon wrote:

Hi.
I found a TODO item "Add dumping of comments on index columns and
composite type columns" for pg_dump
and want to write a patch for it.

But I'm not sure if I understand the problem correctly.
Does "Add dumping of comments on index columns" mean that
pg_dump should dump out COMMENT statements like following?

COMMENT ON COLUMN some_index.index_column1 IS 'Hello column1';

Can anybody give me some advice on this?

Wow, I have no idea what that means. I am wondering if we should just
remove this TODO item. We don't even support comments on indexed
columns, so why would pg_dump need to dump it?

The full text is:

Add dumping of comments on index columns and composite type columns

Do we support comments on composite types?

This item first appeared on the TODO list in Postgres 8.0.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

#3higepon
higepon@gmail.com
In reply to: Bruce Momjian (#2)
Re: pg_dump Add dumping of comments on index columns

Hi.

Wow, I have no idea what that means. I am wondering if we should just
remove this TODO item. We don't even support comments on indexed
columns, so why would pg_dump need to dump it?

Oh I see.
But we still can comment on indexed columns like following on 8.3.7,
is it unsupported feature?

create table person (social_no integer, name text, age integer, uri
text, PRIMARY KEY (social_no));
create index person_age on person using BTREE (age);
comment on column person_age.age IS 'hello index person_age.age';

And we can find the comment in pg_description table.

Do we support comments on composite types

If we do, I will also write a patch for it.

Cheers.

-----
Taro Minowa(Higepon)

http://www.monaos.org/
http://code.google.com/p/mosh-scheme/

Show quoted text

On Tue, Mar 24, 2009 at 11:10 PM, Bruce Momjian <bruce@momjian.us> wrote:

higepon wrote:

Hi.
I found a TODO item "Add dumping of comments on index columns and
composite type columns" for pg_dump
and want to write a patch for it.

But I'm not sure if I understand the problem correctly.
Does "Add dumping of comments on index columns" mean that
pg_dump should dump out COMMENT statements like following?

 COMMENT ON COLUMN some_index.index_column1 IS 'Hello column1';

Can anybody give me some advice on this?

Wow, I have no idea what that means.  I am wondering if we should just
remove this TODO item.  We don't even support comments on indexed
columns, so why would pg_dump need to dump it?

The full text is:

       Add dumping of comments on index columns and composite type columns

Do we support comments on composite types?

This item first appeared on the TODO list in Postgres 8.0.

--
 Bruce Momjian  <bruce@momjian.us>        http://momjian.us
 EnterpriseDB                             http://enterprisedb.com

 + If your life is a hard drive, Christ can be your backup. +

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#2)
Re: pg_dump Add dumping of comments on index columns

Bruce Momjian <bruce@momjian.us> writes:

Wow, I have no idea what that means. I am wondering if we should just
remove this TODO item. We don't even support comments on indexed
columns, so why would pg_dump need to dump it?

The system will let you do it, both cases:

regression=# create type foo as (f1 int, f2 text);
CREATE TYPE
regression=# comment on column foo.f2 is 'column of a composite type';
COMMENT
regression=# create table tt (f1 int primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tt_pkey" for table "tt"
CREATE TABLE
regression=# comment on column tt_pkey.f1 is 'column of an index';
COMMENT

and pg_dump fails to dump both cases.

Commenting on a composite-type column seems reasonable. I'm less happy
about the other because it depends on the names assigned to index
columns, which are implementation artifacts. I'd rather see us forbid
the case.

regards, tom lane

#5higepon
higepon@gmail.com
In reply to: Tom Lane (#4)
1 attachment(s)
Re: pg_dump Add dumping of comments on index columns

Hi.

Here is a patch for pg_dump "Commenting on a composite-type column".
This patch is for Todo item named "Add dumping of comments on index
columns and composite type columns".
As Tom Lane said, this patch is not for dumping "comments on index columns",
but only for "comment on composite-type column".

With this patch, pg_dump can dump comments on composite-type column.

--
-- Name: COLUMN bar.b1; Type: COMMENT; Schema: public; Owner: taro
--

COMMENT ON COLUMN bar.b1 IS 'column of a composite type b1';

--
-- Name: COLUMN bar.b3; Type: COMMENT; Schema: public; Owner: taro
--

COMMENT ON COLUMN bar.b3 IS 'column of a composite type b3';

Would someone please review this?

Cheers.

-----
Taro Minowa(Higepon)

Cybozu Labs, Inc.

http://www.monaos.org/
http://code.google.com/p/mosh-scheme/

Show quoted text

On Tue, Mar 24, 2009 at 11:56 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Bruce Momjian <bruce@momjian.us> writes:

Wow, I have no idea what that means.  I am wondering if we should just
remove this TODO item.  We don't even support comments on indexed
columns, so why would pg_dump need to dump it?

The system will let you do it, both cases:

regression=# create type foo as (f1 int, f2 text);
CREATE TYPE
regression=# comment on column foo.f2 is 'column of a composite type';
COMMENT
regression=# create table tt (f1 int primary key);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "tt_pkey" for table "tt"
CREATE TABLE
regression=# comment on column tt_pkey.f1 is 'column of an index';
COMMENT

and pg_dump fails to dump both cases.

Commenting on a composite-type column seems reasonable.  I'm less happy
about the other because it depends on the names assigned to index
columns, which are implementation artifacts.  I'd rather see us forbid
the case.

                       regards, tom lane

Attachments:

pg_dump_composite_type_v1.patchapplication/octet-stream; name=pg_dump_composite_type_v1.patchDownload
? GNUmakefile
? config.log
? config.status
? pg_dump_composite_type_v1.patch
? src/Makefile.global
? src/backend/postgres
? src/backend/catalog/postgres.bki
? src/backend/catalog/postgres.description
? src/backend/catalog/postgres.shdescription
? src/backend/snowball/snowball_create.sql
? src/backend/utils/probes.h
? src/backend/utils/mb/conversion_procs/conversion_create.sql
? src/bin/initdb/initdb
? src/bin/pg_config/pg_config
? src/bin/pg_controldata/pg_controldata
? src/bin/pg_ctl/pg_ctl
? src/bin/pg_dump/pg_dump
? src/bin/pg_dump/pg_dumpall
? src/bin/pg_dump/pg_restore
? src/bin/pg_resetxlog/pg_resetxlog
? src/bin/psql/psql
? src/bin/scripts/clusterdb
? src/bin/scripts/createdb
? src/bin/scripts/createlang
? src/bin/scripts/createuser
? src/bin/scripts/dropdb
? src/bin/scripts/droplang
? src/bin/scripts/dropuser
? src/bin/scripts/reindexdb
? src/bin/scripts/vacuumdb
? src/include/pg_config.h
? src/include/stamp-h
? src/interfaces/ecpg/compatlib/exports.list
? src/interfaces/ecpg/compatlib/libecpg_compat.3.1.dylib
? src/interfaces/ecpg/ecpglib/exports.list
? src/interfaces/ecpg/ecpglib/libecpg.6.1.dylib
? src/interfaces/ecpg/include/ecpg_config.h
? src/interfaces/ecpg/include/stamp-h
? src/interfaces/ecpg/pgtypeslib/exports.list
? src/interfaces/ecpg/pgtypeslib/libpgtypes.3.1.dylib
? src/interfaces/ecpg/preproc/ecpg
? src/interfaces/libpq/exports.list
? src/interfaces/libpq/libpq.5.2.dylib
? src/port/pg_config_paths.h
? src/test/regress/log
? src/test/regress/pg_regress
? src/test/regress/results
? src/test/regress/testtablespace
? src/test/regress/tmp_check
? src/test/regress/expected/constraints.out
? src/test/regress/expected/copy.out
? src/test/regress/expected/create_function_1.out
? src/test/regress/expected/create_function_2.out
? src/test/regress/expected/largeobject.out
? src/test/regress/expected/largeobject_1.out
? src/test/regress/expected/misc.out
? src/test/regress/expected/tablespace.out
? src/test/regress/sql/constraints.sql
? src/test/regress/sql/copy.sql
? src/test/regress/sql/create_function_1.sql
? src/test/regress/sql/create_function_2.sql
? src/test/regress/sql/largeobject.sql
? src/test/regress/sql/misc.sql
? src/test/regress/sql/tablespace.sql
? src/timezone/zic
Index: src/bin/pg_dump/pg_dump.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v
retrieving revision 1.530
diff -c -r1.530 pg_dump.c
*** src/bin/pg_dump/pg_dump.c	22 Mar 2009 16:44:26 -0000	1.530
--- src/bin/pg_dump/pg_dump.c	26 Mar 2009 07:11:53 -0000
***************
*** 137,142 ****
--- 137,143 ----
  static void dumpEnumType(Archive *fout, TypeInfo *tinfo);
  static void dumpDomain(Archive *fout, TypeInfo *tinfo);
  static void dumpCompositeType(Archive *fout, TypeInfo *tinfo);
+ static void dumpCompositeTypeColumn(Archive *fout, TypeInfo *tinfo);
  static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
  static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
  static void dumpFunc(Archive *fout, FuncInfo *finfo);
***************
*** 6707,6712 ****
--- 6708,6813 ----
  	destroyPQExpBuffer(q);
  	destroyPQExpBuffer(delq);
  	destroyPQExpBuffer(query);
+ 
+ 	/* Dump column comments */
+ 	dumpCompositeTypeColumn(fout, tinfo);
+ }
+ 
+ /*
+  * dumpCompositeTypeColumn
+  *	  writes out to fout the comments on
+  *	  columns of composite type
+  */
+ static void
+ dumpCompositeTypeColumn(Archive *fout, TypeInfo *tinfo)
+ {
+ 	CommentItem *comments;
+ 	int ncomments;
+ 	PGresult *res;
+ 	PQExpBuffer query;
+ 	PQExpBuffer attrquery = createPQExpBuffer();
+ 	PQExpBuffer target;
+ 	Oid colTableOid;
+ 	int i;
+ 	int ntups;
+ 	int i_attname;
+ 	int i_attnum;
+ 
+ 	appendPQExpBuffer(attrquery,
+ 					  "SELECT pg_class.tableoid, "
+ 					  "       pg_attribute.attname, "
+ 					  "        pg_attribute.attnum "
+ 					  "FROM pg_class, pg_attribute "
+ 					  "WHERE pg_class.oid = '%u' and pg_class.oid = pg_attribute.attrelid "
+ 					  "ORDER BY pg_attribute.attnum "
+ 					  , tinfo->typrelid);
+ 
+ 	/* Fetch column's attname */
+ 	res = PQexec(g_conn, attrquery->data);
+ 	check_sql_result(res, g_conn, attrquery->data, PGRES_TUPLES_OK);
+ 	ntups = PQntuples(res);
+ 	if (ntups < 1)
+ 	{
+ 		write_msg(NULL, "query returned no rows: %s\n", attrquery->data);
+ 		exit_nicely();
+ 	}
+ 	colTableOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
+ 
+ 	/* Search for comments associated with relation, using table */
+ 	ncomments = findComments(fout,
+ 							 colTableOid,
+ 							 tinfo->typrelid,
+ 							 &comments);
+ 
+ 	/* If comments exist, build COMMENT ON statements */
+ 	if (ncomments <= 0)
+ 		return;
+ 
+ 	query = createPQExpBuffer();
+ 	target = createPQExpBuffer();
+ 
+ 	i_attnum = PQfnumber(res, "attnum");
+ 	i_attname = PQfnumber(res, "attname");
+ 	while (ncomments > 0)
+ 	{
+ 		const char *descr = comments->descr;
+ 		/* Just to be safe */
+ 		const char *attname = "unknown";
+ 		for (i = 0; i < ntups; i++)
+ 		{
+ 			if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
+ 			{
+ 				attname = PQgetvalue(res, i, i_attname);
+ 				break;
+ 			}
+ 		}
+ 		resetPQExpBuffer(target);
+ 		appendPQExpBuffer(target, "COLUMN %s.",
+ 						  fmtId(tinfo->dobj.name));
+ 		appendPQExpBuffer(target, "%s",
+ 						  fmtId(attname));
+ 		
+ 		resetPQExpBuffer(query);
+ 		appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
+ 		appendStringLiteralAH(query, descr, fout);
+ 		appendPQExpBuffer(query, ";\n");
+ 
+ 		ArchiveEntry(fout, nilCatalogId, createDumpId(),
+ 					 target->data,
+ 					 tinfo->dobj.namespace->dobj.name,
+ 					 NULL,
+ 					 tinfo->rolname,
+ 					 false, "COMMENT", SECTION_NONE, query->data, "", NULL,
+ 					 &(tinfo->dobj.dumpId), 1,
+ 					 NULL, NULL);
+ 
+ 		comments++;
+ 		ncomments--;
+ 	}
+ 	destroyPQExpBuffer(attrquery);
+ 	destroyPQExpBuffer(query);
+ 	destroyPQExpBuffer(target);
+ 	PQclear(res);
  }
  
  /*
#6Robert Haas
robertmhaas@gmail.com
In reply to: higepon (#5)
Re: pg_dump Add dumping of comments on index columns

Would someone please review this?

Since we are about to go to beta, it may be that no one is up for
reviewing it right now. But I've added it to the CommitFest page for
the next CommitFest.

http://wiki.postgresql.org/wiki/CommitFest_2009-First

...Robert

#7higepon
higepon@gmail.com
In reply to: Robert Haas (#6)
Re: pg_dump Add dumping of comments on index columns

Hi.

Since we are about to go to beta, it may be that no one is up for
reviewing it right now.  But I've added it to the CommitFest page for
the next CommitFest.

Thank you.
I wait until the next CommitFest.

-----
Taro Minowa(Higepon)

http://www.monaos.org/
http://code.google.com/p/mosh-scheme/

#8Jaime Casanova
jcasanov@systemguards.com.ec
In reply to: higepon (#5)
1 attachment(s)
Re: pg_dump Add dumping of comments on index columns

On Thu, Mar 26, 2009 at 2:39 AM, higepon<higepon@gmail.com> wrote:

Hi.

Here is a patch for pg_dump "Commenting on a composite-type column".
This patch is for Todo item named "Add dumping of comments on index
columns and composite type columns".

this one looks good to me, the only adjust i made to the patch is
change the name for the function that dump the comments from the
composite types columns for: dumpCompositeTypeColsComment that seems
more clearer to me...

the patch works just fine...

--
Atentamente,
Jaime Casanova
Soporte y capacitación de PostgreSQL
Asesoría y desarrollo de sistemas
Guayaquil - Ecuador
Cel. +59387171157

Attachments:

pg_dump_composite_type_v2.patchtext/x-diff; charset=US-ASCII; name=pg_dump_composite_type_v2.patchDownload
Index: src/bin/pg_dump/pg_dump.c
===================================================================
RCS file: /home/postgres/pgrepo/pgsql/src/bin/pg_dump/pg_dump.c,v
retrieving revision 1.540
diff -c -r1.540 pg_dump.c
*** src/bin/pg_dump/pg_dump.c	2 Jul 2009 21:34:32 -0000	1.540
--- src/bin/pg_dump/pg_dump.c	14 Jul 2009 07:22:38 -0000
***************
*** 133,138 ****
--- 133,139 ----
  static void dumpEnumType(Archive *fout, TypeInfo *tinfo);
  static void dumpDomain(Archive *fout, TypeInfo *tinfo);
  static void dumpCompositeType(Archive *fout, TypeInfo *tinfo);
+ static void dumpCompositeTypeColsComment(Archive *fout, TypeInfo *tinfo);
  static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
  static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
  static void dumpFunc(Archive *fout, FuncInfo *finfo);
***************
*** 6708,6716 ****
--- 6709,6818 ----
  	destroyPQExpBuffer(q);
  	destroyPQExpBuffer(delq);
  	destroyPQExpBuffer(query);
+ 
+  	/* Dump column comments */
+  	dumpCompositeTypeColsComment(fout, tinfo);
  }
  
  /*
+  * dumpCompositeTypeColsComment
+  *	  writes out to fout the comments on
+  *	  columns of composite type
+  */
+ static void
+ dumpCompositeTypeColsComment(Archive *fout, TypeInfo *tinfo)
+ {
+ 	CommentItem *comments;
+ 	int ncomments;
+ 	PGresult *res;
+ 	PQExpBuffer query;
+ 	PQExpBuffer attrquery = createPQExpBuffer();
+ 	PQExpBuffer target;
+ 	Oid colTableOid;
+ 	int i;
+ 	int ntups;
+ 	int i_attname;
+ 	int i_attnum;
+ 
+ 	appendPQExpBuffer(attrquery,
+ 					  "SELECT pg_class.tableoid, "
+ 					  "       pg_attribute.attname, "
+ 					  "        pg_attribute.attnum "
+ 					  "FROM pg_class, pg_attribute "
+ 					  "WHERE pg_class.oid = '%u' and pg_class.oid = pg_attribute.attrelid "
+ 					  "ORDER BY pg_attribute.attnum "
+ 					  , tinfo->typrelid);
+ 
+ 	/* Fetch column's attname */
+ 	res = PQexec(g_conn, attrquery->data);
+ 	check_sql_result(res, g_conn, attrquery->data, PGRES_TUPLES_OK);
+ 	ntups = PQntuples(res);
+ 	if (ntups < 1)
+ 	{
+ 		write_msg(NULL, "query returned no rows: %s\n", attrquery->data);
+ 		exit_nicely();
+ 	}
+ 	colTableOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
+ 
+ 	/* Search for comments associated with relation, using table */
+ 	ncomments = findComments(fout,
+ 							 colTableOid,
+ 							 tinfo->typrelid,
+ 							 &comments);
+ 
+ 	/* If comments exist, build COMMENT ON statements */
+ 	if (ncomments <= 0)
+ 		return;
+ 
+ 	query = createPQExpBuffer();
+ 	target = createPQExpBuffer();
+ 
+ 	i_attnum = PQfnumber(res, "attnum");
+ 	i_attname = PQfnumber(res, "attname");
+ 	while (ncomments > 0)
+ 	{
+ 		const char *descr = comments->descr;
+ 		/* Just to be safe */
+ 		const char *attname = "unknown";
+ 		for (i = 0; i < ntups; i++)
+ 		{
+ 			if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
+ 			{
+ 				attname = PQgetvalue(res, i, i_attname);
+ 				break;
+ 			}
+ 		}
+ 		resetPQExpBuffer(target);
+ 		appendPQExpBuffer(target, "COLUMN %s.",
+ 						  fmtId(tinfo->dobj.name));
+ 		appendPQExpBuffer(target, "%s",
+ 						  fmtId(attname));
+ 		
+ 		resetPQExpBuffer(query);
+ 		appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
+ 		appendStringLiteralAH(query, descr, fout);
+ 		appendPQExpBuffer(query, ";\n");
+ 
+ 		ArchiveEntry(fout, nilCatalogId, createDumpId(),
+ 					 target->data,
+ 					 tinfo->dobj.namespace->dobj.name,
+ 					 NULL,
+ 					 tinfo->rolname,
+ 					 false, "COMMENT", SECTION_NONE, query->data, "", NULL,
+ 					 &(tinfo->dobj.dumpId), 1,
+ 					 NULL, NULL);
+ 
+ 		comments++;
+ 		ncomments--;
+ 	}
+ 	destroyPQExpBuffer(attrquery);
+ 	destroyPQExpBuffer(query);
+ 	destroyPQExpBuffer(target);
+ 	PQclear(res);
+ }
+ 
+ 
+ /*
   * dumpShellType
   *	  writes out to fout the queries to create a shell type
   *
#9higepon
higepon@gmail.com
In reply to: Jaime Casanova (#8)
Re: pg_dump Add dumping of comments on index columns

Jaime Casanova wrote:

this one looks good to me, the only adjust i made to the patch is

Thank you for your review!

---
Taro Minowa(Higepon)

http://www.monaos.org/
http://code.google.com/p/mosh-scheme/

On Tue, Jul 14, 2009 at 4:34 PM, Jaime
Casanova<jcasanov@systemguards.com.ec> wrote:

Show quoted text

On Thu, Mar 26, 2009 at 2:39 AM, higepon<higepon@gmail.com> wrote:

Hi.

Here is a patch for pg_dump "Commenting on a composite-type column".
This patch is for Todo item named "Add dumping of comments on index
columns and composite type columns".

this one looks good to me, the only adjust i made to the patch is
change the name for the function that dump the comments from the
composite types columns for: dumpCompositeTypeColsComment that seems
more clearer to me...

the patch works just fine...

--
Atentamente,
Jaime Casanova
Soporte y capacitación de PostgreSQL
Asesoría y desarrollo de sistemas
Guayaquil - Ecuador
Cel. +59387171157

#10Brendan Jurd
direvus@gmail.com
In reply to: Jaime Casanova (#8)
1 attachment(s)
Re: pg_dump Add dumping of comments on index columns

2009/7/14 Jaime Casanova <jcasanov@systemguards.com.ec>:

On Thu, Mar 26, 2009 at 2:39 AM, higepon<higepon@gmail.com> wrote:

Here is a patch for pg_dump "Commenting on a composite-type column".
This patch is for Todo item named "Add dumping of comments on index
columns and composite type columns".

this one looks good to me, the only adjust i made to the patch is
change the name for the function that dump the comments from the
composite types columns for: dumpCompositeTypeColsComment that seems
more clearer to me...

the patch works just fine...

Oops. I picked this patch up from the commitfest queue because it was
still marked as "Needs Review", not realising that Jaime had already
done an initial review of the patch.

Seems like this one should have been "Ready for Committer", but no matter.

I've also done an initial review of the patch. Everything looks sane
and the patch works as advertised. I made a couple of minor tweaks
for code-style and comment consistency, and my version 3 is attached.

I'm marking this patch Ready for Committer.

I've also added a TODO item "forbid COMMENT on columns of an index",
per Tom's comments upthread.

Cheers,
BJ

Attachments:

pg_dump_composite_type_v3.patchapplication/octet-stream; name=pg_dump_composite_type_v3.patchDownload
*** src/bin/pg_dump/pg_dump.c
--- src/bin/pg_dump/pg_dump.c
***************
*** 134,139 **** static void dumpBaseType(Archive *fout, TypeInfo *tinfo);
--- 134,140 ----
  static void dumpEnumType(Archive *fout, TypeInfo *tinfo);
  static void dumpDomain(Archive *fout, TypeInfo *tinfo);
  static void dumpCompositeType(Archive *fout, TypeInfo *tinfo);
+ static void dumpCompositeTypeColsComment(Archive *fout, TypeInfo *tinfo);
  static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
  static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
  static void dumpFunc(Archive *fout, FuncInfo *finfo);
***************
*** 6755,6762 **** dumpCompositeType(Archive *fout, TypeInfo *tinfo)
--- 6756,6864 ----
  	destroyPQExpBuffer(q);
  	destroyPQExpBuffer(delq);
  	destroyPQExpBuffer(query);
+ 
+  	/* Dump column comments */
+  	dumpCompositeTypeColsComment(fout, tinfo);
+ }
+ 
+ /*
+  * dumpCompositeTypeColsComment
+  *	  writes out to fout the queries to recreate comments on columns of
+  *	  composite types
+  */
+ static void
+ dumpCompositeTypeColsComment(Archive *fout, TypeInfo *tinfo)
+ {
+ 	CommentItem *comments;
+ 	int ncomments;
+ 	PGresult *res;
+ 	PQExpBuffer query;
+ 	PQExpBuffer attrquery = createPQExpBuffer();
+ 	PQExpBuffer target;
+ 	Oid colTableOid;
+ 	int i;
+ 	int ntups;
+ 	int i_attname;
+ 	int i_attnum;
+ 
+ 	appendPQExpBuffer(attrquery,
+ 					  "SELECT pg_class.tableoid, "
+ 					  "       pg_attribute.attname, "
+ 					  "        pg_attribute.attnum "
+ 					  "FROM pg_class, pg_attribute "
+ 					  "WHERE pg_class.oid = '%u' and pg_class.oid = pg_attribute.attrelid "
+ 					  "ORDER BY pg_attribute.attnum ",
+ 					  tinfo->typrelid);
+ 
+ 	/* Fetch column's attname */
+ 	res = PQexec(g_conn, attrquery->data);
+ 	check_sql_result(res, g_conn, attrquery->data, PGRES_TUPLES_OK);
+ 	ntups = PQntuples(res);
+ 	if (ntups < 1)
+ 	{
+ 		write_msg(NULL, "query returned no rows: %s\n", attrquery->data);
+ 		exit_nicely();
+ 	}
+ 	colTableOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
+ 
+ 	/* Search for comments associated with relation, using table */
+ 	ncomments = findComments(fout,
+ 							 colTableOid,
+ 							 tinfo->typrelid,
+ 							 &comments);
+ 
+ 	/* If comments exist, build COMMENT ON statements */
+ 	if (ncomments <= 0)
+ 		return;
+ 
+ 	query = createPQExpBuffer();
+ 	target = createPQExpBuffer();
+ 
+ 	i_attnum = PQfnumber(res, "attnum");
+ 	i_attname = PQfnumber(res, "attname");
+ 	while (ncomments > 0)
+ 	{
+ 		const char *descr = comments->descr;
+ 		/* Just to be safe */
+ 		const char *attname = "unknown";
+ 		for (i = 0; i < ntups; i++)
+ 		{
+ 			if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
+ 			{
+ 				attname = PQgetvalue(res, i, i_attname);
+ 				break;
+ 			}
+ 		}
+ 		resetPQExpBuffer(target);
+ 		appendPQExpBuffer(target, "COLUMN %s.",
+ 						  fmtId(tinfo->dobj.name));
+ 		appendPQExpBuffer(target, "%s",
+ 						  fmtId(attname));
+ 		
+ 		resetPQExpBuffer(query);
+ 		appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
+ 		appendStringLiteralAH(query, descr, fout);
+ 		appendPQExpBuffer(query, ";\n");
+ 
+ 		ArchiveEntry(fout, nilCatalogId, createDumpId(),
+ 					 target->data,
+ 					 tinfo->dobj.namespace->dobj.name,
+ 					 NULL,
+ 					 tinfo->rolname,
+ 					 false, "COMMENT", SECTION_NONE, query->data, "", NULL,
+ 					 &(tinfo->dobj.dumpId), 1,
+ 					 NULL, NULL);
+ 
+ 		comments++;
+ 		ncomments--;
+ 	}
+ 	destroyPQExpBuffer(attrquery);
+ 	destroyPQExpBuffer(query);
+ 	destroyPQExpBuffer(target);
+ 	PQclear(res);
  }
  
+ 
  /*
   * dumpShellType
   *	  writes out to fout the queries to create a shell type
#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Brendan Jurd (#10)
Re: pg_dump Add dumping of comments on index columns

Brendan Jurd <direvus@gmail.com> writes:

I've also done an initial review of the patch. Everything looks sane
and the patch works as advertised. I made a couple of minor tweaks
for code-style and comment consistency, and my version 3 is attached.

I'm marking this patch Ready for Committer.

Applied with minor revisions --- mostly, it leaked memory in the case
of no comments, and the query wasn't very schema-safe.

regards, tom lane

#12Bruce Momjian
bruce@momjian.us
In reply to: Tom Lane (#11)
Re: pg_dump Add dumping of comments on index columns

Tom Lane wrote:

Brendan Jurd <direvus@gmail.com> writes:

I've also done an initial review of the patch. Everything looks sane
and the patch works as advertised. I made a couple of minor tweaks
for code-style and comment consistency, and my version 3 is attached.

I'm marking this patch Ready for Committer.

Applied with minor revisions --- mostly, it leaked memory in the case
of no comments, and the query wasn't very schema-safe.

Just to verify, this patch was about comments on composite columns, not
about dumping comments on index columns (as the subject states), right?
We do have a TODO for index column comments:

Forbid COMMENT on columns of an index

Postgres currently allows comments to be placed on the columns of an
index, but pg_dump doesn't handle them and the column names themselves
are implementation-dependent.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

#13Brendan Jurd
direvus@gmail.com
In reply to: Bruce Momjian (#12)
Re: pg_dump Add dumping of comments on index columns

2009/8/8 Bruce Momjian <bruce@momjian.us>:

Just to verify, this patch was about comments on composite columns, not
about dumping comments on index columns (as the subject states), right?
We do have a TODO for index column comments:

Correct.

If you scroll up a couple of messages [1]37ed240d0907212253peaab8abtf8fd58fbde44cfc2@mail.gmail.com you'll see that I added that
TODO after I reviewed the patch.

Cheers,
BJ

[1]: 37ed240d0907212253peaab8abtf8fd58fbde44cfc2@mail.gmail.com