PostgreSQL Source Code git master
nbtutils.c File Reference
#include "postgres.h"
#include <time.h>
#include "access/nbtree.h"
#include "access/reloptions.h"
#include "access/relscan.h"
#include "commands/progress.h"
#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for nbtutils.c:

Go to the source code of this file.

Data Structures

struct  BTOneVacInfo
 
struct  BTVacInfo
 

Typedefs

typedef struct BTOneVacInfo BTOneVacInfo
 
typedef struct BTVacInfo BTVacInfo
 

Functions

static int _bt_compare_int (const void *va, const void *vb)
 
static int _bt_keep_natts (Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
 
BTScanInsert _bt_mkscankey (Relation rel, IndexTuple itup)
 
void _bt_freestack (BTStack stack)
 
void _bt_killitems (IndexScanDesc scan)
 
BTCycleId _bt_vacuum_cycleid (Relation rel)
 
BTCycleId _bt_start_vacuum (Relation rel)
 
void _bt_end_vacuum (Relation rel)
 
void _bt_end_vacuum_callback (int code, Datum arg)
 
Size BTreeShmemSize (void)
 
void BTreeShmemInit (void)
 
byteabtoptions (Datum reloptions, bool validate)
 
bool btproperty (Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
 
char * btbuildphasename (int64 phasenum)
 
IndexTuple _bt_truncate (Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
 
int _bt_keep_natts_fast (Relation rel, IndexTuple lastleft, IndexTuple firstright)
 
bool _bt_check_natts (Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
 
void _bt_check_third_page (Relation rel, Relation heap, bool needheaptidspace, Page page, IndexTuple newtup)
 
bool _bt_allequalimage (Relation rel, bool debugmessage)
 

Variables

static BTVacInfobtvacinfo
 

Typedef Documentation

◆ BTOneVacInfo

typedef struct BTOneVacInfo BTOneVacInfo

◆ BTVacInfo

typedef struct BTVacInfo BTVacInfo

Function Documentation

◆ _bt_allequalimage()

bool _bt_allequalimage ( Relation  rel,
bool  debugmessage 
)

Definition at line 1181 of file nbtutils.c.

1182{
1183 bool allequalimage = true;
1184
1185 /* INCLUDE indexes can never support deduplication */
1188 return false;
1189
1190 for (int i = 0; i < IndexRelationGetNumberOfKeyAttributes(rel); i++)
1191 {
1192 Oid opfamily = rel->rd_opfamily[i];
1193 Oid opcintype = rel->rd_opcintype[i];
1194 Oid collation = rel->rd_indcollation[i];
1195 Oid equalimageproc;
1196
1197 equalimageproc = get_opfamily_proc(opfamily, opcintype, opcintype,
1199
1200 /*
1201 * If there is no BTEQUALIMAGE_PROC then deduplication is assumed to
1202 * be unsafe. Otherwise, actually call proc and see what it says.
1203 */
1204 if (!OidIsValid(equalimageproc) ||
1205 !DatumGetBool(OidFunctionCall1Coll(equalimageproc, collation,
1206 ObjectIdGetDatum(opcintype))))
1207 {
1208 allequalimage = false;
1209 break;
1210 }
1211 }
1212
1213 if (debugmessage)
1214 {
1215 if (allequalimage)
1216 elog(DEBUG1, "index \"%s\" can safely use deduplication",
1218 else
1219 elog(DEBUG1, "index \"%s\" cannot use deduplication",
1221 }
1222
1223 return allequalimage;
1224}
#define OidIsValid(objectId)
Definition: c.h:788
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:226
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Definition: fmgr.c:1412
int i
Definition: isn.c:77
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:887
#define BTEQUALIMAGE_PROC
Definition: nbtree.h:720
static bool DatumGetBool(Datum X)
Definition: postgres.h:100
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetRelationName(relation)
Definition: rel.h:549
#define IndexRelationGetNumberOfAttributes(relation)
Definition: rel.h:527
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:534
Oid * rd_opcintype
Definition: rel.h:208
Oid * rd_opfamily
Definition: rel.h:207
Oid * rd_indcollation
Definition: rel.h:217

References BTEQUALIMAGE_PROC, DatumGetBool(), DEBUG1, elog, get_opfamily_proc(), i, IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, ObjectIdGetDatum(), OidFunctionCall1Coll(), OidIsValid, RelationData::rd_indcollation, RelationData::rd_opcintype, RelationData::rd_opfamily, and RelationGetRelationName.

Referenced by _bt_leafbuild(), bt_index_check_callback(), and btbuildempty().

◆ _bt_check_natts()

bool _bt_check_natts ( Relation  rel,
bool  heapkeyspace,
Page  page,
OffsetNumber  offnum 
)

Definition at line 964 of file nbtutils.c.

965{
968 BTPageOpaque opaque = BTPageGetOpaque(page);
969 IndexTuple itup;
970 int tupnatts;
971
972 /*
973 * We cannot reliably test a deleted or half-dead page, since they have
974 * dummy high keys
975 */
976 if (P_IGNORE(opaque))
977 return true;
978
979 Assert(offnum >= FirstOffsetNumber &&
980 offnum <= PageGetMaxOffsetNumber(page));
981
982 itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
983 tupnatts = BTreeTupleGetNAtts(itup, rel);
984
985 /* !heapkeyspace indexes do not support deduplication */
986 if (!heapkeyspace && BTreeTupleIsPosting(itup))
987 return false;
988
989 /* Posting list tuples should never have "pivot heap TID" bit set */
990 if (BTreeTupleIsPosting(itup) &&
993 return false;
994
995 /* INCLUDE indexes do not support deduplication */
996 if (natts != nkeyatts && BTreeTupleIsPosting(itup))
997 return false;
998
999 if (P_ISLEAF(opaque))
1000 {
1001 if (offnum >= P_FIRSTDATAKEY(opaque))
1002 {
1003 /*
1004 * Non-pivot tuple should never be explicitly marked as a pivot
1005 * tuple
1006 */
1007 if (BTreeTupleIsPivot(itup))
1008 return false;
1009
1010 /*
1011 * Leaf tuples that are not the page high key (non-pivot tuples)
1012 * should never be truncated. (Note that tupnatts must have been
1013 * inferred, even with a posting list tuple, because only pivot
1014 * tuples store tupnatts directly.)
1015 */
1016 return tupnatts == natts;
1017 }
1018 else
1019 {
1020 /*
1021 * Rightmost page doesn't contain a page high key, so tuple was
1022 * checked above as ordinary leaf tuple
1023 */
1024 Assert(!P_RIGHTMOST(opaque));
1025
1026 /*
1027 * !heapkeyspace high key tuple contains only key attributes. Note
1028 * that tupnatts will only have been explicitly represented in
1029 * !heapkeyspace indexes that happen to have non-key attributes.
1030 */
1031 if (!heapkeyspace)
1032 return tupnatts == nkeyatts;
1033
1034 /* Use generic heapkeyspace pivot tuple handling */
1035 }
1036 }
1037 else /* !P_ISLEAF(opaque) */
1038 {
1039 if (offnum == P_FIRSTDATAKEY(opaque))
1040 {
1041 /*
1042 * The first tuple on any internal page (possibly the first after
1043 * its high key) is its negative infinity tuple. Negative
1044 * infinity tuples are always truncated to zero attributes. They
1045 * are a particular kind of pivot tuple.
1046 */
1047 if (heapkeyspace)
1048 return tupnatts == 0;
1049
1050 /*
1051 * The number of attributes won't be explicitly represented if the
1052 * negative infinity tuple was generated during a page split that
1053 * occurred with a version of Postgres before v11. There must be
1054 * a problem when there is an explicit representation that is
1055 * non-zero, or when there is no explicit representation and the
1056 * tuple is evidently not a pre-pg_upgrade tuple.
1057 *
1058 * Prior to v11, downlinks always had P_HIKEY as their offset.
1059 * Accept that as an alternative indication of a valid
1060 * !heapkeyspace negative infinity tuple.
1061 */
1062 return tupnatts == 0 ||
1064 }
1065 else
1066 {
1067 /*
1068 * !heapkeyspace downlink tuple with separator key contains only
1069 * key attributes. Note that tupnatts will only have been
1070 * explicitly represented in !heapkeyspace indexes that happen to
1071 * have non-key attributes.
1072 */
1073 if (!heapkeyspace)
1074 return tupnatts == nkeyatts;
1075
1076 /* Use generic heapkeyspace pivot tuple handling */
1077 }
1078 }
1079
1080 /* Handle heapkeyspace pivot tuples (excluding minus infinity items) */
1081 Assert(heapkeyspace);
1082
1083 /*
1084 * Explicit representation of the number of attributes is mandatory with
1085 * heapkeyspace index pivot tuples, regardless of whether or not there are
1086 * non-key attributes.
1087 */
1088 if (!BTreeTupleIsPivot(itup))
1089 return false;
1090
1091 /* Pivot tuple should not use posting list representation (redundant) */
1092 if (BTreeTupleIsPosting(itup))
1093 return false;
1094
1095 /*
1096 * Heap TID is a tiebreaker key attribute, so it cannot be untruncated
1097 * when any other key attribute is truncated
1098 */
1099 if (BTreeTupleGetHeapTID(itup) != NULL && tupnatts != nkeyatts)
1100 return false;
1101
1102 /*
1103 * Pivot tuple must have at least one untruncated key attribute (minus
1104 * infinity pivot tuples are the only exception). Pivot tuples can never
1105 * represent that there is a value present for a key attribute that
1106 * exceeds pg_index.indnkeyatts for the index.
1107 */
1108 return tupnatts > 0 && tupnatts <= nkeyatts;
1109}
static void * PageGetItem(const PageData *page, const ItemIdData *itemId)
Definition: bufpage.h:353
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:243
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition: bufpage.h:371
int16_t int16
Definition: c.h:547
Assert(PointerIsAligned(start, uint64))
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static OffsetNumber ItemPointerGetOffsetNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:114
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BT_PIVOT_HEAP_TID_ATTR
Definition: nbtree.h:466
static bool BTreeTupleIsPivot(IndexTuple itup)
Definition: nbtree.h:481
#define P_ISLEAF(opaque)
Definition: nbtree.h:221
#define P_HIKEY
Definition: nbtree.h:368
#define BTPageGetOpaque(page)
Definition: nbtree.h:74
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:370
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:220
#define P_IGNORE(opaque)
Definition: nbtree.h:226
static bool BTreeTupleIsPosting(IndexTuple itup)
Definition: nbtree.h:493
static ItemPointer BTreeTupleGetHeapTID(IndexTuple itup)
Definition: nbtree.h:639
#define BTreeTupleGetNAtts(itup, rel)
Definition: nbtree.h:578
#define FirstOffsetNumber
Definition: off.h:27
ItemPointerData t_tid
Definition: itup.h:37

References Assert(), BT_PIVOT_HEAP_TID_ATTR, BTPageGetOpaque, BTreeTupleGetHeapTID(), BTreeTupleGetNAtts, BTreeTupleIsPivot(), BTreeTupleIsPosting(), FirstOffsetNumber, IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, ItemPointerGetOffsetNumber(), ItemPointerGetOffsetNumberNoCheck(), P_FIRSTDATAKEY, P_HIKEY, P_IGNORE, P_ISLEAF, P_RIGHTMOST, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), and IndexTupleData::t_tid.

Referenced by _bt_compare(), and bt_target_page_check().

◆ _bt_check_third_page()

void _bt_check_third_page ( Relation  rel,
Relation  heap,
bool  needheaptidspace,
Page  page,
IndexTuple  newtup 
)

Definition at line 1124 of file nbtutils.c.

1126{
1127 Size itemsz;
1128 BTPageOpaque opaque;
1129
1130 itemsz = MAXALIGN(IndexTupleSize(newtup));
1131
1132 /* Double check item size against limit */
1133 if (itemsz <= BTMaxItemSize)
1134 return;
1135
1136 /*
1137 * Tuple is probably too large to fit on page, but it's possible that the
1138 * index uses version 2 or version 3, or that page is an internal page, in
1139 * which case a slightly higher limit applies.
1140 */
1141 if (!needheaptidspace && itemsz <= BTMaxItemSizeNoHeapTid)
1142 return;
1143
1144 /*
1145 * Internal page insertions cannot fail here, because that would mean that
1146 * an earlier leaf level insertion that should have failed didn't
1147 */
1148 opaque = BTPageGetOpaque(page);
1149 if (!P_ISLEAF(opaque))
1150 elog(ERROR, "cannot insert oversized tuple of size %zu on internal page of index \"%s\"",
1151 itemsz, RelationGetRelationName(rel));
1152
1153 ereport(ERROR,
1154 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1155 errmsg("index row size %zu exceeds btree version %u maximum %zu for index \"%s\"",
1156 itemsz,
1157 needheaptidspace ? BTREE_VERSION : BTREE_NOVAC_VERSION,
1158 needheaptidspace ? BTMaxItemSize : BTMaxItemSizeNoHeapTid,
1160 errdetail("Index row references tuple (%u,%u) in relation \"%s\".",
1164 errhint("Values larger than 1/3 of a buffer page cannot be indexed.\n"
1165 "Consider a function index of an MD5 hash of the value, "
1166 "or use full text indexing."),
1168}
#define MAXALIGN(LEN)
Definition: c.h:824
size_t Size
Definition: c.h:624
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errhint(const char *fmt,...)
Definition: elog.c:1330
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
static Size IndexTupleSize(const IndexTupleData *itup)
Definition: itup.h:71
#define BTREE_VERSION
Definition: nbtree.h:151
#define BTREE_NOVAC_VERSION
Definition: nbtree.h:153
#define BTMaxItemSizeNoHeapTid
Definition: nbtree.h:170
#define BTMaxItemSize
Definition: nbtree.h:165
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:6103

References BTMaxItemSize, BTMaxItemSizeNoHeapTid, BTPageGetOpaque, BTREE_NOVAC_VERSION, BTREE_VERSION, BTreeTupleGetHeapTID(), elog, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, errtableconstraint(), IndexTupleSize(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), MAXALIGN, P_ISLEAF, and RelationGetRelationName.

Referenced by _bt_buildadd(), and _bt_findinsertloc().

◆ _bt_compare_int()

static int _bt_compare_int ( const void *  va,
const void *  vb 
)
static

Definition at line 167 of file nbtutils.c.

168{
169 int a = *((const int *) va);
170 int b = *((const int *) vb);
171
172 return pg_cmp_s32(a, b);
173}
static int pg_cmp_s32(int32 a, int32 b)
Definition: int.h:713
int b
Definition: isn.c:74
int a
Definition: isn.c:73

References a, b, and pg_cmp_s32().

Referenced by _bt_killitems().

◆ _bt_end_vacuum()

void _bt_end_vacuum ( Relation  rel)

Definition at line 526 of file nbtutils.c.

527{
528 int i;
529
530 LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
531
532 /* Find the array entry */
533 for (i = 0; i < btvacinfo->num_vacuums; i++)
534 {
536
537 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
538 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
539 {
540 /* Remove it by shifting down the last entry */
543 break;
544 }
545 }
546
547 LWLockRelease(BtreeVacuumLock);
548}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_EXCLUSIVE
Definition: lwlock.h:112
static BTVacInfo * btvacinfo
Definition: nbtutils.c:422
LockRelId relid
Definition: nbtutils.c:410
int num_vacuums
Definition: nbtutils.c:417
BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtutils.c:419
LockRelId lockRelId
Definition: rel.h:46
Oid relId
Definition: rel.h:40
Oid dbId
Definition: rel.h:41
LockInfoData rd_lockInfo
Definition: rel.h:114

References btvacinfo, LockRelId::dbId, i, LockInfoData::lockRelId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), BTVacInfo::num_vacuums, RelationData::rd_lockInfo, BTOneVacInfo::relid, LockRelId::relId, and BTVacInfo::vacuums.

Referenced by _bt_end_vacuum_callback(), and btbulkdelete().

◆ _bt_end_vacuum_callback()

void _bt_end_vacuum_callback ( int  code,
Datum  arg 
)

Definition at line 554 of file nbtutils.c.

555{
557}
void _bt_end_vacuum(Relation rel)
Definition: nbtutils.c:526
void * arg
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322

References _bt_end_vacuum(), arg, and DatumGetPointer().

Referenced by btbulkdelete().

◆ _bt_freestack()

void _bt_freestack ( BTStack  stack)

Definition at line 151 of file nbtutils.c.

152{
153 BTStack ostack;
154
155 while (stack != NULL)
156 {
157 ostack = stack;
158 stack = stack->bts_parent;
159 pfree(ostack);
160 }
161}
void pfree(void *pointer)
Definition: mcxt.c:1594
struct BTStackData * bts_parent
Definition: nbtree.h:747

References BTStackData::bts_parent, and pfree().

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

◆ _bt_keep_natts()

static int _bt_keep_natts ( Relation  rel,
IndexTuple  lastleft,
IndexTuple  firstright,
BTScanInsert  itup_key 
)
static

Definition at line 843 of file nbtutils.c.

845{
846 int nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
847 TupleDesc itupdesc = RelationGetDescr(rel);
848 int keepnatts;
849 ScanKey scankey;
850
851 /*
852 * _bt_compare() treats truncated key attributes as having the value minus
853 * infinity, which would break searches within !heapkeyspace indexes. We
854 * must still truncate away non-key attribute values, though.
855 */
856 if (!itup_key->heapkeyspace)
857 return nkeyatts;
858
859 scankey = itup_key->scankeys;
860 keepnatts = 1;
861 for (int attnum = 1; attnum <= nkeyatts; attnum++, scankey++)
862 {
863 Datum datum1,
864 datum2;
865 bool isNull1,
866 isNull2;
867
868 datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
869 datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
870
871 if (isNull1 != isNull2)
872 break;
873
874 if (!isNull1 &&
876 scankey->sk_collation,
877 datum1,
878 datum2)) != 0)
879 break;
880
881 keepnatts++;
882 }
883
884 /*
885 * Assert that _bt_keep_natts_fast() agrees with us in passing. This is
886 * expected in an allequalimage index.
887 */
888 Assert(!itup_key->allequalimage ||
889 keepnatts == _bt_keep_natts_fast(rel, lastleft, firstright));
890
891 return keepnatts;
892}
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1150
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: itup.h:131
int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
Definition: nbtutils.c:917
int16 attnum
Definition: pg_attribute.h:74
uint64_t Datum
Definition: postgres.h:70
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:212
#define RelationGetDescr(relation)
Definition: rel.h:541
bool allequalimage
Definition: nbtree.h:798
bool heapkeyspace
Definition: nbtree.h:797
ScanKeyData scankeys[INDEX_MAX_KEYS]
Definition: nbtree.h:804
FmgrInfo sk_func
Definition: skey.h:71
Oid sk_collation
Definition: skey.h:70

References _bt_keep_natts_fast(), BTScanInsertData::allequalimage, Assert(), attnum, DatumGetInt32(), FunctionCall2Coll(), BTScanInsertData::heapkeyspace, index_getattr(), IndexRelationGetNumberOfKeyAttributes, RelationGetDescr, BTScanInsertData::scankeys, ScanKeyData::sk_collation, and ScanKeyData::sk_func.

Referenced by _bt_truncate().

◆ _bt_keep_natts_fast()

int _bt_keep_natts_fast ( Relation  rel,
IndexTuple  lastleft,
IndexTuple  firstright 
)

Definition at line 917 of file nbtutils.c.

918{
919 TupleDesc itupdesc = RelationGetDescr(rel);
921 int keepnatts;
922
923 keepnatts = 1;
924 for (int attnum = 1; attnum <= keysz; attnum++)
925 {
926 Datum datum1,
927 datum2;
928 bool isNull1,
929 isNull2;
930 CompactAttribute *att;
931
932 datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
933 datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
934 att = TupleDescCompactAttr(itupdesc, attnum - 1);
935
936 if (isNull1 != isNull2)
937 break;
938
939 if (!isNull1 &&
940 !datum_image_eq(datum1, datum2, att->attbyval, att->attlen))
941 break;
942
943 keepnatts++;
944 }
945
946 return keepnatts;
947}
bool datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:266
int16 attlen
Definition: tupdesc.h:71
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:175

References CompactAttribute::attbyval, CompactAttribute::attlen, attnum, datum_image_eq(), index_getattr(), IndexRelationGetNumberOfKeyAttributes, RelationGetDescr, and TupleDescCompactAttr().

Referenced by _bt_afternewitemoff(), _bt_bottomupdel_pass(), _bt_dedup_pass(), _bt_do_singleval(), _bt_keep_natts(), _bt_load(), _bt_set_startikey(), _bt_split_penalty(), and _bt_strategy().

◆ _bt_killitems()

void _bt_killitems ( IndexScanDesc  scan)

Definition at line 205 of file nbtutils.c.

206{
207 Relation rel = scan->indexRelation;
208 BTScanOpaque so = (BTScanOpaque) scan->opaque;
209 Page page;
210 BTPageOpaque opaque;
211 OffsetNumber minoff;
212 OffsetNumber maxoff;
213 int numKilled = so->numKilled;
214 bool killedsomething = false;
215 Buffer buf;
216
217 Assert(numKilled > 0);
219 Assert(scan->heapRelation != NULL); /* can't be a bitmap index scan */
220
221 /* Always invalidate so->killedItems[] before leaving so->currPos */
222 so->numKilled = 0;
223
224 /*
225 * We need to iterate through so->killedItems[] in leaf page order; the
226 * loop below expects this (when marking posting list tuples, at least).
227 * so->killedItems[] is now in whatever order the scan returned items in.
228 * Scrollable cursor scans might have even saved the same item/TID twice.
229 *
230 * Sort and unique-ify so->killedItems[] to deal with all this.
231 */
232 if (numKilled > 1)
233 {
234 qsort(so->killedItems, numKilled, sizeof(int), _bt_compare_int);
235 numKilled = qunique(so->killedItems, numKilled, sizeof(int),
237 }
238
239 if (!so->dropPin)
240 {
241 /*
242 * We have held the pin on this page since we read the index tuples,
243 * so all we need to do is lock it. The pin will have prevented
244 * concurrent VACUUMs from recycling any of the TIDs on the page.
245 */
247 buf = so->currPos.buf;
248 _bt_lockbuf(rel, buf, BT_READ);
249 }
250 else
251 {
252 XLogRecPtr latestlsn;
253
257
258 latestlsn = BufferGetLSNAtomic(buf);
259 Assert(so->currPos.lsn <= latestlsn);
260 if (so->currPos.lsn != latestlsn)
261 {
262 /* Modified, give up on hinting */
263 _bt_relbuf(rel, buf);
264 return;
265 }
266
267 /* Unmodified, hinting is safe */
268 }
269
270 page = BufferGetPage(buf);
271 opaque = BTPageGetOpaque(page);
272 minoff = P_FIRSTDATAKEY(opaque);
273 maxoff = PageGetMaxOffsetNumber(page);
274
275 /* Iterate through so->killedItems[] in leaf page order */
276 for (int i = 0; i < numKilled; i++)
277 {
278 int itemIndex = so->killedItems[i];
279 BTScanPosItem *kitem = &so->currPos.items[itemIndex];
280 OffsetNumber offnum = kitem->indexOffset;
281
282 Assert(itemIndex >= so->currPos.firstItem &&
283 itemIndex <= so->currPos.lastItem);
284 Assert(i == 0 ||
285 offnum >= so->currPos.items[so->killedItems[i - 1]].indexOffset);
286
287 if (offnum < minoff)
288 continue; /* pure paranoia */
289 while (offnum <= maxoff)
290 {
291 ItemId iid = PageGetItemId(page, offnum);
292 IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
293 bool killtuple = false;
294
295 if (BTreeTupleIsPosting(ituple))
296 {
297 int pi = i + 1;
298 int nposting = BTreeTupleGetNPosting(ituple);
299 int j;
300
301 /*
302 * Note that the page may have been modified in almost any way
303 * since we first read it (in the !so->dropPin case), so it's
304 * possible that this posting list tuple wasn't a posting list
305 * tuple when we first encountered its heap TIDs.
306 */
307 for (j = 0; j < nposting; j++)
308 {
309 ItemPointer item = BTreeTupleGetPostingN(ituple, j);
310
311 if (!ItemPointerEquals(item, &kitem->heapTid))
312 break; /* out of posting list loop */
313
314 /*
315 * kitem must have matching offnum when heap TIDs match,
316 * though only in the common case where the page can't
317 * have been concurrently modified
318 */
319 Assert(kitem->indexOffset == offnum || !so->dropPin);
320
321 /*
322 * Read-ahead to later kitems here.
323 *
324 * We rely on the assumption that not advancing kitem here
325 * will prevent us from considering the posting list tuple
326 * fully dead by not matching its next heap TID in next
327 * loop iteration.
328 *
329 * If, on the other hand, this is the final heap TID in
330 * the posting list tuple, then tuple gets killed
331 * regardless (i.e. we handle the case where the last
332 * kitem is also the last heap TID in the last index tuple
333 * correctly -- posting tuple still gets killed).
334 */
335 if (pi < numKilled)
336 kitem = &so->currPos.items[so->killedItems[pi++]];
337 }
338
339 /*
340 * Don't bother advancing the outermost loop's int iterator to
341 * avoid processing killed items that relate to the same
342 * offnum/posting list tuple. This micro-optimization hardly
343 * seems worth it. (Further iterations of the outermost loop
344 * will fail to match on this same posting list's first heap
345 * TID instead, so we'll advance to the next offnum/index
346 * tuple pretty quickly.)
347 */
348 if (j == nposting)
349 killtuple = true;
350 }
351 else if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
352 killtuple = true;
353
354 /*
355 * Mark index item as dead, if it isn't already. Since this
356 * happens while holding a buffer lock possibly in shared mode,
357 * it's possible that multiple processes attempt to do this
358 * simultaneously, leading to multiple full-page images being sent
359 * to WAL (if wal_log_hints or data checksums are enabled), which
360 * is undesirable.
361 */
362 if (killtuple && !ItemIdIsDead(iid))
363 {
364 /* found the item/all posting list items */
365 ItemIdMarkDead(iid);
366 killedsomething = true;
367 break; /* out of inner search loop */
368 }
369 offnum = OffsetNumberNext(offnum);
370 }
371 }
372
373 /*
374 * Since this can be redone later if needed, mark as dirty hint.
375 *
376 * Whenever we mark anything LP_DEAD, we also set the page's
377 * BTP_HAS_GARBAGE flag, which is likewise just a hint. (Note that we
378 * only rely on the page-level flag in !heapkeyspace indexes.)
379 */
380 if (killedsomething)
381 {
382 opaque->btpo_flags |= BTP_HAS_GARBAGE;
384 }
385
386 if (!so->dropPin)
387 _bt_unlockbuf(rel, buf);
388 else
389 _bt_relbuf(rel, buf);
390}
int Buffer
Definition: buf.h:23
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:4499
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:5430
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:436
PageData * Page
Definition: bufpage.h:81
int j
Definition: isn.c:78
#define ItemIdMarkDead(itemId)
Definition: itemid.h:179
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
bool ItemPointerEquals(const ItemPointerData *pointer1, const ItemPointerData *pointer2)
Definition: itemptr.c:35
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:1024
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
Definition: nbtpage.c:846
void _bt_unlockbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:1071
void _bt_lockbuf(Relation rel, Buffer buf, int access)
Definition: nbtpage.c:1040
#define BTScanPosIsPinned(scanpos)
Definition: nbtree.h:1004
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
Definition: nbtree.h:519
#define BTP_HAS_GARBAGE
Definition: nbtree.h:83
#define BTScanPosIsValid(scanpos)
Definition: nbtree.h:1021
static ItemPointer BTreeTupleGetPostingN(IndexTuple posting, int n)
Definition: nbtree.h:545
#define BT_READ
Definition: nbtree.h:730
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:1097
static int _bt_compare_int(const void *va, const void *vb)
Definition: nbtutils.c:167
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
static char buf[DEFAULT_XLOG_SEG_SIZE]
Definition: pg_test_fsync.c:71
#define qsort(a, b, c, d)
Definition: port.h:499
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
#define RelationNeedsWAL(relation)
Definition: rel.h:638
BTScanPosData currPos
Definition: nbtree.h:1093
int * killedItems
Definition: nbtree.h:1071
Buffer buf
Definition: nbtree.h:964
BlockNumber currPage
Definition: nbtree.h:967
int firstItem
Definition: nbtree.h:995
BTScanPosItem items[MaxTIDsPerBTreePage]
Definition: nbtree.h:999
XLogRecPtr lsn
Definition: nbtree.h:970
ItemPointerData heapTid
Definition: nbtree.h:957
OffsetNumber indexOffset
Definition: nbtree.h:958
Relation indexRelation
Definition: relscan.h:139
Relation heapRelation
Definition: relscan.h:138
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References _bt_compare_int(), _bt_getbuf(), _bt_lockbuf(), _bt_relbuf(), _bt_unlockbuf(), Assert(), BT_READ, BTP_HAS_GARBAGE, BTPageGetOpaque, BTreeTupleGetNPosting(), BTreeTupleGetPostingN(), BTreeTupleIsPosting(), BTScanPosIsPinned, BTScanPosIsValid, buf, BTScanPosData::buf, BufferGetLSNAtomic(), BufferGetPage(), BTScanPosData::currPage, BTScanOpaqueData::currPos, BTScanOpaqueData::dropPin, BTScanPosData::firstItem, IndexScanDescData::heapRelation, BTScanPosItem::heapTid, i, BTScanPosItem::indexOffset, IndexScanDescData::indexRelation, ItemIdIsDead, ItemIdMarkDead, ItemPointerEquals(), BTScanPosData::items, j, BTScanOpaqueData::killedItems, BTScanPosData::lsn, MarkBufferDirtyHint(), BTScanOpaqueData::numKilled, OffsetNumberNext, IndexScanDescData::opaque, P_FIRSTDATAKEY, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), qsort, qunique(), RelationNeedsWAL, and IndexTupleData::t_tid.

Referenced by _bt_steppage(), btendscan(), btrescan(), and btrestrpos().

◆ _bt_mkscankey()

BTScanInsert _bt_mkscankey ( Relation  rel,
IndexTuple  itup 
)

Definition at line 59 of file nbtutils.c.

60{
62 ScanKey skey;
63 TupleDesc itupdesc;
64 int indnkeyatts;
65 int16 *indoption;
66 int tupnatts;
67 int i;
68
69 itupdesc = RelationGetDescr(rel);
71 indoption = rel->rd_indoption;
72 tupnatts = itup ? BTreeTupleGetNAtts(itup, rel) : 0;
73
75
76 /*
77 * We'll execute search using scan key constructed on key columns.
78 * Truncated attributes and non-key attributes are omitted from the final
79 * scan key.
80 */
81 key = palloc(offsetof(BTScanInsertData, scankeys) +
82 sizeof(ScanKeyData) * indnkeyatts);
83 if (itup)
84 _bt_metaversion(rel, &key->heapkeyspace, &key->allequalimage);
85 else
86 {
87 /* Utility statement callers can set these fields themselves */
88 key->heapkeyspace = true;
89 key->allequalimage = false;
90 }
91 key->anynullkeys = false; /* initial assumption */
92 key->nextkey = false; /* usual case, required by btinsert */
93 key->backward = false; /* usual case, required by btinsert */
94 key->keysz = Min(indnkeyatts, tupnatts);
95 key->scantid = key->heapkeyspace && itup ?
96 BTreeTupleGetHeapTID(itup) : NULL;
97 skey = key->scankeys;
98 for (i = 0; i < indnkeyatts; i++)
99 {
100 FmgrInfo *procinfo;
101 Datum arg;
102 bool null;
103 int flags;
104
105 /*
106 * We can use the cached (default) support procs since no cross-type
107 * comparison can be needed.
108 */
109 procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
110
111 /*
112 * Key arguments built from truncated attributes (or when caller
113 * provides no tuple) are defensively represented as NULL values. They
114 * should never be used.
115 */
116 if (i < tupnatts)
117 arg = index_getattr(itup, i + 1, itupdesc, &null);
118 else
119 {
120 arg = (Datum) 0;
121 null = true;
122 }
123 flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
125 flags,
126 (AttrNumber) (i + 1),
129 rel->rd_indcollation[i],
130 procinfo,
131 arg);
132 /* Record if any key attribute is NULL (or truncated) */
133 if (null)
134 key->anynullkeys = true;
135 }
136
137 /*
138 * In NULLS NOT DISTINCT mode, we pretend that there are no null keys, so
139 * that full uniqueness check is done.
140 */
141 if (rel->rd_index->indnullsnotdistinct)
142 key->anynullkeys = false;
143
144 return key;
145}
int16 AttrNumber
Definition: attnum.h:21
#define Min(x, y)
Definition: c.h:1016
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:917
void * palloc(Size size)
Definition: mcxt.c:1365
void _bt_metaversion(Relation rel, bool *heapkeyspace, bool *allequalimage)
Definition: nbtpage.c:740
#define BTORDER_PROC
Definition: nbtree.h:717
#define SK_BT_INDOPTION_SHIFT
Definition: nbtree.h:1115
#define InvalidOid
Definition: postgres_ext.h:37
void ScanKeyEntryInitializeWithInfo(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, FmgrInfo *finfo, Datum argument)
Definition: scankey.c:101
#define SK_ISNULL
Definition: skey.h:115
#define InvalidStrategy
Definition: stratnum.h:24
Definition: fmgr.h:57
int16 * rd_indoption
Definition: rel.h:211
Form_pg_index rd_index
Definition: rel.h:192

References _bt_metaversion(), arg, Assert(), BTORDER_PROC, BTreeTupleGetHeapTID(), BTreeTupleGetNAtts, i, index_getattr(), index_getprocinfo(), IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, InvalidOid, InvalidStrategy, sort-test::key, Min, palloc(), RelationData::rd_indcollation, RelationData::rd_index, RelationData::rd_indoption, RelationGetDescr, ScanKeyEntryInitializeWithInfo(), SK_BT_INDOPTION_SHIFT, and SK_ISNULL.

Referenced by _bt_doinsert(), _bt_leafbuild(), _bt_pagedel(), bt_mkscankey_pivotsearch(), bt_rootdescend(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

◆ _bt_start_vacuum()

BTCycleId _bt_start_vacuum ( Relation  rel)

Definition at line 469 of file nbtutils.c.

470{
471 BTCycleId result;
472 int i;
473 BTOneVacInfo *vac;
474
475 LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
476
477 /*
478 * Assign the next cycle ID, being careful to avoid zero as well as the
479 * reserved high values.
480 */
481 result = ++(btvacinfo->cycle_ctr);
482 if (result == 0 || result > MAX_BT_CYCLE_ID)
483 result = btvacinfo->cycle_ctr = 1;
484
485 /* Let's just make sure there's no entry already for this index */
486 for (i = 0; i < btvacinfo->num_vacuums; i++)
487 {
488 vac = &btvacinfo->vacuums[i];
489 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
490 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
491 {
492 /*
493 * Unlike most places in the backend, we have to explicitly
494 * release our LWLock before throwing an error. This is because
495 * we expect _bt_end_vacuum() to be called before transaction
496 * abort cleanup can run to release LWLocks.
497 */
498 LWLockRelease(BtreeVacuumLock);
499 elog(ERROR, "multiple active vacuums for index \"%s\"",
501 }
502 }
503
504 /* OK, add an entry */
506 {
507 LWLockRelease(BtreeVacuumLock);
508 elog(ERROR, "out of btvacinfo slots");
509 }
511 vac->relid = rel->rd_lockInfo.lockRelId;
512 vac->cycleid = result;
514
515 LWLockRelease(BtreeVacuumLock);
516 return result;
517}
#define MAX_BT_CYCLE_ID
Definition: nbtree.h:94
uint16 BTCycleId
Definition: nbtree.h:30
BTCycleId cycleid
Definition: nbtutils.c:411
BTCycleId cycle_ctr
Definition: nbtutils.c:416
int max_vacuums
Definition: nbtutils.c:418

References btvacinfo, BTVacInfo::cycle_ctr, BTOneVacInfo::cycleid, LockRelId::dbId, elog, ERROR, i, LockInfoData::lockRelId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_BT_CYCLE_ID, BTVacInfo::max_vacuums, BTVacInfo::num_vacuums, RelationData::rd_lockInfo, RelationGetRelationName, BTOneVacInfo::relid, LockRelId::relId, and BTVacInfo::vacuums.

Referenced by btbulkdelete().

◆ _bt_truncate()

IndexTuple _bt_truncate ( Relation  rel,
IndexTuple  lastleft,
IndexTuple  firstright,
BTScanInsert  itup_key 
)

Definition at line 698 of file nbtutils.c.

700{
701 TupleDesc itupdesc = RelationGetDescr(rel);
703 int keepnatts;
704 IndexTuple pivot;
705 IndexTuple tidpivot;
706 ItemPointer pivotheaptid;
707 Size newsize;
708
709 /*
710 * We should only ever truncate non-pivot tuples from leaf pages. It's
711 * never okay to truncate when splitting an internal page.
712 */
713 Assert(!BTreeTupleIsPivot(lastleft) && !BTreeTupleIsPivot(firstright));
714
715 /* Determine how many attributes must be kept in truncated tuple */
716 keepnatts = _bt_keep_natts(rel, lastleft, firstright, itup_key);
717
718#ifdef DEBUG_NO_TRUNCATE
719 /* Force truncation to be ineffective for testing purposes */
720 keepnatts = nkeyatts + 1;
721#endif
722
723 pivot = index_truncate_tuple(itupdesc, firstright,
724 Min(keepnatts, nkeyatts));
725
726 if (BTreeTupleIsPosting(pivot))
727 {
728 /*
729 * index_truncate_tuple() just returns a straight copy of firstright
730 * when it has no attributes to truncate. When that happens, we may
731 * need to truncate away a posting list here instead.
732 */
733 Assert(keepnatts == nkeyatts || keepnatts == nkeyatts + 1);
735 pivot->t_info &= ~INDEX_SIZE_MASK;
736 pivot->t_info |= MAXALIGN(BTreeTupleGetPostingOffset(firstright));
737 }
738
739 /*
740 * If there is a distinguishing key attribute within pivot tuple, we're
741 * done
742 */
743 if (keepnatts <= nkeyatts)
744 {
745 BTreeTupleSetNAtts(pivot, keepnatts, false);
746 return pivot;
747 }
748
749 /*
750 * We have to store a heap TID in the new pivot tuple, since no non-TID
751 * key attribute value in firstright distinguishes the right side of the
752 * split from the left side. nbtree conceptualizes this case as an
753 * inability to truncate away any key attributes, since heap TID is
754 * treated as just another key attribute (despite lacking a pg_attribute
755 * entry).
756 *
757 * Use enlarged space that holds a copy of pivot. We need the extra space
758 * to store a heap TID at the end (using the special pivot tuple
759 * representation). Note that the original pivot already has firstright's
760 * possible posting list/non-key attribute values removed at this point.
761 */
762 newsize = MAXALIGN(IndexTupleSize(pivot)) + MAXALIGN(sizeof(ItemPointerData));
763 tidpivot = palloc0(newsize);
764 memcpy(tidpivot, pivot, MAXALIGN(IndexTupleSize(pivot)));
765 /* Cannot leak memory here */
766 pfree(pivot);
767
768 /*
769 * Store all of firstright's key attribute values plus a tiebreaker heap
770 * TID value in enlarged pivot tuple
771 */
772 tidpivot->t_info &= ~INDEX_SIZE_MASK;
773 tidpivot->t_info |= newsize;
774 BTreeTupleSetNAtts(tidpivot, nkeyatts, true);
775 pivotheaptid = BTreeTupleGetHeapTID(tidpivot);
776
777 /*
778 * Lehman & Yao use lastleft as the leaf high key in all cases, but don't
779 * consider suffix truncation. It seems like a good idea to follow that
780 * example in cases where no truncation takes place -- use lastleft's heap
781 * TID. (This is also the closest value to negative infinity that's
782 * legally usable.)
783 */
784 ItemPointerCopy(BTreeTupleGetMaxHeapTID(lastleft), pivotheaptid);
785
786 /*
787 * We're done. Assert() that heap TID invariants hold before returning.
788 *
789 * Lehman and Yao require that the downlink to the right page, which is to
790 * be inserted into the parent page in the second phase of a page split be
791 * a strict lower bound on items on the right page, and a non-strict upper
792 * bound for items on the left page. Assert that heap TIDs follow these
793 * invariants, since a heap TID value is apparently needed as a
794 * tiebreaker.
795 */
796#ifndef DEBUG_NO_TRUNCATE
798 BTreeTupleGetHeapTID(firstright)) < 0);
799 Assert(ItemPointerCompare(pivotheaptid,
800 BTreeTupleGetHeapTID(lastleft)) >= 0);
801 Assert(ItemPointerCompare(pivotheaptid,
802 BTreeTupleGetHeapTID(firstright)) < 0);
803#else
804
805 /*
806 * Those invariants aren't guaranteed to hold for lastleft + firstright
807 * heap TID attribute values when they're considered here only because
808 * DEBUG_NO_TRUNCATE is defined (a heap TID is probably not actually
809 * needed as a tiebreaker). DEBUG_NO_TRUNCATE must therefore use a heap
810 * TID value that always works as a strict lower bound for items to the
811 * right. In particular, it must avoid using firstright's leading key
812 * attribute values along with lastleft's heap TID value when lastleft's
813 * TID happens to be greater than firstright's TID.
814 */
815 ItemPointerCopy(BTreeTupleGetHeapTID(firstright), pivotheaptid);
816
817 /*
818 * Pivot heap TID should never be fully equal to firstright. Note that
819 * the pivot heap TID will still end up equal to lastleft's heap TID when
820 * that's the only usable value.
821 */
822 ItemPointerSetOffsetNumber(pivotheaptid,
824 Assert(ItemPointerCompare(pivotheaptid,
825 BTreeTupleGetHeapTID(firstright)) < 0);
826#endif
827
828 return tidpivot;
829}
IndexTuple index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source, int leavenatts)
Definition: indextuple.c:576
int32 ItemPointerCompare(const ItemPointerData *arg1, const ItemPointerData *arg2)
Definition: itemptr.c:51
static void ItemPointerSetOffsetNumber(ItemPointerData *pointer, OffsetNumber offsetNumber)
Definition: itemptr.h:158
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
Definition: itemptr.h:172
void * palloc0(Size size)
Definition: mcxt.c:1395
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
Definition: nbtree.h:530
static ItemPointer BTreeTupleGetMaxHeapTID(IndexTuple itup)
Definition: nbtree.h:665
static void BTreeTupleSetNAtts(IndexTuple itup, uint16 nkeyatts, bool heaptid)
Definition: nbtree.h:596
static int _bt_keep_natts(Relation rel, IndexTuple lastleft, IndexTuple firstright, BTScanInsert itup_key)
Definition: nbtutils.c:843
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54
unsigned short t_info
Definition: itup.h:49

References _bt_keep_natts(), Assert(), BTreeTupleGetHeapTID(), BTreeTupleGetMaxHeapTID(), BTreeTupleGetPostingOffset(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), BTreeTupleSetNAtts(), index_truncate_tuple(), IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, IndexTupleSize(), ItemPointerCompare(), ItemPointerCopy(), ItemPointerGetOffsetNumber(), ItemPointerSetOffsetNumber(), MAXALIGN, Min, OffsetNumberPrev, palloc0(), pfree(), RelationGetDescr, and IndexTupleData::t_info.

Referenced by _bt_buildadd(), and _bt_split().

◆ _bt_vacuum_cycleid()

BTCycleId _bt_vacuum_cycleid ( Relation  rel)

Definition at line 435 of file nbtutils.c.

436{
437 BTCycleId result = 0;
438 int i;
439
440 /* Share lock is enough since this is a read-only operation */
441 LWLockAcquire(BtreeVacuumLock, LW_SHARED);
442
443 for (i = 0; i < btvacinfo->num_vacuums; i++)
444 {
446
447 if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
448 vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
449 {
450 result = vac->cycleid;
451 break;
452 }
453 }
454
455 LWLockRelease(BtreeVacuumLock);
456 return result;
457}
@ LW_SHARED
Definition: lwlock.h:113

References btvacinfo, BTOneVacInfo::cycleid, LockRelId::dbId, i, LockInfoData::lockRelId, LW_SHARED, LWLockAcquire(), LWLockRelease(), BTVacInfo::num_vacuums, RelationData::rd_lockInfo, BTOneVacInfo::relid, LockRelId::relId, and BTVacInfo::vacuums.

Referenced by _bt_split().

◆ btbuildphasename()

char * btbuildphasename ( int64  phasenum)

Definition at line 650 of file nbtutils.c.

651{
652 switch (phasenum)
653 {
655 return "initializing";
657 return "scanning table";
659 return "sorting live tuples";
661 return "sorting dead tuples";
663 return "loading tuples in tree";
664 default:
665 return NULL;
666 }
667}
#define PROGRESS_BTREE_PHASE_PERFORMSORT_2
Definition: nbtree.h:1148
#define PROGRESS_BTREE_PHASE_LEAF_LOAD
Definition: nbtree.h:1149
#define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN
Definition: nbtree.h:1146
#define PROGRESS_BTREE_PHASE_PERFORMSORT_1
Definition: nbtree.h:1147
#define PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE
Definition: progress.h:126

References PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN, PROGRESS_BTREE_PHASE_LEAF_LOAD, PROGRESS_BTREE_PHASE_PERFORMSORT_1, PROGRESS_BTREE_PHASE_PERFORMSORT_2, and PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE.

Referenced by bthandler().

◆ btoptions()

bytea * btoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 604 of file nbtutils.c.

605{
606 static const relopt_parse_elt tab[] = {
607 {"fillfactor", RELOPT_TYPE_INT, offsetof(BTOptions, fillfactor)},
608 {"vacuum_cleanup_index_scale_factor", RELOPT_TYPE_REAL,
609 offsetof(BTOptions, vacuum_cleanup_index_scale_factor)},
610 {"deduplicate_items", RELOPT_TYPE_BOOL,
611 offsetof(BTOptions, deduplicate_items)}
612 };
613
614 return (bytea *) build_reloptions(reloptions, validate,
616 sizeof(BTOptions),
617 tab, lengthof(tab));
618}
static bool validate(Port *port, const char *auth)
Definition: auth-oauth.c:638
#define lengthof(array)
Definition: c.h:801
static int fillfactor
Definition: pgbench.c:188
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
Definition: reloptions.c:1954
@ RELOPT_KIND_BTREE
Definition: reloptions.h:44
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
@ RELOPT_TYPE_BOOL
Definition: reloptions.h:31
@ RELOPT_TYPE_REAL
Definition: reloptions.h:33
Definition: c.h:706

References build_reloptions(), fillfactor, lengthof, RELOPT_KIND_BTREE, RELOPT_TYPE_BOOL, RELOPT_TYPE_INT, RELOPT_TYPE_REAL, and validate().

Referenced by bthandler().

◆ btproperty()

bool btproperty ( Oid  index_oid,
int  attno,
IndexAMProperty  prop,
const char *  propname,
bool *  res,
bool *  isnull 
)

Definition at line 627 of file nbtutils.c.

630{
631 switch (prop)
632 {
634 /* answer only for columns, not AM or whole index */
635 if (attno == 0)
636 return false;
637 /* otherwise, btree can always return data */
638 *res = true;
639 return true;
640
641 default:
642 return false; /* punt to generic code */
643 }
644}
@ AMPROP_RETURNABLE
Definition: amapi.h:47

References AMPROP_RETURNABLE.

Referenced by bthandler().

◆ BTreeShmemInit()

void BTreeShmemInit ( void  )

Definition at line 576 of file nbtutils.c.

577{
578 bool found;
579
580 btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
582 &found);
583
585 {
586 /* Initialize shared memory area */
587 Assert(!found);
588
589 /*
590 * It doesn't really matter what the cycle counter starts at, but
591 * having it always start the same doesn't seem good. Seed with
592 * low-order bits of time() instead.
593 */
594 btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
595
598 }
599 else
600 Assert(found);
601}
bool IsUnderPostmaster
Definition: globals.c:120
int MaxBackends
Definition: globals.c:146
Size BTreeShmemSize(void)
Definition: nbtutils.c:563
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:389

References Assert(), BTreeShmemSize(), btvacinfo, BTVacInfo::cycle_ctr, IsUnderPostmaster, BTVacInfo::max_vacuums, MaxBackends, BTVacInfo::num_vacuums, and ShmemInitStruct().

Referenced by CreateOrAttachShmemStructs().

◆ BTreeShmemSize()

Size BTreeShmemSize ( void  )

Definition at line 563 of file nbtutils.c.

564{
565 Size size;
566
567 size = offsetof(BTVacInfo, vacuums);
568 size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
569 return size;
570}
Size add_size(Size s1, Size s2)
Definition: shmem.c:495
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510

References add_size(), MaxBackends, and mul_size().

Referenced by BTreeShmemInit(), and CalculateShmemSize().

Variable Documentation

◆ btvacinfo

BTVacInfo* btvacinfo
static

Definition at line 422 of file nbtutils.c.

Referenced by _bt_end_vacuum(), _bt_start_vacuum(), _bt_vacuum_cycleid(), and BTreeShmemInit().