Skip to content

Commit c91dbcc

Browse files
committed
The following patch finishes primary key support. Previously, when
a field was labelled as a primary key, the system automatically created a unique index on the field. This patch extends it so that the index has the indisprimary field set. You can pull a list of primary keys with the followiing select. SELECT pg_class.relname, pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[0] = pg_attribute.attnum AND pg_index.indisunique = 't'; There is nothing in this patch that modifies the template database to set the indisprimary attribute for system tables. Should they be changed or should we only be concerned with user tables? D'Arcy
1 parent 7311da9 commit c91dbcc

File tree

10 files changed

+33
-20
lines changed

10 files changed

+33
-20
lines changed

src/backend/bootstrap/bootparse.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.21 1998/08/24 01:13:36 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.22 1999/01/21 22:48:04 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -225,7 +225,7 @@ Boot_DeclareIndexStmt:
225225
DefineIndex(LexIDStr($5),
226226
LexIDStr($3),
227227
LexIDStr($7),
228-
$9, NIL, 0, 0, NIL);
228+
$9, NIL, 0, 0, 0, NIL);
229229
DO_END;
230230
}
231231
;

src/backend/catalog/index.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.66 1998/12/15 12:45:43 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.67 1999/01/21 22:48:05 momjian Exp $
1111
*
1212
*
1313
* INTERFACE ROUTINES
@@ -82,7 +82,7 @@ static void
8282
static void UpdateIndexRelation(Oid indexoid, Oid heapoid,
8383
FuncIndexInfo *funcInfo, int natts,
8484
AttrNumber *attNums, Oid *classOids, Node *predicate,
85-
List *attributeList, bool islossy, bool unique);
85+
List *attributeList, bool islossy, bool unique, bool primary);
8686
static void DefaultBuild(Relation heapRelation, Relation indexRelation,
8787
int numberOfAttributes, AttrNumber *attributeNumber,
8888
IndexStrategy indexStrategy, uint16 parameterCount,
@@ -734,7 +734,8 @@ UpdateIndexRelation(Oid indexoid,
734734
Node *predicate,
735735
List *attributeList,
736736
bool islossy,
737-
bool unique)
737+
bool unique,
738+
bool primary)
738739
{
739740
Form_pg_index indexForm;
740741
IndexElem *IndexKey;
@@ -775,6 +776,7 @@ UpdateIndexRelation(Oid indexoid,
775776
indexForm->indproc = (PointerIsValid(funcInfo)) ?
776777
FIgetProcOid(funcInfo) : InvalidOid;
777778
indexForm->indislossy = islossy;
779+
indexForm->indisprimary = primary;
778780
indexForm->indisunique = unique;
779781

780782
indexForm->indhaskeytype = 0;
@@ -1014,7 +1016,8 @@ index_create(char *heapRelationName,
10141016
Datum *parameter,
10151017
Node *predicate,
10161018
bool islossy,
1017-
bool unique)
1019+
bool unique,
1020+
bool primary)
10181021
{
10191022
Relation heapRelation;
10201023
Relation indexRelation;
@@ -1126,7 +1129,7 @@ index_create(char *heapRelationName,
11261129
*/
11271130
UpdateIndexRelation(indexoid, heapoid, funcInfo,
11281131
numatts, attNums, classObjectId, predicate,
1129-
attributeList, islossy, unique);
1132+
attributeList, islossy, unique, primary);
11301133

11311134
predInfo = (PredInfo *) palloc(sizeof(PredInfo));
11321135
predInfo->pred = predicate;

src/backend/commands/cluster.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.34 1998/12/14 05:18:39 scrappy Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.35 1999/01/21 22:48:06 momjian Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -321,7 +321,8 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap)
321321
Old_pg_index_Form->indclass,
322322
(uint16) 0, (Datum) NULL, NULL,
323323
Old_pg_index_Form->indislossy,
324-
Old_pg_index_Form->indisunique);
324+
Old_pg_index_Form->indisunique,
325+
Old_pg_index_Form->indisprimary);
325326

326327
heap_close(OldIndex);
327328
heap_close(NewHeap);

