PostgreSQL Source Code  git master
nbtsearch.c File Reference
#include "postgres.h"
#include "access/nbtree.h"
#include "access/relscan.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/predicate.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for nbtsearch.c:

Go to the source code of this file.

Functions

static void _bt_drop_lock_and_maybe_pin (IndexScanDesc scan, BTScanPos sp)
 
static OffsetNumber _bt_binsrch (Relation rel, BTScanInsert key, Buffer buf)
 
static bool _bt_readpage (IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
 
static void _bt_saveitem (BTScanOpaque so, int itemIndex, OffsetNumber offnum, IndexTuple itup)
 
static bool _bt_steppage (IndexScanDesc scan, ScanDirection dir)
 
static bool _bt_readnextpage (IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
 
static bool _bt_parallel_readpage (IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
 
static Buffer _bt_walk_left (Relation rel, Buffer buf, Snapshot snapshot)
 
static bool _bt_endpoint (IndexScanDesc scan, ScanDirection dir)
 
static void _bt_initialize_more_data (BTScanOpaque so, ScanDirection dir)
 
BTStack _bt_search (Relation rel, BTScanInsert key, Buffer *bufP, int access, Snapshot snapshot)
 
Buffer _bt_moveright (Relation rel, BTScanInsert key, Buffer buf, bool forupdate, BTStack stack, int access, Snapshot snapshot)
 
OffsetNumber _bt_binsrch_insert (Relation rel, BTInsertState insertstate)
 
int32 _bt_compare (Relation rel, BTScanInsert key, Page page, OffsetNumber offnum)
 
bool _bt_first (IndexScanDesc scan, ScanDirection dir)
 
bool _bt_next (IndexScanDesc scan, ScanDirection dir)
 
Buffer _bt_get_endpoint (Relation rel, uint32 level, bool rightmost, Snapshot snapshot)
 

Function Documentation

◆ _bt_binsrch()

static OffsetNumber _bt_binsrch ( Relation  rel,
BTScanInsert  key,
Buffer  buf 
)
static

Definition at line 339 of file nbtsearch.c.

References _bt_compare(), Assert, BufferGetPage, BTScanInsertData::nextkey, OffsetNumberPrev, P_FIRSTDATAKEY, P_ISLEAF, PageGetMaxOffsetNumber, PageGetSpecialPointer, BTScanInsertData::scantid, and unlikely.

Referenced by _bt_first(), and _bt_search().

342 {
343  Page page;
344  BTPageOpaque opaque;
345  OffsetNumber low,
346  high;
347  int32 result,
348  cmpval;
349 
350  /* Requesting nextkey semantics while using scantid seems nonsensical */
351  Assert(!key->nextkey || key->scantid == NULL);
352 
353  page = BufferGetPage(buf);
354  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
355 
356  low = P_FIRSTDATAKEY(opaque);
357  high = PageGetMaxOffsetNumber(page);
358 
359  /*
360  * If there are no keys on the page, return the first available slot. Note
361  * this covers two cases: the page is really empty (no keys), or it
362  * contains only a high key. The latter case is possible after vacuuming.
363  * This can never happen on an internal page, however, since they are
364  * never empty (an internal page must have children).
365  */
366  if (unlikely(high < low))
367  return low;
368 
369  /*
370  * Binary search to find the first key on the page >= scan key, or first
371  * key > scankey when nextkey is true.
372  *
373  * For nextkey=false (cmpval=1), the loop invariant is: all slots before
374  * 'low' are < scan key, all slots at or after 'high' are >= scan key.
375  *
376  * For nextkey=true (cmpval=0), the loop invariant is: all slots before
377  * 'low' are <= scan key, all slots at or after 'high' are > scan key.
378  *
379  * We can fall out when high == low.
380  */
381  high++; /* establish the loop invariant for high */
382 
383  cmpval = key->nextkey ? 0 : 1; /* select comparison value */
384 
385  while (high > low)
386  {
387  OffsetNumber mid = low + ((high - low) / 2);
388 
389  /* We have low <= mid < high, so mid points at a real slot */
390 
391  result = _bt_compare(rel, key, page, mid);
392 
393  if (result >= cmpval)
394  low = mid + 1;
395  else
396  high = mid;
397  }
398 
399  /*
400  * At this point we have high == low, but be careful: they could point
401  * past the last slot on the page.
402  *
403  * On a leaf page, we always return the first key >= scan key (resp. >
404  * scan key), which could be the last slot + 1.
405  */
406  if (P_ISLEAF(opaque))
407  return low;
408 
409  /*
410  * On a non-leaf page, return the last key < scan key (resp. <= scan key).
411  * There must be one if _bt_compare() is playing by the rules.
412  */
413  Assert(low > P_FIRSTDATAKEY(opaque));
414 
415  return OffsetNumberPrev(low);
416 }
ItemPointer scantid
Definition: nbtree.h:472
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
signed int int32
Definition: c.h:346
uint16 OffsetNumber
Definition: off.h:24
static char * buf
Definition: pg_test_fsync.c:68
int32 _bt_compare(Relation rel, BTScanInsert key, Page page, OffsetNumber offnum)
Definition: nbtsearch.c:552
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define Assert(condition)
Definition: c.h:732
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
#define unlikely(x)
Definition: c.h:208
Pointer Page
Definition: bufpage.h:78
#define P_ISLEAF(opaque)
Definition: nbtree.h:189

◆ _bt_binsrch_insert()

OffsetNumber _bt_binsrch_insert ( Relation  rel,
BTInsertState  insertstate 
)

Definition at line 438 of file nbtsearch.c.

References _bt_compare(), Assert, BTInsertStateData::bounds_valid, BTInsertStateData::buf, BufferGetPage, InvalidOffsetNumber, BTInsertStateData::itup_key, sort-test::key, BTInsertStateData::low, BTScanInsertData::nextkey, P_FIRSTDATAKEY, P_ISLEAF, PageGetMaxOffsetNumber, PageGetSpecialPointer, BTInsertStateData::stricthigh, and unlikely.

Referenced by _bt_check_unique(), _bt_findinsertloc(), and bt_rootdescend().

439 {
440  BTScanInsert key = insertstate->itup_key;
441  Page page;
442  BTPageOpaque opaque;
443  OffsetNumber low,
444  high,
445  stricthigh;
446  int32 result,
447  cmpval;
448 
449  page = BufferGetPage(insertstate->buf);
450  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
451 
452  Assert(P_ISLEAF(opaque));
453  Assert(!key->nextkey);
454 
455  if (!insertstate->bounds_valid)
456  {
457  /* Start new binary search */
458  low = P_FIRSTDATAKEY(opaque);
459  high = PageGetMaxOffsetNumber(page);
460  }
461  else
462  {
463  /* Restore result of previous binary search against same page */
464  low = insertstate->low;
465  high = insertstate->stricthigh;
466  }
467 
468  /* If there are no keys on the page, return the first available slot */
469  if (unlikely(high < low))
470  {
471  /* Caller can't reuse bounds */
472  insertstate->low = InvalidOffsetNumber;
473  insertstate->stricthigh = InvalidOffsetNumber;
474  insertstate->bounds_valid = false;
475  return low;
476  }
477 
478  /*
479  * Binary search to find the first key on the page >= scan key. (nextkey
480  * is always false when inserting).
481  *
482  * The loop invariant is: all slots before 'low' are < scan key, all slots
483  * at or after 'high' are >= scan key. 'stricthigh' is > scan key, and is
484  * maintained to save additional search effort for caller.
485  *
486  * We can fall out when high == low.
487  */
488  if (!insertstate->bounds_valid)
489  high++; /* establish the loop invariant for high */
490  stricthigh = high; /* high initially strictly higher */
491 
492  cmpval = 1; /* !nextkey comparison value */
493 
494  while (high > low)
495  {
496  OffsetNumber mid = low + ((high - low) / 2);
497 
498  /* We have low <= mid < high, so mid points at a real slot */
499 
500  result = _bt_compare(rel, key, page, mid);
501 
502  if (result >= cmpval)
503  low = mid + 1;
504  else
505  {
506  high = mid;
507  if (result != 0)
508  stricthigh = high;
509  }
510  }
511 
512  /*
513  * On a leaf page, a binary search always returns the first key >= scan
514  * key (at least in !nextkey case), which could be the last slot + 1. This
515  * is also the lower bound of cached search.
516  *
517  * stricthigh may also be the last slot + 1, which prevents caller from
518  * using bounds directly, but is still useful to us if we're called a
519  * second time with cached bounds (cached low will be < stricthigh when
520  * that happens).
521  */
522  insertstate->low = low;
523  insertstate->stricthigh = stricthigh;
524  insertstate->bounds_valid = true;
525 
526  return low;
527 }
bool bounds_valid
Definition: nbtree.h:504
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
OffsetNumber stricthigh
Definition: nbtree.h:506
OffsetNumber low
Definition: nbtree.h:505
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
signed int int32
Definition: c.h:346
uint16 OffsetNumber
Definition: off.h:24
int32 _bt_compare(Relation rel, BTScanInsert key, Page page, OffsetNumber offnum)
Definition: nbtsearch.c:552
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define InvalidOffsetNumber
Definition: off.h:26
BTScanInsert itup_key
Definition: nbtree.h:494
#define Assert(condition)
Definition: c.h:732
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define unlikely(x)
Definition: c.h:208
Pointer Page
Definition: bufpage.h:78
#define P_ISLEAF(opaque)
Definition: nbtree.h:189

◆ _bt_compare()

int32 _bt_compare ( Relation  rel,
BTScanInsert  key,
Page  page,
OffsetNumber  offnum 
)

Definition at line 552 of file nbtsearch.c.

References _bt_check_natts(), Assert, BTreeTupleGetHeapTID, BTreeTupleGetNAtts, DatumGetInt32, FunctionCall2Coll(), BTScanInsertData::heapkeyspace, i, index_getattr, IndexRelationGetNumberOfKeyAttributes, INVERT_COMPARE_RESULT, ItemPointerCompare(), BTScanInsertData::keysz, Min, P_FIRSTDATAKEY, P_ISLEAF, PageGetItem, PageGetItemId, PageGetSpecialPointer, BTScanInsertData::pivotsearch, RelationGetDescr, BTScanInsertData::scankeys, BTScanInsertData::scantid, ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, ScanKeyData::sk_collation, ScanKeyData::sk_flags, ScanKeyData::sk_func, and SK_ISNULL.

Referenced by _bt_binsrch(), _bt_binsrch_insert(), _bt_check_unique(), _bt_doinsert(), _bt_findinsertloc(), _bt_moveright(), bt_rootdescend(), invariant_g_offset(), invariant_l_nontarget_offset(), invariant_l_offset(), and invariant_leq_offset().

556 {
557  TupleDesc itupdesc = RelationGetDescr(rel);
559  IndexTuple itup;
560  ItemPointer heapTid;
561  ScanKey scankey;
562  int ncmpkey;
563  int ntupatts;
564 
565  Assert(_bt_check_natts(rel, key->heapkeyspace, page, offnum));
567  Assert(key->heapkeyspace || key->scantid == NULL);
568 
569  /*
570  * Force result ">" if target item is first data item on an internal page
571  * --- see NOTE above.
572  */
573  if (!P_ISLEAF(opaque) && offnum == P_FIRSTDATAKEY(opaque))
574  return 1;
575 
576  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
577  ntupatts = BTreeTupleGetNAtts(itup, rel);
578 
579  /*
580  * The scan key is set up with the attribute number associated with each
581  * term in the key. It is important that, if the index is multi-key, the
582  * scan contain the first k key attributes, and that they be in order. If
583  * you think about how multi-key ordering works, you'll understand why
584  * this is.
585  *
586  * We don't test for violation of this condition here, however. The
587  * initial setup for the index scan had better have gotten it right (see
588  * _bt_first).
589  */
590 
591  ncmpkey = Min(ntupatts, key->keysz);
592  Assert(key->heapkeyspace || ncmpkey == key->keysz);
593  scankey = key->scankeys;
594  for (int i = 1; i <= ncmpkey; i++)
595  {
596  Datum datum;
597  bool isNull;
598  int32 result;
599 
600  datum = index_getattr(itup, scankey->sk_attno, itupdesc, &isNull);
601 
602  /* see comments about NULLs handling in btbuild */
603  if (scankey->sk_flags & SK_ISNULL) /* key is NULL */
604  {
605  if (isNull)
606  result = 0; /* NULL "=" NULL */
607  else if (scankey->sk_flags & SK_BT_NULLS_FIRST)
608  result = -1; /* NULL "<" NOT_NULL */
609  else
610  result = 1; /* NULL ">" NOT_NULL */
611  }
612  else if (isNull) /* key is NOT_NULL and item is NULL */
613  {
614  if (scankey->sk_flags & SK_BT_NULLS_FIRST)
615  result = 1; /* NOT_NULL ">" NULL */
616  else
617  result = -1; /* NOT_NULL "<" NULL */
618  }
619  else
620  {
621  /*
622  * The sk_func needs to be passed the index value as left arg and
623  * the sk_argument as right arg (they might be of different
624  * types). Since it is convenient for callers to think of
625  * _bt_compare as comparing the scankey to the index item, we have
626  * to flip the sign of the comparison result. (Unless it's a DESC
627  * column, in which case we *don't* flip the sign.)
628  */
629  result = DatumGetInt32(FunctionCall2Coll(&scankey->sk_func,
630  scankey->sk_collation,
631  datum,
632  scankey->sk_argument));
633 
634  if (!(scankey->sk_flags & SK_BT_DESC))
635  INVERT_COMPARE_RESULT(result);
636  }
637 
638  /* if the keys are unequal, return the difference */
639  if (result != 0)
640  return result;
641 
642  scankey++;
643  }
644 
645  /*
646  * All non-truncated attributes (other than heap TID) were found to be
647  * equal. Treat truncated attributes as minus infinity when scankey has a
648  * key attribute value that would otherwise be compared directly.
649  *
650  * Note: it doesn't matter if ntupatts includes non-key attributes;
651  * scankey won't, so explicitly excluding non-key attributes isn't
652  * necessary.
653  */
654  if (key->keysz > ntupatts)
655  return 1;
656 
657  /*
658  * Use the heap TID attribute and scantid to try to break the tie. The
659  * rules are the same as any other key attribute -- only the
660  * representation differs.
661  */
662  heapTid = BTreeTupleGetHeapTID(itup);
663  if (key->scantid == NULL)
664  {
665  /*
666  * Most searches have a scankey that is considered greater than a
667  * truncated pivot tuple if and when the scankey has equal values for
668  * attributes up to and including the least significant untruncated
669  * attribute in tuple.
670  *
671  * For example, if an index has the minimum two attributes (single
672  * user key attribute, plus heap TID attribute), and a page's high key
673  * is ('foo', -inf), and scankey is ('foo', <omitted>), the search
674  * will not descend to the page to the left. The search will descend
675  * right instead. The truncated attribute in pivot tuple means that
676  * all non-pivot tuples on the page to the left are strictly < 'foo',
677  * so it isn't necessary to descend left. In other words, search
678  * doesn't have to descend left because it isn't interested in a match
679  * that has a heap TID value of -inf.
680  *
681  * However, some searches (pivotsearch searches) actually require that
682  * we descend left when this happens. -inf is treated as a possible
683  * match for omitted scankey attribute(s). This is needed by page
684  * deletion, which must re-find leaf pages that are targets for
685  * deletion using their high keys.
686  *
687  * Note: the heap TID part of the test ensures that scankey is being
688  * compared to a pivot tuple with one or more truncated key
689  * attributes.
690  *
691  * Note: pg_upgrade'd !heapkeyspace indexes must always descend to the
692  * left here, since they have no heap TID attribute (and cannot have
693  * any -inf key values in any case, since truncation can only remove
694  * non-key attributes). !heapkeyspace searches must always be
695  * prepared to deal with matches on both sides of the pivot once the
696  * leaf level is reached.
697  */
698  if (key->heapkeyspace && !key->pivotsearch &&
699  key->keysz == ntupatts && heapTid == NULL)
700  return 1;
701 
702  /* All provided scankey arguments found to be equal */
703  return 0;
704  }
705 
706  /*
707  * Treat truncated heap TID as minus infinity, since scankey has a key
708  * attribute value (scantid) that would otherwise be compared directly
709  */
711  if (heapTid == NULL)
712  return 1;
713 
715  return ItemPointerCompare(key->scantid, heapTid);
716 }
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
#define DatumGetInt32(X)
Definition: postgres.h:472
#define RelationGetDescr(relation)
Definition: rel.h:442
ItemPointer scantid
Definition: nbtree.h:472
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
#define Min(x, y)
Definition: c.h:904
#define BTreeTupleGetHeapTID(itup)
Definition: nbtree.h:346
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1150
#define BTreeTupleGetNAtts(itup, rel)
Definition: nbtree.h:327
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
signed int int32
Definition: c.h:346
IndexTupleData * IndexTuple
Definition: itup.h:53
FmgrInfo sk_func
Definition: skey.h:71
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:435
#define SK_ISNULL
Definition: skey.h:115
bool pivotsearch
Definition: nbtree.h:471
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:678
uintptr_t Datum
Definition: postgres.h:367
bool _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
Definition: nbtutils.c:2382
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:732
#define SK_BT_DESC
Definition: nbtree.h:677
bool heapkeyspace
Definition: nbtree.h:468
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
ScanKeyData scankeys[INDEX_MAX_KEYS]
Definition: nbtree.h:474
Oid sk_collation
Definition: skey.h:70
int i
Datum sk_argument
Definition: skey.h:72
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1044
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
AttrNumber sk_attno
Definition: skey.h:67
#define P_ISLEAF(opaque)
Definition: nbtree.h:189

◆ _bt_drop_lock_and_maybe_pin()

static void _bt_drop_lock_and_maybe_pin ( IndexScanDesc  scan,
BTScanPos  sp 
)
static

Definition at line 57 of file nbtsearch.c.

References BTScanPosData::buf, BUFFER_LOCK_UNLOCK, IndexScanDescData::indexRelation, InvalidBuffer, IsMVCCSnapshot, LockBuffer(), RelationNeedsWAL, ReleaseBuffer(), IndexScanDescData::xs_snapshot, and IndexScanDescData::xs_want_itup.

Referenced by _bt_endpoint(), _bt_first(), _bt_parallel_readpage(), and _bt_steppage().

58 {
60 
61  if (IsMVCCSnapshot(scan->xs_snapshot) &&
63  !scan->xs_want_itup)
64  {
65  ReleaseBuffer(sp->buf);
66  sp->buf = InvalidBuffer;
67  }
68 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
#define InvalidBuffer
Definition: buf.h:25
struct SnapshotData * xs_snapshot
Definition: relscan.h:104
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3353
Relation indexRelation
Definition: relscan.h:103
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
#define IsMVCCSnapshot(snapshot)
Definition: snapmgr.h:98
#define RelationNeedsWAL(relation)
Definition: rel.h:518
Buffer buf
Definition: nbtree.h:546

◆ _bt_endpoint()

static bool _bt_endpoint ( IndexScanDesc  scan,
ScanDirection  dir 
)
static

Definition at line 2141 of file nbtsearch.c.

References _bt_drop_lock_and_maybe_pin(), _bt_get_endpoint(), _bt_initialize_more_data(), _bt_readpage(), _bt_steppage(), Assert, BTScanPosInvalidate, buf, BTScanPosData::buf, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, BufferIsValid, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, elog, ERROR, IndexScanDescData::indexRelation, BTScanPosData::itemIndex, BTScanPosData::items, LockBuffer(), IndexScanDescData::opaque, P_FIRSTDATAKEY, P_ISLEAF, P_RIGHTMOST, PageGetMaxOffsetNumber, PageGetSpecialPointer, PredicateLockPage(), PredicateLockRelation(), ScanDirectionIsBackward, ScanDirectionIsForward, IndexScanDescData::xs_heaptid, IndexScanDescData::xs_itup, IndexScanDescData::xs_snapshot, and IndexScanDescData::xs_want_itup.

Referenced by _bt_first().

2142 {
2143  Relation rel = scan->indexRelation;
2144  BTScanOpaque so = (BTScanOpaque) scan->opaque;
2145  Buffer buf;
2146  Page page;
2147  BTPageOpaque opaque;
2148  OffsetNumber start;
2149  BTScanPosItem *currItem;
2150 
2151  /*
2152  * Scan down to the leftmost or rightmost leaf page. This is a simplified
2153  * version of _bt_search(). We don't maintain a stack since we know we
2154  * won't need it.
2155  */
2157 
2158  if (!BufferIsValid(buf))
2159  {
2160  /*
2161  * Empty index. Lock the whole relation, as nothing finer to lock
2162  * exists.
2163  */
2164  PredicateLockRelation(rel, scan->xs_snapshot);
2166  return false;
2167  }
2168 
2170  page = BufferGetPage(buf);
2171  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2172  Assert(P_ISLEAF(opaque));
2173 
2174  if (ScanDirectionIsForward(dir))
2175  {
2176  /* There could be dead pages to the left, so not this: */
2177  /* Assert(P_LEFTMOST(opaque)); */
2178 
2179  start = P_FIRSTDATAKEY(opaque);
2180  }
2181  else if (ScanDirectionIsBackward(dir))
2182  {
2183  Assert(P_RIGHTMOST(opaque));
2184 
2185  start = PageGetMaxOffsetNumber(page);
2186  }
2187  else
2188  {
2189  elog(ERROR, "invalid scan direction: %d", (int) dir);
2190  start = 0; /* keep compiler quiet */
2191  }
2192 
2193  /* remember which buffer we have pinned */
2194  so->currPos.buf = buf;
2195 
2196  _bt_initialize_more_data(so, dir);
2197 
2198  /*
2199  * Now load data from the first page of the scan.
2200  */
2201  if (!_bt_readpage(scan, dir, start))
2202  {
2203  /*
2204  * There's no actually-matching data on this page. Try to advance to
2205  * the next page. Return false if there's no matching data at all.
2206  */
2208  if (!_bt_steppage(scan, dir))
2209  return false;
2210  }
2211  else
2212  {
2213  /* Drop the lock, and maybe the pin, on the current page */
2215  }
2216 
2217  /* OK, itemIndex says what to return */
2218  currItem = &so->currPos.items[so->currPos.itemIndex];
2219  scan->xs_heaptid = currItem->heapTid;
2220  if (scan->xs_want_itup)
2221  scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
2222 
2223  return true;
2224 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
void PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
Definition: predicate.c:2526
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
IndexTuple xs_itup
Definition: relscan.h:127
BTScanPosItem items[MaxIndexTuplesPerPage]
Definition: nbtree.h:578
static void _bt_drop_lock_and_maybe_pin(IndexScanDesc scan, BTScanPos sp)
Definition: nbtsearch.c:57
int itemIndex
Definition: nbtree.h:576
void PredicateLockRelation(Relation relation, Snapshot snapshot)
Definition: predicate.c:2503
char * currTuples
Definition: nbtree.h:650
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
struct SnapshotData * xs_snapshot
Definition: relscan.h:104
Buffer _bt_get_endpoint(Relation rel, uint32 level, bool rightmost, Snapshot snapshot)
Definition: nbtsearch.c:2057
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
Relation indexRelation
Definition: relscan.h:103
uint16 OffsetNumber
Definition: off.h:24
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
#define ERROR
Definition: elog.h:43
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:667
ItemPointerData xs_heaptid
Definition: relscan.h:132
static char * buf
Definition: pg_test_fsync.c:68
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
static void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir)
Definition: nbtsearch.c:2231
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
#define Assert(condition)
Definition: c.h:732
#define BTScanPosInvalidate(scanpos)
Definition: nbtree.h:606
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
BTScanPosData currPos
Definition: nbtree.h:663
#define elog(elevel,...)
Definition: elog.h:226
static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:1623
Buffer buf
Definition: nbtree.h:546
int Buffer
Definition: buf.h:23
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:188
static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
Definition: nbtsearch.c:1396
Pointer Page
Definition: bufpage.h:78
#define P_ISLEAF(opaque)
Definition: nbtree.h:189

◆ _bt_first()

bool _bt_first ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 739 of file nbtsearch.c.

References _bt_binsrch(), _bt_drop_lock_and_maybe_pin(), _bt_endpoint(), _bt_freestack(), _bt_heapkeyspace(), _bt_initialize_more_data(), _bt_parallel_done(), _bt_parallel_readpage(), _bt_parallel_seize(), _bt_preprocess_keys(), _bt_readpage(), _bt_search(), _bt_steppage(), Assert, BT_READ, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BTORDER_PROC, BTScanPosInvalidate, BTScanPosIsValid, buf, BTScanPosData::buf, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferIsValid, cur, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, DatumGetPointer, elog, ERROR, get_opfamily_proc(), i, index_getprocinfo(), INDEX_MAX_KEYS, IndexScanDescData::indexRelation, InvalidBlockNumber, InvalidOid, InvalidStrategy, BTScanPosData::itemIndex, BTScanPosData::items, BTScanOpaqueData::keyData, LockBuffer(), BTScanOpaqueData::numberOfKeys, OffsetNumberPrev, IndexScanDescData::opaque, P_NONE, IndexScanDescData::parallel_scan, pgstat_count_index_scan, PredicateLockPage(), PredicateLockRelation(), BTScanOpaqueData::qual_ok, RelationData::rd_opcintype, RelationData::rd_opfamily, RegProcedureIsValid, RelationGetRelationName, ScanDirectionIsBackward, ScanDirectionIsForward, ScanKeyEntryInitialize(), ScanKeyEntryInitializeWithInfo(), ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, ScanKeyData::sk_collation, ScanKeyData::sk_flags, SK_ISNULL, SK_ROW_END, SK_ROW_HEADER, SK_ROW_MEMBER, SK_SEARCHNOTNULL, ScanKeyData::sk_strategy, ScanKeyData::sk_subtype, status(), IndexScanDescData::xs_heaptid, IndexScanDescData::xs_itup, IndexScanDescData::xs_snapshot, and IndexScanDescData::xs_want_itup.

Referenced by btgetbitmap(), and btgettuple().

740 {
741  Relation rel = scan->indexRelation;
742  BTScanOpaque so = (BTScanOpaque) scan->opaque;
743  Buffer buf;
744  BTStack stack;
745  OffsetNumber offnum;
746  StrategyNumber strat;
747  bool nextkey;
748  bool goback;
749  BTScanInsertData inskey;
750  ScanKey startKeys[INDEX_MAX_KEYS];
751  ScanKeyData notnullkeys[INDEX_MAX_KEYS];
752  int keysCount = 0;
753  int i;
754  bool status = true;
755  StrategyNumber strat_total;
756  BTScanPosItem *currItem;
757  BlockNumber blkno;
758 
760 
762 
763  /*
764  * Examine the scan keys and eliminate any redundant keys; also mark the
765  * keys that must be matched to continue the scan.
766  */
767  _bt_preprocess_keys(scan);
768 
769  /*
770  * Quit now if _bt_preprocess_keys() discovered that the scan keys can
771  * never be satisfied (eg, x == 1 AND x > 2).
772  */
773  if (!so->qual_ok)
774  return false;
775 
776  /*
777  * For parallel scans, get the starting page from shared state. If the
778  * scan has not started, proceed to find out first leaf page in the usual
779  * way while keeping other participating processes waiting. If the scan
780  * has already begun, use the page number from the shared structure.
781  */
782  if (scan->parallel_scan != NULL)
783  {
784  status = _bt_parallel_seize(scan, &blkno);
785  if (!status)
786  return false;
787  else if (blkno == P_NONE)
788  {
789  _bt_parallel_done(scan);
790  return false;
791  }
792  else if (blkno != InvalidBlockNumber)
793  {
794  if (!_bt_parallel_readpage(scan, blkno, dir))
795  return false;
796  goto readcomplete;
797  }
798  }
799 
800  /*----------
801  * Examine the scan keys to discover where we need to start the scan.
802  *
803  * We want to identify the keys that can be used as starting boundaries;
804  * these are =, >, or >= keys for a forward scan or =, <, <= keys for
805  * a backwards scan. We can use keys for multiple attributes so long as
806  * the prior attributes had only =, >= (resp. =, <=) keys. Once we accept
807  * a > or < boundary or find an attribute with no boundary (which can be
808  * thought of as the same as "> -infinity"), we can't use keys for any
809  * attributes to its right, because it would break our simplistic notion
810  * of what initial positioning strategy to use.
811  *
812  * When the scan keys include cross-type operators, _bt_preprocess_keys
813  * may not be able to eliminate redundant keys; in such cases we will
814  * arbitrarily pick a usable one for each attribute. This is correct
815  * but possibly not optimal behavior. (For example, with keys like
816  * "x >= 4 AND x >= 5" we would elect to scan starting at x=4 when
817  * x=5 would be more efficient.) Since the situation only arises given
818  * a poorly-worded query plus an incomplete opfamily, live with it.
819  *
820  * When both equality and inequality keys appear for a single attribute
821  * (again, only possible when cross-type operators appear), we *must*
822  * select one of the equality keys for the starting point, because
823  * _bt_checkkeys() will stop the scan as soon as an equality qual fails.
824  * For example, if we have keys like "x >= 4 AND x = 10" and we elect to
825  * start at x=4, we will fail and stop before reaching x=10. If multiple
826  * equality quals survive preprocessing, however, it doesn't matter which
827  * one we use --- by definition, they are either redundant or
828  * contradictory.
829  *
830  * Any regular (not SK_SEARCHNULL) key implies a NOT NULL qualifier.
831  * If the index stores nulls at the end of the index we'll be starting
832  * from, and we have no boundary key for the column (which means the key
833  * we deduced NOT NULL from is an inequality key that constrains the other
834  * end of the index), then we cons up an explicit SK_SEARCHNOTNULL key to
835  * use as a boundary key. If we didn't do this, we might find ourselves
836  * traversing a lot of null entries at the start of the scan.
837  *
838  * In this loop, row-comparison keys are treated the same as keys on their
839  * first (leftmost) columns. We'll add on lower-order columns of the row
840  * comparison below, if possible.
841  *
842  * The selected scan keys (at most one per index column) are remembered by
843  * storing their addresses into the local startKeys[] array.
844  *----------
845  */
846  strat_total = BTEqualStrategyNumber;
847  if (so->numberOfKeys > 0)
848  {
849  AttrNumber curattr;
850  ScanKey chosen;
851  ScanKey impliesNN;
852  ScanKey cur;
853 
854  /*
855  * chosen is the so-far-chosen key for the current attribute, if any.
856  * We don't cast the decision in stone until we reach keys for the
857  * next attribute.
858  */
859  curattr = 1;
860  chosen = NULL;
861  /* Also remember any scankey that implies a NOT NULL constraint */
862  impliesNN = NULL;
863 
864  /*
865  * Loop iterates from 0 to numberOfKeys inclusive; we use the last
866  * pass to handle after-last-key processing. Actual exit from the
867  * loop is at one of the "break" statements below.
868  */
869  for (cur = so->keyData, i = 0;; cur++, i++)
870  {
871  if (i >= so->numberOfKeys || cur->sk_attno != curattr)
872  {
873  /*
874  * Done looking at keys for curattr. If we didn't find a
875  * usable boundary key, see if we can deduce a NOT NULL key.
876  */
877  if (chosen == NULL && impliesNN != NULL &&
878  ((impliesNN->sk_flags & SK_BT_NULLS_FIRST) ?
881  {
882  /* Yes, so build the key in notnullkeys[keysCount] */
883  chosen = &notnullkeys[keysCount];
884  ScanKeyEntryInitialize(chosen,
886  (impliesNN->sk_flags &
888  curattr,
889  ((impliesNN->sk_flags & SK_BT_NULLS_FIRST) ?
892  InvalidOid,
893  InvalidOid,
894  InvalidOid,
895  (Datum) 0);
896  }
897 
898  /*
899  * If we still didn't find a usable boundary key, quit; else
900  * save the boundary key pointer in startKeys.
901  */
902  if (chosen == NULL)
903  break;
904  startKeys[keysCount++] = chosen;
905 
906  /*
907  * Adjust strat_total, and quit if we have stored a > or <
908  * key.
909  */
910  strat = chosen->sk_strategy;
911  if (strat != BTEqualStrategyNumber)
912  {
913  strat_total = strat;
914  if (strat == BTGreaterStrategyNumber ||
915  strat == BTLessStrategyNumber)
916  break;
917  }
918 
919  /*
920  * Done if that was the last attribute, or if next key is not
921  * in sequence (implying no boundary key is available for the
922  * next attribute).
923  */
924  if (i >= so->numberOfKeys ||
925  cur->sk_attno != curattr + 1)
926  break;
927 
928  /*
929  * Reset for next attr.
930  */
931  curattr = cur->sk_attno;
932  chosen = NULL;
933  impliesNN = NULL;
934  }
935 
936  /*
937  * Can we use this key as a starting boundary for this attr?
938  *
939  * If not, does it imply a NOT NULL constraint? (Because
940  * SK_SEARCHNULL keys are always assigned BTEqualStrategyNumber,
941  * *any* inequality key works for that; we need not test.)
942  */
943  switch (cur->sk_strategy)
944  {
947  if (chosen == NULL)
948  {
949  if (ScanDirectionIsBackward(dir))
950  chosen = cur;
951  else
952  impliesNN = cur;
953  }
954  break;
956  /* override any non-equality choice */
957  chosen = cur;
958  break;
961  if (chosen == NULL)
962  {
963  if (ScanDirectionIsForward(dir))
964  chosen = cur;
965  else
966  impliesNN = cur;
967  }
968  break;
969  }
970  }
971  }
972 
973  /*
974  * If we found no usable boundary keys, we have to start from one end of
975  * the tree. Walk down that edge to the first or last key, and scan from
976  * there.
977  */
978  if (keysCount == 0)
979  {
980  bool match;
981 
982  match = _bt_endpoint(scan, dir);
983 
984  if (!match)
985  {
986  /* No match, so mark (parallel) scan finished */
987  _bt_parallel_done(scan);
988  }
989 
990  return match;
991  }
992 
993  /*
994  * We want to start the scan somewhere within the index. Set up an
995  * insertion scankey we can use to search for the boundary point we
996  * identified above. The insertion scankey is built using the keys
997  * identified by startKeys[]. (Remaining insertion scankey fields are
998  * initialized after initial-positioning strategy is finalized.)
999  */
1000  Assert(keysCount <= INDEX_MAX_KEYS);
1001  for (i = 0; i < keysCount; i++)
1002  {
1003  ScanKey cur = startKeys[i];
1004 
1005  Assert(cur->sk_attno == i + 1);
1006 
1007  if (cur->sk_flags & SK_ROW_HEADER)
1008  {
1009  /*
1010  * Row comparison header: look to the first row member instead.
1011  *
1012  * The member scankeys are already in insertion format (ie, they
1013  * have sk_func = 3-way-comparison function), but we have to watch
1014  * out for nulls, which _bt_preprocess_keys didn't check. A null
1015  * in the first row member makes the condition unmatchable, just
1016  * like qual_ok = false.
1017  */
1018  ScanKey subkey = (ScanKey) DatumGetPointer(cur->sk_argument);
1019 
1020  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1021  if (subkey->sk_flags & SK_ISNULL)
1022  {
1023  _bt_parallel_done(scan);
1024  return false;
1025  }
1026  memcpy(inskey.scankeys + i, subkey, sizeof(ScanKeyData));
1027 
1028  /*
1029  * If the row comparison is the last positioning key we accepted,
1030  * try to add additional keys from the lower-order row members.
1031  * (If we accepted independent conditions on additional index
1032  * columns, we use those instead --- doesn't seem worth trying to
1033  * determine which is more restrictive.) Note that this is OK
1034  * even if the row comparison is of ">" or "<" type, because the
1035  * condition applied to all but the last row member is effectively
1036  * ">=" or "<=", and so the extra keys don't break the positioning
1037  * scheme. But, by the same token, if we aren't able to use all
1038  * the row members, then the part of the row comparison that we
1039  * did use has to be treated as just a ">=" or "<=" condition, and
1040  * so we'd better adjust strat_total accordingly.
1041  */
1042  if (i == keysCount - 1)
1043  {
1044  bool used_all_subkeys = false;
1045 
1046  Assert(!(subkey->sk_flags & SK_ROW_END));
1047  for (;;)
1048  {
1049  subkey++;
1050  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1051  if (subkey->sk_attno != keysCount + 1)
1052  break; /* out-of-sequence, can't use it */
1053  if (subkey->sk_strategy != cur->sk_strategy)
1054  break; /* wrong direction, can't use it */
1055  if (subkey->sk_flags & SK_ISNULL)
1056  break; /* can't use null keys */
1057  Assert(keysCount < INDEX_MAX_KEYS);
1058  memcpy(inskey.scankeys + keysCount, subkey,
1059  sizeof(ScanKeyData));
1060  keysCount++;
1061  if (subkey->sk_flags & SK_ROW_END)
1062  {
1063  used_all_subkeys = true;
1064  break;
1065  }
1066  }
1067  if (!used_all_subkeys)
1068  {
1069  switch (strat_total)
1070  {
1071  case BTLessStrategyNumber:
1072  strat_total = BTLessEqualStrategyNumber;
1073  break;
1075  strat_total = BTGreaterEqualStrategyNumber;
1076  break;
1077  }
1078  }
1079  break; /* done with outer loop */
1080  }
1081  }
1082  else
1083  {
1084  /*
1085  * Ordinary comparison key. Transform the search-style scan key
1086  * to an insertion scan key by replacing the sk_func with the
1087  * appropriate btree comparison function.
1088  *
1089  * If scankey operator is not a cross-type comparison, we can use
1090  * the cached comparison function; otherwise gotta look it up in
1091  * the catalogs. (That can't lead to infinite recursion, since no
1092  * indexscan initiated by syscache lookup will use cross-data-type
1093  * operators.)
1094  *
1095  * We support the convention that sk_subtype == InvalidOid means
1096  * the opclass input type; this is a hack to simplify life for
1097  * ScanKeyInit().
1098  */
1099  if (cur->sk_subtype == rel->rd_opcintype[i] ||
1100  cur->sk_subtype == InvalidOid)
1101  {
1102  FmgrInfo *procinfo;
1103 
1104  procinfo = index_getprocinfo(rel, cur->sk_attno, BTORDER_PROC);
1105  ScanKeyEntryInitializeWithInfo(inskey.scankeys + i,
1106  cur->sk_flags,
1107  cur->sk_attno,
1109  cur->sk_subtype,
1110  cur->sk_collation,
1111  procinfo,
1112  cur->sk_argument);
1113  }
1114  else
1115  {
1116  RegProcedure cmp_proc;
1117 
1118  cmp_proc = get_opfamily_proc(rel->rd_opfamily[i],
1119  rel->rd_opcintype[i],
1120  cur->sk_subtype,
1121  BTORDER_PROC);
1122  if (!RegProcedureIsValid(cmp_proc))
1123  elog(ERROR, "missing support function %d(%u,%u) for attribute %d of index \"%s\"",
1124  BTORDER_PROC, rel->rd_opcintype[i], cur->sk_subtype,
1125  cur->sk_attno, RelationGetRelationName(rel));
1126  ScanKeyEntryInitialize(inskey.scankeys + i,
1127  cur->sk_flags,
1128  cur->sk_attno,
1130  cur->sk_subtype,
1131  cur->sk_collation,
1132  cmp_proc,
1133  cur->sk_argument);
1134  }
1135  }
1136  }
1137 
1138  /*----------
1139  * Examine the selected initial-positioning strategy to determine exactly
1140  * where we need to start the scan, and set flag variables to control the
1141  * code below.
1142  *
1143  * If nextkey = false, _bt_search and _bt_binsrch will locate the first
1144  * item >= scan key. If nextkey = true, they will locate the first
1145  * item > scan key.
1146  *
1147  * If goback = true, we will then step back one item, while if
1148  * goback = false, we will start the scan on the located item.
1149  *----------
1150  */
1151  switch (strat_total)
1152  {
1153  case BTLessStrategyNumber:
1154 
1155  /*
1156  * Find first item >= scankey, then back up one to arrive at last
1157  * item < scankey. (Note: this positioning strategy is only used
1158  * for a backward scan, so that is always the correct starting
1159  * position.)
1160  */
1161  nextkey = false;
1162  goback = true;
1163  break;
1164 
1166 
1167  /*
1168  * Find first item > scankey, then back up one to arrive at last
1169  * item <= scankey. (Note: this positioning strategy is only used
1170  * for a backward scan, so that is always the correct starting
1171  * position.)
1172  */
1173  nextkey = true;
1174  goback = true;
1175  break;
1176 
1177  case BTEqualStrategyNumber:
1178 
1179  /*
1180  * If a backward scan was specified, need to start with last equal
1181  * item not first one.
1182  */
1183  if (ScanDirectionIsBackward(dir))
1184  {
1185  /*
1186  * This is the same as the <= strategy. We will check at the
1187  * end whether the found item is actually =.
1188  */
1189  nextkey = true;
1190  goback = true;
1191  }
1192  else
1193  {
1194  /*
1195  * This is the same as the >= strategy. We will check at the
1196  * end whether the found item is actually =.
1197  */
1198  nextkey = false;
1199  goback = false;
1200  }
1201  break;
1202 
1204 
1205  /*
1206  * Find first item >= scankey. (This is only used for forward
1207  * scans.)
1208  */
1209  nextkey = false;
1210  goback = false;
1211  break;
1212 
1214 
1215  /*
1216  * Find first item > scankey. (This is only used for forward
1217  * scans.)
1218  */
1219  nextkey = true;
1220  goback = false;
1221  break;
1222 
1223  default:
1224  /* can't get here, but keep compiler quiet */
1225  elog(ERROR, "unrecognized strat_total: %d", (int) strat_total);
1226  return false;
1227  }
1228 
1229  /* Initialize remaining insertion scan key fields */
1230  inskey.heapkeyspace = _bt_heapkeyspace(rel);
1231  inskey.anynullkeys = false; /* unused */
1232  inskey.nextkey = nextkey;
1233  inskey.pivotsearch = false;
1234  inskey.scantid = NULL;
1235  inskey.keysz = keysCount;
1236 
1237  /*
1238  * Use the manufactured insertion scan key to descend the tree and
1239  * position ourselves on the target leaf page.
1240  */
1241  stack = _bt_search(rel, &inskey, &buf, BT_READ, scan->xs_snapshot);
1242 
1243  /* don't need to keep the stack around... */
1244  _bt_freestack(stack);
1245 
1246  if (!BufferIsValid(buf))
1247  {
1248  /*
1249  * We only get here if the index is completely empty. Lock relation
1250  * because nothing finer to lock exists.
1251  */
1252  PredicateLockRelation(rel, scan->xs_snapshot);
1253 
1254  /*
1255  * mark parallel scan as done, so that all the workers can finish
1256  * their scan
1257  */
1258  _bt_parallel_done(scan);
1260 
1261  return false;
1262  }
1263  else
1265  scan->xs_snapshot);
1266 
1267  _bt_initialize_more_data(so, dir);
1268 
1269  /* position to the precise item on the page */
1270  offnum = _bt_binsrch(rel, &inskey, buf);
1271 
1272  /*
1273  * If nextkey = false, we are positioned at the first item >= scan key, or
1274  * possibly at the end of a page on which all the existing items are less
1275  * than the scan key and we know that everything on later pages is greater
1276  * than or equal to scan key.
1277  *
1278  * If nextkey = true, we are positioned at the first item > scan key, or
1279  * possibly at the end of a page on which all the existing items are less
1280  * than or equal to the scan key and we know that everything on later
1281  * pages is greater than scan key.
1282  *
1283  * The actually desired starting point is either this item or the prior
1284  * one, or in the end-of-page case it's the first item on the next page or
1285  * the last item on this page. Adjust the starting offset if needed. (If
1286  * this results in an offset before the first item or after the last one,
1287  * _bt_readpage will report no items found, and then we'll step to the
1288  * next page as needed.)
1289  */
1290  if (goback)
1291  offnum = OffsetNumberPrev(offnum);
1292 
1293  /* remember which buffer we have pinned, if any */
1295  so->currPos.buf = buf;
1296 
1297  /*
1298  * Now load data from the first page of the scan.
1299  */
1300  if (!_bt_readpage(scan, dir, offnum))
1301  {
1302  /*
1303  * There's no actually-matching data on this page. Try to advance to
1304  * the next page. Return false if there's no matching data at all.
1305  */
1307  if (!_bt_steppage(scan, dir))
1308  return false;
1309  }
1310  else
1311  {
1312  /* Drop the lock, and maybe the pin, on the current page */
1314  }
1315 
1316 readcomplete:
1317  /* OK, itemIndex says what to return */
1318  currItem = &so->currPos.items[so->currPos.itemIndex];
1319  scan->xs_heaptid = currItem->heapTid;
1320  if (scan->xs_want_itup)
1321  scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
1322 
1323  return true;
1324 }
#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
#define SK_ROW_MEMBER
Definition: skey.h:118
void _bt_freestack(BTStack stack)
Definition: nbtutils.c:163
void PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
Definition: predicate.c:2526
#define BTORDER_PROC
Definition: nbtree.h:392
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:794
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
IndexTuple xs_itup
Definition: relscan.h:127
BTScanPosItem items[MaxIndexTuplesPerPage]
Definition: nbtree.h:578
void ScanKeyEntryInitializeWithInfo(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, FmgrInfo *finfo, Datum argument)
Definition: scankey.c:101
void _bt_parallel_done(IndexScanDesc scan)
Definition: nbtree.c:722
static bool _bt_parallel_readpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
Definition: nbtsearch.c:1908
static void _bt_drop_lock_and_maybe_pin(IndexScanDesc scan, BTScanPos sp)
Definition: nbtsearch.c:57
int itemIndex
Definition: nbtree.h:576
void PredicateLockRelation(Relation relation, Snapshot snapshot)
Definition: predicate.c:2503
char * currTuples
Definition: nbtree.h:650
regproc RegProcedure
Definition: c.h:505
#define P_NONE
Definition: nbtree.h:181
struct cursor * cur
Definition: ecpg.c:28
uint16 StrategyNumber
Definition: stratnum.h:22
#define BTScanPosIsValid(scanpos)
Definition: nbtree.h:600
struct SnapshotData * xs_snapshot
Definition: relscan.h:104
uint32 BlockNumber
Definition: block.h:31
static OffsetNumber _bt_binsrch(Relation rel, BTScanInsert key, Buffer buf)
Definition: nbtsearch.c:339
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
Relation indexRelation
Definition: relscan.h:103
uint16 OffsetNumber
Definition: off.h:24
#define SK_ROW_END
Definition: skey.h:119
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
#define BT_READ
Definition: nbtree.h:402
#define ERROR
Definition: elog.h:43
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:667
StrategyNumber sk_strategy
Definition: skey.h:68
ItemPointerData xs_heaptid
Definition: relscan.h:132
static char * buf
Definition: pg_test_fsync.c:68
void ScanKeyEntryInitialize(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, RegProcedure procedure, Datum argument)
Definition: scankey.c:32
ScanKeyData * ScanKey
Definition: skey.h:75
static bool _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:2141
#define RegProcedureIsValid(p)
Definition: c.h:640
IndexTupleData * IndexTuple
Definition: itup.h:53
#define RelationGetRelationName(relation)
Definition: rel.h:450
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:1373
#define SK_SEARCHNOTNULL
Definition: skey.h:122
bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno)
Definition: nbtree.c:641
Oid * rd_opfamily
Definition: rel.h:158
#define SK_ISNULL
Definition: skey.h:115
#define SK_ROW_HEADER
Definition: skey.h:117
void _bt_preprocess_keys(IndexScanDesc scan)
Definition: nbtutils.c:744
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:678
static void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir)
Definition: nbtsearch.c:2231
struct ParallelIndexScanDescData * parallel_scan
Definition: relscan.h:151
uintptr_t Datum
Definition: postgres.h:367
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
bool _bt_heapkeyspace(Relation rel)
Definition: nbtpage.c:636
#define InvalidOid
Definition: postgres_ext.h:36
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:732
#define SK_BT_DESC
Definition: nbtree.h:677
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
#define BTScanPosInvalidate(scanpos)
Definition: nbtree.h:606
#define INDEX_MAX_KEYS
#define InvalidBlockNumber
Definition: block.h:33
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
int numberOfKeys
Definition: nbtree.h:629
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define DatumGetPointer(X)
Definition: postgres.h:549
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
BTScanPosData currPos
Definition: nbtree.h:663
ScanKey keyData
Definition: nbtree.h:630
Oid sk_collation
Definition: skey.h:70
#define elog(elevel,...)
Definition: elog.h:226
int i
static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:1623
Buffer buf
Definition: nbtree.h:546
Oid * rd_opcintype
Definition: rel.h:159
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define BTLessStrategyNumber
Definition: stratnum.h:29
int Buffer
Definition: buf.h:23
Datum sk_argument
Definition: skey.h:72
static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
Definition: nbtsearch.c:1396
int16 AttrNumber
Definition: attnum.h:21
BTStack _bt_search(Relation rel, BTScanInsert key, Buffer *bufP, int access, Snapshot snapshot)
Definition: nbtsearch.c:92
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
AttrNumber sk_attno
Definition: skey.h:67

◆ _bt_get_endpoint()

Buffer _bt_get_endpoint ( Relation  rel,
uint32  level,
bool  rightmost,
Snapshot  snapshot 
)

Definition at line 2057 of file nbtsearch.c.

References _bt_getroot(), _bt_gettrueroot(), _bt_relandgetbuf(), BT_READ, BTPageOpaqueData::btpo, BTPageOpaqueData::btpo_next, BTreeInnerTupleGetDownLink, buf, BufferGetPage, BufferIsValid, elog, ereport, errcode(), errmsg_internal(), ERROR, InvalidBuffer, BTPageOpaqueData::level, P_FIRSTDATAKEY, P_IGNORE, P_NONE, P_RIGHTMOST, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetSpecialPointer, RelationGetRelationName, and TestForOldSnapshot().

Referenced by _bt_endpoint(), and _bt_insert_parent().

2059 {
2060  Buffer buf;
2061  Page page;
2062  BTPageOpaque opaque;
2063  OffsetNumber offnum;
2064  BlockNumber blkno;
2065  IndexTuple itup;
2066 
2067  /*
2068  * If we are looking for a leaf page, okay to descend from fast root;
2069  * otherwise better descend from true root. (There is no point in being
2070  * smarter about intermediate levels.)
2071  */
2072  if (level == 0)
2073  buf = _bt_getroot(rel, BT_READ);
2074  else
2075  buf = _bt_gettrueroot(rel);
2076 
2077  if (!BufferIsValid(buf))
2078  return InvalidBuffer;
2079 
2080  page = BufferGetPage(buf);
2081  TestForOldSnapshot(snapshot, rel, page);
2082  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2083 
2084  for (;;)
2085  {
2086  /*
2087  * If we landed on a deleted page, step right to find a live page
2088  * (there must be one). Also, if we want the rightmost page, step
2089  * right if needed to get to it (this could happen if the page split
2090  * since we obtained a pointer to it).
2091  */
2092  while (P_IGNORE(opaque) ||
2093  (rightmost && !P_RIGHTMOST(opaque)))
2094  {
2095  blkno = opaque->btpo_next;
2096  if (blkno == P_NONE)
2097  elog(ERROR, "fell off the end of index \"%s\"",
2099  buf = _bt_relandgetbuf(rel, buf, blkno, BT_READ);
2100  page = BufferGetPage(buf);
2101  TestForOldSnapshot(snapshot, rel, page);
2102  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2103  }
2104 
2105  /* Done? */
2106  if (opaque->btpo.level == level)
2107  break;
2108  if (opaque->btpo.level < level)
2109  ereport(ERROR,
2110  (errcode(ERRCODE_INDEX_CORRUPTED),
2111  errmsg_internal("btree level %u not found in index \"%s\"",
2112  level, RelationGetRelationName(rel))));
2113 
2114  /* Descend to leftmost or rightmost child page */
2115  if (rightmost)
2116  offnum = PageGetMaxOffsetNumber(page);
2117  else
2118  offnum = P_FIRSTDATAKEY(opaque);
2119 
2120  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
2121  blkno = BTreeInnerTupleGetDownLink(itup);
2122 
2123  buf = _bt_relandgetbuf(rel, buf, blkno, BT_READ);
2124  page = BufferGetPage(buf);
2125  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2126  }
2127 
2128  return buf;
2129 }
BlockNumber btpo_next
Definition: nbtree.h:58
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:264
#define P_IGNORE(opaque)
Definition: nbtree.h:194
#define BTreeInnerTupleGetDownLink(itup)
Definition: nbtree.h:301
Buffer _bt_relandgetbuf(Relation rel, Buffer obuf, BlockNumber blkno, int access)
Definition: nbtpage.c:893
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
union BTPageOpaqueData::@46 btpo
#define P_NONE
Definition: nbtree.h:181
#define InvalidBuffer
Definition: buf.h:25
int errcode(int sqlerrcode)
Definition: elog.c:570
uint32 BlockNumber
Definition: block.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
uint16 OffsetNumber
Definition: off.h:24
#define BT_READ
Definition: nbtree.h:402
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
IndexTupleData * IndexTuple
Definition: itup.h:53
#define RelationGetRelationName(relation)
Definition: rel.h:450
Buffer _bt_getroot(Relation rel, int access)
Definition: nbtpage.c:255
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
uint32 level
Definition: nbtree.h:61
Buffer _bt_gettrueroot(Relation rel)
Definition: nbtpage.c:488
int errmsg_internal(const char *fmt,...)
Definition: elog.c:814
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define elog(elevel,...)
Definition: elog.h:226
int Buffer
Definition: buf.h:23
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:188
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ _bt_initialize_more_data()

static void _bt_initialize_more_data ( BTScanOpaque  so,
ScanDirection  dir 
)
inlinestatic

Definition at line 2231 of file nbtsearch.c.

References BTScanOpaqueData::currPos, BTScanOpaqueData::markItemIndex, BTScanPosData::moreLeft, BTScanPosData::moreRight, BTScanOpaqueData::numKilled, and ScanDirectionIsForward.

Referenced by _bt_endpoint(), _bt_first(), and _bt_parallel_readpage().

2232 {
2233  /* initialize moreLeft/moreRight appropriately for scan direction */
2234  if (ScanDirectionIsForward(dir))
2235  {
2236  so->currPos.moreLeft = false;
2237  so->currPos.moreRight = true;
2238  }
2239  else
2240  {
2241  so->currPos.moreLeft = true;
2242  so->currPos.moreRight = false;
2243  }
2244  so->numKilled = 0; /* just paranoia */
2245  so->markItemIndex = -1; /* ditto */
2246 }
bool moreRight
Definition: nbtree.h:559
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
bool moreLeft
Definition: nbtree.h:558
int markItemIndex
Definition: nbtree.h:660
BTScanPosData currPos
Definition: nbtree.h:663

◆ _bt_moveright()

Buffer _bt_moveright ( Relation  rel,
BTScanInsert  key,
Buffer  buf,
bool  forupdate,
BTStack  stack,
int  access,
Snapshot  snapshot 
)

Definition at line 237 of file nbtsearch.c.

References _bt_compare(), _bt_finish_split(), _bt_getbuf(), _bt_relandgetbuf(), _bt_relbuf(), BT_READ, BT_WRITE, BTPageOpaqueData::btpo_next, buf, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, elog, ERROR, LockBuffer(), BTScanInsertData::nextkey, P_HIKEY, P_IGNORE, P_INCOMPLETE_SPLIT, P_RIGHTMOST, PageGetSpecialPointer, RelationGetRelationName, and TestForOldSnapshot().

Referenced by _bt_search().

244 {
245  Page page;
246  BTPageOpaque opaque;
247  int32 cmpval;
248 
249  /*
250  * When nextkey = false (normal case): if the scan key that brought us to
251  * this page is > the high key stored on the page, then the page has split
252  * and we need to move right. (pg_upgrade'd !heapkeyspace indexes could
253  * have some duplicates to the right as well as the left, but that's
254  * something that's only ever dealt with on the leaf level, after
255  * _bt_search has found an initial leaf page.)
256  *
257  * When nextkey = true: move right if the scan key is >= page's high key.
258  * (Note that key.scantid cannot be set in this case.)
259  *
260  * The page could even have split more than once, so scan as far as
261  * needed.
262  *
263  * We also have to move right if we followed a link that brought us to a
264  * dead page.
265  */
266  cmpval = key->nextkey ? 0 : 1;
267 
268  for (;;)
269  {
270  page = BufferGetPage(buf);
271  TestForOldSnapshot(snapshot, rel, page);
272  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
273 
274  if (P_RIGHTMOST(opaque))
275  break;
276 
277  /*
278  * Finish any incomplete splits we encounter along the way.
279  */
280  if (forupdate && P_INCOMPLETE_SPLIT(opaque))
281  {
283 
284  /* upgrade our lock if necessary */
285  if (access == BT_READ)
286  {
289  }
290 
291  if (P_INCOMPLETE_SPLIT(opaque))
292  _bt_finish_split(rel, buf, stack);
293  else
294  _bt_relbuf(rel, buf);
295 
296  /* re-acquire the lock in the right mode, and re-check */
297  buf = _bt_getbuf(rel, blkno, access);
298  continue;
299  }
300 
301  if (P_IGNORE(opaque) || _bt_compare(rel, key, page, P_HIKEY) >= cmpval)
302  {
303  /* step right one page */
304  buf = _bt_relandgetbuf(rel, buf, opaque->btpo_next, access);
305  continue;
306  }
307  else
308  break;
309  }
310 
311  if (P_IGNORE(opaque))
312  elog(ERROR, "fell off the end of index \"%s\"",
314 
315  return buf;
316 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
BlockNumber btpo_next
Definition: nbtree.h:58
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:264
#define P_IGNORE(opaque)
Definition: nbtree.h:194
Buffer _bt_relandgetbuf(Relation rel, Buffer obuf, BlockNumber blkno, int access)
Definition: nbtpage.c:893
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
Definition: nbtpage.c:757
uint32 BlockNumber
Definition: block.h:31
#define P_INCOMPLETE_SPLIT(opaque)
Definition: nbtree.h:196
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
signed int int32
Definition: c.h:346
#define BT_READ
Definition: nbtree.h:402
void _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack)
Definition: nbtinsert.c:1856
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define RelationGetRelationName(relation)
Definition: rel.h:450
int32 _bt_compare(Relation rel, BTScanInsert key, Page page, OffsetNumber offnum)
Definition: nbtsearch.c:552
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:912
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define P_HIKEY
Definition: nbtree.h:217
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
#define elog(elevel,...)
Definition: elog.h:226
#define BT_WRITE
Definition: nbtree.h:403
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:188
Pointer Page
Definition: bufpage.h:78

◆ _bt_next()

bool _bt_next ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 1341 of file nbtsearch.c.

References _bt_steppage(), BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, BTScanPosData::firstItem, BTScanPosData::itemIndex, BTScanPosData::items, BTScanPosData::lastItem, IndexScanDescData::opaque, ScanDirectionIsForward, IndexScanDescData::xs_heaptid, IndexScanDescData::xs_itup, and IndexScanDescData::xs_want_itup.

Referenced by btgetbitmap(), and btgettuple().

1342 {
1343  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1344  BTScanPosItem *currItem;
1345 
1346  /*
1347  * Advance to next tuple on current page; or if there's no more, try to
1348  * step to the next page with data.
1349  */
1350  if (ScanDirectionIsForward(dir))
1351  {
1352  if (++so->currPos.itemIndex > so->currPos.lastItem)
1353  {
1354  if (!_bt_steppage(scan, dir))
1355  return false;
1356  }
1357  }
1358  else
1359  {
1360  if (--so->currPos.itemIndex < so->currPos.firstItem)
1361  {
1362  if (!_bt_steppage(scan, dir))
1363  return false;
1364  }
1365  }
1366 
1367  /* OK, itemIndex says what to return */
1368  currItem = &so->currPos.items[so->currPos.itemIndex];
1369  scan->xs_heaptid = currItem->heapTid;
1370  if (scan->xs_want_itup)
1371  scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
1372 
1373  return true;
1374 }
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
IndexTuple xs_itup
Definition: relscan.h:127
BTScanPosItem items[MaxIndexTuplesPerPage]
Definition: nbtree.h:578
int itemIndex
Definition: nbtree.h:576
char * currTuples
Definition: nbtree.h:650
int lastItem
Definition: nbtree.h:575
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:667
ItemPointerData xs_heaptid
Definition: relscan.h:132
IndexTupleData * IndexTuple
Definition: itup.h:53
int firstItem
Definition: nbtree.h:574
BTScanPosData currPos
Definition: nbtree.h:663
static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir)
Definition: nbtsearch.c:1623

◆ _bt_parallel_readpage()

static bool _bt_parallel_readpage ( IndexScanDesc  scan,
BlockNumber  blkno,
ScanDirection  dir 
)
static

Definition at line 1908 of file nbtsearch.c.

References _bt_drop_lock_and_maybe_pin(), _bt_initialize_more_data(), _bt_readnextpage(), BTScanOpaqueData::currPos, and IndexScanDescData::opaque.

Referenced by _bt_first().

1909 {
1910  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1911 
1912  _bt_initialize_more_data(so, dir);
1913 
1914  if (!_bt_readnextpage(scan, blkno, dir))
1915  return false;
1916 
1917  /* Drop the lock, and maybe the pin, on the current page */
1919 
1920  return true;
1921 }
static void _bt_drop_lock_and_maybe_pin(IndexScanDesc scan, BTScanPos sp)
Definition: nbtsearch.c:57
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:667
static void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir)
Definition: nbtsearch.c:2231
static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
Definition: nbtsearch.c:1730
BTScanPosData currPos
Definition: nbtree.h:663

◆ _bt_readnextpage()

static bool _bt_readnextpage ( IndexScanDesc  scan,
BlockNumber  blkno,
ScanDirection  dir 
)
static

Definition at line 1730 of file nbtsearch.c.

References _bt_getbuf(), _bt_parallel_done(), _bt_parallel_release(), _bt_parallel_seize(), _bt_readpage(), _bt_relbuf(), _bt_walk_left(), BT_READ, BTScanPosInvalidate, BTScanPosIsPinned, BTScanPosUnpinIfPinned, BTScanPosData::buf, BufferGetBlockNumber(), BufferGetPage, CHECK_FOR_INTERRUPTS, BTScanPosData::currPage, BTScanOpaqueData::currPos, IndexScanDescData::indexRelation, InvalidBuffer, LockBuffer(), BTScanPosData::moreLeft, BTScanPosData::moreRight, IndexScanDescData::opaque, P_FIRSTDATAKEY, P_IGNORE, P_NONE, PageGetMaxOffsetNumber, PageGetSpecialPointer, IndexScanDescData::parallel_scan, PredicateLockPage(), ScanDirectionIsForward, status(), TestForOldSnapshot(), and IndexScanDescData::xs_snapshot.

Referenced by _bt_parallel_readpage(), and _bt_steppage().

1731 {
1732  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1733  Relation rel;
1734  Page page;
1735  BTPageOpaque opaque;
1736  bool status = true;
1737 
1738  rel = scan->indexRelation;
1739 
1740  if (ScanDirectionIsForward(dir))
1741  {
1742  for (;;)
1743  {
1744  /*
1745  * if we're at end of scan, give up and mark parallel scan as
1746  * done, so that all the workers can finish their scan
1747  */
1748  if (blkno == P_NONE || !so->currPos.moreRight)
1749  {
1750  _bt_parallel_done(scan);
1752  return false;
1753  }
1754  /* check for interrupts while we're not holding any buffer lock */
1756  /* step right one page */
1757  so->currPos.buf = _bt_getbuf(rel, blkno, BT_READ);
1758  page = BufferGetPage(so->currPos.buf);
1759  TestForOldSnapshot(scan->xs_snapshot, rel, page);
1760  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1761  /* check for deleted page */
1762  if (!P_IGNORE(opaque))
1763  {
1764  PredicateLockPage(rel, blkno, scan->xs_snapshot);
1765  /* see if there are any matches on this page */
1766  /* note that this will clear moreRight if we can stop */
1767  if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque)))
1768  break;
1769  }
1770  else if (scan->parallel_scan != NULL)
1771  {
1772  /* allow next page be processed by parallel worker */
1773  _bt_parallel_release(scan, opaque->btpo_next);
1774  }
1775 
1776  /* nope, keep going */
1777  if (scan->parallel_scan != NULL)
1778  {
1779  _bt_relbuf(rel, so->currPos.buf);
1780  status = _bt_parallel_seize(scan, &blkno);
1781  if (!status)
1782  {
1784  return false;
1785  }
1786  }
1787  else
1788  {
1789  blkno = opaque->btpo_next;
1790  _bt_relbuf(rel, so->currPos.buf);
1791  }
1792  }
1793  }
1794  else
1795  {
1796  /*
1797  * Should only happen in parallel cases, when some other backend
1798  * advanced the scan.
1799  */
1800  if (so->currPos.currPage != blkno)
1801  {
1803  so->currPos.currPage = blkno;
1804  }
1805 
1806  /*
1807  * Walk left to the next page with data. This is much more complex
1808  * than the walk-right case because of the possibility that the page
1809  * to our left splits while we are in flight to it, plus the
1810  * possibility that the page we were on gets deleted after we leave
1811  * it. See nbtree/README for details.
1812  *
1813  * It might be possible to rearrange this code to have less overhead
1814  * in pinning and locking, but that would require capturing the left
1815  * pointer when the page is initially read, and using it here, along
1816  * with big changes to _bt_walk_left() and the code below. It is not
1817  * clear whether this would be a win, since if the page immediately to
1818  * the left splits after we read this page and before we step left, we
1819  * would need to visit more pages than with the current code.
1820  *
1821  * Note that if we change the code so that we drop the pin for a scan
1822  * which uses a non-MVCC snapshot, we will need to modify the code for
1823  * walking left, to allow for the possibility that a referenced page
1824  * has been deleted. As long as the buffer is pinned or the snapshot
1825  * is MVCC the page cannot move past the half-dead state to fully
1826  * deleted.
1827  */
1828  if (BTScanPosIsPinned(so->currPos))
1829  LockBuffer(so->currPos.buf, BT_READ);
1830  else
1831  so->currPos.buf = _bt_getbuf(rel, so->currPos.currPage, BT_READ);
1832 
1833  for (;;)
1834  {
1835  /* Done if we know there are no matching keys to the left */
1836  if (!so->currPos.moreLeft)
1837  {
1838  _bt_relbuf(rel, so->currPos.buf);
1839  _bt_parallel_done(scan);
1841  return false;
1842  }
1843 
1844  /* Step to next physical page */
1845  so->currPos.buf = _bt_walk_left(rel, so->currPos.buf,
1846  scan->xs_snapshot);
1847 
1848  /* if we're physically at end of index, return failure */
1849  if (so->currPos.buf == InvalidBuffer)
1850  {
1851  _bt_parallel_done(scan);
1853  return false;
1854  }
1855 
1856  /*
1857  * Okay, we managed to move left to a non-deleted page. Done if
1858  * it's not half-dead and contains matching tuples. Else loop back
1859  * and do it all again.
1860  */
1861  page = BufferGetPage(so->currPos.buf);
1862  TestForOldSnapshot(scan->xs_snapshot, rel, page);
1863  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1864  if (!P_IGNORE(opaque))
1865  {
1867  /* see if there are any matches on this page */
1868  /* note that this will clear moreLeft if we can stop */
1869  if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page)))
1870  break;
1871  }
1872  else if (scan->parallel_scan != NULL)
1873  {
1874  /* allow next page be processed by parallel worker */
1876  }
1877 
1878  /*
1879  * For parallel scans, get the last page scanned as it is quite
1880  * possible that by the time we try to seize the scan, some other
1881  * worker has already advanced the scan to a different page. We
1882  * must continue based on the latest page scanned by any worker.
1883  */
1884  if (scan->parallel_scan != NULL)
1885  {
1886  _bt_relbuf(rel, so->currPos.buf);
1887  status = _bt_parallel_seize(scan, &blkno);
1888  if (!status)
1889  {
1891  return false;
1892  }
1893  so->currPos.buf = _bt_getbuf(rel, blkno, BT_READ);
1894  }
1895  }
1896  }
1897 
1898  return true;
1899 }
void PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
Definition: predicate.c:2526
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:264
#define P_IGNORE(opaque)
Definition: nbtree.h:194
bool moreRight
Definition: nbtree.h:559
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
void _bt_parallel_done(IndexScanDesc scan)
Definition: nbtree.c:722
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
Definition: nbtpage.c:757
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
#define P_NONE
Definition: nbtree.h:181
#define InvalidBuffer
Definition: buf.h:25
BlockNumber currPage
Definition: nbtree.h:549
struct SnapshotData * xs_snapshot
Definition: relscan.h:104
bool moreLeft
Definition: nbtree.h:558
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
Relation indexRelation
Definition: relscan.h:103
#define BT_READ
Definition: nbtree.h:402
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:667
#define BTScanPosIsPinned(scanpos)
Definition: nbtree.h:583
bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno)
Definition: nbtree.c:641
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
struct ParallelIndexScanDescData * parallel_scan
Definition: relscan.h:151
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:912
#define BTScanPosInvalidate(scanpos)
Definition: nbtree.h:606
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page)
Definition: nbtree.c:699
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
BTScanPosData currPos
Definition: nbtree.h:663
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Buffer buf
Definition: nbtree.h:546
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
Definition: nbtsearch.c:1396
#define BTScanPosUnpinIfPinned(scanpos)
Definition: nbtree.h:594
Pointer Page
Definition: bufpage.h:78
static Buffer _bt_walk_left(Relation rel, Buffer buf, Snapshot snapshot)
Definition: nbtsearch.c:1938

