]> granicus.if.org Git - postgresql/commitdiff
Update GiST for new pg_opclass arrangement (finally a clean solution
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 22 Aug 2001 18:24:26 +0000 (18:24 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 22 Aug 2001 18:24:26 +0000 (18:24 +0000)
for haskeytype).  Update GiST contrib modules too.  Add linear-time split
algorithm for R-tree GiST opclass.
From Oleg Bartunov and Teodor Sigaev.

contrib/intarray/_int.sql.in
contrib/rtree_gist/expected/rtree_gist.out
contrib/rtree_gist/rtree_gist.c
contrib/rtree_gist/rtree_gist.sql.in
contrib/rtree_gist/sql/rtree_gist.sql
src/backend/access/gist/gist.c
src/backend/access/gist/gistget.c
src/backend/access/gist/gistscan.c
src/backend/catalog/index.c
src/include/access/gist.h

index 75ead9360012d1a6b69066aad4e9580015d67871..6ee186dc2676e12ab9e14d36459881d3527c0122 100644 (file)
@@ -86,7 +86,7 @@ CREATE FUNCTION g_int_decompress(opaque) RETURNS opaque
        AS 'MODULE_PATHNAME' LANGUAGE 'c';
 
 CREATE FUNCTION g_int_penalty(opaque,opaque,opaque) RETURNS opaque
-       AS 'MODULE_PATHNAME' LANGUAGE 'c';
+       AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);
 
 CREATE FUNCTION g_int_picksplit(opaque, opaque) RETURNS opaque
        AS 'MODULE_PATHNAME' LANGUAGE 'c';
@@ -105,7 +105,7 @@ INSERT INTO pg_opclass (opcamid, opcname, opcintype, opcdefault, opckeytype)
         'gist__int_ops',
         (SELECT oid FROM pg_type WHERE typname = '_int4'),
         true,
-        (SELECT oid FROM pg_type WHERE typname = '_int4'));
+        0);
 
 
 -- get the comparators for _intments and store them in a tmp table
@@ -252,7 +252,7 @@ INSERT INTO pg_opclass (opcamid, opcname, opcintype, opcdefault, opckeytype)
         'gist__intbig_ops',
         (SELECT oid FROM pg_type WHERE typname = '_int4'),
         false,
-        (SELECT oid FROM pg_type WHERE typname = '_int4'));
+        0);
 
 
 -- get the comparators for _intments and store them in a tmp table
index 27f88a859a8930d0e78ce7382e90431b6c2446e5..74ac7e640d118473b592472fda05c5fe53e39ca0 100644 (file)
@@ -19,7 +19,7 @@ select count(*) from boxtmp where b && '(1000,1000,0,0)'::box;
 (1 row)
 
 drop index bix;
-create index bix on boxtmp using gist (b gist_box_ops);
+create index bix on boxtmp using gist (b);
 select count(*) from boxtmp where b && '(1000,1000,0,0)'::box;
  count 
 -------
@@ -36,7 +36,7 @@ select count(*) from polytmp where p && '(1000,1000),(0,0)'::polygon;
 (1 row)
 
 drop index pix;
-create index pix on polytmp using gist (p gist_poly_ops);
+create index pix on polytmp using gist (p);
 select count(*) from polytmp where p && '(1000,1000),(0,0)'::polygon;
  count 
 -------
index abb12d2276929a6855c09a783c4acaef7daf0ce9..5f6f688871e6f9f1a1419c2cf381b78b9497dc17 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *     $Header: /cvsroot/pgsql/contrib/rtree_gist/Attic/rtree_gist.c,v 1.1 2001/05/31 18:27:18 tgl Exp $
+ *     $Header: /cvsroot/pgsql/contrib/rtree_gist/Attic/rtree_gist.c,v 1.2 2001/08/22 18:24:26 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -24,14 +24,6 @@ typedef Datum (*RDF)(PG_FUNCTION_ARGS);
 typedef Datum (*BINARY_UNION)(Datum, Datum, int*);
 typedef float (*SIZE_BOX)(Datum);
 
-/*
-** Workaround for index_formtuple
-*/
-typedef struct polykey {
-       int32 size; /* size in varlena terms */ 
-       BOX     key;
-} POLYKEY; 
-
 /*
 ** box ops
 */
@@ -42,14 +34,13 @@ PG_FUNCTION_INFO_V1(gbox_consistent);
 PG_FUNCTION_INFO_V1(gbox_penalty);
 PG_FUNCTION_INFO_V1(gbox_same);
 
-GISTENTRY * gbox_compress(PG_FUNCTION_ARGS);
-BOX *gbox_union(PG_FUNCTION_ARGS);
-GIST_SPLITVEC * gbox_picksplit(PG_FUNCTION_ARGS);
-bool gbox_consistent(PG_FUNCTION_ARGS);
-float * gbox_penalty(PG_FUNCTION_ARGS);
-bool * gbox_same(PG_FUNCTION_ARGS);
+Datum gbox_compress(PG_FUNCTION_ARGS);
+Datum gbox_union(PG_FUNCTION_ARGS);
+Datum gbox_picksplit(PG_FUNCTION_ARGS);
+Datum gbox_consistent(PG_FUNCTION_ARGS);
+Datum gbox_penalty(PG_FUNCTION_ARGS);
+Datum gbox_same(PG_FUNCTION_ARGS);
 
-static Datum gbox_binary_union(Datum r1, Datum r2, int *sizep);
 static bool gbox_leaf_consistent(BOX *key, BOX *query, StrategyNumber strategy);
 static float size_box( Datum box );
 
@@ -57,37 +48,19 @@ static float size_box( Datum box );
 ** Polygon ops 
 */
 PG_FUNCTION_INFO_V1(gpoly_compress);
-PG_FUNCTION_INFO_V1(gpoly_union);
-PG_FUNCTION_INFO_V1(gpoly_picksplit);
 PG_FUNCTION_INFO_V1(gpoly_consistent);
-PG_FUNCTION_INFO_V1(gpoly_penalty);
-PG_FUNCTION_INFO_V1(gpoly_same);
-
-GISTENTRY * gpoly_compress(PG_FUNCTION_ARGS);
-POLYKEY *gpoly_union(PG_FUNCTION_ARGS);
-GIST_SPLITVEC * gpoly_picksplit(PG_FUNCTION_ARGS);
-bool gpoly_consistent(PG_FUNCTION_ARGS);
-float * gpoly_penalty(PG_FUNCTION_ARGS);
-bool * gpoly_same(PG_FUNCTION_ARGS);
-
-static Datum gpoly_binary_union(Datum r1, Datum r2, int *sizep);
-static float size_polykey( Datum pk );
 
-PG_FUNCTION_INFO_V1(gpoly_inter);
-Datum gpoly_inter(PG_FUNCTION_ARGS);
+Datum gpoly_compress(PG_FUNCTION_ARGS);
+Datum gpoly_consistent(PG_FUNCTION_ARGS);
 
 /*
 ** Common rtree-function (for all ops)
 */
-static Datum rtree_union(bytea *entryvec, int *sizep, BINARY_UNION bu);
-static float * rtree_penalty(GISTENTRY *origentry, GISTENTRY *newentry, 
-               float *result, BINARY_UNION bu, SIZE_BOX sb);
-static GIST_SPLITVEC * rtree_picksplit(bytea *entryvec, GIST_SPLITVEC *v, 
-       int keylen, BINARY_UNION bu, RDF interop, SIZE_BOX sb);
 static bool rtree_internal_consistent(BOX *key, BOX *query, StrategyNumber strategy);
 
 PG_FUNCTION_INFO_V1(rtree_decompress);
-GISTENTRY * rtree_decompress(PG_FUNCTION_ARGS);
+
+Datum rtree_decompress(PG_FUNCTION_ARGS);
 
 /**************************************************
  * Box ops
@@ -99,18 +72,19 @@ GISTENTRY * rtree_decompress(PG_FUNCTION_ARGS);
 ** the predicate x op query == FALSE, where op is the oper
 ** corresponding to strategy in the pg_amop table.
 */
