Skip to content

Commit cb05f53

Browse files
committed
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.
1 parent 7d672e1 commit cb05f53

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
22-
* $PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.32 2009/12/15 17:57:47 tgl Exp $
22+
* $PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.33 2009/12/20 18:28:14 tgl Exp $
2323
*
2424
*-------------------------------------------------------------------------
2525
*/
@@ -745,58 +745,62 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
745745
/* Copy comment on index */
746746
if (inhRelation->options & CREATE_TABLE_LIKE_COMMENTS)
747747
{
748+
Form_pg_attribute *attrs;
748749
CommentStmt *stmt;
749-
ListCell *lc;
750-
int i;
750+
int colno;
751751

752752
comment = GetComment(parent_index_oid, RelationRelationId, 0);
753753

754754
if (comment != NULL)
755755
{
756-
/* Assign name for index because CommentStmt requires name. */
756+
/*
757+
* We have to assign the index a name now, so that we
758+
* can reference it in CommentStmt.
759+
*/
757760
if (index_stmt->idxname == NULL)
758-
index_stmt->idxname = chooseIndexName(cxt->relation, index_stmt);
761+
index_stmt->idxname = chooseIndexName(cxt->relation,
762+
index_stmt);
759763

760764
stmt = makeNode(CommentStmt);
761765
stmt->objtype = OBJECT_INDEX;
762-
stmt->objname = list_make2(makeString(cxt->relation->schemaname),
763-
makeString(index_stmt->idxname));
766+
stmt->objname =
767+
list_make2(makeString(cxt->relation->schemaname),
768+
makeString(index_stmt->idxname));
764769
stmt->objargs = NIL;
765770
stmt->comment = comment;
766771

767772
cxt->alist = lappend(cxt->alist, stmt);
768773
}
769774

770-
/* Copy comment on index's columns */
771-
i = 0;
772-
foreach(lc, index_stmt->indexParams)
775+
/* Copy comments on index's columns */
776+
attrs = RelationGetDescr(parent_index)->attrs;
777+
for (colno = 1;
778+
colno <= RelationGetNumberOfAttributes(parent_index);
779+
colno++)
773780
{
774781
char *attname;
775782

776-
i++;
777-
comment = GetComment(parent_index_oid, RelationRelationId, i);
783+
comment = GetComment(parent_index_oid, RelationRelationId,
784+
colno);
778785
if (comment == NULL)
779786
continue;
780787

781-
/* Assign name for index because CommentStmt requires name. */
788+
/*
789+
* We have to assign the index a name now, so that we
790+
* can reference it in CommentStmt.
791+
*/
782792
if (index_stmt->idxname == NULL)
783-
index_stmt->idxname = chooseIndexName(cxt->relation, index_stmt);
793+
index_stmt->idxname = chooseIndexName(cxt->relation,
794+
index_stmt);
784795

785-
attname = ((IndexElem *) lfirst(lc))->name;
786-
787-
/* expression index has a dummy column name */
788-
if (attname == NULL)
789-
{
790-
attname = palloc(NAMEDATALEN);
791-
sprintf(attname, "pg_expression_%d", i);
792-
}
796+
attname = NameStr(attrs[colno - 1]->attname);
793797

794798
stmt = makeNode(CommentStmt);
795799
stmt->objtype = OBJECT_COLUMN;
796-
stmt->objname = list_make3(
797-
makeString(cxt->relation->schemaname),
798-
makeString(index_stmt->idxname),
799-
makeString(attname));
800+
stmt->objname =
801+
list_make3(makeString(cxt->relation->schemaname),
802+
makeString(index_stmt->idxname),
803+
makeString(attname));
800804
stmt->objargs = NIL;
801805
stmt->comment = comment;
802806

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