27#define LOOK_AHEAD_REQUIRED_RECHECKS 3
28#define LOOK_AHEAD_DEFAULT_DISTANCE 5
31 Datum tupdatum,
bool tupnull,
37 bool readpagetup,
int sktrig,
bool *scanBehind);
40 int sktrig,
bool sktrig_required);
41#ifdef USE_ASSERT_CHECKING
43static bool _bt_verify_keys_with_arraykeys(
IndexScanDesc scan);
47 bool advancenonrequired,
bool prechecked,
bool firstmatch,
48 bool *continuescan,
int *ikey);
109 key->heapkeyspace =
true;
110 key->allequalimage =
false;
112 key->anynullkeys =
false;
113 key->nextkey =
false;
114 key->backward =
false;
115 key->keysz =
Min(indnkeyatts, tupnatts);
116 key->scantid =
key->heapkeyspace && itup ?
118 skey =
key->scankeys;
119 for (
i = 0;
i < indnkeyatts;
i++)
155 key->anynullkeys =
true;
162 if (rel->
rd_index->indnullsnotdistinct)
163 key->anynullkeys =
false;
176 while (stack != NULL)
202 Datum tupdatum,
bool tupnull,
232 tupdatum, arrdatum));
273 Datum tupdatum,
bool tupnull,
275 int32 *set_elem_result)
315 if (high_elem >= low_elem)
324 *set_elem_result = result;
331 if (high_elem < low_elem)
334 *set_elem_result = 1;
343 if (high_elem >= low_elem)
352 *set_elem_result = result;
355 mid_elem = high_elem;
359 if (high_elem < low_elem)
362 *set_elem_result = -1;
368 while (high_elem > low_elem)
370 mid_elem = low_elem + ((high_elem - low_elem) / 2);
387 low_elem = mid_elem + 1;
389 high_elem = mid_elem;
397 if (low_elem != mid_elem)
401 *set_elem_result = result;
464 int cur_elem = curArrayKey->
cur_elem;
475 cur_elem = num_elems - 1;
568 if (array->
cur_elem != first_elem_dir)
621 bool readpagetup,
int sktrig,
bool *scanBehind)
627 Assert(sktrig == 0 || readpagetup);
628 Assert(!readpagetup || scanBehind == NULL);
633 for (
int ikey = sktrig; ikey < so->
numberOfKeys; ikey++)
641 Assert(!readpagetup || ikey == sktrig);
653 Assert(ikey > sktrig || ikey == 0);
657 if (
cur->sk_attno > tupnatts)
713 if (readpagetup || result != 0)
787 Assert(_bt_verify_arrays_bt_first(scan, dir));
865 int sktrig,
bool sktrig_required)
871 bool beyond_end_advance =
false,
872 has_required_opposite_direction_only =
false,
873 oppodir_inequality_sktrig =
false,
874 all_required_satisfied =
true,
875 all_satisfied =
true;
889 tupnatts,
false, 0, NULL));
910 Assert(_bt_verify_keys_with_arraykeys(scan));
918 required_opposite_direction_only =
false,
942 has_required_opposite_direction_only =
943 required_opposite_direction_only =
true;
956 if (
cur->sk_attno > tupnatts)
985 if (ikey == sktrig && !array)
990 beyond_end_advance =
true;
991 all_satisfied = all_required_satisfied =
false;
1002 if (
unlikely(required_opposite_direction_only))
1003 oppodir_inequality_sktrig =
true;
1033 if (beyond_end_advance)
1042 if (array && array->
cur_elem != final_elem_dir)
1069 if (!all_required_satisfied ||
cur->sk_attno > tupnatts)
1078 if (array && array->
cur_elem != first_elem_dir)
1095 bool cur_elem_trig = (sktrig_required && ikey == sktrig);
1102 tupdatum, tupnull, array,
cur,
1105 Assert(set_elem >= 0 && set_elem < array->num_elems);
1154 beyond_end_advance =
true;
1156 Assert(all_required_satisfied && all_satisfied);
1165 all_satisfied =
false;
1167 all_required_satisfied =
false;
1182 if (array && array->
cur_elem != set_elem)
1196 goto end_toplevel_scan;
1198 Assert(_bt_verify_keys_with_arraykeys(scan));
1217 if ((sktrig_required && all_required_satisfied) ||
1218 (!sktrig_required && all_satisfied))
1220 int nsktrig = sktrig + 1;
1223 Assert(all_required_satisfied);
1227 false,
false,
false,
1228 &continuescan, &nsktrig) &&
1232 Assert(all_satisfied && continuescan);
1267 Assert(!beyond_end_advance);
1271 tupdesc, nsktrig,
true);
1290 if (!sktrig_required)
1316 !all_required_satisfied);
1326 if (!all_required_satisfied && pstate->
finaltup == tuple)
1337 if (!all_required_satisfied && pstate->
finaltup &&
1371 if (so->
scanBehind && has_required_opposite_direction_only)
1428 else if (has_required_opposite_direction_only && pstate->
finaltup &&
1429 (all_required_satisfied || oppodir_inequality_sktrig) &&
1508#ifdef USE_ASSERT_CHECKING
1544 if (array->
cur_elem != first_elem_dir)
1548 return _bt_verify_keys_with_arraykeys(scan);
1583 if (last_sk_attno >
cur->sk_attno)
1585 last_sk_attno =
cur->sk_attno;
1642#ifdef USE_ASSERT_CHECKING
1653 tupnatts,
false, 0, NULL));
1665 false,
false,
false,
1666 &dcontinuescan, &dikey));
1796 false,
false,
false, &continuescan, &ikey);
1845 bool advancenonrequired,
bool prechecked,
bool firstmatch,
1846 bool *continuescan,
int *ikey)
1850 *continuescan =
true;
1857 bool requiredSameDir =
false,
1858 requiredOppositeDirOnly =
false;
1866 requiredSameDir =
true;
1869 requiredOppositeDirOnly =
true;
1888 (requiredSameDir || (requiredOppositeDirOnly && firstmatch)) &&
1892 if (
key->sk_attno > tupnatts)
1938 if (requiredSameDir)
1939 *continuescan =
false;
1965 *continuescan =
false;
1983 *continuescan =
false;
1999 if (!(requiredOppositeDirOnly && firstmatch) &&
2001 datum,
key->sk_argument)))
2013 if (requiredSameDir)
2014 *continuescan =
false;
2022 else if (advancenonrequired &&
2026 tupdesc, *ikey,
false);
2053 int32 cmpresult = 0;
2106 *continuescan =
false;
2124 *continuescan =
false;
2146 *continuescan =
false;
2149 *continuescan =
false;
2180 result = (cmpresult < 0);
2183 result = (cmpresult <= 0);
2186 result = (cmpresult >= 0);
2189 result = (cmpresult > 0);
2207 *continuescan =
false;
2210 *continuescan =
false;
2283 pstate->
skip = aheadoffnum + 1;
2285 pstate->
skip = aheadoffnum - 1;
2342 bool killedsomething =
false;
2389 for (
i = 0;
i < numKilled;
i++)
2396 itemIndex <= so->currPos.lastItem);
2397 if (offnum < minoff)
2399 while (offnum <= maxoff)
2403 bool killtuple =
false;
2425 for (
j = 0;
j < nposting;
j++)
2484 killedsomething =
true;
2498 if (killedsomething)
2614 elog(
ERROR,
"multiple active vacuums for index \"%s\"",
2724 offsetof(
BTOptions, vacuum_cleanup_index_scale_factor)},
2744 bool *
res,
bool *isnull)
2770 return "initializing";
2772 return "scanning table";
2774 return "sorting live tuples";
2776 return "sorting dead tuples";
2778 return "loading tuples in tree";
2833#ifdef DEBUG_NO_TRUNCATE
2835 keepnatts = nkeyatts + 1;
2839 Min(keepnatts, nkeyatts));
2848 Assert(keepnatts == nkeyatts || keepnatts == nkeyatts + 1);
2850 pivot->
t_info &= ~INDEX_SIZE_MASK;
2858 if (keepnatts <= nkeyatts)
2887 tidpivot->
t_info &= ~INDEX_SIZE_MASK;
2888 tidpivot->
t_info |= newsize;
2911#ifndef DEBUG_NO_TRUNCATE
2986 if (isNull1 != isNull2)
3051 if (isNull1 != isNull2)
3131 return tupnatts == natts;
3147 return tupnatts == nkeyatts;
3163 return tupnatts == 0;
3177 return tupnatts == 0 ||
3189 return tupnatts == nkeyatts;
3223 return tupnatts > 0 && tupnatts <= nkeyatts;
3265 elog(
ERROR,
"cannot insert oversized tuple of size %zu on internal page of index \"%s\"",
3269 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3270 errmsg(
"index row size %zu exceeds btree version %u maximum %zu for index \"%s\"",
3276 errdetail(
"Index row references tuple (%u,%u) in relation \"%s\".",
3280 errhint(
"Values larger than 1/3 of a buffer page cannot be indexed.\n"
3281 "Consider a function index of an MD5 hash of the value, "
3282 "or use full text indexing."),
3299 bool allequalimage =
true;
3324 allequalimage =
false;
3332 elog(
DEBUG1,
"index \"%s\" can safely use deduplication",
3335 elog(
DEBUG1,
"index \"%s\" cannot use deduplication",
3339 return allequalimage;
#define InvalidAttrNumber
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 Assert(condition)
#define FLEXIBLE_ARRAY_MEMBER
#define OidIsValid(objectId)
bool datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
static void PGresult * res
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)
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
#define IndexTupleSize(itup)
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#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 BTMaxItemSizeNoHeapTid(page)
#define PROGRESS_BTREE_PHASE_LEAF_LOAD
#define BTEQUALIMAGE_PROC
#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 BTMaxItemSize(page)
#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
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)
static bool _bt_check_compare(IndexScanDesc scan, ScanDirection dir, IndexTuple tuple, int tupnatts, TupleDesc tupdesc, bool advancenonrequired, bool prechecked, bool firstmatch, bool *continuescan, int *ikey)
void _bt_end_vacuum(Relation rel)
bool _bt_checkkeys(IndexScanDesc scan, BTReadPageState *pstate, bool arrayKeys, IndexTuple tuple, int tupnatts)
void _bt_end_vacuum_callback(int code, Datum arg)
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
int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
#define LOOK_AHEAD_DEFAULT_DISTANCE
static BTVacInfo * btvacinfo
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)
static bool _bt_advance_array_keys_increment(IndexScanDesc scan, ScanDirection dir)
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)
bool _bt_allequalimage(Relation rel, bool debugmessage)
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 *continuescan)
bool _bt_oppodir_checkkeys(IndexScanDesc scan, ScanDirection dir, IndexTuple finaltup)
BTCycleId _bt_start_vacuum(Relation rel)
#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)
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
static pg_noinline void Size size
#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
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)