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.2 1996/08/26 06:27:48 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 *-------------------------------------------------------------------------
67 #include "access/attnum.h"
68 #include "access/genam.h"
69 #include "access/heapam.h"
70 #include "access/itup.h"
71 #include "access/relscan.h"
72 #include "access/sdir.h"
73 #include "access/skey.h"
74 #include "access/funcindex.h"
76 #include "storage/lmgr.h"
77 #include "utils/elog.h"
78 #include "utils/palloc.h"
79 #include "utils/rel.h"
80 #include "utils/relcache.h"
82 #include "catalog/catname.h"
83 #include "catalog/pg_attribute.h"
84 #include "catalog/pg_index.h"
85 #include "catalog/pg_proc.h"
87 #include "catalog/index.h"
92 * undefine macros we aren't going to use that would otherwise
93 * get in our way.. delete is defined in c.h and the am's are
107 /* ----------------------------------------------------------------
108 * macros used in index_ routines
109 * ----------------------------------------------------------------
111 #define RELATION_CHECKS \
112 Assert(RelationIsValid(relation)); \
113 Assert(PointerIsValid(relation->rd_am))
115 #define SCAN_CHECKS \
116 Assert(IndexScanIsValid(scan)); \
117 Assert(RelationIsValid(scan->relation)); \
118 Assert(PointerIsValid(scan->relation->rd_am))
120 #define GET_REL_PROCEDURE(x,y) \
121 CppConcat(procedure = relation->rd_am->,y); \
122 if (! RegProcedureIsValid(procedure)) \
123 elog(WARN, "index_%s: invalid %s regproc", \
124 CppAsString(x), CppAsString(y))
126 #define GET_SCAN_PROCEDURE(x,y) \
127 CppConcat(procedure = scan->relation->rd_am->,y); \
128 if (! RegProcedureIsValid(procedure)) \
129 elog(WARN, "index_%s: invalid %s regproc", \
130 CppAsString(x), CppAsString(y))
133 /* ----------------------------------------------------------------
134 * index_ interface functions
135 * ----------------------------------------------------------------
138 * index_open - open an index relation by relationId
140 * presently the relcache routines do all the work we need
141 * to open/close index relations.
145 index_open(Oid relationId)
147 return RelationIdGetRelation(relationId);
151 * index_openr - open a index relation by name
153 * presently the relcache routines do all the work we need
154 * to open/close index relations.
158 index_openr(char *relationName)
160 return RelationNameGetRelation(relationName);
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 (void) RelationClose(relation);
177 * index_insert - insert an index tuple into a relation
181 index_insert(Relation relation,
184 ItemPointer heap_t_ctid)
186 RegProcedure procedure;
187 InsertIndexResult specificResult;
190 GET_REL_PROCEDURE(insert,aminsert);
193 * have the am's insert proc do all the work.
196 specificResult = (InsertIndexResult)
197 fmgr(procedure, relation, datum, nulls, heap_t_ctid, NULL);
200 * the insert proc is supposed to return a "specific result" and
201 * this routine has to return a "general result" so after we get
202 * something back from the insert proc, we allocate a
203 * "general result" and copy some crap between the two.
205 * As far as I'm concerned all this result shit is needlessly c
206 * omplicated and should be eliminated. -cim 1/19/91
208 * mao concurs. regardless of how we feel here, however, it is
209 * important to free memory we don't intend to return to anyone.
212 * this "general result" crap is now gone. -ay 3/6/95
216 return (specificResult);
220 * index_delete - delete an item from an index relation
224 index_delete(Relation relation, ItemPointer indexItem)
226 RegProcedure procedure;
229 GET_REL_PROCEDURE(delete,amdelete);
231 (void) fmgr(procedure, relation, indexItem);
235 * index_beginscan - start a scan of an index
239 index_beginscan(Relation relation,
244 IndexScanDesc scandesc;
245 RegProcedure procedure;
248 GET_REL_PROCEDURE(beginscan,ambeginscan);
250 RelationSetRIntentLock(relation);
252 scandesc = (IndexScanDesc)
253 fmgr(procedure, relation, scanFromEnd, numberOfKeys, key);
259 * index_rescan - restart a scan of an index
263 index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
265 RegProcedure procedure;
268 GET_SCAN_PROCEDURE(rescan,amrescan);
270 (void) fmgr(procedure, scan, scanFromEnd, key);
274 * index_endscan - end a scan
278 index_endscan(IndexScanDesc scan)
280 RegProcedure procedure;
283 GET_SCAN_PROCEDURE(endscan,amendscan);
285 (void) fmgr(procedure, scan);
287 RelationUnsetRIntentLock(scan->relation);
291 * index_markpos - mark a scan position
295 index_markpos(IndexScanDesc scan)
297 RegProcedure procedure;
300 GET_SCAN_PROCEDURE(markpos,ammarkpos);
302 (void) fmgr(procedure, scan);
306 * index_restrpos - restore a scan position
310 index_restrpos(IndexScanDesc scan)
312 RegProcedure procedure;
315 GET_SCAN_PROCEDURE(restrpos,amrestrpos);
317 (void) fmgr(procedure, scan);
321 * index_getnext - get the next tuple from a scan
323 * A RetrieveIndexResult is a index tuple/heap tuple pair
327 index_getnext(IndexScanDesc scan,
328 ScanDirection direction)
330 RegProcedure procedure;
331 RetrieveIndexResult result;
334 GET_SCAN_PROCEDURE(getnext,amgettuple);
337 * have the am's gettuple proc do all the work.
340 result = (RetrieveIndexResult)
341 fmgr(procedure, scan, direction);
349 * Some indexed access methods may require support routines that are
350 * not in the operator class/operator model imposed by pg_am. These
351 * access methods may store the OIDs of registered procedures they
352 * need in pg_amproc. These registered procedure OIDs are ordered in
353 * a way that makes sense to the access method, and used only by the
354 * access method. The general index code doesn't know anything about
355 * the routines involved; it just builds an ordered list of them for
356 * each attribute on which an index is defined.
358 * This routine returns the requested procedure OID for a particular
363 index_getprocid(Relation irel,
370 natts = irel->rd_rel->relnatts;
372 loc = irel->rd_support;
376 return (loc[(natts * (procnum - 1)) + (attnum - 1)]);
380 GetIndexValue(HeapTuple tuple,
383 AttrNumber attrNums[],
384 FuncIndexInfo *fInfo,
391 if (PointerIsValid(fInfo) && FIgetProcOid(fInfo) != InvalidOid) {
393 Datum *attData = (Datum *)palloc(FIgetnArgs(fInfo)*sizeof(Datum));
395 for (i = 0; i < FIgetnArgs(fInfo); i++) {
396 attData[i] = (Datum) heap_getattr(tuple,
402 returnVal = (Datum)fmgr_array_args(FIgetProcOid(fInfo),
409 returnVal = (Datum) heap_getattr(tuple, buffer, attrNums[attOff],