1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2003-11-09 22:30:38 +01:00
|
|
|
* nbtutils.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Utility code for Postgres btree implementation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-11-29 20:52:15 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtutils.c,v 1.57 2003/11/29 19:51:40 pgsql Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "postgres.h"
|
1996-10-23 09:42:13 +02:00
|
|
|
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/nbtree.h"
|
2000-11-21 22:16:06 +01:00
|
|
|
#include "catalog/catalog.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "executor/execdebug.h"
|
2002-05-24 20:57:57 +02:00
|
|
|
|
|
|
|
|
2000-02-18 07:32:39 +01:00
|
|
|
/*
|
|
|
|
* _bt_mkscankey
|
|
|
|
* Build a scan key that contains comparison data from itup
|
|
|
|
* as well as comparator routines appropriate to the key datatypes.
|
|
|
|
*
|
2000-07-21 08:42:39 +02:00
|
|
|
* The result is intended for use with _bt_compare().
|
2000-02-18 07:32:39 +01:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
ScanKey
|
1996-07-09 08:22:35 +02:00
|
|
|
_bt_mkscankey(Relation rel, IndexTuple itup)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
ScanKey skey;
|
|
|
|
TupleDesc itupdesc;
|
|
|
|
int natts;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
itupdesc = RelationGetDescr(rel);
|
2000-02-18 07:32:39 +01:00
|
|
|
natts = RelationGetNumberOfAttributes(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
|
|
|
|
|
|
|
|
for (i = 0; i < natts; i++)
|
1997-03-24 09:48:16 +01:00
|
|
|
{
|
2003-11-09 22:30:38 +01:00
|
|
|
FmgrInfo *procinfo;
|
|
|
|
Datum arg;
|
|
|
|
bool null;
|
|
|
|
|
|
|
|
/*
|
2003-11-12 22:15:59 +01:00
|
|
|
* We can use the cached (default) support procs since no cross-type
|
|
|
|
* comparison can be needed.
|
2003-11-09 22:30:38 +01:00
|
|
|
*/
|
2001-10-07 01:21:45 +02:00
|
|
|
procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
|
1997-09-07 07:04:48 +02:00
|
|
|
arg = index_getattr(itup, i + 1, itupdesc, &null);
|
2001-10-07 01:21:45 +02:00
|
|
|
ScanKeyEntryInitializeWithInfo(&skey[i],
|
2003-11-09 22:30:38 +01:00
|
|
|
null ? SK_ISNULL : 0,
|
2001-10-07 01:21:45 +02:00
|
|
|
(AttrNumber) (i + 1),
|
2003-11-09 22:30:38 +01:00
|
|
|
InvalidStrategy,
|
2003-11-12 22:15:59 +01:00
|
|
|
InvalidOid,
|
2001-10-07 01:21:45 +02:00
|
|
|
procinfo,
|
2003-11-12 22:15:59 +01:00
|
|
|
arg);
|
2000-02-18 07:32:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return skey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _bt_mkscankey_nodata
|
|
|
|
* Build a scan key that contains comparator routines appropriate to
|
2003-11-09 22:30:38 +01:00
|
|
|
* the key datatypes, but no comparison data. The comparison data
|
|
|
|
* ultimately used must match the key datatypes.
|
2000-02-18 07:32:39 +01:00
|
|
|
*
|
2000-07-21 08:42:39 +02:00
|
|
|
* The result cannot be used with _bt_compare(). Currently this
|
2003-11-09 22:30:38 +01:00
|
|
|
* routine is only called by nbtsort.c and tuplesort.c, which have
|
|
|
|
* their own comparison routines.
|
2000-02-18 07:32:39 +01:00
|
|
|
*/
|
|
|
|
ScanKey
|
|
|
|
_bt_mkscankey_nodata(Relation rel)
|
|
|
|
{
|
|
|
|
ScanKey skey;
|
2003-11-09 22:30:38 +01:00
|
|
|
TupleDesc itupdesc;
|
2000-02-18 07:32:39 +01:00
|
|
|
int natts;
|
|
|
|
int i;
|
|
|
|
|
2003-11-09 22:30:38 +01:00
|
|
|
itupdesc = RelationGetDescr(rel);
|
2000-02-18 07:32:39 +01:00
|
|
|
natts = RelationGetNumberOfAttributes(rel);
|
|
|
|
|
|
|
|
skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
|
|
|
|
|
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
2003-11-09 22:30:38 +01:00
|
|
|
FmgrInfo *procinfo;
|
|
|
|
|
|
|
|
/*
|
2003-11-12 22:15:59 +01:00
|
|
|
* We can use the cached (default) support procs since no cross-type
|
|
|
|
* comparison can be needed.
|
2003-11-09 22:30:38 +01:00
|
|
|
*/
|
2001-10-07 01:21:45 +02:00
|
|
|
procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
|
|
|
|
ScanKeyEntryInitializeWithInfo(&skey[i],
|
|
|
|
SK_ISNULL,
|
|
|
|
(AttrNumber) (i + 1),
|
2003-11-09 22:30:38 +01:00
|
|
|
InvalidStrategy,
|
2003-11-12 22:15:59 +01:00
|
|
|
InvalidOid,
|
2001-10-07 01:21:45 +02:00
|
|
|
procinfo,
|
2003-11-12 22:15:59 +01:00
|
|
|
(Datum) 0);
|
1997-03-24 09:48:16 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return skey;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-02-18 07:32:39 +01:00
|
|
|
/*
|
|
|
|
* free a scan key made by either _bt_mkscankey or _bt_mkscankey_nodata.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
|
|
|
_bt_freeskey(ScanKey skey)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
pfree(skey);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-21 21:21:00 +02:00
|
|
|
/*
|
|
|
|
* free a retracement stack made by _bt_search.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
|
|
|
_bt_freestack(BTStack stack)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
BTStack ostack;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
while (stack != (BTStack) NULL)
|
|
|
|
{
|
|
|
|
ostack = stack;
|
|
|
|
stack = stack->bts_parent;
|
|
|
|
pfree(ostack);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-21 21:21:00 +02:00
|
|
|
/*
|
|
|
|
* Construct a BTItem from a plain IndexTuple.
|
|
|
|
*
|
|
|
|
* This is now useless code, since a BTItem *is* an index tuple with
|
2001-03-22 05:01:46 +01:00
|
|
|
* no extra stuff. We hang onto it for the moment to preserve the
|
2000-07-21 21:21:00 +02:00
|
|
|
* notational distinction, in case we want to add some extra stuff
|
|
|
|
* again someday.
|
|
|
|
*/
|
|
|
|
BTItem
|
|
|
|
_bt_formitem(IndexTuple itup)
|
|
|
|
{
|
|
|
|
int nbytes_btitem;
|
|
|
|
BTItem btitem;
|
|
|
|
Size tuplen;
|
|
|
|
|
|
|
|
/* make a copy of the index tuple with room for extra stuff */
|
|
|
|
tuplen = IndexTupleSize(itup);
|
|
|
|
nbytes_btitem = tuplen + (sizeof(BTItemData) - sizeof(IndexTupleData));
|
|
|
|
|
|
|
|
btitem = (BTItem) palloc(nbytes_btitem);
|
|
|
|
memcpy((char *) &(btitem->bti_itup), (char *) itup, tuplen);
|
|
|
|
|
|
|
|
return btitem;
|
|
|
|
}
|
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/*----------
|
2003-11-12 22:15:59 +01:00
|
|
|
* _bt_preprocess_keys() -- Preprocess scan keys
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-11-12 22:15:59 +01:00
|
|
|
* The caller-supplied keys (in scan->keyData[]) are copied to
|
|
|
|
* so->keyData[] with possible transformation. scan->numberOfKeys is
|
|
|
|
* the number of input keys, so->numberOfKeys gets the number of output
|
|
|
|
* keys (possibly less, never greater).
|
2000-07-25 06:47:59 +02:00
|
|
|
*
|
2003-11-12 22:15:59 +01:00
|
|
|
* The primary purpose of this routine is to discover how many scan keys
|
|
|
|
* must be satisfied to continue the scan. It also attempts to eliminate
|
|
|
|
* redundant keys and detect contradictory keys. At present, redundant and
|
|
|
|
* contradictory keys can only be detected for same-data-type comparisons,
|
|
|
|
* but that's the usual case so it seems worth doing.
|
|
|
|
*
|
|
|
|
* The output keys must be sorted by index attribute. Presently we expect
|
|
|
|
* (but verify) that the input keys are already so sorted --- this is done
|
|
|
|
* by group_clauses_by_indexkey() in indxpath.c. Some reordering of the keys
|
|
|
|
* within each attribute may be done as a byproduct of the processing here,
|
|
|
|
* but no other code depends on that.
|
2000-07-25 06:47:59 +02:00
|
|
|
*
|
2003-11-12 22:15:59 +01:00
|
|
|
* Aside from preparing so->keyData[], this routine sets
|
|
|
|
* so->numberOfRequiredKeys to the number of quals that must be satisfied to
|
2000-07-25 06:47:59 +02:00
|
|
|
* continue the scan. _bt_checkkeys uses this. For example, if the quals
|
|
|
|
* are "x = 1 AND y < 4 AND z < 5", then _bt_checkkeys will reject a tuple
|
|
|
|
* (1,2,7), but we must continue the scan in case there are tuples (1,3,z).
|
|
|
|
* But once we reach tuples like (1,4,z) we can stop scanning because no
|
2001-03-22 05:01:46 +01:00
|
|
|
* later tuples could match. This is reflected by setting
|
2003-11-12 22:15:59 +01:00
|
|
|
* so->numberOfRequiredKeys to 2, the number of leading keys that must be
|
|
|
|
* matched to continue the scan. In general, numberOfRequiredKeys is equal
|
|
|
|
* to the number of keys for leading attributes with "=" keys, plus the
|
|
|
|
* key(s) for the first non "=" attribute, which can be seen to be correct
|
|
|
|
* by considering the above example.
|
|
|
|
*
|
|
|
|
* If possible, redundant keys are eliminated: we keep only the tightest
|
|
|
|
* >/>= bound and the tightest </<= bound, and if there's an = key then
|
|
|
|
* that's the only one returned. (So, we return either a single = key,
|
|
|
|
* or one or two boundary-condition keys for each attr.) However, we can
|
|
|
|
* only detect redundant keys when the right-hand datatypes are all equal
|
|
|
|
* to the index datatype, because we do not know suitable operators for
|
|
|
|
* comparing right-hand values of two different datatypes. (In theory
|
|
|
|
* we could handle comparison of a RHS of the index datatype with a RHS of
|
|
|
|
* another type, but that seems too much pain for too little gain.) So,
|
|
|
|
* keys whose operator has a nondefault subtype (ie, its RHS is not of the
|
|
|
|
* index datatype) are ignored here, except for noting whether they impose
|
|
|
|
* an "=" condition or not.
|
|
|
|
*
|
|
|
|
* As a byproduct of this work, we can detect contradictory quals such
|
|
|
|
* as "x = 1 AND x > 2". If we see that, we return so->quals_ok = FALSE,
|
|
|
|
* indicating the scan need not be run at all since no tuples can match.
|
|
|
|
* Again though, only keys with RHS datatype equal to the index datatype
|
|
|
|
* can be checked for contradictions.
|
2000-07-25 06:47:59 +02:00
|
|
|
*
|
2002-05-24 20:57:57 +02:00
|
|
|
* Furthermore, we detect the case where the index is unique and we have
|
2002-09-04 22:31:48 +02:00
|
|
|
* equality quals for all columns. In this case there can be at most one
|
2002-05-24 20:57:57 +02:00
|
|
|
* (visible) matching tuple. index_getnext uses this to avoid uselessly
|
|
|
|
* continuing the scan after finding one match.
|
2000-07-25 06:47:59 +02:00
|
|
|
*----------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2003-11-12 22:15:59 +01:00
|
|
|
_bt_preprocess_keys(IndexScanDesc scan)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-05-24 20:57:57 +02:00
|
|
|
Relation relation = scan->indexRelation;
|
|
|
|
BTScanOpaque so = (BTScanOpaque) scan->opaque;
|
2003-11-12 22:15:59 +01:00
|
|
|
int numberOfKeys = scan->numberOfKeys;
|
|
|
|
int new_numberOfKeys;
|
|
|
|
ScanKey inkeys;
|
|
|
|
ScanKey outkeys;
|
2000-07-25 06:47:59 +02:00
|
|
|
ScanKey cur;
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKey xform[BTMaxStrategyNumber];
|
|
|
|
bool allEqualSoFar;
|
|
|
|
bool hasOtherTypeEqual;
|
2000-05-30 06:25:00 +02:00
|
|
|
Datum test;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j;
|
2000-07-25 06:47:59 +02:00
|
|
|
AttrNumber attno;
|
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
/* initialize result variables */
|
2000-07-25 06:47:59 +02:00
|
|
|
so->qual_ok = true;
|
2003-11-12 22:15:59 +01:00
|
|
|
so->numberOfKeys = 0;
|
2000-07-25 06:47:59 +02:00
|
|
|
so->numberOfRequiredKeys = 0;
|
2002-05-24 20:57:57 +02:00
|
|
|
scan->keys_are_unique = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (numberOfKeys < 1)
|
2000-07-25 06:47:59 +02:00
|
|
|
return; /* done if qual-less scan */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
inkeys = scan->keyData;
|
|
|
|
outkeys = so->keyData;
|
|
|
|
cur = &inkeys[0];
|
|
|
|
/* we check that input keys are correctly ordered */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (cur->sk_attno != 1)
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "key(s) for attribute 1 missed");
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/* We can short-circuit most of the work if there's just one key */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (numberOfKeys == 1)
|
1997-03-18 19:41:37 +01:00
|
|
|
{
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* We don't use indices for 'A is null' and 'A is not null'
|
2000-07-25 06:47:59 +02:00
|
|
|
* currently and 'A < = > <> NULL' will always fail - so qual is
|
2003-11-12 22:15:59 +01:00
|
|
|
* not OK if comparison value is NULL. - vadim 03/21/97
|
1997-03-18 19:41:37 +01:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (cur->sk_flags & SK_ISNULL)
|
2000-07-25 06:47:59 +02:00
|
|
|
so->qual_ok = false;
|
2002-05-24 20:57:57 +02:00
|
|
|
else if (relation->rd_index->indisunique &&
|
|
|
|
relation->rd_rel->relnatts == 1)
|
|
|
|
{
|
|
|
|
/* it's a unique index, do we have an equality qual? */
|
2003-11-09 22:30:38 +01:00
|
|
|
if (cur->sk_strategy == BTEqualStrategyNumber)
|
2002-05-24 20:57:57 +02:00
|
|
|
scan->keys_are_unique = true;
|
|
|
|
}
|
2003-11-12 22:15:59 +01:00
|
|
|
memcpy(outkeys, inkeys, sizeof(ScanKeyData));
|
|
|
|
so->numberOfKeys = 1;
|
2000-07-25 06:47:59 +02:00
|
|
|
so->numberOfRequiredKeys = 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
return;
|
1997-03-18 19:41:37 +01:00
|
|
|
}
|
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/*
|
|
|
|
* Otherwise, do the full set of pushups.
|
|
|
|
*/
|
|
|
|
new_numberOfKeys = 0;
|
|
|
|
allEqualSoFar = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize for processing of keys for attr 1.
|
|
|
|
*
|
2003-11-12 22:15:59 +01:00
|
|
|
* xform[i] points to the currently best scan key of strategy type i+1,
|
|
|
|
* if any is found with a default operator subtype; it is NULL if we
|
|
|
|
* haven't yet found such a key for this attr. Scan keys of nondefault
|
|
|
|
* subtypes are transferred to the output with no processing except for
|
|
|
|
* noting if they are of "=" type.
|
2000-07-25 06:47:59 +02:00
|
|
|
*/
|
|
|
|
attno = 1;
|
2003-11-12 22:15:59 +01:00
|
|
|
memset(xform, 0, sizeof(xform));
|
|
|
|
hasOtherTypeEqual = false;
|
2000-07-25 06:47:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop iterates from 0 to numberOfKeys inclusive; we use the last
|
|
|
|
* pass to handle after-last-key processing. Actual exit from the
|
|
|
|
* loop is at the "break" statement below.
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
for (i = 0;; cur++, i++)
|
1997-03-18 19:41:37 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (i < numberOfKeys)
|
2000-07-25 06:47:59 +02:00
|
|
|
{
|
|
|
|
/* See comments above: any NULL implies cannot match qual */
|
|
|
|
if (cur->sk_flags & SK_ISNULL)
|
|
|
|
{
|
|
|
|
so->qual_ok = false;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Quit processing so we don't try to invoke comparison
|
2000-07-25 06:47:59 +02:00
|
|
|
* routines on NULLs.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* If we are at the end of the keys for a particular attr, finish
|
|
|
|
* up processing and emit the cleaned-up keys.
|
2000-07-25 06:47:59 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (i == numberOfKeys || cur->sk_attno != attno)
|
|
|
|
{
|
2000-07-25 06:47:59 +02:00
|
|
|
bool priorAllEqualSoFar = allEqualSoFar;
|
|
|
|
|
|
|
|
/* check input keys are correctly ordered */
|
|
|
|
if (i < numberOfKeys && cur->sk_attno != attno + 1)
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "key(s) for attribute %d missed", attno + 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If = has been specified, no other key will be used. In case
|
2000-07-25 06:47:59 +02:00
|
|
|
* of key > 2 && key == 1 and so on we have to set qual_ok to
|
|
|
|
* false before discarding the other keys.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
if (xform[BTEqualStrategyNumber - 1])
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKey eq = xform[BTEqualStrategyNumber - 1];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (j = BTMaxStrategyNumber; --j >= 0;)
|
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKey chk = xform[j];
|
|
|
|
|
|
|
|
if (!chk || j == (BTEqualStrategyNumber - 1))
|
1997-09-07 07:04:48 +02:00
|
|
|
continue;
|
2003-11-09 22:30:38 +01:00
|
|
|
test = FunctionCall2(&chk->sk_func,
|
|
|
|
eq->sk_argument,
|
|
|
|
chk->sk_argument);
|
2000-05-30 06:25:00 +02:00
|
|
|
if (!DatumGetBool(test))
|
2003-11-12 22:15:59 +01:00
|
|
|
{
|
2000-07-25 06:47:59 +02:00
|
|
|
so->qual_ok = false;
|
2003-11-12 22:15:59 +01:00
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[BTLessStrategyNumber - 1] = NULL;
|
|
|
|
xform[BTLessEqualStrategyNumber - 1] = NULL;
|
|
|
|
xform[BTGreaterEqualStrategyNumber - 1] = NULL;
|
|
|
|
xform[BTGreaterStrategyNumber - 1] = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-09-27 20:20:21 +02:00
|
|
|
else
|
2000-07-25 06:47:59 +02:00
|
|
|
{
|
|
|
|
/*
|
2003-11-12 22:15:59 +01:00
|
|
|
* If no "=" for this key, we're done with required keys
|
2000-07-25 06:47:59 +02:00
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
if (! hasOtherTypeEqual)
|
|
|
|
allEqualSoFar = false;
|
2000-07-25 06:47:59 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/* keep only one of <, <= */
|
2003-11-12 22:15:59 +01:00
|
|
|
if (xform[BTLessStrategyNumber - 1]
|
|
|
|
&& xform[BTLessEqualStrategyNumber - 1])
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKey lt = xform[BTLessStrategyNumber - 1];
|
|
|
|
ScanKey le = xform[BTLessEqualStrategyNumber - 1];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-09 22:30:38 +01:00
|
|
|
test = FunctionCall2(&le->sk_func,
|
|
|
|
lt->sk_argument,
|
|
|
|
le->sk_argument);
|
2000-05-30 06:25:00 +02:00
|
|
|
if (DatumGetBool(test))
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[BTLessEqualStrategyNumber - 1] = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[BTLessStrategyNumber - 1] = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/* keep only one of >, >= */
|
2003-11-12 22:15:59 +01:00
|
|
|
if (xform[BTGreaterStrategyNumber - 1]
|
|
|
|
&& xform[BTGreaterEqualStrategyNumber - 1])
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKey gt = xform[BTGreaterStrategyNumber - 1];
|
|
|
|
ScanKey ge = xform[BTGreaterEqualStrategyNumber - 1];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-09 22:30:38 +01:00
|
|
|
test = FunctionCall2(&ge->sk_func,
|
|
|
|
gt->sk_argument,
|
|
|
|
ge->sk_argument);
|
2000-05-30 06:25:00 +02:00
|
|
|
if (DatumGetBool(test))
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[BTGreaterEqualStrategyNumber - 1] = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[BTGreaterStrategyNumber - 1] = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/*
|
2003-11-12 22:15:59 +01:00
|
|
|
* Emit the cleaned-up keys into the outkeys[] array.
|
2000-07-25 06:47:59 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
for (j = BTMaxStrategyNumber; --j >= 0;)
|
2000-07-25 06:47:59 +02:00
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
if (xform[j])
|
|
|
|
memcpy(&outkeys[new_numberOfKeys++], xform[j],
|
2000-07-25 06:47:59 +02:00
|
|
|
sizeof(ScanKeyData));
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/*
|
|
|
|
* If all attrs before this one had "=", include these keys
|
|
|
|
* into the required-keys count.
|
|
|
|
*/
|
|
|
|
if (priorAllEqualSoFar)
|
|
|
|
so->numberOfRequiredKeys = new_numberOfKeys;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/*
|
|
|
|
* Exit loop here if done.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (i == numberOfKeys)
|
|
|
|
break;
|
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/* Re-initialize for new attno */
|
1997-09-07 07:04:48 +02:00
|
|
|
attno = cur->sk_attno;
|
2003-11-12 22:15:59 +01:00
|
|
|
memset(xform, 0, sizeof(xform));
|
|
|
|
hasOtherTypeEqual = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
/* check strategy this key's operator corresponds to */
|
2003-11-09 22:30:38 +01:00
|
|
|
j = cur->sk_strategy - 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
/* if wrong RHS data type, punt */
|
|
|
|
if (cur->sk_subtype != InvalidOid)
|
|
|
|
{
|
|
|
|
memcpy(&outkeys[new_numberOfKeys++], cur,
|
|
|
|
sizeof(ScanKeyData));
|
|
|
|
if (j == (BTEqualStrategyNumber - 1))
|
|
|
|
hasOtherTypeEqual = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* have we seen one of these before? */
|
2003-11-12 22:15:59 +01:00
|
|
|
if (xform[j])
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
/* yup, keep the more restrictive key */
|
2000-05-30 06:25:00 +02:00
|
|
|
test = FunctionCall2(&cur->sk_func,
|
2000-07-25 06:47:59 +02:00
|
|
|
cur->sk_argument,
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[j]->sk_argument);
|
2000-05-30 06:25:00 +02:00
|
|
|
if (DatumGetBool(test))
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[j] = cur;
|
1997-09-07 07:04:48 +02:00
|
|
|
else if (j == (BTEqualStrategyNumber - 1))
|
2003-11-12 22:15:59 +01:00
|
|
|
{
|
|
|
|
/* key == a && key == b, but a != b */
|
2001-03-23 05:49:58 +01:00
|
|
|
so->qual_ok = false;
|
2003-11-12 22:15:59 +01:00
|
|
|
return;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-07-25 06:47:59 +02:00
|
|
|
/* nope, so remember this scankey */
|
2003-11-12 22:15:59 +01:00
|
|
|
xform[j] = cur;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
so->numberOfKeys = new_numberOfKeys;
|
2002-05-24 20:57:57 +02:00
|
|
|
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* If unique index and we have equality keys for all columns, set
|
|
|
|
* keys_are_unique flag for higher levels.
|
2002-05-24 20:57:57 +02:00
|
|
|
*/
|
|
|
|
if (allEqualSoFar && relation->rd_index->indisunique &&
|
|
|
|
relation->rd_rel->relnatts == new_numberOfKeys)
|
|
|
|
scan->keys_are_unique = true;
|
|
|
|
}
|
|
|
|
|
2000-07-21 21:21:00 +02:00
|
|
|
/*
|
2000-07-25 06:47:59 +02:00
|
|
|
* Test whether an indextuple satisfies all the scankey conditions.
|
2000-07-21 21:21:00 +02:00
|
|
|
*
|
2000-07-25 06:47:59 +02:00
|
|
|
* If the tuple fails to pass the qual, we also determine whether there's
|
|
|
|
* any need to continue the scan beyond this tuple, and set *continuescan
|
2003-11-12 22:15:59 +01:00
|
|
|
* accordingly. See comments for _bt_preprocess_keys(), above, about how
|
|
|
|
* this is done.
|
2000-07-21 21:21:00 +02:00
|
|
|
*/
|
1997-03-24 09:48:16 +01:00
|
|
|
bool
|
2000-07-25 06:47:59 +02:00
|
|
|
_bt_checkkeys(IndexScanDesc scan, IndexTuple tuple,
|
|
|
|
ScanDirection dir, bool *continuescan)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
BTScanOpaque so = (BTScanOpaque) scan->opaque;
|
2002-05-21 01:51:44 +02:00
|
|
|
int keysz = so->numberOfKeys;
|
2003-11-12 22:15:59 +01:00
|
|
|
int ikey;
|
1997-09-08 04:41:22 +02:00
|
|
|
TupleDesc tupdesc;
|
|
|
|
ScanKey key;
|
2000-07-25 06:47:59 +02:00
|
|
|
|
|
|
|
*continuescan = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/* If no keys, always scan the whole index */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (keysz == 0)
|
1998-09-01 05:29:17 +02:00
|
|
|
return true;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
IncrIndexProcessed();
|
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
tupdesc = RelationGetDescr(scan->indexRelation);
|
|
|
|
|
|
|
|
for (key = so->keyData, ikey = 0; ikey < keysz; key++, ikey++)
|
1997-03-24 09:48:16 +01:00
|
|
|
{
|
2000-07-25 06:47:59 +02:00
|
|
|
Datum datum;
|
|
|
|
bool isNull;
|
|
|
|
Datum test;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
datum = index_getattr(tuple,
|
2000-07-25 06:47:59 +02:00
|
|
|
key->sk_attno,
|
1997-09-07 07:04:48 +02:00
|
|
|
tupdesc,
|
|
|
|
&isNull);
|
1997-03-24 09:48:16 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* btree doesn't support 'A is null' clauses, yet */
|
2000-07-25 06:47:59 +02:00
|
|
|
if (key->sk_flags & SK_ISNULL)
|
1999-04-13 19:18:29 +02:00
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
/* we shouldn't get here, really; see _bt_preprocess_keys() */
|
2000-07-25 06:47:59 +02:00
|
|
|
*continuescan = false;
|
1999-04-13 19:18:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
if (isNull)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-07-25 06:47:59 +02:00
|
|
|
/*
|
|
|
|
* Since NULLs are sorted after non-NULLs, we know we have
|
|
|
|
* reached the upper limit of the range of values for this
|
2001-03-22 05:01:46 +01:00
|
|
|
* index attr. On a forward scan, we can stop if this qual is
|
|
|
|
* one of the "must match" subset. On a backward scan,
|
2000-07-25 06:47:59 +02:00
|
|
|
* however, we should keep going.
|
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
if (ikey < so->numberOfRequiredKeys &&
|
2000-07-25 06:47:59 +02:00
|
|
|
ScanDirectionIsForward(dir))
|
|
|
|
*continuescan = false;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/*
|
|
|
|
* In any case, this indextuple doesn't match the qual.
|
|
|
|
*/
|
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-07-25 06:47:59 +02:00
|
|
|
|
2003-11-09 22:30:38 +01:00
|
|
|
test = FunctionCall2(&key->sk_func, datum, key->sk_argument);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-09 22:30:38 +01:00
|
|
|
if (!DatumGetBool(test))
|
2000-07-25 06:47:59 +02:00
|
|
|
{
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Tuple fails this qual. If it's a required qual, then we
|
2003-11-12 22:15:59 +01:00
|
|
|
* may be able to conclude no further tuples will pass, either.
|
|
|
|
* We have to look at the scan direction and the qual type.
|
|
|
|
*
|
|
|
|
* Note: the only case in which we would keep going after failing
|
|
|
|
* a required qual is if there are partially-redundant quals that
|
|
|
|
* _bt_preprocess_keys() was unable to eliminate. For example,
|
|
|
|
* given "x > 4 AND x > 10" where both are cross-type comparisons
|
|
|
|
* and so not removable, we might start the scan at the x = 4
|
|
|
|
* boundary point. The "x > 10" condition will fail until we
|
|
|
|
* pass x = 10, but we must not stop the scan on its account.
|
|
|
|
*
|
|
|
|
* Note: because we stop the scan as soon as any required equality
|
|
|
|
* qual fails, it is critical that equality quals be used for the
|
|
|
|
* initial positioning in _bt_first() when they are available.
|
|
|
|
* See comments in _bt_first().
|
|
|
|
*/
|
|
|
|
if (ikey < so->numberOfRequiredKeys)
|
|
|
|
{
|
|
|
|
switch (key->sk_strategy)
|
|
|
|
{
|
|
|
|
case BTLessStrategyNumber:
|
|
|
|
case BTLessEqualStrategyNumber:
|
|
|
|
if (ScanDirectionIsForward(dir))
|
|
|
|
*continuescan = false;
|
|
|
|
break;
|
|
|
|
case BTEqualStrategyNumber:
|
|
|
|
*continuescan = false;
|
|
|
|
break;
|
|
|
|
case BTGreaterEqualStrategyNumber:
|
|
|
|
case BTGreaterStrategyNumber:
|
|
|
|
if (ScanDirectionIsBackward(dir))
|
|
|
|
*continuescan = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized StrategyNumber: %d",
|
|
|
|
key->sk_strategy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In any case, this indextuple doesn't match the qual.
|
2000-07-25 06:47:59 +02:00
|
|
|
*/
|
1998-09-01 05:29:17 +02:00
|
|
|
return false;
|
2000-07-25 06:47:59 +02:00
|
|
|
}
|
1997-03-24 09:48:16 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-25 06:47:59 +02:00
|
|
|
/* If we get here, the tuple passes all quals. */
|
1998-09-01 05:29:17 +02:00
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|