2006-05-02 13:28:56 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* ginscan.c
|
2006-10-04 02:30:14 +02:00
|
|
|
* routines to manage scans inverted index relations
|
2006-05-02 13:28:56 +02:00
|
|
|
*
|
|
|
|
*
|
2007-01-05 23:20:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
2006-05-02 13:28:56 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2007-05-27 05:50:39 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/access/gin/ginscan.c,v 1.10 2007/05/27 03:50:38 tgl Exp $
|
2006-05-02 13:28:56 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/gin.h"
|
2006-08-03 17:22:09 +02:00
|
|
|
#include "pgstat.h"
|
2006-05-02 13:28:56 +02:00
|
|
|
#include "utils/memutils.h"
|
|
|
|
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
Datum
|
|
|
|
ginbeginscan(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Relation rel = (Relation) PG_GETARG_POINTER(0);
|
|
|
|
int keysz = PG_GETARG_INT32(1);
|
|
|
|
ScanKey scankey = (ScanKey) PG_GETARG_POINTER(2);
|
2006-05-02 13:28:56 +02:00
|
|
|
IndexScanDesc scan;
|
|
|
|
|
|
|
|
scan = RelationGetIndexScan(rel, keysz, scankey);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(scan);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-10-04 02:30:14 +02:00
|
|
|
fillScanKey(GinState *ginstate, GinScanKey key, Datum query,
|
|
|
|
Datum *entryValues, uint32 nEntryValues, StrategyNumber strategy)
|
|
|
|
{
|
|
|
|
uint32 i,
|
|
|
|
j;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
key->nentries = nEntryValues;
|
2006-10-04 02:30:14 +02:00
|
|
|
key->entryRes = (bool *) palloc0(sizeof(bool) * nEntryValues);
|
|
|
|
key->scanEntry = (GinScanEntry) palloc(sizeof(GinScanEntryData) * nEntryValues);
|
2006-05-02 13:28:56 +02:00
|
|
|
key->strategy = strategy;
|
|
|
|
key->query = query;
|
2006-10-04 02:30:14 +02:00
|
|
|
key->firstCall = TRUE;
|
|
|
|
ItemPointerSet(&(key->curItem), InvalidBlockNumber, InvalidOffsetNumber);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (i = 0; i < nEntryValues; i++)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
key->scanEntry[i].pval = key->entryRes + i;
|
|
|
|
key->scanEntry[i].entry = entryValues[i];
|
2006-10-04 02:30:14 +02:00
|
|
|
ItemPointerSet(&(key->scanEntry[i].curItem), InvalidBlockNumber, InvalidOffsetNumber);
|
2006-05-02 13:28:56 +02:00
|
|
|
key->scanEntry[i].offset = InvalidOffsetNumber;
|
|
|
|
key->scanEntry[i].buffer = InvalidBuffer;
|
|
|
|
key->scanEntry[i].list = NULL;
|
|
|
|
key->scanEntry[i].nlist = 0;
|
|
|
|
|
|
|
|
/* link to the equals entry in current scan key */
|
|
|
|
key->scanEntry[i].master = NULL;
|
2006-10-04 02:30:14 +02:00
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
if (compareEntries(ginstate, entryValues[i], entryValues[j]) == 0)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
key->scanEntry[i].master = key->scanEntry + j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-02 17:48:11 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
|
2006-05-02 13:28:56 +02:00
|
|
|
static void
|
2006-10-04 02:30:14 +02:00
|
|
|
resetScanKeys(GinScanKey keys, uint32 nkeys)
|
|
|
|
{
|
|
|
|
uint32 i,
|
|
|
|
j;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (keys == NULL)
|
2006-05-02 13:28:56 +02:00
|
|
|
return;
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
{
|
|
|
|
GinScanKey key = keys + i;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
key->firstCall = TRUE;
|
2006-10-04 02:30:14 +02:00
|
|
|
ItemPointerSet(&(key->curItem), InvalidBlockNumber, InvalidOffsetNumber);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (j = 0; j < key->nentries; j++)
|
|
|
|
{
|
|
|
|
if (key->scanEntry[j].buffer != InvalidBuffer)
|
|
|
|
ReleaseBuffer(key->scanEntry[i].buffer);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
ItemPointerSet(&(key->scanEntry[j].curItem), InvalidBlockNumber, InvalidOffsetNumber);
|
2006-05-02 13:28:56 +02:00
|
|
|
key->scanEntry[j].offset = InvalidOffsetNumber;
|
|
|
|
key->scanEntry[j].buffer = InvalidBuffer;
|
|
|
|
key->scanEntry[j].list = NULL;
|
|
|
|
key->scanEntry[j].nlist = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-02 17:48:11 +02:00
|
|
|
#endif
|
|
|
|
|
2006-05-02 13:28:56 +02:00
|
|
|
static void
|
2006-10-04 02:30:14 +02:00
|
|
|
freeScanKeys(GinScanKey keys, uint32 nkeys, bool removeRes)
|
|
|
|
{
|
|
|
|
uint32 i,
|
|
|
|
j;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (keys == NULL)
|
2006-05-02 13:28:56 +02:00
|
|
|
return;
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
{
|
|
|
|
GinScanKey key = keys + i;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (j = 0; j < key->nentries; j++)
|
|
|
|
{
|
|
|
|
if (key->scanEntry[j].buffer != InvalidBuffer)
|
|
|
|
ReleaseBuffer(key->scanEntry[j].buffer);
|
|
|
|
if (removeRes && key->scanEntry[j].list)
|
2006-05-02 13:28:56 +02:00
|
|
|
pfree(key->scanEntry[j].list);
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (removeRes)
|
2006-05-02 13:28:56 +02:00
|
|
|
pfree(key->entryRes);
|
|
|
|
pfree(key->scanEntry);
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-05-02 13:28:56 +02:00
|
|
|
pfree(keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-10-04 02:30:14 +02:00
|
|
|
newScanKey(IndexScanDesc scan)
|
|
|
|
{
|
|
|
|
ScanKey scankey = scan->keyData;
|
2006-05-02 13:28:56 +02:00
|
|
|
GinScanOpaque so = (GinScanOpaque) scan->opaque;
|
2006-10-04 02:30:14 +02:00
|
|
|
int i;
|
|
|
|
uint32 nkeys = 0;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
so->keys = (GinScanKey) palloc(scan->numberOfKeys * sizeof(GinScanKeyData));
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-09-14 13:26:49 +02:00
|
|
|
if (scan->numberOfKeys < 1)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2006-10-04 02:30:14 +02:00
|
|
|
errmsg("GIN indexes do not support whole-index scans")));
|
2006-09-14 13:26:49 +02:00
|
|
|
|
2007-01-31 16:09:45 +01:00
|
|
|
so->isVoidRes = false;
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (i = 0; i < scan->numberOfKeys; i++)
|
|
|
|
{
|
|
|
|
Datum *entryValues;
|
2007-01-31 16:09:45 +01:00
|
|
|
int32 nEntryValues;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (scankey[i].sk_flags & SK_ISNULL)
|
2006-05-02 13:28:56 +02:00
|
|
|
elog(ERROR, "Gin doesn't support NULL as scan key");
|
2006-10-04 02:30:14 +02:00
|
|
|
Assert(scankey[i].sk_attno == 1);
|
|
|
|
|
|
|
|
entryValues = (Datum *) DatumGetPointer(
|
|
|
|
FunctionCall3(
|
|
|
|
&so->ginstate.extractQueryFn,
|
|
|
|
scankey[i].sk_argument,
|
|
|
|
PointerGetDatum(&nEntryValues),
|
|
|
|
UInt16GetDatum(scankey[i].sk_strategy)
|
|
|
|
)
|
|
|
|
);
|
2007-01-31 16:09:45 +01:00
|
|
|
if ( nEntryValues < 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* extractQueryFn signals that nothing will be found,
|
|
|
|
* so we can just set isVoidRes flag...
|
|
|
|
*/
|
|
|
|
so->isVoidRes = true;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
if (entryValues == NULL || nEntryValues == 0)
|
2006-05-02 13:28:56 +02:00
|
|
|
/* full scan... */
|
|
|
|
continue;
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
fillScanKey(&so->ginstate, &(so->keys[nkeys]), scankey[i].sk_argument,
|
|
|
|
entryValues, nEntryValues, scankey[i].sk_strategy);
|
2006-05-02 13:28:56 +02:00
|
|
|
nkeys++;
|
|
|
|
}
|
|
|
|
|
|
|
|
so->nkeys = nkeys;
|
|
|
|
|
2007-01-31 16:09:45 +01:00
|
|
|
if (so->nkeys == 0 && !so->isVoidRes)
|
2006-09-14 13:26:49 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2006-10-06 19:14:01 +02:00
|
|
|
errmsg("GIN index does not support search with void query")));
|
2006-08-03 17:22:09 +02:00
|
|
|
|
2007-05-27 05:50:39 +02:00
|
|
|
pgstat_count_index_scan(scan->indexRelation);
|
2006-05-02 13:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 02:30:14 +02:00
|
|
|
ginrescan(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
|
2006-10-04 02:30:14 +02:00
|
|
|
ScanKey scankey = (ScanKey) PG_GETARG_POINTER(1);
|
|
|
|
GinScanOpaque so;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
so = (GinScanOpaque) scan->opaque;
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (so == NULL)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
/* if called from ginbeginscan */
|
2006-10-04 02:30:14 +02:00
|
|
|
so = (GinScanOpaque) palloc(sizeof(GinScanOpaqueData));
|
2006-05-02 13:28:56 +02:00
|
|
|
so->tempCtx = AllocSetContextCreate(CurrentMemoryContext,
|
2006-10-04 02:30:14 +02:00
|
|
|
"Gin scan temporary context",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
2006-05-02 13:28:56 +02:00
|
|
|
initGinState(&so->ginstate, scan->indexRelation);
|
|
|
|
scan->opaque = so;
|
2006-10-04 02:30:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
freeScanKeys(so->keys, so->nkeys, TRUE);
|
|
|
|
freeScanKeys(so->markPos, so->nkeys, FALSE);
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
so->markPos = so->keys = NULL;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (scankey && scan->numberOfKeys > 0)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
memmove(scan->keyData, scankey,
|
2006-10-04 02:30:14 +02:00
|
|
|
scan->numberOfKeys * sizeof(ScanKeyData));
|
2006-05-02 13:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 02:30:14 +02:00
|
|
|
ginendscan(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
|
2006-10-04 02:30:14 +02:00
|
|
|
GinScanOpaque so = (GinScanOpaque) scan->opaque;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (so != NULL)
|
|
|
|
{
|
|
|
|
freeScanKeys(so->keys, so->nkeys, TRUE);
|
|
|
|
freeScanKeys(so->markPos, so->nkeys, FALSE);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
MemoryContextDelete(so->tempCtx);
|
|
|
|
|
|
|
|
pfree(so);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
static GinScanKey
|
2006-10-04 02:30:14 +02:00
|
|
|
copyScanKeys(GinScanKey keys, uint32 nkeys)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
GinScanKey newkeys;
|
2006-10-04 02:30:14 +02:00
|
|
|
uint32 i,
|
|
|
|
j;
|
|
|
|
|
|
|
|
newkeys = (GinScanKey) palloc(sizeof(GinScanKeyData) * nkeys);
|
|
|
|
memcpy(newkeys, keys, sizeof(GinScanKeyData) * nkeys);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
{
|
|
|
|
newkeys[i].scanEntry = (GinScanEntry) palloc(sizeof(GinScanEntryData) * keys[i].nentries);
|
|
|
|
memcpy(newkeys[i].scanEntry, keys[i].scanEntry, sizeof(GinScanEntryData) * keys[i].nentries);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
for (j = 0; j < keys[i].nentries; j++)
|
|
|
|
{
|
|
|
|
if (keys[i].scanEntry[j].buffer != InvalidBuffer)
|
|
|
|
IncrBufferRefCount(keys[i].scanEntry[j].buffer);
|
|
|
|
if (keys[i].scanEntry[j].master)
|
|
|
|
{
|
|
|
|
int masterN = keys[i].scanEntry[j].master - keys[i].scanEntry;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
newkeys[i].scanEntry[j].master = newkeys[i].scanEntry + masterN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newkeys;
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
Datum
|
|
|
|
ginmarkpos(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
|
2006-10-04 02:30:14 +02:00
|
|
|
GinScanOpaque so = (GinScanOpaque) scan->opaque;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
freeScanKeys(so->markPos, so->nkeys, FALSE);
|
2006-10-04 02:30:14 +02:00
|
|
|
so->markPos = copyScanKeys(so->keys, so->nkeys);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
Datum
|
|
|
|
ginrestrpos(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-05-02 13:28:56 +02:00
|
|
|
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
|
2006-10-04 02:30:14 +02:00
|
|
|
GinScanOpaque so = (GinScanOpaque) scan->opaque;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
freeScanKeys(so->keys, so->nkeys, FALSE);
|
2006-10-04 02:30:14 +02:00
|
|
|
so->keys = copyScanKeys(so->markPos, so->nkeys);
|
2006-05-02 13:28:56 +02:00
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|