◆ _bt_readpage()

static bool _bt_readpage ( IndexScanDesc  scan,
ScanDirection  dir,
OffsetNumber  offnum 
)
static

Definition at line 1396 of file nbtsearch.c.

References _bt_checkkeys(), _bt_parallel_release(), _bt_saveitem(), Assert, BTreeTupleGetNAtts, BTScanPosIsPinned, BTScanPosData::buf, BufferGetBlockNumber(), BufferGetLSNAtomic(), BufferGetPage, BufferIsValid, BTScanPosData::currPage, BTScanOpaqueData::currPos, BTScanPosData::firstItem, IndexScanDescData::ignore_killed_tuples, IndexScanDescData::indexRelation, IndexRelationGetNumberOfAttributes, ItemIdIsDead, BTScanPosData::itemIndex, BTScanPosData::lastItem, BTScanPosData::lsn, Max, MaxIndexTuplesPerPage, Min, BTScanPosData::moreLeft, BTScanPosData::moreRight, BTScanPosData::nextPage, BTScanPosData::nextTupleOffset, OffsetNumberNext, OffsetNumberPrev, IndexScanDescData::opaque, P_FIRSTDATAKEY, P_HIKEY, P_RIGHTMOST, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetSpecialPointer, IndexScanDescData::parallel_scan, and ScanDirectionIsForward.

