Index: src/backend/parser/analyze.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/parser/analyze.c,v retrieving revision 1.264 diff -c -r1.264 analyze.c *** src/backend/parser/analyze.c 2003/02/13 22:50:01 1.264 --- src/backend/parser/analyze.c 2003/02/17 05:12:37 *************** *** 667,693 **** } static char * ! CreateIndexName(char *table_name, char *column_name, ! char *label, List *indices) { ! int pass = 0; ! char *iname = NULL; ! List *ilist; char typename[NAMEDATALEN]; /* * The type name for makeObjectName is label, or labelN if that's * necessary to prevent collisions among multiple indexes for the same ! * table. Note there is no check for collisions with already-existing ! * indexes, only among the indexes we're about to create now; this ! * ought to be improved someday. */ strncpy(typename, label, sizeof(typename)); for (;;) { iname = makeObjectName(table_name, column_name, typename); foreach(ilist, indices) { IndexStmt *index = lfirst(ilist); --- 667,693 ---- } static char * ! CreateIndexName(Oid namespaceId, char *table_name, char *column_name, ! const char *label, List *indices) { ! int pass = 0; ! char *iname = NULL; ! List *ilist; char typename[NAMEDATALEN]; /* * The type name for makeObjectName is label, or labelN if that's * necessary to prevent collisions among multiple indexes for the same ! * table. Note that names of existing indexes are also checked. */ strncpy(typename, label, sizeof(typename)); for (;;) { + /* Generate a possible name */ iname = makeObjectName(table_name, column_name, typename); + /* Check to see if is already used in the statement */ foreach(ilist, indices) { IndexStmt *index = lfirst(ilist); *************** *** 696,704 **** strcmp(iname, index->idxname) == 0) break; } ! /* ran through entire list? then no name conflict found so done */ ! if (ilist == NIL) break; /* found a conflict, so try a new name component */ pfree(iname); --- 696,707 ---- strcmp(iname, index->idxname) == 0) break; } ! /* If we've run through the entire list, and the relation ! does not exist in the schema already then we can ! stop looping */ ! if (ilist == NIL && get_relname_relid(iname, namespaceId) == InvalidOid) { break; + } /* found a conflict, so try a new name component */ pfree(iname); *************** *** 839,850 **** A_Const *snamenode; FuncCall *funccallnode; CreateSeqStmt *seqstmt; /* * Determine name and namespace to use for the sequence. */ ! sname = makeObjectName(cxt->relation->relname, column->colname, "seq"); ! snamespace = get_namespace_name(RangeVarGetCreationNamespace(cxt->relation)); elog(NOTICE, "%s will create implicit sequence '%s' for SERIAL column '%s.%s'", cxt->stmtType, sname, cxt->relation->relname, column->colname); --- 842,855 ---- A_Const *snamenode; FuncCall *funccallnode; CreateSeqStmt *seqstmt; + Oid namespaceId; /* * Determine name and namespace to use for the sequence. */ ! namespaceId = RangeVarGetCreationNamespace(cxt->relation); ! snamespace = get_namespace_name(namespaceId); ! sname = CreateIndexName(namespaceId, cxt->relation->relname, column->colname, "seq", NIL); elog(NOTICE, "%s will create implicit sequence '%s' for SERIAL column '%s.%s'", cxt->stmtType, sname, cxt->relation->relname, column->colname); *************** *** 950,969 **** break; case CONSTR_PRIMARY: ! if (constraint->name == NULL) ! constraint->name = makeObjectName(cxt->relation->relname, ! NULL, ! "pkey"); if (constraint->keys == NIL) constraint->keys = makeList1(makeString(column->colname)); cxt->ixconstraints = lappend(cxt->ixconstraints, constraint); break; case CONSTR_UNIQUE: ! if (constraint->name == NULL) ! constraint->name = makeObjectName(cxt->relation->relname, ! column->colname, ! "key"); if (constraint->keys == NIL) constraint->keys = makeList1(makeString(column->colname)); cxt->ixconstraints = lappend(cxt->ixconstraints, constraint); --- 955,976 ---- break; case CONSTR_PRIMARY: ! if (constraint->name == NULL) { ! Oid namespaceId; ! namespaceId = RangeVarGetCreationNamespace(cxt->relation); ! constraint->name = CreateIndexName(namespaceId, cxt->relation->relname, NULL, "pkey", NIL); ! } if (constraint->keys == NIL) constraint->keys = makeList1(makeString(column->colname)); cxt->ixconstraints = lappend(cxt->ixconstraints, constraint); break; case CONSTR_UNIQUE: ! if (constraint->name == NULL) { ! Oid namespaceId; ! namespaceId = RangeVarGetCreationNamespace(cxt->relation); ! constraint->name = CreateIndexName(namespaceId, cxt->relation->relname, column->colname, "key", NIL); ! } if (constraint->keys == NIL) constraint->keys = makeList1(makeString(column->colname)); cxt->ixconstraints = lappend(cxt->ixconstraints, constraint); *************** *** 998,1007 **** switch (constraint->contype) { case CONSTR_PRIMARY: ! if (constraint->name == NULL) ! constraint->name = makeObjectName(cxt->relation->relname, ! NULL, ! "pkey"); cxt->ixconstraints = lappend(cxt->ixconstraints, constraint); break; --- 1005,1015 ---- switch (constraint->contype) { case CONSTR_PRIMARY: ! if (constraint->name == NULL) { ! Oid namespaceId; ! namespaceId = RangeVarGetCreationNamespace(cxt->relation); ! constraint->name = CreateIndexName(namespaceId, cxt->relation->relname, NULL, "pkey", NIL); ! } cxt->ixconstraints = lappend(cxt->ixconstraints, constraint); break; *************** *** 1073,1080 **** if (constraint->name != NULL) index->idxname = pstrdup(constraint->name); ! else if (constraint->contype == CONSTR_PRIMARY) ! index->idxname = makeObjectName(cxt->relation->relname, NULL, "pkey"); else index->idxname = NULL; /* will set it later */ --- 1081,1091 ---- if (constraint->name != NULL) index->idxname = pstrdup(constraint->name); ! else if (constraint->contype == CONSTR_PRIMARY) { ! Oid namespaceId; ! namespaceId = RangeVarGetCreationNamespace(cxt->relation); ! index->idxname = CreateIndexName(namespaceId, cxt->relation->relname, NULL, "pkey", NIL); ! } else index->idxname = NULL; /* will set it later */ *************** *** 1259,1267 **** /* * Finally, select unique names for all not-previously-named indices, * and display NOTICE messages. - * - * XXX in ALTER TABLE case, we fail to consider name collisions against - * pre-existing indexes. */ foreach(indexlist, cxt->alist) { --- 1270,1275 ---- *************** *** 1269,1276 **** if (index->idxname == NULL && index->indexParams != NIL) { iparam = lfirst(index->indexParams); ! index->idxname = CreateIndexName(cxt->relation->relname, iparam->name ? iparam->name : strVal(llast(iparam->funcname)), "key", cxt->alist); --- 1277,1287 ---- if (index->idxname == NULL && index->indexParams != NIL) { + Oid namespaceId; iparam = lfirst(index->indexParams); ! namespaceId = RangeVarGetCreationNamespace(cxt->relation); ! index->idxname = CreateIndexName(namespaceId, ! cxt->relation->relname, iparam->name ? iparam->name : strVal(llast(iparam->funcname)), "key", cxt->alist);