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