Referenced by _bt_endpoint(), _bt_first(), and _bt_readnextpage().

1397 {
1398  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1399  Page page;
1400  BTPageOpaque opaque;
1401  OffsetNumber minoff;
1402  OffsetNumber maxoff;
1403  int itemIndex;
1404  bool continuescan;
1405  int indnatts;
1406 
1407  /*
1408  * We must have the buffer pinned and locked, but the usual macro can't be
1409  * used here; this function is what makes it good for currPos.
1410  */
1412 
1413  page = BufferGetPage(so->currPos.buf);
1414  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1415 
1416  /* allow next page be processed by parallel worker */
1417  if (scan->parallel_scan)
1418  {
1419  if (ScanDirectionIsForward(dir))
1420  _bt_parallel_release(scan, opaque->btpo_next);
1421  else
1423  }
1424 
1425  continuescan = true; /* default assumption */
1427  minoff = P_FIRSTDATAKEY(opaque);
1428  maxoff = PageGetMaxOffsetNumber(page);
1429 
1430  /*
1431  * We note the buffer's block number so that we can release the pin later.
1432  * This allows us to re-read the buffer if it is needed again for hinting.
1433  */
1435 
1436  /*
1437  * We save the LSN of the page as we read it, so that we know whether it
1438  * safe to apply LP_DEAD hints to the page later. This allows us to drop
1439  * the pin for MVCC scans, which allows vacuum to avoid blocking.
1440  */
1442 
1443  /*
1444  * we must save the page's right-link while scanning it; this tells us
1445  * where to step right to after we're done with these items. There is no
1446  * corresponding need for the left-link, since splits always go right.
1447  */
1448  so->currPos.nextPage = opaque->btpo_next;
1449 
1450  /* initialize tuple workspace to empty */
1451  so->currPos.nextTupleOffset = 0;
1452 
1453  /*
1454  * Now that the current page has been made consistent, the macro should be
1455  * good.
1456  */
1458 
1459  if (ScanDirectionIsForward(dir))
1460  {
1461  /* load items[] in ascending order */
1462  itemIndex = 0;
1463 
1464  offnum = Max(offnum, minoff);
1465 
1466  while (offnum <= maxoff)
1467  {
1468  ItemId iid = PageGetItemId(page, offnum);
1469  IndexTuple itup;
1470 
1471  /*
1472  * If the scan specifies not to return killed tuples, then we
1473  * treat a killed tuple as not passing the qual
1474  */
1475  if (scan->ignore_killed_tuples && ItemIdIsDead(iid))
1476  {
1477  offnum = OffsetNumberNext(offnum);
1478  continue;
1479  }
1480 
1481  itup = (IndexTuple) PageGetItem(page, iid);
1482 
1483  if (_bt_checkkeys(scan, itup, indnatts, dir, &continuescan))
1484  {
1485  /* tuple passes all scan key conditions, so remember it */
1486  _bt_saveitem(so, itemIndex, offnum, itup);
1487  itemIndex++;
1488  }
1489  /* When !continuescan, there can't be any more matches, so stop */
1490  if (!continuescan)
1491  break;
1492 
1493  offnum = OffsetNumberNext(offnum);
1494  }
1495 
1496  /*
1497  * We don't need to visit page to the right when the high key
1498  * indicates that no more matches will be found there.
1499  *
1500  * Checking the high key like this works out more often than you might
1501  * think. Leaf page splits pick a split point between the two most
1502  * dissimilar tuples (this is weighed against the need to evenly share
1503  * free space). Leaf pages with high key attribute values that can
1504  * only appear on non-pivot tuples on the right sibling page are
1505  * common.
1506  */
1507  if (continuescan && !P_RIGHTMOST(opaque))
1508  {
1509  ItemId iid = PageGetItemId(page, P_HIKEY);
1510  IndexTuple itup = (IndexTuple) PageGetItem(page, iid);
1511  int truncatt;
1512 
1513  truncatt = BTreeTupleGetNAtts(itup, scan->indexRelation);
1514  _bt_checkkeys(scan, itup, truncatt, dir, &continuescan);
1515  }
1516 
1517  if (!continuescan)
1518  so->currPos.moreRight = false;
1519 
1520  Assert(itemIndex <= MaxIndexTuplesPerPage);
1521  so->currPos.firstItem = 0;
1522  so->currPos.lastItem = itemIndex - 1;
1523  so->currPos.itemIndex = 0;
1524  }
1525  else
1526  {
1527  /* load items[] in descending order */
1528  itemIndex = MaxIndexTuplesPerPage;
1529 
1530  offnum = Min(offnum, maxoff);
1531 
1532  while (offnum >= minoff)
1533  {
1534  ItemId iid = PageGetItemId(page, offnum);
1535  IndexTuple itup;
1536  bool tuple_alive;
1537  bool passes_quals;
1538 
1539  /*
1540  * If the scan specifies not to return killed tuples, then we
1541  * treat a killed tuple as not passing the qual. Most of the
1542  * time, it's a win to not bother examining the tuple's index
1543  * keys, but just skip to the next tuple (previous, actually,
1544  * since we're scanning backwards). However, if this is the first
1545  * tuple on the page, we do check the index keys, to prevent
1546  * uselessly advancing to the page to the left. This is similar
1547  * to the high key optimization used by forward scans.
1548  */
1549  if (scan->ignore_killed_tuples && ItemIdIsDead(iid))
1550  {
1551  Assert(offnum >= P_FIRSTDATAKEY(opaque));
1552  if (offnum > P_FIRSTDATAKEY(opaque))
1553  {
1554  offnum = OffsetNumberPrev(offnum);
1555  continue;
1556  }
1557 
1558  tuple_alive = false;
1559  }
1560  else
1561  tuple_alive = true;
1562 
1563  itup = (IndexTuple) PageGetItem(page, iid);
1564 
1565  passes_quals = _bt_checkkeys(scan, itup, indnatts, dir,
1566  &continuescan);
1567  if (passes_quals && tuple_alive)
1568  {
1569  /* tuple passes all scan key conditions, so remember it */
1570  itemIndex--;
1571  _bt_saveitem(so, itemIndex, offnum, itup);
1572  }
1573  if (!continuescan)
1574  {
1575  /* there can't be any more matches, so stop */
1576  so->currPos.moreLeft = false;
1577  break;
1578  }
1579 
1580  offnum = OffsetNumberPrev(offnum);
1581  }
1582 
1583  Assert(itemIndex >= 0);
1584  so->currPos.firstItem = itemIndex;
1587  }
1588 
1589  return (so->currPos.firstItem <= so->currPos.lastItem);
1590 }
bool moreRight
Definition: nbtree.h:559
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
int itemIndex
Definition: nbtree.h:576
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:219
#define Min(x, y)
Definition: c.h:904
BlockNumber currPage
Definition: nbtree.h:549
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define BTreeTupleGetNAtts(itup, rel)
Definition: nbtree.h:327
bool moreLeft
Definition: nbtree.h:558
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
bool ignore_killed_tuples
Definition: relscan.h:114
Relation indexRelation
Definition: relscan.h:103
uint16 OffsetNumber
Definition: off.h:24
int nextTupleOffset
Definition: nbtree.h:565
int lastItem
Definition: nbtree.h:575
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:667
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2876
#define BTScanPosIsPinned(scanpos)
Definition: nbtree.h:583
IndexTupleData * IndexTuple
Definition: itup.h:53
static void _bt_saveitem(BTScanOpaque so, int itemIndex, OffsetNumber offnum, IndexTuple itup)
Definition: nbtsearch.c:1594
#define IndexRelationGetNumberOfAttributes(relation)
Definition: rel.h:428
int firstItem
Definition: nbtree.h:574
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
struct ParallelIndexScanDescData * parallel_scan
Definition: relscan.h:151
bool _bt_checkkeys(IndexScanDesc scan, IndexTuple tuple, int tupnatts, ScanDirection dir, bool *continuescan)
Definition: nbtutils.c:1357
#define Max(x, y)
Definition: c.h:898
#define Assert(condition)
Definition: c.h:732
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page)
Definition: nbtree.c:699
#define P_HIKEY
Definition: nbtree.h:217
BlockNumber nextPage
Definition: nbtree.h:550
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
BTScanPosData currPos
Definition: nbtree.h:663
#define MaxIndexTuplesPerPage
Definition: itup.h:145
XLogRecPtr lsn
Definition: nbtree.h:548
Buffer buf
Definition: nbtree.h:546
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:188
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ _bt_saveitem()

