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.85 2005/10/06 02:29:11 tgl 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"
69 #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));
360 * index_endscan - end a scan
364 index_endscan(IndexScanDesc scan)
369 GET_SCAN_PROCEDURE(amendscan);
371 /* Release any held pin on a heap page */
372 if (BufferIsValid(scan->xs_cbuf))
374 ReleaseBuffer(scan->xs_cbuf);
375 scan->xs_cbuf = InvalidBuffer;
378 /* End the AM's scan */
379 FunctionCall1(procedure, PointerGetDatum(scan));
381 /* Release index lock and refcount acquired by index_beginscan */
383 UnlockRelation(scan->indexRelation, AccessShareLock);
385 RelationDecrementReferenceCount(scan->indexRelation);
387 /* Release the scan data structure itself */
392 * index_markpos - mark a scan position
396 index_markpos(IndexScanDesc scan)
401 GET_SCAN_PROCEDURE(ammarkpos);
403 scan->unique_tuple_mark = scan->unique_tuple_pos;
405 FunctionCall1(procedure, PointerGetDatum(scan));
409 * index_restrpos - restore a scan position
411 * NOTE: this only restores the internal scan state of the index AM.
412 * The current result tuple (scan->xs_ctup) doesn't change. See comments
413 * for ExecRestrPos().
417 index_restrpos(IndexScanDesc scan)
422 GET_SCAN_PROCEDURE(amrestrpos);
424 scan->kill_prior_tuple = false; /* for safety */
427 * We do not reset got_tuple; so if the scan is actually being
428 * short-circuited by index_getnext, the effective position
429 * restoration is done by restoring unique_tuple_pos.
431 scan->unique_tuple_pos = scan->unique_tuple_mark;
433 FunctionCall1(procedure, PointerGetDatum(scan));
437 * index_getnext - get the next heap tuple from a scan
439 * The result is the next heap tuple satisfying the scan keys and the
440 * snapshot, or NULL if no more matching tuples exist. On success,
441 * the buffer containing the heap tuple is pinned (the pin will be dropped
442 * at the next index_getnext or index_endscan). The index TID corresponding
443 * to the heap tuple can be obtained if needed from scan->currentItemData.
447 index_getnext(IndexScanDesc scan, ScanDirection direction)
449 HeapTuple heapTuple = &scan->xs_ctup;
453 GET_SCAN_PROCEDURE(amgettuple);
456 * If we already got a tuple and it must be unique, there's no need to
457 * make the index AM look through any additional tuples. (This can
458 * save a useful amount of work in scenarios where there are many dead
459 * tuples due to heavy update activity.)
461 * To do this we must keep track of the logical scan position
462 * (before/on/after tuple). Also, we have to be sure to release scan
463 * resources before returning NULL; if we fail to do so then a
464 * multi-index scan can easily run the system out of free buffers. We
465 * can release index-level resources fairly cheaply by calling
466 * index_rescan. This means there are two persistent states as far as
467 * the index AM is concerned: on-tuple and rescanned. If we are
468 * actually asked to re-fetch the single tuple, we have to go through
469 * a fresh indexscan startup, which penalizes that (infrequent) case.
471 if (scan->keys_are_unique && scan->got_tuple)
473 int new_tuple_pos = scan->unique_tuple_pos;
475 if (ScanDirectionIsForward(direction))
477 if (new_tuple_pos <= 0)
482 if (new_tuple_pos >= 0)
485 if (new_tuple_pos == 0)
488 * We are moving onto the unique tuple from having been off
489 * it. We just fall through and let the index AM do the work.
490 * Note we should get the right answer regardless of scan
493 scan->unique_tuple_pos = 0; /* need to update position */
498 * Moving off the tuple; must do amrescan to release
499 * index-level pins before we return NULL. Since index_rescan
500 * will reset my state, must save and restore...
502 int unique_tuple_mark = scan->unique_tuple_mark;
504 index_rescan(scan, NULL /* no change to key */ );
506 scan->keys_are_unique = true;
507 scan->got_tuple = true;
508 scan->unique_tuple_pos = new_tuple_pos;
509 scan->unique_tuple_mark = unique_tuple_mark;
515 /* just make sure this is false... */
516 scan->kill_prior_tuple = false;
523 * The AM's gettuple proc finds the next tuple matching the scan
526 found = DatumGetBool(FunctionCall2(procedure,
527 PointerGetDatum(scan),
528 Int32GetDatum(direction)));
530 /* Reset kill flag immediately for safety */
531 scan->kill_prior_tuple = false;
535 /* Release any held pin on a heap page */
536 if (BufferIsValid(scan->xs_cbuf))
538 ReleaseBuffer(scan->xs_cbuf);
539 scan->xs_cbuf = InvalidBuffer;
541 return NULL; /* failure exit */
544 pgstat_count_index_tuples(&scan->xs_pgstat_info, 1);
547 * Fetch the heap tuple and see if it matches the snapshot.
549 if (heap_release_fetch(scan->heapRelation, scan->xs_snapshot,
550 heapTuple, &scan->xs_cbuf, true,
551 &scan->xs_pgstat_info))
554 /* Skip if no undeleted tuple at this location */
555 if (heapTuple->t_data == NULL)
559 * If we can't see it, maybe no one else can either. Check to see
560 * if the tuple is dead to all transactions. If so, signal the
561 * index AM to not return it on future indexscans.
563 * We told heap_release_fetch to keep a pin on the buffer, so we can
564 * re-access the tuple here. But we must re-lock the buffer first.
566 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
568 if (HeapTupleSatisfiesVacuum(heapTuple->t_data, RecentGlobalXmin,
569 scan->xs_cbuf) == HEAPTUPLE_DEAD)
570 scan->kill_prior_tuple = true;
572 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
576 scan->got_tuple = true;
579 * If we just fetched a known-unique tuple, then subsequent calls will
580 * go through the short-circuit code above. unique_tuple_pos has been
581 * initialized to 0, which is the correct state ("on row").
588 * index_getnext_indexitem - get the next index tuple from a scan
590 * Finds the next index tuple satisfying the scan keys. Note that the
591 * corresponding heap tuple is not accessed, and thus no time qual (snapshot)
592 * check is done, other than the index AM's internal check for killed tuples
593 * (which most callers of this routine will probably want to suppress by
594 * setting scan->ignore_killed_tuples = false).
596 * On success (TRUE return), the found index TID is in scan->currentItemData,
597 * and its heap TID is in scan->xs_ctup.t_self. scan->xs_cbuf is untouched.
601 index_getnext_indexitem(IndexScanDesc scan,
602 ScanDirection direction)
608 GET_SCAN_PROCEDURE(amgettuple);
610 /* just make sure this is false... */
611 scan->kill_prior_tuple = false;
614 * have the am's gettuple proc do all the work.
616 found = DatumGetBool(FunctionCall2(procedure,
617 PointerGetDatum(scan),
618 Int32GetDatum(direction)));
621 pgstat_count_index_tuples(&scan->xs_pgstat_info, 1);
627 * index_getmulti - get multiple tuples from an index scan
629 * Collects the TIDs of multiple heap tuples satisfying the scan keys.
630 * Since there's no interlock between the index scan and the eventual heap
631 * access, this is only safe to use with MVCC-based snapshots: the heap
632 * item slot could have been replaced by a newer tuple by the time we get
635 * A TRUE result indicates more calls should occur; a FALSE result says the
636 * scan is done. *returned_tids could be zero or nonzero in either case.
640 index_getmulti(IndexScanDesc scan,
641 ItemPointer tids, int32 max_tids,
642 int32 *returned_tids)
648 GET_SCAN_PROCEDURE(amgetmulti);
650 /* just make sure this is false... */
651 scan->kill_prior_tuple = false;
654 * have the am's getmulti proc do all the work.
656 found = DatumGetBool(FunctionCall4(procedure,
657 PointerGetDatum(scan),
658 PointerGetDatum(tids),
659 Int32GetDatum(max_tids),
660 PointerGetDatum(returned_tids)));
662 pgstat_count_index_tuples(&scan->xs_pgstat_info, *returned_tids);
668 * index_bulk_delete - do mass deletion of index entries
670 * callback routine tells whether a given main-heap tuple is
673 * return value is an optional palloc'd struct of statistics
676 IndexBulkDeleteResult *
677 index_bulk_delete(Relation indexRelation,
678 IndexBulkDeleteCallback callback,
679 void *callback_state)
682 IndexBulkDeleteResult *result;
685 GET_REL_PROCEDURE(ambulkdelete);
687 result = (IndexBulkDeleteResult *)
688 DatumGetPointer(FunctionCall3(procedure,
689 PointerGetDatum(indexRelation),
690 PointerGetDatum((Pointer) callback),
691 PointerGetDatum(callback_state)));
697 * index_vacuum_cleanup - do post-deletion cleanup of an index
699 * return value is an optional palloc'd struct of statistics
702 IndexBulkDeleteResult *
703 index_vacuum_cleanup(Relation indexRelation,
704 IndexVacuumCleanupInfo *info,
705 IndexBulkDeleteResult *stats)
708 IndexBulkDeleteResult *result;
712 /* It's okay for an index AM not to have a vacuumcleanup procedure */
713 if (!RegProcedureIsValid(indexRelation->rd_am->amvacuumcleanup))
716 GET_REL_PROCEDURE(amvacuumcleanup);
718 result = (IndexBulkDeleteResult *)
719 DatumGetPointer(FunctionCall3(procedure,
720 PointerGetDatum(indexRelation),
721 PointerGetDatum((Pointer) info),
722 PointerGetDatum((Pointer) stats)));
730 * Some indexed access methods may require support routines that are
731 * not in the operator class/operator model imposed by pg_am. These
732 * access methods may store the OIDs of registered procedures they
733 * need in pg_amproc. These registered procedure OIDs are ordered in
734 * a way that makes sense to the access method, and used only by the
735 * access method. The general index code doesn't know anything about
736 * the routines involved; it just builds an ordered list of them for
737 * each attribute on which an index is defined.
739 * This routine returns the requested procedure OID for a particular
744 index_getprocid(Relation irel,
752 nproc = irel->rd_am->amsupport;
754 Assert(procnum > 0 && procnum <= (uint16) nproc);
756 procindex = (nproc * (attnum - 1)) + (procnum - 1);
758 loc = irel->rd_support;
762 return loc[procindex];
768 * This routine allows index AMs to keep fmgr lookup info for
769 * support procs in the relcache.
771 * Note: the return value points into cached data that will be lost during
772 * any relcache rebuild! Therefore, either use the callinfo right away,
773 * or save it only after having acquired some type of lock on the index rel.
777 index_getprocinfo(Relation irel,
785 nproc = irel->rd_am->amsupport;
787 Assert(procnum > 0 && procnum <= (uint16) nproc);
789 procindex = (nproc * (attnum - 1)) + (procnum - 1);
791 locinfo = irel->rd_supportinfo;
793 Assert(locinfo != NULL);
795 locinfo += procindex;
797 /* Initialize the lookup info if first time through */
798 if (locinfo->fn_oid == InvalidOid)
800 RegProcedure *loc = irel->rd_support;
805 procId = loc[procindex];
808 * Complain if function was not found during
809 * IndexSupportInitialize. This should not happen unless the
810 * system tables contain bogus entries for the index opclass. (If
811 * an AM wants to allow a support function to be optional, it can
812 * use index_getprocid.)
814 if (!RegProcedureIsValid(procId))
815 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
816 procnum, attnum, RelationGetRelationName(irel));
818 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);