1 /*-------------------------------------------------------------------------
4 * general index access method routines
6 * Portions Copyright (c) 1996-2001, 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.57 2002/04/17 20:57:56 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_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_cost_estimator - fetch amcostestimate procedure OID
27 * index_getprocid - get a support procedure OID
30 * This file contains the index_ routines which used
31 * to be a scattered collection of stuff in access/genam.
35 * Scans are implemented as follows:
37 * `0' represents an invalid item pointer.
38 * `-' represents an unknown item pointer.
39 * `X' represents a known item pointers.
40 * `+' represents known or invalid item pointers.
41 * `*' represents any item pointers.
43 * State is represented by a triple of these symbols in the order of
44 * previous, current, next. Note that the case of reverse scans works
48 * (1) + + - + 0 0 (if the next item pointer is invalid)
49 * (2) + X - (otherwise)
50 * (3) * 0 0 * 0 0 (no change)
51 * (4) + X 0 X 0 0 (shift)
52 * (5) * + X + X - (shift, add unknown)
54 * All other states cannot occur.
56 * Note: It would be possible to cache the status of the previous and
57 * next item pointer using the flags.
59 *-------------------------------------------------------------------------
64 #include "access/genam.h"
65 #include "access/heapam.h"
66 #include "utils/relcache.h"
70 /* ----------------------------------------------------------------
71 * macros used in index_ routines
72 * ----------------------------------------------------------------
74 #define RELATION_CHECKS \
76 AssertMacro(RelationIsValid(relation)), \
77 AssertMacro(PointerIsValid(relation->rd_am)) \
82 AssertMacro(IndexScanIsValid(scan)), \
83 AssertMacro(RelationIsValid(scan->relation)), \
84 AssertMacro(PointerIsValid(scan->relation->rd_am)) \
87 #define GET_REL_PROCEDURE(x,y) \
89 procedure = relation->rd_am->y, \
90 (!RegProcedureIsValid(procedure)) ? \
91 elog(ERROR, "index_%s: invalid %s regproc", \
92 CppAsString(x), CppAsString(y)) \
96 #define GET_SCAN_PROCEDURE(x,y) \
98 procedure = scan->relation->rd_am->y, \
99 (!RegProcedureIsValid(procedure)) ? \
100 elog(ERROR, "index_%s: invalid %s regproc", \
101 CppAsString(x), CppAsString(y)) \
106 /* ----------------------------------------------------------------
107 * index_ interface functions
108 * ----------------------------------------------------------------
112 * index_open - open an index relation by relation OID
114 * Note: we acquire no lock on the index. An AccessShareLock is
115 * acquired by index_beginscan (and released by index_endscan).
116 * Generally, the caller should already hold some type of lock on
117 * the parent relation to ensure that the index doesn't disappear.
119 * This is a convenience routine adapted for indexscan use.
120 * Some callers may prefer to use relation_open directly.
124 index_open(Oid relationId)
128 r = relation_open(relationId, NoLock);
130 if (r->rd_rel->relkind != RELKIND_INDEX)
131 elog(ERROR, "%s is not an index relation",
132 RelationGetRelationName(r));
134 pgstat_initstats(&r->pgstat_info, r);
140 * index_openrv - open an index relation specified
143 * As above, but relation is specified by a RangeVar.
147 index_openrv(const RangeVar *relation)
151 r = relation_openrv(relation, NoLock);
153 if (r->rd_rel->relkind != RELKIND_INDEX)
154 elog(ERROR, "%s is not an index relation",
155 RelationGetRelationName(r));
157 pgstat_initstats(&r->pgstat_info, r);
163 * index_openr - open a system index relation specified by name.
165 * As above, but the relation is specified by an unqualified name;
166 * it is assumed to live in the system catalog namespace.
170 index_openr(const char *sysRelationName)
174 r = relation_openr(sysRelationName, NoLock);
176 if (r->rd_rel->relkind != RELKIND_INDEX)
177 elog(ERROR, "%s is not an index relation",
178 RelationGetRelationName(r));
180 pgstat_initstats(&r->pgstat_info, r);
186 * index_close - close a index relation
188 * presently the relcache routines do all the work we need
189 * to open/close index relations.
193 index_close(Relation relation)
195 RelationClose(relation);
199 * index_insert - insert an index tuple into a relation
203 index_insert(Relation relation,
206 ItemPointer heap_t_ctid,
209 RegProcedure procedure;
210 InsertIndexResult specificResult;
213 GET_REL_PROCEDURE(insert, aminsert);
216 * have the am's insert proc do all the work.
218 specificResult = (InsertIndexResult)
219 DatumGetPointer(OidFunctionCall5(procedure,
220 PointerGetDatum(relation),
221 PointerGetDatum(datum),
222 PointerGetDatum(nulls),
223 PointerGetDatum(heap_t_ctid),
224 PointerGetDatum(heapRel)));
226 /* must be pfree'ed */
227 return specificResult;
231 * index_beginscan - start a scan of an index
235 index_beginscan(Relation relation,
241 RegProcedure procedure;
244 GET_REL_PROCEDURE(beginscan, ambeginscan);
246 RelationIncrementReferenceCount(relation);
249 * Acquire AccessShareLock for the duration of the scan
251 * Note: we could get an SI inval message here and consequently have to
252 * rebuild the relcache entry. The refcount increment above ensures
253 * that we will rebuild it and not just flush it...
255 LockRelation(relation, AccessShareLock);
257 scan = (IndexScanDesc)
258 DatumGetPointer(OidFunctionCall4(procedure,
259 PointerGetDatum(relation),
260 BoolGetDatum(scanFromEnd),
261 UInt16GetDatum(numberOfKeys),
262 PointerGetDatum(key)));
264 pgstat_initstats(&scan->xs_pgstat_info, relation);
267 * We want to look up the amgettuple procedure just once per scan, not
268 * once per index_getnext call. So do it here and save the fmgr info
269 * result in the scan descriptor.
271 GET_SCAN_PROCEDURE(beginscan, amgettuple);
272 fmgr_info(procedure, &scan->fn_getnext);
278 * index_rescan - restart a scan of an index
282 index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
284 RegProcedure procedure;
287 GET_SCAN_PROCEDURE(rescan, amrescan);
289 OidFunctionCall3(procedure,
290 PointerGetDatum(scan),
291 BoolGetDatum(scanFromEnd),
292 PointerGetDatum(key));
294 pgstat_reset_index_scan(&scan->xs_pgstat_info);
298 * index_endscan - end a scan
302 index_endscan(IndexScanDesc scan)
304 RegProcedure procedure;
307 GET_SCAN_PROCEDURE(endscan, amendscan);
309 OidFunctionCall1(procedure, PointerGetDatum(scan));
311 /* Release lock and refcount acquired by index_beginscan */
313 UnlockRelation(scan->relation, AccessShareLock);
315 RelationDecrementReferenceCount(scan->relation);
317 /* Release the scan data structure itself */
322 * index_markpos - mark a scan position
326 index_markpos(IndexScanDesc scan)
328 RegProcedure procedure;
331 GET_SCAN_PROCEDURE(markpos, ammarkpos);
333 OidFunctionCall1(procedure, PointerGetDatum(scan));
337 * index_restrpos - restore a scan position
341 index_restrpos(IndexScanDesc scan)
343 RegProcedure procedure;
346 GET_SCAN_PROCEDURE(restrpos, amrestrpos);
348 OidFunctionCall1(procedure, PointerGetDatum(scan));
352 * index_getnext - get the next tuple from a scan
354 * A RetrieveIndexResult is a index tuple/heap tuple pair
358 index_getnext(IndexScanDesc scan,
359 ScanDirection direction)
361 RetrieveIndexResult result;
365 pgstat_count_index_scan(&scan->xs_pgstat_info);
368 * have the am's gettuple proc do all the work. index_beginscan
369 * already set up fn_getnext.
371 result = (RetrieveIndexResult)
372 DatumGetPointer(FunctionCall2(&scan->fn_getnext,
373 PointerGetDatum(scan),
374 Int32GetDatum(direction)));
377 pgstat_count_index_getnext(&scan->xs_pgstat_info);
382 * index_bulk_delete - do mass deletion of index entries
384 * callback routine tells whether a given main-heap tuple is
387 * return value is an optional palloc'd struct of statistics
390 IndexBulkDeleteResult *
391 index_bulk_delete(Relation relation,
392 IndexBulkDeleteCallback callback,
393 void *callback_state)
395 RegProcedure procedure;
396 IndexBulkDeleteResult *result;
399 GET_REL_PROCEDURE(bulk_delete, ambulkdelete);
401 result = (IndexBulkDeleteResult *)
402 DatumGetPointer(OidFunctionCall3(procedure,
403 PointerGetDatum(relation),
404 PointerGetDatum((Pointer) callback),
405 PointerGetDatum(callback_state)));
411 * index_cost_estimator
413 * Fetch the amcostestimate procedure OID for an index.
415 * We could combine fetching and calling the procedure,
416 * as index_insert does for example; but that would require
417 * importing a bunch of planner/optimizer stuff into this file.
421 index_cost_estimator(Relation relation)
423 RegProcedure procedure;
426 GET_REL_PROCEDURE(cost_estimator, amcostestimate);
434 * Some indexed access methods may require support routines that are
435 * not in the operator class/operator model imposed by pg_am. These
436 * access methods may store the OIDs of registered procedures they
437 * need in pg_amproc. These registered procedure OIDs are ordered in
438 * a way that makes sense to the access method, and used only by the
439 * access method. The general index code doesn't know anything about
440 * the routines involved; it just builds an ordered list of them for
441 * each attribute on which an index is defined.
443 * This routine returns the requested procedure OID for a particular
448 index_getprocid(Relation irel,
456 nproc = irel->rd_am->amsupport;
458 Assert(procnum > 0 && procnum <= (uint16) nproc);
460 procindex = (nproc * (attnum - 1)) + (procnum - 1);
462 loc = irel->rd_support;
466 return loc[procindex];
472 * This routine allows index AMs to keep fmgr lookup info for
473 * support procs in the relcache.
477 index_getprocinfo(Relation irel,
485 nproc = irel->rd_am->amsupport;
487 Assert(procnum > 0 && procnum <= (uint16) nproc);
489 procindex = (nproc * (attnum - 1)) + (procnum - 1);
491 locinfo = irel->rd_supportinfo;
493 Assert(locinfo != NULL);
495 locinfo += procindex;
497 /* Initialize the lookup info if first time through */
498 if (locinfo->fn_oid == InvalidOid)
500 RegProcedure *loc = irel->rd_support;
505 procId = loc[procindex];
508 * Complain if function was not found during IndexSupportInitialize.
509 * This should not happen unless the system tables contain bogus
510 * entries for the index opclass. (If an AM wants to allow a
511 * support function to be optional, it can use index_getprocid.)
513 if (!RegProcedureIsValid(procId))
514 elog(ERROR, "Missing support function %d for attribute %d of index %s",
515 procnum, attnum, RelationGetRelationName(irel));
517 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);