src/backend/commands/defind.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/defind.c,v 1.29 1998/12/15 12:45:56 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/defind.c,v 1.30 1999/01/21 22:48:06 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -71,6 +71,7 @@ DefineIndex(char *heapRelationName,
7171
List *attributeList,
7272
List *parameterList,
7373
bool unique,
74+
bool primary,
7475
Expr *predicate,
7576
List *rangetable)
7677
{
@@ -189,7 +190,7 @@ DefineIndex(char *heapRelationName,
189190
&fInfo, NULL, accessMethodId,
190191
numberOfAttributes, attributeNumberA,
191192
classObjectId, parameterCount, parameterA, (Node *) cnfPred,
192-
lossy, unique);
193+
lossy, unique, primary);
193194
}
194195
else
195196
{
@@ -206,7 +207,7 @@ DefineIndex(char *heapRelationName,
206207
attributeList,
207208
accessMethodId, numberOfAttributes, attributeNumberA,
208209
classObjectId, parameterCount, parameterA, (Node *) cnfPred,
209-
lossy, unique);
210+
lossy, unique, primary);
210211
}
211212
}
212213

src/backend/parser/analyze.c

Lines changed: 5 additions & 1 deletion
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.93 1999/01/21 16:08:38 vadim Exp $
8+
* $Id: analyze.c,v 1.94 1999/01/21 22:48:07 momjian Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -721,10 +721,14 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
721721
elog(ERROR, "CREATE TABLE/PRIMARY KEY multiple keys for table %s are not legal", stmt->relname);
722722

723723
have_pkey = TRUE;
724+
index->primary = TRUE;
724725
index->idxname = makeTableName(stmt->relname, "pkey", NULL);
725726
}
726727
else
728+
{
729+
index->primary = FALSE;
727730
index->idxname = NULL;
731+
}
728732

729733
index->relname = stmt->relname;
730734
index->accessMethod = "btree";

src/backend/storage/large_object/inv_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.44 1998/12/15 12:46:26 vadim Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.45 1999/01/21 22:48:09 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -178,7 +178,7 @@ inv_create(int flags)
178178
classObjectId[0] = INT4_OPS_OID;
179179
index_create(objname, indname, NULL, NULL, BTREE_AM_OID,
180180
1, &attNums[0], &classObjectId[0],
181-
0, (Datum) NULL, NULL, FALSE, FALSE);
181+
0, (Datum) NULL, NULL, FALSE, FALSE, FALSE);
182182

183183
/* make the index visible in this transaction */
184184
CommandCounterIncrement();

src/backend/tcop/utility.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.52 1999/01/17 06:18:44 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.53 1999/01/21 22:48:11 momjian Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -404,6 +404,7 @@ ProcessUtility(Node *parsetree,
404404
stmt->indexParams, /* parameters */
405405
stmt->withClause,
406406
stmt->unique,
407+
0, /* CREATE INDEX can't be primary */
407408
(Expr *) stmt->whereClause,
408409
stmt->rangetable);
409410
}

src/include/catalog/index.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: index.h,v 1.13 1998/09/01 04:34:43 momjian Exp $
9+
* $Id: index.h,v 1.14 1999/01/21 22:48:14 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -38,7 +38,8 @@ extern void index_create(char *heapRelationName,
3838
Datum *parameter,
3939
Node *predicate,
4040
bool islossy,
41-
bool unique);
41+
bool unique,
42+
bool primary);
4243

4344
extern void index_destroy(Oid indexId);
4445

src/include/commands/defrem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: defrem.h,v 1.13 1998/09/01 04:35:30 momjian Exp $
9+
* $Id: defrem.h,v 1.14 1999/01/21 22:48:16 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -25,6 +25,7 @@ extern void DefineIndex(char *heapRelationName,
2525
List *attributeList,
2626
List *parameterList,
2727
bool unique,
28+
bool primary,
2829
Expr *predicate,
2930
List *rangetable);
3031
extern void ExtendIndex(char *indexRelationName,

src/include/nodes/parsenodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: parsenodes.h,v 1.67 1999/01/21 16:08:55 vadim Exp $
9+
* $Id: parsenodes.h,v 1.68 1999/01/21 22:48:20 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -333,6 +333,7 @@ typedef struct IndexStmt
333333
* transformStmt() */
334334
bool *lossy; /* is index lossy? */
335335
bool unique; /* is index unique? */
336+
bool primary; /* is index on primary key? */
336337
} IndexStmt;
337338

338339
/* ----------------------

0 commit comments

Comments
 (0)