static void _bt_saveitem ( BTScanOpaque  so,
int  itemIndex,
OffsetNumber  offnum,
IndexTuple  itup 
)
static

Definition at line 1594 of file nbtsearch.c.

References BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, BTScanPosItem::heapTid, BTScanPosItem::indexOffset, IndexTupleSize, BTScanPosData::items, MAXALIGN, BTScanPosData::nextTupleOffset, IndexTupleData::t_tid, and BTScanPosItem::tupleOffset.

Referenced by _bt_readpage().

1596 {
1597  BTScanPosItem *currItem = &so->currPos.items[itemIndex];
1598 
1599  currItem->heapTid = itup->t_tid;
1600  currItem->indexOffset = offnum;
1601  if (so->currTuples)
1602  {
1603  Size itupsz = IndexTupleSize(itup);
1604 
1605  currItem->tupleOffset = so->currPos.nextTupleOffset;
1606  memcpy(so->currTuples + so->currPos.nextTupleOffset, itup, itupsz);
1607  so->currPos.nextTupleOffset += MAXALIGN(itupsz);
1608  }
1609 }
BTScanPosItem items[MaxIndexTuplesPerPage]
Definition: nbtree.h:578
char * currTuples
Definition: nbtree.h:650
ItemPointerData t_tid
Definition: itup.h:37
OffsetNumber indexOffset
Definition: nbtree.h:540
int nextTupleOffset
Definition: nbtree.h:565
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:685
ItemPointerData heapTid
Definition: nbtree.h:539
BTScanPosData currPos
Definition: nbtree.h:663
LocationIndex tupleOffset
Definition: nbtree.h:541
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ _bt_search()

