Primary key support - Mailing list pgsql-hackers

From D'Arcy" "J.M." Cain
Subject Primary key support
Date
Msg-id m1035Mb-0000bmC@druid.net
Whole thread Raw
List pgsql-hackers
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.attrelidAND       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?


*** ../src.original/./backend/parser/analyze.c    Sun Jan 17 23:39:14 1999
--- ./backend/parser/analyze.c    Sun Jan 17 23:39:56 1999
***************
*** 716,725 ****
--- 716,729 ----                 elog(ERROR, "CREATE TABLE/PRIMARY KEY multiple keys for table %s are not legal",
stmt->relname);             have_pkey = TRUE;
 
+             index->primary = TRUE;             index->idxname = makeTableName(stmt->relname, "pkey", NULL);         }
       else
 
+         {
+             index->primary = FALSE;             index->idxname = NULL;
+         }          index->relname = stmt->relname;         index->accessMethod = "btree";
*** ../src.original/./backend/catalog/index.c    Sat Jan 16 09:49:31 1999
--- ./backend/catalog/index.c    Sat Jan 16 09:56:53 1999
***************
*** 82,88 **** static void UpdateIndexRelation(Oid indexoid, Oid heapoid,                     FuncIndexInfo *funcInfo,
intnatts,                     AttrNumber *attNums, Oid *classOids, Node *predicate,
 
!                     List *attributeList, bool islossy, bool unique); static void DefaultBuild(Relation heapRelation,
RelationindexRelation,              int numberOfAttributes, AttrNumber *attributeNumber,              IndexStrategy
indexStrategy,uint16 parameterCount,
 
--- 82,88 ---- static void UpdateIndexRelation(Oid indexoid, Oid heapoid,                     FuncIndexInfo *funcInfo,
intnatts,                     AttrNumber *attNums, Oid *classOids, Node *predicate,
 
!                     List *attributeList, bool islossy, bool unique, bool primary); static void DefaultBuild(Relation
heapRelation,Relation indexRelation,              int numberOfAttributes, AttrNumber *attributeNumber,
IndexStrategyindexStrategy, uint16 parameterCount,
 
***************
*** 734,740 ****                     Node *predicate,                     List *attributeList,                     bool
islossy,
!                     bool unique) {     Form_pg_index indexForm;     IndexElem  *IndexKey;
--- 734,741 ----                     Node *predicate,                     List *attributeList,                     bool
islossy,
!                     bool unique,
!                     bool primary) {     Form_pg_index indexForm;     IndexElem  *IndexKey;
***************
*** 775,780 ****
--- 776,782 ----     indexForm->indproc = (PointerIsValid(funcInfo)) ?         FIgetProcOid(funcInfo) : InvalidOid;
indexForm->indislossy= islossy;
 
+     indexForm->indisprimary = primary;     indexForm->indisunique = unique;      indexForm->indhaskeytype = 0;
***************
*** 1014,1020 ****              Datum *parameter,              Node *predicate,              bool islossy,
!              bool unique) {     Relation    heapRelation;     Relation    indexRelation;
--- 1016,1023 ----              Datum *parameter,              Node *predicate,              bool islossy,
!              bool unique,
!              bool primary) {     Relation    heapRelation;     Relation    indexRelation;
***************
*** 1126,1132 ****      */     UpdateIndexRelation(indexoid, heapoid, funcInfo,                         numatts,
attNums,classObjectId, predicate,
 
!                         attributeList, islossy, unique);      predInfo = (PredInfo *) palloc(sizeof(PredInfo));
predInfo->pred= predicate;
 
--- 1129,1135 ----      */     UpdateIndexRelation(indexoid, heapoid, funcInfo,                         numatts,
attNums,classObjectId, predicate,
 
!                         attributeList, islossy, unique, primary);      predInfo = (PredInfo *)
palloc(sizeof(PredInfo));    predInfo->pred = predicate;
 
*** ../src.original/./backend/commands/cluster.c    Sat Jan 16 09:58:59 1999
--- ./backend/commands/cluster.c    Sat Jan 16 09:59:34 1999
***************
*** 321,327 ****                  Old_pg_index_Form->indclass,                  (uint16) 0, (Datum) NULL, NULL,
        Old_pg_index_Form->indislossy,
 
!                  Old_pg_index_Form->indisunique);      heap_close(OldIndex);     heap_close(NewHeap);
--- 321,328 ----                  Old_pg_index_Form->indclass,                  (uint16) 0, (Datum) NULL, NULL,
        Old_pg_index_Form->indislossy,
 
!                  Old_pg_index_Form->indisunique,
!                  Old_pg_index_Form->indisprimary);      heap_close(OldIndex);     heap_close(NewHeap);
*** ../src.original/./backend/commands/defind.c    Mon Jan 18 08:16:36 1999
--- ./backend/commands/defind.c    Mon Jan 18 08:20:09 1999
***************
*** 71,76 ****
--- 71,77 ----             List *attributeList,             List *parameterList,             bool unique,
+             bool primary,             Expr *predicate,             List *rangetable) {
***************
*** 189,195 ****                      &fInfo, NULL, accessMethodId,                      numberOfAttributes,
attributeNumberA,             classObjectId, parameterCount, parameterA, (Node *) cnfPred,
 
!                      lossy, unique);     }     else     {
--- 190,196 ----                      &fInfo, NULL, accessMethodId,                      numberOfAttributes,
attributeNumberA,             classObjectId, parameterCount, parameterA, (Node *) cnfPred,
 
!                      lossy, unique, primary);     }     else     {
***************
*** 206,212 ****                      attributeList,                      accessMethodId, numberOfAttributes,
attributeNumberA,             classObjectId, parameterCount, parameterA, (Node *) cnfPred,
 
!                      lossy, unique);     } } 
--- 207,213 ----                      attributeList,                      accessMethodId, numberOfAttributes,
attributeNumberA,             classObjectId, parameterCount, parameterA, (Node *) cnfPred,
 
