Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2ac4cf5

Browse files
author
Thomas G. Lockhart
committed
Add information message about implicitly created indices.
Check for duplicate implicit index names and generate unique names.
1 parent a440f8e commit 2ac4cf5

File tree

1 file changed

+78
-10
lines changed

1 file changed

+78
-10
lines changed

src/backend/parser/analyze.c

+78-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.52 1997/12/04 23:07:18 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.53 1997/12/09 01:44:14 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -366,6 +366,57 @@ makeTableName(void *elem,...)
366366
return (name);
367367
} /* makeTableName() */
368368

369+
char *
370+
CreateIndexName(char *tname, char *cname, char *label, List *indices);
371+
372+
char *
373+
CreateIndexName(char *tname, char *cname, char *label, List *indices)
374+
{
375+
int pass = 0;
376+
char *iname = NULL;
377+
List *ilist;
378+
IndexStmt *index;
379+
char name2[NAMEDATALEN+1];
380+
381+
/* use working storage, since we might be trying several possibilities */
382+
strcpy(name2,cname);
383+
while (iname == NULL)
384+
{
385+
iname = makeTableName(tname, name2, label, NULL);
386+
/* unable to make a name at all? then quit */
387+
if (iname == NULL)
388+
break;
389+
390+
#if PARSEDEBUG
391+
printf("CreateNameIndex- check %s against indices\n",iname);
392+
#endif
393+
394+
ilist = indices;
395+
while (ilist != NIL)
396+
{
397+
index = lfirst(ilist);
398+
#if PARSEDEBUG
399+
printf("CreateNameIndex- compare %s with existing index %s\n",iname,index->idxname);
400+
#endif
401+
if (strcasecmp(iname,index->idxname) == 0)
402+
break;
403+
404+
ilist = lnext(ilist);
405+
}
406+
/* ran through entire list? then no name conflict found so done */
407+
if (ilist == NIL)
408+
break;
409+
410+
/* the last one conflicted, so try a new name component */
411+
pfree(iname);
412+
iname = NULL;
413+
pass++;
414+
sprintf(name2, "%s_%d", cname, (pass+1));
415+
}
416+
417+
return (iname);
418+
} /* CreateIndexName() */
419+
369420
/*
370421
* transformCreateStmt -
371422
* transforms the "create table" statement
@@ -379,6 +430,7 @@ static Query *
379430
transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
380431
{
381432
Query *q;
433+
int have_pkey = FALSE;
382434
List *elements;
383435
Node *element;
384436
List *columns;
@@ -541,9 +593,9 @@ printf("transformCreateStmt- found CHECK clause\n");
541593
* For UNIQUE, create an index as for PRIMARY KEYS, but do not insist on NOT NULL.
542594
*
543595
* Note that this code does not currently look for all possible redundant cases
544-
* and either ignore or stop with warning. The create will fail later when
545-
* names for indices turn out to be redundant, or a user might just find
546-
* extra useless indices which might kill performance. - thomas 1997-12-04
596+
* and either ignore or stop with warning. The create might fail later when
597+
* names for indices turn out to be redundant, or a user might have specified
598+
* extra useless indices which might hurt performance. - thomas 1997-12-08
547599
*/
548600
ilist = NIL;
549601
while (dlist != NIL)
@@ -552,27 +604,38 @@ printf("transformCreateStmt- found CHECK clause\n");
552604
if (nodeTag(constraint) != T_Constraint)
553605
elog(WARN,"parser: internal error; unrecognized deferred node",NULL);
554606

555-
if ((constraint->contype != CONSTR_PRIMARY)
556-
&& (constraint->contype != CONSTR_UNIQUE))
557-
elog(WARN,"parser: internal error; illegal deferred constraint",NULL);
558-
559607
#if PARSEDEBUG
560608
printf("transformCreateStmt- found deferred constraint %s\n",
561609
((constraint->name != NULL)? constraint->name: "(unknown)"));
562610
#endif
563611

612+
if (constraint->contype == CONSTR_PRIMARY)
613+
if (have_pkey)
614+
elog(WARN,"CREATE TABLE/PRIMARY KEY multiple primary keys"
615+
" for table %s are not legal", stmt->relname);
616+
else
617+
have_pkey = TRUE;
618+
else if (constraint->contype != CONSTR_UNIQUE)
619+
elog(WARN,"parser: internal error; unrecognized deferred constraint",NULL);
620+
564621
#if PARSEDEBUG
565622
printf("transformCreateStmt- found deferred %s clause\n",
566623
(constraint->contype == CONSTR_PRIMARY? "PRIMARY KEY": "UNIQUE"));
567624
#endif
625+
568626
index = makeNode(IndexStmt);
569-
ilist = lappend(ilist, index);
570627

571628
index->unique = TRUE;
572629
if (constraint->name != NULL)
573630
index->idxname = constraint->name;
574631
else if (constraint->contype == CONSTR_PRIMARY)
632+
{
633+
if (have_pkey)
634+
elog(WARN,"CREATE TABLE/PRIMARY KEY multiple keys for table %s are not legal", stmt->relname);
635+
636+
have_pkey = TRUE;
575637
index->idxname = makeTableName(stmt->relname, "pkey", NULL);
638+
}
576639
else
577640
index->idxname = NULL;
578641

@@ -619,15 +682,20 @@ printf("transformCreateStmt- mark column %s as NOT NULL\n", column->colname);
619682
index->indexParams = lappend(index->indexParams, iparam);
620683

621684
if (index->idxname == NULL)
622-
index->idxname = makeTableName(stmt->relname, iparam->name, "key", NULL);
685+
index->idxname = CreateIndexName(stmt->relname, iparam->name, "key", ilist);
623686

624687
keys = lnext(keys);
625688
}
626689

627690
if (index->idxname == NULL)
628691
elog(WARN,"parser: unable to construct implicit index for table %s"
629692
"; name too long", stmt->relname);
693+
else
694+
elog(NOTICE,"CREATE TABLE/%s will create implicit index %s for table %s",
695+
((constraint->contype == CONSTR_PRIMARY)? "PRIMARY KEY": "UNIQUE"),
696+
index->idxname, stmt->relname);
630697

698+
ilist = lappend(ilist, index);
631699
dlist = lnext(dlist);
632700
}
633701

0 commit comments

Comments
 (0)