BTStack _bt_search ( Relation  rel,
BTScanInsert  key,
Buffer bufP,
int  access,
Snapshot  snapshot 
)

Definition at line 92 of file nbtsearch.c.

References _bt_binsrch(), _bt_getroot(), _bt_moveright(), _bt_relandgetbuf(), BT_READ, BT_WRITE, BTPageOpaqueData::btpo, BTreeInnerTupleGetDownLink, BTStackData::bts_blkno, BTStackData::bts_offset, BTStackData::bts_parent, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, BufferIsValid, BTPageOpaqueData::level, LockBuffer(), P_ISLEAF, PageGetItem, PageGetItemId, PageGetSpecialPointer, and palloc().

Referenced by _bt_doinsert(), _bt_first(), _bt_pagedel(), and bt_rootdescend().

94 {
95  BTStack stack_in = NULL;
96  int page_access = BT_READ;
97 
98  /* Get the root page to start with */
99  *bufP = _bt_getroot(rel, access);
100 
101  /* If index is empty and access = BT_READ, no root page is created. */
102  if (!BufferIsValid(*bufP))
103  return (BTStack) NULL;
104 
105  /* Loop iterates once per level descended in the tree */
106  for (;;)
107  {
108  Page page;
109  BTPageOpaque opaque;
110  OffsetNumber offnum;
111  ItemId itemid;
112  IndexTuple itup;
113  BlockNumber blkno;
114  BlockNumber par_blkno;
115  BTStack new_stack;
116 
117  /*
118  * Race -- the page we just grabbed may have split since we read its
119  * pointer in the parent (or metapage). If it has, we may need to
120  * move right to its new sibling. Do that.
121  *
122  * In write-mode, allow _bt_moveright to finish any incomplete splits
123  * along the way. Strictly speaking, we'd only need to finish an
124  * incomplete split on the leaf page we're about to insert to, not on
125  * any of the upper levels (they are taken care of in _bt_getstackbuf,
126  * if the leaf page is split and we insert to the parent page). But
127  * this is a good opportunity to finish splits of internal pages too.
128  */
129  *bufP = _bt_moveright(rel, key, *bufP, (access == BT_WRITE), stack_in,
130  page_access, snapshot);
131 
132  /* if this is a leaf page, we're done */
133  page = BufferGetPage(*bufP);
134  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
135  if (P_ISLEAF(opaque))
136  break;
137 
138  /*
139  * Find the appropriate item on the internal page, and get the child
140  * page that it points to.
141  */
142  offnum = _bt_binsrch(rel, key, *bufP);
143  itemid = PageGetItemId(page, offnum);
144  itup = (IndexTuple) PageGetItem(page, itemid);
145  blkno = BTreeInnerTupleGetDownLink(itup);
146  par_blkno = BufferGetBlockNumber(*bufP);
147 
148  /*
149  * We need to save the location of the pivot tuple we chose in the
150  * parent page on a stack. If we need to split a page, we'll use
151  * the stack to work back up to its parent page. If caller ends up
152  * splitting a page one level down, it usually ends up inserting a
153  * new pivot tuple/downlink immediately after the location recorded
154  * here.
155  */
156  new_stack = (BTStack) palloc(sizeof(BTStackData));
157  new_stack->bts_blkno = par_blkno;
158  new_stack->bts_offset = offnum;
159  new_stack->bts_parent = stack_in;
160 
161  /*
162  * Page level 1 is lowest non-leaf page level prior to leaves. So, if
163  * we're on the level 1 and asked to lock leaf page in write mode,
164  * then lock next page in write mode, because it must be a leaf.
165  */
166  if (opaque->btpo.level == 1 && access == BT_WRITE)
167  page_access = BT_WRITE;
168 
169  /* drop the read lock on the parent page, acquire one on the child */
170  *bufP = _bt_relandgetbuf(rel, *bufP, blkno, page_access);
171 
172  /* okay, all set to move down a level */
173  stack_in = new_stack;
174  }
175 
176  /*
177  * If we're asked to lock leaf in write mode, but didn't manage to, then
178  * relock. This should only happen when the root page is a leaf page (and
179  * the only page in the index other than the metapage).
180  */
181  if (access == BT_WRITE && page_access == BT_READ)
182  {
183  /* trade in our read lock for a write lock */
185  LockBuffer(*bufP, BT_WRITE);
186 
187  /*
188  * If the page was split between the time that we surrendered our read
189  * lock and acquired our write lock, then this page may no longer be
190  * the right place for the key we want to insert. In this case, we
191  * need to move right in the tree. See Lehman and Yao for an
192  * excruciatingly precise description.
193  */
194  *bufP = _bt_moveright(rel, key, *bufP, true, stack_in, BT_WRITE,
195  snapshot);
196  }
197 
198  return stack_in;
199 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
#define BTreeInnerTupleGetDownLink(itup)
Definition: nbtree.h:301
Buffer _bt_relandgetbuf(Relation rel, Buffer obuf, BlockNumber blkno, int access)
Definition: nbtpage.c:893
union BTPageOpaqueData::@46 btpo
BTStackData * BTStack
Definition: nbtree.h:419
uint32 BlockNumber
Definition: block.h:31
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
static OffsetNumber _bt_binsrch(Relation rel, BTScanInsert key, Buffer buf)
Definition: nbtsearch.c:339
uint16 OffsetNumber
Definition: off.h:24
#define BT_READ
Definition: nbtree.h:402
OffsetNumber bts_offset
Definition: nbtree.h:415
IndexTupleData * IndexTuple
Definition: itup.h:53
Buffer _bt_getroot(Relation rel, int access)
Definition: nbtpage.c:255
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
uint32 level
Definition: nbtree.h:61
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
BlockNumber bts_blkno
Definition: nbtree.h:414
Buffer _bt_moveright(Relation rel, BTScanInsert key, Buffer buf, bool forupdate, BTStack stack, int access, Snapshot snapshot)
Definition: nbtsearch.c:237
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
struct BTStackData * bts_parent
Definition: nbtree.h:416
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
void * palloc(Size size)
Definition: mcxt.c:924
#define BT_WRITE
Definition: nbtree.h:403
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define P_ISLEAF(opaque)
Definition: nbtree.h:189

◆ _bt_steppage()

static bool _bt_steppage ( IndexScanDesc  scan,
ScanDirection  dir 
)
static

Definition at line 1623 of file nbtsearch.c.

References _bt_drop_lock_and_maybe_pin(), _bt_killitems(), _bt_parallel_seize(), _bt_readnextpage(), Assert, BTScanPosInvalidate, BTScanPosIsPinned, BTScanPosIsValid, BTScanPosUnpinIfPinned, BTScanPosData::buf, BTScanPosData::currPage, BTScanOpaqueData::currPos, BTScanOpaqueData::currTuples, IncrBufferRefCount(), InvalidBlockNumber, BTScanPosData::itemIndex, BTScanPosData::lastItem, BTScanOpaqueData::markItemIndex, BTScanOpaqueData::markPos, BTScanOpaqueData::markTuples, BTScanPosData::moreLeft, BTScanPosData::moreRight, BTScanPosData::nextPage, BTScanPosData::nextTupleOffset, BTScanOpaqueData::numKilled, offsetof, IndexScanDescData::opaque, IndexScanDescData::parallel_scan, ScanDirectionIsForward, and status().

Referenced by _bt_endpoint(), _bt_first(), and _bt_next().

1624 {
1625  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1627  bool status = true;
1628 
1630 
1631  /* Before leaving current page, deal with any killed items */
1632  if (so->numKilled > 0)
1633  _bt_killitems(scan);
1634 
1635  /*
1636  * Before we modify currPos, make a copy of the page data if there was a
1637  * mark position that needs it.
1638  */
1639  if (so->markItemIndex >= 0)
1640  {
1641  /* bump pin on current buffer for assignment to mark buffer */
1642  if (BTScanPosIsPinned(so->currPos))
1644  memcpy(&so->markPos, &so->currPos,
1645  offsetof(BTScanPosData, items[1]) +
1646  so->currPos.lastItem * sizeof(BTScanPosItem));
1647  if (so->markTuples)
1648  memcpy(so->markTuples, so->currTuples,
1649  so->currPos.nextTupleOffset);
1650  so->markPos.itemIndex = so->markItemIndex;
1651  so->markItemIndex = -1;
1652  }
1653 
1654  if (ScanDirectionIsForward(dir))
1655  {
1656  /* Walk right to the next page with data */
1657  if (scan->parallel_scan != NULL)
1658  {
1659  /*
1660  * Seize the scan to get the next block number; if the scan has
1661  * ended already, bail out.
1662  */
1663  status = _bt_parallel_seize(scan, &blkno);
1664  if (!status)
1665  {
1666  /* release the previous buffer, if pinned */
1669  return false;
1670  }
1671  }
1672  else
1673  {
1674  /* Not parallel, so use the previously-saved nextPage link. */
1675  blkno = so->currPos.nextPage;
1676  }
1677 
1678  /* Remember we left a page with data */
1679  so->currPos.moreLeft = true;
1680 
1681  /* release the previous buffer, if pinned */
1683  }
1684  else
1685  {
1686  /* Remember we left a page with data */
1687  so->currPos.moreRight = true;
1688 
1689  if (scan->parallel_scan != NULL)
1690  {
1691  /*
1692  * Seize the scan to get the current block number; if the scan has
1693  * ended already, bail out.
1694  */
1695  status = _bt_parallel_seize(scan, &blkno);
1697  if (!status)
1698  {
1700  return false;
1701  }
1702  }
1703  else
1704  {
1705  /* Not parallel, so just use our own notion of the current page */
1706  blkno = so->currPos.currPage;
1707  }
1708  }
1709 
1710  if (!_bt_readnextpage(scan, blkno, dir))
1711  return false;
1712 
1713  /* Drop the lock, and maybe the pin, on the current page */
1715 
1716  return true;
1717 }
bool moreRight
Definition: nbtree.h:559
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
static void _bt_drop_lock_and_maybe_pin(IndexScanDesc scan, BTScanPos sp)
Definition: nbtsearch.c:57
int itemIndex
Definition: nbtree.h:576
char * currTuples
Definition: nbtree.h:650
BlockNumber currPage
Definition: nbtree.h:549
#define BTScanPosIsValid(scanpos)
Definition: nbtree.h:600
uint32 BlockNumber
Definition: block.h:31
bool moreLeft
Definition: nbtree.h:558
int nextTupleOffset
Definition: nbtree.h:565
int lastItem
Definition: nbtree.h:575
void _bt_killitems(IndexScanDesc scan)
Definition: nbtutils.c:1718
BTScanPosData markPos
Definition: nbtree.h:664
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:667
#define BTScanPosIsPinned(scanpos)
Definition: nbtree.h:583
char * markTuples
Definition: nbtree.h:651
bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno)
Definition: nbtree.c:641
struct ParallelIndexScanDescData * parallel_scan
Definition: relscan.h:151
int markItemIndex
Definition: nbtree.h:660
static bool _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
Definition: nbtsearch.c:1730
#define Assert(condition)
Definition: c.h:732
#define BTScanPosInvalidate(scanpos)
Definition: nbtree.h:606
#define InvalidBlockNumber
Definition: block.h:33
BlockNumber nextPage
Definition: nbtree.h:550
BTScanPosData currPos
Definition: nbtree.h:663
Buffer buf
Definition: nbtree.h:546
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:3391
#define offsetof(type, field)
Definition: c.h:655
#define BTScanPosUnpinIfPinned(scanpos)
Definition: nbtree.h:594

