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.55 2001/11/02 16:30:29 tgl Exp $
14 * index_open - open an index relation by relation OID
15 * index_openr - open an index relation by name
16 * index_close - close an index relation
17 * index_beginscan - start a scan of an index
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_bulk_delete - bulk deletion of index tuples
25 * index_cost_estimator - fetch amcostestimate procedure OID
26 * index_getprocid - get a support procedure OID
29 * This file contains the index_ routines which used
30 * to be a scattered collection of stuff in access/genam.
34 * Scans are implemented as follows:
36 * `0' represents an invalid item pointer.
37 * `-' represents an unknown item pointer.
38 * `X' represents a known item pointers.
39 * `+' represents known or invalid item pointers.
40 * `*' represents any item pointers.
42 * State is represented by a triple of these symbols in the order of
43 * previous, current, next. Note that the case of reverse scans works
47 * (1) + + - + 0 0 (if the next item pointer is invalid)
48 * (2) + X - (otherwise)
49 * (3) * 0 0 * 0 0 (no change)
50 * (4) + X 0 X 0 0 (shift)
51 * (5) * + X + X - (shift, add unknown)
53 * All other states cannot occur.
55 * Note: It would be possible to cache the status of the previous and
56 * next item pointer using the flags.
58 *-------------------------------------------------------------------------
63 #include "access/genam.h"
64 #include "access/heapam.h"
65 #include "utils/relcache.h"
69 /* ----------------------------------------------------------------
70 * macros used in index_ routines
71 * ----------------------------------------------------------------
73 #define RELATION_CHECKS \
75 AssertMacro(RelationIsValid(relation)), \
76 AssertMacro(PointerIsValid(relation->rd_am)) \
81 AssertMacro(IndexScanIsValid(scan)), \
82 AssertMacro(RelationIsValid(scan->relation)), \
83 AssertMacro(PointerIsValid(scan->relation->rd_am)) \
86 #define GET_REL_PROCEDURE(x,y) \
88 procedure = relation->rd_am->y, \
89 (!RegProcedureIsValid(procedure)) ? \
90 elog(ERROR, "index_%s: invalid %s regproc", \
91 CppAsString(x), CppAsString(y)) \
95 #define GET_SCAN_PROCEDURE(x,y) \
97 procedure = scan->relation->rd_am->y, \
98 (!RegProcedureIsValid(procedure)) ? \
99 elog(ERROR, "index_%s: invalid %s regproc", \
100 CppAsString(x), CppAsString(y)) \
105 /* ----------------------------------------------------------------
106 * index_ interface functions
107 * ----------------------------------------------------------------
111 * index_open - open an index relation by relation OID
113 * Note: we acquire no lock on the index. An AccessShareLock is
114 * acquired by index_beginscan (and released by index_endscan).
115 * Generally, the caller should already hold some type of lock on
116 * the parent relation to ensure that the index doesn't disappear.
118 * This is a convenience routine adapted for indexscan use.
119 * Some callers may prefer to use relation_open directly.
123 index_open(Oid relationId)
127 r = relation_open(relationId, NoLock);
129 if (r->rd_rel->relkind != RELKIND_INDEX)
130 elog(ERROR, "%s is not an index relation",
131 RelationGetRelationName(r));
133 pgstat_initstats(&r->pgstat_info, r);
139 * index_openr - open an index relation by name
141 * As above, but lookup by name instead of OID.
145 index_openr(const char *relationName)
149 r = relation_openr(relationName, NoLock);
151 if (r->rd_rel->relkind != RELKIND_INDEX)
152 elog(ERROR, "%s is not an index relation",
153 RelationGetRelationName(r));
155 pgstat_initstats(&r->pgstat_info, r);
161 * index_close - close a index relation
163 * presently the relcache routines do all the work we need
164 * to open/close index relations.
168 index_close(Relation relation)
170 RelationClose(relation);
174 * index_insert - insert an index tuple into a relation
178 index_insert(Relation relation,
181 ItemPointer heap_t_ctid,
184 RegProcedure procedure;
185 InsertIndexResult specificResult;
188 GET_REL_PROCEDURE(insert, aminsert);
191 * have the am's insert proc do all the work.
193 specificResult = (InsertIndexResult)
194 DatumGetPointer(OidFunctionCall5(procedure,
195 PointerGetDatum(relation),
196 PointerGetDatum(datum),
197 PointerGetDatum(nulls),
198 PointerGetDatum(heap_t_ctid),
199 PointerGetDatum(heapRel)));
201 /* must be pfree'ed */
202 return specificResult;
206 * index_beginscan - start a scan of an index
210 index_beginscan(Relation relation,
216 RegProcedure procedure;
219 GET_REL_PROCEDURE(beginscan, ambeginscan);
221 RelationIncrementReferenceCount(relation);
224 * Acquire AccessShareLock for the duration of the scan
226 * Note: we could get an SI inval message here and consequently have to
227 * rebuild the relcache entry. The refcount increment above ensures
228 * that we will rebuild it and not just flush it...
230 LockRelation(relation, AccessShareLock);
232 scan = (IndexScanDesc)
233 DatumGetPointer(OidFunctionCall4(procedure,
234 PointerGetDatum(relation),
235 BoolGetDatum(scanFromEnd),
236 UInt16GetDatum(numberOfKeys),
237 PointerGetDatum(key)));
239 pgstat_initstats(&scan->xs_pgstat_info, relation);
242 * We want to look up the amgettuple procedure just once per scan, not
243 * once per index_getnext call. So do it here and save the fmgr info
244 * result in the scan descriptor.
246 GET_SCAN_PROCEDURE(beginscan, amgettuple);
247 fmgr_info(procedure, &scan->fn_getnext);
253 * index_rescan - restart a scan of an index
257 index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
259 RegProcedure procedure;
262 GET_SCAN_PROCEDURE(rescan, amrescan);
264 OidFunctionCall3(procedure,
265 PointerGetDatum(scan),
266 BoolGetDatum(scanFromEnd),
267 PointerGetDatum(key));
269 pgstat_reset_index_scan(&scan->xs_pgstat_info);
273 * index_endscan - end a scan
277 index_endscan(IndexScanDesc scan)
279 RegProcedure procedure;
282 GET_SCAN_PROCEDURE(endscan, amendscan);
284 OidFunctionCall1(procedure, PointerGetDatum(scan));
286 /* Release lock and refcount acquired by index_beginscan */
288 UnlockRelation(scan->relation, AccessShareLock);
290 RelationDecrementReferenceCount(scan->relation);
292 /* Release the scan data structure itself */
297 * index_markpos - mark a scan position
301 index_markpos(IndexScanDesc scan)
303 RegProcedure procedure;
306 GET_SCAN_PROCEDURE(markpos, ammarkpos);
308 OidFunctionCall1(procedure, PointerGetDatum(scan));
312 * index_restrpos - restore a scan position
316 index_restrpos(IndexScanDesc scan)
318 RegProcedure procedure;
321 GET_SCAN_PROCEDURE(restrpos, amrestrpos);
323 OidFunctionCall1(procedure, PointerGetDatum(scan));
327 * index_getnext - get the next tuple from a scan
329 * A RetrieveIndexResult is a index tuple/heap tuple pair
333 index_getnext(IndexScanDesc scan,
334 ScanDirection direction)
336 RetrieveIndexResult result;
340 pgstat_count_index_scan(&scan->xs_pgstat_info);
343 * have the am's gettuple proc do all the work. index_beginscan
344 * already set up fn_getnext.
346 result = (RetrieveIndexResult)
347 DatumGetPointer(FunctionCall2(&scan->fn_getnext,
348 PointerGetDatum(scan),
349 Int32GetDatum(direction)));
352 pgstat_count_index_getnext(&scan->xs_pgstat_info);
357 * index_bulk_delete - do mass deletion of index entries
359 * callback routine tells whether a given main-heap tuple is
362 * return value is an optional palloc'd struct of statistics
365 IndexBulkDeleteResult *
366 index_bulk_delete(Relation relation,
367 IndexBulkDeleteCallback callback,
368 void *callback_state)
370 RegProcedure procedure;
371 IndexBulkDeleteResult *result;
374 GET_REL_PROCEDURE(bulk_delete, ambulkdelete);
376 result = (IndexBulkDeleteResult *)
377 DatumGetPointer(OidFunctionCall3(procedure,
378 PointerGetDatum(relation),
379 PointerGetDatum((Pointer) callback),
380 PointerGetDatum(callback_state)));
386 * index_cost_estimator
388 * Fetch the amcostestimate procedure OID for an index.
390 * We could combine fetching and calling the procedure,
391 * as index_insert does for example; but that would require
392 * importing a bunch of planner/optimizer stuff into this file.
396 index_cost_estimator(Relation relation)
398 RegProcedure procedure;
401 GET_REL_PROCEDURE(cost_estimator, amcostestimate);
409 * Some indexed access methods may require support routines that are
410 * not in the operator class/operator model imposed by pg_am. These
411 * access methods may store the OIDs of registered procedures they
412 * need in pg_amproc. These registered procedure OIDs are ordered in
413 * a way that makes sense to the access method, and used only by the
414 * access method. The general index code doesn't know anything about
415 * the routines involved; it just builds an ordered list of them for
416 * each attribute on which an index is defined.
418 * This routine returns the requested procedure OID for a particular
423 index_getprocid(Relation irel,
431 nproc = irel->rd_am->amsupport;
433 Assert(procnum > 0 && procnum <= (uint16) nproc);
435 procindex = (nproc * (attnum - 1)) + (procnum - 1);
437 loc = irel->rd_support;
441 return loc[procindex];
447 * This routine allows index AMs to keep fmgr lookup info for
448 * support procs in the relcache.
452 index_getprocinfo(Relation irel,
460 nproc = irel->rd_am->amsupport;
462 Assert(procnum > 0 && procnum <= (uint16) nproc);
464 procindex = (nproc * (attnum - 1)) + (procnum - 1);
466 locinfo = irel->rd_supportinfo;
468 Assert(locinfo != NULL);
470 locinfo += procindex;
472 /* Initialize the lookup info if first time through */
473 if (locinfo->fn_oid == InvalidOid)
475 RegProcedure *loc = irel->rd_support;
479 fmgr_info_cxt(loc[procindex], locinfo, irel->rd_indexcxt);