Oops, thanks to Dan McGuirk for pointing out that I missed part of
the commit :( Here's the rest of the GiST code thta was missing...
This commit is contained in:
parent
2fd6061e1c
commit
e78fe652f4
16
src/backend/access/gist/Makefile.inc
Normal file
16
src/backend/access/gist/Makefile.inc
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#-------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Makefile.inc--
|
||||||
|
# Makefile for access/rtree (R-Tree access method)
|
||||||
|
#
|
||||||
|
# Copyright (c) 1994, Regents of the University of California
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# IDENTIFICATION
|
||||||
|
# /usr/local/devel/pglite/cvs/src/backend/access/rtree/Makefile.inc,v 1.2 1995/03/21 06:50:48 andrew Exp
|
||||||
|
#
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
SUBSRCS+= gist.c gistget.c gistscan.c giststrat.c
|
||||||
|
|
||||||
|
|
1316
src/backend/access/gist/gist.c
Normal file
1316
src/backend/access/gist/gist.c
Normal file
File diff suppressed because it is too large
Load Diff
374
src/backend/access/gist/gistget.c
Normal file
374
src/backend/access/gist/gistget.c
Normal file
@ -0,0 +1,374 @@
|
|||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
*
|
||||||
|
* gistget.c--
|
||||||
|
* fetch tuples from a GiST scan.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* IDENTIFICATION
|
||||||
|
* /usr/local/devel/pglite/cvs/src/backend/access/gisr/gistget.c,v 1.9 1995/08/01 20:16:02 jolly Exp
|
||||||
|
*
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include "storage/bufmgr.h"
|
||||||
|
#include "storage/bufpage.h"
|
||||||
|
|
||||||
|
#include "utils/elog.h"
|
||||||
|
#include "utils/palloc.h"
|
||||||
|
#include "utils/rel.h"
|
||||||
|
|
||||||
|
#include "access/heapam.h"
|
||||||
|
#include "access/genam.h"
|
||||||
|
#include "access/iqual.h"
|
||||||
|
#include "access/gist.h"
|
||||||
|
#include "access/sdir.h"
|
||||||
|
|
||||||
|
#include "executor/execdebug.h"
|
||||||
|
|
||||||
|
static OffsetNumber gistfindnext(IndexScanDesc s, Page p, OffsetNumber n,
|
||||||
|
ScanDirection dir);
|
||||||
|
static RetrieveIndexResult gistscancache(IndexScanDesc s, ScanDirection dir);
|
||||||
|
static RetrieveIndexResult gistfirst(IndexScanDesc s, ScanDirection dir);
|
||||||
|
static RetrieveIndexResult gistnext(IndexScanDesc s, ScanDirection dir);
|
||||||
|
static ItemPointer gistheapptr(Relation r, ItemPointer itemp);
|
||||||
|
|
||||||
|
|
||||||
|
RetrieveIndexResult
|
||||||
|
gistgettuple(IndexScanDesc s, ScanDirection dir)
|
||||||
|
{
|
||||||
|
RetrieveIndexResult res;
|
||||||
|
|
||||||
|
/* if we have it cached in the scan desc, just return the value */
|
||||||
|
if ((res = gistscancache(s, dir)) != (RetrieveIndexResult) NULL)
|
||||||
|
return (res);
|
||||||
|
|
||||||
|
/* not cached, so we'll have to do some work */
|
||||||
|
if (ItemPointerIsValid(&(s->currentItemData))) {
|
||||||
|
res = gistnext(s, dir);
|
||||||
|
} else {
|
||||||
|
res = gistfirst(s, dir);
|
||||||
|
}
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static RetrieveIndexResult
|
||||||
|
gistfirst(IndexScanDesc s, ScanDirection dir)
|
||||||
|
{
|
||||||
|
Buffer b;
|
||||||
|
Page p;
|
||||||
|
OffsetNumber n;
|
||||||
|
OffsetNumber maxoff;
|
||||||
|
RetrieveIndexResult res;
|
||||||
|
GISTPageOpaque po;
|
||||||
|
GISTScanOpaque so;
|
||||||
|
GISTSTACK *stk;
|
||||||
|
BlockNumber blk;
|
||||||
|
IndexTuple it;
|
||||||
|
ItemPointer ip;
|
||||||
|
|
||||||
|
b = ReadBuffer(s->relation, GISTP_ROOT);
|
||||||
|
p = BufferGetPage(b);
|
||||||
|
po = (GISTPageOpaque) PageGetSpecialPointer(p);
|
||||||
|
so = (GISTScanOpaque) s->opaque;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
maxoff = PageGetMaxOffsetNumber(p);
|
||||||
|
if (ScanDirectionIsBackward(dir))
|
||||||
|
n = gistfindnext(s, p, maxoff, dir);
|
||||||
|
else
|
||||||
|
n = gistfindnext(s, p, FirstOffsetNumber, dir);
|
||||||
|
|
||||||
|
while (n < FirstOffsetNumber || n > maxoff) {
|
||||||
|
|
||||||
|
ReleaseBuffer(b);
|
||||||
|
if (so->s_stack == (GISTSTACK *) NULL)
|
||||||
|
return ((RetrieveIndexResult) NULL);
|
||||||
|
|
||||||
|
stk = so->s_stack;
|
||||||
|
b = ReadBuffer(s->relation, stk->gs_blk);
|
||||||
|
p = BufferGetPage(b);
|
||||||
|
po = (GISTPageOpaque) PageGetSpecialPointer(p);
|
||||||
|
maxoff = PageGetMaxOffsetNumber(p);
|
||||||
|
|
||||||
|
if (ScanDirectionIsBackward(dir)) {
|
||||||
|
n = OffsetNumberPrev(stk->gs_child);
|
||||||
|
} else {
|
||||||
|
n = OffsetNumberNext(stk->gs_child);
|
||||||
|
}
|
||||||
|
so->s_stack = stk->gs_parent;
|
||||||
|
pfree(stk);
|
||||||
|
|
||||||
|
n = gistfindnext(s, p, n, dir);
|
||||||
|
}
|
||||||
|
if (po->flags & F_LEAF) {
|
||||||
|
ItemPointerSet(&(s->currentItemData), BufferGetBlockNumber(b), n);
|
||||||
|
|
||||||
|
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||||
|
ip = (ItemPointer) palloc(sizeof(ItemPointerData));
|
||||||
|
memmove((char *) ip, (char *) &(it->t_tid),
|
||||||
|
sizeof(ItemPointerData));
|
||||||
|
ReleaseBuffer(b);
|
||||||
|
|
||||||
|
res = FormRetrieveIndexResult(&(s->currentItemData), ip);
|
||||||
|
|
||||||
|
return (res);
|
||||||
|
} else {
|
||||||
|
stk = (GISTSTACK *) palloc(sizeof(GISTSTACK));
|
||||||
|
stk->gs_child = n;
|
||||||
|
stk->gs_blk = BufferGetBlockNumber(b);
|
||||||
|
stk->gs_parent = so->s_stack;
|
||||||
|
so->s_stack = stk;
|
||||||
|
|
||||||
|
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||||
|
blk = ItemPointerGetBlockNumber(&(it->t_tid));
|
||||||
|
|
||||||
|
ReleaseBuffer(b);
|
||||||
|
b = ReadBuffer(s->relation, blk);
|
||||||
|
p = BufferGetPage(b);
|
||||||
|
po = (GISTPageOpaque) PageGetSpecialPointer(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static RetrieveIndexResult
|
||||||
|
gistnext(IndexScanDesc s, ScanDirection dir)
|
||||||
|
{
|
||||||
|
Buffer b;
|
||||||
|
Page p;
|
||||||
|
OffsetNumber n;
|
||||||
|
OffsetNumber maxoff;
|
||||||
|
RetrieveIndexResult res;
|
||||||
|
GISTPageOpaque po;
|
||||||
|
GISTScanOpaque so;
|
||||||
|
GISTSTACK *stk;
|
||||||
|
BlockNumber blk;
|
||||||
|
IndexTuple it;
|
||||||
|
ItemPointer ip;
|
||||||
|
|
||||||
|
blk = ItemPointerGetBlockNumber(&(s->currentItemData));
|
||||||
|
n = ItemPointerGetOffsetNumber(&(s->currentItemData));
|
||||||
|
|
||||||
|
if (ScanDirectionIsForward(dir)) {
|
||||||
|
n = OffsetNumberNext(n);
|
||||||
|
} else {
|
||||||
|
n = OffsetNumberPrev(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
b = ReadBuffer(s->relation, blk);
|
||||||
|
p = BufferGetPage(b);
|
||||||
|
po = (GISTPageOpaque) PageGetSpecialPointer(p);
|
||||||
|
so = (GISTScanOpaque) s->opaque;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
maxoff = PageGetMaxOffsetNumber(p);
|
||||||
|
n = gistfindnext(s, p, n, dir);
|
||||||
|
|
||||||
|
while (n < FirstOffsetNumber || n > maxoff) {
|
||||||
|
|
||||||
|
ReleaseBuffer(b);
|
||||||
|
if (so->s_stack == (GISTSTACK *) NULL)
|
||||||
|
return ((RetrieveIndexResult) NULL);
|
||||||
|
|
||||||
|
stk = so->s_stack;
|
||||||
|
b = ReadBuffer(s->relation, stk->gs_blk);
|
||||||
|
p = BufferGetPage(b);
|
||||||
|
maxoff = PageGetMaxOffsetNumber(p);
|
||||||
|
po = (GISTPageOpaque) PageGetSpecialPointer(p);
|
||||||
|
|
||||||
|
if (ScanDirectionIsBackward(dir)) {
|
||||||
|
n = OffsetNumberPrev(stk->gs_child);
|
||||||
|
} else {
|
||||||
|
n = OffsetNumberNext(stk->gs_child);
|
||||||
|
}
|
||||||
|
so->s_stack = stk->gs_parent;
|
||||||
|
pfree(stk);
|
||||||
|
|
||||||
|
n = gistfindnext(s, p, n, dir);
|
||||||
|
}
|
||||||
|
if (po->flags & F_LEAF) {
|
||||||
|
ItemPointerSet(&(s->currentItemData), BufferGetBlockNumber(b), n);
|
||||||
|
|
||||||
|
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||||
|
ip = (ItemPointer) palloc(sizeof(ItemPointerData));
|
||||||
|
memmove((char *) ip, (char *) &(it->t_tid),
|
||||||
|
sizeof(ItemPointerData));
|
||||||
|
ReleaseBuffer(b);
|
||||||
|
|
||||||
|
res = FormRetrieveIndexResult(&(s->currentItemData), ip);
|
||||||
|
|
||||||
|
return (res);
|
||||||
|
} else {
|
||||||
|
stk = (GISTSTACK *) palloc(sizeof(GISTSTACK));
|
||||||
|
stk->gs_child = n;
|
||||||
|
stk->gs_blk = BufferGetBlockNumber(b);
|
||||||
|
stk->gs_parent = so->s_stack;
|
||||||
|
so->s_stack = stk;
|
||||||
|
|
||||||
|
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||||
|
blk = ItemPointerGetBlockNumber(&(it->t_tid));
|
||||||
|
|
||||||
|
ReleaseBuffer(b);
|
||||||
|
b = ReadBuffer(s->relation, blk);
|
||||||
|
p = BufferGetPage(b);
|
||||||
|
po = (GISTPageOpaque) PageGetSpecialPointer(p);
|
||||||
|
|
||||||
|
if (ScanDirectionIsBackward(dir)) {
|
||||||
|
n = PageGetMaxOffsetNumber(p);
|
||||||
|
} else {
|
||||||
|
n = FirstOffsetNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Similar to index_keytest, but decompresses the key in the IndexTuple */
|
||||||
|
bool
|
||||||
|
gistindex_keytest(IndexTuple tuple,
|
||||||
|
TupleDesc tupdesc,
|
||||||
|
int scanKeySize,
|
||||||
|
ScanKey key,
|
||||||
|
GISTSTATE *giststate,
|
||||||
|
Relation r,
|
||||||
|
Page p,
|
||||||
|
OffsetNumber offset)
|
||||||
|
{
|
||||||
|
bool isNull;
|
||||||
|
Datum datum;
|
||||||
|
int test;
|
||||||
|
GISTENTRY de;
|
||||||
|
|
||||||
|
IncrIndexProcessed();
|
||||||
|
|
||||||
|
|
||||||
|
while (scanKeySize > 0) {
|
||||||
|
datum = index_getattr(tuple,
|
||||||
|
1,
|
||||||
|
tupdesc,
|
||||||
|
&isNull);
|
||||||
|
gistdentryinit(giststate, &de, (char *)datum, r, p, offset,
|
||||||
|
IndexTupleSize(tuple) - sizeof(IndexTupleData),
|
||||||
|
FALSE);
|
||||||
|
|
||||||
|
if (isNull) {
|
||||||
|
/* XXX eventually should check if SK_ISNULL */
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key[0].sk_flags & SK_COMMUTE) {
|
||||||
|
test = (int) (*(key[0].sk_func))
|
||||||
|
(DatumGetPointer(key[0].sk_argument),
|
||||||
|
&de, key[0].sk_procedure);
|
||||||
|
} else {
|
||||||
|
test = (int) (*(key[0].sk_func))
|
||||||
|
(&de,
|
||||||
|
DatumGetPointer(key[0].sk_argument),
|
||||||
|
key[0].sk_procedure);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!test == !(key[0].sk_flags & SK_NEGATE)) {
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
scanKeySize -= 1;
|
||||||
|
key++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static OffsetNumber
|
||||||
|
gistfindnext(IndexScanDesc s, Page p, OffsetNumber n, ScanDirection dir)
|
||||||
|
{
|
||||||
|
OffsetNumber maxoff;
|
||||||
|
char *it;
|
||||||
|
GISTPageOpaque po;
|
||||||
|
GISTScanOpaque so;
|
||||||
|
GISTENTRY de;
|
||||||
|
GISTSTATE *giststate;
|
||||||
|
|
||||||
|
maxoff = PageGetMaxOffsetNumber(p);
|
||||||
|
po = (GISTPageOpaque) PageGetSpecialPointer(p);
|
||||||
|
so = (GISTScanOpaque) s->opaque;
|
||||||
|
giststate = so->giststate;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If we modified the index during the scan, we may have a pointer to
|
||||||
|
* a ghost tuple, before the scan. If this is the case, back up one.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (so->s_flags & GS_CURBEFORE) {
|
||||||
|
so->s_flags &= ~GS_CURBEFORE;
|
||||||
|
n = OffsetNumberPrev(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (n >= FirstOffsetNumber && n <= maxoff) {
|
||||||
|
it = (char *) PageGetItem(p, PageGetItemId(p, n));
|
||||||
|
if (gistindex_keytest((IndexTuple) it,
|
||||||
|
RelationGetTupleDescriptor(s->relation),
|
||||||
|
s->numberOfKeys, s->keyData, giststate,
|
||||||
|
s->relation, p, n))
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (ScanDirectionIsBackward(dir)) {
|
||||||
|
n = OffsetNumberPrev(n);
|
||||||
|
} else {
|
||||||
|
n = OffsetNumberNext(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
static RetrieveIndexResult
|
||||||
|
gistscancache(IndexScanDesc s, ScanDirection dir)
|
||||||
|
{
|
||||||
|
RetrieveIndexResult res;
|
||||||
|
ItemPointer ip;
|
||||||
|
|
||||||
|
if (!(ScanDirectionIsNoMovement(dir)
|
||||||
|
&& ItemPointerIsValid(&(s->currentItemData)))) {
|
||||||
|
|
||||||
|
return ((RetrieveIndexResult) NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
ip = gistheapptr(s->relation, &(s->currentItemData));
|
||||||
|
|
||||||
|
if (ItemPointerIsValid(ip))
|
||||||
|
res = FormRetrieveIndexResult(&(s->currentItemData), ip);
|
||||||
|
else
|
||||||
|
res = (RetrieveIndexResult) NULL;
|
||||||
|
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* gistheapptr returns the item pointer to the tuple in the heap relation
|
||||||
|
* for which itemp is the index relation item pointer.
|
||||||
|
*/
|
||||||
|
static ItemPointer
|
||||||
|
gistheapptr(Relation r, ItemPointer itemp)
|
||||||
|
{
|
||||||
|
Buffer b;
|
||||||
|
Page p;
|
||||||
|
IndexTuple it;
|
||||||
|
ItemPointer ip;
|
||||||
|
OffsetNumber n;
|
||||||
|
|
||||||
|
ip = (ItemPointer) palloc(sizeof(ItemPointerData));
|
||||||
|
if (ItemPointerIsValid(itemp)) {
|
||||||
|
b = ReadBuffer(r, ItemPointerGetBlockNumber(itemp));
|
||||||
|
p = BufferGetPage(b);
|
||||||
|
n = ItemPointerGetOffsetNumber(itemp);
|
||||||
|
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||||
|
memmove((char *) ip, (char *) &(it->t_tid),
|
||||||
|
sizeof(ItemPointerData));
|
||||||
|
ReleaseBuffer(b);
|
||||||
|
} else {
|
||||||
|
ItemPointerSetInvalid(ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (ip);
|
||||||
|
}
|
1159
src/backend/access/gist/gistold.c
Normal file
1159
src/backend/access/gist/gistold.c
Normal file
File diff suppressed because it is too large
Load Diff
382
src/backend/access/gist/gistscan.c
Normal file
382
src/backend/access/gist/gistscan.c
Normal file
@ -0,0 +1,382 @@
|
|||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
*
|
||||||
|
* gistscan.c--
|
||||||
|
* routines to manage scans on index relations
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* IDENTIFICATION
|
||||||
|
* /usr/local/devel/pglite/cvs/src/backend/access/gist/gistscan.c,v 1.7 1995/06/14 00:10:05 jolly Exp
|
||||||
|
*
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#include "c.h"
|
||||||
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include "storage/bufmgr.h"
|
||||||
|
#include "storage/bufpage.h"
|
||||||
|
|
||||||
|
#include "utils/elog.h"
|
||||||
|
#include "utils/palloc.h"
|
||||||
|
#include "utils/rel.h"
|
||||||
|
|
||||||
|
#include "access/heapam.h"
|
||||||
|
#include "access/genam.h"
|
||||||
|
#include "access/gist.h"
|
||||||
|
#include "access/giststrat.h"
|
||||||
|
|
||||||
|
/* routines defined and used here */
|
||||||
|
static void gistregscan(IndexScanDesc s);
|
||||||
|
static void gistdropscan(IndexScanDesc s);
|
||||||
|
static void gistadjone(IndexScanDesc s, int op, BlockNumber blkno,
|
||||||
|
OffsetNumber offnum);
|
||||||
|
static void adjuststack(GISTSTACK *stk, BlockNumber blkno,
|
||||||
|
OffsetNumber offnum);
|
||||||
|
static void adjustiptr(IndexScanDesc s, ItemPointer iptr,
|
||||||
|
int op, BlockNumber blkno, OffsetNumber offnum);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Whenever we start a GiST scan in a backend, we register it in private
|
||||||
|
* space. Then if the GiST index gets updated, we check all registered
|
||||||
|
* scans and adjust them if the tuple they point at got moved by the
|
||||||
|
* update. We only need to do this in private space, because when we update
|
||||||
|
* an GiST we have a write lock on the tree, so no other process can have
|
||||||
|
* any locks at all on it. A single transaction can have write and read
|
||||||
|
* locks on the same object, so that's why we need to handle this case.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef struct GISTScanListData {
|
||||||
|
IndexScanDesc gsl_scan;
|
||||||
|
struct GISTScanListData *gsl_next;
|
||||||
|
} GISTScanListData;
|
||||||
|
|
||||||
|
typedef GISTScanListData *GISTScanList;
|
||||||
|
|
||||||
|
/* pointer to list of local scans on GiSTs */
|
||||||
|
static GISTScanList GISTScans = (GISTScanList) NULL;
|
||||||
|
|
||||||
|
IndexScanDesc
|
||||||
|
gistbeginscan(Relation r,
|
||||||
|
bool fromEnd,
|
||||||
|
uint16 nkeys,
|
||||||
|
ScanKey key)
|
||||||
|
{
|
||||||
|
IndexScanDesc s;
|
||||||
|
|
||||||
|
RelationSetLockForRead(r);
|
||||||
|
s = RelationGetIndexScan(r, fromEnd, nkeys, key);
|
||||||
|
gistregscan(s);
|
||||||
|
|
||||||
|
return (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
gistrescan(IndexScanDesc s, bool fromEnd, ScanKey key)
|
||||||
|
{
|
||||||
|
GISTScanOpaque p;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!IndexScanIsValid(s)) {
|
||||||
|
elog(WARN, "gistrescan: invalid scan.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Clear all the pointers.
|
||||||
|
*/
|
||||||
|
|
||||||
|
ItemPointerSetInvalid(&s->previousItemData);
|
||||||
|
ItemPointerSetInvalid(&s->currentItemData);
|
||||||
|
ItemPointerSetInvalid(&s->nextItemData);
|
||||||
|
ItemPointerSetInvalid(&s->previousMarkData);
|
||||||
|
ItemPointerSetInvalid(&s->currentMarkData);
|
||||||
|
ItemPointerSetInvalid(&s->nextMarkData);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set flags.
|
||||||
|
*/
|
||||||
|
if (RelationGetNumberOfBlocks(s->relation) == 0) {
|
||||||
|
s->flags = ScanUnmarked;
|
||||||
|
} else if (fromEnd) {
|
||||||
|
s->flags = ScanUnmarked | ScanUncheckedPrevious;
|
||||||
|
} else {
|
||||||
|
s->flags = ScanUnmarked | ScanUncheckedNext;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->scanFromEnd = fromEnd;
|
||||||
|
|
||||||
|
if (s->numberOfKeys > 0) {
|
||||||
|
memmove(s->keyData,
|
||||||
|
key,
|
||||||
|
s->numberOfKeys * sizeof(ScanKeyData));
|
||||||
|
}
|
||||||
|
|
||||||
|
p = (GISTScanOpaque) s->opaque;
|
||||||
|
if (p != (GISTScanOpaque) NULL) {
|
||||||
|
freestack(p->s_stack);
|
||||||
|
freestack(p->s_markstk);
|
||||||
|
p->s_stack = p->s_markstk = (GISTSTACK *) NULL;
|
||||||
|
p->s_flags = 0x0;
|
||||||
|
} else {
|
||||||
|
/* initialize opaque data */
|
||||||
|
p = (GISTScanOpaque) palloc(sizeof(GISTScanOpaqueData));
|
||||||
|
p->s_stack = p->s_markstk = (GISTSTACK *) NULL;
|
||||||
|
p->s_flags = 0x0;
|
||||||
|
s->opaque = p;
|
||||||
|
p->giststate = (GISTSTATE *)palloc(sizeof(GISTSTATE));
|
||||||
|
initGISTstate(p->giststate, s->relation);
|
||||||
|
if (s->numberOfKeys > 0)
|
||||||
|
/*
|
||||||
|
** Play games here with the scan key to use the Consistent
|
||||||
|
** function for all comparisons:
|
||||||
|
** 1) the sk_procedure field will now be used to hold the
|
||||||
|
** strategy number
|
||||||
|
** 2) the sk_func field will point to the Consistent function
|
||||||
|
*/
|
||||||
|
for (i = 0; i < s->numberOfKeys; i++) {
|
||||||
|
/* s->keyData[i].sk_procedure
|
||||||
|
= index_getprocid(s->relation, 1, GIST_CONSISTENT_PROC); */
|
||||||
|
s->keyData[i].sk_procedure
|
||||||
|
= RelationGetGISTStrategy(s->relation, s->keyData[i].sk_attno,
|
||||||
|
s->keyData[i].sk_procedure);
|
||||||
|
s->keyData[i].sk_func = p->giststate->consistentFn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
gistmarkpos(IndexScanDesc s)
|
||||||
|
{
|
||||||
|
GISTScanOpaque p;
|
||||||
|
GISTSTACK *o, *n, *tmp;
|
||||||
|
|
||||||
|
s->currentMarkData = s->currentItemData;
|
||||||
|
p = (GISTScanOpaque) s->opaque;
|
||||||
|
if (p->s_flags & GS_CURBEFORE)
|
||||||
|
p->s_flags |= GS_MRKBEFORE;
|
||||||
|
else
|
||||||
|
p->s_flags &= ~GS_MRKBEFORE;
|
||||||
|
|
||||||
|
o = (GISTSTACK *) NULL;
|
||||||
|
n = p->s_stack;
|
||||||
|
|
||||||
|
/* copy the parent stack from the current item data */
|
||||||
|
while (n != (GISTSTACK *) NULL) {
|
||||||
|
tmp = (GISTSTACK *) palloc(sizeof(GISTSTACK));
|
||||||
|
tmp->gs_child = n->gs_child;
|
||||||
|
tmp->gs_blk = n->gs_blk;
|
||||||
|
tmp->gs_parent = o;
|
||||||
|
o = tmp;
|
||||||
|
n = n->gs_parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
freestack(p->s_markstk);
|
||||||
|
p->s_markstk = o;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
gistrestrpos(IndexScanDesc s)
|
||||||
|
{
|
||||||
|
GISTScanOpaque p;
|
||||||
|
GISTSTACK *o, *n, *tmp;
|
||||||
|
|
||||||
|
s->currentItemData = s->currentMarkData;
|
||||||
|
p = (GISTScanOpaque) s->opaque;
|
||||||
|
if (p->s_flags & GS_MRKBEFORE)
|
||||||
|
p->s_flags |= GS_CURBEFORE;
|
||||||
|
else
|
||||||
|
p->s_flags &= ~GS_CURBEFORE;
|
||||||
|
|
||||||
|
o = (GISTSTACK *) NULL;
|
||||||
|
n = p->s_markstk;
|
||||||
|
|
||||||
|
/* copy the parent stack from the current item data */
|
||||||
|
while (n != (GISTSTACK *) NULL) {
|
||||||
|
tmp = (GISTSTACK *) palloc(sizeof(GISTSTACK));
|
||||||
|
tmp->gs_child = n->gs_child;
|
||||||
|
tmp->gs_blk = n->gs_blk;
|
||||||
|
tmp->gs_parent = o;
|
||||||
|
o = tmp;
|
||||||
|
n = n->gs_parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
freestack(p->s_stack);
|
||||||
|
p->s_stack = o;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
gistendscan(IndexScanDesc s)
|
||||||
|
{
|
||||||
|
GISTScanOpaque p;
|
||||||
|
|
||||||
|
p = (GISTScanOpaque) s->opaque;
|
||||||
|
|
||||||
|
if (p != (GISTScanOpaque) NULL) {
|
||||||
|
freestack(p->s_stack);
|
||||||
|
freestack(p->s_markstk);
|
||||||
|
}
|
||||||
|
|
||||||
|
gistdropscan(s);
|
||||||
|
/* XXX don't unset read lock -- two-phase locking */
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
gistregscan(IndexScanDesc s)
|
||||||
|
{
|
||||||
|
GISTScanList l;
|
||||||
|
|
||||||
|
l = (GISTScanList) palloc(sizeof(GISTScanListData));
|
||||||
|
l->gsl_scan = s;
|
||||||
|
l->gsl_next = GISTScans;
|
||||||
|
GISTScans = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
gistdropscan(IndexScanDesc s)
|
||||||
|
{
|
||||||
|
GISTScanList l;
|
||||||
|
GISTScanList prev;
|
||||||
|
|
||||||
|
prev = (GISTScanList) NULL;
|
||||||
|
|
||||||
|
for (l = GISTScans;
|
||||||
|
l != (GISTScanList) NULL && l->gsl_scan != s;
|
||||||
|
l = l->gsl_next) {
|
||||||
|
prev = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (l == (GISTScanList) NULL)
|
||||||
|
elog(WARN, "GiST scan list corrupted -- cannot find 0x%lx", s);
|
||||||
|
|
||||||
|
if (prev == (GISTScanList) NULL)
|
||||||
|
GISTScans = l->gsl_next;
|
||||||
|
else
|
||||||
|
prev->gsl_next = l->gsl_next;
|
||||||
|
|
||||||
|
pfree(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
gistadjscans(Relation r, int op, BlockNumber blkno, OffsetNumber offnum)
|
||||||
|
{
|
||||||
|
GISTScanList l;
|
||||||
|
Oid relid;
|
||||||
|
|
||||||
|
relid = r->rd_id;
|
||||||
|
for (l = GISTScans; l != (GISTScanList) NULL; l = l->gsl_next) {
|
||||||
|
if (l->gsl_scan->relation->rd_id == relid)
|
||||||
|
gistadjone(l->gsl_scan, op, blkno, offnum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* gistadjone() -- adjust one scan for update.
|
||||||
|
*
|
||||||
|
* By here, the scan passed in is on a modified relation. Op tells
|
||||||
|
* us what the modification is, and blkno and offind tell us what
|
||||||
|
* block and offset index were affected. This routine checks the
|
||||||
|
* current and marked positions, and the current and marked stacks,
|
||||||
|
* to see if any stored location needs to be changed because of the
|
||||||
|
* update. If so, we make the change here.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
gistadjone(IndexScanDesc s,
|
||||||
|
int op,
|
||||||
|
BlockNumber blkno,
|
||||||
|
OffsetNumber offnum)
|
||||||
|
{
|
||||||
|
GISTScanOpaque so;
|
||||||
|
|
||||||
|
adjustiptr(s, &(s->currentItemData), op, blkno, offnum);
|
||||||
|
adjustiptr(s, &(s->currentMarkData), op, blkno, offnum);
|
||||||
|
|
||||||
|
so = (GISTScanOpaque) s->opaque;
|
||||||
|
|
||||||
|
if (op == GISTOP_SPLIT) {
|
||||||
|
adjuststack(so->s_stack, blkno, offnum);
|
||||||
|
adjuststack(so->s_markstk, blkno, offnum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* adjustiptr() -- adjust current and marked item pointers in the scan
|
||||||
|
*
|
||||||
|
* Depending on the type of update and the place it happened, we
|
||||||
|
* need to do nothing, to back up one record, or to start over on
|
||||||
|
* the same page.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
adjustiptr(IndexScanDesc s,
|
||||||
|
ItemPointer iptr,
|
||||||
|
int op,
|
||||||
|
BlockNumber blkno,
|
||||||
|
OffsetNumber offnum)
|
||||||
|
{
|
||||||
|
OffsetNumber curoff;
|
||||||
|
GISTScanOpaque so;
|
||||||
|
|
||||||
|
if (ItemPointerIsValid(iptr)) {
|
||||||
|
if (ItemPointerGetBlockNumber(iptr) == blkno) {
|
||||||
|
curoff = ItemPointerGetOffsetNumber(iptr);
|
||||||
|
so = (GISTScanOpaque) s->opaque;
|
||||||
|
|
||||||
|
switch (op) {
|
||||||
|
case GISTOP_DEL:
|
||||||
|
/* back up one if we need to */
|
||||||
|
if (curoff >= offnum) {
|
||||||
|
|
||||||
|
if (curoff > FirstOffsetNumber) {
|
||||||
|
/* just adjust the item pointer */
|
||||||
|
ItemPointerSet(iptr, blkno, OffsetNumberPrev(curoff));
|
||||||
|
} else {
|
||||||
|
/* remember that we're before the current tuple */
|
||||||
|
ItemPointerSet(iptr, blkno, FirstOffsetNumber);
|
||||||
|
if (iptr == &(s->currentItemData))
|
||||||
|
so->s_flags |= GS_CURBEFORE;
|
||||||
|
else
|
||||||
|
so->s_flags |= GS_MRKBEFORE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GISTOP_SPLIT:
|
||||||
|
/* back to start of page on split */
|
||||||
|
ItemPointerSet(iptr, blkno, FirstOffsetNumber);
|
||||||
|
if (iptr == &(s->currentItemData))
|
||||||
|
so->s_flags &= ~GS_CURBEFORE;
|
||||||
|
else
|
||||||
|
so->s_flags &= ~GS_MRKBEFORE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
elog(WARN, "Bad operation in GiST scan adjust: %d", op);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* adjuststack() -- adjust the supplied stack for a split on a page in
|
||||||
|
* the index we're scanning.
|
||||||
|
*
|
||||||
|
* If a page on our parent stack has split, we need to back up to the
|
||||||
|
* beginning of the page and rescan it. The reason for this is that
|
||||||
|
* the split algorithm for GiSTs doesn't order tuples in any useful
|
||||||
|
* way on a single page. This means on that a split, we may wind up
|
||||||
|
* looking at some heap tuples more than once. This is handled in the
|
||||||
|
* access method update code for heaps; if we've modified the tuple we
|
||||||
|
* are looking at already in this transaction, we ignore the update
|
||||||
|
* request.
|
||||||
|
*/
|
||||||
|
/*ARGSUSED*/
|
||||||
|
static void
|
||||||
|
adjuststack(GISTSTACK *stk,
|
||||||
|
BlockNumber blkno,
|
||||||
|
OffsetNumber offnum)
|
||||||
|
{
|
||||||
|
while (stk != (GISTSTACK *) NULL) {
|
||||||
|
if (stk->gs_blk == blkno)
|
||||||
|
stk->gs_child = FirstOffsetNumber;
|
||||||
|
|
||||||
|
stk = stk->gs_parent;
|
||||||
|
}
|
||||||
|
}
|
119
src/backend/access/gist/giststrat.c
Normal file
119
src/backend/access/gist/giststrat.c
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
*
|
||||||
|
* giststrat.c--
|
||||||
|
* strategy map data for GiSTs.
|
||||||
|
*
|
||||||
|
* Copyright (c) 1994, Regents of the University of California
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* IDENTIFICATION
|
||||||
|
* /usr/local/devel/pglite/cvs/src/backend/access/gist/giststrat.c,v 1.4 1995/06/14 00:10:05 jolly Exp
|
||||||
|
*
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#include "c.h"
|
||||||
|
|
||||||
|
#include "utils/rel.h"
|
||||||
|
|
||||||
|
#include "storage/bufmgr.h"
|
||||||
|
#include "storage/bufpage.h"
|
||||||
|
|
||||||
|
#include "access/istrat.h"
|
||||||
|
#include "access/gist.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Note: negate, commute, and negatecommute all assume that operators are
|
||||||
|
* ordered as follows in the strategy map:
|
||||||
|
*
|
||||||
|
* contains, contained-by
|
||||||
|
*
|
||||||
|
* The negate, commute, and negatecommute arrays are used by the planner
|
||||||
|
* to plan indexed scans over data that appears in the qualificiation in
|
||||||
|
* a boolean negation, or whose operands appear in the wrong order. For
|
||||||
|
* example, if the operator "<%" means "contains", and the user says
|
||||||
|
*
|
||||||
|
* where not rel.box <% "(10,10,20,20)"::box
|
||||||
|
*
|
||||||
|
* the planner can plan an index scan by noting that GiST indices have
|
||||||
|
* an operator in their operator class for negating <%.
|
||||||
|
*
|
||||||
|
* Similarly, if the user says something like
|
||||||
|
*
|
||||||
|
* where "(10,10,20,20)"::box <% rel.box
|
||||||
|
*
|
||||||
|
* the planner can see that the GiST index on rel.box has an operator in
|
||||||
|
* its opclass for commuting <%, and plan the scan using that operator.
|
||||||
|
* This added complexity in the access methods makes the planner a lot easier
|
||||||
|
* to write.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* if a op b, what operator tells us if (not a op b)? */
|
||||||
|
static StrategyNumber GISTNegate[GISTNStrategies] = {
|
||||||
|
InvalidStrategy,
|
||||||
|
InvalidStrategy,
|
||||||
|
InvalidStrategy
|
||||||
|
};
|
||||||
|
|
||||||
|
/* if a op_1 b, what is the operator op_2 such that b op_2 a? */
|
||||||
|
static StrategyNumber GISTCommute[GISTNStrategies] = {
|
||||||
|
InvalidStrategy,
|
||||||
|
InvalidStrategy,
|
||||||
|
InvalidStrategy
|
||||||
|
};
|
||||||
|
|
||||||
|
/* if a op_1 b, what is the operator op_2 such that (b !op_2 a)? */
|
||||||
|
static StrategyNumber GISTNegateCommute[GISTNStrategies] = {
|
||||||
|
InvalidStrategy,
|
||||||
|
InvalidStrategy,
|
||||||
|
InvalidStrategy
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* GiSTs do not currently support TermData (see rtree/rtstrat.c for
|
||||||
|
* discussion of
|
||||||
|
* TermData) -- such logic must be encoded in the user's Consistent function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If you were sufficiently attentive to detail, you would go through
|
||||||
|
* the ExpressionData pain above for every one of the strategies
|
||||||
|
* we defined. I am not. Now we declare the StrategyEvaluationData
|
||||||
|
* structure that gets shipped around to help the planner and the access
|
||||||
|
* method decide what sort of scan it should do, based on (a) what the
|
||||||
|
* user asked for, (b) what operators are defined for a particular opclass,
|
||||||
|
* and (c) the reams of information we supplied above.
|
||||||
|
*
|
||||||
|
* The idea of all of this initialized data is to make life easier on the
|
||||||
|
* user when he defines a new operator class to use this access method.
|
||||||
|
* By filling in all the data, we let him get away with leaving holes in his
|
||||||
|
* operator class, and still let him use the index. The added complexity
|
||||||
|
* in the access methods just isn't worth the trouble, though.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static StrategyEvaluationData GISTEvaluationData = {
|
||||||
|
GISTNStrategies, /* # of strategies */
|
||||||
|
(StrategyTransformMap) GISTNegate, /* how to do (not qual) */
|
||||||
|
(StrategyTransformMap) GISTCommute, /* how to swap operands */
|
||||||
|
(StrategyTransformMap) GISTNegateCommute, /* how to do both */
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
StrategyNumber
|
||||||
|
RelationGetGISTStrategy(Relation r,
|
||||||
|
AttrNumber attnum,
|
||||||
|
RegProcedure proc)
|
||||||
|
{
|
||||||
|
return (RelationGetStrategy(r, attnum, &GISTEvaluationData, proc));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
RelationInvokeGISTStrategy(Relation r,
|
||||||
|
AttrNumber attnum,
|
||||||
|
StrategyNumber s,
|
||||||
|
Datum left,
|
||||||
|
Datum right)
|
||||||
|
{
|
||||||
|
return (RelationInvokeStrategy(r, &GISTEvaluationData, attnum, s,
|
||||||
|
left, right));
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user