7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -366,6 +366,57 @@ makeTableName(void *elem,...)
366
366
return (name );
367
367
} /* makeTableName() */
368
368
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
+
369
420
/*
370
421
* transformCreateStmt -
371
422
* transforms the "create table" statement
@@ -379,6 +430,7 @@ static Query *
379
430
transformCreateStmt (ParseState * pstate , CreateStmt * stmt )
380
431
{
381
432
Query * q ;
433
+ int have_pkey = FALSE;
382
434
List * elements ;
383
435
Node * element ;
384
436
List * columns ;
@@ -541,9 +593,9 @@ printf("transformCreateStmt- found CHECK clause\n");
541
593
* For UNIQUE, create an index as for PRIMARY KEYS, but do not insist on NOT NULL.
542
594
*
543
595
* 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
547
599
*/
548
600
ilist = NIL ;
549
601
while (dlist != NIL )
@@ -552,27 +604,38 @@ printf("transformCreateStmt- found CHECK clause\n");
552
604
if (nodeTag (constraint ) != T_Constraint )
553
605
elog (WARN ,"parser: internal error; unrecognized deferred node" ,NULL );
554
606
555
- if ((constraint -> contype != CONSTR_PRIMARY )
556
- && (constraint -> contype != CONSTR_UNIQUE ))
557
- elog (WARN ,"parser: internal error; illegal deferred constraint" ,NULL );
558
-
559
607
#if PARSEDEBUG
560
608
printf ("transformCreateStmt- found deferred constraint %s\n" ,
561
609
((constraint -> name != NULL )? constraint -> name : "(unknown)" ));
562
610
#endif
563
611
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
+
564
621
#if PARSEDEBUG
565
622
printf ("transformCreateStmt- found deferred %s clause\n" ,
566
623
(constraint -> contype == CONSTR_PRIMARY ? "PRIMARY KEY" : "UNIQUE" ));
567
624
#endif
625
+
568
626
index = makeNode (IndexStmt );
569
- ilist = lappend (ilist , index );
570
627
571
628
index -> unique = TRUE;
572
629
if (constraint -> name != NULL )
573
630
index -> idxname = constraint -> name ;
574
631
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;
575
637
index -> idxname = makeTableName (stmt -> relname , "pkey" , NULL );
638
+ }
576
639
else
577
640
index -> idxname = NULL ;
578
641
@@ -619,15 +682,20 @@ printf("transformCreateStmt- mark column %s as NOT NULL\n", column->colname);
619
682
index -> indexParams = lappend (index -> indexParams , iparam );
620
683
621
684
if (index -> idxname == NULL )
622
- index -> idxname = makeTableName (stmt -> relname , iparam -> name , "key" , NULL );
685
+ index -> idxname = CreateIndexName (stmt -> relname , iparam -> name , "key" , ilist );
623
686
624
687
keys = lnext (keys );
625
688
}
626
689
627
690
if (index -> idxname == NULL )
628
691
elog (WARN ,"parser: unable to construct implicit index for table %s"
629
692
"; 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 );
630
697
698
+ ilist = lappend (ilist , index );
631
699
dlist = lnext (dlist );
632
700
}
633
701
0 commit comments