1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-18 02:02:55 +03:00

Update GiST for new pg_opclass arrangement (finally a clean solution

for haskeytype).  Update GiST contrib modules too.  Add linear-time split
algorithm for R-tree GiST opclass.
From Oleg Bartunov and Teodor Sigaev.
This commit is contained in:
Tom Lane
2001-08-22 18:24:26 +00:00
parent 38a4c35116
commit a54075a6d6
10 changed files with 397 additions and 594 deletions

View 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 $
*
*-------------------------------------------------------------------------
*/
@@ -19,21 +19,15 @@
#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] )

View 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) );

View 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);
}

View 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"
@@ -62,9 +63,11 @@
/* 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(TYPEOID,
ObjectIdGetDatum(retType),
tuple = SearchSysCache(CLAOID,
ObjectIdGetDatum(classObjectId[0]),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "Type %u does not exist", retType);
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(keyType),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
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 */