!                      lossy, unique, primary);     } } 
*** ../src.original/./backend/storage/large_object/inv_api.c    Sat Jan 16 10:03:19 1999
--- ./backend/storage/large_object/inv_api.c    Sat Jan 16 10:04:19 1999
***************
*** 178,184 ****     classObjectId[0] = INT4_OPS_OID;     index_create(objname, indname, NULL, NULL, BTREE_AM_OID,
           1, &attNums[0], &classObjectId[0],
 
!                  0, (Datum) NULL, NULL, FALSE, FALSE);      /* make the index visible in this transaction */
CommandCounterIncrement();
--- 178,184 ----     classObjectId[0] = INT4_OPS_OID;     index_create(objname, indname, NULL, NULL, BTREE_AM_OID,
           1, &attNums[0], &classObjectId[0],
 
!                  0, (Datum) NULL, NULL, FALSE, FALSE, FALSE);      /* make the index visible in this transaction */
 CommandCounterIncrement();
 
*** ../src.original/./backend/bootstrap/bootparse.y    Sat Jan 16 10:05:39 1999
--- ./backend/bootstrap/bootparse.y    Sun Jan 17 23:33:13 1999
***************
*** 225,231 ****                     DefineIndex(LexIDStr($5),                                 LexIDStr($3),
                    LexIDStr($7),
 
!                                 $9, NIL, 0, 0, NIL);                     DO_END;                 }         ;
--- 225,231 ----                     DefineIndex(LexIDStr($5),                                 LexIDStr($3),
                    LexIDStr($7),
 
!                                 $9, NIL, 0, 0, 0, NIL);                     DO_END;                 }         ;
*** ../src.original/./backend/tcop/utility.c    Sun Jan 17 01:18:44 1999
--- ./backend/tcop/utility.c    Mon Jan 18 08:21:12 1999
***************
*** 404,409 ****
--- 404,410 ----                             stmt->indexParams,    /* parameters */
stmt->withClause,                            stmt->unique,
 
+                             0,        /* CREATE INDEX can't be primary */                             (Expr *)
stmt->whereClause,                            stmt->rangetable);             }
 
*** ../src.original/./include/nodes/parsenodes.h    Sun Jan 17 23:40:37 1999
--- ./include/nodes/parsenodes.h    Sun Jan 17 23:41:05 1999
***************
*** 332,337 ****
--- 332,338 ----                                  * transformStmt() */     bool       *lossy;            /* is index
lossy?*/     bool        unique;            /* is index unique? */
 
+     bool        primary;        /* is index on primary key? */ } IndexStmt;  /* ----------------------
*** ../src.original/./include/catalog/index.h    Sat Jan 16 09:57:09 1999
--- ./include/catalog/index.h    Sat Jan 16 10:04:30 1999
***************
*** 38,44 ****              Datum *parameter,              Node *predicate,              bool islossy,
!              bool unique);  extern void index_destroy(Oid indexId); 
--- 38,45 ----              Datum *parameter,              Node *predicate,              bool islossy,
!              bool unique,
!              bool primary);  extern void index_destroy(Oid indexId); 
*** ../src.original/./include/commands/defrem.h    Sat Jan 16 10:02:21 1999
--- ./include/commands/defrem.h    Mon Jan 18 08:12:52 1999
***************
*** 25,30 ****
--- 25,31 ----             List *attributeList,             List *parameterList,             bool unique,
+             bool primary,             Expr *predicate,             List *rangetable); extern void ExtendIndex(char
*indexRelationName,

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.



pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] Re: Beta test of Postgresql 6.5
Next
From: "Hiroshi Inoue"
Date:
Subject: RE: [HACKERS] SPI_prepare() doesn't work well ?