There is no good reason for the CREATE TABLE LIKE INCLUDING COMMENTS code to
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 20 Dec 2009 18:28:14 +0000 (18:28 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 20 Dec 2009 18:28:14 +0000 (18:28 +0000)
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

index 9184ca8c7637933712e5c2f3a62ec922d96ebcb6..89d81bff62431876654ad7b2e3eb87f2cb80fb73 100644 (file)
@@ -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;