-bool 
+Datum
 gbox_consistent(PG_FUNCTION_ARGS)
 {
     GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
     BOX *query       = (BOX*)      PG_GETARG_POINTER(1);
     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
+
     /*
     ** if entry is not leaf, use gbox_internal_consistent,
     ** else use gbox_leaf_consistent
     */
     if ( ! (DatumGetPointer(entry->key) != NULL && query) )
-               return FALSE;
+               PG_RETURN_BOOL(FALSE);
 
     if (GIST_LEAF(entry))
       PG_RETURN_BOOL(gbox_leaf_consistent((BOX *) DatumGetPointer(entry->key), query, strategy));
@@ -123,66 +97,221 @@ gbox_consistent(PG_FUNCTION_ARGS)
 ** The GiST Union method for boxes
 ** returns the minimal bounding box that encloses all the entries in entryvec
 */
-BOX *
+Datum
 gbox_union(PG_FUNCTION_ARGS)
 {
-    return (BOX*) 
-               DatumGetPointer(rtree_union(
-                       (bytea*) PG_GETARG_POINTER(0),
-                       (int*) PG_GETARG_POINTER(1),
-                       gbox_binary_union
-                       ));
+    bytea *entryvec = (bytea*) PG_GETARG_POINTER(0);
+    int *sizep = (int*) PG_GETARG_POINTER(1);
+    int numranges, i;
+    BOX        *cur, *pageunion;
+
+    numranges = (VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY); 
+    pageunion = (BOX *)palloc( sizeof(BOX) );
+    cur = DatumGetBoxP( ((GISTENTRY *) VARDATA(entryvec))[0].key );
+    memcpy( (void*)pageunion, (void*)cur, sizeof( BOX ) ); 
+   
+    for (i = 1; i < numranges; i++) {
+       cur = DatumGetBoxP( ((GISTENTRY *) VARDATA(entryvec))[i].key );
+       if ( pageunion->high.x < cur->high.x ) 
+               pageunion->high.x  = cur->high.x;
+       if ( pageunion->low.x  > cur->low.x  ) 
+               pageunion->low.x   = cur->low.x;
+       if ( pageunion->high.y < cur->high.y ) 
+               pageunion->high.y  = cur->high.y;
+       if ( pageunion->low.y  > cur->low.y  ) 
+               pageunion->low.y   = cur->low.y;
+    }
+    *sizep = sizeof(BOX);
+
+    PG_RETURN_POINTER(pageunion);
 }
 
 /*
 ** GiST Compress methods for boxes
 ** do not do anything.
 */
-GISTENTRY *
+Datum
 gbox_compress(PG_FUNCTION_ARGS)
 {
-    return((GISTENTRY*)PG_GETARG_POINTER(0));
+       PG_RETURN_POINTER(PG_GETARG_POINTER(0));
 }
 
 /*
 ** The GiST Penalty method for boxes
 ** As in the R-tree paper, we use change in area as our penalty metric
 */
-float *
+Datum
 gbox_penalty(PG_FUNCTION_ARGS)
 {
-    return rtree_penalty(
-       (GISTENTRY*) PG_GETARG_POINTER(0),
-       (GISTENTRY*) PG_GETARG_POINTER(1),
-       (float*) PG_GETARG_POINTER(2),
-       gbox_binary_union,
-       size_box
-    );
-}
-
+    GISTENTRY *origentry = (GISTENTRY*) PG_GETARG_POINTER(0);
+    GISTENTRY *newentry  = (GISTENTRY*) PG_GETARG_POINTER(1);
+    float *result        = (float*) PG_GETARG_POINTER(2);
+    Datum      ud;
+    float tmp1;
+   
+    ud = DirectFunctionCall2(rt_box_union, origentry->key, newentry->key);
+    tmp1 = size_box( ud ); 
+    if (DatumGetPointer(ud) != NULL) pfree(DatumGetPointer(ud));
 
+    *result = tmp1 - size_box( origentry->key );
+    PG_RETURN_POINTER(result);
+}
 
 /*
-** The GiST PickSplit method for boxes
-** We use Guttman's poly time split algorithm 
+** The GiST PickSplit method
+** New linear algorithm, see 'New Linear Node Splitting Algorithm for R-tree',
+** C.H.Ang and T.C.Tan
 */
-GIST_SPLITVEC *
+Datum
 gbox_picksplit(PG_FUNCTION_ARGS)
 {
-    return rtree_picksplit(
-       (bytea*)PG_GETARG_POINTER(0),
-       (GIST_SPLITVEC*)PG_GETARG_POINTER(1),
-       sizeof(BOX),
-       gbox_binary_union,
-       rt_box_inter,
-       size_box
-    );
+    bytea *entryvec = (bytea*)PG_GETARG_POINTER(0);
+    GIST_SPLITVEC *v = (GIST_SPLITVEC*)PG_GETARG_POINTER(1);
+    OffsetNumber i;
+    OffsetNumber *listL, *listR, *listB, *listT;
+    BOX         *unionL,*unionR,*unionB,*unionT;
+    int                   posL,   posR,   posB,   posT;
+    BOX        pageunion;
+    BOX        *cur;
+    char       direction=' ';
+    bool       allisequal=true;
+    OffsetNumber maxoff;
+    int nbytes;
+
+    posL = posR = posB = posT = 0; 
+    maxoff = ((VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY)) - 1;
+   
+    cur = DatumGetBoxP( ((GISTENTRY *) VARDATA(entryvec))[FirstOffsetNumber].key );
+    memcpy( (void*)&pageunion, (void*)cur, sizeof( BOX ) ); 
+   
+    /* find MBR */ 
+    for (i = OffsetNumberNext(FirstOffsetNumber); i <= maxoff; i = OffsetNumberNext(i)) {
+       cur = DatumGetBoxP( ((GISTENTRY *) VARDATA(entryvec))[i].key );
+       if ( pageunion.high.x < cur->high.x ) 
+               { allisequal=false; pageunion.high.x  = cur->high.x; }
+       if ( pageunion.low.x  > cur->low.x  ) 
+               { allisequal=false; pageunion.low.x   = cur->low.x;  }
+       if ( pageunion.high.y < cur->high.y ) 
+               { allisequal=false; pageunion.high.y  = cur->high.y; }
+       if ( pageunion.low.y  > cur->low.y  ) 
+               { allisequal=false; pageunion.low.y   = cur->low.y;  }
+    }
+
+    nbytes =  (maxoff + 2) * sizeof(OffsetNumber);
+    listL = (OffsetNumber*)palloc( nbytes );
+    listR = (OffsetNumber*)palloc( nbytes );
+    unionL = (BOX*)palloc( sizeof(BOX) );
+    unionR = (BOX*)palloc( sizeof(BOX) );
+    if ( allisequal ) {
+       cur = DatumGetBoxP( ((GISTENTRY *) VARDATA(entryvec))[OffsetNumberNext(FirstOffsetNumber)].key );
+       if ( memcmp( (void*)cur, (void*)&pageunion, sizeof( BOX ) ) == 0 ) {  
+               v->spl_left = listL;
+               v->spl_right = listR;
+               v->spl_nleft = v->spl_nright = 0;
+               memcpy( (void*)unionL, (void*)&pageunion, sizeof( BOX ) );
+               memcpy( (void*)unionR, (void*)&pageunion, sizeof( BOX ) );
+       
+               for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
+                       if (i <= (maxoff - FirstOffsetNumber + 1)/2) {
+                                       v->spl_left[ v->spl_nleft ] = i;
+                               v->spl_nleft++;
+                       } else {
+                               v->spl_right[ v->spl_nright ] = i;
+                               v->spl_nright++;
+                       }
+               }
+               v->spl_ldatum = BoxPGetDatum( unionL );
+               v->spl_rdatum = BoxPGetDatum( unionR );
+
+               PG_RETURN_POINTER( v );
+       }
+    }
+
+    listB = (OffsetNumber*)palloc( nbytes );
+    listT = (OffsetNumber*)palloc( nbytes );
+    unionB = (BOX*)palloc( sizeof(BOX) );
+    unionT = (BOX*)palloc( sizeof(BOX) );
+
+#define ADDLIST( list, unionD, pos ) do { \
+       if ( pos ) { \
+               if ( unionD->high.x < cur->high.x ) unionD->high.x  = cur->high.x; \
+               if ( unionD->low.x  > cur->low.x  ) unionD->low.x   = cur->low.x; \
+               if ( unionD->high.y < cur->high.y ) unionD->high.y  = cur->high.y; \
+               if ( unionD->low.y  > cur->low.y  ) unionD->low.y   = cur->low.y; \
+       } else { \
+               memcpy( (void*)unionD, (void*) cur, sizeof( BOX ) );  \
+       } \
+       list[pos] = i; \
+       (pos)++; \
+} while(0)
+
+    for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
+       cur = DatumGetBoxP( ((GISTENTRY *) VARDATA(entryvec))[i].key );
+       if ( cur->low.x - pageunion.low.x < pageunion.high.x - cur->high.x )
+               ADDLIST( listL, unionL, posL );
+       else
+               ADDLIST( listR, unionR, posR );
+       if ( cur->low.y - pageunion.low.y < pageunion.high.y - cur->high.y )
+               ADDLIST( listB, unionB, posB );
+       else
+               ADDLIST( listT, unionT, posT );
+    }
+
+    /* which split more optimal? */
+
+    if ( Max( posL, posR ) < Max( posB, posT ) ) {
+       direction = 'x';
+    } else if ( Max( posL, posR ) > Max( posB, posT ) ) {
+       direction = 'y';
+    } else {
+       Datum interLR = DirectFunctionCall2(rt_box_inter,
+                               BoxPGetDatum(unionL),
+                               BoxPGetDatum(unionR));
+       Datum interBT = DirectFunctionCall2(rt_box_inter,
+                               BoxPGetDatum(unionB),
+                               BoxPGetDatum(unionT));
+       float sizeLR, sizeBT;
+
+       sizeLR = size_box( interLR );
+       sizeBT = size_box( interBT );
+
+       if ( sizeLR < sizeBT ) {
+               direction = 'x';
+       //} else if ( sizeLR > sizeBT ) {
+       } else {
+               direction = 'y';
+       } 
+    }
+
+   if ( direction == 'x' ) {
+       pfree( unionB ); pfree( listB );
+       pfree( unionT ); pfree( listT );
+
+       v->spl_left = listL;
+       v->spl_right = listR;
+       v->spl_nleft = posL;
+       v->spl_nright = posR;
+       v->spl_ldatum = BoxPGetDatum( unionL );
+       v->spl_rdatum = BoxPGetDatum( unionR );
+   } else {
+       pfree( unionR ); pfree( listR );
+       pfree( unionL ); pfree( listL );
+
+       v->spl_left = listB;
+       v->spl_right = listT;
+       v->spl_nleft = posB;
+       v->spl_nright = posT;
+       v->spl_ldatum = BoxPGetDatum( unionB );
+       v->spl_rdatum = BoxPGetDatum( unionT );
+   }
+
+   PG_RETURN_POINTER (v);
 }
 
 /*
 ** Equality method
 */
