From cb05f5388d81ff35e00162764b66683e00b7be0d Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 20 Dec 2009 18:28:14 +0000
Subject: [PATCH] There is no good reason for the CREATE TABLE LIKE INCLUDING
 COMMENTS code to have hard-wired knowledge of the rules for naming index
 columns.  It can just look at the actual names in the source index, instead. 
 Do some minor formatting cleanup too.

---
 src/backend/parser/parse_utilcmd.c | 56 ++++++++++++++++--------------
 1 file changed, 30 insertions(+), 26 deletions(-)

diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index 9184ca8c763..89d81bff624 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -19,7 +19,7 @@
  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *	$PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.32 2009/12/15 17:57:47 tgl Exp $
+ *	$PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.33 2009/12/20 18:28:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -745,58 +745,62 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
 			/* Copy comment on index */
 			if (inhRelation->options & CREATE_TABLE_LIKE_COMMENTS)
 			{
+				Form_pg_attribute *attrs;
 				CommentStmt	   *stmt;
-				ListCell	   *lc;
-				int				i;
+				int				colno;
 
 				comment = GetComment(parent_index_oid, RelationRelationId, 0);
 				
 				if (comment != NULL)
 				{
-					/* Assign name for index because CommentStmt requires name. */
+					/*
+					 * We have to assign the index a name now, so that we
+					 * can reference it in CommentStmt.
+					 */
 					if (index_stmt->idxname == NULL)
-						index_stmt->idxname = chooseIndexName(cxt->relation, index_stmt);
+						index_stmt->idxname = chooseIndexName(cxt->relation,
+															  index_stmt);
 
 					stmt = makeNode(CommentStmt);
 					stmt->objtype = OBJECT_INDEX;
-					stmt->objname = list_make2(makeString(cxt->relation->schemaname),
-											   makeString(index_stmt->idxname));
+					stmt->objname =
+						list_make2(makeString(cxt->relation->schemaname),
+								   makeString(index_stmt->idxname));
 					stmt->objargs = NIL;
 					stmt->comment = comment;
 
 					cxt->alist = lappend(cxt->alist, stmt);
 				}
 
-				/* Copy comment on index's columns */
-				i = 0;
-				foreach(lc, index_stmt->indexParams)
+				/* Copy comments on index's columns */
+				attrs = RelationGetDescr(parent_index)->attrs;
+				for (colno = 1;
+					 colno <= RelationGetNumberOfAttributes(parent_index);
+					 colno++)
 				{
 					char	   *attname;
 
-					i++;
-					comment = GetComment(parent_index_oid, RelationRelationId, i);
+					comment = GetComment(parent_index_oid, RelationRelationId,
+										 colno);
 					if (comment == NULL)
 						continue;
 
-					/* Assign name for index because CommentStmt requires name. */
+					/*
+					 * We have to assign the index a name now, so that we
+					 * can reference it in CommentStmt.
+					 */
 					if (index_stmt->idxname == NULL)
-						index_stmt->idxname = chooseIndexName(cxt->relation, index_stmt);
+						index_stmt->idxname = chooseIndexName(cxt->relation,
+															  index_stmt);
 
-					attname = ((IndexElem *) lfirst(lc))->name;
-
-					/* expression index has a dummy column name */
-					if (attname == NULL)
-					{
-						attname = palloc(NAMEDATALEN);
-						sprintf(attname, "pg_expression_%d", i);
-					}
+					attname = NameStr(attrs[colno - 1]->attname);
 
 					stmt = makeNode(CommentStmt);
 					stmt->objtype = OBJECT_COLUMN;
-					stmt->objname = list_make3(
-										makeString(cxt->relation->schemaname),
-										makeString(index_stmt->idxname),
-										makeString(attname));
+					stmt->objname =
+						list_make3(makeString(cxt->relation->schemaname),
+								   makeString(index_stmt->idxname),
+								   makeString(attname));
 					stmt->objargs = NIL;
 					stmt->comment = comment;
 
-- 
GitLab