1 /*-------------------------------------------------------------------------
4 * Utility code for Postgres btree implementation.
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtutils.c,v 1.53 2003/07/28 00:09:14 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/genam.h"
19 #include "access/istrat.h"
20 #include "access/nbtree.h"
21 #include "catalog/catalog.h"
22 #include "executor/execdebug.h"
25 static int _bt_getstrategynumber(RegProcedure sk_procedure, StrategyMap map);
30 * Build a scan key that contains comparison data from itup
31 * as well as comparator routines appropriate to the key datatypes.
33 * The result is intended for use with _bt_compare().
36 _bt_mkscankey(Relation rel, IndexTuple itup)
47 itupdesc = RelationGetDescr(rel);
48 natts = RelationGetNumberOfAttributes(rel);
50 skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
52 for (i = 0; i < natts; i++)
54 procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
55 arg = index_getattr(itup, i + 1, itupdesc, &null);
56 flag = null ? SK_ISNULL : 0x0;
57 ScanKeyEntryInitializeWithInfo(&skey[i],
69 * _bt_mkscankey_nodata
70 * Build a scan key that contains comparator routines appropriate to
71 * the key datatypes, but no comparison data.
73 * The result cannot be used with _bt_compare(). Currently this
74 * routine is only called by utils/sort/tuplesort.c, which has its
75 * own comparison routine.
78 _bt_mkscankey_nodata(Relation rel)
85 natts = RelationGetNumberOfAttributes(rel);
87 skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
89 for (i = 0; i < natts; i++)
91 procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
92 ScanKeyEntryInitializeWithInfo(&skey[i],
104 * free a scan key made by either _bt_mkscankey or _bt_mkscankey_nodata.
107 _bt_freeskey(ScanKey skey)
113 * free a retracement stack made by _bt_search.
116 _bt_freestack(BTStack stack)
120 while (stack != (BTStack) NULL)
123 stack = stack->bts_parent;
129 * Construct a BTItem from a plain IndexTuple.
131 * This is now useless code, since a BTItem *is* an index tuple with
132 * no extra stuff. We hang onto it for the moment to preserve the
133 * notational distinction, in case we want to add some extra stuff
137 _bt_formitem(IndexTuple itup)
143 /* make a copy of the index tuple with room for extra stuff */
144 tuplen = IndexTupleSize(itup);
145 nbytes_btitem = tuplen + (sizeof(BTItemData) - sizeof(IndexTupleData));
147 btitem = (BTItem) palloc(nbytes_btitem);
148 memcpy((char *) &(btitem->bti_itup), (char *) itup, tuplen);
154 * _bt_orderkeys() -- Put keys in a sensible order for conjunctive quals.
156 * After this routine runs, the scan keys are ordered by index attribute
157 * (all quals for attr 1, then all for attr 2, etc) and within each attr
158 * the keys are ordered by constraint type: ">", ">=", "=", "<=", "<".
159 * Furthermore, redundant keys are eliminated: we keep only the tightest
160 * >/>= bound and the tightest </<= bound, and if there's an = key then
161 * that's the only one returned. (So, we return either a single = key,
162 * or one or two boundary-condition keys for each attr.)
164 * As a byproduct of this work, we can detect contradictory quals such
165 * as "x = 1 AND x > 2". If we see that, we return so->quals_ok = FALSE,
166 * indicating the scan need not be run at all since no tuples can match.
168 * Another byproduct is to determine how many quals must be satisfied to
169 * continue the scan. _bt_checkkeys uses this. For example, if the quals
170 * are "x = 1 AND y < 4 AND z < 5", then _bt_checkkeys will reject a tuple
171 * (1,2,7), but we must continue the scan in case there are tuples (1,3,z).
172 * But once we reach tuples like (1,4,z) we can stop scanning because no
173 * later tuples could match. This is reflected by setting
174 * so->numberOfRequiredKeys to the number of leading keys that must be
175 * matched to continue the scan. numberOfRequiredKeys is equal to the
176 * number of leading "=" keys plus the key(s) for the first non "="
177 * attribute, which can be seen to be correct by considering the above
180 * Furthermore, we detect the case where the index is unique and we have
181 * equality quals for all columns. In this case there can be at most one
182 * (visible) matching tuple. index_getnext uses this to avoid uselessly
183 * continuing the scan after finding one match.
185 * The initial ordering of the keys is expected to be by attribute already
186 * (see group_clauses_by_indexkey() in indxpath.c). The task here is to
187 * standardize the appearance of multiple keys for the same attribute.
189 * XXX this routine is one of many places that fail to handle SK_COMMUTE
190 * scankeys properly. Currently, the planner is careful never to generate
191 * any indexquals that would require SK_COMMUTE to be set. Someday we ought
192 * to try to fix this, though it's not real critical as long as indexable
193 * operators all have commutators...
195 * Note: this routine invokes comparison operators via OidFunctionCallN,
196 * ie, without caching function lookups. No point in trying to be smarter,
197 * since these comparisons are executed only when the user expresses a
198 * hokey qualification, and happen only once per scan anyway.
202 _bt_orderkeys(IndexScanDesc scan)
204 Relation relation = scan->indexRelation;
205 BTScanOpaque so = (BTScanOpaque) scan->opaque;
206 ScanKeyData xform[BTMaxStrategyNumber];
207 bool init[BTMaxStrategyNumber];
208 int numberOfKeys = so->numberOfKeys;
216 int new_numberOfKeys;
220 so->numberOfRequiredKeys = 0;
221 scan->keys_are_unique = false;
223 if (numberOfKeys < 1)
224 return; /* done if qual-less scan */
228 /* check input keys are correctly ordered */
229 if (cur->sk_attno != 1)
230 elog(ERROR, "key(s) for attribute 1 missed");
232 /* We can short-circuit most of the work if there's just one key */
233 if (numberOfKeys == 1)
236 * We don't use indices for 'A is null' and 'A is not null'
237 * currently and 'A < = > <> NULL' will always fail - so qual is
238 * not Ok if comparison value is NULL. - vadim 03/21/97
240 if (cur->sk_flags & SK_ISNULL)
242 else if (relation->rd_index->indisunique &&
243 relation->rd_rel->relnatts == 1)
245 /* it's a unique index, do we have an equality qual? */
246 map = IndexStrategyGetStrategyMap(RelationGetIndexStrategy(relation),
249 j = _bt_getstrategynumber(cur->sk_procedure, map);
250 if (j == (BTEqualStrategyNumber - 1))
251 scan->keys_are_unique = true;
253 so->numberOfRequiredKeys = 1;
258 * Otherwise, do the full set of pushups.
260 new_numberOfKeys = 0;
261 allEqualSoFar = true;
264 * Initialize for processing of keys for attr 1.
266 * xform[i] holds a copy of the current scan key of strategy type i+1, if
267 * any; init[i] is TRUE if we have found such a key for this attr.
270 map = IndexStrategyGetStrategyMap(RelationGetIndexStrategy(relation),
273 MemSet(xform, 0, sizeof(xform)); /* not really necessary */
274 MemSet(init, 0, sizeof(init));
277 * Loop iterates from 0 to numberOfKeys inclusive; we use the last
278 * pass to handle after-last-key processing. Actual exit from the
279 * loop is at the "break" statement below.
281 for (i = 0;; cur++, i++)
283 if (i < numberOfKeys)
285 /* See comments above: any NULL implies cannot match qual */
286 if (cur->sk_flags & SK_ISNULL)
291 * Quit processing so we don't try to invoke comparison
299 * If we are at the end of the keys for a particular attr, finish
300 * up processing and emit the cleaned-up keys.
302 if (i == numberOfKeys || cur->sk_attno != attno)
304 bool priorAllEqualSoFar = allEqualSoFar;
306 /* check input keys are correctly ordered */
307 if (i < numberOfKeys && cur->sk_attno != attno + 1)
308 elog(ERROR, "key(s) for attribute %d missed", attno + 1);
311 * If = has been specified, no other key will be used. In case
312 * of key > 2 && key == 1 and so on we have to set qual_ok to
313 * false before discarding the other keys.
315 if (init[BTEqualStrategyNumber - 1])
320 eq = &xform[BTEqualStrategyNumber - 1];
321 for (j = BTMaxStrategyNumber; --j >= 0;)
324 j == (BTEqualStrategyNumber - 1))
327 test = OidFunctionCall2(chk->sk_procedure,
330 if (!DatumGetBool(test))
333 init[BTLessStrategyNumber - 1] = false;
334 init[BTLessEqualStrategyNumber - 1] = false;
335 init[BTGreaterEqualStrategyNumber - 1] = false;
336 init[BTGreaterStrategyNumber - 1] = false;
341 * No "=" for this key, so we're done with required keys
343 allEqualSoFar = false;
346 /* keep only one of <, <= */
347 if (init[BTLessStrategyNumber - 1]
348 && init[BTLessEqualStrategyNumber - 1])
350 ScanKeyData *lt = &xform[BTLessStrategyNumber - 1];
351 ScanKeyData *le = &xform[BTLessEqualStrategyNumber - 1];
353 test = OidFunctionCall2(le->sk_procedure,
356 if (DatumGetBool(test))
357 init[BTLessEqualStrategyNumber - 1] = false;
359 init[BTLessStrategyNumber - 1] = false;
362 /* keep only one of >, >= */
363 if (init[BTGreaterStrategyNumber - 1]
364 && init[BTGreaterEqualStrategyNumber - 1])
366 ScanKeyData *gt = &xform[BTGreaterStrategyNumber - 1];
367 ScanKeyData *ge = &xform[BTGreaterEqualStrategyNumber - 1];
369 test = OidFunctionCall2(ge->sk_procedure,
372 if (DatumGetBool(test))
373 init[BTGreaterEqualStrategyNumber - 1] = false;
375 init[BTGreaterStrategyNumber - 1] = false;
379 * Emit the cleaned-up keys back into the key[] array in the
380 * correct order. Note we are overwriting our input here!
381 * It's OK because (a) xform[] is a physical copy of the keys
382 * we want, (b) we cannot emit more keys than we input, so we
383 * won't overwrite as-yet-unprocessed keys.
385 for (j = BTMaxStrategyNumber; --j >= 0;)
388 memcpy(&key[new_numberOfKeys++], &xform[j],
389 sizeof(ScanKeyData));
393 * If all attrs before this one had "=", include these keys
394 * into the required-keys count.
396 if (priorAllEqualSoFar)
397 so->numberOfRequiredKeys = new_numberOfKeys;
400 * Exit loop here if done.
402 if (i == numberOfKeys)
405 /* Re-initialize for new attno */
406 attno = cur->sk_attno;
407 map = IndexStrategyGetStrategyMap(RelationGetIndexStrategy(relation),
410 MemSet(xform, 0, sizeof(xform)); /* not really necessary */
411 MemSet(init, 0, sizeof(init));
414 /* figure out which strategy this key's operator corresponds to */
415 j = _bt_getstrategynumber(cur->sk_procedure, map);
417 /* have we seen one of these before? */
420 /* yup, keep the more restrictive value */
421 test = FunctionCall2(&cur->sk_func,
423 xform[j].sk_argument);
424 if (DatumGetBool(test))
425 xform[j].sk_argument = cur->sk_argument;
426 else if (j == (BTEqualStrategyNumber - 1))
428 /* key == a && key == b, but a != b */
432 /* nope, so remember this scankey */
433 memcpy(&xform[j], cur, sizeof(ScanKeyData));
438 so->numberOfKeys = new_numberOfKeys;
441 * If unique index and we have equality keys for all columns, set
442 * keys_are_unique flag for higher levels.
444 if (allEqualSoFar && relation->rd_index->indisunique &&
445 relation->rd_rel->relnatts == new_numberOfKeys)
446 scan->keys_are_unique = true;
450 * Determine which btree strategy an operator procedure matches.
452 * Result is strategy number minus 1.
455 _bt_getstrategynumber(RegProcedure sk_procedure, StrategyMap map)
459 for (j = BTMaxStrategyNumber; --j >= 0;)
461 if (sk_procedure == map->entry[j].sk_procedure)
464 elog(ERROR, "could not identify operator %u", sk_procedure);
465 return -1; /* keep compiler quiet */
469 * Test whether an indextuple satisfies all the scankey conditions.
471 * If the tuple fails to pass the qual, we also determine whether there's
472 * any need to continue the scan beyond this tuple, and set *continuescan
473 * accordingly. See comments for _bt_orderkeys(), above, about how this is
477 _bt_checkkeys(IndexScanDesc scan, IndexTuple tuple,
478 ScanDirection dir, bool *continuescan)
480 BTScanOpaque so = (BTScanOpaque) scan->opaque;
481 int keysz = so->numberOfKeys;
486 *continuescan = true;
488 /* If no keys, always scan the whole index */
492 tupdesc = RelationGetDescr(scan->indexRelation);
496 IncrIndexProcessed();
504 datum = index_getattr(tuple,
509 /* btree doesn't support 'A is null' clauses, yet */
510 if (key->sk_flags & SK_ISNULL)
512 /* we shouldn't get here, really; see _bt_orderkeys() */
513 *continuescan = false;
520 * Since NULLs are sorted after non-NULLs, we know we have
521 * reached the upper limit of the range of values for this
522 * index attr. On a forward scan, we can stop if this qual is
523 * one of the "must match" subset. On a backward scan,
524 * however, we should keep going.
526 if (keysok < so->numberOfRequiredKeys &&
527 ScanDirectionIsForward(dir))
528 *continuescan = false;
531 * In any case, this indextuple doesn't match the qual.
536 if (key->sk_flags & SK_COMMUTE)
537 test = FunctionCall2(&key->sk_func,
538 key->sk_argument, datum);
540 test = FunctionCall2(&key->sk_func,
541 datum, key->sk_argument);
543 if (DatumGetBool(test) == !!(key->sk_flags & SK_NEGATE))
546 * Tuple fails this qual. If it's a required qual, then we
547 * can conclude no further tuples will pass, either.
549 if (keysok < so->numberOfRequiredKeys)
550 *continuescan = false;
559 /* If we get here, the tuple passes all quals. */