-bool *
+Datum
 gbox_same(PG_FUNCTION_ARGS)
 {
   BOX *b1 = (BOX*) PG_GETARG_POINTER(0);
@@ -192,7 +321,7 @@ gbox_same(PG_FUNCTION_ARGS)
        *result = DatumGetBool( DirectFunctionCall2( box_same, PointerGetDatum(b1), PointerGetDatum(b2)) );
   else
        *result = ( b1==NULL && b2==NULL ) ? TRUE : FALSE; 
-  return(result);
+  PG_RETURN_POINTER(result);
 }
 
 /* 
@@ -236,32 +365,6 @@ gbox_leaf_consistent(BOX *key,
     return(retval);
 }
 
-static Datum
-gbox_binary_union(Datum r1, Datum r2, int *sizep)
-{
-    BOX *retval;
-
-    if ( ! (DatumGetPointer(r1) != NULL && DatumGetPointer(r2) != NULL) ) {
-               if ( DatumGetPointer(r1) != NULL ) {
-                       retval = (BOX*) palloc( sizeof(BOX) );
-                       memcpy( retval, DatumGetPointer(r1), sizeof(BOX) );
-               *sizep = sizeof(BOX);
-               } else if ( DatumGetPointer(r2) != NULL ) {
-                       retval = (BOX*) palloc( sizeof(BOX) );
-                       memcpy( retval, DatumGetPointer(r2), sizeof(BOX) );
-               *sizep = sizeof(BOX);
-               } else {
-                       *sizep = 0;
-                       retval = NULL;
-               } 
-    } else {
-       retval = (BOX*) DatumGetPointer(
-                       DirectFunctionCall2(rt_box_union, r1, r2));
-       *sizep = sizeof(BOX);
-    }
-    return PointerGetDatum(retval);
-}
-
 static float 
 size_box( Datum box ) {
     if ( DatumGetPointer(box) != NULL ) {
@@ -278,7 +381,7 @@ size_box( Datum box ) {
  * Polygon ops
  **************************************************/
 
-GISTENTRY *
+Datum
 gpoly_compress(PG_FUNCTION_ARGS)
 {
        GISTENTRY *entry=(GISTENTRY*)PG_GETARG_POINTER(0);
@@ -288,17 +391,16 @@ gpoly_compress(PG_FUNCTION_ARGS)
                retval = palloc(sizeof(GISTENTRY));
                if ( DatumGetPointer(entry->key) != NULL ) {
                        POLYGON *in;
-                       POLYKEY *r;
+                       BOX *r;
                        in = (POLYGON *) PG_DETOAST_DATUM(entry->key);
-                       r = (POLYKEY *) palloc( sizeof(POLYKEY) );
-                       r->size = sizeof(POLYKEY);
-                       memcpy( (void*)&(r->key), (void*)&(in->boundbox), sizeof(BOX) );
+                       r = (BOX *) palloc( sizeof(BOX) );
+                       memcpy( (void*)r, (void*)&(in->boundbox), sizeof(BOX) );
                        if ( in != (POLYGON *) DatumGetPointer(entry->key) )
                                pfree( in );
 
                        gistentryinit(*retval, PointerGetDatum(r),
                                                  entry->rel, entry->page,
-                                                 entry->offset, sizeof(POLYKEY), FALSE);
+                                                 entry->offset, sizeof(BOX), FALSE);
 
                } else {
                        gistentryinit(*retval, (Datum) 0,
@@ -308,355 +410,34 @@ gpoly_compress(PG_FUNCTION_ARGS)
        } else {
                retval = entry;
        }
-       return( retval );
+       PG_RETURN_POINTER( retval );
 }
 
