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 1f3d319db4a13253a788e44ce370b9e754fbf21a..b4045e6c57afc6724046a6ff3dacbacd20c48c8c 100644 (file)
@@ -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;