提交 a54075a6 编写于 作者: T Tom Lane

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.
上级 38a4c351
......@@ -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
......
......@@ -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
-------
......
此差异已折叠。
......@@ -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;
......@@ -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;
......@@ -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] )
......
......@@ -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) );
......
......@@ -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);
}
......
......@@ -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(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 */
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册