1 /*-------------------------------------------------------------------------
4 * general index access method routines
6 * Portions Copyright (c) 1996-2007, 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.98 2007/05/27 03:50:38 tgl Exp $
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_multi - start a scan of an index with amgetmulti
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_getnext - get the next tuple from a scan
24 * index_getmulti - get multiple tuples from a scan
25 * index_bulk_delete - bulk deletion of index tuples
26 * index_vacuum_cleanup - post-deletion cleanup of an index
27 * index_getprocid - get a support procedure OID
28 * index_getprocinfo - get a support procedure's lookup info
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"
68 #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(pname) \
90 procedure = &indexRelation->rd_aminfo->pname; \
91 if (!OidIsValid(procedure->fn_oid)) \
93 RegProcedure procOid = indexRelation->rd_am->pname; \
94 if (!RegProcedureIsValid(procOid)) \
95 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
96 fmgr_info_cxt(procOid, procedure, indexRelation->rd_indexcxt); \
100 #define GET_SCAN_PROCEDURE(pname) \
102 procedure = &scan->indexRelation->rd_aminfo->pname; \
103 if (!OidIsValid(procedure->fn_oid)) \
105 RegProcedure procOid = scan->indexRelation->rd_am->pname; \
106 if (!RegProcedureIsValid(procOid)) \
107 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
108 fmgr_info_cxt(procOid, procedure, scan->indexRelation->rd_indexcxt); \
112 static IndexScanDesc index_beginscan_internal(Relation indexRelation,
113 int nkeys, ScanKey key);
116 /* ----------------------------------------------------------------
117 * index_ interface functions
118 * ----------------------------------------------------------------
122 * index_open - open an index relation by relation OID
124 * If lockmode is not "NoLock", the specified kind of lock is
125 * obtained on the index. (Generally, NoLock should only be
126 * used if the caller knows it has some appropriate lock on the
129 * An error is raised if the index does not exist.
131 * This is a convenience routine adapted for indexscan use.
132 * Some callers may prefer to use relation_open directly.
136 index_open(Oid relationId, LOCKMODE lockmode)
140 r = relation_open(relationId, lockmode);
142 if (r->rd_rel->relkind != RELKIND_INDEX)
144 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
145 errmsg("\"%s\" is not an index",
146 RelationGetRelationName(r))));
152 * index_close - close an index relation
154 * If lockmode is not "NoLock", we then release the specified lock.
156 * Note that it is often sensible to hold a lock beyond index_close;
157 * in that case, the lock is released automatically at xact end.
161 index_close(Relation relation, LOCKMODE lockmode)
163 LockRelId relid = relation->rd_lockInfo.lockRelId;
165 Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
167 /* The relcache does the real work... */
168 RelationClose(relation);
170 if (lockmode != NoLock)
171 UnlockRelationId(&relid, lockmode);
175 * index_insert - insert an index tuple into a relation
179 index_insert(Relation indexRelation,
182 ItemPointer heap_t_ctid,
183 Relation heapRelation,
184 bool check_uniqueness)
189 GET_REL_PROCEDURE(aminsert);
192 * have the am's insert proc do all the work.
194 return DatumGetBool(FunctionCall6(procedure,
195 PointerGetDatum(indexRelation),
196 PointerGetDatum(values),
197 PointerGetDatum(isnull),
198 PointerGetDatum(heap_t_ctid),
199 PointerGetDatum(heapRelation),
200 BoolGetDatum(check_uniqueness)));
204 * index_beginscan - start a scan of an index with amgettuple
206 * Note: heapRelation may be NULL if there is no intention of calling
207 * index_getnext on this scan; index_getnext_indexitem will not use the
208 * heapRelation link (nor the snapshot). However, the caller had better
209 * be holding some kind of lock on the heap relation in any case, to ensure
210 * no one deletes it (or the index) out from under us. Caller must also
211 * be holding a lock on the index.
214 index_beginscan(Relation heapRelation,
215 Relation indexRelation,
217 int nkeys, ScanKey key)
221 scan = index_beginscan_internal(indexRelation, nkeys, key);
224 * Save additional parameters into the scandesc. Everything else was set
225 * up by RelationGetIndexScan.
227 scan->is_multiscan = false;
228 scan->heapRelation = heapRelation;
229 scan->xs_snapshot = snapshot;
235 * index_beginscan_multi - start a scan of an index with amgetmulti
237 * As above, caller had better be holding some lock on the parent heap
238 * relation, even though it's not explicitly mentioned here.
241 index_beginscan_multi(Relation indexRelation,
243 int nkeys, ScanKey key)
247 scan = index_beginscan_internal(indexRelation, nkeys, key);
250 * Save additional parameters into the scandesc. Everything else was set
251 * up by RelationGetIndexScan.
253 scan->is_multiscan = true;
254 scan->xs_snapshot = snapshot;
260 * index_beginscan_internal --- common code for index_beginscan variants
263 index_beginscan_internal(Relation indexRelation,
264 int nkeys, ScanKey key)
270 GET_REL_PROCEDURE(ambeginscan);
273 * We hold a reference count to the relcache entry throughout the scan.
275 RelationIncrementReferenceCount(indexRelation);
278 * Tell the AM to open a scan.
280 scan = (IndexScanDesc)
281 DatumGetPointer(FunctionCall3(procedure,
282 PointerGetDatum(indexRelation),
283 Int32GetDatum(nkeys),
284 PointerGetDatum(key)));
290 * index_rescan - (re)start a scan of an index
292 * The caller may specify a new set of scankeys (but the number of keys
293 * cannot change). To restart the scan without changing keys, pass NULL
296 * Note that this is also called when first starting an indexscan;
297 * see RelationGetIndexScan. Keys *must* be passed in that case,
298 * unless scan->numberOfKeys is zero.
302 index_rescan(IndexScanDesc scan, ScanKey key)
307 GET_SCAN_PROCEDURE(amrescan);
309 /* Release any held pin on a heap page */
310 if (BufferIsValid(scan->xs_cbuf))
312 ReleaseBuffer(scan->xs_cbuf);
313 scan->xs_cbuf = InvalidBuffer;
316 scan->kill_prior_tuple = false; /* for safety */
318 FunctionCall2(procedure,
319 PointerGetDatum(scan),
320 PointerGetDatum(key));
324 * index_endscan - end a scan
328 index_endscan(IndexScanDesc scan)
333 GET_SCAN_PROCEDURE(amendscan);
335 /* Release any held pin on a heap page */
336 if (BufferIsValid(scan->xs_cbuf))
338 ReleaseBuffer(scan->xs_cbuf);
339 scan->xs_cbuf = InvalidBuffer;
342 /* End the AM's scan */
343 FunctionCall1(procedure, PointerGetDatum(scan));
345 /* Release index refcount acquired by index_beginscan */
346 RelationDecrementReferenceCount(scan->indexRelation);
348 /* Release the scan data structure itself */
353 * index_markpos - mark a scan position
357 index_markpos(IndexScanDesc scan)
362 GET_SCAN_PROCEDURE(ammarkpos);
364 FunctionCall1(procedure, PointerGetDatum(scan));
368 * index_restrpos - restore a scan position
370 * NOTE: this only restores the internal scan state of the index AM.
371 * The current result tuple (scan->xs_ctup) doesn't change. See comments
372 * for ExecRestrPos().
376 index_restrpos(IndexScanDesc scan)
381 GET_SCAN_PROCEDURE(amrestrpos);
383 scan->kill_prior_tuple = false; /* for safety */
385 FunctionCall1(procedure, PointerGetDatum(scan));
389 * index_getnext - get the next heap tuple from a scan
391 * The result is the next heap tuple satisfying the scan keys and the
392 * snapshot, or NULL if no more matching tuples exist. On success,
393 * the buffer containing the heap tuple is pinned (the pin will be dropped
394 * at the next index_getnext or index_endscan).
398 index_getnext(IndexScanDesc scan, ScanDirection direction)
400 HeapTuple heapTuple = &scan->xs_ctup;
404 GET_SCAN_PROCEDURE(amgettuple);
406 /* just make sure this is false... */
407 scan->kill_prior_tuple = false;
414 * The AM's gettuple proc finds the next tuple matching the scan keys.
416 found = DatumGetBool(FunctionCall2(procedure,
417 PointerGetDatum(scan),
418 Int32GetDatum(direction)));
420 /* Reset kill flag immediately for safety */
421 scan->kill_prior_tuple = false;
425 /* Release any held pin on a heap page */
426 if (BufferIsValid(scan->xs_cbuf))
428 ReleaseBuffer(scan->xs_cbuf);
429 scan->xs_cbuf = InvalidBuffer;
431 return NULL; /* failure exit */
434 pgstat_count_index_tuples(scan->indexRelation, 1);
437 * Fetch the heap tuple and see if it matches the snapshot.
439 if (heap_release_fetch(scan->heapRelation, scan->xs_snapshot,
440 heapTuple, &scan->xs_cbuf, true,
441 scan->indexRelation))
444 /* Skip if no undeleted tuple at this location */
445 if (heapTuple->t_data == NULL)
449 * If we can't see it, maybe no one else can either. Check to see if
450 * the tuple is dead to all transactions. If so, signal the index AM
451 * to not return it on future indexscans.
453 * We told heap_release_fetch to keep a pin on the buffer, so we can
454 * re-access the tuple here. But we must re-lock the buffer first.
456 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
458 if (HeapTupleSatisfiesVacuum(heapTuple->t_data, RecentGlobalXmin,
459 scan->xs_cbuf) == HEAPTUPLE_DEAD)
460 scan->kill_prior_tuple = true;
462 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
470 * index_getnext_indexitem - get the next index tuple from a scan
472 * Finds the next index tuple satisfying the scan keys. Note that the
473 * corresponding heap tuple is not accessed, and thus no time qual (snapshot)
474 * check is done, other than the index AM's internal check for killed tuples
475 * (which most callers of this routine will probably want to suppress by
476 * setting scan->ignore_killed_tuples = false).
478 * On success (TRUE return), the heap TID of the found index entry is in
479 * scan->xs_ctup.t_self. scan->xs_cbuf is untouched.
483 index_getnext_indexitem(IndexScanDesc scan,
484 ScanDirection direction)
490 GET_SCAN_PROCEDURE(amgettuple);
492 /* just make sure this is false... */
493 scan->kill_prior_tuple = false;
496 * have the am's gettuple proc do all the work.
498 found = DatumGetBool(FunctionCall2(procedure,
499 PointerGetDatum(scan),
500 Int32GetDatum(direction)));
503 pgstat_count_index_tuples(scan->indexRelation, 1);
509 * index_getmulti - get multiple tuples from an index scan
511 * Collects the TIDs of multiple heap tuples satisfying the scan keys.
512 * Since there's no interlock between the index scan and the eventual heap
513 * access, this is only safe to use with MVCC-based snapshots: the heap
514 * item slot could have been replaced by a newer tuple by the time we get
517 * A TRUE result indicates more calls should occur; a FALSE result says the
518 * scan is done. *returned_tids could be zero or nonzero in either case.
522 index_getmulti(IndexScanDesc scan,
523 ItemPointer tids, int32 max_tids,
524 int32 *returned_tids)
530 GET_SCAN_PROCEDURE(amgetmulti);
532 /* just make sure this is false... */
533 scan->kill_prior_tuple = false;
536 * have the am's getmulti proc do all the work.
538 found = DatumGetBool(FunctionCall4(procedure,
539 PointerGetDatum(scan),
540 PointerGetDatum(tids),
541 Int32GetDatum(max_tids),
542 PointerGetDatum(returned_tids)));
544 pgstat_count_index_tuples(scan->indexRelation, *returned_tids);
550 * index_bulk_delete - do mass deletion of index entries
552 * callback routine tells whether a given main-heap tuple is
555 * return value is an optional palloc'd struct of statistics
558 IndexBulkDeleteResult *
559 index_bulk_delete(IndexVacuumInfo *info,
560 IndexBulkDeleteResult *stats,
561 IndexBulkDeleteCallback callback,
562 void *callback_state)
564 Relation indexRelation = info->index;
566 IndexBulkDeleteResult *result;
569 GET_REL_PROCEDURE(ambulkdelete);
571 result = (IndexBulkDeleteResult *)
572 DatumGetPointer(FunctionCall4(procedure,
573 PointerGetDatum(info),
574 PointerGetDatum(stats),
575 PointerGetDatum((Pointer) callback),
576 PointerGetDatum(callback_state)));
582 * index_vacuum_cleanup - do post-deletion cleanup of an index
584 * return value is an optional palloc'd struct of statistics
587 IndexBulkDeleteResult *
588 index_vacuum_cleanup(IndexVacuumInfo *info,
589 IndexBulkDeleteResult *stats)
591 Relation indexRelation = info->index;
593 IndexBulkDeleteResult *result;
596 GET_REL_PROCEDURE(amvacuumcleanup);
598 result = (IndexBulkDeleteResult *)
599 DatumGetPointer(FunctionCall2(procedure,
600 PointerGetDatum(info),
601 PointerGetDatum(stats)));
609 * Index access methods typically require support routines that are
610 * not directly the implementation of any WHERE-clause query operator
611 * and so cannot be kept in pg_amop. Instead, such routines are kept
612 * in pg_amproc. These registered procedure OIDs are assigned numbers
613 * according to a convention established by the access method.
614 * The general index code doesn't know anything about the routines
615 * involved; it just builds an ordered list of them for
616 * each attribute on which an index is defined.
618 * As of Postgres 8.3, support routines within an operator family
619 * are further subdivided by the "left type" and "right type" of the
620 * query operator(s) that they support. The "default" functions for a
621 * particular indexed attribute are those with both types equal to
622 * the index opclass' opcintype (note that this is subtly different
623 * from the indexed attribute's own type: it may be a binary-compatible
624 * type instead). Only the default functions are stored in relcache
625 * entries --- access methods can use the syscache to look up non-default
628 * This routine returns the requested default procedure OID for a
629 * particular indexed attribute.
633 index_getprocid(Relation irel,
641 nproc = irel->rd_am->amsupport;
643 Assert(procnum > 0 && procnum <= (uint16) nproc);
645 procindex = (nproc * (attnum - 1)) + (procnum - 1);
647 loc = irel->rd_support;
651 return loc[procindex];
657 * This routine allows index AMs to keep fmgr lookup info for
658 * support procs in the relcache. As above, only the "default"
659 * functions for any particular indexed attribute are cached.
661 * Note: the return value points into cached data that will be lost during
662 * any relcache rebuild! Therefore, either use the callinfo right away,
663 * or save it only after having acquired some type of lock on the index rel.
667 index_getprocinfo(Relation irel,
675 nproc = irel->rd_am->amsupport;
677 Assert(procnum > 0 && procnum <= (uint16) nproc);
679 procindex = (nproc * (attnum - 1)) + (procnum - 1);
681 locinfo = irel->rd_supportinfo;
683 Assert(locinfo != NULL);
685 locinfo += procindex;
687 /* Initialize the lookup info if first time through */
688 if (locinfo->fn_oid == InvalidOid)
690 RegProcedure *loc = irel->rd_support;
695 procId = loc[procindex];
698 * Complain if function was not found during IndexSupportInitialize.
699 * This should not happen unless the system tables contain bogus
700 * entries for the index opclass. (If an AM wants to allow a support
701 * function to be optional, it can use index_getprocid.)
703 if (!RegProcedureIsValid(procId))
704 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
705 procnum, attnum, RelationGetRelationName(irel));
707 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);