-bool 
+Datum
 gpoly_consistent(PG_FUNCTION_ARGS)
 {
     GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
     POLYGON *query       = (POLYGON*)PG_DETOAST_DATUM( PG_GETARG_POINTER(1) );
     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     bool result;
+
     /*
     ** if entry is not leaf, use gbox_internal_consistent,
     ** else use gbox_leaf_consistent
     */
     if ( ! (DatumGetPointer(entry->key) != NULL && query) )
-       return FALSE;
+               PG_RETURN_BOOL(FALSE);
 
-    result = rtree_internal_consistent((BOX*)&( ((POLYKEY *) DatumGetPointer(entry->key))->key ), 
+    result = rtree_internal_consistent((BOX *) DatumGetPointer(entry->key), 
                &(query->boundbox), strategy);
 
     PG_FREE_IF_COPY(query,1);
     PG_RETURN_BOOL( result ); 
 }
 
-POLYKEY *
-gpoly_union(PG_FUNCTION_ARGS)
-{
-    return (POLYKEY*) 
-               DatumGetPointer(rtree_union(
-                       (bytea*) PG_GETARG_POINTER(0),
-                       (int*) PG_GETARG_POINTER(1),
-                       gpoly_binary_union
-                       ));
-}
-
-float *
-gpoly_penalty(PG_FUNCTION_ARGS)
-{
-    return rtree_penalty(
-       (GISTENTRY*) PG_GETARG_POINTER(0),
-       (GISTENTRY*) PG_GETARG_POINTER(1),
-       (float*) PG_GETARG_POINTER(2),
-       gpoly_binary_union,
-       size_polykey
-    );
-}
-
-GIST_SPLITVEC *
-gpoly_picksplit(PG_FUNCTION_ARGS)
-{
-    return rtree_picksplit(
-       (bytea*)PG_GETARG_POINTER(0),
-       (GIST_SPLITVEC*)PG_GETARG_POINTER(1),
-       sizeof(POLYKEY),
-       gpoly_binary_union,
-       gpoly_inter,
-       size_polykey
-    );
-}
-
-bool *
-gpoly_same(PG_FUNCTION_ARGS)
-{
-  POLYKEY *b1 = (POLYKEY*) PG_GETARG_POINTER(0);
-  POLYKEY *b2 = (POLYKEY*) PG_GETARG_POINTER(1);
-
-  bool *result = (bool*) PG_GETARG_POINTER(2);
-  if ( b1 && b2 )
-       *result = DatumGetBool( DirectFunctionCall2( box_same, 
-               PointerGetDatum(&(b1->key)), 
-               PointerGetDatum(&(b2->key))) );
-  else
-       *result = ( b1==NULL && b2==NULL ) ? TRUE : FALSE; 
-  return(result);
-}
-
-/* 
-** SUPPORT ROUTINES for polygons
-*/
-Datum 
-gpoly_inter(PG_FUNCTION_ARGS)
-{
-       POLYKEY *b1 = (POLYKEY*) PG_GETARG_POINTER(0);
-       POLYKEY *b2 = (POLYKEY*) PG_GETARG_POINTER(1);
-       Datum interd;
-
-       interd = DirectFunctionCall2(rt_box_inter,
-                                                                PointerGetDatum( &(b1->key) ),
-                                                                PointerGetDatum( &(b2->key) ));
-       
-       if (DatumGetPointer(interd) != NULL) {
-               POLYKEY *tmp = (POLYKEY*) palloc( sizeof(POLYKEY) );
-               tmp->size = sizeof(POLYKEY);
-               memcpy( &(tmp->key), DatumGetPointer(interd), sizeof(BOX) );
-               pfree( DatumGetPointer(interd) );
-               PG_RETURN_POINTER( tmp );
-       } else 
-               PG_RETURN_POINTER( NULL );
-}
-
-static Datum
-gpoly_binary_union(Datum r1, Datum r2, int *sizep)
-{
-    POLYKEY *retval;
-
-    if ( ! (DatumGetPointer(r1) != NULL && DatumGetPointer(r2) != NULL) ) {
-               if ( DatumGetPointer(r1) != NULL ) {
-                       retval = (POLYKEY*)palloc( sizeof(POLYKEY) );
-                       memcpy( (void*)retval, DatumGetPointer(r1), sizeof(POLYKEY) );
-               *sizep = sizeof(POLYKEY);
-               } else if ( DatumGetPointer(r2) != NULL ) {
-                       retval = (POLYKEY*)palloc( sizeof(POLYKEY) );
-                       memcpy( (void*)retval, DatumGetPointer(r2), sizeof(POLYKEY) );
-               *sizep = sizeof(POLYKEY);
-               } else {
-                       *sizep = 0;
-                       retval = NULL;
-               } 
-    } else {
-       BOX *key = (BOX*)DatumGetPointer(
-                       DirectFunctionCall2(
-                       rt_box_union,
-                       PointerGetDatum( &(((POLYKEY*) DatumGetPointer(r1))->key) ),
-                       PointerGetDatum( &(((POLYKEY*) DatumGetPointer(r2))->key) )) );
-               retval = (POLYKEY*)palloc( sizeof(POLYKEY) );
-               memcpy( &(retval->key), key, sizeof(BOX) );
-               pfree( key );
-       *sizep = retval->size = sizeof(POLYKEY);
-    }
-    return PointerGetDatum(retval);
-}
-
-
-static float 
-size_polykey( Datum pk ) {
-    if ( DatumGetPointer(pk) != NULL ) {
-               float size;
-
-       DirectFunctionCall2( rt_box_size,
-                                                        PointerGetDatum( &(((POLYKEY*) DatumGetPointer(pk))->key) ),
-                                                        PointerGetDatum( &size ) );
-               return size;
-    } else
-               return 0.0;
-}
-
-
-/*
-** Common rtree-function (for all ops)
-*/
-
-static Datum
-rtree_union(bytea *entryvec, int *sizep, BINARY_UNION bu)
-{
-    int numranges, i;
-    Datum      out,
-                       tmp;
-
-    numranges = (VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY); 
-    tmp = ((GISTENTRY *) VARDATA(entryvec))[0].key;
-    out = (Datum) 0;
-
-    for (i = 1; i < numranges; i++) {
-               out = (*bu)(tmp,
-                                       ((GISTENTRY *) VARDATA(entryvec))[i].key,
-                                       sizep);
-               if (i > 1 && DatumGetPointer(tmp) != NULL)
-                       pfree(DatumGetPointer(tmp));
-               tmp = out;
-    }
-
-    return(out);
-}
-
-static float *
-rtree_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result, BINARY_UNION bu, SIZE_BOX sb)
-{
-    Datum      ud;
-    float tmp1;
-    int sizep;
-   
-    ud = (*bu)( origentry->key, newentry->key, &sizep );
-    tmp1 = (*sb)( ud ); 
-    if (DatumGetPointer(ud) != NULL) pfree(DatumGetPointer(ud));
-
-    *result = tmp1 - (*sb)( origentry->key );
-    return(result);
-}
-
-/*
-** The GiST PickSplit method
-** We use Guttman's poly time split algorithm 
-*/
-static GIST_SPLITVEC *
-rtree_picksplit(bytea *entryvec, GIST_SPLITVEC *v, int keylen, BINARY_UNION bu, RDF interop, SIZE_BOX sb)
-{
-    OffsetNumber i, j;
-    Datum datum_alpha, datum_beta;
-    Datum datum_l, datum_r;
-    Datum union_d, union_dl, union_dr;
-    Datum inter_d;
-    bool firsttime;
-    float size_alpha, size_beta, size_union, size_inter;
-    float size_waste, waste;
-    float size_l, size_r;
-    int nbytes;
-    int sizep;
-    OffsetNumber seed_1 = 0, seed_2 = 0;
-    OffsetNumber *left, *right;
-    OffsetNumber maxoff;
-
-    maxoff = ((VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY)) - 2;
-    nbytes =  (maxoff + 2) * sizeof(OffsetNumber);
-    v->spl_left = (OffsetNumber *) palloc(nbytes);
-    v->spl_right = (OffsetNumber *) palloc(nbytes);
-    
-    firsttime = true;
-    waste = 0.0;
-    
-    for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i)) {
-       datum_alpha = ((GISTENTRY *) VARDATA(entryvec))[i].key;
-       for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j)) {
-           datum_beta = ((GISTENTRY *) VARDATA(entryvec))[j].key;
-           
-           /* compute the wasted space by unioning these guys */
-           /* size_waste = size_union - size_inter; */ 
-           union_d = (*bu)( datum_alpha, datum_beta, &sizep );
-           if ( DatumGetPointer(union_d) != NULL ) {
-                       size_union = (*sb)(union_d);
-                       pfree(DatumGetPointer(union_d));
-           } else
-                       size_union = 0.0;
-
-           if ( DatumGetPointer(datum_alpha) != NULL &&
-                        DatumGetPointer(datum_beta) != NULL ) {
-                       inter_d = DirectFunctionCall2(interop,
-                                                                                 datum_alpha,
-                                                                                 datum_beta);
-                       if ( DatumGetPointer(inter_d) != NULL ) {
-                               size_inter = (*sb)(inter_d);
-                               pfree(DatumGetPointer(inter_d));
-                       } else 
-                               size_inter = 0.0;
-           } else
-                       size_inter = 0.0;
-
-           size_waste = size_union - size_inter;
-           
-           /*
-            *  are these a more promising split that what we've
-            *  already seen?
-            */
-           
-           if (size_waste > waste || firsttime) {
-               waste = size_waste;
-               seed_1 = i;
-               seed_2 = j;
-               firsttime = false;
-           }
-       }
-    }
-    
-    left = v->spl_left;
-    v->spl_nleft = 0;
-    right = v->spl_right;
-    v->spl_nright = 0;
-  
-    if ( DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_1].key) != NULL )
-       {
-               datum_l = PointerGetDatum(palloc( keylen ));
-               memcpy(DatumGetPointer(datum_l),
-                          DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_1].key),
-                          keylen);
-    } else 
-               datum_l = (Datum) 0;
-    size_l  = (*sb)( datum_l ); 
-    if ( DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_2].key) != NULL )
-       {
-               datum_r = PointerGetDatum(palloc( keylen ));
-               memcpy(DatumGetPointer(datum_r),
-                          DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_2].key),
-                          keylen);
-    } else 
-               datum_r = (Datum) 0;
-    size_r  = (*sb)( datum_r ); 
-    
-    /*
-     *  Now split up the regions between the two seeds.  An important
-     *  property of this split algorithm is that the split vector v
-     *  has the indices of items to be split in order in its left and
-     *  right vectors.  We exploit this property by doing a merge in
-     *  the code that actually splits the page.
-     *
-     *  For efficiency, we also place the new index tuple in this loop.
-     *  This is handled at the very end, when we have placed all the
-     *  existing tuples and i == maxoff + 1.
-     */
-    
-    maxoff = OffsetNumberNext(maxoff);
-    for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
-       
-       /*
-        *  If we've already decided where to place this item, just
-        *  put it on the right list.  Otherwise, we need to figure
-        *  out which page needs the least enlargement in order to
-        *  store the item.
-        */
-       
-       if (i == seed_1) {
-           *left++ = i;
-           v->spl_nleft++;
-           continue;
-       } else if (i == seed_2) {
-           *right++ = i;
-           v->spl_nright++;
-           continue;
-       }
-       
-       /* okay, which page needs least enlargement? */ 
-       datum_alpha = ((GISTENTRY *) VARDATA(entryvec))[i].key;
-       union_dl = (*bu)( datum_l, datum_alpha, &sizep );
-       union_dr = (*bu)( datum_r, datum_alpha, &sizep );
-       size_alpha = (*sb)( union_dl ); 
-       size_beta  = (*sb)( union_dr ); 
-       
-       /* pick which page to add it to */
-       if (size_alpha - size_l < size_beta - size_r) {
-           pfree(DatumGetPointer(datum_l));
-           pfree(DatumGetPointer(union_dr));
-           datum_l = union_dl;
-           size_l = size_alpha;
-           *left++ = i;
-           v->spl_nleft++;
-       } else {
-           pfree(DatumGetPointer(datum_r));
-           pfree(DatumGetPointer(union_dl));
-           datum_r = union_dr;
-           size_r = size_alpha;
-           *right++ = i;
-           v->spl_nright++;
-       }
-    }
-    *left = *right = FirstOffsetNumber;        /* sentinel value, see dosplit() */
-    
-    v->spl_ldatum = datum_l;
-    v->spl_rdatum = datum_r;
-
-    return( v );
-}
+/*****************************************
+ * Common rtree-function (for all ops)
+ *****************************************/
 
 static bool 
 rtree_internal_consistent(BOX *key,
@@ -694,8 +475,8 @@ rtree_internal_consistent(BOX *key,
 ** GiST DeCompress methods
 ** do not do anything.
 */
-GISTENTRY *
+Datum
 rtree_decompress(PG_FUNCTION_ARGS)
 {
-    return((GISTENTRY*)PG_GETARG_POINTER(0));
+       PG_RETURN_POINTER(PG_GETARG_POINTER(0));
 }
index c030fe471bb3a4b58d8252ec990ba54925201437..df9bb823f09cce239bb7c55d1aae470f5d87916a 100644 (file)
@@ -27,7 +27,7 @@ INSERT INTO pg_opclass (opcamid, opcname, opcintype, opcdefault, opckeytype)
         (SELECT oid FROM pg_am WHERE amname = 'gist'),
         'gist_box_ops',
         (SELECT oid FROM pg_type WHERE typname = 'box'),
-        false,
+        true,
         0);
 
 -- get the comparators for boxes and store them in a tmp table
@@ -183,22 +183,14 @@ create function gpoly_consistent(opaque,polygon,int4) returns bool as 'MODULE_PA
 
 create function gpoly_compress(opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
 
-create function gpoly_penalty(opaque,opaque,opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
-
-create function gpoly_picksplit(opaque, opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
-
-create function gpoly_union(bytea, opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
-
-create function gpoly_same(opaque, opaque, opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
-
 -- add a new opclass (non-default)
 INSERT INTO pg_opclass (opcamid, opcname, opcintype, opcdefault, opckeytype)
     VALUES (
         (SELECT oid FROM pg_am WHERE amname = 'gist'),
         'gist_poly_ops',
         (SELECT oid FROM pg_type WHERE typname = 'polygon'),
-        false,
-        0);
+        true,
+        (SELECT oid FROM pg_type WHERE typname = 'box'));
 
 -- get the comparators for polygons and store them in a tmp table
 -- hack for 757 (poly_contain_pt) Teodor
@@ -211,7 +203,7 @@ WHERE o.oprleft = t.oid and o.oid <> 757
 -- using the tmp table, generate the amop entries 
 -- poly_left
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr) 
-   SELECT opcl.oid, 1, false, c.opoid
+   SELECT opcl.oid, 1, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -220,7 +212,7 @@ INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
 
 -- poly_overleft
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr) 
-   SELECT opcl.oid, 2, false, c.opoid
+   SELECT opcl.oid, 2, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -229,7 +221,7 @@ INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
 
 -- poly_overlap
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr) 
-   SELECT opcl.oid, 3, false, c.opoid
+   SELECT opcl.oid, 3, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -238,7 +230,7 @@ INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
 
 -- poly_overright
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr) 
-   SELECT opcl.oid, 4, false, c.opoid
+   SELECT opcl.oid, 4, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -247,7 +239,7 @@ INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
 
 -- poly_right
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
-   SELECT opcl.oid, 5, false, c.opoid
+   SELECT opcl.oid, 5, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -256,7 +248,7 @@ INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
 
 -- poly_same
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr) 
-   SELECT opcl.oid, 6, false, c.opoid
+   SELECT opcl.oid, 6, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -265,7 +257,7 @@ INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
 
 -- poly_contains
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
-   SELECT opcl.oid, 7, false, c.opoid
+   SELECT opcl.oid, 7, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -274,7 +266,7 @@ INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
 
 -- poly_contained
 INSERT INTO pg_amop (amopclaid, amopstrategy, amopreqcheck, amopopr)
