]> granicus.if.org Git - postgresql/blob - src/backend/access/index/indexam.c
Error message editing in backend/access.
[postgresql] / src / backend / access / index / indexam.c
1 /*-------------------------------------------------------------------------
2  *
3  * indexam.c
4  *        general index access method routines
5  *
6  * Portions Copyright (c) 1996-2002, 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.67 2003/07/21 20:29:39 tgl Exp $
12  *
13  * INTERFACE ROUTINES
14  *              index_open              - open an index relation by relation OID
15  *              index_openrv    - open an index relation specified by a RangeVar
16  *              index_openr             - open a system index relation by name
17  *              index_close             - close an index relation
18  *              index_beginscan - start a scan of an index
19  *              index_rescan    - restart a scan of an index
20  *              index_endscan   - end a scan
21  *              index_insert    - insert an index tuple into a 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_bulk_delete       - bulk deletion of index tuples
26  *              index_vacuum_cleanup    - post-deletion cleanup of an index
27  *              index_cost_estimator    - fetch amcostestimate procedure OID
28  *              index_getprocid - get a support procedure OID
29  *
30  * NOTES
31  *              This file contains the index_ routines which used
32  *              to be a scattered collection of stuff in access/genam.
33  *
34  *
35  * old comments
36  *              Scans are implemented as follows:
37  *
38  *              `0' represents an invalid item pointer.
39  *              `-' represents an unknown item pointer.
40  *              `X' represents a known item pointers.
41  *              `+' represents known or invalid item pointers.
42  *              `*' represents any item pointers.
43  *
44  *              State is represented by a triple of these symbols in the order of
45  *              previous, current, next.  Note that the case of reverse scans works
46  *              identically.
47  *
48  *                              State   Result
49  *              (1)             + + -   + 0 0                   (if the next item pointer is invalid)
50  *              (2)                             + X -                   (otherwise)
51  *              (3)             * 0 0   * 0 0                   (no change)
52  *              (4)             + X 0   X 0 0                   (shift)
53  *              (5)             * + X   + X -                   (shift, add unknown)
54  *
55  *              All other states cannot occur.
56  *
57  *              Note: It would be possible to cache the status of the previous and
58  *                        next item pointer using the flags.
59  *
60  *-------------------------------------------------------------------------
61  */
62
63 #include "postgres.h"
64
65 #include "access/genam.h"
66 #include "access/heapam.h"
67 #include "utils/relcache.h"
68
69 #include "pgstat.h"
70
71 /* ----------------------------------------------------------------
72  *                                      macros used in index_ routines
73  * ----------------------------------------------------------------
74  */
75 #define RELATION_CHECKS \
76 ( \
77         AssertMacro(RelationIsValid(indexRelation)), \
78         AssertMacro(PointerIsValid(indexRelation->rd_am)) \
79 )
80
81 #define SCAN_CHECKS \
82 ( \
83         AssertMacro(IndexScanIsValid(scan)), \
84         AssertMacro(RelationIsValid(scan->indexRelation)), \
85         AssertMacro(PointerIsValid(scan->indexRelation->rd_am)) \
86 )
87
88 #define GET_REL_PROCEDURE(x,y) \
89 ( \
90         procedure = indexRelation->rd_am->y, \
91         (!RegProcedureIsValid(procedure)) ? \
92                 elog(ERROR, "index_%s: invalid %s regproc", \
93                          CppAsString(x), CppAsString(y)) \
94         : (void)NULL \
95 )
96
97 #define GET_SCAN_PROCEDURE(x,y) \
98 ( \
99         procedure = scan->indexRelation->rd_am->y, \
100         (!RegProcedureIsValid(procedure)) ? \
101                 elog(ERROR, "index_%s: invalid %s regproc", \
102                          CppAsString(x), CppAsString(y)) \
103         : (void)NULL \
104 )
105
106
107 /* ----------------------------------------------------------------
108  *                                 index_ interface functions
109  * ----------------------------------------------------------------
110  */
111
112 /* ----------------
113  *              index_open - open an index relation by relation OID
114  *
115  *              Note: we acquire no lock on the index.  An AccessShareLock is
116  *              acquired by index_beginscan (and released by index_endscan).
117  *              Generally, the caller should already hold some type of lock on
118  *              the parent relation to ensure that the index doesn't disappear.
119  *
120  *              This is a convenience routine adapted for indexscan use.
121  *              Some callers may prefer to use relation_open directly.
122  * ----------------
123  */
124 Relation
125 index_open(Oid relationId)
126 {
127         Relation        r;
128
129         r = relation_open(relationId, NoLock);
130
131         if (r->rd_rel->relkind != RELKIND_INDEX)
132                 ereport(ERROR,
133                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
134                                  errmsg("\"%s\" is not an index relation",
135                                                 RelationGetRelationName(r))));
136
137         pgstat_initstats(&r->pgstat_info, r);
138
139         return r;
140 }
141
142 /* ----------------
143  *              index_openrv - open an index relation specified
144  *              by a RangeVar node
145  *
146  *              As above, but relation is specified by a RangeVar.
147  * ----------------
148  */
149 Relation
150 index_openrv(const RangeVar *relation)
151 {
152         Relation        r;
153
154         r = relation_openrv(relation, NoLock);
155
156         if (r->rd_rel->relkind != RELKIND_INDEX)
157                 ereport(ERROR,
158                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
159                                  errmsg("\"%s\" is not an index relation",
160                                                 RelationGetRelationName(r))));
161
162         pgstat_initstats(&r->pgstat_info, r);
163
164         return r;
165 }
166
167 /* ----------------
168  *              index_openr - open a system index relation specified by name.
169  *
170  *              As above, but the relation is specified by an unqualified name;
171  *              it is assumed to live in the system catalog namespace.
172  * ----------------
173  */
174 Relation
175 index_openr(const char *sysRelationName)
176 {
177         Relation        r;
178
179         r = relation_openr(sysRelationName, NoLock);
180
181         if (r->rd_rel->relkind != RELKIND_INDEX)
182                 ereport(ERROR,
183                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
184                                  errmsg("\"%s\" is not an index relation",
185                                                 RelationGetRelationName(r))));
186
187         pgstat_initstats(&r->pgstat_info, r);
188
189         return r;
190 }
191
192 /* ----------------
193  *              index_close - close a index relation
194  *
195  *              presently the relcache routines do all the work we need
196  *              to open/close index relations.
197  * ----------------
198  */
199 void
200 index_close(Relation relation)
201 {
202         RelationClose(relation);
203 }
204
205 /* ----------------
206  *              index_insert - insert an index tuple into a relation
207  * ----------------
208  */
209 InsertIndexResult
210 index_insert(Relation indexRelation,
211                          Datum *datums,
212                          char *nulls,
213                          ItemPointer heap_t_ctid,
214                          Relation heapRelation,
215                          bool check_uniqueness)
216 {
217         RegProcedure procedure;
218         InsertIndexResult specificResult;
219
220         RELATION_CHECKS;
221         GET_REL_PROCEDURE(insert, aminsert);
222
223         /*
224          * have the am's insert proc do all the work.
225          */
226         specificResult = (InsertIndexResult)
227                 DatumGetPointer(OidFunctionCall6(procedure,
228                                                                                  PointerGetDatum(indexRelation),
229                                                                                  PointerGetDatum(datums),
230                                                                                  PointerGetDatum(nulls),
231                                                                                  PointerGetDatum(heap_t_ctid),
232                                                                                  PointerGetDatum(heapRelation),
233                                                                                  BoolGetDatum(check_uniqueness)));
234
235         /* must be pfree'ed */
236         return specificResult;
237 }
238
239 /* ----------------
240  *              index_beginscan - start a scan of an index
241  *
242  * Note: heapRelation may be NULL if there is no intention of calling
243  * index_getnext on this scan; index_getnext_indexitem will not use the
244  * heapRelation link (nor the snapshot).  However, the caller had better
245  * be holding some kind of lock on the heap relation in any case, to ensure
246  * no one deletes it (or the index) out from under us.
247  * ----------------
248  */
249 IndexScanDesc
250 index_beginscan(Relation heapRelation,
251                                 Relation indexRelation,
252                                 Snapshot snapshot,
253                                 int nkeys, ScanKey key)
254 {
255         IndexScanDesc scan;
256         RegProcedure procedure;
257
258         RELATION_CHECKS;
259         GET_REL_PROCEDURE(beginscan, ambeginscan);
260
261         RelationIncrementReferenceCount(indexRelation);
262
263         /*
264          * Acquire AccessShareLock for the duration of the scan
265          *
266          * Note: we could get an SI inval message here and consequently have to
267          * rebuild the relcache entry.  The refcount increment above ensures
268          * that we will rebuild it and not just flush it...
269          */
270         LockRelation(indexRelation, AccessShareLock);
271
272         /*
273          * Tell the AM to open a scan.
274          */
275         scan = (IndexScanDesc)
276                 DatumGetPointer(OidFunctionCall3(procedure,
277                                                                                  PointerGetDatum(indexRelation),
278                                                                                  Int32GetDatum(nkeys),
279                                                                                  PointerGetDatum(key)));
280
281         /*
282          * Save additional parameters into the scandesc.  Everything else was
283          * set up by RelationGetIndexScan.
284          */
285         scan->heapRelation = heapRelation;
286         scan->xs_snapshot = snapshot;
287
288         /*
289          * We want to look up the amgettuple procedure just once per scan, not
290          * once per index_getnext call.  So do it here and save the fmgr info
291          * result in the scan descriptor.
292          */
293         GET_SCAN_PROCEDURE(beginscan, amgettuple);
294         fmgr_info(procedure, &scan->fn_getnext);
295
296         return scan;
297 }
298
299 /* ----------------
300  *              index_rescan  - (re)start a scan of an index
301  *
302  * The caller may specify a new set of scankeys (but the number of keys
303  * cannot change).  To restart the scan without changing keys, pass NULL
304  * for the key array.
305  *
306  * Note that this is also called when first starting an indexscan;
307  * see RelationGetIndexScan.  Keys *must* be passed in that case,
308  * unless scan->numberOfKeys is zero.
309  * ----------------
310  */
311 void
312 index_rescan(IndexScanDesc scan, ScanKey key)
313 {
314         RegProcedure procedure;
315
316         SCAN_CHECKS;
317         GET_SCAN_PROCEDURE(rescan, amrescan);
318
319         scan->kill_prior_tuple = false;         /* for safety */
320         scan->keys_are_unique = false;          /* may be set by index AM */
321         scan->got_tuple = false;
322         scan->unique_tuple_pos = 0;
323         scan->unique_tuple_mark = 0;
324
325         OidFunctionCall2(procedure,
326                                          PointerGetDatum(scan),
327                                          PointerGetDatum(key));
328
329         pgstat_reset_index_scan(&scan->xs_pgstat_info);
330 }
331
332 /* ----------------
333  *              index_endscan - end a scan
334  * ----------------
335  */
336 void
337 index_endscan(IndexScanDesc scan)
338 {
339         RegProcedure procedure;
340
341         SCAN_CHECKS;
342         GET_SCAN_PROCEDURE(endscan, amendscan);
343
344         /* Release any held pin on a heap page */
345         if (BufferIsValid(scan->xs_cbuf))
346         {
347                 ReleaseBuffer(scan->xs_cbuf);
348                 scan->xs_cbuf = InvalidBuffer;
349         }
350
351         /* End the AM's scan */
352         OidFunctionCall1(procedure, PointerGetDatum(scan));
353
354         /* Release index lock and refcount acquired by index_beginscan */
355
356         UnlockRelation(scan->indexRelation, AccessShareLock);
357
358         RelationDecrementReferenceCount(scan->indexRelation);
359
360         /* Release the scan data structure itself */
361         IndexScanEnd(scan);
362 }
363
364 /* ----------------
365  *              index_markpos  - mark a scan position
366  * ----------------
367  */
368 void
369 index_markpos(IndexScanDesc scan)
370 {
371         RegProcedure procedure;
372
373         SCAN_CHECKS;
374         GET_SCAN_PROCEDURE(markpos, ammarkpos);
375
376         scan->unique_tuple_mark = scan->unique_tuple_pos;
377
378         OidFunctionCall1(procedure, PointerGetDatum(scan));
379 }
380
381 /* ----------------
382  *              index_restrpos  - restore a scan position
383  * ----------------
384  */
385 void
386 index_restrpos(IndexScanDesc scan)
387 {
388         RegProcedure procedure;
389
390         SCAN_CHECKS;
391         GET_SCAN_PROCEDURE(restrpos, amrestrpos);
392
393         scan->kill_prior_tuple = false;         /* for safety */
394
395         /*
396          * We do not reset got_tuple; so if the scan is actually being
397          * short-circuited by index_getnext, the effective position restoration
398          * is done by restoring unique_tuple_pos.
399          */
400         scan->unique_tuple_pos = scan->unique_tuple_mark;
401
402         OidFunctionCall1(procedure, PointerGetDatum(scan));
403 }
404
405 /* ----------------
406  *              index_getnext - get the next heap tuple from a scan
407  *
408  * The result is the next heap tuple satisfying the scan keys and the
409  * snapshot, or NULL if no more matching tuples exist.  On success,
410  * the buffer containing the heap tuple is pinned (the pin will be dropped
411  * at the next index_getnext or index_endscan).  The index TID corresponding
412  * to the heap tuple can be obtained if needed from scan->currentItemData.
413  * ----------------
414  */
415 HeapTuple
416 index_getnext(IndexScanDesc scan, ScanDirection direction)
417 {
418         HeapTuple       heapTuple = &scan->xs_ctup;
419
420         SCAN_CHECKS;
421
422         /* Release any previously held pin */
423         if (BufferIsValid(scan->xs_cbuf))
424         {
425                 ReleaseBuffer(scan->xs_cbuf);
426                 scan->xs_cbuf = InvalidBuffer;
427         }
428
429         /*
430          * If we already got a tuple and it must be unique, there's no need
431          * to make the index AM look through any additional tuples.  (This can
432          * save a useful amount of work in scenarios where there are many dead
433          * tuples due to heavy update activity.)
434          *
435          * To do this we must keep track of the logical scan position
436          * (before/on/after tuple).  Also, we have to be sure to release scan
437          * resources before returning NULL; if we fail to do so then a multi-index
438          * scan can easily run the system out of free buffers.  We can release
439          * index-level resources fairly cheaply by calling index_rescan.  This
440          * means there are two persistent states as far as the index AM is
441          * concerned: on-tuple and rescanned.  If we are actually asked to
442          * re-fetch the single tuple, we have to go through a fresh indexscan
443          * startup, which penalizes that (infrequent) case.
444          */
445         if (scan->keys_are_unique && scan->got_tuple)
446         {
447                 int             new_tuple_pos = scan->unique_tuple_pos;
448
449                 if (ScanDirectionIsForward(direction))
450                 {
451                         if (new_tuple_pos <= 0)
452                                 new_tuple_pos++;
453                 }
454                 else
455                 {
456                         if (new_tuple_pos >= 0)
457                                 new_tuple_pos--;
458                 }
459                 if (new_tuple_pos == 0)
460                 {
461                         /*
462                          * We are moving onto the unique tuple from having been off it.
463                          * We just fall through and let the index AM do the work.  Note
464                          * we should get the right answer regardless of scan direction.
465                          */
466                         scan->unique_tuple_pos = 0;     /* need to update position */
467                 }
468                 else
469                 {
470                         /*
471                          * Moving off the tuple; must do amrescan to release index-level
472                          * pins before we return NULL.  Since index_rescan will reset
473                          * my state, must save and restore...
474                          */
475                         int             unique_tuple_mark = scan->unique_tuple_mark;
476
477                         index_rescan(scan, NULL /* no change to key */);
478
479                         scan->keys_are_unique = true;
480                         scan->got_tuple = true;
481                         scan->unique_tuple_pos = new_tuple_pos;
482                         scan->unique_tuple_mark = unique_tuple_mark;
483
484                         return NULL;
485                 }
486         }
487
488         /* just make sure this is false... */
489         scan->kill_prior_tuple = false;
490
491         for (;;)
492         {
493                 bool            found;
494                 uint16          sv_infomask;
495
496                 pgstat_count_index_scan(&scan->xs_pgstat_info);
497
498                 /*
499                  * The AM's gettuple proc finds the next tuple matching the scan
500                  * keys.  index_beginscan already set up fn_getnext.
501                  */
502                 found = DatumGetBool(FunctionCall2(&scan->fn_getnext,
503                                                                                    PointerGetDatum(scan),
504                                                                                    Int32GetDatum(direction)));
505
506                 /* Reset kill flag immediately for safety */
507                 scan->kill_prior_tuple = false;
508
509                 if (!found)
510                         return NULL;            /* failure exit */
511
512                 /*
513                  * Fetch the heap tuple and see if it matches the snapshot.
514                  */
515                 if (heap_fetch(scan->heapRelation, scan->xs_snapshot,
516                                            heapTuple, &scan->xs_cbuf, true,
517                                            &scan->xs_pgstat_info))
518                         break;
519
520                 /* Skip if no tuple at this location */
521                 if (heapTuple->t_data == NULL)
522                         continue;                       /* should we raise an error instead? */
523
524                 /*
525                  * If we can't see it, maybe no one else can either.  Check to see
526                  * if the tuple is dead to all transactions.  If so, signal the
527                  * index AM to not return it on future indexscans.
528                  *
529                  * We told heap_fetch to keep a pin on the buffer, so we can
530                  * re-access the tuple here.  But we must re-lock the buffer
531                  * first. Also, it's just barely possible for an update of hint
532                  * bits to occur here.
533                  */
534                 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
535                 sv_infomask = heapTuple->t_data->t_infomask;
536
537                 if (HeapTupleSatisfiesVacuum(heapTuple->t_data, RecentGlobalXmin) ==
538                         HEAPTUPLE_DEAD)
539                         scan->kill_prior_tuple = true;
540
541                 if (sv_infomask != heapTuple->t_data->t_infomask)
542                         SetBufferCommitInfoNeedsSave(scan->xs_cbuf);
543                 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
544                 ReleaseBuffer(scan->xs_cbuf);
545                 scan->xs_cbuf = InvalidBuffer;
546         }
547
548         /* Success exit */
549         scan->got_tuple = true;
550
551         /*
552          * If we just fetched a known-unique tuple, then subsequent calls will
553          * go through the short-circuit code above.  unique_tuple_pos has been
554          * initialized to 0, which is the correct state ("on row").
555          */
556
557         pgstat_count_index_getnext(&scan->xs_pgstat_info);
558
559         return heapTuple;
560 }
561
562 /* ----------------
563  *              index_getnext_indexitem - get the next index tuple from a scan
564  *
565  * Finds the next index tuple satisfying the scan keys.  Note that the
566  * corresponding heap tuple is not accessed, and thus no time qual (snapshot)
567  * check is done, other than the index AM's internal check for killed tuples
568  * (which most callers of this routine will probably want to suppress by
569  * setting scan->ignore_killed_tuples = false).
570  *
571  * On success (TRUE return), the found index TID is in scan->currentItemData,
572  * and its heap TID is in scan->xs_ctup.t_self.  scan->xs_cbuf is untouched.
573  * ----------------
574  */
575 bool
576 index_getnext_indexitem(IndexScanDesc scan,
577                                                 ScanDirection direction)
578 {
579         bool            found;
580
581         SCAN_CHECKS;
582
583         /* just make sure this is false... */
584         scan->kill_prior_tuple = false;
585
586         /*
587          * have the am's gettuple proc do all the work. index_beginscan
588          * already set up fn_getnext.
589          */
590         found = DatumGetBool(FunctionCall2(&scan->fn_getnext,
591                                                                            PointerGetDatum(scan),
592                                                                            Int32GetDatum(direction)));
593
594         return found;
595 }
596
597 /* ----------------
598  *              index_bulk_delete - do mass deletion of index entries
599  *
600  *              callback routine tells whether a given main-heap tuple is
601  *              to be deleted
602  *
603  *              return value is an optional palloc'd struct of statistics
604  * ----------------
605  */
606 IndexBulkDeleteResult *
607 index_bulk_delete(Relation indexRelation,
608                                   IndexBulkDeleteCallback callback,
609                                   void *callback_state)
610 {
611         RegProcedure procedure;
612         IndexBulkDeleteResult *result;
613
614         RELATION_CHECKS;
615         GET_REL_PROCEDURE(bulk_delete, ambulkdelete);
616
617         result = (IndexBulkDeleteResult *)
618                 DatumGetPointer(OidFunctionCall3(procedure,
619                                                                                  PointerGetDatum(indexRelation),
620                                                                          PointerGetDatum((Pointer) callback),
621                                                                            PointerGetDatum(callback_state)));
622
623         return result;
624 }
625
626 /* ----------------
627  *              index_vacuum_cleanup - do post-deletion cleanup of an index
628  *
629  *              return value is an optional palloc'd struct of statistics
630  * ----------------
631  */
632 IndexBulkDeleteResult *
633 index_vacuum_cleanup(Relation indexRelation,
634                                          IndexVacuumCleanupInfo *info,
635                                          IndexBulkDeleteResult *stats)
636 {
637         RegProcedure procedure;
638         IndexBulkDeleteResult *result;
639
640         RELATION_CHECKS;
641
642         /* It's okay for an index AM not to have a vacuumcleanup procedure */
643         if (!RegProcedureIsValid(indexRelation->rd_am->amvacuumcleanup))
644                 return stats;
645
646         GET_REL_PROCEDURE(vacuum_cleanup, amvacuumcleanup);
647
648         result = (IndexBulkDeleteResult *)
649                 DatumGetPointer(OidFunctionCall3(procedure,
650                                                                                  PointerGetDatum(indexRelation),
651                                                                                  PointerGetDatum((Pointer) info),
652                                                                                  PointerGetDatum((Pointer) stats)));
653
654         return result;
655 }
656
657 /* ----------------
658  *              index_cost_estimator
659  *
660  *              Fetch the amcostestimate procedure OID for an index.
661  *
662  *              We could combine fetching and calling the procedure,
663  *              as index_insert does for example; but that would require
664  *              importing a bunch of planner/optimizer stuff into this file.
665  * ----------------
666  */
667 RegProcedure
668 index_cost_estimator(Relation indexRelation)
669 {
670         RegProcedure procedure;
671
672         RELATION_CHECKS;
673         GET_REL_PROCEDURE(cost_estimator, amcostestimate);
674
675         return procedure;
676 }
677
678 /* ----------------
679  *              index_getprocid
680  *
681  *              Some indexed access methods may require support routines that are
682  *              not in the operator class/operator model imposed by pg_am.      These
683  *              access methods may store the OIDs of registered procedures they
684  *              need in pg_amproc.      These registered procedure OIDs are ordered in
685  *              a way that makes sense to the access method, and used only by the
686  *              access method.  The general index code doesn't know anything about
687  *              the routines involved; it just builds an ordered list of them for
688  *              each attribute on which an index is defined.
689  *
690  *              This routine returns the requested procedure OID for a particular
691  *              indexed attribute.
692  * ----------------
693  */
694 RegProcedure
695 index_getprocid(Relation irel,
696                                 AttrNumber attnum,
697                                 uint16 procnum)
698 {
699         RegProcedure *loc;
700         int                     nproc;
701         int                     procindex;
702
703         nproc = irel->rd_am->amsupport;
704
705         Assert(procnum > 0 && procnum <= (uint16) nproc);
706
707         procindex = (nproc * (attnum - 1)) + (procnum - 1);
708
709         loc = irel->rd_support;
710
711         Assert(loc != NULL);
712
713         return loc[procindex];
714 }
715
716 /* ----------------
717  *              index_getprocinfo
718  *
719  *              This routine allows index AMs to keep fmgr lookup info for
720  *              support procs in the relcache.
721  * ----------------
722  */
723 struct FmgrInfo *
724 index_getprocinfo(Relation irel,
725                                   AttrNumber attnum,
726                                   uint16 procnum)
727 {
728         FmgrInfo   *locinfo;
729         int                     nproc;
730         int                     procindex;
731
732         nproc = irel->rd_am->amsupport;
733
734         Assert(procnum > 0 && procnum <= (uint16) nproc);
735
736         procindex = (nproc * (attnum - 1)) + (procnum - 1);
737
738         locinfo = irel->rd_supportinfo;
739
740         Assert(locinfo != NULL);
741
742         locinfo += procindex;
743
744         /* Initialize the lookup info if first time through */
745         if (locinfo->fn_oid == InvalidOid)
746         {
747                 RegProcedure *loc = irel->rd_support;
748                 RegProcedure procId;
749
750                 Assert(loc != NULL);
751
752                 procId = loc[procindex];
753
754                 /*
755                  * Complain if function was not found during
756                  * IndexSupportInitialize. This should not happen unless the
757                  * system tables contain bogus entries for the index opclass.  (If
758                  * an AM wants to allow a support function to be optional, it can
759                  * use index_getprocid.)
760                  */
761                 if (!RegProcedureIsValid(procId))
762                         elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
763                                  procnum, attnum, RelationGetRelationName(irel));
764
765                 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);
766         }
767
768         return locinfo;
769 }