1 /*-------------------------------------------------------------------------
4 * general index access method routines
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/access/index/indexam.c,v 1.84 2005/06/27 12:45:22 teodor Exp $
14 * index_open - open an index relation by relation OID
15 * index_openrv - open an index relation specified by a RangeVar
16 * index_close - close an index relation
17 * index_beginscan - start a scan of an index with amgettuple
18 * index_beginscan_multi - start a scan of an index with amgetmulti
19 * index_rescan - restart a scan of an index
20 * index_endscan - end a scan
21 * index_insert - insert an index tuple into a relation
22 * index_markpos - mark a scan position
23 * index_restrpos - restore a scan position
24 * index_getnext - get the next tuple from a scan
25 * index_getmulti - get multiple tuples from a scan
26 * index_bulk_delete - bulk deletion of index tuples
27 * index_vacuum_cleanup - post-deletion cleanup of an index
28 * index_getprocid - get a support procedure OID
29 * index_getprocinfo - get a support procedure's lookup info
32 * This file contains the index_ routines which used
33 * to be a scattered collection of stuff in access/genam.
37 * Scans are implemented as follows:
39 * `0' represents an invalid item pointer.
40 * `-' represents an unknown item pointer.
41 * `X' represents a known item pointers.
42 * `+' represents known or invalid item pointers.
43 * `*' represents any item pointers.
45 * State is represented by a triple of these symbols in the order of
46 * previous, current, next. Note that the case of reverse scans works
50 * (1) + + - + 0 0 (if the next item pointer is invalid)
51 * (2) + X - (otherwise)
52 * (3) * 0 0 * 0 0 (no change)
53 * (4) + X 0 X 0 0 (shift)
54 * (5) * + X + X - (shift, add unknown)
56 * All other states cannot occur.
58 * Note: It would be possible to cache the status of the previous and
59 * next item pointer using the flags.
61 *-------------------------------------------------------------------------
66 #include "access/genam.h"
67 #include "access/heapam.h"
68 #include "utils/relcache.h"
72 /* ----------------------------------------------------------------
73 * macros used in index_ routines
74 * ----------------------------------------------------------------
76 #define RELATION_CHECKS \
78 AssertMacro(RelationIsValid(indexRelation)), \
79 AssertMacro(PointerIsValid(indexRelation->rd_am)) \
84 AssertMacro(IndexScanIsValid(scan)), \
85 AssertMacro(RelationIsValid(scan->indexRelation)), \
86 AssertMacro(PointerIsValid(scan->indexRelation->rd_am)) \
89 #define GET_REL_PROCEDURE(pname) \
91 procedure = &indexRelation->rd_aminfo->pname; \
92 if (!OidIsValid(procedure->fn_oid)) \
94 RegProcedure procOid = indexRelation->rd_am->pname; \
95 if (!RegProcedureIsValid(procOid)) \
96 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
97 fmgr_info_cxt(procOid, procedure, indexRelation->rd_indexcxt); \
101 #define GET_SCAN_PROCEDURE(pname) \
103 procedure = &scan->indexRelation->rd_aminfo->pname; \
104 if (!OidIsValid(procedure->fn_oid)) \
106 RegProcedure procOid = scan->indexRelation->rd_am->pname; \
107 if (!RegProcedureIsValid(procOid)) \
108 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
109 fmgr_info_cxt(procOid, procedure, scan->indexRelation->rd_indexcxt); \
113 static IndexScanDesc index_beginscan_internal(Relation indexRelation,
114 int nkeys, ScanKey key);
117 /* ----------------------------------------------------------------
118 * index_ interface functions
119 * ----------------------------------------------------------------
123 * index_open - open an index relation by relation OID
125 * Note: we acquire no lock on the index. A lock is not needed when
126 * simply examining the index reldesc; the index's schema information
127 * is considered to be protected by the lock that the caller had better
128 * be holding on the parent relation. Some type of lock should be
129 * obtained on the index before physically accessing it, however.
130 * This is handled automatically for most uses by index_beginscan
131 * and index_endscan for scan cases, or by ExecOpenIndices and
132 * ExecCloseIndices for update cases. Other callers will need to
133 * obtain their own locks.
135 * This is a convenience routine adapted for indexscan use.
136 * Some callers may prefer to use relation_open directly.
140 index_open(Oid relationId)
144 r = relation_open(relationId, NoLock);
146 if (r->rd_rel->relkind != RELKIND_INDEX)
148 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
149 errmsg("\"%s\" is not an index",
150 RelationGetRelationName(r))));
152 pgstat_initstats(&r->pgstat_info, r);
158 * index_openrv - open an index relation specified
161 * As above, but relation is specified by a RangeVar.
165 index_openrv(const RangeVar *relation)
169 r = relation_openrv(relation, NoLock);
171 if (r->rd_rel->relkind != RELKIND_INDEX)
173 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
174 errmsg("\"%s\" is not an index",
175 RelationGetRelationName(r))));
177 pgstat_initstats(&r->pgstat_info, r);
183 * index_close - close a index relation
185 * presently the relcache routines do all the work we need
186 * to open/close index relations.
190 index_close(Relation relation)
192 RelationClose(relation);
196 * index_insert - insert an index tuple into a relation
200 index_insert(Relation indexRelation,
203 ItemPointer heap_t_ctid,
204 Relation heapRelation,
205 bool check_uniqueness)
210 GET_REL_PROCEDURE(aminsert);
213 * have the am's insert proc do all the work.
215 return DatumGetBool(FunctionCall6(procedure,
216 PointerGetDatum(indexRelation),
217 PointerGetDatum(values),
218 PointerGetDatum(isnull),
219 PointerGetDatum(heap_t_ctid),
220 PointerGetDatum(heapRelation),
221 BoolGetDatum(check_uniqueness)));
225 * index_beginscan - start a scan of an index with amgettuple
227 * Note: heapRelation may be NULL if there is no intention of calling
228 * index_getnext on this scan; index_getnext_indexitem will not use the
229 * heapRelation link (nor the snapshot). However, the caller had better
230 * be holding some kind of lock on the heap relation in any case, to ensure
231 * no one deletes it (or the index) out from under us.
234 index_beginscan(Relation heapRelation,
235 Relation indexRelation,
237 int nkeys, ScanKey key)
241 scan = index_beginscan_internal(indexRelation, nkeys, key);
244 * Save additional parameters into the scandesc. Everything else was
245 * set up by RelationGetIndexScan.
247 scan->is_multiscan = false;
248 scan->heapRelation = heapRelation;
249 scan->xs_snapshot = snapshot;
255 * index_beginscan_multi - start a scan of an index with amgetmulti
257 * As above, caller had better be holding some lock on the parent heap
258 * relation, even though it's not explicitly mentioned here.
261 index_beginscan_multi(Relation indexRelation,
263 int nkeys, ScanKey key)
267 scan = index_beginscan_internal(indexRelation, nkeys, key);
270 * Save additional parameters into the scandesc. Everything else was
271 * set up by RelationGetIndexScan.
273 scan->is_multiscan = true;
274 scan->xs_snapshot = snapshot;
280 * index_beginscan_internal --- common code for index_beginscan variants
283 index_beginscan_internal(Relation indexRelation,
284 int nkeys, ScanKey key)
291 RelationIncrementReferenceCount(indexRelation);
294 * Acquire AccessShareLock for the duration of the scan
296 * Note: we could get an SI inval message here and consequently have to
297 * rebuild the relcache entry. The refcount increment above ensures
298 * that we will rebuild it and not just flush it...
300 LockRelation(indexRelation, AccessShareLock);
303 * LockRelation can clean rd_aminfo structure, so fill procedure
307 GET_REL_PROCEDURE(ambeginscan);
310 * Tell the AM to open a scan.
312 scan = (IndexScanDesc)
313 DatumGetPointer(FunctionCall3(procedure,
314 PointerGetDatum(indexRelation),
315 Int32GetDatum(nkeys),
316 PointerGetDatum(key)));
322 * index_rescan - (re)start a scan of an index
324 * The caller may specify a new set of scankeys (but the number of keys
325 * cannot change). To restart the scan without changing keys, pass NULL
328 * Note that this is also called when first starting an indexscan;
329 * see RelationGetIndexScan. Keys *must* be passed in that case,
330 * unless scan->numberOfKeys is zero.
334 index_rescan(IndexScanDesc scan, ScanKey key)
339 GET_SCAN_PROCEDURE(amrescan);
341 /* Release any held pin on a heap page */
342 if (BufferIsValid(scan->xs_cbuf))
344 ReleaseBuffer(scan->xs_cbuf);
345 scan->xs_cbuf = InvalidBuffer;
348 scan->kill_prior_tuple = false; /* for safety */
349 scan->keys_are_unique = false; /* may be set by index AM */
350 scan->got_tuple = false;
351 scan->unique_tuple_pos = 0;
352 scan->unique_tuple_mark = 0;
354 FunctionCall2(procedure,
355 PointerGetDatum(scan),
356 PointerGetDatum(key));
358 pgstat_reset_index_scan(&scan->xs_pgstat_info);
362 * index_endscan - end a scan
366 index_endscan(IndexScanDesc scan)
371 GET_SCAN_PROCEDURE(amendscan);
373 /* Release any held pin on a heap page */
374 if (BufferIsValid(scan->xs_cbuf))
376 ReleaseBuffer(scan->xs_cbuf);
377 scan->xs_cbuf = InvalidBuffer;
380 /* End the AM's scan */
381 FunctionCall1(procedure, PointerGetDatum(scan));
383 /* Release index lock and refcount acquired by index_beginscan */
385 UnlockRelation(scan->indexRelation, AccessShareLock);
387 RelationDecrementReferenceCount(scan->indexRelation);
389 /* Release the scan data structure itself */
394 * index_markpos - mark a scan position
398 index_markpos(IndexScanDesc scan)
403 GET_SCAN_PROCEDURE(ammarkpos);
405 scan->unique_tuple_mark = scan->unique_tuple_pos;
407 FunctionCall1(procedure, PointerGetDatum(scan));
411 * index_restrpos - restore a scan position
413 * NOTE: this only restores the internal scan state of the index AM.
414 * The current result tuple (scan->xs_ctup) doesn't change. See comments
415 * for ExecRestrPos().
419 index_restrpos(IndexScanDesc scan)
424 GET_SCAN_PROCEDURE(amrestrpos);
426 scan->kill_prior_tuple = false; /* for safety */
429 * We do not reset got_tuple; so if the scan is actually being
430 * short-circuited by index_getnext, the effective position
431 * restoration is done by restoring unique_tuple_pos.
433 scan->unique_tuple_pos = scan->unique_tuple_mark;
435 FunctionCall1(procedure, PointerGetDatum(scan));
439 * index_getnext - get the next heap tuple from a scan
441 * The result is the next heap tuple satisfying the scan keys and the
442 * snapshot, or NULL if no more matching tuples exist. On success,
443 * the buffer containing the heap tuple is pinned (the pin will be dropped
444 * at the next index_getnext or index_endscan). The index TID corresponding
445 * to the heap tuple can be obtained if needed from scan->currentItemData.
449 index_getnext(IndexScanDesc scan, ScanDirection direction)
451 HeapTuple heapTuple = &scan->xs_ctup;
455 GET_SCAN_PROCEDURE(amgettuple);
458 * If we already got a tuple and it must be unique, there's no need to
459 * make the index AM look through any additional tuples. (This can
460 * save a useful amount of work in scenarios where there are many dead
461 * tuples due to heavy update activity.)
463 * To do this we must keep track of the logical scan position
464 * (before/on/after tuple). Also, we have to be sure to release scan
465 * resources before returning NULL; if we fail to do so then a
466 * multi-index scan can easily run the system out of free buffers. We
467 * can release index-level resources fairly cheaply by calling
468 * index_rescan. This means there are two persistent states as far as
469 * the index AM is concerned: on-tuple and rescanned. If we are
470 * actually asked to re-fetch the single tuple, we have to go through
471 * a fresh indexscan startup, which penalizes that (infrequent) case.
473 if (scan->keys_are_unique && scan->got_tuple)
475 int new_tuple_pos = scan->unique_tuple_pos;
477 if (ScanDirectionIsForward(direction))
479 if (new_tuple_pos <= 0)
484 if (new_tuple_pos >= 0)
487 if (new_tuple_pos == 0)
490 * We are moving onto the unique tuple from having been off
491 * it. We just fall through and let the index AM do the work.
492 * Note we should get the right answer regardless of scan
495 scan->unique_tuple_pos = 0; /* need to update position */
500 * Moving off the tuple; must do amrescan to release
501 * index-level pins before we return NULL. Since index_rescan
502 * will reset my state, must save and restore...
504 int unique_tuple_mark = scan->unique_tuple_mark;
506 index_rescan(scan, NULL /* no change to key */ );
508 scan->keys_are_unique = true;
509 scan->got_tuple = true;
510 scan->unique_tuple_pos = new_tuple_pos;
511 scan->unique_tuple_mark = unique_tuple_mark;
517 /* just make sure this is false... */
518 scan->kill_prior_tuple = false;
524 pgstat_count_index_scan(&scan->xs_pgstat_info);
527 * The AM's gettuple proc finds the next tuple matching the scan
530 found = DatumGetBool(FunctionCall2(procedure,
531 PointerGetDatum(scan),
532 Int32GetDatum(direction)));
534 /* Reset kill flag immediately for safety */
535 scan->kill_prior_tuple = false;
539 /* Release any held pin on a heap page */
540 if (BufferIsValid(scan->xs_cbuf))
542 ReleaseBuffer(scan->xs_cbuf);
543 scan->xs_cbuf = InvalidBuffer;
545 return NULL; /* failure exit */
549 * Fetch the heap tuple and see if it matches the snapshot.
551 if (heap_release_fetch(scan->heapRelation, scan->xs_snapshot,
552 heapTuple, &scan->xs_cbuf, true,
553 &scan->xs_pgstat_info))
556 /* Skip if no undeleted tuple at this location */
557 if (heapTuple->t_data == NULL)
561 * If we can't see it, maybe no one else can either. Check to see
562 * if the tuple is dead to all transactions. If so, signal the
563 * index AM to not return it on future indexscans.
565 * We told heap_release_fetch to keep a pin on the buffer, so we can
566 * re-access the tuple here. But we must re-lock the buffer first.
568 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
570 if (HeapTupleSatisfiesVacuum(heapTuple->t_data, RecentGlobalXmin,
571 scan->xs_cbuf) == HEAPTUPLE_DEAD)
572 scan->kill_prior_tuple = true;
574 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
578 scan->got_tuple = true;
581 * If we just fetched a known-unique tuple, then subsequent calls will
582 * go through the short-circuit code above. unique_tuple_pos has been
583 * initialized to 0, which is the correct state ("on row").
586 pgstat_count_index_getnext(&scan->xs_pgstat_info);
592 * index_getnext_indexitem - get the next index tuple from a scan
594 * Finds the next index tuple satisfying the scan keys. Note that the
595 * corresponding heap tuple is not accessed, and thus no time qual (snapshot)
596 * check is done, other than the index AM's internal check for killed tuples
597 * (which most callers of this routine will probably want to suppress by
598 * setting scan->ignore_killed_tuples = false).
600 * On success (TRUE return), the found index TID is in scan->currentItemData,
601 * and its heap TID is in scan->xs_ctup.t_self. scan->xs_cbuf is untouched.
605 index_getnext_indexitem(IndexScanDesc scan,
606 ScanDirection direction)
612 GET_SCAN_PROCEDURE(amgettuple);
614 /* just make sure this is false... */
615 scan->kill_prior_tuple = false;
618 * have the am's gettuple proc do all the work.
620 found = DatumGetBool(FunctionCall2(procedure,
621 PointerGetDatum(scan),
622 Int32GetDatum(direction)));
628 * index_getmulti - get multiple tuples from an index scan
630 * Collects the TIDs of multiple heap tuples satisfying the scan keys.
631 * Since there's no interlock between the index scan and the eventual heap
632 * access, this is only safe to use with MVCC-based snapshots: the heap
633 * item slot could have been replaced by a newer tuple by the time we get
636 * A TRUE result indicates more calls should occur; a FALSE result says the
637 * scan is done. *returned_tids could be zero or nonzero in either case.
641 index_getmulti(IndexScanDesc scan,
642 ItemPointer tids, int32 max_tids,
643 int32 *returned_tids)
649 GET_SCAN_PROCEDURE(amgetmulti);
651 /* just make sure this is false... */
652 scan->kill_prior_tuple = false;
655 * have the am's getmulti proc do all the work.
657 found = DatumGetBool(FunctionCall4(procedure,
658 PointerGetDatum(scan),
659 PointerGetDatum(tids),
660 Int32GetDatum(max_tids),
661 PointerGetDatum(returned_tids)));
667 * index_bulk_delete - do mass deletion of index entries
669 * callback routine tells whether a given main-heap tuple is
672 * return value is an optional palloc'd struct of statistics
675 IndexBulkDeleteResult *
676 index_bulk_delete(Relation indexRelation,
677 IndexBulkDeleteCallback callback,
678 void *callback_state)
681 IndexBulkDeleteResult *result;
684 GET_REL_PROCEDURE(ambulkdelete);
686 result = (IndexBulkDeleteResult *)
687 DatumGetPointer(FunctionCall3(procedure,
688 PointerGetDatum(indexRelation),
689 PointerGetDatum((Pointer) callback),
690 PointerGetDatum(callback_state)));
696 * index_vacuum_cleanup - do post-deletion cleanup of an index
698 * return value is an optional palloc'd struct of statistics
701 IndexBulkDeleteResult *
702 index_vacuum_cleanup(Relation indexRelation,
703 IndexVacuumCleanupInfo *info,
704 IndexBulkDeleteResult *stats)
707 IndexBulkDeleteResult *result;
711 /* It's okay for an index AM not to have a vacuumcleanup procedure */
712 if (!RegProcedureIsValid(indexRelation->rd_am->amvacuumcleanup))
715 GET_REL_PROCEDURE(amvacuumcleanup);
717 result = (IndexBulkDeleteResult *)
718 DatumGetPointer(FunctionCall3(procedure,
719 PointerGetDatum(indexRelation),
720 PointerGetDatum((Pointer) info),
721 PointerGetDatum((Pointer) stats)));
729 * Some indexed access methods may require support routines that are
730 * not in the operator class/operator model imposed by pg_am. These
731 * access methods may store the OIDs of registered procedures they
732 * need in pg_amproc. These registered procedure OIDs are ordered in
733 * a way that makes sense to the access method, and used only by the
734 * access method. The general index code doesn't know anything about
735 * the routines involved; it just builds an ordered list of them for
736 * each attribute on which an index is defined.
738 * This routine returns the requested procedure OID for a particular
743 index_getprocid(Relation irel,
751 nproc = irel->rd_am->amsupport;
753 Assert(procnum > 0 && procnum <= (uint16) nproc);
755 procindex = (nproc * (attnum - 1)) + (procnum - 1);
757 loc = irel->rd_support;
761 return loc[procindex];
767 * This routine allows index AMs to keep fmgr lookup info for
768 * support procs in the relcache.
770 * Note: the return value points into cached data that will be lost during
771 * any relcache rebuild! Therefore, either use the callinfo right away,
772 * or save it only after having acquired some type of lock on the index rel.
776 index_getprocinfo(Relation irel,
784 nproc = irel->rd_am->amsupport;
786 Assert(procnum > 0 && procnum <= (uint16) nproc);
788 procindex = (nproc * (attnum - 1)) + (procnum - 1);
790 locinfo = irel->rd_supportinfo;
792 Assert(locinfo != NULL);
794 locinfo += procindex;
796 /* Initialize the lookup info if first time through */
797 if (locinfo->fn_oid == InvalidOid)
799 RegProcedure *loc = irel->rd_support;
804 procId = loc[procindex];
807 * Complain if function was not found during
808 * IndexSupportInitialize. This should not happen unless the
809 * system tables contain bogus entries for the index opclass. (If
810 * an AM wants to allow a support function to be optional, it can
811 * use index_getprocid.)
813 if (!RegProcedureIsValid(procId))
814 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
815 procnum, attnum, RelationGetRelationName(irel));
817 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);