-   SELECT opcl.oid, 8, false, c.opoid
+   SELECT opcl.oid, 8, true, c.opoid
    FROM pg_opclass opcl, rt_ops_tmp c
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
@@ -300,7 +292,7 @@ INSERT INTO pg_amproc (amopclaid, amprocnum, amproc)
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
       and opcname = 'gist_poly_ops'
-      and proname = 'gpoly_union';
+      and proname = 'gbox_union';
 
 INSERT INTO pg_amproc (amopclaid, amprocnum, amproc)
    SELECT opcl.oid, 3, pro.oid
@@ -324,7 +316,7 @@ INSERT INTO pg_amproc (amopclaid, amprocnum, amproc)
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
       and opcname = 'gist_poly_ops'
-      and proname = 'gpoly_penalty';
+      and proname = 'gbox_penalty';
 
 INSERT INTO pg_amproc (amopclaid, amprocnum, amproc)
    SELECT opcl.oid, 6, pro.oid
@@ -332,7 +324,7 @@ INSERT INTO pg_amproc (amopclaid, amprocnum, amproc)
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
       and opcname = 'gist_poly_ops'
-      and proname = 'gpoly_picksplit';
+      and proname = 'gbox_picksplit';
 
 INSERT INTO pg_amproc (amopclaid, amprocnum, amproc)
    SELECT opcl.oid, 7, pro.oid
@@ -340,7 +332,7 @@ INSERT INTO pg_amproc (amopclaid, amprocnum, amproc)
    WHERE
       opcamid = (SELECT oid FROM pg_am WHERE amname = 'gist')
       and opcname = 'gist_poly_ops'
-      and proname = 'gpoly_same';
+      and proname = 'gbox_same';
 
 end transaction;
 
index 592d4c3e78bc6fbae4279d59f2b8410acc7d093f..7a2a7faf85173ad7914c9ce79e09442ab746a3db 100644 (file)
@@ -18,7 +18,7 @@ select count(*) from boxtmp where b && '(1000,1000,0,0)'::box;
 
 drop index bix;
 
-create index bix on boxtmp using gist (b gist_box_ops);
+create index bix on boxtmp using gist (b);
 
 select count(*) from boxtmp where b && '(1000,1000,0,0)'::box;
 
@@ -32,7 +32,7 @@ select count(*) from polytmp where p && '(1000,1000),(0,0)'::polygon;
 
 drop index pix;
 
-create index pix on polytmp using gist (p gist_poly_ops);
+create index pix on polytmp using gist (p);
 
 select count(*) from polytmp where p && '(1000,1000),(0,0)'::polygon;
 
index 779a0aa8757217c75bffb5783237c5e2af182975..e05e07b6366522d8daf2af00e9445b63723db4be 100644 (file)
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/access/gist/gist.c,v 1.82 2001/08/21 16:35:59 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/access/gist/gist.c,v 1.83 2001/08/22 18:24:26 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 #include "access/gistscan.h"
 #include "access/heapam.h"
 #include "catalog/index.h"
-#include "catalog/pg_index.h"
-#include "catalog/pg_opclass.h"
-#include "executor/executor.h"
 #include "miscadmin.h"
-#include "utils/lsyscache.h"
-#include "utils/syscache.h"
-#include "access/xlogutils.h"
 
 
 #undef GIST_PAGEADDITEM
 
-#define ATTSIZE( datum, Rel, i, isnull ) \
+#define ATTSIZE( datum, TupDesc, i, isnull ) \
        ( \
                ( isnull ) ? 0 : \
-                       att_addlength(0, (Rel)->rd_att->attrs[(i)-1]->attlen, (datum)) \
+                       att_addlength(0, (TupDesc)->attrs[(i)-1]->attlen, (datum)) \
        )
 
 /* result's status */
@@ -214,6 +208,7 @@ gistbuild(PG_FUNCTION_ARGS)
                UpdateStats(irelid, buildstate.indtuples);
        }
 
+       freeGISTstate( &buildstate.giststate );
 #ifdef GISTDEBUG
        gist_dumptree(index, 0, GISTP_ROOT, 0);
 #endif
@@ -253,7 +248,7 @@ gistbuildCallback(Relation index,
                                           (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
                                           -1 /* size is currently bogus */ , TRUE, FALSE);
                        if (attdata[i] != tmpcentry.key &&
-                               !(buildstate->giststate.attbyval[i]))
+                               !( isAttByVal(&buildstate->giststate, i)))
                                compvec[i] = TRUE;
                        else
                                compvec[i] = FALSE;
@@ -262,7 +257,7 @@ gistbuildCallback(Relation index,
        }
 
        /* form an index tuple and point it at the heap tuple */
-       itup = index_formtuple(RelationGetDescr(index), attdata, nulls);
+       itup = index_formtuple(buildstate->giststate.tupdesc, attdata, nulls);
        itup->t_tid = htup->t_self;
 
        /*
@@ -330,14 +325,14 @@ gistinsert(PG_FUNCTION_ARGS)
                        gistcentryinit(&giststate, i, &tmpentry, datum[i],
                                           (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
                                           -1 /* size is currently bogus */ , TRUE, FALSE );
-                       if (datum[i] != tmpentry.key && !(giststate.attbyval[i]))
+                       if (datum[i] != tmpentry.key && !( isAttByVal( &giststate, i)))
                                compvec[i] = TRUE;
                        else
                                compvec[i] = FALSE;
                        datum[i] = tmpentry.key;
                }
        }
-       itup = index_formtuple(RelationGetDescr(r), datum, nulls);
+       itup = index_formtuple(giststate.tupdesc, datum, nulls);
        itup->t_tid = *ht_ctid;
 
        res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
@@ -347,6 +342,7 @@ gistinsert(PG_FUNCTION_ARGS)
                if (compvec[i] == TRUE)
                        pfree(DatumGetPointer(datum[i]));
        pfree(itup);
