1 /*-------------------------------------------------------------------------
4 * general index access method routines
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/access/index/indexam.c,v 1.7 1996/11/05 10:02:03 scrappy Exp $
13 * index_open - open an index relation by relationId
14 * index_openr - open a index relation by name
15 * index_close - close a index relation
16 * index_beginscan - start a scan of an index
17 * index_rescan - restart a scan of an index
18 * index_endscan - end a scan
19 * index_insert - insert an index tuple into a relation
20 * index_delete - delete an item from an index 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_fetch - retrieve tuple with tid
25 * ** index_replace - replace a tuple
26 * ** index_getattr - get an attribute from an index tuple
27 * index_getprocid - get a support procedure id from the rel tuple
29 * IndexScanIsValid - check index scan
32 * This file contains the index_ routines which used
33 * to be a scattered collection of stuff in access/genam.
35 * The ** routines: index_fetch, index_replace, and index_getattr
36 * have not yet been implemented. They may not be needed.
39 * Scans are implemented as follows:
41 * `0' represents an invalid item pointer.
42 * `-' represents an unknown item pointer.
43 * `X' represents a known item pointers.
44 * `+' represents known or invalid item pointers.
45 * `*' represents any item pointers.
47 * State is represented by a triple of these symbols in the order of
48 * previous, current, next. Note that the case of reverse scans works
52 * (1) + + - + 0 0 (if the next item pointer is invalid)
53 * (2) + X - (otherwise)
54 * (3) * 0 0 * 0 0 (no change)
55 * (4) + X 0 X 0 0 (shift)
56 * (5) * + X + X - (shift, add unknown)
58 * All other states cannot occur.
60 * Note: It would be possible to cache the status of the previous and
61 * next item pointer using the flags.
63 *-------------------------------------------------------------------------
68 #include <access/genam.h>
69 #include <utils/relcache.h>
71 #include <storage/lmgr.h>
72 #include <access/heaptuple.h>
75 * undefine macros we aren't going to use that would otherwise
76 * get in our way.. delete is defined in c.h and the am's are
90 /* ----------------------------------------------------------------
91 * macros used in index_ routines
92 * ----------------------------------------------------------------
94 #define RELATION_CHECKS \
95 Assert(RelationIsValid(relation)); \
96 Assert(PointerIsValid(relation->rd_am))
99 Assert(IndexScanIsValid(scan)); \
100 Assert(RelationIsValid(scan->relation)); \
101 Assert(PointerIsValid(scan->relation->rd_am))
103 #define GET_REL_PROCEDURE(x,y) \
104 CppConcat(procedure = relation->rd_am->,y); \
105 if (! RegProcedureIsValid(procedure)) \
106 elog(WARN, "index_%s: invalid %s regproc", \
107 CppAsString(x), CppAsString(y))
109 #define GET_SCAN_PROCEDURE(x,y) \
110 CppConcat(procedure = scan->relation->rd_am->,y); \
111 if (! RegProcedureIsValid(procedure)) \
112 elog(WARN, "index_%s: invalid %s regproc", \
113 CppAsString(x), CppAsString(y))
116 /* ----------------------------------------------------------------
117 * index_ interface functions
118 * ----------------------------------------------------------------
121 * index_open - open an index relation by relationId
123 * presently the relcache routines do all the work we need
124 * to open/close index relations.
128 index_open(Oid relationId)
130 return RelationIdGetRelation(relationId);
134 * index_openr - open a index relation by name
136 * presently the relcache routines do all the work we need
137 * to open/close index relations.
141 index_openr(char *relationName)
143 return RelationNameGetRelation(relationName);
147 * index_close - close a index relation
149 * presently the relcache routines do all the work we need
150 * to open/close index relations.
154 index_close(Relation relation)
156 (void) RelationClose(relation);
160 * index_insert - insert an index tuple into a relation
164 index_insert(Relation relation,
167 ItemPointer heap_t_ctid)
169 RegProcedure procedure;
170 InsertIndexResult specificResult;
173 GET_REL_PROCEDURE(insert,aminsert);
176 * have the am's insert proc do all the work.
179 specificResult = (InsertIndexResult)
180 fmgr(procedure, relation, datum, nulls, heap_t_ctid, NULL);
183 * the insert proc is supposed to return a "specific result" and
184 * this routine has to return a "general result" so after we get
185 * something back from the insert proc, we allocate a
186 * "general result" and copy some crap between the two.
188 * As far as I'm concerned all this result shit is needlessly c
189 * omplicated and should be eliminated. -cim 1/19/91
191 * mao concurs. regardless of how we feel here, however, it is
192 * important to free memory we don't intend to return to anyone.
195 * this "general result" crap is now gone. -ay 3/6/95
199 return (specificResult);
203 * index_delete - delete an item from an index relation
207 index_delete(Relation relation, ItemPointer indexItem)
209 RegProcedure procedure;
212 GET_REL_PROCEDURE(delete,amdelete);
214 (void) fmgr(procedure, relation, indexItem);
218 * index_beginscan - start a scan of an index
222 index_beginscan(Relation relation,
227 IndexScanDesc scandesc;
228 RegProcedure procedure;
231 GET_REL_PROCEDURE(beginscan,ambeginscan);
233 RelationSetRIntentLock(relation);
235 scandesc = (IndexScanDesc)
236 fmgr(procedure, relation, scanFromEnd, numberOfKeys, key);
242 * index_rescan - restart a scan of an index
246 index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
248 RegProcedure procedure;
251 GET_SCAN_PROCEDURE(rescan,amrescan);
253 (void) fmgr(procedure, scan, scanFromEnd, key);
257 * index_endscan - end a scan
261 index_endscan(IndexScanDesc scan)
263 RegProcedure procedure;
266 GET_SCAN_PROCEDURE(endscan,amendscan);
268 (void) fmgr(procedure, scan);
270 RelationUnsetRIntentLock(scan->relation);
274 * index_markpos - mark a scan position
278 index_markpos(IndexScanDesc scan)
280 RegProcedure procedure;
283 GET_SCAN_PROCEDURE(markpos,ammarkpos);
285 (void) fmgr(procedure, scan);
289 * index_restrpos - restore a scan position
293 index_restrpos(IndexScanDesc scan)
295 RegProcedure procedure;
298 GET_SCAN_PROCEDURE(restrpos,amrestrpos);
300 (void) fmgr(procedure, scan);
304 * index_getnext - get the next tuple from a scan
306 * A RetrieveIndexResult is a index tuple/heap tuple pair
310 index_getnext(IndexScanDesc scan,
311 ScanDirection direction)
313 RegProcedure procedure;
314 RetrieveIndexResult result;
317 GET_SCAN_PROCEDURE(getnext,amgettuple);
320 * have the am's gettuple proc do all the work.
323 result = (RetrieveIndexResult)
324 fmgr(procedure, scan, direction);
332 * Some indexed access methods may require support routines that are
333 * not in the operator class/operator model imposed by pg_am. These
334 * access methods may store the OIDs of registered procedures they
335 * need in pg_amproc. These registered procedure OIDs are ordered in
336 * a way that makes sense to the access method, and used only by the
337 * access method. The general index code doesn't know anything about
338 * the routines involved; it just builds an ordered list of them for
339 * each attribute on which an index is defined.
341 * This routine returns the requested procedure OID for a particular
346 index_getprocid(Relation irel,
353 natts = irel->rd_rel->relnatts;
355 loc = irel->rd_support;
359 return (loc[(natts * (procnum - 1)) + (attnum - 1)]);
363 GetIndexValue(HeapTuple tuple,
366 AttrNumber attrNums[],
367 FuncIndexInfo *fInfo,
374 if (PointerIsValid(fInfo) && FIgetProcOid(fInfo) != InvalidOid) {
376 Datum *attData = (Datum *)palloc(FIgetnArgs(fInfo)*sizeof(Datum));
378 for (i = 0; i < FIgetnArgs(fInfo); i++) {
379 attData[i] = (Datum) heap_getattr(tuple,
385 returnVal = (Datum)fmgr_array_args(FIgetProcOid(fInfo),
392 returnVal = (Datum) heap_getattr(tuple, buffer, attrNums[attOff],