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
|
|
|
*
|
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, 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-01-02 17:58:17 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/access/gist/gistscan.c,v 1.78 2010/01/02 16:57:34 momjian Exp $
|
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
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "access/genam.h"
|
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"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "storage/bufmgr.h"
|
2005-05-17 02:59:30 +02:00
|
|
|
#include "utils/memutils.h"
|
1996-10-21 07:14:02 +02:00
|
|
|
|
2005-06-27 14:45:23 +02:00
|
|
|
static void gistfreestack(GISTSearchStack *s);
|
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);
|
|
|
|
ScanKey key = (ScanKey) PG_GETARG_POINTER(2);
|
2005-05-17 02:59:30 +02:00
|
|
|
IndexScanDesc scan;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
scan = RelationGetIndexScan(r, nkeys, key);
|
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
|
|
|
|
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);
|
2005-05-17 02:59:30 +02:00
|
|
|
GISTScanOpaque so;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
so = (GISTScanOpaque) scan->opaque;
|
|
|
|
if (so != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-03-24 00:01:03 +01:00
|
|
|
/* rescan an existing indexscan --- reset state */
|
2005-05-17 02:59:30 +02:00
|
|
|
gistfreestack(so->stack);
|
2008-10-20 15:39:44 +02:00
|
|
|
so->stack = NULL;
|
2005-05-17 02:59:30 +02:00
|
|
|
/* drop pins on buffers -- no locks held */
|
|
|
|
if (BufferIsValid(so->curbuf))
|
|
|
|
{
|
|
|
|
ReleaseBuffer(so->curbuf);
|
|
|
|
so->curbuf = InvalidBuffer;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* initialize opaque data */
|
2005-05-17 02:59:30 +02:00
|
|
|
so = (GISTScanOpaque) palloc(sizeof(GISTScanOpaqueData));
|
2008-10-20 15:39:44 +02:00
|
|
|
so->stack = NULL;
|
2005-05-17 02:59:30 +02:00
|
|
|
so->tempCxt = createTempGistContext();
|
2008-10-20 15:39:44 +02:00
|
|
|
so->curbuf = InvalidBuffer;
|
2005-05-17 02:59:30 +02:00
|
|
|
so->giststate = (GISTSTATE *) palloc(sizeof(GISTSTATE));
|
|
|
|
initGISTstate(so->giststate, scan->indexRelation);
|
|
|
|
|
|
|
|
scan->opaque = so;
|
2003-03-24 00:01:03 +01:00
|
|
|
}
|
|
|
|
|
2007-01-20 19:43:35 +01:00
|
|
|
/*
|
|
|
|
* Clear all the pointers.
|
|
|
|
*/
|
|
|
|
ItemPointerSetInvalid(&so->curpos);
|
2008-08-23 12:37:24 +02:00
|
|
|
so->nPageData = so->curPageData = 0;
|
2007-01-20 19:43:35 +01:00
|
|
|
|
2008-12-04 12:08:46 +01:00
|
|
|
so->qual_ok = true;
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
/*
|
2005-09-22 22:44:36 +02:00
|
|
|
* Modify the scan key so that all the Consistent method is called for
|
|
|
|
* all comparisons. The original operator is passed to the Consistent
|
|
|
|
* function in the form of its strategy number, which is available
|
|
|
|
* from the sk_strategy field, and its subtype from the sk_subtype
|
|
|
|
* 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-01-01 22:53:49 +01:00
|
|
|
* SK_SEARCHNULL/SK_SEARCHNOTNULL then nothing can be found (ie,
|
|
|
|
* we assume all indexable operators are strict).
|
2003-03-24 00:01:03 +01:00
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
for (i = 0; i < scan->numberOfKeys; i++)
|
|
|
|
{
|
2010-01-01 22:53:49 +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
|
|
|
|
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);
|
|
|
|
GISTScanOpaque so;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
so = (GISTScanOpaque) scan->opaque;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
if (so != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-05-17 02:59:30 +02:00
|
|
|
gistfreestack(so->stack);
|
|
|
|
if (so->giststate != NULL)
|
|
|
|
freeGISTstate(so->giststate);
|
|
|
|
/* drop pins on buffers -- we aren't holding any locks */
|
|
|
|
if (BufferIsValid(so->curbuf))
|
|
|
|
ReleaseBuffer(so->curbuf);
|
|
|
|
MemoryContextDelete(so->tempCxt);
|
|
|
|
pfree(scan->opaque);
|
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
|
|
|
}
|
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
static void
|
2005-06-27 14:45:23 +02:00
|
|
|
gistfreestack(GISTSearchStack *s)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2005-05-17 02:59:30 +02:00
|
|
|
while (s != NULL)
|
|
|
|
{
|
2005-06-27 14:45:23 +02:00
|
|
|
GISTSearchStack *p = s->next;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2005-05-17 02:59:30 +02:00
|
|
|
pfree(s);
|
|
|
|
s = p;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-08-26 22:02:12 +02:00
|
|
|
}
|