1 /*-------------------------------------------------------------------------
4 * Utility code for Postgres btree implementation.
6 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/access/nbtree/nbtutils.c
13 *-------------------------------------------------------------------------
20 #include "access/nbtree.h"
21 #include "access/reloptions.h"
22 #include "access/relscan.h"
23 #include "miscadmin.h"
24 #include "utils/array.h"
25 #include "utils/lsyscache.h"
26 #include "utils/memutils.h"
27 #include "utils/rel.h"
30 typedef struct BTSortArrayContext
37 static Datum _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey,
39 Datum *elems, int nelems);
40 static int _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey,
42 Datum *elems, int nelems);
43 static int _bt_compare_array_elements(const void *a, const void *b, void *arg);
44 static bool _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
45 ScanKey leftarg, ScanKey rightarg,
47 static bool _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption);
48 static void _bt_mark_scankey_required(ScanKey skey);
49 static bool _bt_check_rowcompare(ScanKey skey,
50 IndexTuple tuple, TupleDesc tupdesc,
51 ScanDirection dir, bool *continuescan);
56 * Build an insertion scan key that contains comparison data from itup
57 * as well as comparator routines appropriate to the key datatypes.
59 * The result is intended for use with _bt_compare().
62 _bt_mkscankey(Relation rel, IndexTuple itup)
66 int indnatts PG_USED_FOR_ASSERTS_ONLY;
71 itupdesc = RelationGetDescr(rel);
72 indnatts = IndexRelationGetNumberOfAttributes(rel);
73 indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
74 indoption = rel->rd_indoption;
76 Assert(indnkeyatts > 0);
77 Assert(indnkeyatts <= indnatts);
78 Assert(BTreeTupleGetNAtts(itup, rel) == indnatts ||
79 BTreeTupleGetNAtts(itup, rel) == indnkeyatts);
82 * We'll execute search using scan key constructed on key columns. Non-key
83 * (INCLUDE index) columns are always omitted from scan keys.
85 skey = (ScanKey) palloc(indnkeyatts * sizeof(ScanKeyData));
87 for (i = 0; i < indnkeyatts; i++)
95 * We can use the cached (default) support procs since no cross-type
96 * comparison can be needed.
98 procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
99 arg = index_getattr(itup, i + 1, itupdesc, &null);
100 flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
101 ScanKeyEntryInitializeWithInfo(&skey[i],
103 (AttrNumber) (i + 1),
106 rel->rd_indcollation[i],
115 * _bt_mkscankey_nodata
116 * Build an insertion scan key that contains 3-way comparator routines
117 * appropriate to the key datatypes, but no comparison data. The
118 * comparison data ultimately used must match the key datatypes.
120 * The result cannot be used with _bt_compare(), unless comparison
121 * data is first stored into the key entries. Currently this
122 * routine is only called by nbtsort.c and tuplesort.c, which have
123 * their own comparison routines.
126 _bt_mkscankey_nodata(Relation rel)
133 indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
134 indoption = rel->rd_indoption;
136 skey = (ScanKey) palloc(indnkeyatts * sizeof(ScanKeyData));
138 for (i = 0; i < indnkeyatts; i++)
144 * We can use the cached (default) support procs since no cross-type
145 * comparison can be needed.
147 procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
148 flags = SK_ISNULL | (indoption[i] << SK_BT_INDOPTION_SHIFT);
149 ScanKeyEntryInitializeWithInfo(&skey[i],
151 (AttrNumber) (i + 1),
154 rel->rd_indcollation[i],
163 * free a scan key made by either _bt_mkscankey or _bt_mkscankey_nodata.
166 _bt_freeskey(ScanKey skey)
172 * free a retracement stack made by _bt_search.
175 _bt_freestack(BTStack stack)
179 while (stack != NULL)
182 stack = stack->bts_parent;
189 * _bt_preprocess_array_keys() -- Preprocess SK_SEARCHARRAY scan keys
191 * If there are any SK_SEARCHARRAY scan keys, deconstruct the array(s) and
192 * set up BTArrayKeyInfo info for each one that is an equality-type key.
193 * Prepare modified scan keys in so->arrayKeyData, which will hold the current
194 * array elements during each primitive indexscan operation. For inequality
195 * array keys, it's sufficient to find the extreme element value and replace
196 * the whole array with that scalar value.
198 * Note: the reason we need so->arrayKeyData, rather than just scribbling
199 * on scan->keyData, is that callers are permitted to call btrescan without
200 * supplying a new set of scankey data.
203 _bt_preprocess_array_keys(IndexScanDesc scan)
205 BTScanOpaque so = (BTScanOpaque) scan->opaque;
206 int numberOfKeys = scan->numberOfKeys;
207 int16 *indoption = scan->indexRelation->rd_indoption;
211 MemoryContext oldContext;
213 /* Quick check to see if there are any array keys */
215 for (i = 0; i < numberOfKeys; i++)
217 cur = &scan->keyData[i];
218 if (cur->sk_flags & SK_SEARCHARRAY)
221 Assert(!(cur->sk_flags & (SK_ROW_HEADER | SK_SEARCHNULL | SK_SEARCHNOTNULL)));
222 /* If any arrays are null as a whole, we can quit right now. */
223 if (cur->sk_flags & SK_ISNULL)
225 so->numArrayKeys = -1;
226 so->arrayKeyData = NULL;
232 /* Quit if nothing to do. */
233 if (numArrayKeys == 0)
235 so->numArrayKeys = 0;
236 so->arrayKeyData = NULL;
241 * Make a scan-lifespan context to hold array-associated data, or reset it
242 * if we already have one from a previous rescan cycle.
244 if (so->arrayContext == NULL)
245 so->arrayContext = AllocSetContextCreate(CurrentMemoryContext,
246 "BTree array context",
247 ALLOCSET_SMALL_SIZES);
249 MemoryContextReset(so->arrayContext);
251 oldContext = MemoryContextSwitchTo(so->arrayContext);
253 /* Create modifiable copy of scan->keyData in the workspace context */
254 so->arrayKeyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
255 memcpy(so->arrayKeyData,
257 scan->numberOfKeys * sizeof(ScanKeyData));
259 /* Allocate space for per-array data in the workspace context */
260 so->arrayKeys = (BTArrayKeyInfo *) palloc0(numArrayKeys * sizeof(BTArrayKeyInfo));
262 /* Now process each array key */
264 for (i = 0; i < numberOfKeys; i++)
276 cur = &so->arrayKeyData[i];
277 if (!(cur->sk_flags & SK_SEARCHARRAY))
281 * First, deconstruct the array into elements. Anything allocated
282 * here (including a possibly detoasted array value) is in the
285 arrayval = DatumGetArrayTypeP(cur->sk_argument);
286 /* We could cache this data, but not clear it's worth it */
287 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
288 &elmlen, &elmbyval, &elmalign);
289 deconstruct_array(arrayval,
290 ARR_ELEMTYPE(arrayval),
291 elmlen, elmbyval, elmalign,
292 &elem_values, &elem_nulls, &num_elems);
295 * Compress out any null elements. We can ignore them since we assume
296 * all btree operators are strict.
299 for (j = 0; j < num_elems; j++)
302 elem_values[num_nonnulls++] = elem_values[j];
305 /* We could pfree(elem_nulls) now, but not worth the cycles */
307 /* If there's no non-nulls, the scan qual is unsatisfiable */
308 if (num_nonnulls == 0)
315 * If the comparison operator is not equality, then the array qual
316 * degenerates to a simple comparison against the smallest or largest
317 * non-null array element, as appropriate.
319 switch (cur->sk_strategy)
321 case BTLessStrategyNumber:
322 case BTLessEqualStrategyNumber:
324 _bt_find_extreme_element(scan, cur,
325 BTGreaterStrategyNumber,
326 elem_values, num_nonnulls);
328 case BTEqualStrategyNumber:
329 /* proceed with rest of loop */
331 case BTGreaterEqualStrategyNumber:
332 case BTGreaterStrategyNumber:
334 _bt_find_extreme_element(scan, cur,
335 BTLessStrategyNumber,
336 elem_values, num_nonnulls);
339 elog(ERROR, "unrecognized StrategyNumber: %d",
340 (int) cur->sk_strategy);
345 * Sort the non-null elements and eliminate any duplicates. We must
346 * sort in the same ordering used by the index column, so that the
347 * successive primitive indexscans produce data in index order.
349 num_elems = _bt_sort_array_elements(scan, cur,
350 (indoption[cur->sk_attno - 1] & INDOPTION_DESC) != 0,
351 elem_values, num_nonnulls);
354 * And set up the BTArrayKeyInfo data.
356 so->arrayKeys[numArrayKeys].scan_key = i;
357 so->arrayKeys[numArrayKeys].num_elems = num_elems;
358 so->arrayKeys[numArrayKeys].elem_values = elem_values;
362 so->numArrayKeys = numArrayKeys;
364 MemoryContextSwitchTo(oldContext);
368 * _bt_find_extreme_element() -- get least or greatest array element
370 * scan and skey identify the index column, whose opfamily determines the
371 * comparison semantics. strat should be BTLessStrategyNumber to get the
372 * least element, or BTGreaterStrategyNumber to get the greatest.
375 _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey,
376 StrategyNumber strat,
377 Datum *elems, int nelems)
379 Relation rel = scan->indexRelation;
382 RegProcedure cmp_proc;
388 * Determine the nominal datatype of the array elements. We have to
389 * support the convention that sk_subtype == InvalidOid means the opclass
390 * input type; this is a hack to simplify life for ScanKeyInit().
392 elemtype = skey->sk_subtype;
393 if (elemtype == InvalidOid)
394 elemtype = rel->rd_opcintype[skey->sk_attno - 1];
397 * Look up the appropriate comparison operator in the opfamily.
399 * Note: it's possible that this would fail, if the opfamily is
400 * incomplete, but it seems quite unlikely that an opfamily would omit
401 * non-cross-type comparison operators for any datatype that it supports
404 cmp_op = get_opfamily_member(rel->rd_opfamily[skey->sk_attno - 1],
408 if (!OidIsValid(cmp_op))
409 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
410 strat, elemtype, elemtype,
411 rel->rd_opfamily[skey->sk_attno - 1]);
412 cmp_proc = get_opcode(cmp_op);
413 if (!RegProcedureIsValid(cmp_proc))
414 elog(ERROR, "missing oprcode for operator %u", cmp_op);
416 fmgr_info(cmp_proc, &flinfo);
420 for (i = 1; i < nelems; i++)
422 if (DatumGetBool(FunctionCall2Coll(&flinfo,
433 * _bt_sort_array_elements() -- sort and de-dup array elements
435 * The array elements are sorted in-place, and the new number of elements
436 * after duplicate removal is returned.
438 * scan and skey identify the index column, whose opfamily determines the
439 * comparison semantics. If reverse is true, we sort in descending order.
442 _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey,
444 Datum *elems, int nelems)
446 Relation rel = scan->indexRelation;
448 RegProcedure cmp_proc;
449 BTSortArrayContext cxt;
454 return nelems; /* no work to do */
457 * Determine the nominal datatype of the array elements. We have to
458 * support the convention that sk_subtype == InvalidOid means the opclass
459 * input type; this is a hack to simplify life for ScanKeyInit().
461 elemtype = skey->sk_subtype;
462 if (elemtype == InvalidOid)
463 elemtype = rel->rd_opcintype[skey->sk_attno - 1];
466 * Look up the appropriate comparison function in the opfamily.
468 * Note: it's possible that this would fail, if the opfamily is
469 * incomplete, but it seems quite unlikely that an opfamily would omit
470 * non-cross-type support functions for any datatype that it supports at
473 cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
477 if (!RegProcedureIsValid(cmp_proc))
478 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
479 BTORDER_PROC, elemtype, elemtype,
480 rel->rd_opfamily[skey->sk_attno - 1]);
482 /* Sort the array elements */
483 fmgr_info(cmp_proc, &cxt.flinfo);
484 cxt.collation = skey->sk_collation;
485 cxt.reverse = reverse;
486 qsort_arg((void *) elems, nelems, sizeof(Datum),
487 _bt_compare_array_elements, (void *) &cxt);
489 /* Now scan the sorted elements and remove duplicates */
491 for (i = 1; i < nelems; i++)
495 compare = DatumGetInt32(FunctionCall2Coll(&cxt.flinfo,
500 elems[++last_non_dup] = elems[i];
503 return last_non_dup + 1;
507 * qsort_arg comparator for sorting array elements
510 _bt_compare_array_elements(const void *a, const void *b, void *arg)
512 Datum da = *((const Datum *) a);
513 Datum db = *((const Datum *) b);
514 BTSortArrayContext *cxt = (BTSortArrayContext *) arg;
517 compare = DatumGetInt32(FunctionCall2Coll(&cxt->flinfo,
526 * _bt_start_array_keys() -- Initialize array keys at start of a scan
528 * Set up the cur_elem counters and fill in the first sk_argument value for
529 * each array scankey. We can't do this until we know the scan direction.
532 _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
534 BTScanOpaque so = (BTScanOpaque) scan->opaque;
537 for (i = 0; i < so->numArrayKeys; i++)
539 BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
540 ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
542 Assert(curArrayKey->num_elems > 0);
543 if (ScanDirectionIsBackward(dir))
544 curArrayKey->cur_elem = curArrayKey->num_elems - 1;
546 curArrayKey->cur_elem = 0;
547 skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
552 * _bt_advance_array_keys() -- Advance to next set of array elements
554 * Returns true if there is another set of values to consider, false if not.
555 * On true result, the scankeys are initialized with the next set of values.
558 _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir)
560 BTScanOpaque so = (BTScanOpaque) scan->opaque;
565 * We must advance the last array key most quickly, since it will
566 * correspond to the lowest-order index column among the available
567 * qualifications. This is necessary to ensure correct ordering of output
568 * when there are multiple array keys.
570 for (i = so->numArrayKeys - 1; i >= 0; i--)
572 BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
573 ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
574 int cur_elem = curArrayKey->cur_elem;
575 int num_elems = curArrayKey->num_elems;
577 if (ScanDirectionIsBackward(dir))
581 cur_elem = num_elems - 1;
582 found = false; /* need to advance next array key */
589 if (++cur_elem >= num_elems)
592 found = false; /* need to advance next array key */
598 curArrayKey->cur_elem = cur_elem;
599 skey->sk_argument = curArrayKey->elem_values[cur_elem];
604 /* advance parallel scan */
605 if (scan->parallel_scan != NULL)
606 _bt_parallel_advance_array_keys(scan);
612 * _bt_mark_array_keys() -- Handle array keys during btmarkpos
614 * Save the current state of the array keys as the "mark" position.
617 _bt_mark_array_keys(IndexScanDesc scan)
619 BTScanOpaque so = (BTScanOpaque) scan->opaque;
622 for (i = 0; i < so->numArrayKeys; i++)
624 BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
626 curArrayKey->mark_elem = curArrayKey->cur_elem;
631 * _bt_restore_array_keys() -- Handle array keys during btrestrpos
633 * Restore the array keys to where they were when the mark was set.
636 _bt_restore_array_keys(IndexScanDesc scan)
638 BTScanOpaque so = (BTScanOpaque) scan->opaque;
639 bool changed = false;
642 /* Restore each array key to its position when the mark was set */
643 for (i = 0; i < so->numArrayKeys; i++)
645 BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
646 ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
647 int mark_elem = curArrayKey->mark_elem;
649 if (curArrayKey->cur_elem != mark_elem)
651 curArrayKey->cur_elem = mark_elem;
652 skey->sk_argument = curArrayKey->elem_values[mark_elem];
658 * If we changed any keys, we must redo _bt_preprocess_keys. That might
659 * sound like overkill, but in cases with multiple keys per index column
660 * it seems necessary to do the full set of pushups.
664 _bt_preprocess_keys(scan);
665 /* The mark should have been set on a consistent set of keys... */
672 * _bt_preprocess_keys() -- Preprocess scan keys
674 * The given search-type keys (in scan->keyData[] or so->arrayKeyData[])
675 * are copied to so->keyData[] with possible transformation.
676 * scan->numberOfKeys is the number of input keys, so->numberOfKeys gets
677 * the number of output keys (possibly less, never greater).
679 * The output keys are marked with additional sk_flag bits beyond the
680 * system-standard bits supplied by the caller. The DESC and NULLS_FIRST
681 * indoption bits for the relevant index attribute are copied into the flags.
682 * Also, for a DESC column, we commute (flip) all the sk_strategy numbers
683 * so that the index sorts in the desired direction.
685 * One key purpose of this routine is to discover which scan keys must be
686 * satisfied to continue the scan. It also attempts to eliminate redundant
687 * keys and detect contradictory keys. (If the index opfamily provides
688 * incomplete sets of cross-type operators, we may fail to detect redundant
689 * or contradictory keys, but we can survive that.)
691 * The output keys must be sorted by index attribute. Presently we expect
692 * (but verify) that the input keys are already so sorted --- this is done
693 * by match_clauses_to_index() in indxpath.c. Some reordering of the keys
694 * within each attribute may be done as a byproduct of the processing here,
695 * but no other code depends on that.
697 * The output keys are marked with flags SK_BT_REQFWD and/or SK_BT_REQBKWD
698 * if they must be satisfied in order to continue the scan forward or backward
699 * respectively. _bt_checkkeys uses these flags. For example, if the quals
700 * are "x = 1 AND y < 4 AND z < 5", then _bt_checkkeys will reject a tuple
701 * (1,2,7), but we must continue the scan in case there are tuples (1,3,z).
702 * But once we reach tuples like (1,4,z) we can stop scanning because no
703 * later tuples could match. This is reflected by marking the x and y keys,
704 * but not the z key, with SK_BT_REQFWD. In general, the keys for leading
705 * attributes with "=" keys are marked both SK_BT_REQFWD and SK_BT_REQBKWD.
706 * For the first attribute without an "=" key, any "<" and "<=" keys are
707 * marked SK_BT_REQFWD while any ">" and ">=" keys are marked SK_BT_REQBKWD.
708 * This can be seen to be correct by considering the above example. Note
709 * in particular that if there are no keys for a given attribute, the keys for
710 * subsequent attributes can never be required; for instance "WHERE y = 4"
711 * requires a full-index scan.
713 * If possible, redundant keys are eliminated: we keep only the tightest
714 * >/>= bound and the tightest </<= bound, and if there's an = key then
715 * that's the only one returned. (So, we return either a single = key,
716 * or one or two boundary-condition keys for each attr.) However, if we
717 * cannot compare two keys for lack of a suitable cross-type operator,
718 * we cannot eliminate either. If there are two such keys of the same
719 * operator strategy, the second one is just pushed into the output array
720 * without further processing here. We may also emit both >/>= or both
721 * </<= keys if we can't compare them. The logic about required keys still
722 * works if we don't eliminate redundant keys.
724 * Note that one reason we need direction-sensitive required-key flags is
725 * precisely that we may not be able to eliminate redundant keys. Suppose
726 * we have "x > 4::int AND x > 10::bigint", and we are unable to determine
727 * which key is more restrictive for lack of a suitable cross-type operator.
728 * _bt_first will arbitrarily pick one of the keys to do the initial
729 * positioning with. If it picks x > 4, then the x > 10 condition will fail
730 * until we reach index entries > 10; but we can't stop the scan just because
731 * x > 10 is failing. On the other hand, if we are scanning backwards, then
732 * failure of either key is indeed enough to stop the scan. (In general, when
733 * inequality keys are present, the initial-positioning code only promises to
734 * position before the first possible match, not exactly at the first match,
735 * for a forward scan; or after the last match for a backward scan.)
737 * As a byproduct of this work, we can detect contradictory quals such
738 * as "x = 1 AND x > 2". If we see that, we return so->qual_ok = false,
739 * indicating the scan need not be run at all since no tuples can match.
740 * (In this case we do not bother completing the output key array!)
741 * Again, missing cross-type operators might cause us to fail to prove the
742 * quals contradictory when they really are, but the scan will work correctly.
744 * Row comparison keys are currently also treated without any smarts:
745 * we just transfer them into the preprocessed array without any
746 * editorialization. We can treat them the same as an ordinary inequality
747 * comparison on the row's first index column, for the purposes of the logic
748 * about required keys.
750 * Note: the reason we have to copy the preprocessed scan keys into private
751 * storage is that we are modifying the array based on comparisons of the
752 * key argument values, which could change on a rescan or after moving to
753 * new elements of array keys. Therefore we can't overwrite the source data.
756 _bt_preprocess_keys(IndexScanDesc scan)
758 BTScanOpaque so = (BTScanOpaque) scan->opaque;
759 int numberOfKeys = scan->numberOfKeys;
760 int16 *indoption = scan->indexRelation->rd_indoption;
761 int new_numberOfKeys;
762 int numberOfEqualCols;
766 ScanKey xform[BTMaxStrategyNumber];
772 /* initialize result variables */
774 so->numberOfKeys = 0;
776 if (numberOfKeys < 1)
777 return; /* done if qual-less scan */
780 * Read so->arrayKeyData if array keys are present, else scan->keyData
782 if (so->arrayKeyData != NULL)
783 inkeys = so->arrayKeyData;
785 inkeys = scan->keyData;
787 outkeys = so->keyData;
789 /* we check that input keys are correctly ordered */
790 if (cur->sk_attno < 1)
791 elog(ERROR, "btree index keys must be ordered by attribute");
793 /* We can short-circuit most of the work if there's just one key */
794 if (numberOfKeys == 1)
796 /* Apply indoption to scankey (might change sk_strategy!) */
797 if (!_bt_fix_scankey_strategy(cur, indoption))
799 memcpy(outkeys, cur, sizeof(ScanKeyData));
800 so->numberOfKeys = 1;
801 /* We can mark the qual as required if it's for first index col */
802 if (cur->sk_attno == 1)
803 _bt_mark_scankey_required(outkeys);
808 * Otherwise, do the full set of pushups.
810 new_numberOfKeys = 0;
811 numberOfEqualCols = 0;
814 * Initialize for processing of keys for attr 1.
816 * xform[i] points to the currently best scan key of strategy type i+1; it
817 * is NULL if we haven't yet found such a key for this attr.
820 memset(xform, 0, sizeof(xform));
823 * Loop iterates from 0 to numberOfKeys inclusive; we use the last pass to
824 * handle after-last-key processing. Actual exit from the loop is at the
825 * "break" statement below.
827 for (i = 0;; cur++, i++)
829 if (i < numberOfKeys)
831 /* Apply indoption to scankey (might change sk_strategy!) */
832 if (!_bt_fix_scankey_strategy(cur, indoption))
834 /* NULL can't be matched, so give up */
841 * If we are at the end of the keys for a particular attr, finish up
842 * processing and emit the cleaned-up keys.
844 if (i == numberOfKeys || cur->sk_attno != attno)
846 int priorNumberOfEqualCols = numberOfEqualCols;
848 /* check input keys are correctly ordered */
849 if (i < numberOfKeys && cur->sk_attno < attno)
850 elog(ERROR, "btree index keys must be ordered by attribute");
853 * If = has been specified, all other keys can be eliminated as
854 * redundant. If we have a case like key = 1 AND key > 2, we can
855 * set qual_ok to false and abandon further processing.
857 * We also have to deal with the case of "key IS NULL", which is
858 * unsatisfiable in combination with any other index condition. By
859 * the time we get here, that's been classified as an equality
860 * check, and we've rejected any combination of it with a regular
861 * equality condition; but not with other types of conditions.
863 if (xform[BTEqualStrategyNumber - 1])
865 ScanKey eq = xform[BTEqualStrategyNumber - 1];
867 for (j = BTMaxStrategyNumber; --j >= 0;)
869 ScanKey chk = xform[j];
871 if (!chk || j == (BTEqualStrategyNumber - 1))
874 if (eq->sk_flags & SK_SEARCHNULL)
876 /* IS NULL is contradictory to anything else */
881 if (_bt_compare_scankey_args(scan, chk, eq, chk,
886 /* keys proven mutually contradictory */
890 /* else discard the redundant non-equality key */
893 /* else, cannot determine redundancy, keep both keys */
895 /* track number of attrs for which we have "=" keys */
899 /* try to keep only one of <, <= */
900 if (xform[BTLessStrategyNumber - 1]
901 && xform[BTLessEqualStrategyNumber - 1])
903 ScanKey lt = xform[BTLessStrategyNumber - 1];
904 ScanKey le = xform[BTLessEqualStrategyNumber - 1];
906 if (_bt_compare_scankey_args(scan, le, lt, le,
910 xform[BTLessEqualStrategyNumber - 1] = NULL;
912 xform[BTLessStrategyNumber - 1] = NULL;
916 /* try to keep only one of >, >= */
917 if (xform[BTGreaterStrategyNumber - 1]
918 && xform[BTGreaterEqualStrategyNumber - 1])
920 ScanKey gt = xform[BTGreaterStrategyNumber - 1];
921 ScanKey ge = xform[BTGreaterEqualStrategyNumber - 1];
923 if (_bt_compare_scankey_args(scan, ge, gt, ge,
927 xform[BTGreaterEqualStrategyNumber - 1] = NULL;
929 xform[BTGreaterStrategyNumber - 1] = NULL;
934 * Emit the cleaned-up keys into the outkeys[] array, and then
935 * mark them if they are required. They are required (possibly
936 * only in one direction) if all attrs before this one had "=".
938 for (j = BTMaxStrategyNumber; --j >= 0;)
942 ScanKey outkey = &outkeys[new_numberOfKeys++];
944 memcpy(outkey, xform[j], sizeof(ScanKeyData));
945 if (priorNumberOfEqualCols == attno - 1)
946 _bt_mark_scankey_required(outkey);
951 * Exit loop here if done.
953 if (i == numberOfKeys)
956 /* Re-initialize for new attno */
957 attno = cur->sk_attno;
958 memset(xform, 0, sizeof(xform));
961 /* check strategy this key's operator corresponds to */
962 j = cur->sk_strategy - 1;
964 /* if row comparison, push it directly to the output array */
965 if (cur->sk_flags & SK_ROW_HEADER)
967 ScanKey outkey = &outkeys[new_numberOfKeys++];
969 memcpy(outkey, cur, sizeof(ScanKeyData));
970 if (numberOfEqualCols == attno - 1)
971 _bt_mark_scankey_required(outkey);
974 * We don't support RowCompare using equality; such a qual would
975 * mess up the numberOfEqualCols tracking.
977 Assert(j != (BTEqualStrategyNumber - 1));
981 /* have we seen one of these before? */
982 if (xform[j] == NULL)
984 /* nope, so remember this scankey */
989 /* yup, keep only the more restrictive key */
990 if (_bt_compare_scankey_args(scan, cur, cur, xform[j],
995 else if (j == (BTEqualStrategyNumber - 1))
997 /* key == a && key == b, but a != b */
1001 /* else old key is more restrictive, keep it */
1006 * We can't determine which key is more restrictive. Keep the
1007 * previous one in xform[j] and push this one directly to the
1010 ScanKey outkey = &outkeys[new_numberOfKeys++];
1012 memcpy(outkey, cur, sizeof(ScanKeyData));
1013 if (numberOfEqualCols == attno - 1)
1014 _bt_mark_scankey_required(outkey);
1019 so->numberOfKeys = new_numberOfKeys;
1023 * Compare two scankey values using a specified operator.
1025 * The test we want to perform is logically "leftarg op rightarg", where
1026 * leftarg and rightarg are the sk_argument values in those ScanKeys, and
1027 * the comparison operator is the one in the op ScanKey. However, in
1028 * cross-data-type situations we may need to look up the correct operator in
1029 * the index's opfamily: it is the one having amopstrategy = op->sk_strategy
1030 * and amoplefttype/amoprighttype equal to the two argument datatypes.
1032 * If the opfamily doesn't supply a complete set of cross-type operators we
1033 * may not be able to make the comparison. If we can make the comparison
1034 * we store the operator result in *result and return true. We return false
1035 * if the comparison could not be made.
1037 * Note: op always points at the same ScanKey as either leftarg or rightarg.
1038 * Since we don't scribble on the scankeys, this aliasing should cause no
1041 * Note: this routine needs to be insensitive to any DESC option applied
1042 * to the index column. For example, "x < 4" is a tighter constraint than
1043 * "x < 5" regardless of which way the index is sorted.
1046 _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
1047 ScanKey leftarg, ScanKey rightarg,
1050 Relation rel = scan->indexRelation;
1056 StrategyNumber strat;
1059 * First, deal with cases where one or both args are NULL. This should
1060 * only happen when the scankeys represent IS NULL/NOT NULL conditions.
1062 if ((leftarg->sk_flags | rightarg->sk_flags) & SK_ISNULL)
1067 if (leftarg->sk_flags & SK_ISNULL)
1069 Assert(leftarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
1074 if (rightarg->sk_flags & SK_ISNULL)
1076 Assert(rightarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
1083 * We treat NULL as either greater than or less than all other values.
1084 * Since true > false, the tests below work correctly for NULLS LAST
1085 * logic. If the index is NULLS FIRST, we need to flip the strategy.
1087 strat = op->sk_strategy;
1088 if (op->sk_flags & SK_BT_NULLS_FIRST)
1089 strat = BTCommuteStrategyNumber(strat);
1093 case BTLessStrategyNumber:
1094 *result = (leftnull < rightnull);
1096 case BTLessEqualStrategyNumber:
1097 *result = (leftnull <= rightnull);
1099 case BTEqualStrategyNumber:
1100 *result = (leftnull == rightnull);
1102 case BTGreaterEqualStrategyNumber:
1103 *result = (leftnull >= rightnull);
1105 case BTGreaterStrategyNumber:
1106 *result = (leftnull > rightnull);
1109 elog(ERROR, "unrecognized StrategyNumber: %d", (int) strat);
1110 *result = false; /* keep compiler quiet */
1117 * The opfamily we need to worry about is identified by the index column.
1119 Assert(leftarg->sk_attno == rightarg->sk_attno);
1121 opcintype = rel->rd_opcintype[leftarg->sk_attno - 1];
1124 * Determine the actual datatypes of the ScanKey arguments. We have to
1125 * support the convention that sk_subtype == InvalidOid means the opclass
1126 * input type; this is a hack to simplify life for ScanKeyInit().
1128 lefttype = leftarg->sk_subtype;
1129 if (lefttype == InvalidOid)
1130 lefttype = opcintype;
1131 righttype = rightarg->sk_subtype;
1132 if (righttype == InvalidOid)
1133 righttype = opcintype;
1134 optype = op->sk_subtype;
1135 if (optype == InvalidOid)
1139 * If leftarg and rightarg match the types expected for the "op" scankey,
1140 * we can use its already-looked-up comparison function.
1142 if (lefttype == opcintype && righttype == optype)
1144 *result = DatumGetBool(FunctionCall2Coll(&op->sk_func,
1146 leftarg->sk_argument,
1147 rightarg->sk_argument));
1152 * Otherwise, we need to go to the syscache to find the appropriate
1153 * operator. (This cannot result in infinite recursion, since no
1154 * indexscan initiated by syscache lookup will use cross-data-type
1157 * If the sk_strategy was flipped by _bt_fix_scankey_strategy, we have to
1158 * un-flip it to get the correct opfamily member.
1160 strat = op->sk_strategy;
1161 if (op->sk_flags & SK_BT_DESC)
1162 strat = BTCommuteStrategyNumber(strat);
1164 cmp_op = get_opfamily_member(rel->rd_opfamily[leftarg->sk_attno - 1],
1168 if (OidIsValid(cmp_op))
1170 RegProcedure cmp_proc = get_opcode(cmp_op);
1172 if (RegProcedureIsValid(cmp_proc))
1174 *result = DatumGetBool(OidFunctionCall2Coll(cmp_proc,
1176 leftarg->sk_argument,
1177 rightarg->sk_argument));
1182 /* Can't make the comparison */
1183 *result = false; /* suppress compiler warnings */
1188 * Adjust a scankey's strategy and flags setting as needed for indoptions.
1190 * We copy the appropriate indoption value into the scankey sk_flags
1191 * (shifting to avoid clobbering system-defined flag bits). Also, if
1192 * the DESC option is set, commute (flip) the operator strategy number.
1194 * A secondary purpose is to check for IS NULL/NOT NULL scankeys and set up
1195 * the strategy field correctly for them.
1197 * Lastly, for ordinary scankeys (not IS NULL/NOT NULL), we check for a
1198 * NULL comparison value. Since all btree operators are assumed strict,
1199 * a NULL means that the qual cannot be satisfied. We return true if the
1200 * comparison value isn't NULL, or false if the scan should be abandoned.
1202 * This function is applied to the *input* scankey structure; therefore
1203 * on a rescan we will be looking at already-processed scankeys. Hence
1204 * we have to be careful not to re-commute the strategy if we already did it.
1205 * It's a bit ugly to modify the caller's copy of the scankey but in practice
1206 * there shouldn't be any problem, since the index's indoptions are certainly
1207 * not going to change while the scankey survives.
1210 _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption)
1214 addflags = indoption[skey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1217 * We treat all btree operators as strict (even if they're not so marked
1218 * in pg_proc). This means that it is impossible for an operator condition
1219 * with a NULL comparison constant to succeed, and we can reject it right
1222 * However, we now also support "x IS NULL" clauses as search conditions,
1223 * so in that case keep going. The planner has not filled in any
1224 * particular strategy in this case, so set it to BTEqualStrategyNumber
1225 * --- we can treat IS NULL as an equality operator for purposes of search
1228 * Likewise, "x IS NOT NULL" is supported. We treat that as either "less
1229 * than NULL" in a NULLS LAST index, or "greater than NULL" in a NULLS
1232 * Note: someday we might have to fill in sk_collation from the index
1233 * column's collation. At the moment this is a non-issue because we'll
1234 * never actually call the comparison operator on a NULL.
1236 if (skey->sk_flags & SK_ISNULL)
1238 /* SK_ISNULL shouldn't be set in a row header scankey */
1239 Assert(!(skey->sk_flags & SK_ROW_HEADER));
1241 /* Set indoption flags in scankey (might be done already) */
1242 skey->sk_flags |= addflags;
1244 /* Set correct strategy for IS NULL or NOT NULL search */
1245 if (skey->sk_flags & SK_SEARCHNULL)
1247 skey->sk_strategy = BTEqualStrategyNumber;
1248 skey->sk_subtype = InvalidOid;
1249 skey->sk_collation = InvalidOid;
1251 else if (skey->sk_flags & SK_SEARCHNOTNULL)
1253 if (skey->sk_flags & SK_BT_NULLS_FIRST)
1254 skey->sk_strategy = BTGreaterStrategyNumber;
1256 skey->sk_strategy = BTLessStrategyNumber;
1257 skey->sk_subtype = InvalidOid;
1258 skey->sk_collation = InvalidOid;
1262 /* regular qual, so it cannot be satisfied */
1266 /* Needn't do the rest */
1270 /* Adjust strategy for DESC, if we didn't already */
1271 if ((addflags & SK_BT_DESC) && !(skey->sk_flags & SK_BT_DESC))
1272 skey->sk_strategy = BTCommuteStrategyNumber(skey->sk_strategy);
1273 skey->sk_flags |= addflags;
1275 /* If it's a row header, fix row member flags and strategies similarly */
1276 if (skey->sk_flags & SK_ROW_HEADER)
1278 ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1282 Assert(subkey->sk_flags & SK_ROW_MEMBER);
1283 addflags = indoption[subkey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1284 if ((addflags & SK_BT_DESC) && !(subkey->sk_flags & SK_BT_DESC))
1285 subkey->sk_strategy = BTCommuteStrategyNumber(subkey->sk_strategy);
1286 subkey->sk_flags |= addflags;
1287 if (subkey->sk_flags & SK_ROW_END)
1297 * Mark a scankey as "required to continue the scan".
1299 * Depending on the operator type, the key may be required for both scan
1300 * directions or just one. Also, if the key is a row comparison header,
1301 * we have to mark its first subsidiary ScanKey as required. (Subsequent
1302 * subsidiary ScanKeys are normally for lower-order columns, and thus
1303 * cannot be required, since they're after the first non-equality scankey.)
1305 * Note: when we set required-key flag bits in a subsidiary scankey, we are
1306 * scribbling on a data structure belonging to the index AM's caller, not on
1307 * our private copy. This should be OK because the marking will not change
1308 * from scan to scan within a query, and so we'd just re-mark the same way
1309 * anyway on a rescan. Something to keep an eye on though.
1312 _bt_mark_scankey_required(ScanKey skey)
1316 switch (skey->sk_strategy)
1318 case BTLessStrategyNumber:
1319 case BTLessEqualStrategyNumber:
1320 addflags = SK_BT_REQFWD;
1322 case BTEqualStrategyNumber:
1323 addflags = SK_BT_REQFWD | SK_BT_REQBKWD;
1325 case BTGreaterEqualStrategyNumber:
1326 case BTGreaterStrategyNumber:
1327 addflags = SK_BT_REQBKWD;
1330 elog(ERROR, "unrecognized StrategyNumber: %d",
1331 (int) skey->sk_strategy);
1332 addflags = 0; /* keep compiler quiet */
1336 skey->sk_flags |= addflags;
1338 if (skey->sk_flags & SK_ROW_HEADER)
1340 ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1342 /* First subkey should be same column/operator as the header */
1343 Assert(subkey->sk_flags & SK_ROW_MEMBER);
1344 Assert(subkey->sk_attno == skey->sk_attno);
1345 Assert(subkey->sk_strategy == skey->sk_strategy);
1346 subkey->sk_flags |= addflags;
1351 * Test whether an indextuple satisfies all the scankey conditions.
1353 * If so, return the address of the index tuple on the index page.
1354 * If not, return NULL.
1356 * If the tuple fails to pass the qual, we also determine whether there's
1357 * any need to continue the scan beyond this tuple, and set *continuescan
1358 * accordingly. See comments for _bt_preprocess_keys(), above, about how
1361 * scan: index scan descriptor (containing a search-type scankey)
1362 * page: buffer page containing index tuple
1363 * offnum: offset number of index tuple (must be a valid item!)
1364 * dir: direction we are scanning in
1365 * continuescan: output parameter (will be set correctly in all cases)
1367 * Caller must hold pin and lock on the index page.
1370 _bt_checkkeys(IndexScanDesc scan,
1371 Page page, OffsetNumber offnum,
1372 ScanDirection dir, bool *continuescan)
1374 ItemId iid = PageGetItemId(page, offnum);
1383 *continuescan = true; /* default assumption */
1386 * If the scan specifies not to return killed tuples, then we treat a
1387 * killed tuple as not passing the qual. Most of the time, it's a win to
1388 * not bother examining the tuple's index keys, but just return
1389 * immediately with continuescan = true to proceed to the next tuple.
1390 * However, if this is the last tuple on the page, we should check the
1391 * index keys to prevent uselessly advancing to the next page.
1393 if (scan->ignore_killed_tuples && ItemIdIsDead(iid))
1395 /* return immediately if there are more tuples on the page */
1396 if (ScanDirectionIsForward(dir))
1398 if (offnum < PageGetMaxOffsetNumber(page))
1403 BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1405 if (offnum > P_FIRSTDATAKEY(opaque))
1410 * OK, we want to check the keys so we can set continuescan correctly,
1411 * but we'll return NULL even if the tuple passes the key tests.
1413 tuple_alive = false;
1418 tuple = (IndexTuple) PageGetItem(page, iid);
1420 tupdesc = RelationGetDescr(scan->indexRelation);
1421 so = (BTScanOpaque) scan->opaque;
1422 keysz = so->numberOfKeys;
1424 for (key = so->keyData, ikey = 0; ikey < keysz; key++, ikey++)
1430 Assert(key->sk_attno <= BTreeTupleGetNAtts(tuple, scan->indexRelation));
1431 /* row-comparison keys need special processing */
1432 if (key->sk_flags & SK_ROW_HEADER)
1434 if (_bt_check_rowcompare(key, tuple, tupdesc, dir, continuescan))
1439 datum = index_getattr(tuple,
1444 if (key->sk_flags & SK_ISNULL)
1446 /* Handle IS NULL/NOT NULL tests */
1447 if (key->sk_flags & SK_SEARCHNULL)
1450 continue; /* tuple satisfies this qual */
1454 Assert(key->sk_flags & SK_SEARCHNOTNULL);
1456 continue; /* tuple satisfies this qual */
1460 * Tuple fails this qual. If it's a required qual for the current
1461 * scan direction, then we can conclude no further tuples will
1464 if ((key->sk_flags & SK_BT_REQFWD) &&
1465 ScanDirectionIsForward(dir))
1466 *continuescan = false;
1467 else if ((key->sk_flags & SK_BT_REQBKWD) &&
1468 ScanDirectionIsBackward(dir))
1469 *continuescan = false;
1472 * In any case, this indextuple doesn't match the qual.
1479 if (key->sk_flags & SK_BT_NULLS_FIRST)
1482 * Since NULLs are sorted before non-NULLs, we know we have
1483 * reached the lower limit of the range of values for this
1484 * index attr. On a backward scan, we can stop if this qual
1485 * is one of the "must match" subset. We can stop regardless
1486 * of whether the qual is > or <, so long as it's required,
1487 * because it's not possible for any future tuples to pass. On
1488 * a forward scan, however, we must keep going, because we may
1489 * have initially positioned to the start of the index.
1491 if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1492 ScanDirectionIsBackward(dir))
1493 *continuescan = false;
1498 * Since NULLs are sorted after non-NULLs, we know we have
1499 * reached the upper limit of the range of values for this
1500 * index attr. On a forward scan, we can stop if this qual is
1501 * one of the "must match" subset. We can stop regardless of
1502 * whether the qual is > or <, so long as it's required,
1503 * because it's not possible for any future tuples to pass. On
1504 * a backward scan, however, we must keep going, because we
1505 * may have initially positioned to the end of the index.
1507 if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1508 ScanDirectionIsForward(dir))
1509 *continuescan = false;
1513 * In any case, this indextuple doesn't match the qual.
1518 test = FunctionCall2Coll(&key->sk_func, key->sk_collation,
1519 datum, key->sk_argument);
1521 if (!DatumGetBool(test))
1524 * Tuple fails this qual. If it's a required qual for the current
1525 * scan direction, then we can conclude no further tuples will
1528 * Note: because we stop the scan as soon as any required equality
1529 * qual fails, it is critical that equality quals be used for the
1530 * initial positioning in _bt_first() when they are available. See
1531 * comments in _bt_first().
1533 if ((key->sk_flags & SK_BT_REQFWD) &&
1534 ScanDirectionIsForward(dir))
1535 *continuescan = false;
1536 else if ((key->sk_flags & SK_BT_REQBKWD) &&
1537 ScanDirectionIsBackward(dir))
1538 *continuescan = false;
1541 * In any case, this indextuple doesn't match the qual.
1547 /* Check for failure due to it being a killed tuple. */
1551 /* If we get here, the tuple passes all index quals. */
1556 * Test whether an indextuple satisfies a row-comparison scan condition.
1558 * Return true if so, false if not. If not, also clear *continuescan if
1559 * it's not possible for any future tuples in the current scan direction
1562 * This is a subroutine for _bt_checkkeys, which see for more info.
1565 _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
1566 ScanDirection dir, bool *continuescan)
1568 ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1569 int32 cmpresult = 0;
1572 /* First subkey should be same as the header says */
1573 Assert(subkey->sk_attno == skey->sk_attno);
1575 /* Loop over columns of the row condition */
1581 Assert(subkey->sk_flags & SK_ROW_MEMBER);
1583 datum = index_getattr(tuple,
1590 if (subkey->sk_flags & SK_BT_NULLS_FIRST)
1593 * Since NULLs are sorted before non-NULLs, we know we have
1594 * reached the lower limit of the range of values for this
1595 * index attr. On a backward scan, we can stop if this qual
1596 * is one of the "must match" subset. We can stop regardless
1597 * of whether the qual is > or <, so long as it's required,
1598 * because it's not possible for any future tuples to pass. On
1599 * a forward scan, however, we must keep going, because we may
1600 * have initially positioned to the start of the index.
1602 if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1603 ScanDirectionIsBackward(dir))
1604 *continuescan = false;
1609 * Since NULLs are sorted after non-NULLs, we know we have
1610 * reached the upper limit of the range of values for this
1611 * index attr. On a forward scan, we can stop if this qual is
1612 * one of the "must match" subset. We can stop regardless of
1613 * whether the qual is > or <, so long as it's required,
1614 * because it's not possible for any future tuples to pass. On
1615 * a backward scan, however, we must keep going, because we
1616 * may have initially positioned to the end of the index.
1618 if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1619 ScanDirectionIsForward(dir))
1620 *continuescan = false;
1624 * In any case, this indextuple doesn't match the qual.
1629 if (subkey->sk_flags & SK_ISNULL)
1632 * Unlike the simple-scankey case, this isn't a disallowed case.
1633 * But it can never match. If all the earlier row comparison
1634 * columns are required for the scan direction, we can stop the
1635 * scan, because there can't be another tuple that will succeed.
1637 if (subkey != (ScanKey) DatumGetPointer(skey->sk_argument))
1639 if ((subkey->sk_flags & SK_BT_REQFWD) &&
1640 ScanDirectionIsForward(dir))
1641 *continuescan = false;
1642 else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1643 ScanDirectionIsBackward(dir))
1644 *continuescan = false;
1648 /* Perform the test --- three-way comparison not bool operator */
1649 cmpresult = DatumGetInt32(FunctionCall2Coll(&subkey->sk_func,
1650 subkey->sk_collation,
1652 subkey->sk_argument));
1654 if (subkey->sk_flags & SK_BT_DESC)
1655 cmpresult = -cmpresult;
1657 /* Done comparing if unequal, else advance to next column */
1661 if (subkey->sk_flags & SK_ROW_END)
1667 * At this point cmpresult indicates the overall result of the row
1668 * comparison, and subkey points to the deciding column (or the last
1669 * column if the result is "=").
1671 switch (subkey->sk_strategy)
1673 /* EQ and NE cases aren't allowed here */
1674 case BTLessStrategyNumber:
1675 result = (cmpresult < 0);
1677 case BTLessEqualStrategyNumber:
1678 result = (cmpresult <= 0);
1680 case BTGreaterEqualStrategyNumber:
1681 result = (cmpresult >= 0);
1683 case BTGreaterStrategyNumber:
1684 result = (cmpresult > 0);
1687 elog(ERROR, "unrecognized RowCompareType: %d",
1688 (int) subkey->sk_strategy);
1689 result = 0; /* keep compiler quiet */
1696 * Tuple fails this qual. If it's a required qual for the current
1697 * scan direction, then we can conclude no further tuples will pass,
1698 * either. Note we have to look at the deciding column, not
1699 * necessarily the first or last column of the row condition.
1701 if ((subkey->sk_flags & SK_BT_REQFWD) &&
1702 ScanDirectionIsForward(dir))
1703 *continuescan = false;
1704 else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1705 ScanDirectionIsBackward(dir))
1706 *continuescan = false;
1713 * _bt_killitems - set LP_DEAD state for items an indexscan caller has
1714 * told us were killed
1716 * scan->opaque, referenced locally through so, contains information about the
1717 * current page and killed tuples thereon (generally, this should only be
1718 * called if so->numKilled > 0).
1720 * The caller does not have a lock on the page and may or may not have the
1721 * page pinned in a buffer. Note that read-lock is sufficient for setting
1722 * LP_DEAD status (which is only a hint).
1724 * We match items by heap TID before assuming they are the right ones to
1725 * delete. We cope with cases where items have moved right due to insertions.
1726 * If an item has moved off the current page due to a split, we'll fail to
1727 * find it and do nothing (this is not an error case --- we assume the item
1728 * will eventually get marked in a future indexscan).
1730 * Note that if we hold a pin on the target page continuously from initially
1731 * reading the items until applying this function, VACUUM cannot have deleted
1732 * any items from the page, and so there is no need to search left from the
1733 * recorded offset. (This observation also guarantees that the item is still
1734 * the right one to delete, which might otherwise be questionable since heap
1735 * TIDs can get recycled.) This holds true even if the page has been modified
1736 * by inserts and page splits, so there is no need to consult the LSN.
1738 * If the pin was released after reading the page, then we re-read it. If it
1739 * has been modified since we read it (as determined by the LSN), we dare not
1740 * flag any entries because it is possible that the old entry was vacuumed
1741 * away and the TID was re-used by a completely different heap tuple.
1744 _bt_killitems(IndexScanDesc scan)
1746 BTScanOpaque so = (BTScanOpaque) scan->opaque;
1748 BTPageOpaque opaque;
1749 OffsetNumber minoff;
1750 OffsetNumber maxoff;
1752 int numKilled = so->numKilled;
1753 bool killedsomething = false;
1755 Assert(BTScanPosIsValid(so->currPos));
1758 * Always reset the scan state, so we don't look for same items on other
1763 if (BTScanPosIsPinned(so->currPos))
1766 * We have held the pin on this page since we read the index tuples,
1767 * so all we need to do is lock it. The pin will have prevented
1768 * re-use of any TID on the page, so there is no need to check the
1771 LockBuffer(so->currPos.buf, BT_READ);
1773 page = BufferGetPage(so->currPos.buf);
1779 /* Attempt to re-read the buffer, getting pin and lock. */
1780 buf = _bt_getbuf(scan->indexRelation, so->currPos.currPage, BT_READ);
1782 /* It might not exist anymore; in which case we can't hint it. */
1783 if (!BufferIsValid(buf))
1786 page = BufferGetPage(buf);
1787 if (BufferGetLSNAtomic(buf) == so->currPos.lsn)
1788 so->currPos.buf = buf;
1791 /* Modified while not pinned means hinting is not safe. */
1792 _bt_relbuf(scan->indexRelation, buf);
1797 opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1798 minoff = P_FIRSTDATAKEY(opaque);
1799 maxoff = PageGetMaxOffsetNumber(page);
1801 for (i = 0; i < numKilled; i++)
1803 int itemIndex = so->killedItems[i];
1804 BTScanPosItem *kitem = &so->currPos.items[itemIndex];
1805 OffsetNumber offnum = kitem->indexOffset;
1807 Assert(itemIndex >= so->currPos.firstItem &&
1808 itemIndex <= so->currPos.lastItem);
1809 if (offnum < minoff)
1810 continue; /* pure paranoia */
1811 while (offnum <= maxoff)
1813 ItemId iid = PageGetItemId(page, offnum);
1814 IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
1816 if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
1818 /* found the item */
1819 ItemIdMarkDead(iid);
1820 killedsomething = true;
1821 break; /* out of inner search loop */
1823 offnum = OffsetNumberNext(offnum);
1828 * Since this can be redone later if needed, mark as dirty hint.
1830 * Whenever we mark anything LP_DEAD, we also set the page's
1831 * BTP_HAS_GARBAGE flag, which is likewise just a hint.
1833 if (killedsomething)
1835 opaque->btpo_flags |= BTP_HAS_GARBAGE;
1836 MarkBufferDirtyHint(so->currPos.buf, true);
1839 LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK);
1844 * The following routines manage a shared-memory area in which we track
1845 * assignment of "vacuum cycle IDs" to currently-active btree vacuuming
1846 * operations. There is a single counter which increments each time we
1847 * start a vacuum to assign it a cycle ID. Since multiple vacuums could
1848 * be active concurrently, we have to track the cycle ID for each active
1849 * vacuum; this requires at most MaxBackends entries (usually far fewer).
1850 * We assume at most one vacuum can be active for a given index.
1852 * Access to the shared memory area is controlled by BtreeVacuumLock.
1853 * In principle we could use a separate lmgr locktag for each index,
1854 * but a single LWLock is much cheaper, and given the short time that
1855 * the lock is ever held, the concurrency hit should be minimal.
1858 typedef struct BTOneVacInfo
1860 LockRelId relid; /* global identifier of an index */
1861 BTCycleId cycleid; /* cycle ID for its active VACUUM */
1864 typedef struct BTVacInfo
1866 BTCycleId cycle_ctr; /* cycle ID most recently assigned */
1867 int num_vacuums; /* number of currently active VACUUMs */
1868 int max_vacuums; /* allocated length of vacuums[] array */
1869 BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER];
1872 static BTVacInfo *btvacinfo;
1876 * _bt_vacuum_cycleid --- get the active vacuum cycle ID for an index,
1877 * or zero if there is no active VACUUM
1879 * Note: for correct interlocking, the caller must already hold pin and
1880 * exclusive lock on each buffer it will store the cycle ID into. This
1881 * ensures that even if a VACUUM starts immediately afterwards, it cannot
1882 * process those pages until the page split is complete.
1885 _bt_vacuum_cycleid(Relation rel)
1887 BTCycleId result = 0;
1890 /* Share lock is enough since this is a read-only operation */
1891 LWLockAcquire(BtreeVacuumLock, LW_SHARED);
1893 for (i = 0; i < btvacinfo->num_vacuums; i++)
1895 BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1897 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1898 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1900 result = vac->cycleid;
1905 LWLockRelease(BtreeVacuumLock);
1910 * _bt_start_vacuum --- assign a cycle ID to a just-starting VACUUM operation
1912 * Note: the caller must guarantee that it will eventually call
1913 * _bt_end_vacuum, else we'll permanently leak an array slot. To ensure
1914 * that this happens even in elog(FATAL) scenarios, the appropriate coding
1915 * is not just a PG_TRY, but
1916 * PG_ENSURE_ERROR_CLEANUP(_bt_end_vacuum_callback, PointerGetDatum(rel))
1919 _bt_start_vacuum(Relation rel)
1925 LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1928 * Assign the next cycle ID, being careful to avoid zero as well as the
1929 * reserved high values.
1931 result = ++(btvacinfo->cycle_ctr);
1932 if (result == 0 || result > MAX_BT_CYCLE_ID)
1933 result = btvacinfo->cycle_ctr = 1;
1935 /* Let's just make sure there's no entry already for this index */
1936 for (i = 0; i < btvacinfo->num_vacuums; i++)
1938 vac = &btvacinfo->vacuums[i];
1939 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1940 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1943 * Unlike most places in the backend, we have to explicitly
1944 * release our LWLock before throwing an error. This is because
1945 * we expect _bt_end_vacuum() to be called before transaction
1946 * abort cleanup can run to release LWLocks.
1948 LWLockRelease(BtreeVacuumLock);
1949 elog(ERROR, "multiple active vacuums for index \"%s\"",
1950 RelationGetRelationName(rel));
1954 /* OK, add an entry */
1955 if (btvacinfo->num_vacuums >= btvacinfo->max_vacuums)
1957 LWLockRelease(BtreeVacuumLock);
1958 elog(ERROR, "out of btvacinfo slots");
1960 vac = &btvacinfo->vacuums[btvacinfo->num_vacuums];
1961 vac->relid = rel->rd_lockInfo.lockRelId;
1962 vac->cycleid = result;
1963 btvacinfo->num_vacuums++;
1965 LWLockRelease(BtreeVacuumLock);
1970 * _bt_end_vacuum --- mark a btree VACUUM operation as done
1972 * Note: this is deliberately coded not to complain if no entry is found;
1973 * this allows the caller to put PG_TRY around the start_vacuum operation.
1976 _bt_end_vacuum(Relation rel)
1980 LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1982 /* Find the array entry */
1983 for (i = 0; i < btvacinfo->num_vacuums; i++)
1985 BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1987 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1988 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1990 /* Remove it by shifting down the last entry */
1991 *vac = btvacinfo->vacuums[btvacinfo->num_vacuums - 1];
1992 btvacinfo->num_vacuums--;
1997 LWLockRelease(BtreeVacuumLock);
2001 * _bt_end_vacuum wrapped as an on_shmem_exit callback function
2004 _bt_end_vacuum_callback(int code, Datum arg)
2006 _bt_end_vacuum((Relation) DatumGetPointer(arg));
2010 * BTreeShmemSize --- report amount of shared memory space needed
2013 BTreeShmemSize(void)
2017 size = offsetof(BTVacInfo, vacuums);
2018 size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
2023 * BTreeShmemInit --- initialize this module's shared memory
2026 BTreeShmemInit(void)
2030 btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
2034 if (!IsUnderPostmaster)
2036 /* Initialize shared memory area */
2040 * It doesn't really matter what the cycle counter starts at, but
2041 * having it always start the same doesn't seem good. Seed with
2042 * low-order bits of time() instead.
2044 btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
2046 btvacinfo->num_vacuums = 0;
2047 btvacinfo->max_vacuums = MaxBackends;
2054 btoptions(Datum reloptions, bool validate)
2056 return default_reloptions(reloptions, validate, RELOPT_KIND_BTREE);
2060 * btproperty() -- Check boolean properties of indexes.
2062 * This is optional, but handling AMPROP_RETURNABLE here saves opening the rel
2063 * to call btcanreturn.
2066 btproperty(Oid index_oid, int attno,
2067 IndexAMProperty prop, const char *propname,
2068 bool *res, bool *isnull)
2072 case AMPROP_RETURNABLE:
2073 /* answer only for columns, not AM or whole index */
2076 /* otherwise, btree can always return data */
2081 return false; /* punt to generic code */
2086 * _bt_nonkey_truncate() -- create tuple without non-key suffix attributes.
2088 * Returns truncated index tuple allocated in caller's memory context, with key
2089 * attributes copied from caller's itup argument. Currently, suffix truncation
2090 * is only performed to create pivot tuples in INCLUDE indexes, but some day it
2091 * could be generalized to remove suffix attributes after the first
2092 * distinguishing key attribute.
2094 * Truncated tuple is guaranteed to be no larger than the original, which is
2095 * important for staying under the 1/3 of a page restriction on tuple size.
2097 * Note that returned tuple's t_tid offset will hold the number of attributes
2098 * present, so the original item pointer offset is not represented. Caller
2099 * should only change truncated tuple's downlink.
2102 _bt_nonkey_truncate(Relation rel, IndexTuple itup)
2104 int nkeyattrs = IndexRelationGetNumberOfKeyAttributes(rel);
2105 IndexTuple truncated;
2108 * We should only ever truncate leaf index tuples, which must have both key
2109 * and non-key attributes. It's never okay to truncate a second time.
2111 Assert(BTreeTupleGetNAtts(itup, rel) ==
2112 IndexRelationGetNumberOfAttributes(rel));
2114 truncated = index_truncate_tuple(RelationGetDescr(rel), itup, nkeyattrs);
2115 BTreeTupleSetNAtts(truncated, nkeyattrs);
2121 * _bt_check_natts() -- Verify tuple has expected number of attributes.
2123 * Returns value indicating if the expected number of attributes were found
2124 * for a particular offset on page. This can be used as a general purpose
2127 * Testing a tuple directly with BTreeTupleGetNAtts() should generally be
2128 * preferred to calling here. That's usually more convenient, and is always
2129 * more explicit. Call here instead when offnum's tuple may be a negative
2130 * infinity tuple that uses the pre-v11 on-disk representation, or when a low
2131 * context check is appropriate.
2134 _bt_check_natts(Relation rel, Page page, OffsetNumber offnum)
2136 int16 natts = IndexRelationGetNumberOfAttributes(rel);
2137 int16 nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
2138 BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2142 * We cannot reliably test a deleted or half-deleted page, since they have
2145 if (P_IGNORE(opaque))
2148 Assert(offnum >= FirstOffsetNumber &&
2149 offnum <= PageGetMaxOffsetNumber(page));
2151 * Mask allocated for number of keys in index tuple must be able to fit
2152 * maximum possible number of index attributes
2154 StaticAssertStmt(BT_N_KEYS_OFFSET_MASK >= INDEX_MAX_KEYS,
2155 "BT_N_KEYS_OFFSET_MASK can't fit INDEX_MAX_KEYS");
2157 itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
2159 if (P_ISLEAF(opaque))
2161 if (offnum >= P_FIRSTDATAKEY(opaque))
2164 * Leaf tuples that are not the page high key (non-pivot tuples)
2165 * should never be truncated
2167 return BTreeTupleGetNAtts(itup, rel) == natts;
2172 * Rightmost page doesn't contain a page high key, so tuple was
2173 * checked above as ordinary leaf tuple
2175 Assert(!P_RIGHTMOST(opaque));
2177 /* Page high key tuple contains only key attributes */
2178 return BTreeTupleGetNAtts(itup, rel) == nkeyatts;
2181 else /* !P_ISLEAF(opaque) */
2183 if (offnum == P_FIRSTDATAKEY(opaque))
2186 * The first tuple on any internal page (possibly the first after
2187 * its high key) is its negative infinity tuple. Negative infinity
2188 * tuples are always truncated to zero attributes. They are a
2189 * particular kind of pivot tuple.
2191 * The number of attributes won't be explicitly represented if the
2192 * negative infinity tuple was generated during a page split that
2193 * occurred with a version of Postgres before v11. There must be a
2194 * problem when there is an explicit representation that is
2195 * non-zero, or when there is no explicit representation and the
2196 * tuple is evidently not a pre-pg_upgrade tuple.
2198 * Prior to v11, downlinks always had P_HIKEY as their offset. Use
2199 * that to decide if the tuple is a pre-v11 tuple.
2201 return BTreeTupleGetNAtts(itup, rel) == 0 ||
2202 ((itup->t_info & INDEX_ALT_TID_MASK) == 0 &&
2203 ItemPointerGetOffsetNumber(&(itup->t_tid)) == P_HIKEY);
2208 * Tuple contains only key attributes despite on is it page high
2211 return BTreeTupleGetNAtts(itup, rel) == nkeyatts;