27#define LOOK_AHEAD_REQUIRED_RECHECKS 3
28#define LOOK_AHEAD_DEFAULT_DISTANCE 5
29#define NSKIPADVANCES_THRESHOLD 3
32 Datum tupdatum,
bool tupnull,
35 Datum tupdatum,
bool tupnull,
37 int32 *set_elem_result);
39 int32 set_elem_result,
Datum tupdatum,
bool tupnull);
46 bool *skip_array_set);
50 bool readpagetup,
int sktrig,
bool *scanBehind);
53 int sktrig,
bool sktrig_required);
54#ifdef USE_ASSERT_CHECKING
56static bool _bt_verify_keys_with_arraykeys(
IndexScanDesc scan);
62 bool advancenonrequired,
bool forcenonrequired,
63 bool *continuescan,
int *ikey);
66 ScanDirection dir,
bool forcenonrequired,
bool *continuescan);
124 key->heapkeyspace =
true;
125 key->allequalimage =
false;
127 key->anynullkeys =
false;
128 key->nextkey =
false;
129 key->backward =
false;
130 key->keysz =
Min(indnkeyatts, tupnatts);
131 key->scantid =
key->heapkeyspace && itup ?
133 skey =
key->scankeys;
134 for (
i = 0;
i < indnkeyatts;
i++)
170 key->anynullkeys =
true;
177 if (rel->
rd_index->indnullsnotdistinct)
178 key->anynullkeys =
false;
191 while (stack != NULL)
217 Datum tupdatum,
bool tupnull,
248 tupdatum, arrdatum));
289 Datum tupdatum,
bool tupnull,
291 int32 *set_elem_result)
333 if (high_elem >= low_elem)
342 *set_elem_result = result;
349 if (high_elem < low_elem)
352 *set_elem_result = 1;
361 if (high_elem >= low_elem)
370 *set_elem_result = result;
373 mid_elem = high_elem;
377 if (high_elem < low_elem)
380 *set_elem_result = -1;
386 while (high_elem > low_elem)
388 mid_elem = low_elem + ((high_elem - low_elem) / 2);
405 low_elem = mid_elem + 1;
407 high_elem = mid_elem;
415 if (low_elem != mid_elem)
419 *set_elem_result = result;
444 Datum tupdatum,
bool tupnull,
446 int32 *set_elem_result)
458 *set_elem_result = 0;
465 *set_elem_result = -1;
467 *set_elem_result = 1;
475 *set_elem_result = 0;
487 *set_elem_result = -1;
493 *set_elem_result = 1;
506 *set_elem_result = 1;
512 *set_elem_result = -1;
519#ifdef USE_ASSERT_CHECKING
551 int32 set_elem_result,
Datum tupdatum,
bool tupnull)
684 else if (low_not_high)
706 Datum dec_sk_argument;
839 Datum inc_sk_argument;
976 bool *skip_array_set)
992 *skip_array_set =
true;
1149 bool readpagetup,
int sktrig,
bool *scanBehind)
1155 Assert(sktrig == 0 || readpagetup);
1156 Assert(!readpagetup || scanBehind == NULL);
1159 *scanBehind =
false;
1161 for (
int ikey = sktrig; ikey < so->
numberOfKeys; ikey++)
1169 Assert(!readpagetup || ikey == sktrig);
1181 Assert(ikey > sktrig || ikey == 0);
1185 if (
cur->sk_attno > tupnatts)
1270 for (
int arrayidx = 0; arrayidx < so->
numArrayKeys; arrayidx++)
1278 array,
cur, &result);
1309 if (readpagetup || result != 0)
1383 Assert(_bt_verify_arrays_bt_first(scan, dir));
1462 int sktrig,
bool sktrig_required)
1468 bool beyond_end_advance =
false,
1469 skip_array_advanced =
false,
1470 has_required_opposite_direction_only =
false,
1471 all_required_satisfied =
true,
1472 all_satisfied =
true;
1475 Assert(_bt_verify_keys_with_arraykeys(scan));
1477 if (sktrig_required)
1483 tupnatts,
false, 0, NULL));
1492 else if (sktrig < so->numberOfKeys - 1 &&
1508 false, &continuescan,
1519 required_opposite_direction_only =
false,
1543 has_required_opposite_direction_only =
1544 required_opposite_direction_only =
true;
1555 if (
cur->sk_attno > tupnatts)
1584 if (ikey == sktrig && !array)
1589 beyond_end_advance =
true;
1590 all_satisfied = all_required_satisfied =
false;
1620 if (beyond_end_advance)
1647 if (!all_required_satisfied ||
cur->sk_attno > tupnatts)
1664 bool cur_elem_trig = (sktrig_required && ikey == sktrig);
1672 tupdatum, tupnull, array,
cur,
1681 tupdatum, tupnull, array,
cur,
1731 beyond_end_advance =
true;
1733 Assert(all_required_satisfied && all_satisfied);
1742 all_satisfied =
false;
1744 all_required_satisfied =
false;
1766 skip_array_advanced =
true;
1768 else if (array->
cur_elem != set_elem)
1783 if (beyond_end_advance &&
1785 goto end_toplevel_scan;
1787 Assert(_bt_verify_keys_with_arraykeys(scan));
1793 if (sktrig_required && skip_array_advanced)
1819 if ((sktrig_required && all_required_satisfied) ||
1820 (!sktrig_required && all_satisfied))
1822 int nsktrig = sktrig + 1;
1825 Assert(all_required_satisfied);
1829 false, &continuescan,
1834 Assert(all_satisfied && continuescan);
1869 Assert(!beyond_end_advance);
1873 tupdesc, nsktrig,
true);
1892 if (!sktrig_required)
1915 !all_required_satisfied);
1925 if (!all_required_satisfied && pstate->
finaltup == tuple)
1936 if (!all_required_satisfied && pstate->
finaltup &&
2008 else if (has_required_opposite_direction_only && pstate->
finaltup &&
2144#ifdef USE_ASSERT_CHECKING
2180 if (array->
cur_elem != first_elem_dir)
2184 return _bt_verify_keys_with_arraykeys(scan);
2220 if (last_sk_attno >
cur->sk_attno)
2222 last_sk_attno =
cur->sk_attno;
2286#ifdef USE_ASSERT_CHECKING
2305 Assert(arrayKeys || ikey == dikey);
2400 nfinaltupatts,
false, 0, NULL))
2442 false, &continuescan,
2495 firstchangingattnum;
2496 bool start_past_saop_eq =
false;
2563 if (
key->sk_attno > firstchangingattnum)
2575 if (firstnull || lastnull)
2585 key->sk_collation, firstdatum,
2592 key->sk_collation, lastdatum,
2613 if (
key->sk_attno >= firstchangingattnum)
2631 key->sk_collation, firstdatum,
2650 if (
key->sk_attno >= firstchangingattnum)
2657 firstdatum, firstnull, array,
key,
2663 start_past_saop_eq =
true;
2688 if (
key->sk_attno > firstchangingattnum)
2697 firstdatum, firstnull, array,
key,
2704 lastdatum, lastnull, array,
key,
2797 bool advancenonrequired,
bool forcenonrequired,
2798 bool *continuescan,
int *ikey)
2802 Assert(!forcenonrequired || advancenonrequired);
2804 *continuescan =
true;
2811 bool requiredSameDir =
false,
2812 requiredOppositeDirOnly =
false;
2819 if (forcenonrequired)
2825 requiredSameDir =
true;
2828 requiredOppositeDirOnly =
true;
2830 if (
key->sk_attno > tupnatts)
2851 Assert(requiredSameDir || forcenonrequired);
2858 if (forcenonrequired)
2860 tupdesc, *ikey,
false);
2862 *continuescan =
false;
2870 forcenonrequired, continuescan))
2901 if (requiredSameDir)
2902 *continuescan =
false;
2911 Assert(forcenonrequired && *ikey > 0);
2932 tupdesc, *ikey,
false);
2948 if ((requiredSameDir || requiredOppositeDirOnly) &&
2950 *continuescan =
false;
2966 if ((requiredSameDir || requiredOppositeDirOnly) &&
2968 *continuescan =
false;
2978 datum,
key->sk_argument)))
2990 if (requiredSameDir)
2991 *continuescan =
false;
2999 else if (advancenonrequired &&
3003 tupdesc, *ikey,
false);
3028 bool forcenonrequired,
bool *continuescan)
3031 int32 cmpresult = 0;
3068 if (forcenonrequired)
3088 *continuescan =
false;
3106 *continuescan =
false;
3126 if (forcenonrequired)
3132 *continuescan =
false;
3135 *continuescan =
false;
3166 result = (cmpresult < 0);
3169 result = (cmpresult <= 0);
3172 result = (cmpresult >= 0);
3175 result = (cmpresult > 0);
3183 if (!result && !forcenonrequired)
3193 *continuescan =
false;
3196 *continuescan =
false;
3271 pstate->
skip = aheadoffnum + 1;
3273 pstate->
skip = aheadoffnum - 1;
3330 bool killedsomething =
false;
3377 for (
i = 0;
i < numKilled;
i++)
3384 itemIndex <= so->currPos.lastItem);
3385 if (offnum < minoff)
3387 while (offnum <= maxoff)
3391 bool killtuple =
false;
3413 for (
j = 0;
j < nposting;
j++)
3472 killedsomething =
true;
3486 if (killedsomething)
3602 elog(
ERROR,
"multiple active vacuums for index \"%s\"",
3712 offsetof(
BTOptions, vacuum_cleanup_index_scale_factor)},
3732 bool *res,
bool *isnull)
3758 return "initializing";
3760 return "scanning table";
3762 return "sorting live tuples";
3764 return "sorting dead tuples";
3766 return "loading tuples in tree";
3821#ifdef DEBUG_NO_TRUNCATE
3823 keepnatts = nkeyatts + 1;
3827 Min(keepnatts, nkeyatts));
3836 Assert(keepnatts == nkeyatts || keepnatts == nkeyatts + 1);
3838 pivot->
t_info &= ~INDEX_SIZE_MASK;
3846 if (keepnatts <= nkeyatts)
3875 tidpivot->
t_info &= ~INDEX_SIZE_MASK;
3876 tidpivot->
t_info |= newsize;
3899#ifndef DEBUG_NO_TRUNCATE
3974 if (isNull1 != isNull2)
4039 if (isNull1 != isNull2)
4119 return tupnatts == natts;
4135 return tupnatts == nkeyatts;
4151 return tupnatts == 0;
4165 return tupnatts == 0 ||
4177 return tupnatts == nkeyatts;
4211 return tupnatts > 0 && tupnatts <= nkeyatts;
4253 elog(
ERROR,
"cannot insert oversized tuple of size %zu on internal page of index \"%s\"",
4257 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4258 errmsg(
"index row size %zu exceeds btree version %u maximum %zu for index \"%s\"",
4263 errdetail(
"Index row references tuple (%u,%u) in relation \"%s\".",
4267 errhint(
"Values larger than 1/3 of a buffer page cannot be indexed.\n"
4268 "Consider a function index of an MD5 hash of the value, "
4269 "or use full text indexing."),
4286 bool allequalimage =
true;
4311 allequalimage =
false;
4319 elog(
DEBUG1,
"index \"%s\" can safely use deduplication",
4322 elog(
DEBUG1,
"index \"%s\" cannot use deduplication",
4326 return allequalimage;
#define InvalidAttrNumber
static bool validate(Port *port, const char *auth)
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
static Page BufferGetPage(Buffer buffer)
static Item PageGetItem(const PageData *page, const ItemIdData *itemId)
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
#define INVERT_COMPARE_RESULT(var)
#define PG_USED_FOR_ASSERTS_ONLY
#define FLEXIBLE_ARRAY_MEMBER
#define OidIsValid(objectId)
Datum datumCopy(Datum value, bool typByVal, int typLen)
bool datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Assert(PointerIsAligned(start, uint64))
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
IndexTuple index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source, int leavenatts)
if(TABLE==NULL||TABLE_index==NULL)
#define ItemIdMarkDead(itemId)
#define ItemIdIsDead(itemId)
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
static void ItemPointerSetOffsetNumber(ItemPointerData *pointer, OffsetNumber offsetNumber)
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
static OffsetNumber ItemPointerGetOffsetNumberNoCheck(const ItemPointerData *pointer)
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
IndexTupleData * IndexTuple
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static Size IndexTupleSize(const IndexTupleData *itup)
#define MaxIndexTuplesPerPage
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void pfree(void *pointer)
void * palloc0(Size size)
void _bt_relbuf(Relation rel, Buffer buf)
void _bt_metaversion(Relation rel, bool *heapkeyspace, bool *allequalimage)
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
void _bt_unlockbuf(Relation rel, Buffer buf)
void _bt_lockbuf(Relation rel, Buffer buf, int access)
void _bt_parallel_primscan_schedule(IndexScanDesc scan, BlockNumber curr_page)
void _bt_parallel_done(IndexScanDesc scan)
#define BTScanPosIsPinned(scanpos)
#define BT_PIVOT_HEAP_TID_ATTR
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
static bool BTreeTupleIsPivot(IndexTuple itup)
#define PROGRESS_BTREE_PHASE_PERFORMSORT_2
#define PROGRESS_BTREE_PHASE_LEAF_LOAD
#define BTEQUALIMAGE_PROC
#define P_LEFTMOST(opaque)
#define BTPageGetOpaque(page)
#define BTScanPosIsValid(scanpos)
#define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN
#define SK_BT_INDOPTION_SHIFT
#define P_FIRSTDATAKEY(opaque)
#define PROGRESS_BTREE_PHASE_PERFORMSORT_1
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
#define P_RIGHTMOST(opaque)
#define SK_BT_NULLS_FIRST
static ItemPointer BTreeTupleGetPostingN(IndexTuple posting, int n)
static ItemPointer BTreeTupleGetMaxHeapTID(IndexTuple itup)
static bool BTreeTupleIsPosting(IndexTuple itup)
#define BTREE_NOVAC_VERSION
#define BTMaxItemSizeNoHeapTid
static ItemPointer BTreeTupleGetHeapTID(IndexTuple itup)
static void BTreeTupleSetNAtts(IndexTuple itup, uint16 nkeyatts, bool heaptid)
#define BTreeTupleGetNAtts(itup, rel)
BTScanOpaqueData * BTScanOpaque
static void _bt_rewind_nonrequired_arrays(IndexScanDesc scan, ScanDirection dir)
void _bt_check_third_page(Relation rel, Relation heap, bool needheaptidspace, Page page, IndexTuple newtup)
bool _bt_scanbehind_checkkeys(IndexScanDesc scan, ScanDirection dir, IndexTuple finaltup)
void _bt_end_vacuum(Relation rel)
static void _bt_binsrch_skiparray_skey(bool cur_elem_trig, ScanDirection dir, Datum tupdatum, bool tupnull, BTArrayKeyInfo *array, ScanKey cur, int32 *set_elem_result)
static void _bt_array_set_low_or_high(Relation rel, ScanKey skey, BTArrayKeyInfo *array, bool low_not_high)
bool _bt_checkkeys(IndexScanDesc scan, BTReadPageState *pstate, bool arrayKeys, IndexTuple tuple, int tupnatts)
static void _bt_skiparray_set_element(Relation rel, ScanKey skey, BTArrayKeyInfo *array, int32 set_elem_result, Datum tupdatum, bool tupnull)
void _bt_end_vacuum_callback(int code, Datum arg)
#define NSKIPADVANCES_THRESHOLD
int _bt_binsrch_array_skey(FmgrInfo *orderproc, bool cur_elem_trig, ScanDirection dir, Datum tupdatum, bool tupnull, BTArrayKeyInfo *array, ScanKey cur, int32 *set_elem_result)
void _bt_freestack(BTStack stack)
void BTreeShmemInit(void)
struct BTVacInfo BTVacInfo
BTCycleId _bt_vacuum_cycleid(Relation rel)
BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup)
void _bt_killitems(IndexScanDesc scan)
static bool _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate, IndexTuple tuple, int tupnatts, TupleDesc tupdesc, int sktrig, bool sktrig_required)
bool _bt_start_prim_scan(IndexScanDesc scan, ScanDirection dir)
bool _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
IndexTuple _bt_truncate(Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
#define LOOK_AHEAD_REQUIRED_RECHECKS
static bool _bt_oppodir_checkkeys(IndexScanDesc scan, ScanDirection dir, IndexTuple finaltup)
int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
#define LOOK_AHEAD_DEFAULT_DISTANCE
static BTVacInfo * btvacinfo
static bool _bt_check_compare(IndexScanDesc scan, ScanDirection dir, IndexTuple tuple, int tupnatts, TupleDesc tupdesc, bool advancenonrequired, bool forcenonrequired, bool *continuescan, int *ikey)
void _bt_set_startikey(IndexScanDesc scan, BTReadPageState *pstate)
static bool _bt_array_decrement(Relation rel, ScanKey skey, BTArrayKeyInfo *array)
char * btbuildphasename(int64 phasenum)
static bool _bt_tuple_before_array_skeys(IndexScanDesc scan, ScanDirection dir, IndexTuple tuple, TupleDesc tupdesc, int tupnatts, bool readpagetup, int sktrig, bool *scanBehind)
bytea * btoptions(Datum reloptions, bool validate)
Size BTreeShmemSize(void)
static int _bt_keep_natts(Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
bool btproperty(Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
static void _bt_skiparray_set_isnull(Relation rel, ScanKey skey, BTArrayKeyInfo *array)
bool _bt_allequalimage(Relation rel, bool debugmessage)
static bool _bt_advance_array_keys_increment(IndexScanDesc scan, ScanDirection dir, bool *skip_array_set)
static void _bt_checkkeys_look_ahead(IndexScanDesc scan, BTReadPageState *pstate, int tupnatts, TupleDesc tupdesc)
static int32 _bt_compare_array_skey(FmgrInfo *orderproc, Datum tupdatum, bool tupnull, Datum arrdatum, ScanKey cur)
struct BTOneVacInfo BTOneVacInfo
void _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
static bool _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, int tupnatts, TupleDesc tupdesc, ScanDirection dir, bool forcenonrequired, bool *continuescan)
BTCycleId _bt_start_vacuum(Relation rel)
static bool _bt_array_increment(Relation rel, ScanKey skey, BTArrayKeyInfo *array)
#define OffsetNumberNext(offsetNumber)
#define FirstOffsetNumber
#define OffsetNumberPrev(offsetNumber)
static bool DatumGetBool(Datum X)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
static int32 DatumGetInt32(Datum X)
#define PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
#define IndexRelationGetNumberOfAttributes(relation)
#define IndexRelationGetNumberOfKeyAttributes(relation)
int errtableconstraint(Relation rel, const char *conname)
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
void ScanKeyEntryInitializeWithInfo(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, FmgrInfo *finfo, Datum argument)
#define ScanDirectionIsForward(direction)
#define ScanDirectionIsBackward(direction)
#define ScanDirectionIsNoMovement(direction)
@ NoMovementScanDirection
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
#define BTGreaterStrategyNumber
#define BTLessStrategyNumber
#define BTEqualStrategyNumber
#define BTLessEqualStrategyNumber
#define BTGreaterEqualStrategyNumber
ScanKeyData scankeys[INDEX_MAX_KEYS]
BTArrayKeyInfo * arrayKeys
BTScanPosItem items[MaxTIDsPerBTreePage]
struct BTStackData * bts_parent
BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER]
struct ParallelIndexScanDescData * parallel_scan
StrategyNumber sk_strategy
SkipSupportIncDec decrement
SkipSupportIncDec increment
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)