1 /*-------------------------------------------------------------------------
4 * general index access method routines
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/index/indexam.c,v 1.44 2000/05/30 04:24:32 tgl Exp $
14 * index_open - open an index relation by relationId
15 * index_openr - open a index relation by name
16 * index_close - close a 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_delete - delete an item from an index 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_fetch - retrieve tuple with tid
26 * ** index_replace - replace a tuple
27 * ** index_getattr - get an attribute from an index tuple
28 * index_getprocid - get a support procedure id from the rel tuple
30 * IndexScanIsValid - check index scan
33 * This file contains the index_ routines which used
34 * to be a scattered collection of stuff in access/genam.
36 * The ** routines: index_fetch, index_replace, and index_getattr
37 * have not yet been implemented. They may not be needed.
40 * Scans are implemented as follows:
42 * `0' represents an invalid item pointer.
43 * `-' represents an unknown item pointer.
44 * `X' represents a known item pointers.
45 * `+' represents known or invalid item pointers.
46 * `*' represents any item pointers.
48 * State is represented by a triple of these symbols in the order of
49 * previous, current, next. Note that the case of reverse scans works
53 * (1) + + - + 0 0 (if the next item pointer is invalid)
54 * (2) + X - (otherwise)
55 * (3) * 0 0 * 0 0 (no change)
56 * (4) + X 0 X 0 0 (shift)
57 * (5) * + X + X - (shift, add unknown)
59 * All other states cannot occur.
61 * Note: It would be possible to cache the status of the previous and
62 * next item pointer using the flags.
64 *-------------------------------------------------------------------------
69 #include "access/genam.h"
70 #include "access/heapam.h"
71 #include "utils/relcache.h"
74 /* ----------------------------------------------------------------
75 * macros used in index_ routines
76 * ----------------------------------------------------------------
78 #define RELATION_CHECKS \
80 AssertMacro(RelationIsValid(relation)), \
81 AssertMacro(PointerIsValid(relation->rd_am)) \
86 AssertMacro(IndexScanIsValid(scan)), \
87 AssertMacro(RelationIsValid(scan->relation)), \
88 AssertMacro(PointerIsValid(scan->relation->rd_am)) \
91 #define GET_REL_PROCEDURE(x,y) \
93 procedure = relation->rd_am->y, \
94 (!RegProcedureIsValid(procedure)) ? \
95 elog(ERROR, "index_%s: invalid %s regproc", \
96 CppAsString(x), CppAsString(y)) \
100 #define GET_SCAN_PROCEDURE(x,y) \
102 procedure = scan->relation->rd_am->y, \
103 (!RegProcedureIsValid(procedure)) ? \
104 elog(ERROR, "index_%s: invalid %s regproc", \
105 CppAsString(x), CppAsString(y)) \
110 /* ----------------------------------------------------------------
111 * index_ interface functions
112 * ----------------------------------------------------------------
115 * index_open - open an index relation by relationId
117 * presently the relcache routines do all the work we need
118 * to open/close index relations. However, callers of index_open
119 * expect it to succeed, so we need to check for a failure return.
121 * Note: we acquire no lock on the index. An AccessShareLock is
122 * acquired by index_beginscan (and released by index_endscan).
126 index_open(Oid relationId)
130 r = RelationIdGetRelation(relationId);
132 if (!RelationIsValid(r))
133 elog(ERROR, "Index %u does not exist", relationId);
135 if (r->rd_rel->relkind != RELKIND_INDEX)
136 elog(ERROR, "%s is not an index relation", RelationGetRelationName(r));
142 * index_openr - open a index relation by name
144 * As above, but lookup by name instead of OID.
148 index_openr(char *relationName)
152 r = RelationNameGetRelation(relationName);
154 if (!RelationIsValid(r))
155 elog(ERROR, "Index '%s' does not exist", relationName);
157 if (r->rd_rel->relkind != RELKIND_INDEX)
158 elog(ERROR, "%s is not an index relation", RelationGetRelationName(r));
164 * index_close - close a index relation
166 * presently the relcache routines do all the work we need
167 * to open/close index relations.
171 index_close(Relation relation)
173 RelationClose(relation);
177 * index_insert - insert an index tuple into a relation
181 index_insert(Relation relation,
184 ItemPointer heap_t_ctid,
187 RegProcedure procedure;
188 InsertIndexResult specificResult;
191 GET_REL_PROCEDURE(insert, aminsert);
194 * have the am's insert proc do all the work.
197 specificResult = (InsertIndexResult)
198 DatumGetPointer(OidFunctionCall6(procedure,
199 PointerGetDatum(relation),
200 PointerGetDatum(datum),
201 PointerGetDatum(nulls),
202 PointerGetDatum(heap_t_ctid),
203 PointerGetDatum(heapRel),
204 PointerGetDatum(NULL)));
206 /* must be pfree'ed */
207 return specificResult;
211 * index_delete - delete an item from an index relation
215 index_delete(Relation relation, ItemPointer indexItem)
217 RegProcedure procedure;
220 GET_REL_PROCEDURE(delete, amdelete);
222 OidFunctionCall2(procedure,
223 PointerGetDatum(relation),
224 PointerGetDatum(indexItem));
228 * index_beginscan - start a scan of an index
232 index_beginscan(Relation relation,
237 IndexScanDesc scandesc;
238 RegProcedure procedure;
241 GET_REL_PROCEDURE(beginscan, ambeginscan);
243 RelationIncrementReferenceCount(relation);
246 * Acquire AccessShareLock for the duration of the scan
248 * Note: we could get an SI inval message here and consequently have
249 * to rebuild the relcache entry. The refcount increment above
250 * ensures that we will rebuild it and not just flush it...
253 LockRelation(relation, AccessShareLock);
255 scandesc = (IndexScanDesc)
256 DatumGetPointer(OidFunctionCall4(procedure,
257 PointerGetDatum(relation),
258 BoolGetDatum(scanFromEnd),
259 UInt16GetDatum(numberOfKeys),
260 PointerGetDatum(key)));
266 * index_rescan - restart a scan of an index
270 index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
272 RegProcedure procedure;
275 GET_SCAN_PROCEDURE(rescan, amrescan);
277 OidFunctionCall3(procedure,
278 PointerGetDatum(scan),
279 BoolGetDatum(scanFromEnd),
280 PointerGetDatum(key));
284 * index_endscan - end a scan
288 index_endscan(IndexScanDesc scan)
290 RegProcedure procedure;
293 GET_SCAN_PROCEDURE(endscan, amendscan);
295 OidFunctionCall1(procedure, PointerGetDatum(scan));
297 /* Release lock and refcount acquired by index_beginscan */
299 UnlockRelation(scan->relation, AccessShareLock);
301 RelationDecrementReferenceCount(scan->relation);
303 /* Release the scan data structure itself */
308 * index_markpos - mark a scan position
312 index_markpos(IndexScanDesc scan)
314 RegProcedure procedure;
317 GET_SCAN_PROCEDURE(markpos, ammarkpos);
319 OidFunctionCall1(procedure, PointerGetDatum(scan));
323 * index_restrpos - restore a scan position
327 index_restrpos(IndexScanDesc scan)
329 RegProcedure procedure;
332 GET_SCAN_PROCEDURE(restrpos, amrestrpos);
334 OidFunctionCall1(procedure, PointerGetDatum(scan));
338 * index_getnext - get the next tuple from a scan
340 * A RetrieveIndexResult is a index tuple/heap tuple pair
344 index_getnext(IndexScanDesc scan,
345 ScanDirection direction)
347 RetrieveIndexResult result;
352 * Look up the access procedure only once per scan.
355 if (scan->fn_getnext.fn_oid == InvalidOid)
357 RegProcedure procedure;
359 GET_SCAN_PROCEDURE(getnext, amgettuple);
360 fmgr_info(procedure, &scan->fn_getnext);
364 * have the am's gettuple proc do all the work.
367 result = (RetrieveIndexResult)
368 DatumGetPointer(FunctionCall2(&scan->fn_getnext,
369 PointerGetDatum(scan),
370 Int32GetDatum(direction)));
376 * index_cost_estimator
378 * Fetch the amcostestimate procedure OID for an index.
380 * We could combine fetching and calling the procedure,
381 * as index_insert does for example; but that would require
382 * importing a bunch of planner/optimizer stuff into this file.
386 index_cost_estimator(Relation relation)
388 RegProcedure procedure;
391 GET_REL_PROCEDURE(cost_estimator, amcostestimate);
399 * Some indexed access methods may require support routines that are
400 * not in the operator class/operator model imposed by pg_am. These
401 * access methods may store the OIDs of registered procedures they
402 * need in pg_amproc. These registered procedure OIDs are ordered in
403 * a way that makes sense to the access method, and used only by the
404 * access method. The general index code doesn't know anything about
405 * the routines involved; it just builds an ordered list of them for
406 * each attribute on which an index is defined.
408 * This routine returns the requested procedure OID for a particular
413 index_getprocid(Relation irel,
420 natts = irel->rd_rel->relnatts;
422 loc = irel->rd_support;
426 return loc[(natts * (procnum - 1)) + (attnum - 1)];
430 GetIndexValue(HeapTuple tuple,
433 AttrNumber *attrNums,
434 FuncIndexInfo *fInfo,
439 if (PointerIsValid(fInfo) && FIgetProcOid(fInfo) != InvalidOid)
442 FunctionCallInfoData fcinfo;
444 bool anynull = false;
447 * XXX ought to store lookup info in FuncIndexInfo so it need not
448 * be repeated on each call?
450 fmgr_info(FIgetProcOid(fInfo), &flinfo);
452 MemSet(&fcinfo, 0, sizeof(fcinfo));
453 fcinfo.flinfo = &flinfo;
454 fcinfo.nargs = FIgetnArgs(fInfo);
456 for (i = 0; i < FIgetnArgs(fInfo); i++)
458 fcinfo.arg[i] = heap_getattr(tuple,
462 anynull |= fcinfo.argnull[i];
464 if (flinfo.fn_strict && anynull)
466 /* force a null result for strict function */
467 returnVal = (Datum) 0;
472 returnVal = FunctionCallInvoke(&fcinfo);
473 *attNull = fcinfo.isnull;
477 returnVal = heap_getattr(tuple, attrNums[attOff], hTupDesc, attNull);