PostgreSQL Source Code  git master
nbtutils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nbtutils.c
4  * Utility code for Postgres btree implementation.
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/access/nbtree/nbtutils.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include <time.h>
19 
20 #include "access/nbtree.h"
21 #include "access/reloptions.h"
22 #include "access/relscan.h"
23 #include "commands/progress.h"
24 #include "lib/qunique.h"
25 #include "miscadmin.h"
26 #include "utils/array.h"
27 #include "utils/datum.h"
28 #include "utils/lsyscache.h"
29 #include "utils/memutils.h"
30 #include "utils/rel.h"
31 
32 
33 typedef struct BTSortArrayContext
34 {
37  bool reverse;
39 
41  StrategyNumber strat,
42  Datum *elems, int nelems);
43 static int _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey,
44  bool reverse,
45  Datum *elems, int nelems);
46 static int _bt_compare_array_elements(const void *a, const void *b, void *arg);
48  ScanKey leftarg, ScanKey rightarg,
49  bool *result);
50 static bool _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption);
51 static void _bt_mark_scankey_required(ScanKey skey);
52 static bool _bt_check_rowcompare(ScanKey skey,
53  IndexTuple tuple, int tupnatts, TupleDesc tupdesc,
54  ScanDirection dir, bool *continuescan);
55 static int _bt_keep_natts(Relation rel, IndexTuple lastleft,
56  IndexTuple firstright, BTScanInsert itup_key);
57 
58 
59 /*
60  * _bt_mkscankey
61  * Build an insertion scan key that contains comparison data from itup
62  * as well as comparator routines appropriate to the key datatypes.
63  *
64  * When itup is a non-pivot tuple, the returned insertion scan key is
65  * suitable for finding a place for it to go on the leaf level. Pivot
66  * tuples can be used to re-find leaf page with matching high key, but
67  * then caller needs to set scan key's pivotsearch field to true. This
68  * allows caller to search for a leaf page with a matching high key,
69  * which is usually to the left of the first leaf page a non-pivot match
70  * might appear on.
71  *
72  * The result is intended for use with _bt_compare() and _bt_truncate().
73  * Callers that don't need to fill out the insertion scankey arguments
74  * (e.g. they use an ad-hoc comparison routine, or only need a scankey
75  * for _bt_truncate()) can pass a NULL index tuple. The scankey will
76  * be initialized as if an "all truncated" pivot tuple was passed
77  * instead.
78  *
79  * Note that we may occasionally have to share lock the metapage to
80  * determine whether or not the keys in the index are expected to be
81  * unique (i.e. if this is a "heapkeyspace" index). We assume a
82  * heapkeyspace index when caller passes a NULL tuple, allowing index
83  * build callers to avoid accessing the non-existent metapage.
84  */
87 {
89  ScanKey skey;
90  TupleDesc itupdesc;
91  int indnkeyatts;
92  int16 *indoption;
93  int tupnatts;
94  int i;
95 
96  itupdesc = RelationGetDescr(rel);
97  indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
98  indoption = rel->rd_indoption;
99  tupnatts = itup ? BTreeTupleGetNAtts(itup, rel) : 0;
100 
101  Assert(tupnatts <= IndexRelationGetNumberOfAttributes(rel));
102 
103  /*
104  * We'll execute search using scan key constructed on key columns.
105  * Truncated attributes and non-key attributes are omitted from the final
106  * scan key.
107  */
108  key = palloc(offsetof(BTScanInsertData, scankeys) +
109  sizeof(ScanKeyData) * indnkeyatts);
110  key->heapkeyspace = itup == NULL || _bt_heapkeyspace(rel);
111  key->anynullkeys = false; /* initial assumption */
112  key->nextkey = false;
113  key->pivotsearch = false;
114  key->keysz = Min(indnkeyatts, tupnatts);
115  key->scantid = key->heapkeyspace && itup ?
116  BTreeTupleGetHeapTID(itup) : NULL;
117  skey = key->scankeys;
118  for (i = 0; i < indnkeyatts; i++)
119  {
120  FmgrInfo *procinfo;
121  Datum arg;
122  bool null;
123  int flags;
124 
125  /*
126  * We can use the cached (default) support procs since no cross-type
127  * comparison can be needed.
128  */
129  procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
130 
131  /*
132  * Key arguments built from truncated attributes (or when caller
133  * provides no tuple) are defensively represented as NULL values. They
134  * should never be used.
135  */
136  if (i < tupnatts)
137  arg = index_getattr(itup, i + 1, itupdesc, &null);
138  else
139  {
140  arg = (Datum) 0;
141  null = true;
142  }
143  flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
145  flags,
146  (AttrNumber) (i + 1),
148  InvalidOid,
149  rel->rd_indcollation[i],
150  procinfo,
151  arg);
152  /* Record if any key attribute is NULL (or truncated) */
153  if (null)
154  key->anynullkeys = true;
155  }
156 
157  return key;
158 }
159 
160 /*
161  * free a retracement stack made by _bt_search.
162  */
163 void
165 {
166  BTStack ostack;
167 
168  while (stack != NULL)
169  {
170  ostack = stack;
171  stack = stack->bts_parent;
172  pfree(ostack);
173  }
174 }
175 
176 
177 /*
178  * _bt_preprocess_array_keys() -- Preprocess SK_SEARCHARRAY scan keys
179  *
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.
186  *
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.
190  */
191 void
193 {
194  BTScanOpaque so = (BTScanOpaque) scan->opaque;
195  int numberOfKeys = scan->numberOfKeys;
196  int16 *indoption = scan->indexRelation->rd_indoption;
197  int numArrayKeys;
198  ScanKey cur;
199  int i;
200  MemoryContext oldContext;
201 
202  /* Quick check to see if there are any array keys */
203  numArrayKeys = 0;
204  for (i = 0; i < numberOfKeys; i++)
205  {
206  cur = &scan->keyData[i];
207  if (cur->sk_flags & SK_SEARCHARRAY)
208  {
209  numArrayKeys++;
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)
213  {
214  so->numArrayKeys = -1;
215  so->arrayKeyData = NULL;
216  return;
217  }
218  }
219  }
220 
221  /* Quit if nothing to do. */
222  if (numArrayKeys == 0)
223  {
224  so->numArrayKeys = 0;
225  so->arrayKeyData = NULL;
226  return;
227  }
228 
229  /*
230  * Make a scan-lifespan context to hold array-associated data, or reset it
231  * if we already have one from a previous rescan cycle.
232  */
233  if (so->arrayContext == NULL)
235  "BTree array context",
237  else
239 
240  oldContext = MemoryContextSwitchTo(so->arrayContext);
241 
242  /* Create modifiable copy of scan->keyData in the workspace context */
243  so->arrayKeyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
244  memcpy(so->arrayKeyData,
245  scan->keyData,
246  scan->numberOfKeys * sizeof(ScanKeyData));
247 
248  /* Allocate space for per-array data in the workspace context */
249  so->arrayKeys = (BTArrayKeyInfo *) palloc0(numArrayKeys * sizeof(BTArrayKeyInfo));
250 
251  /* Now process each array key */
252  numArrayKeys = 0;
253  for (i = 0; i < numberOfKeys; i++)
254  {
255  ArrayType *arrayval;
256  int16 elmlen;
257  bool elmbyval;
258  char elmalign;
259  int num_elems;
260  Datum *elem_values;
261  bool *elem_nulls;
262  int num_nonnulls;
263  int j;
264 
265  cur = &so->arrayKeyData[i];
266  if (!(cur->sk_flags & SK_SEARCHARRAY))
267  continue;
268 
269  /*
270  * First, deconstruct the array into elements. Anything allocated
271  * here (including a possibly detoasted array value) is in the
272  * workspace context.
273  */
274  arrayval = DatumGetArrayTypeP(cur->sk_argument);
275  /* We could cache this data, but not clear it's worth it */
277  &elmlen, &elmbyval, &elmalign);
278  deconstruct_array(arrayval,
279  ARR_ELEMTYPE(arrayval),
280  elmlen, elmbyval, elmalign,
281  &elem_values, &elem_nulls, &num_elems);
282 
283  /*
284  * Compress out any null elements. We can ignore them since we assume
285  * all btree operators are strict.
286  */
287  num_nonnulls = 0;
288  for (j = 0; j < num_elems; j++)
289  {
290  if (!elem_nulls[j])
291  elem_values[num_nonnulls++] = elem_values[j];
292  }
293 
294  /* We could pfree(elem_nulls) now, but not worth the cycles */
295 
296  /* If there's no non-nulls, the scan qual is unsatisfiable */
297  if (num_nonnulls == 0)
298  {
299  numArrayKeys = -1;
300  break;
301  }
302 
303  /*
304  * If the comparison operator is not equality, then the array qual
305  * degenerates to a simple comparison against the smallest or largest
306  * non-null array element, as appropriate.
307  */
308  switch (cur->sk_strategy)
309  {
312  cur->sk_argument =
315  elem_values, num_nonnulls);
316  continue;
318  /* proceed with rest of loop */
319  break;
322  cur->sk_argument =
325  elem_values, num_nonnulls);
326  continue;
327  default:
328  elog(ERROR, "unrecognized StrategyNumber: %d",
329  (int) cur->sk_strategy);
330  break;
331  }
332 
333  /*
334  * Sort the non-null elements and eliminate any duplicates. We must
335  * sort in the same ordering used by the index column, so that the
336  * successive primitive indexscans produce data in index order.
337  */
338  num_elems = _bt_sort_array_elements(scan, cur,
339  (indoption[cur->sk_attno - 1] & INDOPTION_DESC) != 0,
340  elem_values, num_nonnulls);
341 
342  /*
343  * And set up the BTArrayKeyInfo data.
344  */
345  so->arrayKeys[numArrayKeys].scan_key = i;
346  so->arrayKeys[numArrayKeys].num_elems = num_elems;
347  so->arrayKeys[numArrayKeys].elem_values = elem_values;
348  numArrayKeys++;
349  }
350 
351  so->numArrayKeys = numArrayKeys;
352 
353  MemoryContextSwitchTo(oldContext);
354 }
355 
356 /*
357  * _bt_find_extreme_element() -- get least or greatest array element
358  *
359  * scan and skey identify the index column, whose opfamily determines the
360  * comparison semantics. strat should be BTLessStrategyNumber to get the
361  * least element, or BTGreaterStrategyNumber to get the greatest.
362  */
363 static Datum
365  StrategyNumber strat,
366  Datum *elems, int nelems)
367 {
368  Relation rel = scan->indexRelation;
369  Oid elemtype,
370  cmp_op;
371  RegProcedure cmp_proc;
373  Datum result;
374  int i;
375 
376  /*
377  * Determine the nominal datatype of the array elements. We have to
378  * support the convention that sk_subtype == InvalidOid means the opclass
379  * input type; this is a hack to simplify life for ScanKeyInit().
380  */
381  elemtype = skey->sk_subtype;
382  if (elemtype == InvalidOid)
383  elemtype = rel->rd_opcintype[skey->sk_attno - 1];
384 
385  /*
386  * Look up the appropriate comparison operator in the opfamily.
387  *
388  * Note: it's possible that this would fail, if the opfamily is
389  * incomplete, but it seems quite unlikely that an opfamily would omit
390  * non-cross-type comparison operators for any datatype that it supports
391  * at all.
392  */
393  cmp_op = get_opfamily_member(rel->rd_opfamily[skey->sk_attno - 1],
394  elemtype,
395  elemtype,
396  strat);
397  if (!OidIsValid(cmp_op))
398  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
399  strat, elemtype, elemtype,
400  rel->rd_opfamily[skey->sk_attno - 1]);
401  cmp_proc = get_opcode(cmp_op);
402  if (!RegProcedureIsValid(cmp_proc))
403  elog(ERROR, "missing oprcode for operator %u", cmp_op);
404 
405  fmgr_info(cmp_proc, &flinfo);
406 
407  Assert(nelems > 0);
408  result = elems[0];
409  for (i = 1; i < nelems; i++)
410  {
411  if (DatumGetBool(FunctionCall2Coll(&flinfo,
412  skey->sk_collation,
413  elems[i],
414  result)))
415  result = elems[i];
416  }
417 
418  return result;
419 }
420 
421 /*
422  * _bt_sort_array_elements() -- sort and de-dup array elements
423  *
424  * The array elements are sorted in-place, and the new number of elements
425  * after duplicate removal is returned.
426  *
427  * scan and skey identify the index column, whose opfamily determines the
428  * comparison semantics. If reverse is true, we sort in descending order.
429  */
430 static int
432  bool reverse,
433  Datum *elems, int nelems)
434 {
435  Relation rel = scan->indexRelation;
436  Oid elemtype;
437  RegProcedure cmp_proc;
438  BTSortArrayContext cxt;
439 
440  if (nelems <= 1)
441  return nelems; /* no work to do */
442 
443  /*
444  * Determine the nominal datatype of the array elements. We have to
445  * support the convention that sk_subtype == InvalidOid means the opclass
446  * input type; this is a hack to simplify life for ScanKeyInit().
447  */
448  elemtype = skey->sk_subtype;
449  if (elemtype == InvalidOid)
450  elemtype = rel->rd_opcintype[skey->sk_attno - 1];
451 
452  /*
453  * Look up the appropriate comparison function in the opfamily.
454  *
455  * Note: it's possible that this would fail, if the opfamily is
456  * incomplete, but it seems quite unlikely that an opfamily would omit
457  * non-cross-type support functions for any datatype that it supports at
458  * all.
459  */
460  cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
461  elemtype,
462  elemtype,
463  BTORDER_PROC);
464  if (!RegProcedureIsValid(cmp_proc))
465  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
466  BTORDER_PROC, elemtype, elemtype,
467  rel->rd_opfamily[skey->sk_attno - 1]);
468 
469  /* Sort the array elements */
470  fmgr_info(cmp_proc, &cxt.flinfo);
471  cxt.collation = skey->sk_collation;
472  cxt.reverse = reverse;
473  qsort_arg((void *) elems, nelems, sizeof(Datum),
474  _bt_compare_array_elements, (void *) &cxt);
475 
476  /* Now scan the sorted elements and remove duplicates */
477  return qunique_arg(elems, nelems, sizeof(Datum),
479 }
480 
481 /*
482  * qsort_arg comparator for sorting array elements
483  */
484 static int
485 _bt_compare_array_elements(const void *a, const void *b, void *arg)
486 {
487  Datum da = *((const Datum *) a);
488  Datum db = *((const Datum *) b);
489  BTSortArrayContext *cxt = (BTSortArrayContext *) arg;
490  int32 compare;
491 
492  compare = DatumGetInt32(FunctionCall2Coll(&cxt->flinfo,
493  cxt->collation,
494  da, db));
495  if (cxt->reverse)
496  INVERT_COMPARE_RESULT(compare);
497  return compare;
498 }
499 
500 /*
501  * _bt_start_array_keys() -- Initialize array keys at start of a scan
502  *
503  * Set up the cur_elem counters and fill in the first sk_argument value for
504  * each array scankey. We can't do this until we know the scan direction.
505  */
506 void
508 {
509  BTScanOpaque so = (BTScanOpaque) scan->opaque;
510  int i;
511 
512  for (i = 0; i < so->numArrayKeys; i++)
513  {
514  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
515  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
516 
517  Assert(curArrayKey->num_elems > 0);
518  if (ScanDirectionIsBackward(dir))
519  curArrayKey->cur_elem = curArrayKey->num_elems - 1;
520  else
521  curArrayKey->cur_elem = 0;
522  skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
523  }
524 }
525 
526 /*
527  * _bt_advance_array_keys() -- Advance to next set of array elements
528  *
529  * Returns true if there is another set of values to consider, false if not.
530  * On true result, the scankeys are initialized with the next set of values.
531  */
532 bool
534 {
535  BTScanOpaque so = (BTScanOpaque) scan->opaque;
536  bool found = false;
537  int i;
538 
539  /*
540  * We must advance the last array key most quickly, since it will
541  * correspond to the lowest-order index column among the available
542  * qualifications. This is necessary to ensure correct ordering of output
543  * when there are multiple array keys.
544  */
545  for (i = so->numArrayKeys - 1; i >= 0; i--)
546  {
547  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
548  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
549  int cur_elem = curArrayKey->cur_elem;
550  int num_elems = curArrayKey->num_elems;
551 
552  if (ScanDirectionIsBackward(dir))
553  {
554  if (--cur_elem < 0)
555  {
556  cur_elem = num_elems - 1;
557  found = false; /* need to advance next array key */
558  }
559  else
560  found = true;
561  }
562  else
563  {
564  if (++cur_elem >= num_elems)
565  {
566  cur_elem = 0;
567  found = false; /* need to advance next array key */
568  }
569  else
570  found = true;
571  }
572 
573  curArrayKey->cur_elem = cur_elem;
574  skey->sk_argument = curArrayKey->elem_values[cur_elem];
575  if (found)
576  break;
577  }
578 
579  /* advance parallel scan */
580  if (scan->parallel_scan != NULL)
582 
583  return found;
584 }
585 
586 /*
587  * _bt_mark_array_keys() -- Handle array keys during btmarkpos
588  *
589  * Save the current state of the array keys as the "mark" position.
590  */
591 void
593 {
594  BTScanOpaque so = (BTScanOpaque) scan->opaque;
595  int i;
596 
597  for (i = 0; i < so->numArrayKeys; i++)
598  {
599  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
600 
601  curArrayKey->mark_elem = curArrayKey->cur_elem;
602  }
603 }
604 
605 /*
606  * _bt_restore_array_keys() -- Handle array keys during btrestrpos
607  *
608  * Restore the array keys to where they were when the mark was set.
609  */
610 void
612 {
613  BTScanOpaque so = (BTScanOpaque) scan->opaque;
614  bool changed = false;
615  int i;
616 
617  /* Restore each array key to its position when the mark was set */
618  for (i = 0; i < so->numArrayKeys; i++)
619  {
620  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
621  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
622  int mark_elem = curArrayKey->mark_elem;
623 
624  if (curArrayKey->cur_elem != mark_elem)
625  {
626  curArrayKey->cur_elem = mark_elem;
627  skey->sk_argument = curArrayKey->elem_values[mark_elem];
628  changed = true;
629  }
630  }
631 
632  /*
633  * If we changed any keys, we must redo _bt_preprocess_keys. That might
634  * sound like overkill, but in cases with multiple keys per index column
635  * it seems necessary to do the full set of pushups.
636  */
637  if (changed)
638  {
639  _bt_preprocess_keys(scan);
640  /* The mark should have been set on a consistent set of keys... */
641  Assert(so->qual_ok);
642  }
643 }
644 
645 
646 /*
647  * _bt_preprocess_keys() -- Preprocess scan keys
648  *
649  * The given search-type keys (in scan->keyData[] or so->arrayKeyData[])
650  * are copied to so->keyData[] with possible transformation.
651  * scan->numberOfKeys is the number of input keys, so->numberOfKeys gets
652  * the number of output keys (possibly less, never greater).
653  *
654  * The output keys are marked with additional sk_flags bits beyond the
655  * system-standard bits supplied by the caller. The DESC and NULLS_FIRST
656  * indoption bits for the relevant index attribute are copied into the flags.
657  * Also, for a DESC column, we commute (flip) all the sk_strategy numbers
658  * so that the index sorts in the desired direction.
659  *
660  * One key purpose of this routine is to discover which scan keys must be
661  * satisfied to continue the scan. It also attempts to eliminate redundant
662  * keys and detect contradictory keys. (If the index opfamily provides
663  * incomplete sets of cross-type operators, we may fail to detect redundant
664  * or contradictory keys, but we can survive that.)
665  *
666  * The output keys must be sorted by index attribute. Presently we expect
667  * (but verify) that the input keys are already so sorted --- this is done
668  * by match_clauses_to_index() in indxpath.c. Some reordering of the keys
669  * within each attribute may be done as a byproduct of the processing here,
670  * but no other code depends on that.
671  *
672  * The output keys are marked with flags SK_BT_REQFWD and/or SK_BT_REQBKWD
673  * if they must be satisfied in order to continue the scan forward or backward
674  * respectively. _bt_checkkeys uses these flags. For example, if the quals
675  * are "x = 1 AND y < 4 AND z < 5", then _bt_checkkeys will reject a tuple
676  * (1,2,7), but we must continue the scan in case there are tuples (1,3,z).
677  * But once we reach tuples like (1,4,z) we can stop scanning because no
678  * later tuples could match. This is reflected by marking the x and y keys,
679  * but not the z key, with SK_BT_REQFWD. In general, the keys for leading
680  * attributes with "=" keys are marked both SK_BT_REQFWD and SK_BT_REQBKWD.
681  * For the first attribute without an "=" key, any "<" and "<=" keys are
682  * marked SK_BT_REQFWD while any ">" and ">=" keys are marked SK_BT_REQBKWD.
683  * This can be seen to be correct by considering the above example. Note
684  * in particular that if there are no keys for a given attribute, the keys for
685  * subsequent attributes can never be required; for instance "WHERE y = 4"
686  * requires a full-index scan.
687  *
688  * If possible, redundant keys are eliminated: we keep only the tightest
689  * >/>= bound and the tightest </<= bound, and if there's an = key then
690  * that's the only one returned. (So, we return either a single = key,
691  * or one or two boundary-condition keys for each attr.) However, if we
692  * cannot compare two keys for lack of a suitable cross-type operator,
693  * we cannot eliminate either. If there are two such keys of the same
694  * operator strategy, the second one is just pushed into the output array
695  * without further processing here. We may also emit both >/>= or both
696  * </<= keys if we can't compare them. The logic about required keys still
697  * works if we don't eliminate redundant keys.
698  *
699  * Note that one reason we need direction-sensitive required-key flags is
700  * precisely that we may not be able to eliminate redundant keys. Suppose
701  * we have "x > 4::int AND x > 10::bigint", and we are unable to determine
702  * which key is more restrictive for lack of a suitable cross-type operator.
703  * _bt_first will arbitrarily pick one of the keys to do the initial
704  * positioning with. If it picks x > 4, then the x > 10 condition will fail
705  * until we reach index entries > 10; but we can't stop the scan just because
706  * x > 10 is failing. On the other hand, if we are scanning backwards, then
707  * failure of either key is indeed enough to stop the scan. (In general, when
708  * inequality keys are present, the initial-positioning code only promises to
709  * position before the first possible match, not exactly at the first match,
710  * for a forward scan; or after the last match for a backward scan.)
711  *
712  * As a byproduct of this work, we can detect contradictory quals such
713  * as "x = 1 AND x > 2". If we see that, we return so->qual_ok = false,
714  * indicating the scan need not be run at all since no tuples can match.
715  * (In this case we do not bother completing the output key array!)
716  * Again, missing cross-type operators might cause us to fail to prove the
717  * quals contradictory when they really are, but the scan will work correctly.
718  *
719  * Row comparison keys are currently also treated without any smarts:
720  * we just transfer them into the preprocessed array without any
721  * editorialization. We can treat them the same as an ordinary inequality
722  * comparison on the row's first index column, for the purposes of the logic
723  * about required keys.
724  *
725  * Note: the reason we have to copy the preprocessed scan keys into private
726  * storage is that we are modifying the array based on comparisons of the
727  * key argument values, which could change on a rescan or after moving to
728  * new elements of array keys. Therefore we can't overwrite the source data.
729  */
730 void
732 {
733  BTScanOpaque so = (BTScanOpaque) scan->opaque;
734  int numberOfKeys = scan->numberOfKeys;
735  int16 *indoption = scan->indexRelation->rd_indoption;
736  int new_numberOfKeys;
737  int numberOfEqualCols;
738  ScanKey inkeys;
739  ScanKey outkeys;
740  ScanKey cur;
742  bool test_result;
743  int i,
744  j;
745  AttrNumber attno;
746 
747  /* initialize result variables */
748  so->qual_ok = true;
749  so->numberOfKeys = 0;
750 
751  if (numberOfKeys < 1)
752  return; /* done if qual-less scan */
753 
754  /*
755  * Read so->arrayKeyData if array keys are present, else scan->keyData
756  */
757  if (so->arrayKeyData != NULL)
758  inkeys = so->arrayKeyData;
759  else
760  inkeys = scan->keyData;
761 
762  outkeys = so->keyData;
763  cur = &inkeys[0];
764  /* we check that input keys are correctly ordered */
765  if (cur->sk_attno < 1)
766  elog(ERROR, "btree index keys must be ordered by attribute");
767 
768  /* We can short-circuit most of the work if there's just one key */
769  if (numberOfKeys == 1)
770  {
771  /* Apply indoption to scankey (might change sk_strategy!) */
772  if (!_bt_fix_scankey_strategy(cur, indoption))
773  so->qual_ok = false;
774  memcpy(outkeys, cur, sizeof(ScanKeyData));
775  so->numberOfKeys = 1;
776  /* We can mark the qual as required if it's for first index col */
777  if (cur->sk_attno == 1)
778  _bt_mark_scankey_required(outkeys);
779  return;
780  }
781 
782  /*
783  * Otherwise, do the full set of pushups.
784  */
785  new_numberOfKeys = 0;
786  numberOfEqualCols = 0;
787 
788  /*
789  * Initialize for processing of keys for attr 1.
790  *
791  * xform[i] points to the currently best scan key of strategy type i+1; it
792  * is NULL if we haven't yet found such a key for this attr.
793  */
794  attno = 1;
795  memset(xform, 0, sizeof(xform));
796 
797  /*
798  * Loop iterates from 0 to numberOfKeys inclusive; we use the last pass to
799  * handle after-last-key processing. Actual exit from the loop is at the
800  * "break" statement below.
801  */
802  for (i = 0;; cur++, i++)
803  {
804  if (i < numberOfKeys)
805  {
806  /* Apply indoption to scankey (might change sk_strategy!) */
807  if (!_bt_fix_scankey_strategy(cur, indoption))
808  {
809  /* NULL can't be matched, so give up */
810  so->qual_ok = false;
811  return;
812  }
813  }
814 
815  /*
816  * If we are at the end of the keys for a particular attr, finish up
817  * processing and emit the cleaned-up keys.
818  */
819  if (i == numberOfKeys || cur->sk_attno != attno)
820  {
821  int priorNumberOfEqualCols = numberOfEqualCols;
822 
823  /* check input keys are correctly ordered */
824  if (i < numberOfKeys && cur->sk_attno < attno)
825  elog(ERROR, "btree index keys must be ordered by attribute");
826 
827  /*
828  * If = has been specified, all other keys can be eliminated as
829  * redundant. If we have a case like key = 1 AND key > 2, we can
830  * set qual_ok to false and abandon further processing.
831  *
832  * We also have to deal with the case of "key IS NULL", which is
833  * unsatisfiable in combination with any other index condition. By
834  * the time we get here, that's been classified as an equality
835  * check, and we've rejected any combination of it with a regular
836  * equality condition; but not with other types of conditions.
837  */
838  if (xform[BTEqualStrategyNumber - 1])
839  {
840  ScanKey eq = xform[BTEqualStrategyNumber - 1];
841 
842  for (j = BTMaxStrategyNumber; --j >= 0;)
843  {
844  ScanKey chk = xform[j];
845 
846  if (!chk || j == (BTEqualStrategyNumber - 1))
847  continue;
848 
849  if (eq->sk_flags & SK_SEARCHNULL)
850  {
851  /* IS NULL is contradictory to anything else */
852  so->qual_ok = false;
853  return;
854  }
855 
856  if (_bt_compare_scankey_args(scan, chk, eq, chk,
857  &test_result))
858  {
859  if (!test_result)
860  {
861  /* keys proven mutually contradictory */
862  so->qual_ok = false;
863  return;
864  }
865  /* else discard the redundant non-equality key */
866  xform[j] = NULL;
867  }
868  /* else, cannot determine redundancy, keep both keys */
869  }
870  /* track number of attrs for which we have "=" keys */
871  numberOfEqualCols++;
872  }
873 
874  /* try to keep only one of <, <= */
875  if (xform[BTLessStrategyNumber - 1]
876  && xform[BTLessEqualStrategyNumber - 1])
877  {
878  ScanKey lt = xform[BTLessStrategyNumber - 1];
879  ScanKey le = xform[BTLessEqualStrategyNumber - 1];
880 
881  if (_bt_compare_scankey_args(scan, le, lt, le,
882  &test_result))
883  {
884  if (test_result)
885  xform[BTLessEqualStrategyNumber - 1] = NULL;
886  else
887  xform[BTLessStrategyNumber - 1] = NULL;
888  }
889  }
890 
891  /* try to keep only one of >, >= */
892  if (xform[BTGreaterStrategyNumber - 1]
893  && xform[BTGreaterEqualStrategyNumber - 1])
894  {
895  ScanKey gt = xform[BTGreaterStrategyNumber - 1];
896  ScanKey ge = xform[BTGreaterEqualStrategyNumber - 1];
897 
898  if (_bt_compare_scankey_args(scan, ge, gt, ge,
899  &test_result))
900  {
901  if (test_result)
902  xform[BTGreaterEqualStrategyNumber - 1] = NULL;
903  else
904  xform[BTGreaterStrategyNumber - 1] = NULL;
905  }
906  }
907 
908  /*
909  * Emit the cleaned-up keys into the outkeys[] array, and then
910  * mark them if they are required. They are required (possibly
911  * only in one direction) if all attrs before this one had "=".
912  */
913  for (j = BTMaxStrategyNumber; --j >= 0;)
914  {
915  if (xform[j])
916  {
917  ScanKey outkey = &outkeys[new_numberOfKeys++];
918 
919  memcpy(outkey, xform[j], sizeof(ScanKeyData));
920  if (priorNumberOfEqualCols == attno - 1)
922  }
923  }
924 
925  /*
926  * Exit loop here if done.
927  */
928  if (i == numberOfKeys)
929  break;
930 
931  /* Re-initialize for new attno */
932  attno = cur->sk_attno;
933  memset(xform, 0, sizeof(xform));
934  }
935 
936  /* check strategy this key's operator corresponds to */
937  j = cur->sk_strategy - 1;
938 
939  /* if row comparison, push it directly to the output array */
940  if (cur->sk_flags & SK_ROW_HEADER)
941  {
942  ScanKey outkey = &outkeys[new_numberOfKeys++];
943 
944  memcpy(outkey, cur, sizeof(ScanKeyData));
945  if (numberOfEqualCols == attno - 1)
947 
948  /*
949  * We don't support RowCompare using equality; such a qual would
950  * mess up the numberOfEqualCols tracking.
951  */
952  Assert(j != (BTEqualStrategyNumber - 1));
953  continue;
954  }
955 
956  /* have we seen one of these before? */
957  if (xform[j] == NULL)
958  {
959  /* nope, so remember this scankey */
960  xform[j] = cur;
961  }
962  else
963  {
964  /* yup, keep only the more restrictive key */
965  if (_bt_compare_scankey_args(scan, cur, cur, xform[j],
966  &test_result))
967  {
968  if (test_result)
969  xform[j] = cur;
970  else if (j == (BTEqualStrategyNumber - 1))
971  {
972  /* key == a && key == b, but a != b */
973  so->qual_ok = false;
974  return;
975  }
976  /* else old key is more restrictive, keep it */
977  }
978  else
979  {
980  /*
981  * We can't determine which key is more restrictive. Keep the
982  * previous one in xform[j] and push this one directly to the
983  * output array.
984  */
985  ScanKey outkey = &outkeys[new_numberOfKeys++];
986 
987  memcpy(outkey, cur, sizeof(ScanKeyData));
988  if (numberOfEqualCols == attno - 1)
990  }
991  }
992  }
993 
994  so->numberOfKeys = new_numberOfKeys;
995 }
996 
997 /*
998  * Compare two scankey values using a specified operator.
999  *
1000  * The test we want to perform is logically "leftarg op rightarg", where
1001  * leftarg and rightarg are the sk_argument values in those ScanKeys, and
1002  * the comparison operator is the one in the op ScanKey. However, in
1003  * cross-data-type situations we may need to look up the correct operator in
1004  * the index's opfamily: it is the one having amopstrategy = op->sk_strategy
1005  * and amoplefttype/amoprighttype equal to the two argument datatypes.
1006  *
1007  * If the opfamily doesn't supply a complete set of cross-type operators we
1008  * may not be able to make the comparison. If we can make the comparison
1009  * we store the operator result in *result and return true. We return false
1010  * if the comparison could not be made.
1011  *
1012  * Note: op always points at the same ScanKey as either leftarg or rightarg.
1013  * Since we don't scribble on the scankeys, this aliasing should cause no
1014  * trouble.
1015  *
1016  * Note: this routine needs to be insensitive to any DESC option applied
1017  * to the index column. For example, "x < 4" is a tighter constraint than
1018  * "x < 5" regardless of which way the index is sorted.
1019  */
1020 static bool
1022  ScanKey leftarg, ScanKey rightarg,
1023  bool *result)
1024 {
1025  Relation rel = scan->indexRelation;
1026  Oid lefttype,
1027  righttype,
1028  optype,
1029  opcintype,
1030  cmp_op;
1031  StrategyNumber strat;
1032 
1033  /*
1034  * First, deal with cases where one or both args are NULL. This should
1035  * only happen when the scankeys represent IS NULL/NOT NULL conditions.
1036  */
1037  if ((leftarg->sk_flags | rightarg->sk_flags) & SK_ISNULL)
1038  {
1039  bool leftnull,
1040  rightnull;
1041 
1042  if (leftarg->sk_flags & SK_ISNULL)
1043  {
1045  leftnull = true;
1046  }
1047  else
1048  leftnull = false;
1049  if (rightarg->sk_flags & SK_ISNULL)
1050  {
1051  Assert(rightarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
1052  rightnull = true;
1053  }
1054  else
1055  rightnull = false;
1056 
1057  /*
1058  * We treat NULL as either greater than or less than all other values.
1059  * Since true > false, the tests below work correctly for NULLS LAST
1060  * logic. If the index is NULLS FIRST, we need to flip the strategy.
1061  */
1062  strat = op->sk_strategy;
1063  if (op->sk_flags & SK_BT_NULLS_FIRST)
1064  strat = BTCommuteStrategyNumber(strat);
1065 
1066  switch (strat)
1067  {
1068  case BTLessStrategyNumber:
1069  *result = (leftnull < rightnull);
1070  break;
1072  *result = (leftnull <= rightnull);
1073  break;
1074  case BTEqualStrategyNumber:
1075  *result = (leftnull == rightnull);
1076  break;
1078  *result = (leftnull >= rightnull);
1079  break;
1081  *result = (leftnull > rightnull);
1082  break;
1083  default:
1084  elog(ERROR, "unrecognized StrategyNumber: %d", (int) strat);
1085  *result = false; /* keep compiler quiet */
1086  break;
1087  }
1088  return true;
1089  }
1090 
1091  /*
1092  * The opfamily we need to worry about is identified by the index column.
1093  */
1094  Assert(leftarg->sk_attno == rightarg->sk_attno);
1095 
1096  opcintype = rel->rd_opcintype[leftarg->sk_attno - 1];
1097 
1098  /*
1099  * Determine the actual datatypes of the ScanKey arguments. We have to
1100  * support the convention that sk_subtype == InvalidOid means the opclass
1101  * input type; this is a hack to simplify life for ScanKeyInit().
1102  */
1103  lefttype = leftarg->sk_subtype;
1104  if (lefttype == InvalidOid)
1105  lefttype = opcintype;
1106  righttype = rightarg->sk_subtype;
1107  if (righttype == InvalidOid)
1108  righttype = opcintype;
1109  optype = op->sk_subtype;
1110  if (optype == InvalidOid)
1111  optype = opcintype;
1112 
1113  /*
1114  * If leftarg and rightarg match the types expected for the "op" scankey,
1115  * we can use its already-looked-up comparison function.
1116  */
1117  if (lefttype == opcintype && righttype == optype)
1118  {
1119  *result = DatumGetBool(FunctionCall2Coll(&op->sk_func,
1120  op->sk_collation,
1121  leftarg->sk_argument,
1122  rightarg->sk_argument));
1123  return true;
1124  }
1125 
1126  /*
1127  * Otherwise, we need to go to the syscache to find the appropriate
1128  * operator. (This cannot result in infinite recursion, since no
1129  * indexscan initiated by syscache lookup will use cross-data-type
1130  * operators.)
1131  *
1132  * If the sk_strategy was flipped by _bt_fix_scankey_strategy, we have to
1133  * un-flip it to get the correct opfamily member.
1134  */
1135  strat = op->sk_strategy;
1136  if (op->sk_flags & SK_BT_DESC)
1137  strat = BTCommuteStrategyNumber(strat);
1138 
1139  cmp_op = get_opfamily_member(rel->rd_opfamily[leftarg->sk_attno - 1],
1140  lefttype,
1141  righttype,
1142  strat);
1143  if (OidIsValid(cmp_op))
1144  {
1145  RegProcedure cmp_proc = get_opcode(cmp_op);
1146 
1147  if (RegProcedureIsValid(cmp_proc))
1148  {
1149  *result = DatumGetBool(OidFunctionCall2Coll(cmp_proc,
1150  op->sk_collation,
1151  leftarg->sk_argument,
1152  rightarg->sk_argument));
1153  return true;
1154  }
1155  }
1156 
1157  /* Can't make the comparison */
1158  *result = false; /* suppress compiler warnings */
1159  return false;
1160 }
1161 
1162 /*
1163  * Adjust a scankey's strategy and flags setting as needed for indoptions.
1164  *
1165  * We copy the appropriate indoption value into the scankey sk_flags
1166  * (shifting to avoid clobbering system-defined flag bits). Also, if
1167  * the DESC option is set, commute (flip) the operator strategy number.
1168  *
1169  * A secondary purpose is to check for IS NULL/NOT NULL scankeys and set up
1170  * the strategy field correctly for them.
1171  *
1172  * Lastly, for ordinary scankeys (not IS NULL/NOT NULL), we check for a
1173  * NULL comparison value. Since all btree operators are assumed strict,
1174  * a NULL means that the qual cannot be satisfied. We return true if the
1175  * comparison value isn't NULL, or false if the scan should be abandoned.
1176  *
1177  * This function is applied to the *input* scankey structure; therefore
1178  * on a rescan we will be looking at already-processed scankeys. Hence
1179  * we have to be careful not to re-commute the strategy if we already did it.
1180  * It's a bit ugly to modify the caller's copy of the scankey but in practice
1181  * there shouldn't be any problem, since the index's indoptions are certainly
1182  * not going to change while the scankey survives.
1183  */
1184 static bool
1186 {
1187  int addflags;
1188 
1189  addflags = indoption[skey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1190 
1191  /*
1192  * We treat all btree operators as strict (even if they're not so marked
1193  * in pg_proc). This means that it is impossible for an operator condition
1194  * with a NULL comparison constant to succeed, and we can reject it right
1195  * away.
1196  *
1197  * However, we now also support "x IS NULL" clauses as search conditions,
1198  * so in that case keep going. The planner has not filled in any
1199  * particular strategy in this case, so set it to BTEqualStrategyNumber
1200  * --- we can treat IS NULL as an equality operator for purposes of search
1201  * strategy.
1202  *
1203  * Likewise, "x IS NOT NULL" is supported. We treat that as either "less
1204  * than NULL" in a NULLS LAST index, or "greater than NULL" in a NULLS
1205  * FIRST index.
1206  *
1207  * Note: someday we might have to fill in sk_collation from the index
1208  * column's collation. At the moment this is a non-issue because we'll
1209  * never actually call the comparison operator on a NULL.
1210  */
1211  if (skey->sk_flags & SK_ISNULL)
1212  {
1213  /* SK_ISNULL shouldn't be set in a row header scankey */
1214  Assert(!(skey->sk_flags & SK_ROW_HEADER));
1215 
1216  /* Set indoption flags in scankey (might be done already) */
1217  skey->sk_flags |= addflags;
1218 
1219  /* Set correct strategy for IS NULL or NOT NULL search */
1220  if (skey->sk_flags & SK_SEARCHNULL)
1221  {
1223  skey->sk_subtype = InvalidOid;
1224  skey->sk_collation = InvalidOid;
1225  }
1226  else if (skey->sk_flags & SK_SEARCHNOTNULL)
1227  {
1228  if (skey->sk_flags & SK_BT_NULLS_FIRST)
1230  else
1232  skey->sk_subtype = InvalidOid;
1233  skey->sk_collation = InvalidOid;
1234  }
1235  else
1236  {
1237  /* regular qual, so it cannot be satisfied */
1238  return false;
1239  }
1240 
1241  /* Needn't do the rest */
1242  return true;
1243  }
1244 
1245  /* Adjust strategy for DESC, if we didn't already */
1246  if ((addflags & SK_BT_DESC) && !(skey->sk_flags & SK_BT_DESC))
1248  skey->sk_flags |= addflags;
1249 
1250  /* If it's a row header, fix row member flags and strategies similarly */
1251  if (skey->sk_flags & SK_ROW_HEADER)
1252  {
1253  ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1254 
1255  for (;;)
1256  {
1257  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1258  addflags = indoption[subkey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1259  if ((addflags & SK_BT_DESC) && !(subkey->sk_flags & SK_BT_DESC))
1260  subkey->sk_strategy = BTCommuteStrategyNumber(subkey->sk_strategy);
1261  subkey->sk_flags |= addflags;
1262  if (subkey->sk_flags & SK_ROW_END)
1263  break;
1264  subkey++;
1265  }
1266  }
1267 
1268  return true;
1269 }
1270 
1271 /*
1272  * Mark a scankey as "required to continue the scan".
1273  *
1274  * Depending on the operator type, the key may be required for both scan
1275  * directions or just one. Also, if the key is a row comparison header,
1276  * we have to mark its first subsidiary ScanKey as required. (Subsequent
1277  * subsidiary ScanKeys are normally for lower-order columns, and thus
1278  * cannot be required, since they're after the first non-equality scankey.)
1279  *
1280  * Note: when we set required-key flag bits in a subsidiary scankey, we are
1281  * scribbling on a data structure belonging to the index AM's caller, not on
1282  * our private copy. This should be OK because the marking will not change
1283  * from scan to scan within a query, and so we'd just re-mark the same way
1284  * anyway on a rescan. Something to keep an eye on though.
1285  */
1286 static void
1288 {
1289  int addflags;
1290 
1291  switch (skey->sk_strategy)
1292  {
1293  case BTLessStrategyNumber:
1295  addflags = SK_BT_REQFWD;
1296  break;
1297  case BTEqualStrategyNumber:
1298  addflags = SK_BT_REQFWD | SK_BT_REQBKWD;
1299  break;
1302  addflags = SK_BT_REQBKWD;
1303  break;
1304  default:
1305  elog(ERROR, "unrecognized StrategyNumber: %d",
1306  (int) skey->sk_strategy);
1307  addflags = 0; /* keep compiler quiet */
1308  break;
1309  }
1310 
1311  skey->sk_flags |= addflags;
1312 
1313  if (skey->sk_flags & SK_ROW_HEADER)
1314  {
1315  ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1316 
1317  /* First subkey should be same column/operator as the header */
1318  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1319  Assert(subkey->sk_attno == skey->sk_attno);
1320  Assert(subkey->sk_strategy == skey->sk_strategy);
1321  subkey->sk_flags |= addflags;
1322  }
1323 }
1324 
1325 /*
1326  * Test whether an indextuple satisfies all the scankey conditions.
1327  *
1328  * Return true if so, false if not. If the tuple fails to pass the qual,
1329  * we also determine whether there's any need to continue the scan beyond
1330  * this tuple, and set *continuescan accordingly. See comments for
1331  * _bt_preprocess_keys(), above, about how this is done.
1332  *
1333  * Forward scan callers can pass a high key tuple in the hopes of having
1334  * us set *continuescan to false, and avoiding an unnecessary visit to
1335  * the page to the right.
1336  *
1337  * scan: index scan descriptor (containing a search-type scankey)
1338  * tuple: index tuple to test
1339  * tupnatts: number of attributes in tupnatts (high key may be truncated)
1340  * dir: direction we are scanning in
1341  * continuescan: output parameter (will be set correctly in all cases)
1342  */
1343 bool
1344 _bt_checkkeys(IndexScanDesc scan, IndexTuple tuple, int tupnatts,
1345  ScanDirection dir, bool *continuescan)
1346 {
1347  TupleDesc tupdesc;
1348  BTScanOpaque so;
1349  int keysz;
1350  int ikey;
1351  ScanKey key;
1352 
1353  Assert(BTreeTupleGetNAtts(tuple, scan->indexRelation) == tupnatts);
1354 
1355  *continuescan = true; /* default assumption */
1356 
1357  tupdesc = RelationGetDescr(scan->indexRelation);
1358  so = (BTScanOpaque) scan->opaque;
1359  keysz = so->numberOfKeys;
1360 
1361  for (key = so->keyData, ikey = 0; ikey < keysz; key++, ikey++)
1362  {
1363  Datum datum;
1364  bool isNull;
1365  Datum test;
1366 
1367  if (key->sk_attno > tupnatts)
1368  {
1369  /*
1370  * This attribute is truncated (must be high key). The value for
1371  * this attribute in the first non-pivot tuple on the page to the
1372  * right could be any possible value. Assume that truncated
1373  * attribute passes the qual.
1374  */
1376  continue;
1377  }
1378 
1379  /* row-comparison keys need special processing */
1380  if (key->sk_flags & SK_ROW_HEADER)
1381  {
1382  if (_bt_check_rowcompare(key, tuple, tupnatts, tupdesc, dir,
1383  continuescan))
1384  continue;
1385  return false;
1386  }
1387 
1388  datum = index_getattr(tuple,
1389  key->sk_attno,
1390  tupdesc,
1391  &isNull);
1392 
1393  if (key->sk_flags & SK_ISNULL)
1394  {
1395  /* Handle IS NULL/NOT NULL tests */
1396  if (key->sk_flags & SK_SEARCHNULL)
1397  {
1398  if (isNull)
1399  continue; /* tuple satisfies this qual */
1400  }
1401  else
1402  {
1404  if (!isNull)
1405  continue; /* tuple satisfies this qual */
1406  }
1407 
1408  /*
1409  * Tuple fails this qual. If it's a required qual for the current
1410  * scan direction, then we can conclude no further tuples will
1411  * pass, either.
1412  */
1413  if ((key->sk_flags & SK_BT_REQFWD) &&
1415  *continuescan = false;
1416  else if ((key->sk_flags & SK_BT_REQBKWD) &&
1418  *continuescan = false;
1419 
1420  /*
1421  * In any case, this indextuple doesn't match the qual.
1422  */
1423  return false;
1424  }
1425 
1426  if (isNull)
1427  {
1428  if (key->sk_flags & SK_BT_NULLS_FIRST)
1429  {
1430  /*
1431  * Since NULLs are sorted before non-NULLs, we know we have
1432  * reached the lower limit of the range of values for this
1433  * index attr. On a backward scan, we can stop if this qual
1434  * is one of the "must match" subset. We can stop regardless
1435  * of whether the qual is > or <, so long as it's required,
1436  * because it's not possible for any future tuples to pass. On
1437  * a forward scan, however, we must keep going, because we may
1438  * have initially positioned to the start of the index.
1439  */
1440  if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1442  *continuescan = false;
1443  }
1444  else
1445  {
1446  /*
1447  * Since NULLs are sorted after non-NULLs, we know we have
1448  * reached the upper limit of the range of values for this
1449  * index attr. On a forward scan, we can stop if this qual is
1450  * one of the "must match" subset. We can stop regardless of
1451  * whether the qual is > or <, so long as it's required,
1452  * because it's not possible for any future tuples to pass. On
1453  * a backward scan, however, we must keep going, because we
1454  * may have initially positioned to the end of the index.
1455  */
1456  if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1458  *continuescan = false;
1459  }
1460 
1461  /*
1462  * In any case, this indextuple doesn't match the qual.
1463  */
1464  return false;
1465  }
1466 
1467  test = FunctionCall2Coll(&key->sk_func, key->sk_collation,
1468  datum, key->sk_argument);
1469 
1470  if (!DatumGetBool(test))
1471  {
1472  /*
1473  * Tuple fails this qual. If it's a required qual for the current
1474  * scan direction, then we can conclude no further tuples will
1475  * pass, either.
1476  *
1477  * Note: because we stop the scan as soon as any required equality
1478  * qual fails, it is critical that equality quals be used for the
1479  * initial positioning in _bt_first() when they are available. See
1480  * comments in _bt_first().
1481  */
1482  if ((key->sk_flags & SK_BT_REQFWD) &&
1484  *continuescan = false;
1485  else if ((key->sk_flags & SK_BT_REQBKWD) &&
1487  *continuescan = false;
1488 
1489  /*
1490  * In any case, this indextuple doesn't match the qual.
1491  */
1492  return false;
1493  }
1494  }
1495 
1496  /* If we get here, the tuple passes all index quals. */
1497  return true;
1498 }
1499 
1500 /*
1501  * Test whether an indextuple satisfies a row-comparison scan condition.
1502  *
1503  * Return true if so, false if not. If not, also clear *continuescan if
1504  * it's not possible for any future tuples in the current scan direction
1505  * to pass the qual.
1506  *
1507  * This is a subroutine for _bt_checkkeys, which see for more info.
1508  */
1509 static bool
1510 _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, int tupnatts,
1511  TupleDesc tupdesc, ScanDirection dir, bool *continuescan)
1512 {
1513  ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1514  int32 cmpresult = 0;
1515  bool result;
1516 
1517  /* First subkey should be same as the header says */
1518  Assert(subkey->sk_attno == skey->sk_attno);
1519 
1520  /* Loop over columns of the row condition */
1521  for (;;)
1522  {
1523  Datum datum;
1524  bool isNull;
1525 
1526  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1527 
1528  if (subkey->sk_attno > tupnatts)
1529  {
1530  /*
1531  * This attribute is truncated (must be high key). The value for
1532  * this attribute in the first non-pivot tuple on the page to the
1533  * right could be any possible value. Assume that truncated
1534  * attribute passes the qual.
1535  */
1537  cmpresult = 0;
1538  if (subkey->sk_flags & SK_ROW_END)
1539  break;
1540  subkey++;
1541  continue;
1542  }
1543 
1544  datum = index_getattr(tuple,
1545  subkey->sk_attno,
1546  tupdesc,
1547  &isNull);
1548 
1549  if (isNull)
1550  {
1551  if (subkey->sk_flags & SK_BT_NULLS_FIRST)
1552  {
1553  /*
1554  * Since NULLs are sorted before non-NULLs, we know we have
1555  * reached the lower limit of the range of values for this
1556  * index attr. On a backward scan, we can stop if this qual
1557  * is one of the "must match" subset. We can stop regardless
1558  * of whether the qual is > or <, so long as it's required,
1559  * because it's not possible for any future tuples to pass. On
1560  * a forward scan, however, we must keep going, because we may
1561  * have initially positioned to the start of the index.
1562  */
1563  if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1565  *continuescan = false;
1566  }
1567  else
1568  {
1569  /*
1570  * Since NULLs are sorted after non-NULLs, we know we have
1571  * reached the upper limit of the range of values for this
1572  * index attr. On a forward scan, we can stop if this qual is
1573  * one of the "must match" subset. We can stop regardless of
1574  * whether the qual is > or <, so long as it's required,
1575  * because it's not possible for any future tuples to pass. On
1576  * a backward scan, however, we must keep going, because we
1577  * may have initially positioned to the end of the index.
1578  */
1579  if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1581  *continuescan = false;
1582  }
1583 
1584  /*
1585  * In any case, this indextuple doesn't match the qual.
1586  */
1587  return false;
1588  }
1589 
1590  if (subkey->sk_flags & SK_ISNULL)
1591  {
1592  /*
1593  * Unlike the simple-scankey case, this isn't a disallowed case.
1594  * But it can never match. If all the earlier row comparison
1595  * columns are required for the scan direction, we can stop the
1596  * scan, because there can't be another tuple that will succeed.
1597  */
1598  if (subkey != (ScanKey) DatumGetPointer(skey->sk_argument))
1599  subkey--;
1600  if ((subkey->sk_flags & SK_BT_REQFWD) &&
1602  *continuescan = false;
1603  else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1605  *continuescan = false;
1606  return false;
1607  }
1608 
1609  /* Perform the test --- three-way comparison not bool operator */
1610  cmpresult = DatumGetInt32(FunctionCall2Coll(&subkey->sk_func,
1611  subkey->sk_collation,
1612  datum,
1613  subkey->sk_argument));
1614 
1615  if (subkey->sk_flags & SK_BT_DESC)
1616  INVERT_COMPARE_RESULT(cmpresult);
1617 
1618  /* Done comparing if unequal, else advance to next column */
1619  if (cmpresult != 0)
1620  break;
1621 
1622  if (subkey->sk_flags & SK_ROW_END)
1623  break;
1624  subkey++;
1625  }
1626 
1627  /*
1628  * At this point cmpresult indicates the overall result of the row
1629  * comparison, and subkey points to the deciding column (or the last
1630  * column if the result is "=").
1631  */
1632  switch (subkey->sk_strategy)
1633  {
1634  /* EQ and NE cases aren't allowed here */
1635  case BTLessStrategyNumber:
1636  result = (cmpresult < 0);
1637  break;
1639  result = (cmpresult <= 0);
1640  break;
1642  result = (cmpresult >= 0);
1643  break;
1645  result = (cmpresult > 0);
1646  break;
1647  default:
1648  elog(ERROR, "unrecognized RowCompareType: %d",
1649  (int) subkey->sk_strategy);
1650  result = 0; /* keep compiler quiet */
1651  break;
1652  }
1653 
1654  if (!result)
1655  {
1656  /*
1657  * Tuple fails this qual. If it's a required qual for the current
1658  * scan direction, then we can conclude no further tuples will pass,
1659  * either. Note we have to look at the deciding column, not
1660  * necessarily the first or last column of the row condition.
1661  */
1662  if ((subkey->sk_flags & SK_BT_REQFWD) &&
1664  *continuescan = false;
1665  else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1667  *continuescan = false;
1668  }
1669 
1670  return result;
1671 }
1672 
1673 /*
1674  * _bt_killitems - set LP_DEAD state for items an indexscan caller has
1675  * told us were killed
1676  *
1677  * scan->opaque, referenced locally through so, contains information about the
1678  * current page and killed tuples thereon (generally, this should only be
1679  * called if so->numKilled > 0).
1680  *
1681  * The caller does not have a lock on the page and may or may not have the
1682  * page pinned in a buffer. Note that read-lock is sufficient for setting
1683  * LP_DEAD status (which is only a hint).
1684  *
1685  * We match items by heap TID before assuming they are the right ones to
1686  * delete. We cope with cases where items have moved right due to insertions.
1687  * If an item has moved off the current page due to a split, we'll fail to
1688  * find it and do nothing (this is not an error case --- we assume the item
1689  * will eventually get marked in a future indexscan).
1690  *
1691  * Note that if we hold a pin on the target page continuously from initially
1692  * reading the items until applying this function, VACUUM cannot have deleted
1693  * any items from the page, and so there is no need to search left from the
1694  * recorded offset. (This observation also guarantees that the item is still
1695  * the right one to delete, which might otherwise be questionable since heap
1696  * TIDs can get recycled.) This holds true even if the page has been modified
1697  * by inserts and page splits, so there is no need to consult the LSN.
1698  *
1699  * If the pin was released after reading the page, then we re-read it. If it
1700  * has been modified since we read it (as determined by the LSN), we dare not
1701  * flag any entries because it is possible that the old entry was vacuumed
1702  * away and the TID was re-used by a completely different heap tuple.
1703  */
1704 void
1706 {
1707  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1708  Page page;
1709  BTPageOpaque opaque;
1710  OffsetNumber minoff;
1711  OffsetNumber maxoff;
1712  int i;
1713  int numKilled = so->numKilled;
1714  bool killedsomething = false;
1715 
1717 
1718  /*
1719  * Always reset the scan state, so we don't look for same items on other
1720  * pages.
1721  */
1722  so->numKilled = 0;
1723 
1724  if (BTScanPosIsPinned(so->currPos))
1725  {
1726  /*
1727  * We have held the pin on this page since we read the index tuples,
1728  * so all we need to do is lock it. The pin will have prevented
1729  * re-use of any TID on the page, so there is no need to check the
1730  * LSN.
1731  */
1732  LockBuffer(so->currPos.buf, BT_READ);
1733 
1734  page = BufferGetPage(so->currPos.buf);
1735  }
1736  else
1737  {
1738  Buffer buf;
1739 
1740  /* Attempt to re-read the buffer, getting pin and lock. */
1741  buf = _bt_getbuf(scan->indexRelation, so->currPos.currPage, BT_READ);
1742 
1743  /* It might not exist anymore; in which case we can't hint it. */
1744  if (!BufferIsValid(buf))
1745  return;
1746 
1747  page = BufferGetPage(buf);
1748  if (BufferGetLSNAtomic(buf) == so->currPos.lsn)
1749  so->currPos.buf = buf;
1750  else
1751  {
1752  /* Modified while not pinned means hinting is not safe. */
1753  _bt_relbuf(scan->indexRelation, buf);
1754  return;
1755  }
1756  }
1757 
1758  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1759  minoff = P_FIRSTDATAKEY(opaque);
1760  maxoff = PageGetMaxOffsetNumber(page);
1761 
1762  for (i = 0; i < numKilled; i++)
1763  {
1764  int itemIndex = so->killedItems[i];
1765  BTScanPosItem *kitem = &so->currPos.items[itemIndex];
1766  OffsetNumber offnum = kitem->indexOffset;
1767 
1768  Assert(itemIndex >= so->currPos.firstItem &&
1769  itemIndex <= so->currPos.lastItem);
1770  if (offnum < minoff)
1771  continue; /* pure paranoia */
1772  while (offnum <= maxoff)
1773  {
1774  ItemId iid = PageGetItemId(page, offnum);
1775  IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
1776 
1777  if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
1778  {
1779  /* found the item */
1780  ItemIdMarkDead(iid);
1781  killedsomething = true;
1782  break; /* out of inner search loop */
1783  }
1784  offnum = OffsetNumberNext(offnum);
1785  }
1786  }
1787 
1788  /*
1789  * Since this can be redone later if needed, mark as dirty hint.
1790  *
1791  * Whenever we mark anything LP_DEAD, we also set the page's
1792  * BTP_HAS_GARBAGE flag, which is likewise just a hint.
1793  */
1794  if (killedsomething)
1795  {
1796  opaque->btpo_flags |= BTP_HAS_GARBAGE;
1797  MarkBufferDirtyHint(so->currPos.buf, true);
1798  }
1799 
1801 }
1802 
1803 
1804 /*
1805  * The following routines manage a shared-memory area in which we track
1806  * assignment of "vacuum cycle IDs" to currently-active btree vacuuming
1807  * operations. There is a single counter which increments each time we
1808  * start a vacuum to assign it a cycle ID. Since multiple vacuums could
1809  * be active concurrently, we have to track the cycle ID for each active
1810  * vacuum; this requires at most MaxBackends entries (usually far fewer).
1811  * We assume at most one vacuum can be active for a given index.
1812  *
1813  * Access to the shared memory area is controlled by BtreeVacuumLock.
1814  * In principle we could use a separate lmgr locktag for each index,
1815  * but a single LWLock is much cheaper, and given the short time that
1816  * the lock is ever held, the concurrency hit should be minimal.
1817  */
1818 
1819 typedef struct BTOneVacInfo
1820 {
1821  LockRelId relid; /* global identifier of an index */
1822  BTCycleId cycleid; /* cycle ID for its active VACUUM */
1823 } BTOneVacInfo;
1824 
1825 typedef struct BTVacInfo
1826 {
1827  BTCycleId cycle_ctr; /* cycle ID most recently assigned */
1828  int num_vacuums; /* number of currently active VACUUMs */
1829  int max_vacuums; /* allocated length of vacuums[] array */
1830  BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER];
1831 } BTVacInfo;
1832 
1834 
1835 
1836 /*
1837  * _bt_vacuum_cycleid --- get the active vacuum cycle ID for an index,
1838  * or zero if there is no active VACUUM
1839  *
1840  * Note: for correct interlocking, the caller must already hold pin and
1841  * exclusive lock on each buffer it will store the cycle ID into. This
1842  * ensures that even if a VACUUM starts immediately afterwards, it cannot
1843  * process those pages until the page split is complete.
1844  */
1845 BTCycleId
1847 {
1848  BTCycleId result = 0;
1849  int i;
1850 
1851  /* Share lock is enough since this is a read-only operation */
1852  LWLockAcquire(BtreeVacuumLock, LW_SHARED);
1853 
1854  for (i = 0; i < btvacinfo->num_vacuums; i++)
1855  {
1856  BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1857 
1858  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1859  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1860  {
1861  result = vac->cycleid;
1862  break;
1863  }
1864  }
1865 
1866  LWLockRelease(BtreeVacuumLock);
1867  return result;
1868 }
1869 
1870 /*
1871  * _bt_start_vacuum --- assign a cycle ID to a just-starting VACUUM operation
1872  *
1873  * Note: the caller must guarantee that it will eventually call
1874  * _bt_end_vacuum, else we'll permanently leak an array slot. To ensure
1875  * that this happens even in elog(FATAL) scenarios, the appropriate coding
1876  * is not just a PG_TRY, but
1877  * PG_ENSURE_ERROR_CLEANUP(_bt_end_vacuum_callback, PointerGetDatum(rel))
1878  */
1879 BTCycleId
1881 {
1882  BTCycleId result;
1883  int i;
1884  BTOneVacInfo *vac;
1885 
1886  LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1887 
1888  /*
1889  * Assign the next cycle ID, being careful to avoid zero as well as the
1890  * reserved high values.
1891  */
1892  result = ++(btvacinfo->cycle_ctr);
1893  if (result == 0 || result > MAX_BT_CYCLE_ID)
1894  result = btvacinfo->cycle_ctr = 1;
1895 
1896  /* Let's just make sure there's no entry already for this index */
1897  for (i = 0; i < btvacinfo->num_vacuums; i++)
1898  {
1899  vac = &btvacinfo->vacuums[i];
1900  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1901  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1902  {
1903  /*
1904  * Unlike most places in the backend, we have to explicitly
1905  * release our LWLock before throwing an error. This is because
1906  * we expect _bt_end_vacuum() to be called before transaction
1907  * abort cleanup can run to release LWLocks.
1908  */
1909  LWLockRelease(BtreeVacuumLock);
1910  elog(ERROR, "multiple active vacuums for index \"%s\"",
1912  }
1913  }
1914 
1915  /* OK, add an entry */
1916  if (btvacinfo->num_vacuums >= btvacinfo->max_vacuums)
1917  {
1918  LWLockRelease(BtreeVacuumLock);
1919  elog(ERROR, "out of btvacinfo slots");
1920  }
1921  vac = &btvacinfo->vacuums[btvacinfo->num_vacuums];
1922  vac->relid = rel->rd_lockInfo.lockRelId;
1923  vac->cycleid = result;
1924  btvacinfo->num_vacuums++;
1925 
1926  LWLockRelease(BtreeVacuumLock);
1927  return result;
1928 }
1929 
1930 /*
1931  * _bt_end_vacuum --- mark a btree VACUUM operation as done
1932  *
1933  * Note: this is deliberately coded not to complain if no entry is found;
1934  * this allows the caller to put PG_TRY around the start_vacuum operation.
1935  */
1936 void
1938 {
1939  int i;
1940 
1941  LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1942 
1943  /* Find the array entry */
1944  for (i = 0; i < btvacinfo->num_vacuums; i++)
1945  {
1946  BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1947 
1948  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1949  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1950  {
1951  /* Remove it by shifting down the last entry */
1952  *vac = btvacinfo->vacuums[btvacinfo->num_vacuums - 1];
1953  btvacinfo->num_vacuums--;
1954  break;
1955  }
1956  }
1957 
1958  LWLockRelease(BtreeVacuumLock);
1959 }
1960 
1961 /*
1962  * _bt_end_vacuum wrapped as an on_shmem_exit callback function
1963  */
1964 void
1966 {
1968 }
1969 
1970 /*
1971  * BTreeShmemSize --- report amount of shared memory space needed
1972  */
1973 Size
1975 {
1976  Size size;
1977 
1978  size = offsetof(BTVacInfo, vacuums);
1979  size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
1980  return size;
1981 }
1982 
1983 /*
1984  * BTreeShmemInit --- initialize this module's shared memory
1985  */
1986 void
1988 {
1989  bool found;
1990 
1991  btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
1992  BTreeShmemSize(),
1993  &found);
1994 
1995  if (!IsUnderPostmaster)
1996  {
1997  /* Initialize shared memory area */
1998  Assert(!found);
1999 
2000  /*
2001  * It doesn't really matter what the cycle counter starts at, but
2002  * having it always start the same doesn't seem good. Seed with
2003  * low-order bits of time() instead.
2004  */
2005  btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
2006 
2007  btvacinfo->num_vacuums = 0;
2008  btvacinfo->max_vacuums = MaxBackends;
2009  }
2010  else
2011  Assert(found);
2012 }
2013 
2014 bytea *
2015 btoptions(Datum reloptions, bool validate)
2016 {
2017  return default_reloptions(reloptions, validate, RELOPT_KIND_BTREE);
2018 }
2019 
2020 /*
2021  * btproperty() -- Check boolean properties of indexes.
2022  *
2023  * This is optional, but handling AMPROP_RETURNABLE here saves opening the rel
2024  * to call btcanreturn.
2025  */
2026 bool
2027 btproperty(Oid index_oid, int attno,
2028  IndexAMProperty prop, const char *propname,
2029  bool *res, bool *isnull)
2030 {
2031  switch (prop)
2032  {
2033  case AMPROP_RETURNABLE:
2034  /* answer only for columns, not AM or whole index */
2035  if (attno == 0)
2036  return false;
2037  /* otherwise, btree can always return data */
2038  *res = true;
2039  return true;
2040 
2041  default:
2042  return false; /* punt to generic code */
2043  }
2044 }
2045 
2046 /*
2047  * btbuildphasename() -- Return name of index build phase.
2048  */
2049 char *
2050 btbuildphasename(int64 phasenum)
2051 {
2052  switch (phasenum)
2053  {
2055  return "initializing";
2057  return "scanning table";
2059  return "sorting live tuples";
2061  return "sorting dead tuples";
2063  return "loading tuples in tree";
2064  default:
2065  return NULL;
2066  }
2067 }
2068 
2069 /*
2070  * _bt_truncate() -- create tuple without unneeded suffix attributes.
2071  *
2072  * Returns truncated pivot index tuple allocated in caller's memory context,
2073  * with key attributes copied from caller's firstright argument. If rel is
2074  * an INCLUDE index, non-key attributes will definitely be truncated away,
2075  * since they're not part of the key space. More aggressive suffix
2076  * truncation can take place when it's clear that the returned tuple does not
2077  * need one or more suffix key attributes. We only need to keep firstright
2078  * attributes up to and including the first non-lastleft-equal attribute.
2079  * Caller's insertion scankey is used to compare the tuples; the scankey's
2080  * argument values are not considered here.
2081  *
2082  * Note that returned tuple's t_tid offset will hold the number of attributes
2083  * present, so the original item pointer offset is not represented. Caller
2084  * should only change truncated tuple's downlink. Note also that truncated
2085  * key attributes are treated as containing "minus infinity" values by
2086  * _bt_compare().
2087  *
2088  * In the worst case (when a heap TID must be appended to distinguish lastleft
2089  * from firstright), the size of the returned tuple is the size of firstright
2090  * plus the size of an additional MAXALIGN()'d item pointer. This guarantee
2091  * is important, since callers need to stay under the 1/3 of a page
2092  * restriction on tuple size. If this routine is ever taught to truncate
2093  * within an attribute/datum, it will need to avoid returning an enlarged
2094  * tuple to caller when truncation + TOAST compression ends up enlarging the
2095  * final datum.
2096  */
2097 IndexTuple
2098 _bt_truncate(Relation rel, IndexTuple lastleft, IndexTuple firstright,
2099  BTScanInsert itup_key)
2100 {
2101  TupleDesc itupdesc = RelationGetDescr(rel);
2104  int keepnatts;
2105  IndexTuple pivot;
2106  ItemPointer pivotheaptid;
2107  Size newsize;
2108 
2109  /*
2110  * We should only ever truncate leaf index tuples. It's never okay to
2111  * truncate a second time.
2112  */
2113  Assert(BTreeTupleGetNAtts(lastleft, rel) == natts);
2114  Assert(BTreeTupleGetNAtts(firstright, rel) == natts);
2115 
2116  /* Determine how many attributes must be kept in truncated tuple */
2117  keepnatts = _bt_keep_natts(rel, lastleft, firstright, itup_key);
2118 
2119 #ifdef DEBUG_NO_TRUNCATE
2120  /* Force truncation to be ineffective for testing purposes */
2121  keepnatts = nkeyatts + 1;
2122 #endif
2123 
2124  if (keepnatts <= natts)
2125  {
2126  IndexTuple tidpivot;
2127 
2128  pivot = index_truncate_tuple(itupdesc, firstright, keepnatts);
2129 
2130  /*
2131  * If there is a distinguishing key attribute within new pivot tuple,
2132  * there is no need to add an explicit heap TID attribute
2133  */
2134  if (keepnatts <= nkeyatts)
2135  {
2136  BTreeTupleSetNAtts(pivot, keepnatts);
2137  return pivot;
2138  }
2139 
2140  /*
2141  * Only truncation of non-key attributes was possible, since key
2142  * attributes are all equal. It's necessary to add a heap TID
2143  * attribute to the new pivot tuple.
2144  */
2145  Assert(natts != nkeyatts);
2146  newsize = IndexTupleSize(pivot) + MAXALIGN(sizeof(ItemPointerData));
2147  tidpivot = palloc0(newsize);
2148  memcpy(tidpivot, pivot, IndexTupleSize(pivot));
2149  /* cannot leak memory here */
2150  pfree(pivot);
2151  pivot = tidpivot;
2152  }
2153  else
2154  {
2155  /*
2156  * No truncation was possible, since key attributes are all equal.
2157  * It's necessary to add a heap TID attribute to the new pivot tuple.
2158  */
2159  Assert(natts == nkeyatts);
2160  newsize = IndexTupleSize(firstright) + MAXALIGN(sizeof(ItemPointerData));
2161  pivot = palloc0(newsize);
2162  memcpy(pivot, firstright, IndexTupleSize(firstright));
2163  }
2164 
2165  /*
2166  * We have to use heap TID as a unique-ifier in the new pivot tuple, since
2167  * no non-TID key attribute in the right item readily distinguishes the
2168  * right side of the split from the left side. Use enlarged space that
2169  * holds a copy of first right tuple; place a heap TID value within the
2170  * extra space that remains at the end.
2171  *
2172  * nbtree conceptualizes this case as an inability to truncate away any
2173  * key attribute. We must use an alternative representation of heap TID
2174  * within pivots because heap TID is only treated as an attribute within
2175  * nbtree (e.g., there is no pg_attribute entry).
2176  */
2177  Assert(itup_key->heapkeyspace);
2178  pivot->t_info &= ~INDEX_SIZE_MASK;
2179  pivot->t_info |= newsize;
2180 
2181  /*
2182  * Lehman & Yao use lastleft as the leaf high key in all cases, but don't
2183  * consider suffix truncation. It seems like a good idea to follow that
2184  * example in cases where no truncation takes place -- use lastleft's heap
2185  * TID. (This is also the closest value to negative infinity that's
2186  * legally usable.)
2187  */
2188  pivotheaptid = (ItemPointer) ((char *) pivot + newsize -
2189  sizeof(ItemPointerData));
2190  ItemPointerCopy(&lastleft->t_tid, pivotheaptid);
2191 
2192  /*
2193  * Lehman and Yao require that the downlink to the right page, which is to
2194  * be inserted into the parent page in the second phase of a page split be
2195  * a strict lower bound on items on the right page, and a non-strict upper
2196  * bound for items on the left page. Assert that heap TIDs follow these
2197  * invariants, since a heap TID value is apparently needed as a
2198  * tiebreaker.
2199  */
2200 #ifndef DEBUG_NO_TRUNCATE
2201  Assert(ItemPointerCompare(&lastleft->t_tid, &firstright->t_tid) < 0);
2202  Assert(ItemPointerCompare(pivotheaptid, &lastleft->t_tid) >= 0);
2203  Assert(ItemPointerCompare(pivotheaptid, &firstright->t_tid) < 0);
2204 #else
2205 
2206  /*
2207  * Those invariants aren't guaranteed to hold for lastleft + firstright
2208  * heap TID attribute values when they're considered here only because
2209  * DEBUG_NO_TRUNCATE is defined (a heap TID is probably not actually
2210  * needed as a tiebreaker). DEBUG_NO_TRUNCATE must therefore use a heap
2211  * TID value that always works as a strict lower bound for items to the
2212  * right. In particular, it must avoid using firstright's leading key
2213  * attribute values along with lastleft's heap TID value when lastleft's
2214  * TID happens to be greater than firstright's TID.
2215  */
2216  ItemPointerCopy(&firstright->t_tid, pivotheaptid);
2217 
2218  /*
2219  * Pivot heap TID should never be fully equal to firstright. Note that
2220  * the pivot heap TID will still end up equal to lastleft's heap TID when
2221  * that's the only usable value.
2222  */
2223  ItemPointerSetOffsetNumber(pivotheaptid,
2225  Assert(ItemPointerCompare(pivotheaptid, &firstright->t_tid) < 0);
2226 #endif
2227 
2228  BTreeTupleSetNAtts(pivot, nkeyatts);
2229  BTreeTupleSetAltHeapTID(pivot);
2230 
2231  return pivot;
2232 }
2233 
2234 /*
2235  * _bt_keep_natts - how many key attributes to keep when truncating.
2236  *
2237  * Caller provides two tuples that enclose a split point. Caller's insertion
2238  * scankey is used to compare the tuples; the scankey's argument values are
2239  * not considered here.
2240  *
2241  * This can return a number of attributes that is one greater than the
2242  * number of key attributes for the index relation. This indicates that the
2243  * caller must use a heap TID as a unique-ifier in new pivot tuple.
2244  */
2245 static int
2246 _bt_keep_natts(Relation rel, IndexTuple lastleft, IndexTuple firstright,
2247  BTScanInsert itup_key)
2248 {
2249  int nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
2250  TupleDesc itupdesc = RelationGetDescr(rel);
2251  int keepnatts;
2252  ScanKey scankey;
2253 
2254  /*
2255  * Be consistent about the representation of BTREE_VERSION 2/3 tuples
2256  * across Postgres versions; don't allow new pivot tuples to have
2257  * truncated key attributes there. _bt_compare() treats truncated key
2258  * attributes as having the value minus infinity, which would break
2259  * searches within !heapkeyspace indexes.
2260  */
2261  if (!itup_key->heapkeyspace)
2262  {
2263  Assert(nkeyatts != IndexRelationGetNumberOfAttributes(rel));
2264  return nkeyatts;
2265  }
2266 
2267  scankey = itup_key->scankeys;
2268  keepnatts = 1;
2269  for (int attnum = 1; attnum <= nkeyatts; attnum++, scankey++)
2270  {
2271  Datum datum1,
2272  datum2;
2273  bool isNull1,
2274  isNull2;
2275 
2276  datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
2277  datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
2278 
2279  if (isNull1 != isNull2)
2280  break;
2281 
2282  if (!isNull1 &&
2284  scankey->sk_collation,
2285  datum1,
2286  datum2)) != 0)
2287  break;
2288 
2289  keepnatts++;
2290  }
2291 
2292  return keepnatts;
2293 }
2294 
2295 /*
2296  * _bt_keep_natts_fast - fast bitwise variant of _bt_keep_natts.
2297  *
2298  * This is exported so that a candidate split point can have its effect on
2299  * suffix truncation inexpensively evaluated ahead of time when finding a
2300  * split location. A naive bitwise approach to datum comparisons is used to
2301  * save cycles.
2302  *
2303  * The approach taken here usually provides the same answer as _bt_keep_natts
2304  * will (for the same pair of tuples from a heapkeyspace index), since the
2305  * majority of btree opclasses can never indicate that two datums are equal
2306  * unless they're bitwise equal after detoasting.
2307  *
2308  * These issues must be acceptable to callers, typically because they're only
2309  * concerned about making suffix truncation as effective as possible without
2310  * leaving excessive amounts of free space on either side of page split.
2311  * Callers can rely on the fact that attributes considered equal here are
2312  * definitely also equal according to _bt_keep_natts.
2313  */
2314 int
2316 {
2317  TupleDesc itupdesc = RelationGetDescr(rel);
2318  int keysz = IndexRelationGetNumberOfKeyAttributes(rel);
2319  int keepnatts;
2320 
2321  keepnatts = 1;
2322  for (int attnum = 1; attnum <= keysz; attnum++)
2323  {
2324  Datum datum1,
2325  datum2;
2326  bool isNull1,
2327  isNull2;
2328  Form_pg_attribute att;
2329 
2330  datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
2331  datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
2332  att = TupleDescAttr(itupdesc, attnum - 1);
2333 
2334  if (isNull1 != isNull2)
2335  break;
2336 
2337  if (!isNull1 &&
2338  !datum_image_eq(datum1, datum2, att->attbyval, att->attlen))
2339  break;
2340 
2341  keepnatts++;
2342  }
2343 
2344  return keepnatts;
2345 }
2346 
2347 /*
2348  * _bt_check_natts() -- Verify tuple has expected number of attributes.
2349  *
2350  * Returns value indicating if the expected number of attributes were found
2351  * for a particular offset on page. This can be used as a general purpose
2352  * sanity check.
2353  *
2354  * Testing a tuple directly with BTreeTupleGetNAtts() should generally be
2355  * preferred to calling here. That's usually more convenient, and is always
2356  * more explicit. Call here instead when offnum's tuple may be a negative
2357  * infinity tuple that uses the pre-v11 on-disk representation, or when a low
2358  * context check is appropriate. This routine is as strict as possible about
2359  * what is expected on each version of btree.
2360  */
2361 bool
2362 _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
2363 {
2367  IndexTuple itup;
2368  int tupnatts;
2369 
2370  /*
2371  * We cannot reliably test a deleted or half-dead page, since they have
2372  * dummy high keys
2373  */
2374  if (P_IGNORE(opaque))
2375  return true;
2376 
2377  Assert(offnum >= FirstOffsetNumber &&
2378  offnum <= PageGetMaxOffsetNumber(page));
2379 
2380  /*
2381  * Mask allocated for number of keys in index tuple must be able to fit
2382  * maximum possible number of index attributes
2383  */
2385  "BT_N_KEYS_OFFSET_MASK can't fit INDEX_MAX_KEYS");
2386 
2387  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
2388  tupnatts = BTreeTupleGetNAtts(itup, rel);
2389 
2390  if (P_ISLEAF(opaque))
2391  {
2392  if (offnum >= P_FIRSTDATAKEY(opaque))
2393  {
2394  /*
2395  * Non-pivot tuples currently never use alternative heap TID
2396  * representation -- even those within heapkeyspace indexes
2397  */
2398  if ((itup->t_info & INDEX_ALT_TID_MASK) != 0)
2399  return false;
2400 
2401  /*
2402  * Leaf tuples that are not the page high key (non-pivot tuples)
2403  * should never be truncated. (Note that tupnatts must have been
2404  * inferred, rather than coming from an explicit on-disk
2405  * representation.)
2406  */
2407  return tupnatts == natts;
2408  }
2409  else
2410  {
2411  /*
2412  * Rightmost page doesn't contain a page high key, so tuple was
2413  * checked above as ordinary leaf tuple
2414  */
2415  Assert(!P_RIGHTMOST(opaque));
2416 
2417  /*
2418  * !heapkeyspace high key tuple contains only key attributes. Note
2419  * that tupnatts will only have been explicitly represented in
2420  * !heapkeyspace indexes that happen to have non-key attributes.
2421  */
2422  if (!heapkeyspace)
2423  return tupnatts == nkeyatts;
2424 
2425  /* Use generic heapkeyspace pivot tuple handling */
2426  }
2427  }
2428  else /* !P_ISLEAF(opaque) */
2429  {
2430  if (offnum == P_FIRSTDATAKEY(opaque))
2431  {
2432  /*
2433  * The first tuple on any internal page (possibly the first after
2434  * its high key) is its negative infinity tuple. Negative
2435  * infinity tuples are always truncated to zero attributes. They
2436  * are a particular kind of pivot tuple.
2437  */
2438  if (heapkeyspace)
2439  return tupnatts == 0;
2440 
2441  /*
2442  * The number of attributes won't be explicitly represented if the
2443  * negative infinity tuple was generated during a page split that
2444  * occurred with a version of Postgres before v11. There must be
2445  * a problem when there is an explicit representation that is
2446  * non-zero, or when there is no explicit representation and the
2447  * tuple is evidently not a pre-pg_upgrade tuple.
2448  *
2449  * Prior to v11, downlinks always had P_HIKEY as their offset. Use
2450  * that to decide if the tuple is a pre-v11 tuple.
2451  */
2452  return tupnatts == 0 ||
2453  ((itup->t_info & INDEX_ALT_TID_MASK) == 0 &&
2455  }
2456  else
2457  {
2458  /*
2459  * !heapkeyspace downlink tuple with separator key contains only
2460  * key attributes. Note that tupnatts will only have been
2461  * explicitly represented in !heapkeyspace indexes that happen to
2462  * have non-key attributes.
2463  */
2464  if (!heapkeyspace)
2465  return tupnatts == nkeyatts;
2466 
2467  /* Use generic heapkeyspace pivot tuple handling */
2468  }
2469 
2470  }
2471 
2472  /* Handle heapkeyspace pivot tuples (excluding minus infinity items) */
2473  Assert(heapkeyspace);
2474 
2475  /*
2476  * Explicit representation of the number of attributes is mandatory with
2477  * heapkeyspace index pivot tuples, regardless of whether or not there are
2478  * non-key attributes.
2479  */
2480  if ((itup->t_info & INDEX_ALT_TID_MASK) == 0)
2481  return false;
2482 
2483  /*
2484  * Heap TID is a tiebreaker key attribute, so it cannot be untruncated
2485  * when any other key attribute is truncated
2486  */
2487  if (BTreeTupleGetHeapTID(itup) != NULL && tupnatts != nkeyatts)
2488  return false;
2489 
2490  /*
2491  * Pivot tuple must have at least one untruncated key attribute (minus
2492  * infinity pivot tuples are the only exception). Pivot tuples can never
2493  * represent that there is a value present for a key attribute that
2494  * exceeds pg_index.indnkeyatts for the index.
2495  */
2496  return tupnatts > 0 && tupnatts <= nkeyatts;
2497 }
2498 
2499 /*
2500  *
2501  * _bt_check_third_page() -- check whether tuple fits on a btree page at all.
2502  *
2503  * We actually need to be able to fit three items on every page, so restrict
2504  * any one item to 1/3 the per-page available space. Note that itemsz should
2505  * not include the ItemId overhead.
2506  *
2507  * It might be useful to apply TOAST methods rather than throw an error here.
2508  * Using out of line storage would break assumptions made by suffix truncation
2509  * and by contrib/amcheck, though.
2510  */
2511 void
2512 _bt_check_third_page(Relation rel, Relation heap, bool needheaptidspace,
2513  Page page, IndexTuple newtup)
2514 {
2515  Size itemsz;
2516  BTPageOpaque opaque;
2517 
2518  itemsz = MAXALIGN(IndexTupleSize(newtup));
2519 
2520  /* Double check item size against limit */
2521  if (itemsz <= BTMaxItemSize(page))
2522  return;
2523 
2524  /*
2525  * Tuple is probably too large to fit on page, but it's possible that the
2526  * index uses version 2 or version 3, or that page is an internal page, in
2527  * which case a slightly higher limit applies.
2528  */
2529  if (!needheaptidspace && itemsz <= BTMaxItemSizeNoHeapTid(page))
2530  return;
2531 
2532  /*
2533  * Internal page insertions cannot fail here, because that would mean that
2534  * an earlier leaf level insertion that should have failed didn't
2535  */
2536  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2537  if (!P_ISLEAF(opaque))
2538  elog(ERROR, "cannot insert oversized tuple of size %zu on internal page of index \"%s\"",
2539  itemsz, RelationGetRelationName(rel));
2540 
2541  ereport(ERROR,
2542  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2543  errmsg("index row size %zu exceeds btree version %u maximum %zu for index \"%s\"",
2544  itemsz,
2545  needheaptidspace ? BTREE_VERSION : BTREE_NOVAC_VERSION,
2546  needheaptidspace ? BTMaxItemSize(page) :
2547  BTMaxItemSizeNoHeapTid(page),
2549  errdetail("Index row references tuple (%u,%u) in relation \"%s\".",
2550  ItemPointerGetBlockNumber(&newtup->t_tid),
2552  RelationGetRelationName(heap)),
2553  errhint("Values larger than 1/3 of a buffer page cannot be indexed.\n"
2554  "Consider a function index of an MD5 hash of the value, "
2555  "or use full text indexing."),
2557 }
#define SK_BT_INDOPTION_SHIFT
Definition: nbtree.h:679
static int _bt_keep_natts(Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
Definition: nbtutils.c:2246
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
signed short int16
Definition: c.h:346
#define InvalidStrategy
Definition: stratnum.h:24
Oid sk_subtype
Definition: skey.h:69
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
Definition: fmgr.h:56
static void _bt_mark_scankey_required(ScanKey skey)
Definition: nbtutils.c:1287
#define SK_ROW_MEMBER
Definition: skey.h:118
void _bt_freestack(BTStack stack)
Definition: nbtutils.c:164
LockRelId lockRelId
Definition: rel.h:43
BTCycleId _bt_vacuum_cycleid(Relation rel)
Definition: nbtutils.c:1846
bool _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir)
Definition: nbtutils.c:533
#define AllocSetContextCreate
Definition: memutils.h:170
#define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN
Definition: nbtree.h:688
#define BTORDER_PROC
Definition: nbtree.h:392
int16 * rd_indoption
Definition: rel.h:162
void BTreeShmemInit(void)
Definition: nbtutils.c:1987
int errhint(const char *fmt,...)
Definition: elog.c:1071
static BTVacInfo * btvacinfo
Definition: nbtutils.c:1833
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
IndexAMProperty
Definition: amapi.h:34
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:794
static void test(void)
#define P_IGNORE(opaque)
Definition: nbtree.h:194
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
static size_t qunique_arg(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *, void *), void *arg)
Definition: qunique.h:45
BTScanPosItem items[MaxIndexTuplesPerPage]
Definition: nbtree.h:581
#define DatumGetInt32(X)
Definition: postgres.h:472
void ScanKeyEntryInitializeWithInfo(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, FmgrInfo *finfo, Datum argument)
Definition: scankey.c:101
int mark_elem
Definition: nbtree.h:623
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3435
#define RelationGetDescr(relation)
Definition: rel.h:449
#define ItemIdMarkDead(itemId)
Definition: itemid.h:179
BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup)
Definition: nbtutils.c:86
ItemPointer scantid
Definition: nbtree.h:475
void _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
Definition: nbtutils.c:507
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
Definition: nbtpage.c:757
MemoryContext arrayContext
Definition: nbtree.h:642
static int _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey, bool reverse, Datum *elems, int nelems)
Definition: nbtutils.c:431
#define PROGRESS_BTREE_PHASE_PERFORMSORT_1
Definition: nbtree.h:689
char * btbuildphasename(int64 phasenum)
Definition: nbtutils.c:2050
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2049
#define BTREE_VERSION
Definition: nbtree.h:133
struct BTVacInfo BTVacInfo
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
struct BTSortArrayContext BTSortArrayContext
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
ItemPointerData t_tid
Definition: itup.h:37
regproc RegProcedure
Definition: c.h:506
LockRelId relid
Definition: nbtutils.c:1821
#define Min(x, y)
Definition: c.h:905
struct BTOneVacInfo BTOneVacInfo
Oid dbId
Definition: rel.h:38
#define BTreeTupleGetHeapTID(itup)
Definition: nbtree.h:346
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define BTMaxItemSizeNoHeapTid(page)
Definition: nbtree.h:153
FmgrInfo flinfo
Definition: nbtutils.c:35
BlockNumber currPage
Definition: nbtree.h:552
#define INDEX_SIZE_MASK
Definition: itup.h:65
BTCycleId cycle_ctr
Definition: nbtutils.c:1827
struct cursor * cur
Definition: ecpg.c:28
uint16 StrategyNumber
Definition: stratnum.h:22
int errcode(int sqlerrcode)
Definition: elog.c:608
BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtutils.c:1830
#define BTScanPosIsValid(scanpos)
Definition: nbtree.h:603
OffsetNumber indexOffset
Definition: nbtree.h:543
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
void _bt_end_vacuum_callback(int code, Datum arg)
Definition: nbtutils.c:1965
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1150
unsigned int Oid
Definition: postgres_ext.h:31
Datum * elem_values
Definition: nbtree.h:625
#define BTreeTupleGetNAtts(itup, rel)
Definition: nbtree.h:327
#define OidIsValid(objectId)
Definition: c.h:639
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
#define BTreeTupleSetNAtts(itup, n)
Definition: nbtree.h:336
#define SK_SEARCHARRAY
Definition: skey.h:120
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
signed int int32
Definition: c.h:347
Size BTreeShmemSize(void)
Definition: nbtutils.c:1974
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
Relation indexRelation
Definition: relscan.h:103
uint16 OffsetNumber
Definition: off.h:24
ItemPointerData * ItemPointer
Definition: itemptr.h:49
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define SK_ROW_END
Definition: skey.h:119
#define BTCommuteStrategyNumber(strat)
Definition: nbtree.h:374
IndexTuple _bt_truncate(Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
Definition: nbtutils.c:2098
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
void _bt_restore_array_keys(IndexScanDesc scan)
Definition: nbtutils.c:611
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:843
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:5221
#define BT_READ
Definition: nbtree.h:402
static Datum _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey, StrategyNumber strat, Datum *elems, int nelems)
Definition: nbtutils.c:364
void _bt_killitems(IndexScanDesc scan)
Definition: nbtutils.c:1705
void pfree(void *pointer)
Definition: mcxt.c:1056
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
Oid * rd_indcollation
Definition: rel.h:168
#define ERROR
Definition: elog.h:43
uint16 BTCycleId
Definition: nbtree.h:27
Definition: rel.h:35
#define INDEX_ALT_TID_MASK
Definition: nbtree.h:293
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:670
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
StrategyNumber sk_strategy
Definition: skey.h:68
#define PROGRESS_BTREE_PHASE_PERFORMSORT_2
Definition: nbtree.h:690
int MaxBackends
Definition: globals.c:135
int cur_elem
Definition: nbtree.h:622
int numArrayKeys
Definition: nbtree.h:637
void _bt_end_vacuum(Relation rel)
Definition: nbtutils.c:1937
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2876
LockInfoData rd_lockInfo
Definition: rel.h:86
static char * buf
Definition: pg_test_fsync.c:67
ScanKeyData * ScanKey
Definition: skey.h:75
#define BTScanPosIsPinned(scanpos)
Definition: nbtree.h:586
bool IsUnderPostmaster
Definition: globals.c:109
#define RegProcedureIsValid(p)
Definition: c.h:641
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
int errdetail(const char *fmt,...)
Definition: elog.c:957
FmgrInfo sk_func
Definition: skey.h:71
ScanDirection
Definition: sdir.h:22
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
#define DatumGetBool(X)
Definition: postgres.h:393
void _bt_mark_array_keys(IndexScanDesc scan)
Definition: nbtutils.c:592
#define IndexRelationGetNumberOfAttributes(relation)
Definition: rel.h:435
IndexTuple index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source, int leavenatts)
Definition: indextuple.c:538
#define RelationGetRelationName(relation)
Definition: rel.h:457
BTCycleId cycleid
Definition: nbtutils.c:1822
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define SK_SEARCHNOTNULL
Definition: skey.h:122
Oid * rd_opfamily
Definition: rel.h:158
int firstItem
Definition: nbtree.h:577
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:442
#define BTREE_NOVAC_VERSION
Definition: nbtree.h:135
#define SK_ISNULL
Definition: skey.h:115
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
ScanKey arrayKeyData
Definition: nbtree.h:636
Datum OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1422
#define ereport(elevel, rest)
Definition: elog.h:141
bool pivotsearch
Definition: nbtree.h:474
#define SK_ROW_HEADER
Definition: skey.h:117
static int _bt_compare_array_elements(const void *a, const void *b, void *arg)
Definition: nbtutils.c:485
static bool _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption)
Definition: nbtutils.c:1185
#define MAX_BT_CYCLE_ID
Definition: nbtree.h:87
void _bt_preprocess_keys(IndexScanDesc scan)
Definition: nbtutils.c:731
int max_vacuums
Definition: nbtutils.c:1829
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
#define PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE
Definition: progress.h:87
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:681
bool anynullkeys
Definition: nbtree.h:472
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
struct ParallelIndexScanDescData * parallel_scan
Definition: relscan.h:151
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
int num_elems
Definition: nbtree.h:624
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
static bool _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, int tupnatts, TupleDesc tupdesc, ScanDirection dir, bool *continuescan)
Definition: nbtutils.c:1510
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
#define SK_BT_REQFWD
Definition: nbtree.h:677
bool _bt_heapkeyspace(Relation rel)
Definition: nbtpage.c:636
bool _bt_checkkeys(IndexScanDesc scan, IndexTuple tuple, int tupnatts, ScanDirection dir, bool *continuescan)
Definition: nbtutils.c:1344
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
int16 attnum
Definition: pg_attribute.h:79
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:912
bool _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
Definition: nbtutils.c:2362
int * killedItems
Definition: nbtree.h:645
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:733
#define SK_BT_DESC
Definition: nbtree.h:680
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
bool btproperty(Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
Definition: nbtutils.c:2027
bool heapkeyspace
Definition: nbtree.h:471
struct ItemPointerData ItemPointerData
#define INDEX_MAX_KEYS
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:467
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BT_N_KEYS_OFFSET_MASK
Definition: nbtree.h:297
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
int numberOfKeys
Definition: nbtree.h:632
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define MAXALIGN(LEN)
Definition: c.h:686
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
struct BTStackData * bts_parent
Definition: nbtree.h:419
ScanKeyData scankeys[INDEX_MAX_KEYS]
Definition: nbtree.h:477
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
void _bt_check_third_page(Relation rel, Relation heap, bool needheaptidspace, Page page, IndexTuple newtup)
Definition: nbtutils.c:2512
#define BTreeTupleSetAltHeapTID(itup)
Definition: nbtree.h:360
#define PROGRESS_BTREE_PHASE_LEAF_LOAD
Definition: nbtree.h:691
ItemPointerData heapTid
Definition: nbtree.h:542
#define DatumGetPointer(X)
Definition: postgres.h:549
struct ScanKeyData * keyData
Definition: relscan.h:107
#define ItemPointerSetOffsetNumber(pointer, offsetNumber)
Definition: itemptr.h:148
#define BTMaxItemSize(page)
Definition: nbtree.h:147
#define P_HIKEY
Definition: nbtree.h:217
void _bt_parallel_advance_array_keys(IndexScanDesc scan)
Definition: nbtree.c:763
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
Definition: reloptions.c:1475
int num_vacuums
Definition: nbtutils.c:1828
BTScanPosData currPos
Definition: nbtree.h:666
int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
Definition: nbtutils.c:2315
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
ScanKey keyData
Definition: nbtree.h:633
Oid sk_collation
Definition: skey.h:70
#define elog(elevel,...)
Definition: elog.h:228
BTArrayKeyInfo * arrayKeys
Definition: nbtree.h:641
int i
void _bt_preprocess_array_keys(IndexScanDesc scan)
Definition: nbtutils.c:192
void * arg
bool datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:264
Definition: c.h:550
bytea * btoptions(Datum reloptions, bool validate)
Definition: nbtutils.c:2015
#define BTMaxStrategyNumber
Definition: stratnum.h:35
static bool _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op, ScanKey leftarg, ScanKey rightarg, bool *result)
Definition: nbtutils.c:1021
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
XLogRecPtr lsn
Definition: nbtree.h:551
Buffer buf
Definition: nbtree.h:549
unsigned short t_info
Definition: itup.h:49
BTCycleId _bt_start_vacuum(Relation rel)
Definition: nbtutils.c:1880
Oid * rd_opcintype
Definition: rel.h:159
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define SK_BT_REQBKWD
Definition: nbtree.h:678
int Buffer
Definition: buf.h:23
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:188
Datum sk_argument
Definition: skey.h:72
#define ARR_ELEMTYPE(a)
Definition: array.h:280
int16 AttrNumber
Definition: attnum.h:21
int scan_key
Definition: nbtree.h:621
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1069
#define SK_SEARCHNULL
Definition: skey.h:121
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define offsetof(type, field)
Definition: c.h:656
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
#define BTP_HAS_GARBAGE
Definition: nbtree.h:77
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
AttrNumber sk_attno
Definition: skey.h:67
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161
Oid relId
Definition: rel.h:37
#define P_ISLEAF(opaque)
Definition: nbtree.h:189
#define DatumGetArrayTypeP(X)
Definition: array.h:249