1 /*-------------------------------------------------------------------------
4 * general index access method routines
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/index/indexam.c,v 1.71 2003/09/25 06:57:57 petere Exp $
14 * index_open - open an index relation by relation OID
15 * index_openrv - open an index relation specified by a RangeVar
16 * index_openr - open a system index relation by name
17 * index_close - close an index relation
18 * index_beginscan - start a scan of an index
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_bulk_delete - bulk deletion of index tuples
26 * index_vacuum_cleanup - post-deletion cleanup of an index
27 * index_cost_estimator - fetch amcostestimate procedure OID
28 * index_getprocid - get a support procedure OID
31 * This file contains the index_ routines which used
32 * to be a scattered collection of stuff in access/genam.
36 * Scans are implemented as follows:
38 * `0' represents an invalid item pointer.
39 * `-' represents an unknown item pointer.
40 * `X' represents a known item pointers.
41 * `+' represents known or invalid item pointers.
42 * `*' represents any item pointers.
44 * State is represented by a triple of these symbols in the order of
45 * previous, current, next. Note that the case of reverse scans works
49 * (1) + + - + 0 0 (if the next item pointer is invalid)
50 * (2) + X - (otherwise)
51 * (3) * 0 0 * 0 0 (no change)
52 * (4) + X 0 X 0 0 (shift)
53 * (5) * + X + X - (shift, add unknown)
55 * All other states cannot occur.
57 * Note: It would be possible to cache the status of the previous and
58 * next item pointer using the flags.
60 *-------------------------------------------------------------------------
65 #include "access/genam.h"
66 #include "access/heapam.h"
67 #include "utils/relcache.h"
71 /* ----------------------------------------------------------------
72 * macros used in index_ routines
73 * ----------------------------------------------------------------
75 #define RELATION_CHECKS \
77 AssertMacro(RelationIsValid(indexRelation)), \
78 AssertMacro(PointerIsValid(indexRelation->rd_am)) \
83 AssertMacro(IndexScanIsValid(scan)), \
84 AssertMacro(RelationIsValid(scan->indexRelation)), \
85 AssertMacro(PointerIsValid(scan->indexRelation->rd_am)) \
88 #define GET_REL_PROCEDURE(x,y) \
90 procedure = indexRelation->rd_am->y, \
91 (!RegProcedureIsValid(procedure)) ? \
92 elog(ERROR, "index_%s: invalid %s regproc", \
93 CppAsString(x), CppAsString(y)) \
97 #define GET_SCAN_PROCEDURE(x,y) \
99 procedure = scan->indexRelation->rd_am->y, \
100 (!RegProcedureIsValid(procedure)) ? \
101 elog(ERROR, "index_%s: invalid %s regproc", \
102 CppAsString(x), CppAsString(y)) \
107 /* ----------------------------------------------------------------
108 * index_ interface functions
109 * ----------------------------------------------------------------
113 * index_open - open an index relation by relation OID
115 * Note: we acquire no lock on the index. An AccessShareLock is
116 * acquired by index_beginscan (and released by index_endscan).
117 * Generally, the caller should already hold some type of lock on
118 * the parent relation to ensure that the index doesn't disappear.
120 * This is a convenience routine adapted for indexscan use.
121 * Some callers may prefer to use relation_open directly.
125 index_open(Oid relationId)
129 r = relation_open(relationId, NoLock);
131 if (r->rd_rel->relkind != RELKIND_INDEX)
133 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
134 errmsg("\"%s\" is not an index",
135 RelationGetRelationName(r))));
137 pgstat_initstats(&r->pgstat_info, r);
143 * index_openrv - open an index relation specified
146 * As above, but relation is specified by a RangeVar.
150 index_openrv(const RangeVar *relation)
154 r = relation_openrv(relation, NoLock);
156 if (r->rd_rel->relkind != RELKIND_INDEX)
158 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
159 errmsg("\"%s\" is not an index",
160 RelationGetRelationName(r))));
162 pgstat_initstats(&r->pgstat_info, r);
168 * index_openr - open a system index relation specified by name.
170 * As above, but the relation is specified by an unqualified name;
171 * it is assumed to live in the system catalog namespace.
175 index_openr(const char *sysRelationName)
179 r = relation_openr(sysRelationName, NoLock);
181 if (r->rd_rel->relkind != RELKIND_INDEX)
183 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
184 errmsg("\"%s\" is not an index",
185 RelationGetRelationName(r))));
187 pgstat_initstats(&r->pgstat_info, r);
193 * index_close - close a index relation
195 * presently the relcache routines do all the work we need
196 * to open/close index relations.
200 index_close(Relation relation)
202 RelationClose(relation);
206 * index_insert - insert an index tuple into a relation
210 index_insert(Relation indexRelation,
213 ItemPointer heap_t_ctid,
214 Relation heapRelation,
215 bool check_uniqueness)
217 RegProcedure procedure;
218 InsertIndexResult specificResult;
221 GET_REL_PROCEDURE(insert, aminsert);
224 * have the am's insert proc do all the work.
226 specificResult = (InsertIndexResult)
227 DatumGetPointer(OidFunctionCall6(procedure,
228 PointerGetDatum(indexRelation),
229 PointerGetDatum(datums),
230 PointerGetDatum(nulls),
231 PointerGetDatum(heap_t_ctid),
232 PointerGetDatum(heapRelation),
233 BoolGetDatum(check_uniqueness)));
235 /* must be pfree'ed */
236 return specificResult;
240 * index_beginscan - start a scan of an index
242 * Note: heapRelation may be NULL if there is no intention of calling
243 * index_getnext on this scan; index_getnext_indexitem will not use the
244 * heapRelation link (nor the snapshot). However, the caller had better
245 * be holding some kind of lock on the heap relation in any case, to ensure
246 * no one deletes it (or the index) out from under us.
250 index_beginscan(Relation heapRelation,
251 Relation indexRelation,
253 int nkeys, ScanKey key)
256 RegProcedure procedure;
259 GET_REL_PROCEDURE(beginscan, ambeginscan);
261 RelationIncrementReferenceCount(indexRelation);
264 * Acquire AccessShareLock for the duration of the scan
266 * Note: we could get an SI inval message here and consequently have to
267 * rebuild the relcache entry. The refcount increment above ensures
268 * that we will rebuild it and not just flush it...
270 LockRelation(indexRelation, AccessShareLock);
273 * Tell the AM to open a scan.
275 scan = (IndexScanDesc)
276 DatumGetPointer(OidFunctionCall3(procedure,
277 PointerGetDatum(indexRelation),
278 Int32GetDatum(nkeys),
279 PointerGetDatum(key)));
282 * Save additional parameters into the scandesc. Everything else was
283 * set up by RelationGetIndexScan.
285 scan->heapRelation = heapRelation;
286 scan->xs_snapshot = snapshot;
289 * We want to look up the amgettuple procedure just once per scan, not
290 * once per index_getnext call. So do it here and save the fmgr info
291 * result in the scan descriptor.
293 GET_SCAN_PROCEDURE(beginscan, amgettuple);
294 fmgr_info(procedure, &scan->fn_getnext);
300 * index_rescan - (re)start a scan of an index
302 * The caller may specify a new set of scankeys (but the number of keys
303 * cannot change). To restart the scan without changing keys, pass NULL
306 * Note that this is also called when first starting an indexscan;
307 * see RelationGetIndexScan. Keys *must* be passed in that case,
308 * unless scan->numberOfKeys is zero.
312 index_rescan(IndexScanDesc scan, ScanKey key)
314 RegProcedure procedure;
317 GET_SCAN_PROCEDURE(rescan, amrescan);
319 scan->kill_prior_tuple = false; /* for safety */
320 scan->keys_are_unique = false; /* may be set by index AM */
321 scan->got_tuple = false;
322 scan->unique_tuple_pos = 0;
323 scan->unique_tuple_mark = 0;
325 OidFunctionCall2(procedure,
326 PointerGetDatum(scan),
327 PointerGetDatum(key));
329 pgstat_reset_index_scan(&scan->xs_pgstat_info);
333 * index_endscan - end a scan
337 index_endscan(IndexScanDesc scan)
339 RegProcedure procedure;
342 GET_SCAN_PROCEDURE(endscan, amendscan);
344 /* Release any held pin on a heap page */
345 if (BufferIsValid(scan->xs_cbuf))
347 ReleaseBuffer(scan->xs_cbuf);
348 scan->xs_cbuf = InvalidBuffer;
351 /* End the AM's scan */
352 OidFunctionCall1(procedure, PointerGetDatum(scan));
354 /* Release index lock and refcount acquired by index_beginscan */
356 UnlockRelation(scan->indexRelation, AccessShareLock);
358 RelationDecrementReferenceCount(scan->indexRelation);
360 /* Release the scan data structure itself */
365 * index_markpos - mark a scan position
369 index_markpos(IndexScanDesc scan)
371 RegProcedure procedure;
374 GET_SCAN_PROCEDURE(markpos, ammarkpos);
376 scan->unique_tuple_mark = scan->unique_tuple_pos;
378 OidFunctionCall1(procedure, PointerGetDatum(scan));
382 * index_restrpos - restore a scan position
386 index_restrpos(IndexScanDesc scan)
388 RegProcedure procedure;
391 GET_SCAN_PROCEDURE(restrpos, amrestrpos);
393 scan->kill_prior_tuple = false; /* for safety */
396 * We do not reset got_tuple; so if the scan is actually being
397 * short-circuited by index_getnext, the effective position
398 * restoration is done by restoring unique_tuple_pos.
400 scan->unique_tuple_pos = scan->unique_tuple_mark;
402 OidFunctionCall1(procedure, PointerGetDatum(scan));
406 * index_getnext - get the next heap tuple from a scan
408 * The result is the next heap tuple satisfying the scan keys and the
409 * snapshot, or NULL if no more matching tuples exist. On success,
410 * the buffer containing the heap tuple is pinned (the pin will be dropped
411 * at the next index_getnext or index_endscan). The index TID corresponding
412 * to the heap tuple can be obtained if needed from scan->currentItemData.
416 index_getnext(IndexScanDesc scan, ScanDirection direction)
418 HeapTuple heapTuple = &scan->xs_ctup;
422 /* Release any previously held pin */
423 if (BufferIsValid(scan->xs_cbuf))
425 ReleaseBuffer(scan->xs_cbuf);
426 scan->xs_cbuf = InvalidBuffer;
430 * If we already got a tuple and it must be unique, there's no need to
431 * make the index AM look through any additional tuples. (This can
432 * save a useful amount of work in scenarios where there are many dead
433 * tuples due to heavy update activity.)
435 * To do this we must keep track of the logical scan position
436 * (before/on/after tuple). Also, we have to be sure to release scan
437 * resources before returning NULL; if we fail to do so then a
438 * multi-index scan can easily run the system out of free buffers. We
439 * can release index-level resources fairly cheaply by calling
440 * index_rescan. This means there are two persistent states as far as
441 * the index AM is concerned: on-tuple and rescanned. If we are
442 * actually asked to re-fetch the single tuple, we have to go through
443 * a fresh indexscan startup, which penalizes that (infrequent) case.
445 if (scan->keys_are_unique && scan->got_tuple)
447 int new_tuple_pos = scan->unique_tuple_pos;
449 if (ScanDirectionIsForward(direction))
451 if (new_tuple_pos <= 0)
456 if (new_tuple_pos >= 0)
459 if (new_tuple_pos == 0)
462 * We are moving onto the unique tuple from having been off
463 * it. We just fall through and let the index AM do the work.
464 * Note we should get the right answer regardless of scan
467 scan->unique_tuple_pos = 0; /* need to update position */
472 * Moving off the tuple; must do amrescan to release
473 * index-level pins before we return NULL. Since index_rescan
474 * will reset my state, must save and restore...
476 int unique_tuple_mark = scan->unique_tuple_mark;
478 index_rescan(scan, NULL /* no change to key */ );
480 scan->keys_are_unique = true;
481 scan->got_tuple = true;
482 scan->unique_tuple_pos = new_tuple_pos;
483 scan->unique_tuple_mark = unique_tuple_mark;
489 /* just make sure this is false... */
490 scan->kill_prior_tuple = false;
497 pgstat_count_index_scan(&scan->xs_pgstat_info);
500 * The AM's gettuple proc finds the next tuple matching the scan
501 * keys. index_beginscan already set up fn_getnext.
503 found = DatumGetBool(FunctionCall2(&scan->fn_getnext,
504 PointerGetDatum(scan),
505 Int32GetDatum(direction)));
507 /* Reset kill flag immediately for safety */
508 scan->kill_prior_tuple = false;
511 return NULL; /* failure exit */
514 * Fetch the heap tuple and see if it matches the snapshot.
516 if (heap_fetch(scan->heapRelation, scan->xs_snapshot,
517 heapTuple, &scan->xs_cbuf, true,
518 &scan->xs_pgstat_info))
521 /* Skip if no tuple at this location */
522 if (heapTuple->t_data == NULL)
523 continue; /* should we raise an error instead? */
526 * If we can't see it, maybe no one else can either. Check to see
527 * if the tuple is dead to all transactions. If so, signal the
528 * index AM to not return it on future indexscans.
530 * We told heap_fetch to keep a pin on the buffer, so we can
531 * re-access the tuple here. But we must re-lock the buffer
532 * first. Also, it's just barely possible for an update of hint
533 * bits to occur here.
535 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
536 sv_infomask = heapTuple->t_data->t_infomask;
538 if (HeapTupleSatisfiesVacuum(heapTuple->t_data, RecentGlobalXmin) ==
540 scan->kill_prior_tuple = true;
542 if (sv_infomask != heapTuple->t_data->t_infomask)
543 SetBufferCommitInfoNeedsSave(scan->xs_cbuf);
544 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
545 ReleaseBuffer(scan->xs_cbuf);
546 scan->xs_cbuf = InvalidBuffer;
550 scan->got_tuple = true;
553 * If we just fetched a known-unique tuple, then subsequent calls will
554 * go through the short-circuit code above. unique_tuple_pos has been
555 * initialized to 0, which is the correct state ("on row").
558 pgstat_count_index_getnext(&scan->xs_pgstat_info);
564 * index_getnext_indexitem - get the next index tuple from a scan
566 * Finds the next index tuple satisfying the scan keys. Note that the
567 * corresponding heap tuple is not accessed, and thus no time qual (snapshot)
568 * check is done, other than the index AM's internal check for killed tuples
569 * (which most callers of this routine will probably want to suppress by
570 * setting scan->ignore_killed_tuples = false).
572 * On success (TRUE return), the found index TID is in scan->currentItemData,
573 * and its heap TID is in scan->xs_ctup.t_self. scan->xs_cbuf is untouched.
577 index_getnext_indexitem(IndexScanDesc scan,
578 ScanDirection direction)
584 /* just make sure this is false... */
585 scan->kill_prior_tuple = false;
588 * have the am's gettuple proc do all the work. index_beginscan
589 * already set up fn_getnext.
591 found = DatumGetBool(FunctionCall2(&scan->fn_getnext,
592 PointerGetDatum(scan),
593 Int32GetDatum(direction)));
599 * index_bulk_delete - do mass deletion of index entries
601 * callback routine tells whether a given main-heap tuple is
604 * return value is an optional palloc'd struct of statistics
607 IndexBulkDeleteResult *
608 index_bulk_delete(Relation indexRelation,
609 IndexBulkDeleteCallback callback,
610 void *callback_state)
612 RegProcedure procedure;
613 IndexBulkDeleteResult *result;
616 GET_REL_PROCEDURE(bulk_delete, ambulkdelete);
618 result = (IndexBulkDeleteResult *)
619 DatumGetPointer(OidFunctionCall3(procedure,
620 PointerGetDatum(indexRelation),
621 PointerGetDatum((Pointer) callback),
622 PointerGetDatum(callback_state)));
628 * index_vacuum_cleanup - do post-deletion cleanup of an index
630 * return value is an optional palloc'd struct of statistics
633 IndexBulkDeleteResult *
634 index_vacuum_cleanup(Relation indexRelation,
635 IndexVacuumCleanupInfo *info,
636 IndexBulkDeleteResult *stats)
638 RegProcedure procedure;
639 IndexBulkDeleteResult *result;
643 /* It's okay for an index AM not to have a vacuumcleanup procedure */
644 if (!RegProcedureIsValid(indexRelation->rd_am->amvacuumcleanup))
647 GET_REL_PROCEDURE(vacuum_cleanup, amvacuumcleanup);
649 result = (IndexBulkDeleteResult *)
650 DatumGetPointer(OidFunctionCall3(procedure,
651 PointerGetDatum(indexRelation),
652 PointerGetDatum((Pointer) info),
653 PointerGetDatum((Pointer) stats)));
659 * index_cost_estimator
661 * Fetch the amcostestimate procedure OID for an index.
663 * We could combine fetching and calling the procedure,
664 * as index_insert does for example; but that would require
665 * importing a bunch of planner/optimizer stuff into this file.
669 index_cost_estimator(Relation indexRelation)
671 RegProcedure procedure;
674 GET_REL_PROCEDURE(cost_estimator, amcostestimate);
682 * Some indexed access methods may require support routines that are
683 * not in the operator class/operator model imposed by pg_am. These
684 * access methods may store the OIDs of registered procedures they
685 * need in pg_amproc. These registered procedure OIDs are ordered in
686 * a way that makes sense to the access method, and used only by the
687 * access method. The general index code doesn't know anything about
688 * the routines involved; it just builds an ordered list of them for
689 * each attribute on which an index is defined.
691 * This routine returns the requested procedure OID for a particular
696 index_getprocid(Relation irel,
704 nproc = irel->rd_am->amsupport;
706 Assert(procnum > 0 && procnum <= (uint16) nproc);
708 procindex = (nproc * (attnum - 1)) + (procnum - 1);
710 loc = irel->rd_support;
714 return loc[procindex];
720 * This routine allows index AMs to keep fmgr lookup info for
721 * support procs in the relcache.
725 index_getprocinfo(Relation irel,
733 nproc = irel->rd_am->amsupport;
735 Assert(procnum > 0 && procnum <= (uint16) nproc);
737 procindex = (nproc * (attnum - 1)) + (procnum - 1);
739 locinfo = irel->rd_supportinfo;
741 Assert(locinfo != NULL);
743 locinfo += procindex;
745 /* Initialize the lookup info if first time through */
746 if (locinfo->fn_oid == InvalidOid)
748 RegProcedure *loc = irel->rd_support;
753 procId = loc[procindex];
756 * Complain if function was not found during
757 * IndexSupportInitialize. This should not happen unless the
758 * system tables contain bogus entries for the index opclass. (If
759 * an AM wants to allow a support function to be optional, it can
760 * use index_getprocid.)
762 if (!RegProcedureIsValid(procId))
763 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
764 procnum, attnum, RelationGetRelationName(irel));
766 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);