+       freeGISTstate( &giststate );
 
        PG_RETURN_POINTER(res);
 }
@@ -663,15 +659,15 @@ gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate) {
        for (j = 0; j < r->rd_att->natts; j++) {
                reallen=0; 
                for (i = 0; i < len; i++) {
-                       datum = index_getattr(itvec[i], j+1, r->rd_att, &IsNull);
+                       datum = index_getattr(itvec[i], j+1, giststate->tupdesc, &IsNull);
                        if ( IsNull ) continue;
 
                        gistdentryinit(giststate, j,
                                                   &((GISTENTRY *) VARDATA(evec))[reallen],
                                                   datum,
                                                   (Relation) NULL, (Page) NULL, (OffsetNumber) NULL,
-                                                  ATTSIZE( datum, r, j+1, IsNull ), FALSE, IsNull);
-                       if ( (!giststate->attbyval[j]) &&  
+                                                  ATTSIZE( datum, giststate->tupdesc, j+1, IsNull ), FALSE, IsNull);
+                       if ( (!isAttByVal(giststate,j)) &&  
                                  ((GISTENTRY *) VARDATA(evec))[reallen].key != datum ) 
                                needfree[reallen] = TRUE;
                        else
@@ -706,7 +702,7 @@ gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate) {
                                datumsize, FALSE, FALSE);
                        isnull[j] = ' ';
                        attr[j] =  centry[j].key;
-                       if ( !giststate->attbyval[j] ) {
+                       if ( !isAttByVal( giststate, j ) ) {
                                whatfree[j] = TRUE;
                                if ( centry[j].key != datum )
                                        pfree(DatumGetPointer(datum));
@@ -718,7 +714,7 @@ gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate) {
        pfree(evec);
        pfree(needfree);
 
-       newtup = (IndexTuple) index_formtuple(r->rd_att, attr, isnull);
+       newtup = (IndexTuple) index_formtuple(giststate->tupdesc, attr, isnull);
        for (j = 0; j < r->rd_att->natts; j++)
                if ( whatfree[j] )
                        pfree(DatumGetPointer(attr[j]));
@@ -802,7 +798,7 @@ gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *gis
        
                        isnull[j] = ' ';
                        attr[j] =  centry[j].key;
-                       if ( (!giststate->attbyval[j]) ) {
+                       if ( (!isAttByVal( giststate, j ) ) ) {
                                whatfree[j] = TRUE;
                                if ( centry[j].key != datum )
                                        pfree(DatumGetPointer(datum));
@@ -814,7 +810,7 @@ gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *gis
 
        if (neednew) {
                /* need to update key */
-               newtup = (IndexTuple) index_formtuple(r->rd_att, attr, isnull);
+               newtup = (IndexTuple) index_formtuple(giststate->tupdesc, attr, isnull);
                newtup->t_tid = oldtup->t_tid;
        }
        
@@ -861,7 +857,7 @@ gistunionsubkey( Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLIT
                                if ( spl->spl_idgrp[ entries[i] ] )
                                        continue;
                                datum = index_getattr(itvec[ entries[i]-1 ], j+1,
-                                                                         r->rd_att, &IsNull);
+                                                                         giststate->tupdesc, &IsNull);
                                if ( IsNull )
                                        continue;
                                gistdentryinit(giststate, j,
@@ -869,8 +865,8 @@ gistunionsubkey( Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLIT
                                                           datum,
                                                           (Relation) NULL, (Page) NULL,
                                                           (OffsetNumber) NULL,
-                                                          ATTSIZE( datum, r, j+1, IsNull ), FALSE, IsNull);
-                               if ( (!giststate->attbyval[j]) &&
+                                                          ATTSIZE( datum, giststate->tupdesc, j+1, IsNull ), FALSE, IsNull);
+                               if ( (!isAttByVal( giststate, j )) &&
                                                ((GISTENTRY *) VARDATA(evec))[reallen].key != datum )
                                        needfree[reallen] = TRUE;
                                else
@@ -1064,7 +1060,7 @@ gistadjsubkey(Relation r,
                                                                          PointerGetDatum(evec),
                                                                          PointerGetDatum(&datumsize));
 
-                                       if ( (!giststate->attbyval[j]) && !v->spl_lisnull[j] )
+                                       if ( (!isAttByVal( giststate, j )) && !v->spl_lisnull[j] )
                                                pfree( DatumGetPointer(v->spl_lattr[j]) );
                                        v->spl_lattr[j] = datum;
                                        v->spl_lattrsize[j] = datumsize;
@@ -1089,7 +1085,7 @@ gistadjsubkey(Relation r,
                                                                          PointerGetDatum(evec),
                                                                          PointerGetDatum(&datumsize));
 
-                                       if ( (!giststate->attbyval[j]) && !v->spl_risnull[j] )
+                                       if ( (!isAttByVal( giststate, j)) && !v->spl_risnull[j] )
                                                pfree( DatumGetPointer(v->spl_rattr[j]) );
 
                                        v->spl_rattr[j] = datum;
@@ -1171,11 +1167,11 @@ gistSplit(Relation r,
        VARATT_SIZEP(entryvec) = (*len + 1) * sizeof(GISTENTRY) + VARHDRSZ;
        for (i = 1; i <= *len; i++)
        {
-               datum = index_getattr(itup[i - 1], 1, r->rd_att, &IsNull);
+               datum = index_getattr(itup[i - 1], 1, giststate->tupdesc, &IsNull);
                gistdentryinit(giststate, 0,&((GISTENTRY *) VARDATA(entryvec))[i],
                                        datum, r, p, i,
-                                       ATTSIZE( datum, r, 1, IsNull ), FALSE, IsNull);
-               if ( (!giststate->attbyval[0]) && ((GISTENTRY *) VARDATA(entryvec))[i].key != datum )  
+                                       ATTSIZE( datum, giststate->tupdesc, 1, IsNull ), FALSE, IsNull);
+               if ( (!isAttByVal(giststate,0)) && ((GISTENTRY *) VARDATA(entryvec))[i].key != datum )  
                        decompvec[i] = TRUE;
                else
                        decompvec[i] = FALSE;
@@ -1250,7 +1246,7 @@ gistSplit(Relation r,
                          (res && rvectup[nlen - 1] == itup[*len - 1]) ? res : NULL);
                ReleaseBuffer(rightbuf);
                for( j=1; j<r->rd_att->natts; j++ ) 
-                       if ( (!giststate->attbyval[j]) && !v.spl_risnull[j] )
+                       if ( (!isAttByVal(giststate,j)) && !v.spl_risnull[j] )
                                pfree( DatumGetPointer(v.spl_rattr[j]) ); 
        }
        else
@@ -1280,7 +1276,7 @@ gistSplit(Relation r,
                ReleaseBuffer(leftbuf);
 
                for( j=1; j<r->rd_att->natts; j++ ) 
-                       if ( (!giststate->attbyval[j]) && !v.spl_lisnull[j] )
+                       if ( (!isAttByVal(giststate,j)) && !v.spl_lisnull[j] )
                                pfree( DatumGetPointer(v.spl_lattr[j]) ); 
 
                newtup = gistjoinvector(newtup, &nlen, lntup, llen);
@@ -1382,11 +1378,11 @@ gistchoose(Relation r, Page p, IndexTuple it,   /* it has compressed entry */
                IndexTuple itup = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
                sum_grow=0;
                for (j=0; j<r->rd_att->natts; j++) {
-                       datum = index_getattr(itup, j+1, r->rd_att, &IsNull);
-                       gistdentryinit(giststate, j, &entry, datum, r, p, i, ATTSIZE( datum, r, j+1, IsNull ), FALSE, IsNull);
+                       datum = index_getattr(itup, j+1, giststate->tupdesc, &IsNull);
+                       gistdentryinit(giststate, j, &entry, datum, r, p, i, ATTSIZE( datum, giststate->tupdesc, j+1, IsNull ), FALSE, IsNull);
                        gistpenalty( giststate, j, &entry, IsNull, &identry[j], isnull[j], &usize); 
 
-                       if ( (!giststate->attbyval[j]) && entry.key != datum) 
+                       if ( (!isAttByVal(giststate,j)) && entry.key != datum) 
                                pfree(DatumGetPointer(entry.key));
 
                        if ( which_grow[j]<0 || usize < which_grow[j] ) {
@@ -1553,25 +1549,13 @@ initGISTstate(GISTSTATE *giststate, Relation index)
                                penalty_proc,
                                picksplit_proc,
                                equal_proc;
-       HeapTuple       itup;
-       HeapTuple       ctup;
-       Form_pg_index itupform;
-       Form_pg_opclass opclassform;
-       Oid                     inputtype;
-       Oid                     keytype;
        int i;
 
        if (index->rd_att->natts > INDEX_MAX_KEYS)
                elog(ERROR, "initGISTstate: numberOfAttributes %d > %d",
                         index->rd_att->natts, INDEX_MAX_KEYS);
 
-       itup = SearchSysCache(INDEXRELID,
-                                                 ObjectIdGetDatum(RelationGetRelid(index)),
-                                                 0, 0, 0);
-       if (!HeapTupleIsValid(itup))
-               elog(ERROR, "initGISTstate: index %u not found",
-                        RelationGetRelid(index));
-       itupform = (Form_pg_index) GETSTRUCT(itup);
+       giststate->tupdesc = index->rd_att;
 
        for (i = 0; i < index->rd_att->natts; i++)
        {
@@ -1589,36 +1573,12 @@ initGISTstate(GISTSTATE *giststate, Relation index)
                fmgr_info(penalty_proc,         &((giststate->penaltyFn)[i])            );
                fmgr_info(picksplit_proc,       &((giststate->picksplitFn)[i])          );
                fmgr_info(equal_proc,           &((giststate->equalFn)[i])              );
-
-               /* Check opclass entry to see if there is a keytype */
-               ctup = SearchSysCache(CLAOID,
-                                                         ObjectIdGetDatum(itupform->indclass[i]),
-                                                         0, 0, 0);
-               if (!HeapTupleIsValid(ctup))
-                       elog(ERROR, "cache lookup failed for opclass %u",
-                                itupform->indclass[i]);
-               opclassform = (Form_pg_opclass) GETSTRUCT(ctup);
-               inputtype = opclassform->opcintype;
-               keytype = opclassform->opckeytype;
-               ReleaseSysCache(ctup);
-
-               if (OidIsValid(keytype))
-               {
-                       /* index column type is (possibly) different from input data */
-                       giststate->haskeytype[i] = true;
-                       giststate->attbyval[i] = get_typbyval(inputtype);
-                       giststate->keytypbyval[i] = index->rd_att->attrs[i]->attbyval;
-               }
-               else
-               {
-                       /* Normal case where index column type is same as input data */
-                       giststate->haskeytype[i] = false;
-                       giststate->attbyval[i] = index->rd_att->attrs[i]->attbyval;
-                       giststate->keytypbyval[i] = false;      /* not actually used */
-               }
        }
+}
 
-       ReleaseSysCache(itup);
+void
+freeGISTstate(GISTSTATE *giststate) {
+       /* no work */
 }
 
 #ifdef GIST_PAGEADDITEM
@@ -1678,23 +1638,26 @@ gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e,
                           Datum k, Relation r, Page pg, OffsetNumber o,
                           int b, bool l, bool isNull)
 {
-       GISTENTRY  *dep;
-
-       gistentryinit(*e, k, r, pg, o, b, l);
-       if (giststate->haskeytype[nkey])
+       if ( b && ! isNull )
        {
-               if ( b && ! isNull ) {
-                       dep = (GISTENTRY *)
-                               DatumGetPointer(FunctionCall1(&giststate->decompressFn[nkey],
+               GISTENTRY  *dep;
+
+               gistentryinit(*e, k, r, pg, o, b, l);
+               dep = (GISTENTRY *)
+                       DatumGetPointer(FunctionCall1(&giststate->decompressFn[nkey],
                                                                                  PointerGetDatum(e)));
-                       gistentryinit(*e, dep->key, dep->rel, dep->page, dep->offset, dep->bytes,
-                                         dep->leafkey);
-                       if (dep != e)
-                               pfree(dep);
-               } else {
-                       gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
+               /* decompressFn may just return the given pointer */
+               if (dep != e)
+               {
+                       gistentryinit(*e, dep->key, dep->rel, dep->page, dep->offset,
+                                                 dep->bytes, dep->leafkey);
+                       pfree(dep);
                }
        }
+       else
+       {
+               gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
+       }
 }
 
 
@@ -1706,23 +1669,26 @@ gistcentryinit(GISTSTATE *giststate, int nkey,
                           GISTENTRY *e, Datum k, Relation r,
                           Page pg, OffsetNumber o, int b, bool l, bool isNull)
 {
-       GISTENTRY  *cep;
-
-       gistentryinit(*e, k, r, pg, o, b, l);
-       if (giststate->haskeytype[nkey])
+       if (!isNull)
        {
-               if ( ! isNull ) {
-                       cep = (GISTENTRY *)
-                               DatumGetPointer(FunctionCall1(&giststate->compressFn[nkey],
+               GISTENTRY  *cep;
+
+               gistentryinit(*e, k, r, pg, o, b, l);
+               cep = (GISTENTRY *)
+                       DatumGetPointer(FunctionCall1(&giststate->compressFn[nkey],
                                                                                  PointerGetDatum(e)));
+               /* compressFn may just return the given pointer */
+               if (cep != e)
+               {
                        gistentryinit(*e, cep->key, cep->rel, cep->page, cep->offset,
                                                  cep->bytes, cep->leafkey);
-                       if (cep != e)
-                               pfree(cep);
-               } else {
-                       gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
+                       pfree(cep);
                }
        }
+       else
+       {
+               gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
+       }
 }
 
 static IndexTuple
@@ -1747,7 +1713,7 @@ gistFormTuple( GISTSTATE *giststate, Relation r,
                                datumsize[j], FALSE, FALSE);
                        isnullchar[j] = ' ';
                        compatt[j] = centry[j].key;
-                       if ( !giststate->attbyval[j] ) {
+                       if ( !isAttByVal(giststate,j) ) {
                                whatfree[j] = TRUE;
                                if ( centry[j].key != attdata[j] )
                                        pfree(DatumGetPointer(attdata[j]));
@@ -1756,7 +1722,7 @@ gistFormTuple( GISTSTATE *giststate, Relation r,
                }
        }
 
-       tup = (IndexTuple) index_formtuple(r->rd_att, compatt, isnullchar);
+       tup = (IndexTuple) index_formtuple(giststate->tupdesc, compatt, isnullchar);
        for (j = 0; j < r->rd_att->natts; j++)
                if ( whatfree[j] ) pfree(DatumGetPointer(compatt[j]));
 
@@ -1770,11 +1736,11 @@ gistDeCompressAtt( GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p,
        Datum   datum;
 
        for(i=0; i < r->rd_att->natts; i++ ) {
-               datum = index_getattr(tuple, i+1, r->rd_att, &isnull[i]);
+               datum = index_getattr(tuple, i+1, giststate->tupdesc, &isnull[i]);
                gistdentryinit(giststate, i, &attdata[i],
                                        datum, r, p, o,
-                                       ATTSIZE( datum, r, i+1, isnull[i] ), FALSE, isnull[i]);
-               if ( giststate->attbyval[i] ) 
+                                       ATTSIZE( datum, giststate->tupdesc, i+1, isnull[i] ), FALSE, isnull[i]);
+               if ( isAttByVal(giststate,i) ) 
                        decompvec[i] = FALSE;
                else {
                        if (attdata[i].key == datum || isnull[i] )
index d334b56928e8c8c3bad36a7bc0f36450eefbf7e3..be2d7498453a33f5d7d8d93085030f09f134663e 100644 (file)
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/access/gist/gistget.c,v 1.29 2001/08/10 14:34:28 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/access/gist/gistget.c,v 1.30 2001/08/22 18:24:26 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -239,7 +239,7 @@ gistindex_keytest(IndexTuple tuple,
        {
                datum = index_getattr(tuple,
                                                          key[0].sk_attno,
-                                                         tupdesc,
+                                                         giststate->tupdesc,
                                                          &isNull);
                if (isNull)
                {
@@ -271,7 +271,7 @@ gistindex_keytest(IndexTuple tuple,
                                                                 ObjectIdGetDatum(key[0].sk_procedure));
                }
 
-               if ( de.key != datum )
+               if ( de.key != datum && ! isAttByVal( giststate, key[0].sk_attno-1 ) )
                        if ( DatumGetPointer(de.key) != NULL )
                                pfree( DatumGetPointer(de.key) );
 
index 9358692a53cc4f170294d1fd0a9b3e7d40608e72..2b65321b038281764184c2bc969417285ad4d30b 100644 (file)
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/access/gist/gistscan.c,v 1.38 2001/07/15 22:48:15 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/access/gist/gistscan.c,v 1.39 2001/08/22 18:24:26 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -234,6 +234,8 @@ gistendscan(PG_FUNCTION_ARGS)
        {
                gistfreestack(p->s_stack);
                gistfreestack(p->s_markstk);
+               if ( p->giststate != NULL ) 
+                       freeGISTstate( p->giststate );
                pfree(s->opaque);
        }
 
index 9ea03697b470d2eead27eff52987f0a48aaae189..f11b3d9f31921909ccfcf59416e8359bbb6bda20 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.161 2001/08/21 16:36:00 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.162 2001/08/22 18:24:26 tgl Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -33,6 +33,7 @@
 #include "catalog/index.h"
 #include "catalog/indexing.h"
 #include "catalog/pg_index.h"
+#include "catalog/pg_opclass.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_type.h"
 #include "commands/comment.h"
 /* non-export function prototypes */
 static Oid GetHeapRelationOid(char *heapRelationName, char *indexRelationName,
                                   bool istemp);
-static TupleDesc BuildFuncTupleDesc(Oid funcOid);
+static TupleDesc BuildFuncTupleDesc(Oid funcOid,
+                                                                       Oid *classObjectId);
 static TupleDesc ConstructTupleDescriptor(Relation heapRelation,
-                                                int numatts, AttrNumber *attNums);
+                                                                                 int numatts, AttrNumber *attNums,
+                                                                                 Oid *classObjectId);
 static void ConstructIndexReldesc(Relation indexRelation, Oid amoid);
 static Oid     UpdateRelationRelation(Relation indexRelation, char *temp_relname);
 static void InitializeAttributeOids(Relation indexRelation,
@@ -124,11 +127,14 @@ GetHeapRelationOid(char *heapRelationName, char *indexRelationName, bool istemp)
 }
 
 static TupleDesc
-BuildFuncTupleDesc(Oid funcOid)
+BuildFuncTupleDesc(Oid funcOid,
+                                  Oid *classObjectId)
 {
        TupleDesc       funcTupDesc;
        HeapTuple       tuple;
+       Oid                     keyType;
        Oid                     retType;
+       Form_pg_type typeTup;
 
        /*
         * Allocate and zero a tuple descriptor for a one-column tuple.
@@ -156,25 +162,41 @@ BuildFuncTupleDesc(Oid funcOid)
        ReleaseSysCache(tuple);
 
        /*
-        * Lookup the return type in pg_type for the type length etc.
+        * Check the opclass to see if it provides a keytype (overriding the
+        * function result type).
+        */
+       tuple = SearchSysCache(CLAOID,
+                                                  ObjectIdGetDatum(classObjectId[0]),
+                                                  0, 0, 0);
+       if (!HeapTupleIsValid(tuple))
+               elog(ERROR, "Opclass %u does not exist", classObjectId[0]);
+       keyType = ((Form_pg_opclass) GETSTRUCT(tuple))->opckeytype;
+       ReleaseSysCache(tuple);
+
+       if (!OidIsValid(keyType))
+               keyType = retType;
+
+       /*
+        * Lookup the key type in pg_type for the type length etc.
         */
        tuple = SearchSysCache(TYPEOID,
-                                                  ObjectIdGetDatum(retType),
+                                                  ObjectIdGetDatum(keyType),
                                                   0, 0, 0);
        if (!HeapTupleIsValid(tuple))
-               elog(ERROR, "Type %u does not exist", retType);
+               elog(ERROR, "Type %u does not exist", keyType);
+       typeTup = (Form_pg_type) GETSTRUCT(tuple);
 
        /*
         * Assign some of the attributes values. Leave the rest as 0.
         */
-       funcTupDesc->attrs[0]->attlen = ((Form_pg_type) GETSTRUCT(tuple))->typlen;
-       funcTupDesc->attrs[0]->atttypid = retType;
        funcTupDesc->attrs[0]->attnum = 1;
-       funcTupDesc->attrs[0]->attbyval = ((Form_pg_type) GETSTRUCT(tuple))->typbyval;
+       funcTupDesc->attrs[0]->atttypid = keyType;
+       funcTupDesc->attrs[0]->attlen = typeTup->typlen;
+       funcTupDesc->attrs[0]->attbyval = typeTup->typbyval;
        funcTupDesc->attrs[0]->attcacheoff = -1;
        funcTupDesc->attrs[0]->atttypmod = -1;
-       funcTupDesc->attrs[0]->attstorage = ((Form_pg_type) GETSTRUCT(tuple))->typstorage;
-       funcTupDesc->attrs[0]->attalign = ((Form_pg_type) GETSTRUCT(tuple))->typalign;
+       funcTupDesc->attrs[0]->attstorage = typeTup->typstorage;
+       funcTupDesc->attrs[0]->attalign = typeTup->typalign;
 
        ReleaseSysCache(tuple);
 
@@ -190,7 +212,8 @@ BuildFuncTupleDesc(Oid funcOid)
 static TupleDesc
 ConstructTupleDescriptor(Relation heapRelation,
                                                 int numatts,
-                                                AttrNumber *attNums)
+                                                AttrNumber *attNums,
+                                                Oid *classObjectId)
 {
        TupleDesc       heapTupDesc;
        TupleDesc       indexTupDesc;
@@ -217,6 +240,8 @@ ConstructTupleDescriptor(Relation heapRelation,
                AttrNumber      atnum;          /* attributeNumber[attributeOffset] */
                Form_pg_attribute from;
                Form_pg_attribute to;
+               HeapTuple       tuple;
+               Oid                     keyType;
 
                /*
                 * get the attribute number and make sure it's valid; determine
@@ -269,6 +294,40 @@ ConstructTupleDescriptor(Relation heapRelation,
                 * fix it later.
                 */
                to->attrelid = InvalidOid;
+
+               /*
+                * Check the opclass to see if it provides a keytype (overriding
+                * the attribute type).
+                */
+               tuple = SearchSysCache(CLAOID,
+                                                          ObjectIdGetDatum(classObjectId[i]),
+                                                          0, 0, 0);
+               if (!HeapTupleIsValid(tuple))
+                       elog(ERROR, "Opclass %u does not exist", classObjectId[i]);
+               keyType = ((Form_pg_opclass) GETSTRUCT(tuple))->opckeytype;
+               ReleaseSysCache(tuple);
+
+               if (OidIsValid(keyType) && keyType != to->atttypid)
+               {
+                       /* index value and heap value have different types */
+                       Form_pg_type typeTup;
+
+                       tuple = SearchSysCache(TYPEOID,
+                                                                  ObjectIdGetDatum(keyType),
+                                                                  0, 0, 0);
+                       if (!HeapTupleIsValid(tuple))
+                               elog(ERROR, "Type %u does not exist", keyType);
+                       typeTup = (Form_pg_type) GETSTRUCT(tuple);
+
+                       to->atttypid   = keyType;
+                       to->atttypmod  = -1;
+                       to->attlen     = typeTup->typlen;
+                       to->attbyval   = typeTup->typbyval;
+                       to->attalign   = typeTup->typalign;
+                       to->attstorage = typeTup->typstorage;
+
+                       ReleaseSysCache(tuple);
+               }
        }
 
        return indexTupDesc;
@@ -703,19 +762,21 @@ index_create(char *heapRelationName,
        heapRelation = heap_open(heapoid, ShareLock);
 
        /*
-        * construct new tuple descriptor
+        * construct tuple descriptor for index tuples
         */
        if (OidIsValid(indexInfo->ii_FuncOid))
-               indexTupDesc = BuildFuncTupleDesc(indexInfo->ii_FuncOid);
+               indexTupDesc = BuildFuncTupleDesc(indexInfo->ii_FuncOid,
+                                                                                 classObjectId);
        else
                indexTupDesc = ConstructTupleDescriptor(heapRelation,
                                                                                                indexInfo->ii_NumKeyAttrs,
-                                                                                  indexInfo->ii_KeyAttrNumbers);
+                                                                                               indexInfo->ii_KeyAttrNumbers,
+                                                                                               classObjectId);
 
        if (istemp)
        {
                /* save user relation name because heap_create changes it */
-               temp_relname = pstrdup(indexRelationName);              /* save original value */
+               temp_relname = pstrdup(indexRelationName);              /* save original */
                indexRelationName = palloc(NAMEDATALEN);
                strcpy(indexRelationName, temp_relname);                /* heap_create will
                                                                                                                 * change this */
index 760c025af6d323b2b18c0ff1bed33e8514707ee7..d6bce8d84510c79f3a2d353cd609916b34037c15 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: gist.h,v 1.31 2001/08/21 16:36:05 tgl Exp $
+ * $Id: gist.h,v 1.32 2001/08/22 18:24:26 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -72,11 +72,11 @@ typedef struct GISTSTATE
        FmgrInfo        penaltyFn[INDEX_MAX_KEYS];
        FmgrInfo        picksplitFn[INDEX_MAX_KEYS];
        FmgrInfo        equalFn[INDEX_MAX_KEYS];
-       bool            attbyval[INDEX_MAX_KEYS];
-       bool            haskeytype[INDEX_MAX_KEYS];
-       bool            keytypbyval[INDEX_MAX_KEYS];
+
+       TupleDesc       tupdesc;
 } GISTSTATE;
 
+#define isAttByVal( gs, anum ) (gs)->tupdesc->attrs[anum]->attbyval
 
 /*
  *     When we're doing a scan, we need to keep track of the parent stack
@@ -169,6 +169,7 @@ extern Datum gistbulkdelete(PG_FUNCTION_ARGS);
 extern void _gistdump(Relation r);
 extern void gistfreestack(GISTSTACK *s);
 extern void initGISTstate(GISTSTATE *giststate, Relation index);
+extern void freeGISTstate(GISTSTATE *giststate);
 extern void gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e,
                                                   Datum k, Relation r, Page pg, OffsetNumber o,
                                                   int b, bool l, bool isNull);