]> granicus.if.org Git - postgresql/blob - src/backend/access/index/indexam.c
Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[postgresql] / src / backend / access / index / indexam.c
1 /*-------------------------------------------------------------------------
2  *
3  * indexam.c
4  *        general index access method routines
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/access/index/indexam.c,v 1.47 2001/01/24 19:42:48 momjian Exp $
12  *
13  * INTERFACE ROUTINES
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
29  *
30  *              IndexScanIsValid - check index scan
31  *
32  * NOTES
33  *              This file contains the index_ routines which used
34  *              to be a scattered collection of stuff in access/genam.
35  *
36  *              The ** routines: index_fetch, index_replace, and index_getattr
37  *              have not yet been implemented.  They may not be needed.
38  *
39  * old comments
40  *              Scans are implemented as follows:
41  *
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.
47  *
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
50  *              identically.
51  *
52  *                              State   Result
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)
58  *
59  *              All other states cannot occur.
60  *
61  *              Note: It would be possible to cache the status of the previous and
62  *                        next item pointer using the flags.
63  *
64  *-------------------------------------------------------------------------
65  */
66
67 #include "postgres.h"
68
69 #include "access/genam.h"
70 #include "access/heapam.h"
71 #include "utils/relcache.h"
72
73
74 /* ----------------------------------------------------------------
75  *                                      macros used in index_ routines
76  * ----------------------------------------------------------------
77  */
78 #define RELATION_CHECKS \
79 ( \
80         AssertMacro(RelationIsValid(relation)), \
81         AssertMacro(PointerIsValid(relation->rd_am)) \
82 )
83
84 #define SCAN_CHECKS \
85 ( \
86         AssertMacro(IndexScanIsValid(scan)), \
87         AssertMacro(RelationIsValid(scan->relation)), \
88         AssertMacro(PointerIsValid(scan->relation->rd_am)) \
89 )
90
91 #define GET_REL_PROCEDURE(x,y) \
92 ( \
93         procedure = relation->rd_am->y, \
94         (!RegProcedureIsValid(procedure)) ? \
95                 elog(ERROR, "index_%s: invalid %s regproc", \
96                         CppAsString(x), CppAsString(y)) \
97         : (void)NULL \
98 )
99
100 #define GET_SCAN_PROCEDURE(x,y) \
101 ( \
102         procedure = scan->relation->rd_am->y, \
103         (!RegProcedureIsValid(procedure)) ? \
104                 elog(ERROR, "index_%s: invalid %s regproc", \
105                         CppAsString(x), CppAsString(y)) \
106         : (void)NULL \
107 )
108
109
110 /* ----------------------------------------------------------------
111  *                                 index_ interface functions
112  * ----------------------------------------------------------------
113  */
114 /* ----------------
115  *              index_open - open an index relation by relationId
116  *
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.
120  *
121  *              Note: we acquire no lock on the index.  An AccessShareLock is
122  *              acquired by index_beginscan (and released by index_endscan).
123  * ----------------
124  */
125 Relation
126 index_open(Oid relationId)
127 {
128         Relation        r;
129
130         r = RelationIdGetRelation(relationId);
131
132         if (!RelationIsValid(r))
133                 elog(ERROR, "Index %u does not exist", relationId);
134
135         if (r->rd_rel->relkind != RELKIND_INDEX)
136                 elog(ERROR, "%s is not an index relation", RelationGetRelationName(r));
137
138         return r;
139 }
140
141 /* ----------------
142  *              index_openr - open a index relation by name
143  *
144  *              As above, but lookup by name instead of OID.
145  * ----------------
146  */
147 Relation
148 index_openr(char *relationName)
149 {
150         Relation        r;
151
152         r = RelationNameGetRelation(relationName);
153
154         if (!RelationIsValid(r))
155                 elog(ERROR, "Index '%s' does not exist", relationName);
156
157         if (r->rd_rel->relkind != RELKIND_INDEX)
158                 elog(ERROR, "%s is not an index relation", RelationGetRelationName(r));
159
160         return r;
161 }
162
163 /* ----------------
164  *              index_close - close a index relation
165  *
166  *              presently the relcache routines do all the work we need
167  *              to open/close index relations.
168  * ----------------
169  */
170 void
171 index_close(Relation relation)
172 {
173         RelationClose(relation);
174 }
175
176 /* ----------------
177  *              index_insert - insert an index tuple into a relation
178  * ----------------
179  */
180 InsertIndexResult
181 index_insert(Relation relation,
182                          Datum *datum,
183                          char *nulls,
184                          ItemPointer heap_t_ctid,
185                          Relation heapRel)
186 {
187         RegProcedure procedure;
188         InsertIndexResult specificResult;
189
190         RELATION_CHECKS;
191         GET_REL_PROCEDURE(insert, aminsert);
192
193         /* ----------------
194          *      have the am's insert proc do all the work.
195          * ----------------
196          */
197         specificResult = (InsertIndexResult)
198                 DatumGetPointer(OidFunctionCall5(procedure,
199                                                                                  PointerGetDatum(relation),
200                                                                                  PointerGetDatum(datum),
201                                                                                  PointerGetDatum(nulls),
202                                                                                  PointerGetDatum(heap_t_ctid),
203                                                                                  PointerGetDatum(heapRel)));
204
205         /* must be pfree'ed */
206         return specificResult;
207 }
208
209 /* ----------------
210  *              index_delete - delete an item from an index relation
211  * ----------------
212  */
213 void
214 index_delete(Relation relation, ItemPointer indexItem)
215 {
216         RegProcedure procedure;
217
218         RELATION_CHECKS;
219         GET_REL_PROCEDURE(delete, amdelete);
220
221         OidFunctionCall2(procedure,
222                                          PointerGetDatum(relation),
223                                          PointerGetDatum(indexItem));
224 }
225
226 /* ----------------
227  *              index_beginscan - start a scan of an index
228  * ----------------
229  */
230 IndexScanDesc
231 index_beginscan(Relation relation,
232                                 bool scanFromEnd,
233                                 uint16 numberOfKeys,
234                                 ScanKey key)
235 {
236         IndexScanDesc scandesc;
237         RegProcedure procedure;
238
239         RELATION_CHECKS;
240         GET_REL_PROCEDURE(beginscan, ambeginscan);
241
242         RelationIncrementReferenceCount(relation);
243
244         /* ----------------
245          *      Acquire AccessShareLock for the duration of the scan
246          *
247          *      Note: we could get an SI inval message here and consequently have
248          *      to rebuild the relcache entry.  The refcount increment above
249          *      ensures that we will rebuild it and not just flush it...
250          * ----------------
251          */
252         LockRelation(relation, AccessShareLock);
253
254         scandesc = (IndexScanDesc)
255                 DatumGetPointer(OidFunctionCall4(procedure,
256                                                                                  PointerGetDatum(relation),
257                                                                                  BoolGetDatum(scanFromEnd),
258                                                                                  UInt16GetDatum(numberOfKeys),
259                                                                                  PointerGetDatum(key)));
260
261         return scandesc;
262 }
263
264 /* ----------------
265  *              index_rescan  - restart a scan of an index
266  * ----------------
267  */
268 void
269 index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
270 {
271         RegProcedure procedure;
272
273         SCAN_CHECKS;
274         GET_SCAN_PROCEDURE(rescan, amrescan);
275
276         OidFunctionCall3(procedure,
277                                          PointerGetDatum(scan),
278                                          BoolGetDatum(scanFromEnd),
279                                          PointerGetDatum(key));
280 }
281
282 /* ----------------
283  *              index_endscan - end a scan
284  * ----------------
285  */
286 void
287 index_endscan(IndexScanDesc scan)
288 {
289         RegProcedure procedure;
290
291         SCAN_CHECKS;
292         GET_SCAN_PROCEDURE(endscan, amendscan);
293
294         OidFunctionCall1(procedure, PointerGetDatum(scan));
295
296         /* Release lock and refcount acquired by index_beginscan */
297
298         UnlockRelation(scan->relation, AccessShareLock);
299
300         RelationDecrementReferenceCount(scan->relation);
301
302         /* Release the scan data structure itself */
303         IndexScanEnd(scan);
304 }
305
306 /* ----------------
307  *              index_markpos  - mark a scan position
308  * ----------------
309  */
310 void
311 index_markpos(IndexScanDesc scan)
312 {
313         RegProcedure procedure;
314
315         SCAN_CHECKS;
316         GET_SCAN_PROCEDURE(markpos, ammarkpos);
317
318         OidFunctionCall1(procedure, PointerGetDatum(scan));
319 }
320
321 /* ----------------
322  *              index_restrpos  - restore a scan position
323  * ----------------
324  */
325 void
326 index_restrpos(IndexScanDesc scan)
327 {
328         RegProcedure procedure;
329
330         SCAN_CHECKS;
331         GET_SCAN_PROCEDURE(restrpos, amrestrpos);
332
333         OidFunctionCall1(procedure, PointerGetDatum(scan));
334 }
335
336 /* ----------------
337  *              index_getnext - get the next tuple from a scan
338  *
339  *              A RetrieveIndexResult is a index tuple/heap tuple pair
340  * ----------------
341  */
342 RetrieveIndexResult
343 index_getnext(IndexScanDesc scan,
344                           ScanDirection direction)
345 {
346         RetrieveIndexResult result;
347
348         SCAN_CHECKS;
349
350         /* ----------------
351          *      Look up the access procedure only once per scan.
352          * ----------------
353          */
354         if (scan->fn_getnext.fn_oid == InvalidOid)
355         {
356                 RegProcedure procedure;
357
358                 GET_SCAN_PROCEDURE(getnext, amgettuple);
359                 fmgr_info(procedure, &scan->fn_getnext);
360         }
361
362         /* ----------------
363          *      have the am's gettuple proc do all the work.
364          * ----------------
365          */
366         result = (RetrieveIndexResult)
367                 DatumGetPointer(FunctionCall2(&scan->fn_getnext,
368                                                                           PointerGetDatum(scan),
369                                                                           Int32GetDatum(direction)));
370
371         return result;
372 }
373
374 /* ----------------
375  *              index_cost_estimator
376  *
377  *              Fetch the amcostestimate procedure OID for an index.
378  *
379  *              We could combine fetching and calling the procedure,
380  *              as index_insert does for example; but that would require
381  *              importing a bunch of planner/optimizer stuff into this file.
382  * ----------------
383  */
384 RegProcedure
385 index_cost_estimator(Relation relation)
386 {
387         RegProcedure procedure;
388
389         RELATION_CHECKS;
390         GET_REL_PROCEDURE(cost_estimator, amcostestimate);
391
392         return procedure;
393 }
394
395 /* ----------------
396  *              index_getprocid
397  *
398  *              Some indexed access methods may require support routines that are
399  *              not in the operator class/operator model imposed by pg_am.      These
400  *              access methods may store the OIDs of registered procedures they
401  *              need in pg_amproc.      These registered procedure OIDs are ordered in
402  *              a way that makes sense to the access method, and used only by the
403  *              access method.  The general index code doesn't know anything about
404  *              the routines involved; it just builds an ordered list of them for
405  *              each attribute on which an index is defined.
406  *
407  *              This routine returns the requested procedure OID for a particular
408  *              indexed attribute.
409  * ----------------
410  */
411 RegProcedure
412 index_getprocid(Relation irel,
413                                 AttrNumber attnum,
414                                 uint16 procnum)
415 {
416         RegProcedure *loc;
417         int                     natts;
418
419         natts = irel->rd_rel->relnatts;
420
421         loc = irel->rd_support;
422
423         Assert(loc != NULL);
424
425         return loc[(natts * (procnum - 1)) + (attnum - 1)];
426 }