Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * nbtutils.c
4 : : * Utility code for Postgres btree implementation.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/access/nbtree/nbtutils.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : :
16 : : #include "postgres.h"
17 : :
18 : : #include <time.h>
19 : :
20 : : #include "access/nbtree.h"
21 : : #include "access/reloptions.h"
22 : : #include "access/relscan.h"
23 : : #include "commands/progress.h"
24 : : #include "common/int.h"
25 : : #include "lib/qunique.h"
26 : : #include "miscadmin.h"
27 : : #include "utils/datum.h"
28 : : #include "utils/lsyscache.h"
29 : : #include "utils/rel.h"
30 : :
31 : :
32 : : static int _bt_compare_int(const void *va, const void *vb);
33 : : static int _bt_keep_natts(Relation rel, IndexTuple lastleft,
34 : : IndexTuple firstright, BTScanInsert itup_key);
35 : :
36 : :
37 : : /*
38 : : * _bt_mkscankey
39 : : * Build an insertion scan key that contains comparison data from itup
40 : : * as well as comparator routines appropriate to the key datatypes.
41 : : *
42 : : * The result is intended for use with _bt_compare() and _bt_truncate().
43 : : * Callers that don't need to fill out the insertion scankey arguments
44 : : * (e.g. they use an ad-hoc comparison routine, or only need a scankey
45 : : * for _bt_truncate()) can pass a NULL index tuple. The scankey will
46 : : * be initialized as if an "all truncated" pivot tuple was passed
47 : : * instead.
48 : : *
49 : : * Note that we may occasionally have to share lock the metapage to
50 : : * determine whether or not the keys in the index are expected to be
51 : : * unique (i.e. if this is a "heapkeyspace" index). We assume a
52 : : * heapkeyspace index when caller passes a NULL tuple, allowing index
53 : : * build callers to avoid accessing the non-existent metapage. We
54 : : * also assume that the index is _not_ allequalimage when a NULL tuple
55 : : * is passed; CREATE INDEX callers call _bt_allequalimage() to set the
56 : : * field themselves.
57 : : */
58 : : BTScanInsert
1009 pg@bowt.ie 59 :CBC 7785000 : _bt_mkscankey(Relation rel, IndexTuple itup)
60 : : {
61 : : BTScanInsert key;
62 : : ScanKey skey;
63 : : TupleDesc itupdesc;
64 : : int indnkeyatts;
65 : : int16 *indoption;
66 : : int tupnatts;
67 : : int i;
68 : :
10057 bruce@momjian.us 69 :GNC 7785000 : itupdesc = RelationGetDescr(rel);
2899 teodor@sigaev.ru 70 : 7785000 : indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
7005 tgl@sss.pgh.pa.us 71 : 7785000 : indoption = rel->rd_indoption;
2552 pg@bowt.ie 72 [ + + + + ]: 7785000 : tupnatts = itup ? BTreeTupleGetNAtts(itup, rel) : 0;
73 : :
74 [ - + ]: 7785000 : Assert(tupnatts <= IndexRelationGetNumberOfAttributes(rel));
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 : 7785000 : key = palloc(offsetof(BTScanInsertData, scankeys) +
82 : 7785000 : sizeof(ScanKeyData) * indnkeyatts);
2209 83 [ + + ]: 7785000 : if (itup)
1009 84 : 7711003 : _bt_metaversion(rel, &key->heapkeyspace, &key->allequalimage);
85 : : else
86 : : {
87 : : /* Utility statement callers can set these fields themselves */
2209 88 : 73997 : key->heapkeyspace = true;
89 : 73997 : key->allequalimage = false;
90 : : }
2489 tgl@sss.pgh.pa.us 91 : 7785000 : key->anynullkeys = false; /* initial assumption */
828 pg@bowt.ie 92 : 7785000 : key->nextkey = false; /* usual case, required by btinsert */
93 : 7785000 : key->backward = false; /* usual case, required by btinsert */
2552 94 : 7785000 : key->keysz = Min(indnkeyatts, tupnatts);
95 [ + + ]: 7785000 : key->scantid = key->heapkeyspace && itup ?
96 [ + - ]: 15570000 : BTreeTupleGetHeapTID(itup) : NULL;
97 : 7785000 : skey = key->scankeys;
2899 teodor@sigaev.ru 98 [ + + ]: 20004435 : 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 : : */
8926 tgl@sss.pgh.pa.us 109 : 12219435 : 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 : : */
2552 pg@bowt.ie 116 [ + + ]: 12219435 : if (i < tupnatts)
117 : 12087453 : arg = index_getattr(itup, i + 1, itupdesc, &null);
118 : : else
119 : : {
120 : 131982 : arg = (Datum) 0;
121 : 131982 : null = true;
122 : : }
123 : 12219435 : flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
8926 tgl@sss.pgh.pa.us 124 : 12219435 : ScanKeyEntryInitializeWithInfo(&skey[i],
125 : : flags,
126 : 12219435 : (AttrNumber) (i + 1),
127 : : InvalidStrategy,
128 : : InvalidOid,
5451 129 : 12219435 : rel->rd_indcollation[i],
130 : : procinfo,
131 : : arg);
132 : : /* Record if any key attribute is NULL (or truncated) */
2518 pg@bowt.ie 133 [ + + ]: 12219435 : if (null)
134 : 142306 : 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 : : */
1501 peter@eisentraut.org 141 [ + + ]: 7785000 : if (rel->rd_index->indnullsnotdistinct)
142 : 93 : key->anynullkeys = false;
143 : :
2552 pg@bowt.ie 144 : 7785000 : return key;
145 : : }
146 : :
147 : : /*
148 : : * qsort comparison function for int arrays
149 : : */
150 : : static int
95 151 : 269274 : _bt_compare_int(const void *va, const void *vb)
152 : : {
153 : 269274 : int a = *((const int *) va);
154 : 269274 : int b = *((const int *) vb);
155 : :
156 : 269274 : return pg_cmp_s32(a, b);
157 : : }
158 : :
159 : : /*
160 : : * _bt_killitems - set LP_DEAD state for items an indexscan caller has
161 : : * told us were killed
162 : : *
163 : : * scan->opaque, referenced locally through so, contains information about the
164 : : * current page and killed tuples thereon (generally, this should only be
165 : : * called if so->numKilled > 0).
166 : : *
167 : : * Caller should not have a lock on the so->currPos page, but must hold a
168 : : * buffer pin when !so->dropPin. When we return, it still won't be locked.
169 : : * It'll continue to hold whatever pins were held before calling here.
170 : : *
171 : : * We match items by heap TID before assuming they are the right ones to set
172 : : * LP_DEAD. If the scan is one that holds a buffer pin on the target page
173 : : * continuously from initially reading the items until applying this function
174 : : * (if it is a !so->dropPin scan), VACUUM cannot have deleted any items on the
175 : : * page, so the page's TIDs can't have been recycled by now. There's no risk
176 : : * that we'll confuse a new index tuple that happens to use a recycled TID
177 : : * with a now-removed tuple with the same TID (that used to be on this same
178 : : * page). We can't rely on that during scans that drop buffer pins eagerly
179 : : * (so->dropPin scans), though, so we must condition setting LP_DEAD bits on
180 : : * the page LSN having not changed since back when _bt_readpage saw the page.
181 : : * We totally give up on setting LP_DEAD bits when the page LSN changed.
182 : : *
183 : : * We give up much less often during !so->dropPin scans, but it still happens.
184 : : * We cope with cases where items have moved right due to insertions. If an
185 : : * item has moved off the current page due to a split, we'll fail to find it
186 : : * and just give up on it.
187 : : */
188 : : void
4008 kgrittn@postgresql.o 189 :CBC 87178 : _bt_killitems(IndexScanDesc scan)
190 : : {
282 pg@bowt.ie 191 : 87178 : Relation rel = scan->indexRelation;
7252 tgl@sss.pgh.pa.us 192 : 87178 : BTScanOpaque so = (BTScanOpaque) scan->opaque;
193 : : Page page;
194 : : BTPageOpaque opaque;
195 : : OffsetNumber minoff;
196 : : OffsetNumber maxoff;
4008 kgrittn@postgresql.o 197 : 87178 : int numKilled = so->numKilled;
7252 tgl@sss.pgh.pa.us 198 : 87178 : bool killedsomething = false;
199 : : Buffer buf;
200 : :
282 pg@bowt.ie 201 [ - + ]: 87178 : Assert(numKilled > 0);
4008 kgrittn@postgresql.o 202 [ - + - - : 87178 : Assert(BTScanPosIsValid(so->currPos));
- + ]
282 pg@bowt.ie 203 [ - + ]: 87178 : Assert(scan->heapRelation != NULL); /* can't be a bitmap index scan */
204 : :
205 : : /* Always invalidate so->killedItems[] before leaving so->currPos */
4008 kgrittn@postgresql.o 206 : 87178 : so->numKilled = 0;
207 : :
208 : : /*
209 : : * We need to iterate through so->killedItems[] in leaf page order; the
210 : : * loop below expects this (when marking posting list tuples, at least).
211 : : * so->killedItems[] is now in whatever order the scan returned items in.
212 : : * Scrollable cursor scans might have even saved the same item/TID twice.
213 : : *
214 : : * Sort and unique-ify so->killedItems[] to deal with all this.
215 : : */
95 pg@bowt.ie 216 [ + + ]:GNC 87178 : if (numKilled > 1)
217 : : {
218 : 8419 : qsort(so->killedItems, numKilled, sizeof(int), _bt_compare_int);
219 : 8419 : numKilled = qunique(so->killedItems, numKilled, sizeof(int),
220 : : _bt_compare_int);
221 : : }
222 : :
282 pg@bowt.ie 223 [ + + ]:CBC 87178 : if (!so->dropPin)
224 : : {
225 : : /*
226 : : * We have held the pin on this page since we read the index tuples,
227 : : * so all we need to do is lock it. The pin will have prevented
228 : : * concurrent VACUUMs from recycling any of the TIDs on the page.
229 : : */
230 [ - + - - : 19616 : Assert(BTScanPosIsPinned(so->currPos));
- + ]
277 231 : 19616 : buf = so->currPos.buf;
232 : 19616 : _bt_lockbuf(rel, buf, BT_READ);
233 : : }
234 : : else
235 : : {
236 : : XLogRecPtr latestlsn;
237 : :
282 238 [ - + - - : 67562 : Assert(!BTScanPosIsPinned(so->currPos));
- + ]
239 : 67562 : buf = _bt_getbuf(rel, so->currPos.currPage, BT_READ);
240 : :
241 : 67562 : latestlsn = BufferGetLSNAtomic(buf);
242 [ - + ]: 67562 : Assert(so->currPos.lsn <= latestlsn);
243 [ + + ]: 67562 : if (so->currPos.lsn != latestlsn)
244 : : {
245 : : /* Modified, give up on hinting */
246 : 66 : _bt_relbuf(rel, buf);
4008 kgrittn@postgresql.o 247 : 66 : return;
248 : : }
249 : :
250 : : /* Unmodified, hinting is safe */
251 : : }
252 : :
277 pg@bowt.ie 253 : 87112 : page = BufferGetPage(buf);
1444 michael@paquier.xyz 254 : 87112 : opaque = BTPageGetOpaque(page);
7252 tgl@sss.pgh.pa.us 255 [ + + ]: 87112 : minoff = P_FIRSTDATAKEY(opaque);
256 : 87112 : maxoff = PageGetMaxOffsetNumber(page);
257 : :
258 : : /* Iterate through so->killedItems[] in leaf page order */
282 pg@bowt.ie 259 [ + + ]: 290380 : for (int i = 0; i < numKilled; i++)
260 : : {
7102 bruce@momjian.us 261 : 203269 : int itemIndex = so->killedItems[i];
262 : 203269 : BTScanPosItem *kitem = &so->currPos.items[itemIndex];
263 : 203269 : OffsetNumber offnum = kitem->indexOffset;
264 : :
7252 tgl@sss.pgh.pa.us 265 [ + - - + ]: 203269 : Assert(itemIndex >= so->currPos.firstItem &&
266 : : itemIndex <= so->currPos.lastItem);
95 pg@bowt.ie 267 [ + + - + ]:GNC 203269 : Assert(i == 0 ||
268 : : offnum >= so->currPos.items[so->killedItems[i - 1]].indexOffset);
269 : :
7252 tgl@sss.pgh.pa.us 270 [ - + ]:CBC 203269 : if (offnum < minoff)
7252 tgl@sss.pgh.pa.us 271 :UBC 0 : continue; /* pure paranoia */
7252 tgl@sss.pgh.pa.us 272 [ + + ]:CBC 4002830 : while (offnum <= maxoff)
273 : : {
274 : 3974167 : ItemId iid = PageGetItemId(page, offnum);
275 : 3974167 : IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
2209 pg@bowt.ie 276 : 3974167 : bool killtuple = false;
277 : :
278 [ + + ]: 3974167 : if (BTreeTupleIsPosting(ituple))
279 : : {
280 : 975423 : int pi = i + 1;
281 : 975423 : int nposting = BTreeTupleGetNPosting(ituple);
282 : : int j;
283 : :
284 : : /*
285 : : * Note that the page may have been modified in almost any way
286 : : * since we first read it (in the !so->dropPin case), so it's
287 : : * possible that this posting list tuple wasn't a posting list
288 : : * tuple when we first encountered its heap TIDs.
289 : : */
290 [ + + ]: 1001570 : for (j = 0; j < nposting; j++)
291 : : {
292 : 1000759 : ItemPointer item = BTreeTupleGetPostingN(ituple, j);
293 : :
294 [ + + ]: 1000759 : if (!ItemPointerEquals(item, &kitem->heapTid))
295 : 974612 : break; /* out of posting list loop */
296 : :
297 : : /*
298 : : * kitem must have matching offnum when heap TIDs match,
299 : : * though only in the common case where the page can't
300 : : * have been concurrently modified
301 : : */
282 302 [ - + - - ]: 26147 : Assert(kitem->indexOffset == offnum || !so->dropPin);
303 : :
304 : : /*
305 : : * Read-ahead to later kitems here.
306 : : *
307 : : * We rely on the assumption that not advancing kitem here
308 : : * will prevent us from considering the posting list tuple
309 : : * fully dead by not matching its next heap TID in next
310 : : * loop iteration.
311 : : *
312 : : * If, on the other hand, this is the final heap TID in
313 : : * the posting list tuple, then tuple gets killed
314 : : * regardless (i.e. we handle the case where the last
315 : : * kitem is also the last heap TID in the last index tuple
316 : : * correctly -- posting tuple still gets killed).
317 : : */
2209 318 [ + + ]: 26147 : if (pi < numKilled)
319 : 9776 : kitem = &so->currPos.items[so->killedItems[pi++]];
320 : : }
321 : :
322 : : /*
323 : : * Don't bother advancing the outermost loop's int iterator to
324 : : * avoid processing killed items that relate to the same
325 : : * offnum/posting list tuple. This micro-optimization hardly
326 : : * seems worth it. (Further iterations of the outermost loop
327 : : * will fail to match on this same posting list's first heap
328 : : * TID instead, so we'll advance to the next offnum/index
329 : : * tuple pretty quickly.)
330 : : */
331 [ + + ]: 975423 : if (j == nposting)
332 : 811 : killtuple = true;
333 : : }
334 [ + + ]: 2998744 : else if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
335 : 174161 : killtuple = true;
336 : :
337 : : /*
338 : : * Mark index item as dead, if it isn't already. Since this
339 : : * happens while holding a buffer lock possibly in shared mode,
340 : : * it's possible that multiple processes attempt to do this
341 : : * simultaneously, leading to multiple full-page images being sent
342 : : * to WAL (if wal_log_hints or data checksums are enabled), which
343 : : * is undesirable.
344 : : */
2130 alvherre@alvh.no-ip. 345 [ + + + + ]: 3974167 : if (killtuple && !ItemIdIsDead(iid))
346 : : {
5 andres@anarazel.de 347 [ + + ]:GNC 174606 : if (!killedsomething)
348 : : {
349 : : /*
350 : : * Use the hint bit infrastructure to check if we can
351 : : * update the page while just holding a share lock. If we
352 : : * are not allowed, there's no point continuing.
353 : : */
354 [ + + ]: 70047 : if (!BufferBeginSetHintBits(buf))
355 : 1 : goto unlock_page;
356 : : }
357 : :
358 : : /* found the item/all posting list items */
6759 tgl@sss.pgh.pa.us 359 :CBC 174605 : ItemIdMarkDead(iid);
7252 360 : 174605 : killedsomething = true;
361 : 174605 : break; /* out of inner search loop */
362 : : }
363 : 3799561 : offnum = OffsetNumberNext(offnum);
364 : : }
365 : : }
366 : :
367 : : /*
368 : : * Since this can be redone later if needed, mark as dirty hint.
369 : : *
370 : : * Whenever we mark anything LP_DEAD, we also set the page's
371 : : * BTP_HAS_GARBAGE flag, which is likewise just a hint. (Note that we
372 : : * only rely on the page-level flag in !heapkeyspace indexes.)
373 : : */
374 [ + + ]: 87111 : if (killedsomething)
375 : : {
7173 376 : 70046 : opaque->btpo_flags |= BTP_HAS_GARBAGE;
5 andres@anarazel.de 377 :GNC 70046 : BufferFinishSetHintBits(buf, true, true);
378 : : }
379 : :
380 : 17065 : unlock_page:
277 pg@bowt.ie 381 [ + + ]:CBC 87112 : if (!so->dropPin)
382 : 19616 : _bt_unlockbuf(rel, buf);
383 : : else
384 : 67496 : _bt_relbuf(rel, buf);
385 : : }
386 : :
387 : :
388 : : /*
389 : : * The following routines manage a shared-memory area in which we track
390 : : * assignment of "vacuum cycle IDs" to currently-active btree vacuuming
391 : : * operations. There is a single counter which increments each time we
392 : : * start a vacuum to assign it a cycle ID. Since multiple vacuums could
393 : : * be active concurrently, we have to track the cycle ID for each active
394 : : * vacuum; this requires at most MaxBackends entries (usually far fewer).
395 : : * We assume at most one vacuum can be active for a given index.
396 : : *
397 : : * Access to the shared memory area is controlled by BtreeVacuumLock.
398 : : * In principle we could use a separate lmgr locktag for each index,
399 : : * but a single LWLock is much cheaper, and given the short time that
400 : : * the lock is ever held, the concurrency hit should be minimal.
401 : : */
402 : :
403 : : typedef struct BTOneVacInfo
404 : : {
405 : : LockRelId relid; /* global identifier of an index */
406 : : BTCycleId cycleid; /* cycle ID for its active VACUUM */
407 : : } BTOneVacInfo;
408 : :
409 : : typedef struct BTVacInfo
410 : : {
411 : : BTCycleId cycle_ctr; /* cycle ID most recently assigned */
412 : : int num_vacuums; /* number of currently active VACUUMs */
413 : : int max_vacuums; /* allocated length of vacuums[] array */
414 : : BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER];
415 : : } BTVacInfo;
416 : :
417 : : static BTVacInfo *btvacinfo;
418 : :
419 : :
420 : : /*
421 : : * _bt_vacuum_cycleid --- get the active vacuum cycle ID for an index,
422 : : * or zero if there is no active VACUUM
423 : : *
424 : : * Note: for correct interlocking, the caller must already hold pin and
425 : : * exclusive lock on each buffer it will store the cycle ID into. This
426 : : * ensures that even if a VACUUM starts immediately afterwards, it cannot
427 : : * process those pages until the page split is complete.
428 : : */
429 : : BTCycleId
7251 tgl@sss.pgh.pa.us 430 : 13241 : _bt_vacuum_cycleid(Relation rel)
431 : : {
432 : 13241 : BTCycleId result = 0;
433 : : int i;
434 : :
435 : : /* Share lock is enough since this is a read-only operation */
436 : 13241 : LWLockAcquire(BtreeVacuumLock, LW_SHARED);
437 : :
438 [ + + ]: 13245 : for (i = 0; i < btvacinfo->num_vacuums; i++)
439 : : {
440 : 6 : BTOneVacInfo *vac = &btvacinfo->vacuums[i];
441 : :
442 [ + + ]: 6 : if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
7251 tgl@sss.pgh.pa.us 443 [ + - ]:GBC 2 : vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
444 : : {
445 : 2 : result = vac->cycleid;
446 : 2 : break;
447 : : }
448 : : }
449 : :
7251 tgl@sss.pgh.pa.us 450 :CBC 13241 : LWLockRelease(BtreeVacuumLock);
451 : 13241 : return result;
452 : : }
453 : :
454 : : /*
455 : : * _bt_start_vacuum --- assign a cycle ID to a just-starting VACUUM operation
456 : : *
457 : : * Note: the caller must guarantee that it will eventually call
458 : : * _bt_end_vacuum, else we'll permanently leak an array slot. To ensure
459 : : * that this happens even in elog(FATAL) scenarios, the appropriate coding
460 : : * is not just a PG_TRY, but
461 : : * PG_ENSURE_ERROR_CLEANUP(_bt_end_vacuum_callback, PointerGetDatum(rel))
462 : : */
463 : : BTCycleId
464 : 1626 : _bt_start_vacuum(Relation rel)
465 : : {
466 : : BTCycleId result;
467 : : int i;
468 : : BTOneVacInfo *vac;
469 : :
470 : 1626 : LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
471 : :
472 : : /*
473 : : * Assign the next cycle ID, being careful to avoid zero as well as the
474 : : * reserved high values.
475 : : */
6915 476 : 1626 : result = ++(btvacinfo->cycle_ctr);
477 [ + - - + ]: 1626 : if (result == 0 || result > MAX_BT_CYCLE_ID)
6915 tgl@sss.pgh.pa.us 478 :UBC 0 : result = btvacinfo->cycle_ctr = 1;
479 : :
480 : : /* Let's just make sure there's no entry already for this index */
7251 tgl@sss.pgh.pa.us 481 [ - + ]:CBC 1626 : for (i = 0; i < btvacinfo->num_vacuums; i++)
482 : : {
7251 tgl@sss.pgh.pa.us 483 :UBC 0 : vac = &btvacinfo->vacuums[i];
484 [ # # ]: 0 : if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
485 [ # # ]: 0 : vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
486 : : {
487 : : /*
488 : : * Unlike most places in the backend, we have to explicitly
489 : : * release our LWLock before throwing an error. This is because
490 : : * we expect _bt_end_vacuum() to be called before transaction
491 : : * abort cleanup can run to release LWLocks.
492 : : */
6925 493 : 0 : LWLockRelease(BtreeVacuumLock);
7251 494 [ # # ]: 0 : elog(ERROR, "multiple active vacuums for index \"%s\"",
495 : : RelationGetRelationName(rel));
496 : : }
497 : : }
498 : :
499 : : /* OK, add an entry */
7251 tgl@sss.pgh.pa.us 500 [ - + ]:CBC 1626 : if (btvacinfo->num_vacuums >= btvacinfo->max_vacuums)
501 : : {
6925 tgl@sss.pgh.pa.us 502 :UBC 0 : LWLockRelease(BtreeVacuumLock);
7251 503 [ # # ]: 0 : elog(ERROR, "out of btvacinfo slots");
504 : : }
7251 tgl@sss.pgh.pa.us 505 :CBC 1626 : vac = &btvacinfo->vacuums[btvacinfo->num_vacuums];
506 : 1626 : vac->relid = rel->rd_lockInfo.lockRelId;
507 : 1626 : vac->cycleid = result;
508 : 1626 : btvacinfo->num_vacuums++;
509 : :
510 : 1626 : LWLockRelease(BtreeVacuumLock);
511 : 1626 : return result;
512 : : }
513 : :
514 : : /*
515 : : * _bt_end_vacuum --- mark a btree VACUUM operation as done
516 : : *
517 : : * Note: this is deliberately coded not to complain if no entry is found;
518 : : * this allows the caller to put PG_TRY around the start_vacuum operation.
519 : : */
520 : : void
521 : 1626 : _bt_end_vacuum(Relation rel)
522 : : {
523 : : int i;
524 : :
525 : 1626 : LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
526 : :
527 : : /* Find the array entry */
528 [ + - ]: 1626 : for (i = 0; i < btvacinfo->num_vacuums; i++)
529 : : {
530 : 1626 : BTOneVacInfo *vac = &btvacinfo->vacuums[i];
531 : :
532 [ + - ]: 1626 : if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
533 [ + - ]: 1626 : vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
534 : : {
535 : : /* Remove it by shifting down the last entry */
536 : 1626 : *vac = btvacinfo->vacuums[btvacinfo->num_vacuums - 1];
537 : 1626 : btvacinfo->num_vacuums--;
538 : 1626 : break;
539 : : }
540 : : }
541 : :
542 : 1626 : LWLockRelease(BtreeVacuumLock);
543 : 1626 : }
544 : :
545 : : /*
546 : : * _bt_end_vacuum wrapped as an on_shmem_exit callback function
547 : : */
548 : : void
6542 tgl@sss.pgh.pa.us 549 :GBC 1 : _bt_end_vacuum_callback(int code, Datum arg)
550 : : {
551 : 1 : _bt_end_vacuum((Relation) DatumGetPointer(arg));
552 : 1 : }
553 : :
554 : : /*
555 : : * BTreeShmemSize --- report amount of shared memory space needed
556 : : */
557 : : Size
7251 tgl@sss.pgh.pa.us 558 :CBC 3297 : BTreeShmemSize(void)
559 : : {
560 : : Size size;
561 : :
4041 562 : 3297 : size = offsetof(BTVacInfo, vacuums);
1433 rhaas@postgresql.org 563 : 3297 : size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
7251 tgl@sss.pgh.pa.us 564 : 3297 : return size;
565 : : }
566 : :
567 : : /*
568 : : * BTreeShmemInit --- initialize this module's shared memory
569 : : */
570 : : void
571 : 1150 : BTreeShmemInit(void)
572 : : {
573 : : bool found;
574 : :
575 : 1150 : btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
576 : : BTreeShmemSize(),
577 : : &found);
578 : :
579 [ + - ]: 1150 : if (!IsUnderPostmaster)
580 : : {
581 : : /* Initialize shared memory area */
582 [ - + ]: 1150 : Assert(!found);
583 : :
584 : : /*
585 : : * It doesn't really matter what the cycle counter starts at, but
586 : : * having it always start the same doesn't seem good. Seed with
587 : : * low-order bits of time() instead.
588 : : */
589 : 1150 : btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
590 : :
591 : 1150 : btvacinfo->num_vacuums = 0;
1433 rhaas@postgresql.org 592 : 1150 : btvacinfo->max_vacuums = MaxBackends;
593 : : }
594 : : else
7251 tgl@sss.pgh.pa.us 595 [ # # ]:UBC 0 : Assert(found);
7251 tgl@sss.pgh.pa.us 596 :CBC 1150 : }
597 : :
598 : : bytea *
3710 599 : 165 : btoptions(Datum reloptions, bool validate)
600 : : {
601 : : static const relopt_parse_elt tab[] = {
602 : : {"fillfactor", RELOPT_TYPE_INT, offsetof(BTOptions, fillfactor)},
603 : : {"vacuum_cleanup_index_scale_factor", RELOPT_TYPE_REAL,
604 : : offsetof(BTOptions, vacuum_cleanup_index_scale_factor)},
605 : : {"deduplicate_items", RELOPT_TYPE_BOOL,
606 : : offsetof(BTOptions, deduplicate_items)}
607 : : };
608 : :
2302 michael@paquier.xyz 609 : 165 : return (bytea *) build_reloptions(reloptions, validate,
610 : : RELOPT_KIND_BTREE,
611 : : sizeof(BTOptions),
612 : : tab, lengthof(tab));
613 : : }
614 : :
615 : : /*
616 : : * btproperty() -- Check boolean properties of indexes.
617 : : *
618 : : * This is optional, but handling AMPROP_RETURNABLE here saves opening the rel
619 : : * to call btcanreturn.
620 : : */
621 : : bool
3501 tgl@sss.pgh.pa.us 622 : 378 : btproperty(Oid index_oid, int attno,
623 : : IndexAMProperty prop, const char *propname,
624 : : bool *res, bool *isnull)
625 : : {
626 [ + + ]: 378 : switch (prop)
627 : : {
628 : 21 : case AMPROP_RETURNABLE:
629 : : /* answer only for columns, not AM or whole index */
630 [ + + ]: 21 : if (attno == 0)
631 : 6 : return false;
632 : : /* otherwise, btree can always return data */
633 : 15 : *res = true;
634 : 15 : return true;
635 : :
636 : 357 : default:
637 : 357 : return false; /* punt to generic code */
638 : : }
639 : : }
640 : :
641 : : /*
642 : : * btbuildphasename() -- Return name of index build phase.
643 : : */
644 : : char *
2539 alvherre@alvh.no-ip. 645 :UBC 0 : btbuildphasename(int64 phasenum)
646 : : {
647 [ # # # # : 0 : switch (phasenum)
# # ]
648 : : {
649 : 0 : case PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE:
650 : 0 : return "initializing";
651 : 0 : case PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN:
652 : 0 : return "scanning table";
653 : 0 : case PROGRESS_BTREE_PHASE_PERFORMSORT_1:
654 : 0 : return "sorting live tuples";
655 : 0 : case PROGRESS_BTREE_PHASE_PERFORMSORT_2:
656 : 0 : return "sorting dead tuples";
657 : 0 : case PROGRESS_BTREE_PHASE_LEAF_LOAD:
658 : 0 : return "loading tuples in tree";
659 : 0 : default:
660 : 0 : return NULL;
661 : : }
662 : : }
663 : :
664 : : /*
665 : : * _bt_truncate() -- create tuple without unneeded suffix attributes.
666 : : *
667 : : * Returns truncated pivot index tuple allocated in caller's memory context,
668 : : * with key attributes copied from caller's firstright argument. If rel is
669 : : * an INCLUDE index, non-key attributes will definitely be truncated away,
670 : : * since they're not part of the key space. More aggressive suffix
671 : : * truncation can take place when it's clear that the returned tuple does not
672 : : * need one or more suffix key attributes. We only need to keep firstright
673 : : * attributes up to and including the first non-lastleft-equal attribute.
674 : : * Caller's insertion scankey is used to compare the tuples; the scankey's
675 : : * argument values are not considered here.
676 : : *
677 : : * Note that returned tuple's t_tid offset will hold the number of attributes
678 : : * present, so the original item pointer offset is not represented. Caller
679 : : * should only change truncated tuple's downlink. Note also that truncated
680 : : * key attributes are treated as containing "minus infinity" values by
681 : : * _bt_compare().
682 : : *
683 : : * In the worst case (when a heap TID must be appended to distinguish lastleft
684 : : * from firstright), the size of the returned tuple is the size of firstright
685 : : * plus the size of an additional MAXALIGN()'d item pointer. This guarantee
686 : : * is important, since callers need to stay under the 1/3 of a page
687 : : * restriction on tuple size. If this routine is ever taught to truncate
688 : : * within an attribute/datum, it will need to avoid returning an enlarged
689 : : * tuple to caller when truncation + TOAST compression ends up enlarging the
690 : : * final datum.
691 : : */
692 : : IndexTuple
2552 pg@bowt.ie 693 :CBC 33714 : _bt_truncate(Relation rel, IndexTuple lastleft, IndexTuple firstright,
694 : : BTScanInsert itup_key)
695 : : {
696 : 33714 : TupleDesc itupdesc = RelationGetDescr(rel);
697 : 33714 : int16 nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
698 : : int keepnatts;
699 : : IndexTuple pivot;
700 : : IndexTuple tidpivot;
701 : : ItemPointer pivotheaptid;
702 : : Size newsize;
703 : :
704 : : /*
705 : : * We should only ever truncate non-pivot tuples from leaf pages. It's
706 : : * never okay to truncate when splitting an internal page.
707 : : */
2209 708 [ + - - + ]: 33714 : Assert(!BTreeTupleIsPivot(lastleft) && !BTreeTupleIsPivot(firstright));
709 : :
710 : : /* Determine how many attributes must be kept in truncated tuple */
2552 711 : 33714 : keepnatts = _bt_keep_natts(rel, lastleft, firstright, itup_key);
712 : :
713 : : #ifdef DEBUG_NO_TRUNCATE
714 : : /* Force truncation to be ineffective for testing purposes */
715 : : keepnatts = nkeyatts + 1;
716 : : #endif
717 : :
2176 718 : 33714 : pivot = index_truncate_tuple(itupdesc, firstright,
719 : : Min(keepnatts, nkeyatts));
720 : :
721 [ + + ]: 33714 : if (BTreeTupleIsPosting(pivot))
722 : : {
723 : : /*
724 : : * index_truncate_tuple() just returns a straight copy of firstright
725 : : * when it has no attributes to truncate. When that happens, we may
726 : : * need to truncate away a posting list here instead.
727 : : */
728 [ + + - + ]: 690 : Assert(keepnatts == nkeyatts || keepnatts == nkeyatts + 1);
729 [ - + ]: 690 : Assert(IndexRelationGetNumberOfAttributes(rel) == nkeyatts);
730 : 690 : pivot->t_info &= ~INDEX_SIZE_MASK;
731 : 690 : pivot->t_info |= MAXALIGN(BTreeTupleGetPostingOffset(firstright));
732 : : }
733 : :
734 : : /*
735 : : * If there is a distinguishing key attribute within pivot tuple, we're
736 : : * done
737 : : */
738 [ + + ]: 33714 : if (keepnatts <= nkeyatts)
739 : : {
2168 740 : 33082 : BTreeTupleSetNAtts(pivot, keepnatts, false);
2176 741 : 33082 : return pivot;
742 : : }
743 : :
744 : : /*
745 : : * We have to store a heap TID in the new pivot tuple, since no non-TID
746 : : * key attribute value in firstright distinguishes the right side of the
747 : : * split from the left side. nbtree conceptualizes this case as an
748 : : * inability to truncate away any key attributes, since heap TID is
749 : : * treated as just another key attribute (despite lacking a pg_attribute
750 : : * entry).
751 : : *
752 : : * Use enlarged space that holds a copy of pivot. We need the extra space
753 : : * to store a heap TID at the end (using the special pivot tuple
754 : : * representation). Note that the original pivot already has firstright's
755 : : * possible posting list/non-key attribute values removed at this point.
756 : : */
757 : 632 : newsize = MAXALIGN(IndexTupleSize(pivot)) + MAXALIGN(sizeof(ItemPointerData));
758 : 632 : tidpivot = palloc0(newsize);
759 : 632 : memcpy(tidpivot, pivot, MAXALIGN(IndexTupleSize(pivot)));
760 : : /* Cannot leak memory here */
761 : 632 : pfree(pivot);
762 : :
763 : : /*
764 : : * Store all of firstright's key attribute values plus a tiebreaker heap
765 : : * TID value in enlarged pivot tuple
766 : : */
767 : 632 : tidpivot->t_info &= ~INDEX_SIZE_MASK;
768 : 632 : tidpivot->t_info |= newsize;
2168 769 : 632 : BTreeTupleSetNAtts(tidpivot, nkeyatts, true);
2176 770 : 632 : pivotheaptid = BTreeTupleGetHeapTID(tidpivot);
771 : :
772 : : /*
773 : : * Lehman & Yao use lastleft as the leaf high key in all cases, but don't
774 : : * consider suffix truncation. It seems like a good idea to follow that
775 : : * example in cases where no truncation takes place -- use lastleft's heap
776 : : * TID. (This is also the closest value to negative infinity that's
777 : : * legally usable.)
778 : : */
2209 779 : 632 : ItemPointerCopy(BTreeTupleGetMaxHeapTID(lastleft), pivotheaptid);
780 : :
781 : : /*
782 : : * We're done. Assert() that heap TID invariants hold before returning.
783 : : *
784 : : * Lehman and Yao require that the downlink to the right page, which is to
785 : : * be inserted into the parent page in the second phase of a page split be
786 : : * a strict lower bound on items on the right page, and a non-strict upper
787 : : * bound for items on the left page. Assert that heap TIDs follow these
788 : : * invariants, since a heap TID value is apparently needed as a
789 : : * tiebreaker.
790 : : */
791 : : #ifndef DEBUG_NO_TRUNCATE
792 [ - + ]: 632 : Assert(ItemPointerCompare(BTreeTupleGetMaxHeapTID(lastleft),
793 : : BTreeTupleGetHeapTID(firstright)) < 0);
794 [ - + ]: 632 : Assert(ItemPointerCompare(pivotheaptid,
795 : : BTreeTupleGetHeapTID(lastleft)) >= 0);
796 [ - + ]: 632 : Assert(ItemPointerCompare(pivotheaptid,
797 : : BTreeTupleGetHeapTID(firstright)) < 0);
798 : : #else
799 : :
800 : : /*
801 : : * Those invariants aren't guaranteed to hold for lastleft + firstright
802 : : * heap TID attribute values when they're considered here only because
803 : : * DEBUG_NO_TRUNCATE is defined (a heap TID is probably not actually
804 : : * needed as a tiebreaker). DEBUG_NO_TRUNCATE must therefore use a heap
805 : : * TID value that always works as a strict lower bound for items to the
806 : : * right. In particular, it must avoid using firstright's leading key
807 : : * attribute values along with lastleft's heap TID value when lastleft's
808 : : * TID happens to be greater than firstright's TID.
809 : : */
810 : : ItemPointerCopy(BTreeTupleGetHeapTID(firstright), pivotheaptid);
811 : :
812 : : /*
813 : : * Pivot heap TID should never be fully equal to firstright. Note that
814 : : * the pivot heap TID will still end up equal to lastleft's heap TID when
815 : : * that's the only usable value.
816 : : */
817 : : ItemPointerSetOffsetNumber(pivotheaptid,
818 : : OffsetNumberPrev(ItemPointerGetOffsetNumber(pivotheaptid)));
819 : : Assert(ItemPointerCompare(pivotheaptid,
820 : : BTreeTupleGetHeapTID(firstright)) < 0);
821 : : #endif
822 : :
2176 823 : 632 : return tidpivot;
824 : : }
825 : :
826 : : /*
827 : : * _bt_keep_natts - how many key attributes to keep when truncating.
828 : : *
829 : : * Caller provides two tuples that enclose a split point. Caller's insertion
830 : : * scankey is used to compare the tuples; the scankey's argument values are
831 : : * not considered here.
832 : : *
833 : : * This can return a number of attributes that is one greater than the
834 : : * number of key attributes for the index relation. This indicates that the
835 : : * caller must use a heap TID as a unique-ifier in new pivot tuple.
836 : : */
837 : : static int
2552 838 : 33714 : _bt_keep_natts(Relation rel, IndexTuple lastleft, IndexTuple firstright,
839 : : BTScanInsert itup_key)
840 : : {
841 : 33714 : int nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
842 : 33714 : TupleDesc itupdesc = RelationGetDescr(rel);
843 : : int keepnatts;
844 : : ScanKey scankey;
845 : :
846 : : /*
847 : : * _bt_compare() treats truncated key attributes as having the value minus
848 : : * infinity, which would break searches within !heapkeyspace indexes. We
849 : : * must still truncate away non-key attribute values, though.
850 : : */
851 [ - + ]: 33714 : if (!itup_key->heapkeyspace)
2552 pg@bowt.ie 852 :UBC 0 : return nkeyatts;
853 : :
2552 pg@bowt.ie 854 :CBC 33714 : scankey = itup_key->scankeys;
855 : 33714 : keepnatts = 1;
856 [ + + ]: 40879 : for (int attnum = 1; attnum <= nkeyatts; attnum++, scankey++)
857 : : {
858 : : Datum datum1,
859 : : datum2;
860 : : bool isNull1,
861 : : isNull2;
862 : :
863 : 40247 : datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
864 : 40247 : datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
865 : :
866 [ - + ]: 40247 : if (isNull1 != isNull2)
867 : 33082 : break;
868 : :
869 [ + + + + ]: 80479 : if (!isNull1 &&
870 : 40232 : DatumGetInt32(FunctionCall2Coll(&scankey->sk_func,
871 : : scankey->sk_collation,
872 : : datum1,
873 : : datum2)) != 0)
874 : 33082 : break;
875 : :
876 : 7165 : keepnatts++;
877 : : }
878 : :
879 : : /*
880 : : * Assert that _bt_keep_natts_fast() agrees with us in passing. This is
881 : : * expected in an allequalimage index.
882 : : */
2209 883 [ + + - + ]: 33714 : Assert(!itup_key->allequalimage ||
884 : : keepnatts == _bt_keep_natts_fast(rel, lastleft, firstright));
885 : :
2552 886 : 33714 : return keepnatts;
887 : : }
888 : :
889 : : /*
890 : : * _bt_keep_natts_fast - fast bitwise variant of _bt_keep_natts.
891 : : *
892 : : * This is exported so that a candidate split point can have its effect on
893 : : * suffix truncation inexpensively evaluated ahead of time when finding a
894 : : * split location. A naive bitwise approach to datum comparisons is used to
895 : : * save cycles.
896 : : *
897 : : * The approach taken here usually provides the same answer as _bt_keep_natts
898 : : * will (for the same pair of tuples from a heapkeyspace index), since the
899 : : * majority of btree opclasses can never indicate that two datums are equal
900 : : * unless they're bitwise equal after detoasting. When an index only has
901 : : * "equal image" columns, routine is guaranteed to give the same result as
902 : : * _bt_keep_natts would.
903 : : *
904 : : * Callers can rely on the fact that attributes considered equal here are
905 : : * definitely also equal according to _bt_keep_natts, even when the index uses
906 : : * an opclass or collation that is not "allequalimage"/deduplication-safe.
907 : : * This weaker guarantee is good enough for nbtsplitloc.c caller, since false
908 : : * negatives generally only have the effect of making leaf page splits use a
909 : : * more balanced split point.
910 : : */
911 : : int
912 : 7448022 : _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
913 : : {
914 : 7448022 : TupleDesc itupdesc = RelationGetDescr(rel);
915 : 7448022 : int keysz = IndexRelationGetNumberOfKeyAttributes(rel);
916 : : int keepnatts;
917 : :
918 : 7448022 : keepnatts = 1;
919 [ + + ]: 12417118 : for (int attnum = 1; attnum <= keysz; attnum++)
920 : : {
921 : : Datum datum1,
922 : : datum2;
923 : : bool isNull1,
924 : : isNull2;
925 : : CompactAttribute *att;
926 : :
927 : 11083961 : datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
928 : 11083961 : datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
450 drowley@postgresql.o 929 : 11083961 : att = TupleDescCompactAttr(itupdesc, attnum - 1);
930 : :
2552 pg@bowt.ie 931 [ + + ]: 11083961 : if (isNull1 != isNull2)
932 : 6114865 : break;
933 : :
934 [ + + ]: 11083859 : if (!isNull1 &&
2315 935 [ + + ]: 11060316 : !datum_image_eq(datum1, datum2, att->attbyval, att->attlen))
2552 936 : 6114763 : break;
937 : :
938 : 4969096 : keepnatts++;
939 : : }
940 : :
941 : 7448022 : return keepnatts;
942 : : }
943 : :
944 : : /*
945 : : * _bt_check_natts() -- Verify tuple has expected number of attributes.
946 : : *
947 : : * Returns value indicating if the expected number of attributes were found
948 : : * for a particular offset on page. This can be used as a general purpose
949 : : * sanity check.
950 : : *
951 : : * Testing a tuple directly with BTreeTupleGetNAtts() should generally be
952 : : * preferred to calling here. That's usually more convenient, and is always
953 : : * more explicit. Call here instead when offnum's tuple may be a negative
954 : : * infinity tuple that uses the pre-v11 on-disk representation, or when a low
955 : : * context check is appropriate. This routine is as strict as possible about
956 : : * what is expected on each version of btree.
957 : : */
958 : : bool
959 : 162794901 : _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
960 : : {
2880 tgl@sss.pgh.pa.us 961 : 162794901 : int16 natts = IndexRelationGetNumberOfAttributes(rel);
962 : 162794901 : int16 nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
1444 michael@paquier.xyz 963 : 162794901 : BTPageOpaque opaque = BTPageGetOpaque(page);
964 : : IndexTuple itup;
965 : : int tupnatts;
966 : :
967 : : /*
968 : : * We cannot reliably test a deleted or half-dead page, since they have
969 : : * dummy high keys
970 : : */
2887 teodor@sigaev.ru 971 [ - + ]: 162794901 : if (P_IGNORE(opaque))
2887 teodor@sigaev.ru 972 :UBC 0 : return true;
973 : :
2887 teodor@sigaev.ru 974 [ + - - + ]:CBC 162794901 : Assert(offnum >= FirstOffsetNumber &&
975 : : offnum <= PageGetMaxOffsetNumber(page));
976 : :
977 : 162794901 : itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
2552 pg@bowt.ie 978 [ + + ]: 162794901 : tupnatts = BTreeTupleGetNAtts(itup, rel);
979 : :
980 : : /* !heapkeyspace indexes do not support deduplication */
2209 981 [ - + - - ]: 162794901 : if (!heapkeyspace && BTreeTupleIsPosting(itup))
2209 pg@bowt.ie 982 :UBC 0 : return false;
983 : :
984 : : /* Posting list tuples should never have "pivot heap TID" bit set */
2209 pg@bowt.ie 985 [ + + ]:CBC 162794901 : if (BTreeTupleIsPosting(itup) &&
986 [ - + ]: 1734695 : (ItemPointerGetOffsetNumberNoCheck(&itup->t_tid) &
987 : : BT_PIVOT_HEAP_TID_ATTR) != 0)
2209 pg@bowt.ie 988 :UBC 0 : return false;
989 : :
990 : : /* INCLUDE indexes do not support deduplication */
2209 pg@bowt.ie 991 [ + + - + ]:CBC 162794901 : if (natts != nkeyatts && BTreeTupleIsPosting(itup))
2209 pg@bowt.ie 992 :UBC 0 : return false;
993 : :
2887 teodor@sigaev.ru 994 [ + + ]:CBC 162794901 : if (P_ISLEAF(opaque))
995 : : {
996 [ + + + + ]: 116816809 : if (offnum >= P_FIRSTDATAKEY(opaque))
997 : : {
998 : : /*
999 : : * Non-pivot tuple should never be explicitly marked as a pivot
1000 : : * tuple
1001 : : */
2209 pg@bowt.ie 1002 [ - + ]: 107245206 : if (BTreeTupleIsPivot(itup))
2552 pg@bowt.ie 1003 :UBC 0 : return false;
1004 : :
1005 : : /*
1006 : : * Leaf tuples that are not the page high key (non-pivot tuples)
1007 : : * should never be truncated. (Note that tupnatts must have been
1008 : : * inferred, even with a posting list tuple, because only pivot
1009 : : * tuples store tupnatts directly.)
1010 : : */
2552 pg@bowt.ie 1011 :CBC 107245206 : return tupnatts == natts;
1012 : : }
1013 : : else
1014 : : {
1015 : : /*
1016 : : * Rightmost page doesn't contain a page high key, so tuple was
1017 : : * checked above as ordinary leaf tuple
1018 : : */
2887 teodor@sigaev.ru 1019 [ - + ]: 9571603 : Assert(!P_RIGHTMOST(opaque));
1020 : :
1021 : : /*
1022 : : * !heapkeyspace high key tuple contains only key attributes. Note
1023 : : * that tupnatts will only have been explicitly represented in
1024 : : * !heapkeyspace indexes that happen to have non-key attributes.
1025 : : */
2552 pg@bowt.ie 1026 [ - + ]: 9571603 : if (!heapkeyspace)
2552 pg@bowt.ie 1027 :UBC 0 : return tupnatts == nkeyatts;
1028 : :
1029 : : /* Use generic heapkeyspace pivot tuple handling */
1030 : : }
1031 : : }
1032 : : else /* !P_ISLEAF(opaque) */
1033 : : {
2887 teodor@sigaev.ru 1034 [ + + + + ]:CBC 45978092 : if (offnum == P_FIRSTDATAKEY(opaque))
1035 : : {
1036 : : /*
1037 : : * The first tuple on any internal page (possibly the first after
1038 : : * its high key) is its negative infinity tuple. Negative
1039 : : * infinity tuples are always truncated to zero attributes. They
1040 : : * are a particular kind of pivot tuple.
1041 : : */
2552 pg@bowt.ie 1042 [ + - ]: 2113290 : if (heapkeyspace)
1043 : 2113290 : return tupnatts == 0;
1044 : :
1045 : : /*
1046 : : * The number of attributes won't be explicitly represented if the
1047 : : * negative infinity tuple was generated during a page split that
1048 : : * occurred with a version of Postgres before v11. There must be
1049 : : * a problem when there is an explicit representation that is
1050 : : * non-zero, or when there is no explicit representation and the
1051 : : * tuple is evidently not a pre-pg_upgrade tuple.
1052 : : *
1053 : : * Prior to v11, downlinks always had P_HIKEY as their offset.
1054 : : * Accept that as an alternative indication of a valid
1055 : : * !heapkeyspace negative infinity tuple.
1056 : : */
2552 pg@bowt.ie 1057 [ # # # # ]:UBC 0 : return tupnatts == 0 ||
2209 1058 : 0 : ItemPointerGetOffsetNumber(&(itup->t_tid)) == P_HIKEY;
1059 : : }
1060 : : else
1061 : : {
1062 : : /*
1063 : : * !heapkeyspace downlink tuple with separator key contains only
1064 : : * key attributes. Note that tupnatts will only have been
1065 : : * explicitly represented in !heapkeyspace indexes that happen to
1066 : : * have non-key attributes.
1067 : : */
2552 pg@bowt.ie 1068 [ - + ]:CBC 43864802 : if (!heapkeyspace)
2552 pg@bowt.ie 1069 :UBC 0 : return tupnatts == nkeyatts;
1070 : :
1071 : : /* Use generic heapkeyspace pivot tuple handling */
1072 : : }
1073 : : }
1074 : :
1075 : : /* Handle heapkeyspace pivot tuples (excluding minus infinity items) */
2552 pg@bowt.ie 1076 [ - + ]:CBC 53436405 : Assert(heapkeyspace);
1077 : :
1078 : : /*
1079 : : * Explicit representation of the number of attributes is mandatory with
1080 : : * heapkeyspace index pivot tuples, regardless of whether or not there are
1081 : : * non-key attributes.
1082 : : */
2209 1083 [ - + ]: 53436405 : if (!BTreeTupleIsPivot(itup))
2209 pg@bowt.ie 1084 :UBC 0 : return false;
1085 : :
1086 : : /* Pivot tuple should not use posting list representation (redundant) */
2209 pg@bowt.ie 1087 [ - + ]:CBC 53436405 : if (BTreeTupleIsPosting(itup))
2552 pg@bowt.ie 1088 :UBC 0 : return false;
1089 : :
1090 : : /*
1091 : : * Heap TID is a tiebreaker key attribute, so it cannot be untruncated
1092 : : * when any other key attribute is truncated
1093 : : */
2552 pg@bowt.ie 1094 [ + + - + ]:CBC 53436405 : if (BTreeTupleGetHeapTID(itup) != NULL && tupnatts != nkeyatts)
2552 pg@bowt.ie 1095 :UBC 0 : return false;
1096 : :
1097 : : /*
1098 : : * Pivot tuple must have at least one untruncated key attribute (minus
1099 : : * infinity pivot tuples are the only exception). Pivot tuples can never
1100 : : * represent that there is a value present for a key attribute that
1101 : : * exceeds pg_index.indnkeyatts for the index.
1102 : : */
2552 pg@bowt.ie 1103 [ + - + - ]:CBC 53436405 : return tupnatts > 0 && tupnatts <= nkeyatts;
1104 : : }
1105 : :
1106 : : /*
1107 : : *
1108 : : * _bt_check_third_page() -- check whether tuple fits on a btree page at all.
1109 : : *
1110 : : * We actually need to be able to fit three items on every page, so restrict
1111 : : * any one item to 1/3 the per-page available space. Note that itemsz should
1112 : : * not include the ItemId overhead.
1113 : : *
1114 : : * It might be useful to apply TOAST methods rather than throw an error here.
1115 : : * Using out of line storage would break assumptions made by suffix truncation
1116 : : * and by contrib/amcheck, though.
1117 : : */
1118 : : void
1119 : 132 : _bt_check_third_page(Relation rel, Relation heap, bool needheaptidspace,
1120 : : Page page, IndexTuple newtup)
1121 : : {
1122 : : Size itemsz;
1123 : : BTPageOpaque opaque;
1124 : :
1125 : 132 : itemsz = MAXALIGN(IndexTupleSize(newtup));
1126 : :
1127 : : /* Double check item size against limit */
369 1128 [ - + ]: 132 : if (itemsz <= BTMaxItemSize)
2552 pg@bowt.ie 1129 :UBC 0 : return;
1130 : :
1131 : : /*
1132 : : * Tuple is probably too large to fit on page, but it's possible that the
1133 : : * index uses version 2 or version 3, or that page is an internal page, in
1134 : : * which case a slightly higher limit applies.
1135 : : */
369 pg@bowt.ie 1136 [ + - + - ]:CBC 132 : if (!needheaptidspace && itemsz <= BTMaxItemSizeNoHeapTid)
2552 1137 : 132 : return;
1138 : :
1139 : : /*
1140 : : * Internal page insertions cannot fail here, because that would mean that
1141 : : * an earlier leaf level insertion that should have failed didn't
1142 : : */
1444 michael@paquier.xyz 1143 :UBC 0 : opaque = BTPageGetOpaque(page);
2552 pg@bowt.ie 1144 [ # # ]: 0 : if (!P_ISLEAF(opaque))
1145 [ # # ]: 0 : elog(ERROR, "cannot insert oversized tuple of size %zu on internal page of index \"%s\"",
1146 : : itemsz, RelationGetRelationName(rel));
1147 : :
1148 [ # # # # : 0 : ereport(ERROR,
# # ]
1149 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1150 : : errmsg("index row size %zu exceeds btree version %u maximum %zu for index \"%s\"",
1151 : : itemsz,
1152 : : needheaptidspace ? BTREE_VERSION : BTREE_NOVAC_VERSION,
1153 : : needheaptidspace ? BTMaxItemSize : BTMaxItemSizeNoHeapTid,
1154 : : RelationGetRelationName(rel)),
1155 : : errdetail("Index row references tuple (%u,%u) in relation \"%s\".",
1156 : : ItemPointerGetBlockNumber(BTreeTupleGetHeapTID(newtup)),
1157 : : ItemPointerGetOffsetNumber(BTreeTupleGetHeapTID(newtup)),
1158 : : RelationGetRelationName(heap)),
1159 : : errhint("Values larger than 1/3 of a buffer page cannot be indexed.\n"
1160 : : "Consider a function index of an MD5 hash of the value, "
1161 : : "or use full text indexing."),
1162 : : errtableconstraint(heap, RelationGetRelationName(rel))));
1163 : : }
1164 : :
1165 : : /*
1166 : : * Are all attributes in rel "equality is image equality" attributes?
1167 : : *
1168 : : * We use each attribute's BTEQUALIMAGE_PROC opclass procedure. If any
1169 : : * opclass either lacks a BTEQUALIMAGE_PROC procedure or returns false, we
1170 : : * return false; otherwise we return true.
1171 : : *
1172 : : * Returned boolean value is stored in index metapage during index builds.
1173 : : * Deduplication can only be used when we return true.
1174 : : */
1175 : : bool
2209 pg@bowt.ie 1176 :CBC 30239 : _bt_allequalimage(Relation rel, bool debugmessage)
1177 : : {
1178 : 30239 : bool allequalimage = true;
1179 : :
1180 : : /* INCLUDE indexes can never support deduplication */
1181 : 30239 : if (IndexRelationGetNumberOfAttributes(rel) !=
1182 [ + + ]: 30239 : IndexRelationGetNumberOfKeyAttributes(rel))
1183 : 136 : return false;
1184 : :
1185 [ + + ]: 79153 : for (int i = 0; i < IndexRelationGetNumberOfKeyAttributes(rel); i++)
1186 : : {
1187 : 49309 : Oid opfamily = rel->rd_opfamily[i];
1188 : 49309 : Oid opcintype = rel->rd_opcintype[i];
1189 : 49309 : Oid collation = rel->rd_indcollation[i];
1190 : : Oid equalimageproc;
1191 : :
1192 : 49309 : equalimageproc = get_opfamily_proc(opfamily, opcintype, opcintype,
1193 : : BTEQUALIMAGE_PROC);
1194 : :
1195 : : /*
1196 : : * If there is no BTEQUALIMAGE_PROC then deduplication is assumed to
1197 : : * be unsafe. Otherwise, actually call proc and see what it says.
1198 : : */
1199 [ + + ]: 49309 : if (!OidIsValid(equalimageproc) ||
1200 [ + + ]: 49072 : !DatumGetBool(OidFunctionCall1Coll(equalimageproc, collation,
1201 : : ObjectIdGetDatum(opcintype))))
1202 : : {
1203 : 259 : allequalimage = false;
1204 : 259 : break;
1205 : : }
1206 : : }
1207 : :
1208 [ + + ]: 30103 : if (debugmessage)
1209 : : {
1210 [ + + ]: 26061 : if (allequalimage)
1211 [ + + ]: 25802 : elog(DEBUG1, "index \"%s\" can safely use deduplication",
1212 : : RelationGetRelationName(rel));
1213 : : else
1214 [ - + ]: 259 : elog(DEBUG1, "index \"%s\" cannot use deduplication",
1215 : : RelationGetRelationName(rel));
1216 : : }
1217 : :
1218 : 30103 : return allequalimage;
1219 : : }
|