◆ _bt_walk_left()

static Buffer _bt_walk_left ( Relation  rel,
Buffer  buf,
Snapshot  snapshot 
)
static

Definition at line 1938 of file nbtsearch.c.

References _bt_getbuf(), _bt_relandgetbuf(), _bt_relbuf(), BT_READ, BTPageOpaqueData::btpo_next, BTPageOpaqueData::btpo_prev, buf, BufferGetBlockNumber(), BufferGetPage, CHECK_FOR_INTERRUPTS, elog, ERROR, InvalidBuffer, P_ISDELETED, P_LEFTMOST, P_RIGHTMOST, PageGetSpecialPointer, RelationGetRelationName, and TestForOldSnapshot().

Referenced by _bt_readnextpage().

1939 {
1940  Page page;
1941  BTPageOpaque opaque;
1942 
1943  page = BufferGetPage(buf);
1944  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1945 
1946  for (;;)
1947  {
1948  BlockNumber obknum;
1949  BlockNumber lblkno;
1950  BlockNumber blkno;
1951  int tries;
1952 
1953  /* if we're at end of tree, release buf and return failure */
1954  if (P_LEFTMOST(opaque))
1955  {
1956  _bt_relbuf(rel, buf);
1957  break;
1958  }
1959  /* remember original page we are stepping left from */
1960  obknum = BufferGetBlockNumber(buf);
1961  /* step left */
1962  blkno = lblkno = opaque->btpo_prev;
1963  _bt_relbuf(rel, buf);
1964  /* check for interrupts while we're not holding any buffer lock */
1966  buf = _bt_getbuf(rel, blkno, BT_READ);
1967  page = BufferGetPage(buf);
1968  TestForOldSnapshot(snapshot, rel, page);
1969  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1970 
1971  /*
1972  * If this isn't the page we want, walk right till we find what we
1973  * want --- but go no more than four hops (an arbitrary limit). If we
1974  * don't find the correct page by then, the most likely bet is that
1975  * the original page got deleted and isn't in the sibling chain at all
1976  * anymore, not that its left sibling got split more than four times.
1977  *
1978  * Note that it is correct to test P_ISDELETED not P_IGNORE here,
1979  * because half-dead pages are still in the sibling chain. Caller
1980  * must reject half-dead pages if wanted.
1981  */
1982  tries = 0;
1983  for (;;)
1984  {
1985  if (!P_ISDELETED(opaque) && opaque->btpo_next == obknum)
1986  {
1987  /* Found desired page, return it */
1988  return buf;
1989  }
1990  if (P_RIGHTMOST(opaque) || ++tries > 4)
1991  break;
1992  blkno = opaque->btpo_next;
1993  buf = _bt_relandgetbuf(rel, buf, blkno, BT_READ);
1994  page = BufferGetPage(buf);
1995  TestForOldSnapshot(snapshot, rel, page);
1996  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1997  }
1998 
1999  /* Return to the original page to see what's up */
2000  buf = _bt_relandgetbuf(rel, buf, obknum, BT_READ);
2001  page = BufferGetPage(buf);
2002  TestForOldSnapshot(snapshot, rel, page);
2003  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2004  if (P_ISDELETED(opaque))
2005  {
2006  /*
2007  * It was deleted. Move right to first nondeleted page (there
2008  * must be one); that is the page that has acquired the deleted
2009  * one's keyspace, so stepping left from it will take us where we
2010  * want to be.
2011  */
2012  for (;;)
2013  {
2014  if (P_RIGHTMOST(opaque))
2015  elog(ERROR, "fell off the end of index \"%s\"",
2017  blkno = opaque->btpo_next;
2018  buf = _bt_relandgetbuf(rel, buf, blkno, BT_READ);
2019  page = BufferGetPage(buf);
2020  TestForOldSnapshot(snapshot, rel, page);
2021  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
2022  if (!P_ISDELETED(opaque))
2023  break;
2024  }
2025 
2026  /*
2027  * Now return to top of loop, resetting obknum to point to this
2028  * nondeleted page, and try again.
2029  */
2030  }
2031  else
2032  {
2033  /*
2034  * It wasn't deleted; the explanation had better be that the page
2035  * to the left got split or deleted. Without this check, we'd go
2036  * into an infinite loop if there's anything wrong.
2037  */
2038  if (opaque->btpo_prev == lblkno)
2039  elog(ERROR, "could not find left sibling of block %u in index \"%s\"",
2040  obknum, RelationGetRelationName(rel));
2041  /* Okay to try again with new lblkno value */
2042  }
2043  }
2044 
2045  return InvalidBuffer;
2046 }
BlockNumber btpo_next
Definition: nbtree.h:58
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:264
Buffer _bt_relandgetbuf(Relation rel, Buffer obuf, BlockNumber blkno, int access)
Definition: nbtpage.c:893
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
Definition: nbtpage.c:757
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
#define BT_READ
Definition: nbtree.h:402
#define ERROR
Definition: elog.h:43
BlockNumber btpo_prev
Definition: nbtree.h:57
static char * buf
Definition: pg_test_fsync.c:68
#define RelationGetRelationName(relation)
Definition: rel.h:450
#define P_LEFTMOST(opaque)
Definition: nbtree.h:187
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define P_ISDELETED(opaque)
Definition: nbtree.h:191
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:912
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
#define elog(elevel,...)
Definition: elog.h:226
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:188
Pointer Page
Definition: bufpage.h:78