1 /*-------------------------------------------------------------------------
4 * Utility code for Postgres btree implementation.
6 * Portions Copyright (c) 1996-2011, 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)
70 itupdesc = RelationGetDescr(rel);
71 natts = RelationGetNumberOfAttributes(rel);
72 indoption = rel->rd_indoption;
74 skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
76 for (i = 0; i < natts; i++)
84 * We can use the cached (default) support procs since no cross-type
85 * comparison can be needed.
87 procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
88 arg = index_getattr(itup, i + 1, itupdesc, &null);
89 flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
90 ScanKeyEntryInitializeWithInfo(&skey[i],
95 rel->rd_indcollation[i],
104 * _bt_mkscankey_nodata
105 * Build an insertion scan key that contains 3-way comparator routines
106 * appropriate to the key datatypes, but no comparison data. The
107 * comparison data ultimately used must match the key datatypes.
109 * The result cannot be used with _bt_compare(), unless comparison
110 * data is first stored into the key entries. Currently this
111 * routine is only called by nbtsort.c and tuplesort.c, which have
112 * their own comparison routines.
115 _bt_mkscankey_nodata(Relation rel)
122 natts = RelationGetNumberOfAttributes(rel);
123 indoption = rel->rd_indoption;
125 skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
127 for (i = 0; i < natts; i++)
133 * We can use the cached (default) support procs since no cross-type
134 * comparison can be needed.
136 procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
137 flags = SK_ISNULL | (indoption[i] << SK_BT_INDOPTION_SHIFT);
138 ScanKeyEntryInitializeWithInfo(&skey[i],
140 (AttrNumber) (i + 1),
143 rel->rd_indcollation[i],
152 * free a scan key made by either _bt_mkscankey or _bt_mkscankey_nodata.
155 _bt_freeskey(ScanKey skey)
161 * free a retracement stack made by _bt_search.
164 _bt_freestack(BTStack stack)
168 while (stack != NULL)
171 stack = stack->bts_parent;
178 * _bt_preprocess_array_keys() -- Preprocess SK_SEARCHARRAY scan keys
180 * If there are any SK_SEARCHARRAY scan keys, deconstruct the array(s) and
181 * set up BTArrayKeyInfo info for each one that is an equality-type key.
182 * Prepare modified scan keys in so->arrayKeyData, which will hold the current
183 * array elements during each primitive indexscan operation. For inequality
184 * array keys, it's sufficient to find the extreme element value and replace
185 * the whole array with that scalar value.
187 * Note: the reason we need so->arrayKeyData, rather than just scribbling
188 * on scan->keyData, is that callers are permitted to call btrescan without
189 * supplying a new set of scankey data.
192 _bt_preprocess_array_keys(IndexScanDesc scan)
194 BTScanOpaque so = (BTScanOpaque) scan->opaque;
195 int numberOfKeys = scan->numberOfKeys;
196 int16 *indoption = scan->indexRelation->rd_indoption;
200 MemoryContext oldContext;
202 /* Quick check to see if there are any array keys */
204 for (i = 0; i < numberOfKeys; i++)
206 cur = &scan->keyData[i];
207 if (cur->sk_flags & SK_SEARCHARRAY)
210 Assert(!(cur->sk_flags & (SK_ROW_HEADER | SK_SEARCHNULL | SK_SEARCHNOTNULL)));
211 /* If any arrays are null as a whole, we can quit right now. */
212 if (cur->sk_flags & SK_ISNULL)
214 so->numArrayKeys = -1;
215 so->arrayKeyData = NULL;
221 /* Quit if nothing to do. */
222 if (numArrayKeys == 0)
224 so->numArrayKeys = 0;
225 so->arrayKeyData = NULL;
230 * Make a scan-lifespan context to hold array-associated data, or reset
231 * it if we already have one from a previous rescan cycle.
233 if (so->arrayContext == NULL)
234 so->arrayContext = AllocSetContextCreate(CurrentMemoryContext,
235 "BTree Array Context",
236 ALLOCSET_SMALL_MINSIZE,
237 ALLOCSET_SMALL_INITSIZE,
238 ALLOCSET_SMALL_MAXSIZE);
240 MemoryContextReset(so->arrayContext);
242 oldContext = MemoryContextSwitchTo(so->arrayContext);
244 /* Create modifiable copy of scan->keyData in the workspace context */
245 so->arrayKeyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
246 memcpy(so->arrayKeyData,
248 scan->numberOfKeys * sizeof(ScanKeyData));
250 /* Allocate space for per-array data in the workspace context */
251 so->arrayKeys = (BTArrayKeyInfo *) palloc0(numArrayKeys * sizeof(BTArrayKeyInfo));
253 /* Now process each array key */
255 for (i = 0; i < numberOfKeys; i++)
267 cur = &so->arrayKeyData[i];
268 if (!(cur->sk_flags & SK_SEARCHARRAY))
272 * First, deconstruct the array into elements. Anything allocated
273 * here (including a possibly detoasted array value) is in the
276 arrayval = DatumGetArrayTypeP(cur->sk_argument);
277 /* We could cache this data, but not clear it's worth it */
278 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
279 &elmlen, &elmbyval, &elmalign);
280 deconstruct_array(arrayval,
281 ARR_ELEMTYPE(arrayval),
282 elmlen, elmbyval, elmalign,
283 &elem_values, &elem_nulls, &num_elems);
286 * Compress out any null elements. We can ignore them since we assume
287 * all btree operators are strict.
290 for (j = 0; j < num_elems; j++)
293 elem_values[num_nonnulls++] = elem_values[j];
296 /* We could pfree(elem_nulls) now, but not worth the cycles */
298 /* If there's no non-nulls, the scan qual is unsatisfiable */
299 if (num_nonnulls == 0)
306 * If the comparison operator is not equality, then the array qual
307 * degenerates to a simple comparison against the smallest or largest
308 * non-null array element, as appropriate.
310 switch (cur->sk_strategy)
312 case BTLessStrategyNumber:
313 case BTLessEqualStrategyNumber:
315 _bt_find_extreme_element(scan, cur,
316 BTGreaterStrategyNumber,
317 elem_values, num_nonnulls);
319 case BTEqualStrategyNumber:
320 /* proceed with rest of loop */
322 case BTGreaterEqualStrategyNumber:
323 case BTGreaterStrategyNumber:
325 _bt_find_extreme_element(scan, cur,
326 BTLessStrategyNumber,
327 elem_values, num_nonnulls);
330 elog(ERROR, "unrecognized StrategyNumber: %d",
331 (int) cur->sk_strategy);
336 * Sort the non-null elements and eliminate any duplicates. We must
337 * sort in the same ordering used by the index column, so that the
338 * successive primitive indexscans produce data in index order.
340 num_elems = _bt_sort_array_elements(scan, cur,
341 (indoption[cur->sk_attno - 1] & INDOPTION_DESC) != 0,
342 elem_values, num_nonnulls);
345 * And set up the BTArrayKeyInfo data.
347 so->arrayKeys[numArrayKeys].scan_key = i;
348 so->arrayKeys[numArrayKeys].num_elems = num_elems;
349 so->arrayKeys[numArrayKeys].elem_values = elem_values;
353 so->numArrayKeys = numArrayKeys;
355 MemoryContextSwitchTo(oldContext);
359 * _bt_find_extreme_element() -- get least or greatest array element
361 * scan and skey identify the index column, whose opfamily determines the
362 * comparison semantics. strat should be BTLessStrategyNumber to get the
363 * least element, or BTGreaterStrategyNumber to get the greatest.
366 _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey,
367 StrategyNumber strat,
368 Datum *elems, int nelems)
370 Relation rel = scan->indexRelation;
373 RegProcedure cmp_proc;
379 * Determine the nominal datatype of the array elements. We have to
380 * support the convention that sk_subtype == InvalidOid means the opclass
381 * input type; this is a hack to simplify life for ScanKeyInit().
383 elemtype = skey->sk_subtype;
384 if (elemtype == InvalidOid)
385 elemtype = rel->rd_opcintype[skey->sk_attno - 1];
388 * Look up the appropriate comparison operator in the opfamily.
390 * Note: it's possible that this would fail, if the opfamily is incomplete,
391 * but it seems quite unlikely that an opfamily would omit non-cross-type
392 * comparison operators for any datatype that it supports at all.
394 cmp_op = get_opfamily_member(rel->rd_opfamily[skey->sk_attno - 1],
398 if (!OidIsValid(cmp_op))
399 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
400 strat, elemtype, elemtype,
401 rel->rd_opfamily[skey->sk_attno - 1]);
402 cmp_proc = get_opcode(cmp_op);
403 if (!RegProcedureIsValid(cmp_proc))
404 elog(ERROR, "missing oprcode for operator %u", cmp_op);
406 fmgr_info(cmp_proc, &flinfo);
410 for (i = 1; i < nelems; i++)
412 if (DatumGetBool(FunctionCall2Coll(&flinfo,
423 * _bt_sort_array_elements() -- sort and de-dup array elements
425 * The array elements are sorted in-place, and the new number of elements
426 * after duplicate removal is returned.
428 * scan and skey identify the index column, whose opfamily determines the
429 * comparison semantics. If reverse is true, we sort in descending order.
432 _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey,
434 Datum *elems, int nelems)
436 Relation rel = scan->indexRelation;
438 RegProcedure cmp_proc;
439 BTSortArrayContext cxt;
444 return nelems; /* no work to do */
447 * Determine the nominal datatype of the array elements. We have to
448 * support the convention that sk_subtype == InvalidOid means the opclass
449 * input type; this is a hack to simplify life for ScanKeyInit().
451 elemtype = skey->sk_subtype;
452 if (elemtype == InvalidOid)
453 elemtype = rel->rd_opcintype[skey->sk_attno - 1];
456 * Look up the appropriate comparison function in the opfamily.
458 * Note: it's possible that this would fail, if the opfamily is incomplete,
459 * but it seems quite unlikely that an opfamily would omit non-cross-type
460 * support functions for any datatype that it supports at all.
462 cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
466 if (!RegProcedureIsValid(cmp_proc))
467 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
468 BTORDER_PROC, elemtype, elemtype,
469 rel->rd_opfamily[skey->sk_attno - 1]);
471 /* Sort the array elements */
472 fmgr_info(cmp_proc, &cxt.flinfo);
473 cxt.collation = skey->sk_collation;
474 cxt.reverse = reverse;
475 qsort_arg((void *) elems, nelems, sizeof(Datum),
476 _bt_compare_array_elements, (void *) &cxt);
478 /* Now scan the sorted elements and remove duplicates */
480 for (i = 1; i < nelems; i++)
484 compare = DatumGetInt32(FunctionCall2Coll(&cxt.flinfo,
489 elems[++last_non_dup] = elems[i];
492 return last_non_dup + 1;
496 * qsort_arg comparator for sorting array elements
499 _bt_compare_array_elements(const void *a, const void *b, void *arg)
501 Datum da = *((const Datum *) a);
502 Datum db = *((const Datum *) b);
503 BTSortArrayContext *cxt = (BTSortArrayContext *) arg;
506 compare = DatumGetInt32(FunctionCall2Coll(&cxt->flinfo,
515 * _bt_start_array_keys() -- Initialize array keys at start of a scan
517 * Set up the cur_elem counters and fill in the first sk_argument value for
518 * each array scankey. We can't do this until we know the scan direction.
521 _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
523 BTScanOpaque so = (BTScanOpaque) scan->opaque;
526 for (i = 0; i < so->numArrayKeys; i++)
528 BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
529 ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
531 Assert(curArrayKey->num_elems > 0);
532 if (ScanDirectionIsBackward(dir))
533 curArrayKey->cur_elem = curArrayKey->num_elems - 1;
535 curArrayKey->cur_elem = 0;
536 skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
541 * _bt_advance_array_keys() -- Advance to next set of array elements
543 * Returns TRUE if there is another set of values to consider, FALSE if not.
544 * On TRUE result, the scankeys are initialized with the next set of values.
547 _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir)
549 BTScanOpaque so = (BTScanOpaque) scan->opaque;
554 * We must advance the last array key most quickly, since it will
555 * correspond to the lowest-order index column among the available
556 * qualifications. This is necessary to ensure correct ordering of output
557 * when there are multiple array keys.
559 for (i = so->numArrayKeys - 1; i >= 0; i--)
561 BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
562 ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
563 int cur_elem = curArrayKey->cur_elem;
564 int num_elems = curArrayKey->num_elems;
566 if (ScanDirectionIsBackward(dir))
570 cur_elem = num_elems - 1;
571 found = false; /* need to advance next array key */
578 if (++cur_elem >= num_elems)
581 found = false; /* need to advance next array key */
587 curArrayKey->cur_elem = cur_elem;
588 skey->sk_argument = curArrayKey->elem_values[cur_elem];
598 * _bt_preprocess_keys() -- Preprocess scan keys
600 * The given search-type keys (in scan->keyData[] or so->arrayKeyData[])
601 * are copied to so->keyData[] with possible transformation.
602 * scan->numberOfKeys is the number of input keys, so->numberOfKeys gets
603 * the number of output keys (possibly less, never greater).
605 * The output keys are marked with additional sk_flag bits beyond the
606 * system-standard bits supplied by the caller. The DESC and NULLS_FIRST
607 * indoption bits for the relevant index attribute are copied into the flags.
608 * Also, for a DESC column, we commute (flip) all the sk_strategy numbers
609 * so that the index sorts in the desired direction.
611 * One key purpose of this routine is to discover how many scan keys
612 * must be satisfied to continue the scan. It also attempts to eliminate
613 * redundant keys and detect contradictory keys. (If the index opfamily
614 * provides incomplete sets of cross-type operators, we may fail to detect
615 * redundant or contradictory keys, but we can survive that.)
617 * The output keys must be sorted by index attribute. Presently we expect
618 * (but verify) that the input keys are already so sorted --- this is done
619 * by group_clauses_by_indexkey() in indxpath.c. Some reordering of the keys
620 * within each attribute may be done as a byproduct of the processing here,
621 * but no other code depends on that.
623 * The output keys are marked with flags SK_BT_REQFWD and/or SK_BT_REQBKWD
624 * if they must be satisfied in order to continue the scan forward or backward
625 * respectively. _bt_checkkeys uses these flags. For example, if the quals
626 * are "x = 1 AND y < 4 AND z < 5", then _bt_checkkeys will reject a tuple
627 * (1,2,7), but we must continue the scan in case there are tuples (1,3,z).
628 * But once we reach tuples like (1,4,z) we can stop scanning because no
629 * later tuples could match. This is reflected by marking the x and y keys,
630 * but not the z key, with SK_BT_REQFWD. In general, the keys for leading
631 * attributes with "=" keys are marked both SK_BT_REQFWD and SK_BT_REQBKWD.
632 * For the first attribute without an "=" key, any "<" and "<=" keys are
633 * marked SK_BT_REQFWD while any ">" and ">=" keys are marked SK_BT_REQBKWD.
634 * This can be seen to be correct by considering the above example. Note
635 * in particular that if there are no keys for a given attribute, the keys for
636 * subsequent attributes can never be required; for instance "WHERE y = 4"
637 * requires a full-index scan.
639 * If possible, redundant keys are eliminated: we keep only the tightest
640 * >/>= bound and the tightest </<= bound, and if there's an = key then
641 * that's the only one returned. (So, we return either a single = key,
642 * or one or two boundary-condition keys for each attr.) However, if we
643 * cannot compare two keys for lack of a suitable cross-type operator,
644 * we cannot eliminate either. If there are two such keys of the same
645 * operator strategy, the second one is just pushed into the output array
646 * without further processing here. We may also emit both >/>= or both
647 * </<= keys if we can't compare them. The logic about required keys still
648 * works if we don't eliminate redundant keys.
650 * As a byproduct of this work, we can detect contradictory quals such
651 * as "x = 1 AND x > 2". If we see that, we return so->qual_ok = FALSE,
652 * indicating the scan need not be run at all since no tuples can match.
653 * (In this case we do not bother completing the output key array!)
654 * Again, missing cross-type operators might cause us to fail to prove the
655 * quals contradictory when they really are, but the scan will work correctly.
657 * Row comparison keys are currently also treated without any smarts:
658 * we just transfer them into the preprocessed array without any
659 * editorialization. We can treat them the same as an ordinary inequality
660 * comparison on the row's first index column, for the purposes of the logic
661 * about required keys.
663 * Note: the reason we have to copy the preprocessed scan keys into private
664 * storage is that we are modifying the array based on comparisons of the
665 * key argument values, which could change on a rescan or after moving to
666 * new elements of array keys. Therefore we can't overwrite the source data.
669 _bt_preprocess_keys(IndexScanDesc scan)
671 BTScanOpaque so = (BTScanOpaque) scan->opaque;
672 int numberOfKeys = scan->numberOfKeys;
673 int16 *indoption = scan->indexRelation->rd_indoption;
674 int new_numberOfKeys;
675 int numberOfEqualCols;
679 ScanKey xform[BTMaxStrategyNumber];
685 /* initialize result variables */
687 so->numberOfKeys = 0;
689 if (numberOfKeys < 1)
690 return; /* done if qual-less scan */
693 * Read so->arrayKeyData if array keys are present, else scan->keyData
695 if (so->arrayKeyData != NULL)
696 inkeys = so->arrayKeyData;
698 inkeys = scan->keyData;
700 outkeys = so->keyData;
702 /* we check that input keys are correctly ordered */
703 if (cur->sk_attno < 1)
704 elog(ERROR, "btree index keys must be ordered by attribute");
706 /* We can short-circuit most of the work if there's just one key */
707 if (numberOfKeys == 1)
709 /* Apply indoption to scankey (might change sk_strategy!) */
710 if (!_bt_fix_scankey_strategy(cur, indoption))
712 memcpy(outkeys, cur, sizeof(ScanKeyData));
713 so->numberOfKeys = 1;
714 /* We can mark the qual as required if it's for first index col */
715 if (cur->sk_attno == 1)
716 _bt_mark_scankey_required(outkeys);
721 * Otherwise, do the full set of pushups.
723 new_numberOfKeys = 0;
724 numberOfEqualCols = 0;
727 * Initialize for processing of keys for attr 1.
729 * xform[i] points to the currently best scan key of strategy type i+1; it
730 * is NULL if we haven't yet found such a key for this attr.
733 memset(xform, 0, sizeof(xform));
736 * Loop iterates from 0 to numberOfKeys inclusive; we use the last pass to
737 * handle after-last-key processing. Actual exit from the loop is at the
738 * "break" statement below.
740 for (i = 0;; cur++, i++)
742 if (i < numberOfKeys)
744 /* Apply indoption to scankey (might change sk_strategy!) */
745 if (!_bt_fix_scankey_strategy(cur, indoption))
747 /* NULL can't be matched, so give up */
754 * If we are at the end of the keys for a particular attr, finish up
755 * processing and emit the cleaned-up keys.
757 if (i == numberOfKeys || cur->sk_attno != attno)
759 int priorNumberOfEqualCols = numberOfEqualCols;
761 /* check input keys are correctly ordered */
762 if (i < numberOfKeys && cur->sk_attno < attno)
763 elog(ERROR, "btree index keys must be ordered by attribute");
766 * If = has been specified, all other keys can be eliminated as
767 * redundant. If we have a case like key = 1 AND key > 2, we can
768 * set qual_ok to false and abandon further processing.
770 * We also have to deal with the case of "key IS NULL", which is
771 * unsatisfiable in combination with any other index condition.
772 * By the time we get here, that's been classified as an equality
773 * check, and we've rejected any combination of it with a regular
774 * equality condition; but not with other types of conditions.
776 if (xform[BTEqualStrategyNumber - 1])
778 ScanKey eq = xform[BTEqualStrategyNumber - 1];
780 for (j = BTMaxStrategyNumber; --j >= 0;)
782 ScanKey chk = xform[j];
784 if (!chk || j == (BTEqualStrategyNumber - 1))
787 if (eq->sk_flags & SK_SEARCHNULL)
789 /* IS NULL is contradictory to anything else */
794 if (_bt_compare_scankey_args(scan, chk, eq, chk,
799 /* keys proven mutually contradictory */
803 /* else discard the redundant non-equality key */
806 /* else, cannot determine redundancy, keep both keys */
808 /* track number of attrs for which we have "=" keys */
812 /* try to keep only one of <, <= */
813 if (xform[BTLessStrategyNumber - 1]
814 && xform[BTLessEqualStrategyNumber - 1])
816 ScanKey lt = xform[BTLessStrategyNumber - 1];
817 ScanKey le = xform[BTLessEqualStrategyNumber - 1];
819 if (_bt_compare_scankey_args(scan, le, lt, le,
823 xform[BTLessEqualStrategyNumber - 1] = NULL;
825 xform[BTLessStrategyNumber - 1] = NULL;
829 /* try to keep only one of >, >= */
830 if (xform[BTGreaterStrategyNumber - 1]
831 && xform[BTGreaterEqualStrategyNumber - 1])
833 ScanKey gt = xform[BTGreaterStrategyNumber - 1];
834 ScanKey ge = xform[BTGreaterEqualStrategyNumber - 1];
836 if (_bt_compare_scankey_args(scan, ge, gt, ge,
840 xform[BTGreaterEqualStrategyNumber - 1] = NULL;
842 xform[BTGreaterStrategyNumber - 1] = NULL;
847 * Emit the cleaned-up keys into the outkeys[] array, and then
848 * mark them if they are required. They are required (possibly
849 * only in one direction) if all attrs before this one had "=".
851 for (j = BTMaxStrategyNumber; --j >= 0;)
855 ScanKey outkey = &outkeys[new_numberOfKeys++];
857 memcpy(outkey, xform[j], sizeof(ScanKeyData));
858 if (priorNumberOfEqualCols == attno - 1)
859 _bt_mark_scankey_required(outkey);
864 * Exit loop here if done.
866 if (i == numberOfKeys)
869 /* Re-initialize for new attno */
870 attno = cur->sk_attno;
871 memset(xform, 0, sizeof(xform));
874 /* check strategy this key's operator corresponds to */
875 j = cur->sk_strategy - 1;
877 /* if row comparison, push it directly to the output array */
878 if (cur->sk_flags & SK_ROW_HEADER)
880 ScanKey outkey = &outkeys[new_numberOfKeys++];
882 memcpy(outkey, cur, sizeof(ScanKeyData));
883 if (numberOfEqualCols == attno - 1)
884 _bt_mark_scankey_required(outkey);
887 * We don't support RowCompare using equality; such a qual would
888 * mess up the numberOfEqualCols tracking.
890 Assert(j != (BTEqualStrategyNumber - 1));
894 /* have we seen one of these before? */
895 if (xform[j] == NULL)
897 /* nope, so remember this scankey */
902 /* yup, keep only the more restrictive key */
903 if (_bt_compare_scankey_args(scan, cur, cur, xform[j],
908 else if (j == (BTEqualStrategyNumber - 1))
910 /* key == a && key == b, but a != b */
914 /* else old key is more restrictive, keep it */
919 * We can't determine which key is more restrictive. Keep the
920 * previous one in xform[j] and push this one directly to the
923 ScanKey outkey = &outkeys[new_numberOfKeys++];
925 memcpy(outkey, cur, sizeof(ScanKeyData));
926 if (numberOfEqualCols == attno - 1)
927 _bt_mark_scankey_required(outkey);
932 so->numberOfKeys = new_numberOfKeys;
936 * Compare two scankey values using a specified operator.
938 * The test we want to perform is logically "leftarg op rightarg", where
939 * leftarg and rightarg are the sk_argument values in those ScanKeys, and
940 * the comparison operator is the one in the op ScanKey. However, in
941 * cross-data-type situations we may need to look up the correct operator in
942 * the index's opfamily: it is the one having amopstrategy = op->sk_strategy
943 * and amoplefttype/amoprighttype equal to the two argument datatypes.
945 * If the opfamily doesn't supply a complete set of cross-type operators we
946 * may not be able to make the comparison. If we can make the comparison
947 * we store the operator result in *result and return TRUE. We return FALSE
948 * if the comparison could not be made.
950 * Note: op always points at the same ScanKey as either leftarg or rightarg.
951 * Since we don't scribble on the scankeys, this aliasing should cause no
954 * Note: this routine needs to be insensitive to any DESC option applied
955 * to the index column. For example, "x < 4" is a tighter constraint than
956 * "x < 5" regardless of which way the index is sorted.
959 _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
960 ScanKey leftarg, ScanKey rightarg,
963 Relation rel = scan->indexRelation;
969 StrategyNumber strat;
972 * First, deal with cases where one or both args are NULL. This should
973 * only happen when the scankeys represent IS NULL/NOT NULL conditions.
975 if ((leftarg->sk_flags | rightarg->sk_flags) & SK_ISNULL)
980 if (leftarg->sk_flags & SK_ISNULL)
982 Assert(leftarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
987 if (rightarg->sk_flags & SK_ISNULL)
989 Assert(rightarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
996 * We treat NULL as either greater than or less than all other values.
997 * Since true > false, the tests below work correctly for NULLS LAST
998 * logic. If the index is NULLS FIRST, we need to flip the strategy.
1000 strat = op->sk_strategy;
1001 if (op->sk_flags & SK_BT_NULLS_FIRST)
1002 strat = BTCommuteStrategyNumber(strat);
1006 case BTLessStrategyNumber:
1007 *result = (leftnull < rightnull);
1009 case BTLessEqualStrategyNumber:
1010 *result = (leftnull <= rightnull);
1012 case BTEqualStrategyNumber:
1013 *result = (leftnull == rightnull);
1015 case BTGreaterEqualStrategyNumber:
1016 *result = (leftnull >= rightnull);
1018 case BTGreaterStrategyNumber:
1019 *result = (leftnull > rightnull);
1022 elog(ERROR, "unrecognized StrategyNumber: %d", (int) strat);
1023 *result = false; /* keep compiler quiet */
1030 * The opfamily we need to worry about is identified by the index column.
1032 Assert(leftarg->sk_attno == rightarg->sk_attno);
1034 opcintype = rel->rd_opcintype[leftarg->sk_attno - 1];
1037 * Determine the actual datatypes of the ScanKey arguments. We have to
1038 * support the convention that sk_subtype == InvalidOid means the opclass
1039 * input type; this is a hack to simplify life for ScanKeyInit().
1041 lefttype = leftarg->sk_subtype;
1042 if (lefttype == InvalidOid)
1043 lefttype = opcintype;
1044 righttype = rightarg->sk_subtype;
1045 if (righttype == InvalidOid)
1046 righttype = opcintype;
1047 optype = op->sk_subtype;
1048 if (optype == InvalidOid)
1052 * If leftarg and rightarg match the types expected for the "op" scankey,
1053 * we can use its already-looked-up comparison function.
1055 if (lefttype == opcintype && righttype == optype)
1057 *result = DatumGetBool(FunctionCall2Coll(&op->sk_func,
1059 leftarg->sk_argument,
1060 rightarg->sk_argument));
1065 * Otherwise, we need to go to the syscache to find the appropriate
1066 * operator. (This cannot result in infinite recursion, since no
1067 * indexscan initiated by syscache lookup will use cross-data-type
1070 * If the sk_strategy was flipped by _bt_fix_scankey_strategy, we have to
1071 * un-flip it to get the correct opfamily member.
1073 strat = op->sk_strategy;
1074 if (op->sk_flags & SK_BT_DESC)
1075 strat = BTCommuteStrategyNumber(strat);
1077 cmp_op = get_opfamily_member(rel->rd_opfamily[leftarg->sk_attno - 1],
1081 if (OidIsValid(cmp_op))
1083 RegProcedure cmp_proc = get_opcode(cmp_op);
1085 if (RegProcedureIsValid(cmp_proc))
1087 *result = DatumGetBool(OidFunctionCall2Coll(cmp_proc,
1089 leftarg->sk_argument,
1090 rightarg->sk_argument));
1095 /* Can't make the comparison */
1096 *result = false; /* suppress compiler warnings */
1101 * Adjust a scankey's strategy and flags setting as needed for indoptions.
1103 * We copy the appropriate indoption value into the scankey sk_flags
1104 * (shifting to avoid clobbering system-defined flag bits). Also, if
1105 * the DESC option is set, commute (flip) the operator strategy number.
1107 * A secondary purpose is to check for IS NULL/NOT NULL scankeys and set up
1108 * the strategy field correctly for them.
1110 * Lastly, for ordinary scankeys (not IS NULL/NOT NULL), we check for a
1111 * NULL comparison value. Since all btree operators are assumed strict,
1112 * a NULL means that the qual cannot be satisfied. We return TRUE if the
1113 * comparison value isn't NULL, or FALSE if the scan should be abandoned.
1115 * This function is applied to the *input* scankey structure; therefore
1116 * on a rescan we will be looking at already-processed scankeys. Hence
1117 * we have to be careful not to re-commute the strategy if we already did it.
1118 * It's a bit ugly to modify the caller's copy of the scankey but in practice
1119 * there shouldn't be any problem, since the index's indoptions are certainly
1120 * not going to change while the scankey survives.
1123 _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption)
1127 addflags = indoption[skey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1130 * We treat all btree operators as strict (even if they're not so marked
1131 * in pg_proc). This means that it is impossible for an operator condition
1132 * with a NULL comparison constant to succeed, and we can reject it right
1135 * However, we now also support "x IS NULL" clauses as search conditions,
1136 * so in that case keep going. The planner has not filled in any
1137 * particular strategy in this case, so set it to BTEqualStrategyNumber
1138 * --- we can treat IS NULL as an equality operator for purposes of search
1141 * Likewise, "x IS NOT NULL" is supported. We treat that as either "less
1142 * than NULL" in a NULLS LAST index, or "greater than NULL" in a NULLS
1145 * Note: someday we might have to fill in sk_collation from the index
1146 * column's collation. At the moment this is a non-issue because we'll
1147 * never actually call the comparison operator on a NULL.
1149 if (skey->sk_flags & SK_ISNULL)
1151 /* SK_ISNULL shouldn't be set in a row header scankey */
1152 Assert(!(skey->sk_flags & SK_ROW_HEADER));
1154 /* Set indoption flags in scankey (might be done already) */
1155 skey->sk_flags |= addflags;
1157 /* Set correct strategy for IS NULL or NOT NULL search */
1158 if (skey->sk_flags & SK_SEARCHNULL)
1160 skey->sk_strategy = BTEqualStrategyNumber;
1161 skey->sk_subtype = InvalidOid;
1162 skey->sk_collation = InvalidOid;
1164 else if (skey->sk_flags & SK_SEARCHNOTNULL)
1166 if (skey->sk_flags & SK_BT_NULLS_FIRST)
1167 skey->sk_strategy = BTGreaterStrategyNumber;
1169 skey->sk_strategy = BTLessStrategyNumber;
1170 skey->sk_subtype = InvalidOid;
1171 skey->sk_collation = InvalidOid;
1175 /* regular qual, so it cannot be satisfied */
1179 /* Needn't do the rest */
1183 /* Adjust strategy for DESC, if we didn't already */
1184 if ((addflags & SK_BT_DESC) && !(skey->sk_flags & SK_BT_DESC))
1185 skey->sk_strategy = BTCommuteStrategyNumber(skey->sk_strategy);
1186 skey->sk_flags |= addflags;
1188 /* If it's a row header, fix row member flags and strategies similarly */
1189 if (skey->sk_flags & SK_ROW_HEADER)
1191 ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1195 Assert(subkey->sk_flags & SK_ROW_MEMBER);
1196 addflags = indoption[subkey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1197 if ((addflags & SK_BT_DESC) && !(subkey->sk_flags & SK_BT_DESC))
1198 subkey->sk_strategy = BTCommuteStrategyNumber(subkey->sk_strategy);
1199 subkey->sk_flags |= addflags;
1200 if (subkey->sk_flags & SK_ROW_END)
1210 * Mark a scankey as "required to continue the scan".
1212 * Depending on the operator type, the key may be required for both scan
1213 * directions or just one. Also, if the key is a row comparison header,
1214 * we have to mark the appropriate subsidiary ScanKeys as required. In
1215 * such cases, the first subsidiary key is required, but subsequent ones
1216 * are required only as long as they correspond to successive index columns
1217 * and match the leading column as to sort direction.
1218 * Otherwise the row comparison ordering is different from the index ordering
1219 * and so we can't stop the scan on the basis of those lower-order columns.
1221 * Note: when we set required-key flag bits in a subsidiary scankey, we are
1222 * scribbling on a data structure belonging to the index AM's caller, not on
1223 * our private copy. This should be OK because the marking will not change
1224 * from scan to scan within a query, and so we'd just re-mark the same way
1225 * anyway on a rescan. Something to keep an eye on though.
1228 _bt_mark_scankey_required(ScanKey skey)
1232 switch (skey->sk_strategy)
1234 case BTLessStrategyNumber:
1235 case BTLessEqualStrategyNumber:
1236 addflags = SK_BT_REQFWD;
1238 case BTEqualStrategyNumber:
1239 addflags = SK_BT_REQFWD | SK_BT_REQBKWD;
1241 case BTGreaterEqualStrategyNumber:
1242 case BTGreaterStrategyNumber:
1243 addflags = SK_BT_REQBKWD;
1246 elog(ERROR, "unrecognized StrategyNumber: %d",
1247 (int) skey->sk_strategy);
1248 addflags = 0; /* keep compiler quiet */
1252 skey->sk_flags |= addflags;
1254 if (skey->sk_flags & SK_ROW_HEADER)
1256 ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1257 AttrNumber attno = skey->sk_attno;
1259 /* First subkey should be same as the header says */
1260 Assert(subkey->sk_attno == attno);
1264 Assert(subkey->sk_flags & SK_ROW_MEMBER);
1265 if (subkey->sk_attno != attno)
1266 break; /* non-adjacent key, so not required */
1267 if (subkey->sk_strategy != skey->sk_strategy)
1268 break; /* wrong direction, so not required */
1269 subkey->sk_flags |= addflags;
1270 if (subkey->sk_flags & SK_ROW_END)
1279 * Test whether an indextuple satisfies all the scankey conditions.
1281 * If so, return the address of the index tuple on the index page.
1282 * If not, return NULL.
1284 * If the tuple fails to pass the qual, we also determine whether there's
1285 * any need to continue the scan beyond this tuple, and set *continuescan
1286 * accordingly. See comments for _bt_preprocess_keys(), above, about how
1289 * scan: index scan descriptor (containing a search-type scankey)
1290 * page: buffer page containing index tuple
1291 * offnum: offset number of index tuple (must be a valid item!)
1292 * dir: direction we are scanning in
1293 * continuescan: output parameter (will be set correctly in all cases)
1295 * Caller must hold pin and lock on the index page.
1298 _bt_checkkeys(IndexScanDesc scan,
1299 Page page, OffsetNumber offnum,
1300 ScanDirection dir, bool *continuescan)
1302 ItemId iid = PageGetItemId(page, offnum);
1311 *continuescan = true; /* default assumption */
1314 * If the scan specifies not to return killed tuples, then we treat a
1315 * killed tuple as not passing the qual. Most of the time, it's a win to
1316 * not bother examining the tuple's index keys, but just return
1317 * immediately with continuescan = true to proceed to the next tuple.
1318 * However, if this is the last tuple on the page, we should check the
1319 * index keys to prevent uselessly advancing to the next page.
1321 if (scan->ignore_killed_tuples && ItemIdIsDead(iid))
1323 /* return immediately if there are more tuples on the page */
1324 if (ScanDirectionIsForward(dir))
1326 if (offnum < PageGetMaxOffsetNumber(page))
1331 BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1333 if (offnum > P_FIRSTDATAKEY(opaque))
1338 * OK, we want to check the keys so we can set continuescan correctly,
1339 * but we'll return NULL even if the tuple passes the key tests.
1341 tuple_alive = false;
1346 tuple = (IndexTuple) PageGetItem(page, iid);
1348 tupdesc = RelationGetDescr(scan->indexRelation);
1349 so = (BTScanOpaque) scan->opaque;
1350 keysz = so->numberOfKeys;
1352 for (key = so->keyData, ikey = 0; ikey < keysz; key++, ikey++)
1358 /* row-comparison keys need special processing */
1359 if (key->sk_flags & SK_ROW_HEADER)
1361 if (_bt_check_rowcompare(key, tuple, tupdesc, dir, continuescan))
1366 datum = index_getattr(tuple,
1371 if (key->sk_flags & SK_ISNULL)
1373 /* Handle IS NULL/NOT NULL tests */
1374 if (key->sk_flags & SK_SEARCHNULL)
1377 continue; /* tuple satisfies this qual */
1381 Assert(key->sk_flags & SK_SEARCHNOTNULL);
1383 continue; /* tuple satisfies this qual */
1387 * Tuple fails this qual. If it's a required qual for the current
1388 * scan direction, then we can conclude no further tuples will
1391 if ((key->sk_flags & SK_BT_REQFWD) &&
1392 ScanDirectionIsForward(dir))
1393 *continuescan = false;
1394 else if ((key->sk_flags & SK_BT_REQBKWD) &&
1395 ScanDirectionIsBackward(dir))
1396 *continuescan = false;
1399 * In any case, this indextuple doesn't match the qual.
1406 if (key->sk_flags & SK_BT_NULLS_FIRST)
1409 * Since NULLs are sorted before non-NULLs, we know we have
1410 * reached the lower limit of the range of values for this
1411 * index attr. On a backward scan, we can stop if this qual
1412 * is one of the "must match" subset. On a forward scan,
1413 * however, we should keep going.
1415 if ((key->sk_flags & SK_BT_REQBKWD) &&
1416 ScanDirectionIsBackward(dir))
1417 *continuescan = false;
1422 * Since NULLs are sorted after non-NULLs, we know we have
1423 * reached the upper limit of the range of values for this
1424 * index attr. On a forward scan, we can stop if this qual is
1425 * one of the "must match" subset. On a backward scan,
1426 * however, we should keep going.
1428 if ((key->sk_flags & SK_BT_REQFWD) &&
1429 ScanDirectionIsForward(dir))
1430 *continuescan = false;
1434 * In any case, this indextuple doesn't match the qual.
1439 test = FunctionCall2Coll(&key->sk_func, key->sk_collation,
1440 datum, key->sk_argument);
1442 if (!DatumGetBool(test))
1445 * Tuple fails this qual. If it's a required qual for the current
1446 * scan direction, then we can conclude no further tuples will
1449 * Note: because we stop the scan as soon as any required equality
1450 * qual fails, it is critical that equality quals be used for the
1451 * initial positioning in _bt_first() when they are available. See
1452 * comments in _bt_first().
1454 if ((key->sk_flags & SK_BT_REQFWD) &&
1455 ScanDirectionIsForward(dir))
1456 *continuescan = false;
1457 else if ((key->sk_flags & SK_BT_REQBKWD) &&
1458 ScanDirectionIsBackward(dir))
1459 *continuescan = false;
1462 * In any case, this indextuple doesn't match the qual.
1468 /* Check for failure due to it being a killed tuple. */
1472 /* If we get here, the tuple passes all index quals. */
1477 * Test whether an indextuple satisfies a row-comparison scan condition.
1479 * Return true if so, false if not. If not, also clear *continuescan if
1480 * it's not possible for any future tuples in the current scan direction
1483 * This is a subroutine for _bt_checkkeys, which see for more info.
1486 _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
1487 ScanDirection dir, bool *continuescan)
1489 ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1490 int32 cmpresult = 0;
1493 /* First subkey should be same as the header says */
1494 Assert(subkey->sk_attno == skey->sk_attno);
1496 /* Loop over columns of the row condition */
1502 Assert(subkey->sk_flags & SK_ROW_MEMBER);
1504 datum = index_getattr(tuple,
1511 if (subkey->sk_flags & SK_BT_NULLS_FIRST)
1514 * Since NULLs are sorted before non-NULLs, we know we have
1515 * reached the lower limit of the range of values for this
1516 * index attr. On a backward scan, we can stop if this qual is
1517 * one of the "must match" subset. On a forward scan,
1518 * however, we should keep going.
1520 if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1521 ScanDirectionIsBackward(dir))
1522 *continuescan = false;
1527 * Since NULLs are sorted after non-NULLs, we know we have
1528 * reached the upper limit of the range of values for this
1529 * index attr. On a forward scan, we can stop if this qual is
1530 * one of the "must match" subset. On a backward scan,
1531 * however, we should keep going.
1533 if ((subkey->sk_flags & SK_BT_REQFWD) &&
1534 ScanDirectionIsForward(dir))
1535 *continuescan = false;
1539 * In any case, this indextuple doesn't match the qual.
1544 if (subkey->sk_flags & SK_ISNULL)
1547 * Unlike the simple-scankey case, this isn't a disallowed case.
1548 * But it can never match. If all the earlier row comparison
1549 * columns are required for the scan direction, we can stop the
1550 * scan, because there can't be another tuple that will succeed.
1552 if (subkey != (ScanKey) DatumGetPointer(skey->sk_argument))
1554 if ((subkey->sk_flags & SK_BT_REQFWD) &&
1555 ScanDirectionIsForward(dir))
1556 *continuescan = false;
1557 else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1558 ScanDirectionIsBackward(dir))
1559 *continuescan = false;
1563 /* Perform the test --- three-way comparison not bool operator */
1564 cmpresult = DatumGetInt32(FunctionCall2Coll(&subkey->sk_func,
1565 subkey->sk_collation,
1567 subkey->sk_argument));
1569 if (subkey->sk_flags & SK_BT_DESC)
1570 cmpresult = -cmpresult;
1572 /* Done comparing if unequal, else advance to next column */
1576 if (subkey->sk_flags & SK_ROW_END)
1582 * At this point cmpresult indicates the overall result of the row
1583 * comparison, and subkey points to the deciding column (or the last
1584 * column if the result is "=").
1586 switch (subkey->sk_strategy)
1588 /* EQ and NE cases aren't allowed here */
1589 case BTLessStrategyNumber:
1590 result = (cmpresult < 0);
1592 case BTLessEqualStrategyNumber:
1593 result = (cmpresult <= 0);
1595 case BTGreaterEqualStrategyNumber:
1596 result = (cmpresult >= 0);
1598 case BTGreaterStrategyNumber:
1599 result = (cmpresult > 0);
1602 elog(ERROR, "unrecognized RowCompareType: %d",
1603 (int) subkey->sk_strategy);
1604 result = 0; /* keep compiler quiet */
1611 * Tuple fails this qual. If it's a required qual for the current
1612 * scan direction, then we can conclude no further tuples will pass,
1613 * either. Note we have to look at the deciding column, not
1614 * necessarily the first or last column of the row condition.
1616 if ((subkey->sk_flags & SK_BT_REQFWD) &&
1617 ScanDirectionIsForward(dir))
1618 *continuescan = false;
1619 else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1620 ScanDirectionIsBackward(dir))
1621 *continuescan = false;
1628 * _bt_killitems - set LP_DEAD state for items an indexscan caller has
1629 * told us were killed
1631 * scan->so contains information about the current page and killed tuples
1632 * thereon (generally, this should only be called if so->numKilled > 0).
1634 * The caller must have pin on so->currPos.buf, but may or may not have
1635 * read-lock, as indicated by haveLock. Note that we assume read-lock
1636 * is sufficient for setting LP_DEAD status (which is only a hint).
1638 * We match items by heap TID before assuming they are the right ones to
1639 * delete. We cope with cases where items have moved right due to insertions.
1640 * If an item has moved off the current page due to a split, we'll fail to
1641 * find it and do nothing (this is not an error case --- we assume the item
1642 * will eventually get marked in a future indexscan). Note that because we
1643 * hold pin on the target page continuously from initially reading the items
1644 * until applying this function, VACUUM cannot have deleted any items from
1645 * the page, and so there is no need to search left from the recorded offset.
1646 * (This observation also guarantees that the item is still the right one
1647 * to delete, which might otherwise be questionable since heap TIDs can get
1651 _bt_killitems(IndexScanDesc scan, bool haveLock)
1653 BTScanOpaque so = (BTScanOpaque) scan->opaque;
1655 BTPageOpaque opaque;
1656 OffsetNumber minoff;
1657 OffsetNumber maxoff;
1659 bool killedsomething = false;
1661 Assert(BufferIsValid(so->currPos.buf));
1664 LockBuffer(so->currPos.buf, BT_READ);
1666 page = BufferGetPage(so->currPos.buf);
1667 opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1668 minoff = P_FIRSTDATAKEY(opaque);
1669 maxoff = PageGetMaxOffsetNumber(page);
1671 for (i = 0; i < so->numKilled; i++)
1673 int itemIndex = so->killedItems[i];
1674 BTScanPosItem *kitem = &so->currPos.items[itemIndex];
1675 OffsetNumber offnum = kitem->indexOffset;
1677 Assert(itemIndex >= so->currPos.firstItem &&
1678 itemIndex <= so->currPos.lastItem);
1679 if (offnum < minoff)
1680 continue; /* pure paranoia */
1681 while (offnum <= maxoff)
1683 ItemId iid = PageGetItemId(page, offnum);
1684 IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
1686 if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
1688 /* found the item */
1689 ItemIdMarkDead(iid);
1690 killedsomething = true;
1691 break; /* out of inner search loop */
1693 offnum = OffsetNumberNext(offnum);
1698 * Since this can be redone later if needed, it's treated the same as a
1699 * commit-hint-bit status update for heap tuples: we mark the buffer dirty
1700 * but don't make a WAL log entry.
1702 * Whenever we mark anything LP_DEAD, we also set the page's
1703 * BTP_HAS_GARBAGE flag, which is likewise just a hint.
1705 if (killedsomething)
1707 opaque->btpo_flags |= BTP_HAS_GARBAGE;
1708 SetBufferCommitInfoNeedsSave(so->currPos.buf);
1712 LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK);
1715 * Always reset the scan state, so we don't look for same items on other
1723 * The following routines manage a shared-memory area in which we track
1724 * assignment of "vacuum cycle IDs" to currently-active btree vacuuming
1725 * operations. There is a single counter which increments each time we
1726 * start a vacuum to assign it a cycle ID. Since multiple vacuums could
1727 * be active concurrently, we have to track the cycle ID for each active
1728 * vacuum; this requires at most MaxBackends entries (usually far fewer).
1729 * We assume at most one vacuum can be active for a given index.
1731 * Access to the shared memory area is controlled by BtreeVacuumLock.
1732 * In principle we could use a separate lmgr locktag for each index,
1733 * but a single LWLock is much cheaper, and given the short time that
1734 * the lock is ever held, the concurrency hit should be minimal.
1737 typedef struct BTOneVacInfo
1739 LockRelId relid; /* global identifier of an index */
1740 BTCycleId cycleid; /* cycle ID for its active VACUUM */
1743 typedef struct BTVacInfo
1745 BTCycleId cycle_ctr; /* cycle ID most recently assigned */
1746 int num_vacuums; /* number of currently active VACUUMs */
1747 int max_vacuums; /* allocated length of vacuums[] array */
1748 BTOneVacInfo vacuums[1]; /* VARIABLE LENGTH ARRAY */
1751 static BTVacInfo *btvacinfo;
1755 * _bt_vacuum_cycleid --- get the active vacuum cycle ID for an index,
1756 * or zero if there is no active VACUUM
1758 * Note: for correct interlocking, the caller must already hold pin and
1759 * exclusive lock on each buffer it will store the cycle ID into. This
1760 * ensures that even if a VACUUM starts immediately afterwards, it cannot
1761 * process those pages until the page split is complete.
1764 _bt_vacuum_cycleid(Relation rel)
1766 BTCycleId result = 0;
1769 /* Share lock is enough since this is a read-only operation */
1770 LWLockAcquire(BtreeVacuumLock, LW_SHARED);
1772 for (i = 0; i < btvacinfo->num_vacuums; i++)
1774 BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1776 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1777 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1779 result = vac->cycleid;
1784 LWLockRelease(BtreeVacuumLock);
1789 * _bt_start_vacuum --- assign a cycle ID to a just-starting VACUUM operation
1791 * Note: the caller must guarantee that it will eventually call
1792 * _bt_end_vacuum, else we'll permanently leak an array slot. To ensure
1793 * that this happens even in elog(FATAL) scenarios, the appropriate coding
1794 * is not just a PG_TRY, but
1795 * PG_ENSURE_ERROR_CLEANUP(_bt_end_vacuum_callback, PointerGetDatum(rel))
1798 _bt_start_vacuum(Relation rel)
1804 LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1807 * Assign the next cycle ID, being careful to avoid zero as well as the
1808 * reserved high values.
1810 result = ++(btvacinfo->cycle_ctr);
1811 if (result == 0 || result > MAX_BT_CYCLE_ID)
1812 result = btvacinfo->cycle_ctr = 1;
1814 /* Let's just make sure there's no entry already for this index */
1815 for (i = 0; i < btvacinfo->num_vacuums; i++)
1817 vac = &btvacinfo->vacuums[i];
1818 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1819 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1822 * Unlike most places in the backend, we have to explicitly
1823 * release our LWLock before throwing an error. This is because
1824 * we expect _bt_end_vacuum() to be called before transaction
1825 * abort cleanup can run to release LWLocks.
1827 LWLockRelease(BtreeVacuumLock);
1828 elog(ERROR, "multiple active vacuums for index \"%s\"",
1829 RelationGetRelationName(rel));
1833 /* OK, add an entry */
1834 if (btvacinfo->num_vacuums >= btvacinfo->max_vacuums)
1836 LWLockRelease(BtreeVacuumLock);
1837 elog(ERROR, "out of btvacinfo slots");
1839 vac = &btvacinfo->vacuums[btvacinfo->num_vacuums];
1840 vac->relid = rel->rd_lockInfo.lockRelId;
1841 vac->cycleid = result;
1842 btvacinfo->num_vacuums++;
1844 LWLockRelease(BtreeVacuumLock);
1849 * _bt_end_vacuum --- mark a btree VACUUM operation as done
1851 * Note: this is deliberately coded not to complain if no entry is found;
1852 * this allows the caller to put PG_TRY around the start_vacuum operation.
1855 _bt_end_vacuum(Relation rel)
1859 LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1861 /* Find the array entry */
1862 for (i = 0; i < btvacinfo->num_vacuums; i++)
1864 BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1866 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1867 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1869 /* Remove it by shifting down the last entry */
1870 *vac = btvacinfo->vacuums[btvacinfo->num_vacuums - 1];
1871 btvacinfo->num_vacuums--;
1876 LWLockRelease(BtreeVacuumLock);
1880 * _bt_end_vacuum wrapped as an on_shmem_exit callback function
1883 _bt_end_vacuum_callback(int code, Datum arg)
1885 _bt_end_vacuum((Relation) DatumGetPointer(arg));
1889 * BTreeShmemSize --- report amount of shared memory space needed
1892 BTreeShmemSize(void)
1896 size = offsetof(BTVacInfo, vacuums[0]);
1897 size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
1902 * BTreeShmemInit --- initialize this module's shared memory
1905 BTreeShmemInit(void)
1909 btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
1913 if (!IsUnderPostmaster)
1915 /* Initialize shared memory area */
1919 * It doesn't really matter what the cycle counter starts at, but
1920 * having it always start the same doesn't seem good. Seed with
1921 * low-order bits of time() instead.
1923 btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
1925 btvacinfo->num_vacuums = 0;
1926 btvacinfo->max_vacuums = MaxBackends;
1933 btoptions(PG_FUNCTION_ARGS)
1935 Datum reloptions = PG_GETARG_DATUM(0);
1936 bool validate = PG_GETARG_BOOL(1);
1939 result = default_reloptions(reloptions, validate, RELOPT_KIND_BTREE);
1941 PG_RETURN_BYTEA_P(result);