diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2012-07-16 13:25:18 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2012-07-16 13:25:18 -0400 |
commit | c92be3c0595d504a1516e7e158d085150ff1c4dc (patch) | |
tree | 028234f315cc68ee4369e93a4bead271292f9095 /src/backend/parser/parse_utilcmd.c | |
parent | 54fd196ffc6432b62fe075e564f457db64fb288c (diff) | |
download | postgresql-c92be3c0595d504a1516e7e158d085150ff1c4dc.tar.gz postgresql-c92be3c0595d504a1516e7e158d085150ff1c4dc.zip |
Avoid pre-determining index names during CREATE TABLE LIKE parsing.
Formerly, when trying to copy both indexes and comments, CREATE TABLE LIKE
had to pre-assign names to indexes that had comments, because it made up an
explicit CommentStmt command to apply the comment and so it had to know the
name for the index. This creates bad interactions with other indexes, as
shown in bug #6734 from Daniele Varrazzo: the preassignment logic couldn't
take any other indexes into account so it could choose a conflicting name.
To fix, add a field to IndexStmt that allows it to carry a comment to be
assigned to the new index. (This isn't a user-exposed feature of CREATE
INDEX, only an internal option.) Now we don't need preassignment of index
names in any situation.
I also took the opportunity to refactor DefineIndex to accept the IndexStmt
as such, rather than passing all its fields individually in a mile-long
parameter list.
Back-patch to 9.2, but no further, because it seems too dangerous to change
IndexStmt or DefineIndex's API in released branches. The bug exists back
to 9.0 where CREATE TABLE LIKE grew the ability to copy comments, but given
the lack of prior complaints we'll just let it go unfixed before 9.2.
Diffstat (limited to 'src/backend/parser/parse_utilcmd.c')
-rw-r--r-- | src/backend/parser/parse_utilcmd.c | 58 |
1 files changed, 11 insertions, 47 deletions
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e01e103774e..c22c6ed21f7 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -106,7 +106,6 @@ static void transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_clause); static void transformOfType(CreateStmtContext *cxt, TypeName *ofTypename); -static char *chooseIndexName(const RangeVar *relation, IndexStmt *index_stmt); static IndexStmt *generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx, const AttrNumber *attmap, int attmap_length); @@ -872,33 +871,16 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla index_stmt = generateClonedIndexStmt(cxt, parent_index, attmap, tupleDesc->natts); - /* Copy comment on index */ + /* Copy comment on index, if requested */ if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS) { comment = GetComment(parent_index_oid, RelationRelationId, 0); - if (comment != NULL) - { - CommentStmt *stmt; - - /* - * 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); - - stmt = makeNode(CommentStmt); - stmt->objtype = OBJECT_INDEX; - stmt->objname = - list_make2(makeString(cxt->relation->schemaname), - makeString(index_stmt->idxname)); - stmt->objargs = NIL; - stmt->comment = comment; - - cxt->alist = lappend(cxt->alist, stmt); - } + /* + * We make use of IndexStmt's idxcomment option, so as not to + * need to know now what name the index will have. + */ + index_stmt->idxcomment = comment; } /* Save it in the inh_indexes list for the time being */ @@ -961,29 +943,6 @@ transformOfType(CreateStmtContext *cxt, TypeName *ofTypename) } /* - * chooseIndexName - * - * Compute name for an index. This must match code in indexcmds.c. - * - * XXX this is inherently broken because the indexes aren't created - * immediately, so we fail to resolve conflicts when the same name is - * derived for multiple indexes. However, that's a reasonably uncommon - * situation, so we'll live with it for now. - */ -static char * -chooseIndexName(const RangeVar *relation, IndexStmt *index_stmt) -{ - Oid namespaceId; - List *colnames; - - namespaceId = RangeVarGetCreationNamespace(relation); - colnames = ChooseIndexColumnNames(index_stmt->indexParams); - return ChooseIndexName(relation->relname, namespaceId, - colnames, index_stmt->excludeOpNames, - index_stmt->primary, index_stmt->isconstraint); -} - -/* * Generate an IndexStmt node using information from an already existing index * "source_idx". Attribute numbers should be adjusted according to attmap. */ @@ -1046,7 +1005,10 @@ generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx, index->tableSpace = get_tablespace_name(idxrelrec->reltablespace); else index->tableSpace = NULL; + index->excludeOpNames = NIL; + index->idxcomment = NULL; index->indexOid = InvalidOid; + index->oldNode = InvalidOid; index->unique = idxrec->indisunique; index->primary = idxrec->indisprimary; index->concurrent = false; @@ -1504,7 +1466,9 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) index->whereClause = constraint->where_clause; index->indexParams = NIL; index->excludeOpNames = NIL; + index->idxcomment = NULL; index->indexOid = InvalidOid; + index->oldNode = InvalidOid; index->concurrent = false; /* |