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

Commit 499c81d

Browse files
committed
Prohibit a column from appearing twice in a PRIMARY KEY or UNIQUE
constraint. This case (a) is useless, (b) violates SQL92, and (c) is certain to cause a failure downstream when we try to create an index with duplicated column names. So give an appropriate error message instead of letting the index failure occur. Per report from Colin Strickland. NOTE: currently, CREATE INDEX fooi ON foo(f1,f1) still fails with 'cannot insert duplicate key' error. Should we change that too? What about functional indexes?
1 parent 5621ec0 commit 499c81d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/backend/parser/analyze.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.191 2001/06/25 21:11:44 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.192 2001/07/04 17:36:54 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -954,8 +954,8 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
954954

955955
index = makeNode(IndexStmt);
956956

957-
index->unique = TRUE;
958-
index->primary = (constraint->contype == CONSTR_PRIMARY ? TRUE : FALSE);
957+
index->unique = true;
958+
index->primary = (constraint->contype == CONSTR_PRIMARY);
959959
if (index->primary)
960960
{
961961
if (pkey != NULL)
@@ -1057,6 +1057,17 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
10571057
elog(ERROR, "CREATE TABLE: column \"%s\" named in key does not exist",
10581058
key->name);
10591059

1060+
/* Check for PRIMARY KEY(foo, foo) */
1061+
foreach(columns, index->indexParams)
1062+
{
1063+
iparam = (IndexElem *) lfirst(columns);
1064+
if (strcmp(key->name, iparam->name) == 0)
1065+
elog(ERROR, "CREATE TABLE: column \"%s\" appears twice in %s constraint",
1066+
key->name,
1067+
index->primary ? "PRIMARY KEY" : "UNIQUE");
1068+
}
1069+
1070+
/* OK, add it to the index definition */
10601071
iparam = makeNode(IndexElem);
10611072
iparam->name = pstrdup(key->name);
10621073
iparam->args = NIL;

0 commit comments

Comments
 (0)