Skip to content

Commit 3cfd56f

Browse files
author
Thomas G. Lockhart
committed
Repair the check for redundant UNIQUE and PRIMARY KEY indices.
Also, improve it so that it checks for multi-column constraints. Thanks to Mark Dalphin <mdalphin@amgen.com> for reporting the problem.
1 parent 288373f commit 3cfd56f

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/backend/parser/analyze.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: analyze.c,v 1.116 1999/07/19 00:26:18 tgl Exp $
8+
* $Id: analyze.c,v 1.117 1999/08/15 06:46:49 thomas Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -579,6 +579,9 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
579579
columns = NIL;
580580
dlist = NIL;
581581

582+
/*
583+
* Run through each primary element in the table creation clause
584+
*/
582585
while (elements != NIL)
583586
{
584587
element = lfirst(elements);
@@ -588,6 +591,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
588591
column = (ColumnDef *) element;
589592
columns = lappend(columns, column);
590593

594+
/* Special case SERIAL type? */
591595
if (column->is_sequence)
592596
{
593597
char *sname;
@@ -625,6 +629,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
625629
blist = lcons(sequence, NIL);
626630
}
627631

632+
/* Check for column constraints, if any... */
628633
if (column->constraints != NIL)
629634
{
630635
clist = column->constraints;
@@ -815,28 +820,43 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
815820
* or if a SERIAL column was defined along with a table PRIMARY KEY constraint.
816821
* - thomas 1999-05-11
817822
*/
818-
if ((pkey != NULL) && (length(lfirst(pkey->indexParams)) == 1))
823+
if (pkey != NULL)
819824
{
820825
dlist = ilist;
821826
ilist = NIL;
822827
while (dlist != NIL)
823828
{
824-
int keep = TRUE;
829+
List *pcols, *icols;
830+
int plen, ilen;
831+
int keep = TRUE;
825832

826833
index = lfirst(dlist);
834+
pcols = pkey->indexParams;
835+
icols = index->indexParams;
827836

828-
/*
829-
* has a single column argument, so might be a conflicting
830-
* index...
831-
*/
832-
if ((index != pkey)
833-
&& (length(index->indexParams) == 1))
837+
plen = length(pcols);
838+
ilen = length(icols);
839+
840+
/* Not the same as the primary key? Then we should look... */
841+
if ((index != pkey) && (ilen == plen))
834842
{
835-
char *pname = ((IndexElem *) lfirst(index->indexParams))->name;
836-
char *iname = ((IndexElem *) lfirst(index->indexParams))->name;
843+
keep = FALSE;
844+
while ((pcols != NIL) && (icols != NIL))
845+
{
846+
IndexElem *pcol = lfirst(pcols);
847+
IndexElem *icol = lfirst(icols);
848+
char *pname = pcol->name;
849+
char *iname = icol->name;
837850

838-
/* same names? then don't keep... */
839-
keep = (strcmp(iname, pname) != 0);
851+
/* different names? then no match... */
852+
if (strcmp(iname, pname) != 0)
853+
{
854+
keep = TRUE;
855+
break;
856+
}
857+
pcols = lnext(pcols);
858+
icols = lnext(icols);
859+
}
840860
}
841861

842862
if (keep)

0 commit comments

Comments
 (0)