1996-08-26 22:02:12 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* gistscan.c
|
2001-05-30 21:53:40 +02:00
|
|
|
* routines to manage scans on GiST index relations
|
1996-08-26 22:02:12 +02:00
|
|
|
*
|
|
|
|
*
|
2012-01-02 00:01:58 +01:00
|
|
|
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
2001-05-30 21:53:40 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
1996-08-26 22:02:12 +02:00
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/access/gist/gistscan.c
|
1996-08-26 22:02:12 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "postgres.h"
|
1996-10-31 09:09:47 +01:00
|
|
|
|
2005-05-17 05:34:18 +02:00
|
|
|
#include "access/gist_private.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "access/gistscan.h"
|
2008-06-19 02:46:06 +02:00
|
|
|
#include "access/relscan.h"
|
2005-05-17 02:59:30 +02:00
|
|
|
#include "utils/memutils.h"
|
2010-12-04 02:52:18 +01:00
|
|
|
#include "utils/rel.h"
|
1996-10-21 07:14:02 +02:00
|
|
|
|
2010-12-04 02:52:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* RBTree support functions for the GISTSearchTreeItem queue
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
GISTSearchTreeItemComparator(const RBNode *a, const RBNode *b, void *arg)
|
|
|
|
{
|
|
|
|
const GISTSearchTreeItem *sa = (const GISTSearchTreeItem *) a;
|
|
|
|
const GISTSearchTreeItem *sb = (const GISTSearchTreeItem *) b;
|
|
|
|
IndexScanDesc scan = (IndexScanDesc) arg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Order according to distance comparison */
|
|
|
|
for (i = 0; i < scan->numberOfOrderBys; i++)
|
|
|
|
{
|
|
|
|
if (sa->distances[i] != sb->distances[i])
|
|
|
|
return (sa->distances[i] > sb->distances[i]) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GISTSearchTreeItemCombiner(RBNode *existing, const RBNode *newrb, void *arg)
|
|
|
|
{
|
|
|
|
GISTSearchTreeItem *scurrent = (GISTSearchTreeItem *) existing;
|
|
|
|
const GISTSearchTreeItem *snew = (const GISTSearchTreeItem *) newrb;
|
|
|
|
GISTSearchItem *newitem = snew->head;
|
|
|
|
|
|
|
|
/* snew should have just one item in its chain */
|
|
|
|
Assert(newitem && newitem->next == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If new item is heap tuple, it goes to front of chain; otherwise insert
|
2011-04-10 17:42:00 +02:00
|
|
|
* it before the first index-page item, so that index pages are visited in
|
|
|
|
* LIFO order, ensuring depth-first search of index pages. See comments
|
|
|
|
* in gist_private.h.
|
2010-12-04 02:52:18 +01:00
|
|
|
*/
|
|
|
|
if (GISTSearchItemIsHeap(*newitem))
|
|
|
|
{
|
|
|
|
newitem->next = scurrent->head;
|
|
|
|
scurrent->head = newitem;
|
|
|
|
if (scurrent->lastHeap == NULL)
|
|
|
|
scurrent->lastHeap = newitem;
|
|
|
|
}
|
|
|
|
else if (scurrent->lastHeap == NULL)
|
|
|
|
{
|
|
|
|
newitem->next = scurrent->head;
|
|
|
|
scurrent->head = newitem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newitem->next = scurrent->lastHeap->next;
|
|
|
|
scurrent->lastHeap->next = newitem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static RBNode *
|
|
|
|
GISTSearchTreeItemAllocator(void *arg)
|
|
|
|
{
|
|
|
|
IndexScanDesc scan = (IndexScanDesc) arg;
|
|
|
|
|
|
|
|
return palloc(GSTIHDRSZ + sizeof(double) * scan->numberOfOrderBys);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GISTSearchTreeItemDeleter(RBNode *rb, void *arg)
|
|
|
|
{
|
|
|
|
pfree(rb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Index AM API functions for scanning GiST indexes
|
|
|
|
*/
|
1996-08-26 22:02:12 +02:00
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
gistbeginscan(PG_FUNCTION_ARGS)
|
1996-08-26 22:02:12 +02:00
|
|
|
{
|
2000-06-13 09:35:40 +02:00
|
|
|
Relation r = (Relation) PG_GETARG_POINTER(0);
|
2002-05-21 01:51:44 +02:00
|
|
|
int nkeys = PG_GETARG_INT32(1);
|
2010-12-03 02:50:48 +01:00
|
|
|
int norderbys = PG_GETARG_INT32(2);
|
2005-05-17 02:59:30 +02:00
|
|
|
IndexScanDesc scan;
|
2011-10-01 01:48:57 +02:00
|
|
|
GISTSTATE *giststate;
|
2010-12-03 02:50:48 +01:00
|
|
|
GISTScanOpaque so;
|
2011-10-01 01:48:57 +02:00
|
|
|
MemoryContext oldCxt;
|
2010-12-03 02:50:48 +01:00
|
|
|
|
|
|
|
scan = RelationGetIndexScan(r, nkeys, norderbys);
|
|
|
|
|
2011-10-01 01:48:57 +02:00
|
|
|
/* First, set up a GISTSTATE with a scan-lifespan memory context */
|
|
|
|
giststate = initGISTstate(scan->indexRelation);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Everything made below is in the scanCxt, or is a child of the scanCxt,
|
|
|
|
* so it'll all go away automatically in gistendscan.
|
|
|
|
*/
|
|
|
|
oldCxt = MemoryContextSwitchTo(giststate->scanCxt);
|
|
|
|
|
2010-12-03 02:50:48 +01:00
|
|
|
/* initialize opaque data */
|
2010-12-04 02:52:18 +01:00
|
|
|
so = (GISTScanOpaque) palloc0(sizeof(GISTScanOpaqueData));
|
2011-10-01 01:48:57 +02:00
|
|
|
so->giststate = giststate;
|
|
|
|
giststate->tempCxt = createTempGistContext();
|
|
|
|
so->queue = NULL;
|
|
|
|
so->queueCxt = giststate->scanCxt; /* see gistrescan */
|
|
|
|
|
2010-12-04 02:52:18 +01:00
|
|
|
/* workspaces with size dependent on numberOfOrderBys: */
|
|
|
|
so->tmpTreeItem = palloc(GSTIHDRSZ + sizeof(double) * scan->numberOfOrderBys);
|
|
|
|
so->distances = palloc(sizeof(double) * scan->numberOfOrderBys);
|
|
|
|
so->qual_ok = true; /* in case there are zero keys */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2010-12-03 02:50:48 +01:00
|
|
|
scan->opaque = so;
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
|
2011-10-01 01:48:57 +02:00
|
|
|
MemoryContextSwitchTo(oldCxt);
|
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
PG_RETURN_POINTER(scan);
|
1996-08-26 22:02:12 +02:00
|
|
|
}
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
gistrescan(PG_FUNCTION_ARGS)
|
1996-08-26 22:02:12 +02:00
|
|
|
{
|
2005-05-17 02:59:30 +02:00
|
|
|
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
|
2002-05-21 01:51:44 +02:00
|
|
|
ScanKey key = (ScanKey) PG_GETARG_POINTER(1);
|
2010-12-04 02:52:18 +01:00
|
|
|
ScanKey orderbys = (ScanKey) PG_GETARG_POINTER(3);
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2010-12-04 02:52:18 +01:00
|
|
|
/* nkeys and norderbys arguments are ignored */
|
2010-12-03 02:50:48 +01:00
|
|
|
GISTScanOpaque so = (GISTScanOpaque) scan->opaque;
|
2011-10-01 01:48:57 +02:00
|
|
|
bool first_time;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
2010-12-04 02:52:18 +01:00
|
|
|
MemoryContext oldCxt;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2010-12-03 02:50:48 +01:00
|
|
|
/* rescan an existing indexscan --- reset state */
|
2011-10-01 01:48:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The first time through, we create the search queue in the scanCxt.
|
|
|
|
* Subsequent times through, we create the queue in a separate queueCxt,
|
|
|
|
* which is created on the second call and reset on later calls. Thus, in
|
|
|
|
* the common case where a scan is only rescan'd once, we just put the
|
|
|
|
* queue in scanCxt and don't pay the overhead of making a second memory
|
|
|
|
* context. If we do rescan more than once, the first RBTree is just left
|
|
|
|
* for dead until end of scan; this small wastage seems worth the savings
|
|
|
|
* in the common case.
|
|
|
|
*/
|
|
|
|
if (so->queue == NULL)
|
|
|
|
{
|
|
|
|
/* first time through */
|
|
|
|
Assert(so->queueCxt == so->giststate->scanCxt);
|
|
|
|
first_time = true;
|
|
|
|
}
|
|
|
|
else if (so->queueCxt == so->giststate->scanCxt)
|
|
|
|
{
|
|
|
|
/* second time through */
|
|
|
|
so->queueCxt = AllocSetContextCreate(so->giststate->scanCxt,
|
|
|
|
"GiST queue context",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
first_time = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* third or later time through */
|
|
|
|
MemoryContextReset(so->queueCxt);
|
|
|
|
first_time = false;
|
|
|
|
}
|
2003-03-24 00:01:03 +01:00
|
|
|
|
2010-12-04 02:52:18 +01:00
|
|
|
/* create new, empty RBTree for search queue */
|
|
|
|
oldCxt = MemoryContextSwitchTo(so->queueCxt);
|
|
|
|
so->queue = rb_create(GSTIHDRSZ + sizeof(double) * scan->numberOfOrderBys,
|
|
|
|
GISTSearchTreeItemComparator,
|
|
|
|
GISTSearchTreeItemCombiner,
|
|
|
|
GISTSearchTreeItemAllocator,
|
|
|
|
GISTSearchTreeItemDeleter,
|
|
|
|
scan);
|
|
|
|
MemoryContextSwitchTo(oldCxt);
|
2007-01-20 19:43:35 +01:00
|
|
|
|
2011-10-01 01:48:57 +02:00
|
|
|
so->curTreeItem = NULL;
|
2010-12-04 02:52:18 +01:00
|
|
|
so->firstCall = true;
|
2008-12-04 12:08:46 +01:00
|
|
|
|
2003-03-24 00:01:03 +01:00
|
|
|
/* Update scan key, if a new one is given */
|
2005-05-17 02:59:30 +02:00
|
|
|
if (key && scan->numberOfKeys > 0)
|
2003-03-24 00:01:03 +01:00
|
|
|
{
|
2011-10-01 01:48:57 +02:00
|
|
|
/*
|
|
|
|
* If this isn't the first time through, preserve the fn_extra
|
|
|
|
* pointers, so that if the consistentFns are using them to cache
|
|
|
|
* data, that data is not leaked across a rescan.
|
|
|
|
*/
|
|
|
|
if (!first_time)
|
|
|
|
{
|
|
|
|
for (i = 0; i < scan->numberOfKeys; i++)
|
|
|
|
{
|
|
|
|
ScanKey skey = scan->keyData + i;
|
|
|
|
|
|
|
|
so->giststate->consistentFn[skey->sk_attno - 1].fn_extra =
|
|
|
|
skey->sk_func.fn_extra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
memmove(scan->keyData, key,
|
|
|
|
scan->numberOfKeys * sizeof(ScanKeyData));
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-03-24 00:01:03 +01:00
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Modify the scan key so that the Consistent method is called for all
|
|
|
|
* comparisons. The original operator is passed to the Consistent
|
2005-09-22 22:44:36 +02:00
|
|
|
* function in the form of its strategy number, which is available
|
|
|
|
* from the sk_strategy field, and its subtype from the sk_subtype
|
2011-04-13 01:19:24 +02:00
|
|
|
* field.
|
2008-10-17 19:02:21 +02:00
|
|
|
*
|
|
|
|
* Next, if any of keys is a NULL and that key is not marked with
|
2010-02-26 03:01:40 +01:00
|
|
|
* SK_SEARCHNULL/SK_SEARCHNOTNULL then nothing can be found (ie, we
|
|
|
|
* assume all indexable operators are strict).
|
2011-10-01 01:48:57 +02:00
|
|
|
*
|
|
|
|
* Note: we intentionally memcpy the FmgrInfo to sk_func rather than
|
|
|
|
* using fmgr_info_copy. This is so that the fn_extra field gets
|
|
|
|
* preserved across multiple rescans.
|
2003-03-24 00:01:03 +01:00
|
|
|
*/
|
2010-12-04 02:52:18 +01:00
|
|
|
so->qual_ok = true;
|
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
for (i = 0; i < scan->numberOfKeys; i++)
|
|
|
|
{
|
2010-12-04 02:52:18 +01:00
|
|
|
ScanKey skey = scan->keyData + i;
|
2008-10-17 19:02:21 +02:00
|
|
|
|
2010-01-01 22:53:49 +01:00
|
|
|
skey->sk_func = so->giststate->consistentFn[skey->sk_attno - 1];
|
|
|
|
|
|
|
|
if (skey->sk_flags & SK_ISNULL)
|
2009-06-11 16:49:15 +02:00
|
|
|
{
|
2010-01-01 22:53:49 +01:00
|
|
|
if (!(skey->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL)))
|
2008-10-17 19:02:21 +02:00
|
|
|
so->qual_ok = false;
|
|
|
|
}
|
|
|
|
}
|
1997-05-22 18:01:33 +02:00
|
|
|
}
|
2000-06-13 09:35:40 +02:00
|
|
|
|
2010-12-04 02:52:18 +01:00
|
|
|
/* Update order-by key, if a new one is given */
|
|
|
|
if (orderbys && scan->numberOfOrderBys > 0)
|
|
|
|
{
|
2011-10-01 01:48:57 +02:00
|
|
|
/* As above, preserve fn_extra if not first time through */
|
|
|
|
if (!first_time)
|
|
|
|
{
|
|
|
|
for (i = 0; i < scan->numberOfOrderBys; i++)
|
|
|
|
{
|
|
|
|
ScanKey skey = scan->orderByData + i;
|
|
|
|
|
|
|
|
so->giststate->distanceFn[skey->sk_attno - 1].fn_extra =
|
|
|
|
skey->sk_func.fn_extra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-04 02:52:18 +01:00
|
|
|
memmove(scan->orderByData, orderbys,
|
|
|
|
scan->numberOfOrderBys * sizeof(ScanKeyData));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modify the order-by key so that the Distance method is called for
|
|
|
|
* all comparisons. The original operator is passed to the Distance
|
|
|
|
* function in the form of its strategy number, which is available
|
|
|
|
* from the sk_strategy field, and its subtype from the sk_subtype
|
2011-04-13 01:19:24 +02:00
|
|
|
* field.
|
2011-10-01 01:48:57 +02:00
|
|
|
*
|
|
|
|
* See above comment about why we don't use fmgr_info_copy here.
|
2010-12-04 02:52:18 +01:00
|
|
|
*/
|
|
|
|
for (i = 0; i < scan->numberOfOrderBys; i++)
|
|
|
|
{
|
|
|
|
ScanKey skey = scan->orderByData + i;
|
|
|
|
|
|
|
|
skey->sk_func = so->giststate->distanceFn[skey->sk_attno - 1];
|
|
|
|
|
|
|
|
/* Check we actually have a distance function ... */
|
|
|
|
if (!OidIsValid(skey->sk_func.fn_oid))
|
|
|
|
elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
|
|
|
|
GIST_DISTANCE_PROC, skey->sk_attno,
|
|
|
|
RelationGetRelationName(scan->indexRelation));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-14 07:24:50 +02:00
|
|
|
PG_RETURN_VOID();
|
1996-08-26 22:02:12 +02:00
|
|
|
}
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
gistmarkpos(PG_FUNCTION_ARGS)
|
1996-08-26 22:02:12 +02:00
|
|
|
{
|
2008-10-20 15:39:44 +02:00
|
|
|
elog(ERROR, "GiST does not support mark/restore");
|
2000-06-14 07:24:50 +02:00
|
|
|
PG_RETURN_VOID();
|
1996-08-26 22:02:12 +02:00
|
|
|
}
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
gistrestrpos(PG_FUNCTION_ARGS)
|
1996-08-26 22:02:12 +02:00
|
|
|
{
|
2008-10-20 15:39:44 +02:00
|
|
|
elog(ERROR, "GiST does not support mark/restore");
|
2000-06-14 07:24:50 +02:00
|
|
|
PG_RETURN_VOID();
|
1996-08-26 22:02:12 +02:00
|
|
|
}
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
Datum
|
|
|
|
gistendscan(PG_FUNCTION_ARGS)
|
1996-08-26 22:02:12 +02:00
|
|
|
{
|
2005-05-17 02:59:30 +02:00
|
|
|
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
|
2010-12-03 02:50:48 +01:00
|
|
|
GISTScanOpaque so = (GISTScanOpaque) scan->opaque;
|
|
|
|
|
2011-10-01 01:48:57 +02:00
|
|
|
/*
|
|
|
|
* freeGISTstate is enough to clean up everything made by gistbeginscan,
|
|
|
|
* as well as the queueCxt if there is a separate context for it.
|
|
|
|
*/
|
2010-12-04 02:52:18 +01:00
|
|
|
freeGISTstate(so->giststate);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-14 07:24:50 +02:00
|
|
|
PG_RETURN_VOID();
|
1996-08-26 22:02:12 +02:00
|
|
|
}
|