Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * indexam.c
4 : * general index access method routines
5 : *
6 : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/access/index/indexam.c
12 : *
13 : * INTERFACE ROUTINES
14 : * index_open - open an index relation by relation OID
15 : * index_close - close an index relation
16 : * index_beginscan - start a scan of an index with amgettuple
17 : * index_beginscan_bitmap - start a scan of an index with amgetbitmap
18 : * index_rescan - restart a scan of an index
19 : * index_endscan - end a scan
20 : * index_insert - insert an index tuple into a relation
21 : * index_markpos - mark a scan position
22 : * index_restrpos - restore a scan position
23 : * index_parallelscan_estimate - estimate shared memory for parallel scan
24 : * index_parallelscan_initialize - initialize parallel scan
25 : * index_parallelrescan - (re)start a parallel scan of an index
26 : * index_beginscan_parallel - join parallel index scan
27 : * index_getnext_tid - get the next TID from a scan
28 : * index_fetch_heap - get the scan's next heap tuple
29 : * index_getnext_slot - get the next tuple from a scan
30 : * index_getbitmap - get all tuples from a scan
31 : * index_bulk_delete - bulk deletion of index tuples
32 : * index_vacuum_cleanup - post-deletion cleanup of an index
33 : * index_can_return - does index support index-only scans?
34 : * index_getprocid - get a support procedure OID
35 : * index_getprocinfo - get a support procedure's lookup info
36 : *
37 : * NOTES
38 : * This file contains the index_ routines which used
39 : * to be a scattered collection of stuff in access/genam.
40 : *
41 : *-------------------------------------------------------------------------
42 : */
43 :
44 : #include "postgres.h"
45 :
46 : #include "access/amapi.h"
47 : #include "access/relation.h"
48 : #include "access/reloptions.h"
49 : #include "access/relscan.h"
50 : #include "access/tableam.h"
51 : #include "catalog/index.h"
52 : #include "catalog/pg_type.h"
53 : #include "nodes/execnodes.h"
54 : #include "pgstat.h"
55 : #include "storage/lmgr.h"
56 : #include "storage/predicate.h"
57 : #include "utils/ruleutils.h"
58 : #include "utils/snapmgr.h"
59 : #include "utils/syscache.h"
60 :
61 :
62 : /* ----------------------------------------------------------------
63 : * macros used in index_ routines
64 : *
65 : * Note: the ReindexIsProcessingIndex() check in RELATION_CHECKS is there
66 : * to check that we don't try to scan or do retail insertions into an index
67 : * that is currently being rebuilt or pending rebuild. This helps to catch
68 : * things that don't work when reindexing system catalogs, as well as prevent
69 : * user errors like index expressions that access their own tables. The check
70 : * doesn't prevent the actual rebuild because we don't use RELATION_CHECKS
71 : * when calling the index AM's ambuild routine, and there is no reason for
72 : * ambuild to call its subsidiary routines through this file.
73 : * ----------------------------------------------------------------
74 : */
75 : #define RELATION_CHECKS \
76 : do { \
77 : Assert(RelationIsValid(indexRelation)); \
78 : Assert(PointerIsValid(indexRelation->rd_indam)); \
79 : if (unlikely(ReindexIsProcessingIndex(RelationGetRelid(indexRelation)))) \
80 : ereport(ERROR, \
81 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
82 : errmsg("cannot access index \"%s\" while it is being reindexed", \
83 : RelationGetRelationName(indexRelation)))); \
84 : } while(0)
85 :
86 : #define SCAN_CHECKS \
87 : ( \
88 : AssertMacro(IndexScanIsValid(scan)), \
89 : AssertMacro(RelationIsValid(scan->indexRelation)), \
90 : AssertMacro(PointerIsValid(scan->indexRelation->rd_indam)) \
91 : )
92 :
93 : #define CHECK_REL_PROCEDURE(pname) \
94 : do { \
95 : if (indexRelation->rd_indam->pname == NULL) \
96 : elog(ERROR, "function \"%s\" is not defined for index \"%s\"", \
97 : CppAsString(pname), RelationGetRelationName(indexRelation)); \
98 : } while(0)
99 :
100 : #define CHECK_SCAN_PROCEDURE(pname) \
101 : do { \
102 : if (scan->indexRelation->rd_indam->pname == NULL) \
103 : elog(ERROR, "function \"%s\" is not defined for index \"%s\"", \
104 : CppAsString(pname), RelationGetRelationName(scan->indexRelation)); \
105 : } while(0)
106 :
107 : static IndexScanDesc index_beginscan_internal(Relation indexRelation,
108 : int nkeys, int norderbys, Snapshot snapshot,
109 : ParallelIndexScanDesc pscan, bool temp_snap);
110 : static inline void validate_relation_kind(Relation r);
111 :
112 :
113 : /* ----------------------------------------------------------------
114 : * index_ interface functions
115 : * ----------------------------------------------------------------
116 : */
117 :
118 : /* ----------------
119 : * index_open - open an index relation by relation OID
120 : *
121 : * If lockmode is not "NoLock", the specified kind of lock is
122 : * obtained on the index. (Generally, NoLock should only be
123 : * used if the caller knows it has some appropriate lock on the
124 : * index already.)
125 : *
126 : * An error is raised if the index does not exist.
127 : *
128 : * This is a convenience routine adapted for indexscan use.
129 : * Some callers may prefer to use relation_open directly.
130 : * ----------------
131 : */
132 : Relation
133 14768838 : index_open(Oid relationId, LOCKMODE lockmode)
134 : {
135 : Relation r;
136 :
137 14768838 : r = relation_open(relationId, lockmode);
138 :
139 14768826 : validate_relation_kind(r);
140 :
141 14768804 : return r;
142 : }
143 :
144 : /* ----------------
145 : * try_index_open - open an index relation by relation OID
146 : *
147 : * Same as index_open, except return NULL instead of failing
148 : * if the relation does not exist.
149 : * ----------------
150 : */
151 : Relation
152 2132 : try_index_open(Oid relationId, LOCKMODE lockmode)
153 : {
154 : Relation r;
155 :
156 2132 : r = try_relation_open(relationId, lockmode);
157 :
158 : /* leave if index does not exist */
159 2132 : if (!r)
160 0 : return NULL;
161 :
162 2132 : validate_relation_kind(r);
163 :
164 2132 : return r;
165 : }
166 :
167 : /* ----------------
168 : * index_close - close an index relation
169 : *
170 : * If lockmode is not "NoLock", we then release the specified lock.
171 : *
172 : * Note that it is often sensible to hold a lock beyond index_close;
173 : * in that case, the lock is released automatically at xact end.
174 : * ----------------
175 : */
176 : void
177 14801464 : index_close(Relation relation, LOCKMODE lockmode)
178 : {
179 14801464 : LockRelId relid = relation->rd_lockInfo.lockRelId;
180 :
181 : Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
182 :
183 : /* The relcache does the real work... */
184 14801464 : RelationClose(relation);
185 :
186 14801464 : if (lockmode != NoLock)
187 13553154 : UnlockRelationId(&relid, lockmode);
188 14801464 : }
189 :
190 : /* ----------------
191 : * validate_relation_kind - check the relation's kind
192 : *
193 : * Make sure relkind is an index or a partitioned index.
194 : * ----------------
195 : */
196 : static inline void
197 14770958 : validate_relation_kind(Relation r)
198 : {
199 14770958 : if (r->rd_rel->relkind != RELKIND_INDEX &&
200 12736 : r->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
201 22 : ereport(ERROR,
202 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
203 : errmsg("\"%s\" is not an index",
204 : RelationGetRelationName(r))));
205 14770936 : }
206 :
207 :
208 : /* ----------------
209 : * index_insert - insert an index tuple into a relation
210 : * ----------------
211 : */
212 : bool
213 7676316 : index_insert(Relation indexRelation,
214 : Datum *values,
215 : bool *isnull,
216 : ItemPointer heap_t_ctid,
217 : Relation heapRelation,
218 : IndexUniqueCheck checkUnique,
219 : bool indexUnchanged,
220 : IndexInfo *indexInfo)
221 : {
222 7676316 : RELATION_CHECKS;
223 7676316 : CHECK_REL_PROCEDURE(aminsert);
224 :
225 7676316 : if (!(indexRelation->rd_indam->ampredlocks))
226 576964 : CheckForSerializableConflictIn(indexRelation,
227 : (ItemPointer) NULL,
228 : InvalidBlockNumber);
229 :
230 7676316 : return indexRelation->rd_indam->aminsert(indexRelation, values, isnull,
231 : heap_t_ctid, heapRelation,
232 : checkUnique, indexUnchanged,
233 : indexInfo);
234 : }
235 :
236 : /* -------------------------
237 : * index_insert_cleanup - clean up after all index inserts are done
238 : * -------------------------
239 : */
240 : void
241 2891394 : index_insert_cleanup(Relation indexRelation,
242 : IndexInfo *indexInfo)
243 : {
244 2891394 : RELATION_CHECKS;
245 : Assert(indexInfo);
246 :
247 2891394 : if (indexRelation->rd_indam->aminsertcleanup && indexInfo->ii_AmCache)
248 1084 : indexRelation->rd_indam->aminsertcleanup(indexInfo);
249 2891394 : }
250 :
251 : /*
252 : * index_beginscan - start a scan of an index with amgettuple
253 : *
254 : * Caller must be holding suitable locks on the heap and the index.
255 : */
256 : IndexScanDesc
257 10868878 : index_beginscan(Relation heapRelation,
258 : Relation indexRelation,
259 : Snapshot snapshot,
260 : int nkeys, int norderbys)
261 : {
262 : IndexScanDesc scan;
263 :
264 : Assert(snapshot != InvalidSnapshot);
265 :
266 10868878 : scan = index_beginscan_internal(indexRelation, nkeys, norderbys, snapshot, NULL, false);
267 :
268 : /*
269 : * Save additional parameters into the scandesc. Everything else was set
270 : * up by RelationGetIndexScan.
271 : */
272 10868878 : scan->heapRelation = heapRelation;
273 10868878 : scan->xs_snapshot = snapshot;
274 :
275 : /* prepare to fetch index matches from table */
276 10868878 : scan->xs_heapfetch = table_index_fetch_begin(heapRelation);
277 :
278 10868878 : return scan;
279 : }
280 :
281 : /*
282 : * index_beginscan_bitmap - start a scan of an index with amgetbitmap
283 : *
284 : * As above, caller had better be holding some lock on the parent heap
285 : * relation, even though it's not explicitly mentioned here.
286 : */
287 : IndexScanDesc
288 16024 : index_beginscan_bitmap(Relation indexRelation,
289 : Snapshot snapshot,
290 : int nkeys)
291 : {
292 : IndexScanDesc scan;
293 :
294 : Assert(snapshot != InvalidSnapshot);
295 :
296 16024 : scan = index_beginscan_internal(indexRelation, nkeys, 0, snapshot, NULL, false);
297 :
298 : /*
299 : * Save additional parameters into the scandesc. Everything else was set
300 : * up by RelationGetIndexScan.
301 : */
302 16024 : scan->xs_snapshot = snapshot;
303 :
304 16024 : return scan;
305 : }
306 :
307 : /*
308 : * index_beginscan_internal --- common code for index_beginscan variants
309 : */
310 : static IndexScanDesc
311 10885280 : index_beginscan_internal(Relation indexRelation,
312 : int nkeys, int norderbys, Snapshot snapshot,
313 : ParallelIndexScanDesc pscan, bool temp_snap)
314 : {
315 : IndexScanDesc scan;
316 :
317 10885280 : RELATION_CHECKS;
318 10885280 : CHECK_REL_PROCEDURE(ambeginscan);
319 :
320 10885280 : if (!(indexRelation->rd_indam->ampredlocks))
321 4624 : PredicateLockRelation(indexRelation, snapshot);
322 :
323 : /*
324 : * We hold a reference count to the relcache entry throughout the scan.
325 : */
326 10885280 : RelationIncrementReferenceCount(indexRelation);
327 :
328 : /*
329 : * Tell the AM to open a scan.
330 : */
331 10885280 : scan = indexRelation->rd_indam->ambeginscan(indexRelation, nkeys,
332 : norderbys);
333 : /* Initialize information for parallel scan. */
334 10885280 : scan->parallel_scan = pscan;
335 10885280 : scan->xs_temp_snap = temp_snap;
336 :
337 10885280 : return scan;
338 : }
339 :
340 : /* ----------------
341 : * index_rescan - (re)start a scan of an index
342 : *
343 : * During a restart, the caller may specify a new set of scankeys and/or
344 : * orderbykeys; but the number of keys cannot differ from what index_beginscan
345 : * was told. (Later we might relax that to "must not exceed", but currently
346 : * the index AMs tend to assume that scan->numberOfKeys is what to believe.)
347 : * To restart the scan without changing keys, pass NULL for the key arrays.
348 : * (Of course, keys *must* be passed on the first call, unless
349 : * scan->numberOfKeys is zero.)
350 : * ----------------
351 : */
352 : void
353 11330670 : index_rescan(IndexScanDesc scan,
354 : ScanKey keys, int nkeys,
355 : ScanKey orderbys, int norderbys)
356 : {
357 : SCAN_CHECKS;
358 11330670 : CHECK_SCAN_PROCEDURE(amrescan);
359 :
360 : Assert(nkeys == scan->numberOfKeys);
361 : Assert(norderbys == scan->numberOfOrderBys);
362 :
363 : /* Release resources (like buffer pins) from table accesses */
364 11330670 : if (scan->xs_heapfetch)
365 11310904 : table_index_fetch_reset(scan->xs_heapfetch);
366 :
367 11330670 : scan->kill_prior_tuple = false; /* for safety */
368 11330670 : scan->xs_heap_continue = false;
369 :
370 11330670 : scan->indexRelation->rd_indam->amrescan(scan, keys, nkeys,
371 : orderbys, norderbys);
372 11330670 : }
373 :
374 : /* ----------------
375 : * index_endscan - end a scan
376 : * ----------------
377 : */
378 : void
379 10883690 : index_endscan(IndexScanDesc scan)
380 : {
381 : SCAN_CHECKS;
382 10883690 : CHECK_SCAN_PROCEDURE(amendscan);
383 :
384 : /* Release resources (like buffer pins) from table accesses */
385 10883690 : if (scan->xs_heapfetch)
386 : {
387 10867762 : table_index_fetch_end(scan->xs_heapfetch);
388 10867762 : scan->xs_heapfetch = NULL;
389 : }
390 :
391 : /* End the AM's scan */
392 10883690 : scan->indexRelation->rd_indam->amendscan(scan);
393 :
394 : /* Release index refcount acquired by index_beginscan */
395 10883690 : RelationDecrementReferenceCount(scan->indexRelation);
396 :
397 10883690 : if (scan->xs_temp_snap)
398 378 : UnregisterSnapshot(scan->xs_snapshot);
399 :
400 : /* Release the scan data structure itself */
401 10883690 : IndexScanEnd(scan);
402 10883690 : }
403 :
404 : /* ----------------
405 : * index_markpos - mark a scan position
406 : * ----------------
407 : */
408 : void
409 130042 : index_markpos(IndexScanDesc scan)
410 : {
411 : SCAN_CHECKS;
412 130042 : CHECK_SCAN_PROCEDURE(ammarkpos);
413 :
414 130042 : scan->indexRelation->rd_indam->ammarkpos(scan);
415 130042 : }
416 :
417 : /* ----------------
418 : * index_restrpos - restore a scan position
419 : *
420 : * NOTE: this only restores the internal scan state of the index AM. See
421 : * comments for ExecRestrPos().
422 : *
423 : * NOTE: For heap, in the presence of HOT chains, mark/restore only works
424 : * correctly if the scan's snapshot is MVCC-safe; that ensures that there's at
425 : * most one returnable tuple in each HOT chain, and so restoring the prior
426 : * state at the granularity of the index AM is sufficient. Since the only
427 : * current user of mark/restore functionality is nodeMergejoin.c, this
428 : * effectively means that merge-join plans only work for MVCC snapshots. This
429 : * could be fixed if necessary, but for now it seems unimportant.
430 : * ----------------
431 : */
432 : void
433 54018 : index_restrpos(IndexScanDesc scan)
434 : {
435 : Assert(IsMVCCSnapshot(scan->xs_snapshot));
436 :
437 : SCAN_CHECKS;
438 54018 : CHECK_SCAN_PROCEDURE(amrestrpos);
439 :
440 : /* release resources (like buffer pins) from table accesses */
441 54018 : if (scan->xs_heapfetch)
442 54018 : table_index_fetch_reset(scan->xs_heapfetch);
443 :
444 54018 : scan->kill_prior_tuple = false; /* for safety */
445 54018 : scan->xs_heap_continue = false;
446 :
447 54018 : scan->indexRelation->rd_indam->amrestrpos(scan);
448 54018 : }
449 :
450 : /*
451 : * index_parallelscan_estimate - estimate shared memory for parallel scan
452 : */
453 : Size
454 58 : index_parallelscan_estimate(Relation indexRelation, int nkeys, int norderbys,
455 : Snapshot snapshot)
456 : {
457 : Size nbytes;
458 :
459 : Assert(snapshot != InvalidSnapshot);
460 :
461 58 : RELATION_CHECKS;
462 :
463 58 : nbytes = offsetof(ParallelIndexScanDescData, ps_snapshot_data);
464 58 : nbytes = add_size(nbytes, EstimateSnapshotSpace(snapshot));
465 58 : nbytes = MAXALIGN(nbytes);
466 :
467 : /*
468 : * If amestimateparallelscan is not provided, assume there is no
469 : * AM-specific data needed. (It's hard to believe that could work, but
470 : * it's easy enough to cater to it here.)
471 : */
472 58 : if (indexRelation->rd_indam->amestimateparallelscan != NULL)
473 58 : nbytes = add_size(nbytes,
474 58 : indexRelation->rd_indam->amestimateparallelscan(nkeys,
475 : norderbys));
476 :
477 58 : return nbytes;
478 : }
479 :
480 : /*
481 : * index_parallelscan_initialize - initialize parallel scan
482 : *
483 : * We initialize both the ParallelIndexScanDesc proper and the AM-specific
484 : * information which follows it.
485 : *
486 : * This function calls access method specific initialization routine to
487 : * initialize am specific information. Call this just once in the leader
488 : * process; then, individual workers attach via index_beginscan_parallel.
489 : */
490 : void
491 58 : index_parallelscan_initialize(Relation heapRelation, Relation indexRelation,
492 : Snapshot snapshot, ParallelIndexScanDesc target)
493 : {
494 : Size offset;
495 :
496 : Assert(snapshot != InvalidSnapshot);
497 :
498 58 : RELATION_CHECKS;
499 :
500 58 : offset = add_size(offsetof(ParallelIndexScanDescData, ps_snapshot_data),
501 : EstimateSnapshotSpace(snapshot));
502 58 : offset = MAXALIGN(offset);
503 :
504 58 : target->ps_relid = RelationGetRelid(heapRelation);
505 58 : target->ps_indexid = RelationGetRelid(indexRelation);
506 58 : target->ps_offset = offset;
507 58 : SerializeSnapshot(snapshot, target->ps_snapshot_data);
508 :
509 : /* aminitparallelscan is optional; assume no-op if not provided by AM */
510 58 : if (indexRelation->rd_indam->aminitparallelscan != NULL)
511 : {
512 : void *amtarget;
513 :
514 58 : amtarget = OffsetToPointer(target, offset);
515 58 : indexRelation->rd_indam->aminitparallelscan(amtarget);
516 : }
517 58 : }
518 :
519 : /* ----------------
520 : * index_parallelrescan - (re)start a parallel scan of an index
521 : * ----------------
522 : */
523 : void
524 24 : index_parallelrescan(IndexScanDesc scan)
525 : {
526 : SCAN_CHECKS;
527 :
528 24 : if (scan->xs_heapfetch)
529 24 : table_index_fetch_reset(scan->xs_heapfetch);
530 :
531 : /* amparallelrescan is optional; assume no-op if not provided by AM */
532 24 : if (scan->indexRelation->rd_indam->amparallelrescan != NULL)
533 24 : scan->indexRelation->rd_indam->amparallelrescan(scan);
534 24 : }
535 :
536 : /*
537 : * index_beginscan_parallel - join parallel index scan
538 : *
539 : * Caller must be holding suitable locks on the heap and the index.
540 : */
541 : IndexScanDesc
542 378 : index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys,
543 : int norderbys, ParallelIndexScanDesc pscan)
544 : {
545 : Snapshot snapshot;
546 : IndexScanDesc scan;
547 :
548 : Assert(RelationGetRelid(heaprel) == pscan->ps_relid);
549 378 : snapshot = RestoreSnapshot(pscan->ps_snapshot_data);
550 378 : RegisterSnapshot(snapshot);
551 378 : scan = index_beginscan_internal(indexrel, nkeys, norderbys, snapshot,
552 : pscan, true);
553 :
554 : /*
555 : * Save additional parameters into the scandesc. Everything else was set
556 : * up by index_beginscan_internal.
557 : */
558 378 : scan->heapRelation = heaprel;
559 378 : scan->xs_snapshot = snapshot;
560 :
561 : /* prepare to fetch index matches from table */
562 378 : scan->xs_heapfetch = table_index_fetch_begin(heaprel);
563 :
564 378 : return scan;
565 : }
566 :
567 : /* ----------------
568 : * index_getnext_tid - get the next TID from a scan
569 : *
570 : * The result is the next TID satisfying the scan keys,
571 : * or NULL if no more matching tuples exist.
572 : * ----------------
573 : */
574 : ItemPointer
575 28372444 : index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
576 : {
577 : bool found;
578 :
579 : SCAN_CHECKS;
580 28372444 : CHECK_SCAN_PROCEDURE(amgettuple);
581 :
582 : /* XXX: we should assert that a snapshot is pushed or registered */
583 : Assert(TransactionIdIsValid(RecentXmin));
584 :
585 : /*
586 : * The AM's amgettuple proc finds the next index entry matching the scan
587 : * keys, and puts the TID into scan->xs_heaptid. It should also set
588 : * scan->xs_recheck and possibly scan->xs_itup/scan->xs_hitup, though we
589 : * pay no attention to those fields here.
590 : */
591 28372444 : found = scan->indexRelation->rd_indam->amgettuple(scan, direction);
592 :
593 : /* Reset kill flag immediately for safety */
594 28372444 : scan->kill_prior_tuple = false;
595 28372444 : scan->xs_heap_continue = false;
596 :
597 : /* If we're out of index entries, we're done */
598 28372444 : if (!found)
599 : {
600 : /* release resources (like buffer pins) from table accesses */
601 5509038 : if (scan->xs_heapfetch)
602 5509038 : table_index_fetch_reset(scan->xs_heapfetch);
603 :
604 5509038 : return NULL;
605 : }
606 : Assert(ItemPointerIsValid(&scan->xs_heaptid));
607 :
608 22863406 : pgstat_count_index_tuples(scan->indexRelation, 1);
609 :
610 : /* Return the TID of the tuple we found. */
611 22863406 : return &scan->xs_heaptid;
612 : }
613 :
614 : /* ----------------
615 : * index_fetch_heap - get the scan's next heap tuple
616 : *
617 : * The result is a visible heap tuple associated with the index TID most
618 : * recently fetched by index_getnext_tid, or NULL if no more matching tuples
619 : * exist. (There can be more than one matching tuple because of HOT chains,
620 : * although when using an MVCC snapshot it should be impossible for more than
621 : * one such tuple to exist.)
622 : *
623 : * On success, the buffer containing the heap tup is pinned (the pin will be
624 : * dropped in a future index_getnext_tid, index_fetch_heap or index_endscan
625 : * call).
626 : *
627 : * Note: caller must check scan->xs_recheck, and perform rechecking of the
628 : * scan keys if required. We do not do that here because we don't have
629 : * enough information to do it efficiently in the general case.
630 : * ----------------
631 : */
632 : bool
633 19580912 : index_fetch_heap(IndexScanDesc scan, TupleTableSlot *slot)
634 : {
635 19580912 : bool all_dead = false;
636 : bool found;
637 :
638 19580912 : found = table_index_fetch_tuple(scan->xs_heapfetch, &scan->xs_heaptid,
639 : scan->xs_snapshot, slot,
640 : &scan->xs_heap_continue, &all_dead);
641 :
642 19580902 : if (found)
643 18639894 : pgstat_count_heap_fetch(scan->indexRelation);
644 :
645 : /*
646 : * If we scanned a whole HOT chain and found only dead tuples, tell index
647 : * AM to kill its entry for that TID (this will take effect in the next
648 : * amgettuple call, in index_getnext_tid). We do not do this when in
649 : * recovery because it may violate MVCC to do so. See comments in
650 : * RelationGetIndexScan().
651 : */
652 19580902 : if (!scan->xactStartedInRecovery)
653 19250998 : scan->kill_prior_tuple = all_dead;
654 :
655 19580902 : return found;
656 : }
657 :
658 : /* ----------------
659 : * index_getnext_slot - get the next tuple from a scan
660 : *
661 : * The result is true if a tuple satisfying the scan keys and the snapshot was
662 : * found, false otherwise. The tuple is stored in the specified slot.
663 : *
664 : * On success, resources (like buffer pins) are likely to be held, and will be
665 : * dropped by a future index_getnext_tid, index_fetch_heap or index_endscan
666 : * call).
667 : *
668 : * Note: caller must check scan->xs_recheck, and perform rechecking of the
669 : * scan keys if required. We do not do that here because we don't have
670 : * enough information to do it efficiently in the general case.
671 : * ----------------
672 : */
673 : bool
674 23097110 : index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
675 : {
676 : for (;;)
677 : {
678 23097110 : if (!scan->xs_heap_continue)
679 : {
680 : ItemPointer tid;
681 :
682 : /* Time to fetch the next TID from the index */
683 22953676 : tid = index_getnext_tid(scan, direction);
684 :
685 : /* If we're out of index entries, we're done */
686 22953676 : if (tid == NULL)
687 5426728 : break;
688 :
689 : Assert(ItemPointerEquals(tid, &scan->xs_heaptid));
690 : }
691 :
692 : /*
693 : * Fetch the next (or only) visible heap tuple for this index entry.
694 : * If we don't find anything, loop around and grab the next TID from
695 : * the index.
696 : */
697 : Assert(ItemPointerIsValid(&scan->xs_heaptid));
698 17670382 : if (index_fetch_heap(scan, slot))
699 16838686 : return true;
700 : }
701 :
702 5426728 : return false;
703 : }
704 :
705 : /* ----------------
706 : * index_getbitmap - get all tuples at once from an index scan
707 : *
708 : * Adds the TIDs of all heap tuples satisfying the scan keys to a bitmap.
709 : * Since there's no interlock between the index scan and the eventual heap
710 : * access, this is only safe to use with MVCC-based snapshots: the heap
711 : * item slot could have been replaced by a newer tuple by the time we get
712 : * to it.
713 : *
714 : * Returns the number of matching tuples found. (Note: this might be only
715 : * approximate, so it should only be used for statistical purposes.)
716 : * ----------------
717 : */
718 : int64
719 18798 : index_getbitmap(IndexScanDesc scan, TIDBitmap *bitmap)
720 : {
721 : int64 ntids;
722 :
723 : SCAN_CHECKS;
724 18798 : CHECK_SCAN_PROCEDURE(amgetbitmap);
725 :
726 : /* just make sure this is false... */
727 18798 : scan->kill_prior_tuple = false;
728 :
729 : /*
730 : * have the am's getbitmap proc do all the work.
731 : */
732 18798 : ntids = scan->indexRelation->rd_indam->amgetbitmap(scan, bitmap);
733 :
734 18798 : pgstat_count_index_tuples(scan->indexRelation, ntids);
735 :
736 18798 : return ntids;
737 : }
738 :
739 : /* ----------------
740 : * index_bulk_delete - do mass deletion of index entries
741 : *
742 : * callback routine tells whether a given main-heap tuple is
743 : * to be deleted
744 : *
745 : * return value is an optional palloc'd struct of statistics
746 : * ----------------
747 : */
748 : IndexBulkDeleteResult *
749 2326 : index_bulk_delete(IndexVacuumInfo *info,
750 : IndexBulkDeleteResult *istat,
751 : IndexBulkDeleteCallback callback,
752 : void *callback_state)
753 : {
754 2326 : Relation indexRelation = info->index;
755 :
756 2326 : RELATION_CHECKS;
757 2326 : CHECK_REL_PROCEDURE(ambulkdelete);
758 :
759 2326 : return indexRelation->rd_indam->ambulkdelete(info, istat,
760 : callback, callback_state);
761 : }
762 :
763 : /* ----------------
764 : * index_vacuum_cleanup - do post-deletion cleanup of an index
765 : *
766 : * return value is an optional palloc'd struct of statistics
767 : * ----------------
768 : */
769 : IndexBulkDeleteResult *
770 41482 : index_vacuum_cleanup(IndexVacuumInfo *info,
771 : IndexBulkDeleteResult *istat)
772 : {
773 41482 : Relation indexRelation = info->index;
774 :
775 41482 : RELATION_CHECKS;
776 41482 : CHECK_REL_PROCEDURE(amvacuumcleanup);
777 :
778 41482 : return indexRelation->rd_indam->amvacuumcleanup(info, istat);
779 : }
780 :
781 : /* ----------------
782 : * index_can_return
783 : *
784 : * Does the index access method support index-only scans for the given
785 : * column?
786 : * ----------------
787 : */
788 : bool
789 1190554 : index_can_return(Relation indexRelation, int attno)
790 : {
791 1190554 : RELATION_CHECKS;
792 :
793 : /* amcanreturn is optional; assume false if not provided by AM */
794 1190554 : if (indexRelation->rd_indam->amcanreturn == NULL)
795 272808 : return false;
796 :
797 917746 : return indexRelation->rd_indam->amcanreturn(indexRelation, attno);
798 : }
799 :
800 : /* ----------------
801 : * index_getprocid
802 : *
803 : * Index access methods typically require support routines that are
804 : * not directly the implementation of any WHERE-clause query operator
805 : * and so cannot be kept in pg_amop. Instead, such routines are kept
806 : * in pg_amproc. These registered procedure OIDs are assigned numbers
807 : * according to a convention established by the access method.
808 : * The general index code doesn't know anything about the routines
809 : * involved; it just builds an ordered list of them for
810 : * each attribute on which an index is defined.
811 : *
812 : * As of Postgres 8.3, support routines within an operator family
813 : * are further subdivided by the "left type" and "right type" of the
814 : * query operator(s) that they support. The "default" functions for a
815 : * particular indexed attribute are those with both types equal to
816 : * the index opclass' opcintype (note that this is subtly different
817 : * from the indexed attribute's own type: it may be a binary-compatible
818 : * type instead). Only the default functions are stored in relcache
819 : * entries --- access methods can use the syscache to look up non-default
820 : * functions.
821 : *
822 : * This routine returns the requested default procedure OID for a
823 : * particular indexed attribute.
824 : * ----------------
825 : */
826 : RegProcedure
827 1566670 : index_getprocid(Relation irel,
828 : AttrNumber attnum,
829 : uint16 procnum)
830 : {
831 : RegProcedure *loc;
832 : int nproc;
833 : int procindex;
834 :
835 1566670 : nproc = irel->rd_indam->amsupport;
836 :
837 : Assert(procnum > 0 && procnum <= (uint16) nproc);
838 :
839 1566670 : procindex = (nproc * (attnum - 1)) + (procnum - 1);
840 :
841 1566670 : loc = irel->rd_support;
842 :
843 : Assert(loc != NULL);
844 :
845 1566670 : return loc[procindex];
846 : }
847 :
848 : /* ----------------
849 : * index_getprocinfo
850 : *
851 : * This routine allows index AMs to keep fmgr lookup info for
852 : * support procs in the relcache. As above, only the "default"
853 : * functions for any particular indexed attribute are cached.
854 : *
855 : * Note: the return value points into cached data that will be lost during
856 : * any relcache rebuild! Therefore, either use the callinfo right away,
857 : * or save it only after having acquired some type of lock on the index rel.
858 : * ----------------
859 : */
860 : FmgrInfo *
861 39747576 : index_getprocinfo(Relation irel,
862 : AttrNumber attnum,
863 : uint16 procnum)
864 : {
865 : FmgrInfo *locinfo;
866 : int nproc;
867 : int optsproc;
868 : int procindex;
869 :
870 39747576 : nproc = irel->rd_indam->amsupport;
871 39747576 : optsproc = irel->rd_indam->amoptsprocnum;
872 :
873 : Assert(procnum > 0 && procnum <= (uint16) nproc);
874 :
875 39747576 : procindex = (nproc * (attnum - 1)) + (procnum - 1);
876 :
877 39747576 : locinfo = irel->rd_supportinfo;
878 :
879 : Assert(locinfo != NULL);
880 :
881 39747576 : locinfo += procindex;
882 :
883 : /* Initialize the lookup info if first time through */
884 39747576 : if (locinfo->fn_oid == InvalidOid)
885 : {
886 934034 : RegProcedure *loc = irel->rd_support;
887 : RegProcedure procId;
888 :
889 : Assert(loc != NULL);
890 :
891 934034 : procId = loc[procindex];
892 :
893 : /*
894 : * Complain if function was not found during IndexSupportInitialize.
895 : * This should not happen unless the system tables contain bogus
896 : * entries for the index opclass. (If an AM wants to allow a support
897 : * function to be optional, it can use index_getprocid.)
898 : */
899 934034 : if (!RegProcedureIsValid(procId))
900 0 : elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
901 : procnum, attnum, RelationGetRelationName(irel));
902 :
903 934034 : fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);
904 :
905 934034 : if (procnum != optsproc)
906 : {
907 : /* Initialize locinfo->fn_expr with opclass options Const */
908 932356 : bytea **attoptions = RelationGetIndexAttOptions(irel, false);
909 932356 : MemoryContext oldcxt = MemoryContextSwitchTo(irel->rd_indexcxt);
910 :
911 932356 : set_fn_opclass_options(locinfo, attoptions[attnum - 1]);
912 :
913 932356 : MemoryContextSwitchTo(oldcxt);
914 : }
915 : }
916 :
917 39747576 : return locinfo;
918 : }
919 :
920 : /* ----------------
921 : * index_store_float8_orderby_distances
922 : *
923 : * Convert AM distance function's results (that can be inexact)
924 : * to ORDER BY types and save them into xs_orderbyvals/xs_orderbynulls
925 : * for a possible recheck.
926 : * ----------------
927 : */
928 : void
929 364600 : index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes,
930 : IndexOrderByDistance *distances,
931 : bool recheckOrderBy)
932 : {
933 : int i;
934 :
935 : Assert(distances || !recheckOrderBy);
936 :
937 364600 : scan->xs_recheckorderby = recheckOrderBy;
938 :
939 729218 : for (i = 0; i < scan->numberOfOrderBys; i++)
940 : {
941 364618 : if (orderByTypes[i] == FLOAT8OID)
942 : {
943 : #ifndef USE_FLOAT8_BYVAL
944 : /* must free any old value to avoid memory leakage */
945 : if (!scan->xs_orderbynulls[i])
946 : pfree(DatumGetPointer(scan->xs_orderbyvals[i]));
947 : #endif
948 364488 : if (distances && !distances[i].isnull)
949 : {
950 364428 : scan->xs_orderbyvals[i] = Float8GetDatum(distances[i].value);
951 364428 : scan->xs_orderbynulls[i] = false;
952 : }
953 : else
954 : {
955 60 : scan->xs_orderbyvals[i] = (Datum) 0;
956 60 : scan->xs_orderbynulls[i] = true;
957 : }
958 : }
959 130 : else if (orderByTypes[i] == FLOAT4OID)
960 : {
961 : /* convert distance function's result to ORDER BY type */
962 70 : if (distances && !distances[i].isnull)
963 : {
964 70 : scan->xs_orderbyvals[i] = Float4GetDatum((float4) distances[i].value);
965 70 : scan->xs_orderbynulls[i] = false;
966 : }
967 : else
968 : {
969 0 : scan->xs_orderbyvals[i] = (Datum) 0;
970 0 : scan->xs_orderbynulls[i] = true;
971 : }
972 : }
973 : else
974 : {
975 : /*
976 : * If the ordering operator's return value is anything else, we
977 : * don't know how to convert the float8 bound calculated by the
978 : * distance function to that. The executor won't actually need
979 : * the order by values we return here, if there are no lossy
980 : * results, so only insist on converting if the *recheck flag is
981 : * set.
982 : */
983 60 : if (scan->xs_recheckorderby)
984 0 : elog(ERROR, "ORDER BY operator must return float8 or float4 if the distance function is lossy");
985 60 : scan->xs_orderbynulls[i] = true;
986 : }
987 : }
988 364600 : }
989 :
990 : /* ----------------
991 : * index_opclass_options
992 : *
993 : * Parse opclass-specific options for index column.
994 : * ----------------
995 : */
996 : bytea *
997 693160 : index_opclass_options(Relation indrel, AttrNumber attnum, Datum attoptions,
998 : bool validate)
999 : {
1000 693160 : int amoptsprocnum = indrel->rd_indam->amoptsprocnum;
1001 693160 : Oid procid = InvalidOid;
1002 : FmgrInfo *procinfo;
1003 : local_relopts relopts;
1004 :
1005 : /* fetch options support procedure if specified */
1006 693160 : if (amoptsprocnum != 0)
1007 693100 : procid = index_getprocid(indrel, attnum, amoptsprocnum);
1008 :
1009 693160 : if (!OidIsValid(procid))
1010 : {
1011 : Oid opclass;
1012 : Datum indclassDatum;
1013 : oidvector *indclass;
1014 :
1015 690752 : if (!DatumGetPointer(attoptions))
1016 690746 : return NULL; /* ok, no options, no procedure */
1017 :
1018 : /*
1019 : * Report an error if the opclass's options-parsing procedure does not
1020 : * exist but the opclass options are specified.
1021 : */
1022 6 : indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, indrel->rd_indextuple,
1023 : Anum_pg_index_indclass);
1024 6 : indclass = (oidvector *) DatumGetPointer(indclassDatum);
1025 6 : opclass = indclass->values[attnum - 1];
1026 :
1027 6 : ereport(ERROR,
1028 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1029 : errmsg("operator class %s has no options",
1030 : generate_opclass_name(opclass))));
1031 : }
1032 :
1033 2408 : init_local_reloptions(&relopts, 0);
1034 :
1035 2408 : procinfo = index_getprocinfo(indrel, attnum, amoptsprocnum);
1036 :
1037 2408 : (void) FunctionCall1(procinfo, PointerGetDatum(&relopts));
1038 :
1039 2408 : return build_local_reloptions(&relopts, attoptions, validate);
1040 : }
|