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.28 1998/10/02 16:27:43 momjian 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/heapam.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 \
96 AssertMacro(RelationIsValid(relation)), \
97 AssertMacro(PointerIsValid(relation->rd_am)) \
100 #define SCAN_CHECKS \
102 AssertMacro(IndexScanIsValid(scan)), \
103 AssertMacro(RelationIsValid(scan->relation)), \
104 AssertMacro(PointerIsValid(scan->relation->rd_am)) \
107 #define GET_REL_PROCEDURE(x,y) \
109 procedure = relation->rd_am->y, \
110 (!RegProcedureIsValid(procedure)) ? \
111 elog(ERROR, "index_%s: invalid %s regproc", \
112 CppAsString(x), CppAsString(y)) \
116 #define GET_SCAN_PROCEDURE(x,y) \
118 procedure = scan->relation->rd_am->y, \
119 (!RegProcedureIsValid(procedure)) ? \
120 elog(ERROR, "index_%s: invalid %s regproc", \
121 CppAsString(x), CppAsString(y)) \
126 /* ----------------------------------------------------------------
127 * index_ interface functions
128 * ----------------------------------------------------------------
131 * index_open - open an index relation by relationId
133 * presently the relcache routines do all the work we need
134 * to open/close index relations.
138 index_open(Oid relationId)
140 return RelationIdGetRelation(relationId);
144 * index_openr - open a index relation by name
146 * presently the relcache routines do all the work we need
147 * to open/close index relations.
151 index_openr(char *relationName)
153 return RelationNameGetRelation(relationName);
157 * index_close - close a index relation
159 * presently the relcache routines do all the work we need
160 * to open/close index relations.
164 index_close(Relation relation)
166 RelationClose(relation);
170 * index_insert - insert an index tuple into a relation
174 index_insert(Relation relation,
177 ItemPointer heap_t_ctid,
180 RegProcedure procedure;
181 InsertIndexResult specificResult;
184 GET_REL_PROCEDURE(insert, aminsert);
187 * have the am's insert proc do all the work.
190 specificResult = (InsertIndexResult)
191 fmgr(procedure, relation, datum, nulls, heap_t_ctid, heapRel, NULL);
193 /* must be pfree'ed */
194 return specificResult;
198 * index_delete - delete an item from an index relation
202 index_delete(Relation relation, ItemPointer indexItem)
204 RegProcedure procedure;
207 GET_REL_PROCEDURE(delete, amdelete);
209 fmgr(procedure, relation, indexItem);
213 * index_beginscan - start a scan of an index
217 index_beginscan(Relation relation,
222 IndexScanDesc scandesc;
223 RegProcedure procedure;
226 GET_REL_PROCEDURE(beginscan, ambeginscan);
228 RelationSetRIntentLock(relation);
230 scandesc = (IndexScanDesc)
231 fmgr(procedure, relation, scanFromEnd, numberOfKeys, key);
237 * index_rescan - restart a scan of an index
241 index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
243 RegProcedure procedure;
246 GET_SCAN_PROCEDURE(rescan, amrescan);
248 fmgr(procedure, scan, scanFromEnd, key);
252 * index_endscan - end a scan
256 index_endscan(IndexScanDesc scan)
258 RegProcedure procedure;
261 GET_SCAN_PROCEDURE(endscan, amendscan);
263 fmgr(procedure, scan);
265 RelationUnsetRIntentLock(scan->relation);
269 * index_markpos - mark a scan position
273 index_markpos(IndexScanDesc scan)
275 RegProcedure procedure;
278 GET_SCAN_PROCEDURE(markpos, ammarkpos);
280 fmgr(procedure, scan);
284 * index_restrpos - restore a scan position
288 index_restrpos(IndexScanDesc scan)
290 RegProcedure procedure;
293 GET_SCAN_PROCEDURE(restrpos, amrestrpos);
295 fmgr(procedure, scan);
299 * index_getnext - get the next tuple from a scan
301 * A RetrieveIndexResult is a index tuple/heap tuple pair
305 index_getnext(IndexScanDesc scan,
306 ScanDirection direction)
308 RegProcedure procedure;
309 RetrieveIndexResult result;
312 GET_SCAN_PROCEDURE(getnext, amgettuple);
315 * have the am's gettuple proc do all the work.
318 result = (RetrieveIndexResult) fmgr(procedure, scan, direction);
326 * Some indexed access methods may require support routines that are
327 * not in the operator class/operator model imposed by pg_am. These
328 * access methods may store the OIDs of registered procedures they
329 * need in pg_amproc. These registered procedure OIDs are ordered in
330 * a way that makes sense to the access method, and used only by the
331 * access method. The general index code doesn't know anything about
332 * the routines involved; it just builds an ordered list of them for
333 * each attribute on which an index is defined.
335 * This routine returns the requested procedure OID for a particular
340 index_getprocid(Relation irel,
347 natts = irel->rd_rel->relnatts;
349 loc = irel->rd_support;
353 return loc[(natts * (procnum - 1)) + (attnum - 1)];
357 GetIndexValue(HeapTuple tuple,
360 AttrNumber *attrNums,
361 FuncIndexInfo *fInfo,
367 if (PointerIsValid(fInfo) && FIgetProcOid(fInfo) != InvalidOid)
370 Datum *attData = (Datum *) palloc(FIgetnArgs(fInfo) * sizeof(Datum));
372 for (i = 0; i < FIgetnArgs(fInfo); i++)
374 attData[i] = heap_getattr(tuple,
381 returnVal = (Datum) fmgr_array_args(FIgetProcOid(fInfo),
389 returnVal = heap_getattr(tuple, attrNums[attOff], hTupDesc, attNull);