]> granicus.if.org Git - postgresql/blob - src/backend/access/index/indexam.c
Message editing: remove gratuitous variations in message wording, standardize
[postgresql] / src / backend / access / index / indexam.c
1 /*-------------------------------------------------------------------------
2  *
3  * indexam.c
4  *        general index access method routines
5  *
6  * Portions Copyright (c) 1996-2003, 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.71 2003/09/25 06:57:57 petere 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",
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",
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",
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
398          * restoration 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 to
431          * 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
438          * multi-index scan can easily run the system out of free buffers.      We
439          * can release index-level resources fairly cheaply by calling
440          * index_rescan.  This means there are two persistent states as far as
441          * the index AM is concerned: on-tuple and rescanned.  If we are
442          * actually asked to re-fetch the single tuple, we have to go through
443          * a fresh indexscan 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
463                          * it. We just fall through and let the index AM do the work.
464                          * Note we should get the right answer regardless of scan
465                          * direction.
466                          */
467                         scan->unique_tuple_pos = 0; /* need to update position */
468                 }
469                 else
470                 {
471                         /*
472                          * Moving off the tuple; must do amrescan to release
473                          * index-level pins before we return NULL.      Since index_rescan
474                          * will reset my state, must save and restore...
475                          */
476                         int                     unique_tuple_mark = scan->unique_tuple_mark;
477
478                         index_rescan(scan, NULL /* no change to key */ );
479
480                         scan->keys_are_unique = true;
481                         scan->got_tuple = true;
482                         scan->unique_tuple_pos = new_tuple_pos;
483                         scan->unique_tuple_mark = unique_tuple_mark;
484
485                         return NULL;
486                 }
487         }
488
489         /* just make sure this is false... */
490         scan->kill_prior_tuple = false;
491
492         for (;;)
493         {
494                 bool            found;
495                 uint16          sv_infomask;
496
497                 pgstat_count_index_scan(&scan->xs_pgstat_info);
498
499                 /*
500                  * The AM's gettuple proc finds the next tuple matching the scan
501                  * keys.  index_beginscan already set up fn_getnext.
502                  */
503                 found = DatumGetBool(FunctionCall2(&scan->fn_getnext,
504                                                                                    PointerGetDatum(scan),
505                                                                                    Int32GetDatum(direction)));
506
507                 /* Reset kill flag immediately for safety */
508                 scan->kill_prior_tuple = false;
509
510                 if (!found)
511                         return NULL;            /* failure exit */
512
513                 /*
514                  * Fetch the heap tuple and see if it matches the snapshot.
515                  */
516                 if (heap_fetch(scan->heapRelation, scan->xs_snapshot,
517                                            heapTuple, &scan->xs_cbuf, true,
518                                            &scan->xs_pgstat_info))
519                         break;
520
521                 /* Skip if no tuple at this location */
522                 if (heapTuple->t_data == NULL)
523                         continue;                       /* should we raise an error instead? */
524
525                 /*
526                  * If we can't see it, maybe no one else can either.  Check to see
527                  * if the tuple is dead to all transactions.  If so, signal the
528                  * index AM to not return it on future indexscans.
529                  *
530                  * We told heap_fetch to keep a pin on the buffer, so we can
531                  * re-access the tuple here.  But we must re-lock the buffer
532                  * first. Also, it's just barely possible for an update of hint
533                  * bits to occur here.
534                  */
535                 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
536                 sv_infomask = heapTuple->t_data->t_infomask;
537
538                 if (HeapTupleSatisfiesVacuum(heapTuple->t_data, RecentGlobalXmin) ==
539                         HEAPTUPLE_DEAD)
540                         scan->kill_prior_tuple = true;
541
542                 if (sv_infomask != heapTuple->t_data->t_infomask)
543                         SetBufferCommitInfoNeedsSave(scan->xs_cbuf);
544                 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
545                 ReleaseBuffer(scan->xs_cbuf);
546                 scan->xs_cbuf = InvalidBuffer;
547         }
548
549         /* Success exit */
550         scan->got_tuple = true;
551
552         /*
553          * If we just fetched a known-unique tuple, then subsequent calls will
554          * go through the short-circuit code above.  unique_tuple_pos has been
555          * initialized to 0, which is the correct state ("on row").
556          */
557
558         pgstat_count_index_getnext(&scan->xs_pgstat_info);
559
560         return heapTuple;
561 }
562
563 /* ----------------
564  *              index_getnext_indexitem - get the next index tuple from a scan
565  *
566  * Finds the next index tuple satisfying the scan keys.  Note that the
567  * corresponding heap tuple is not accessed, and thus no time qual (snapshot)
568  * check is done, other than the index AM's internal check for killed tuples
569  * (which most callers of this routine will probably want to suppress by
570  * setting scan->ignore_killed_tuples = false).
571  *
572  * On success (TRUE return), the found index TID is in scan->currentItemData,
573  * and its heap TID is in scan->xs_ctup.t_self.  scan->xs_cbuf is untouched.
574  * ----------------
575  */
576 bool
577 index_getnext_indexitem(IndexScanDesc scan,
578                                                 ScanDirection direction)
579 {
580         bool            found;
581
582         SCAN_CHECKS;
583
584         /* just make sure this is false... */
585         scan->kill_prior_tuple = false;
586
587         /*
588          * have the am's gettuple proc do all the work. index_beginscan
589          * already set up fn_getnext.
590          */
591         found = DatumGetBool(FunctionCall2(&scan->fn_getnext,
592                                                                            PointerGetDatum(scan),
593                                                                            Int32GetDatum(direction)));
594
595         return found;
596 }
597
598 /* ----------------
599  *              index_bulk_delete - do mass deletion of index entries
600  *
601  *              callback routine tells whether a given main-heap tuple is
602  *              to be deleted
603  *
604  *              return value is an optional palloc'd struct of statistics
605  * ----------------
606  */
607 IndexBulkDeleteResult *
608 index_bulk_delete(Relation indexRelation,
609                                   IndexBulkDeleteCallback callback,
610                                   void *callback_state)
611 {
612         RegProcedure procedure;
613         IndexBulkDeleteResult *result;
614
615         RELATION_CHECKS;
616         GET_REL_PROCEDURE(bulk_delete, ambulkdelete);
617
618         result = (IndexBulkDeleteResult *)
619                 DatumGetPointer(OidFunctionCall3(procedure,
620                                                                                  PointerGetDatum(indexRelation),
621                                                                          PointerGetDatum((Pointer) callback),
622                                                                            PointerGetDatum(callback_state)));
623
624         return result;
625 }
626
627 /* ----------------
628  *              index_vacuum_cleanup - do post-deletion cleanup of an index
629  *
630  *              return value is an optional palloc'd struct of statistics
631  * ----------------
632  */
633 IndexBulkDeleteResult *
634 index_vacuum_cleanup(Relation indexRelation,
635                                          IndexVacuumCleanupInfo *info,
636                                          IndexBulkDeleteResult *stats)
637 {
638         RegProcedure procedure;
639         IndexBulkDeleteResult *result;
640
641         RELATION_CHECKS;
642
643         /* It's okay for an index AM not to have a vacuumcleanup procedure */
644         if (!RegProcedureIsValid(indexRelation->rd_am->amvacuumcleanup))
645                 return stats;
646
647         GET_REL_PROCEDURE(vacuum_cleanup, amvacuumcleanup);
648
649         result = (IndexBulkDeleteResult *)
650                 DatumGetPointer(OidFunctionCall3(procedure,
651                                                                                  PointerGetDatum(indexRelation),
652                                                                                  PointerGetDatum((Pointer) info),
653                                                                           PointerGetDatum((Pointer) stats)));
654
655         return result;
656 }
657
658 /* ----------------
659  *              index_cost_estimator
660  *
661  *              Fetch the amcostestimate procedure OID for an index.
662  *
663  *              We could combine fetching and calling the procedure,
664  *              as index_insert does for example; but that would require
665  *              importing a bunch of planner/optimizer stuff into this file.
666  * ----------------
667  */
668 RegProcedure
669 index_cost_estimator(Relation indexRelation)
670 {
671         RegProcedure procedure;
672
673         RELATION_CHECKS;
674         GET_REL_PROCEDURE(cost_estimator, amcostestimate);
675
676         return procedure;
677 }
678
679 /* ----------------
680  *              index_getprocid
681  *
682  *              Some indexed access methods may require support routines that are
683  *              not in the operator class/operator model imposed by pg_am.      These
684  *              access methods may store the OIDs of registered procedures they
685  *              need in pg_amproc.      These registered procedure OIDs are ordered in
686  *              a way that makes sense to the access method, and used only by the
687  *              access method.  The general index code doesn't know anything about
688  *              the routines involved; it just builds an ordered list of them for
689  *              each attribute on which an index is defined.
690  *
691  *              This routine returns the requested procedure OID for a particular
692  *              indexed attribute.
693  * ----------------
694  */
695 RegProcedure
696 index_getprocid(Relation irel,
697                                 AttrNumber attnum,
698                                 uint16 procnum)
699 {
700         RegProcedure *loc;
701         int                     nproc;
702         int                     procindex;
703
704         nproc = irel->rd_am->amsupport;
705
706         Assert(procnum > 0 && procnum <= (uint16) nproc);
707
708         procindex = (nproc * (attnum - 1)) + (procnum - 1);
709
710         loc = irel->rd_support;
711
712         Assert(loc != NULL);
713
714         return loc[procindex];
715 }
716
717 /* ----------------
718  *              index_getprocinfo
719  *
720  *              This routine allows index AMs to keep fmgr lookup info for
721  *              support procs in the relcache.
722  * ----------------
723  */
724 struct FmgrInfo *
725 index_getprocinfo(Relation irel,
726                                   AttrNumber attnum,
727                                   uint16 procnum)
728 {
729         FmgrInfo   *locinfo;
730         int                     nproc;
731         int                     procindex;
732
733         nproc = irel->rd_am->amsupport;
734
735         Assert(procnum > 0 && procnum <= (uint16) nproc);
736
737         procindex = (nproc * (attnum - 1)) + (procnum - 1);
738
739         locinfo = irel->rd_supportinfo;
740
741         Assert(locinfo != NULL);
742
743         locinfo += procindex;
744
745         /* Initialize the lookup info if first time through */
746         if (locinfo->fn_oid == InvalidOid)
747         {
748                 RegProcedure *loc = irel->rd_support;
749                 RegProcedure procId;
750
751                 Assert(loc != NULL);
752
753                 procId = loc[procindex];
754
755                 /*
756                  * Complain if function was not found during
757                  * IndexSupportInitialize. This should not happen unless the
758                  * system tables contain bogus entries for the index opclass.  (If
759                  * an AM wants to allow a support function to be optional, it can
760                  * use index_getprocid.)
761                  */
762                 if (!RegProcedureIsValid(procId))
763                         elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
764                                  procnum, attnum, RelationGetRelationName(irel));
765
766                 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);
767         }
768
769         return locinfo;
770 }