]> granicus.if.org Git - postgresql/blob - src/backend/access/index/indexam.c
Remove some unused and seldom used fields from RelationAmInfo.
[postgresql] / src / backend / access / index / indexam.c
1 /*-------------------------------------------------------------------------
2  *
3  * indexam.c
4  *        general index access method routines
5  *
6  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/access/index/indexam.c
12  *
13  * INTERFACE ROUTINES
14  *              index_open              - open an index relation by relation OID
15  *              index_close             - close an index relation
16  *              index_beginscan - start a scan of an index with amgettuple
17  *              index_beginscan_bitmap - start a scan of an index with amgetbitmap
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_markpos   - mark a scan position
22  *              index_restrpos  - restore a scan position
23  *              index_getnext_tid       - get the next TID from a scan
24  *              index_fetch_heap                - get the scan's next heap tuple
25  *              index_getnext   - get the next heap tuple from a scan
26  *              index_getbitmap - get all tuples from a scan
27  *              index_bulk_delete       - bulk deletion of index tuples
28  *              index_vacuum_cleanup    - post-deletion cleanup of an index
29  *              index_can_return        - does index support index-only scans?
30  *              index_getprocid - get a support procedure OID
31  *              index_getprocinfo - get a support procedure's lookup info
32  *
33  * NOTES
34  *              This file contains the index_ routines which used
35  *              to be a scattered collection of stuff in access/genam.
36  *
37  *
38  * old comments
39  *              Scans are implemented as follows:
40  *
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.
46  *
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
49  *              identically.
50  *
51  *                              State   Result
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)
57  *
58  *              All other states cannot occur.
59  *
60  *              Note: It would be possible to cache the status of the previous and
61  *                        next item pointer using the flags.
62  *
63  *-------------------------------------------------------------------------
64  */
65
66 #include "postgres.h"
67
68 #include "access/relscan.h"
69 #include "access/transam.h"
70 #include "catalog/index.h"
71 #include "pgstat.h"
72 #include "storage/bufmgr.h"
73 #include "storage/lmgr.h"
74 #include "storage/predicate.h"
75 #include "utils/snapmgr.h"
76 #include "utils/tqual.h"
77
78
79 /* ----------------------------------------------------------------
80  *                                      macros used in index_ routines
81  *
82  * Note: the ReindexIsProcessingIndex() check in RELATION_CHECKS is there
83  * to check that we don't try to scan or do retail insertions into an index
84  * that is currently being rebuilt or pending rebuild.  This helps to catch
85  * things that don't work when reindexing system catalogs.  The assertion
86  * doesn't prevent the actual rebuild because we don't use RELATION_CHECKS
87  * when calling the index AM's ambuild routine, and there is no reason for
88  * ambuild to call its subsidiary routines through this file.
89  * ----------------------------------------------------------------
90  */
91 #define RELATION_CHECKS \
92 ( \
93         AssertMacro(RelationIsValid(indexRelation)), \
94         AssertMacro(PointerIsValid(indexRelation->rd_am)), \
95         AssertMacro(!ReindexIsProcessingIndex(RelationGetRelid(indexRelation))) \
96 )
97
98 #define SCAN_CHECKS \
99 ( \
100         AssertMacro(IndexScanIsValid(scan)), \
101         AssertMacro(RelationIsValid(scan->indexRelation)), \
102         AssertMacro(PointerIsValid(scan->indexRelation->rd_am)) \
103 )
104
105 #define GET_REL_PROCEDURE(pname) \
106 do { \
107         procedure = &indexRelation->rd_aminfo->pname; \
108         if (!OidIsValid(procedure->fn_oid)) \
109         { \
110                 RegProcedure    procOid = indexRelation->rd_am->pname; \
111                 if (!RegProcedureIsValid(procOid)) \
112                         elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
113                 fmgr_info_cxt(procOid, procedure, indexRelation->rd_indexcxt); \
114         } \
115 } while(0)
116
117 #define GET_UNCACHED_REL_PROCEDURE(pname) \
118 do { \
119         if (!RegProcedureIsValid(indexRelation->rd_am->pname)) \
120                 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
121         fmgr_info(indexRelation->rd_am->pname, &procedure); \
122 } while(0)
123
124 #define GET_SCAN_PROCEDURE(pname) \
125 do { \
126         procedure = &scan->indexRelation->rd_aminfo->pname; \
127         if (!OidIsValid(procedure->fn_oid)) \
128         { \
129                 RegProcedure    procOid = scan->indexRelation->rd_am->pname; \
130                 if (!RegProcedureIsValid(procOid)) \
131                         elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
132                 fmgr_info_cxt(procOid, procedure, scan->indexRelation->rd_indexcxt); \
133         } \
134 } while(0)
135
136 static IndexScanDesc index_beginscan_internal(Relation indexRelation,
137                                                  int nkeys, int norderbys, Snapshot snapshot);
138
139
140 /* ----------------------------------------------------------------
141  *                                 index_ interface functions
142  * ----------------------------------------------------------------
143  */
144
145 /* ----------------
146  *              index_open - open an index relation by relation OID
147  *
148  *              If lockmode is not "NoLock", the specified kind of lock is
149  *              obtained on the index.  (Generally, NoLock should only be
150  *              used if the caller knows it has some appropriate lock on the
151  *              index already.)
152  *
153  *              An error is raised if the index does not exist.
154  *
155  *              This is a convenience routine adapted for indexscan use.
156  *              Some callers may prefer to use relation_open directly.
157  * ----------------
158  */
159 Relation
160 index_open(Oid relationId, LOCKMODE lockmode)
161 {
162         Relation        r;
163
164         r = relation_open(relationId, lockmode);
165
166         if (r->rd_rel->relkind != RELKIND_INDEX)
167                 ereport(ERROR,
168                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
169                                  errmsg("\"%s\" is not an index",
170                                                 RelationGetRelationName(r))));
171
172         return r;
173 }
174
175 /* ----------------
176  *              index_close - close an index relation
177  *
178  *              If lockmode is not "NoLock", we then release the specified lock.
179  *
180  *              Note that it is often sensible to hold a lock beyond index_close;
181  *              in that case, the lock is released automatically at xact end.
182  * ----------------
183  */
184 void
185 index_close(Relation relation, LOCKMODE lockmode)
186 {
187         LockRelId       relid = relation->rd_lockInfo.lockRelId;
188
189         Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
190
191         /* The relcache does the real work... */
192         RelationClose(relation);
193
194         if (lockmode != NoLock)
195                 UnlockRelationId(&relid, lockmode);
196 }
197
198 /* ----------------
199  *              index_insert - insert an index tuple into a relation
200  * ----------------
201  */
202 bool
203 index_insert(Relation indexRelation,
204                          Datum *values,
205                          bool *isnull,
206                          ItemPointer heap_t_ctid,
207                          Relation heapRelation,
208                          IndexUniqueCheck checkUnique)
209 {
210         FmgrInfo   *procedure;
211
212         RELATION_CHECKS;
213         GET_REL_PROCEDURE(aminsert);
214
215         if (!(indexRelation->rd_am->ampredlocks))
216                 CheckForSerializableConflictIn(indexRelation,
217                                                                            (HeapTuple) NULL,
218                                                                            InvalidBuffer);
219
220         /*
221          * have the am's insert proc do all the work.
222          */
223         return DatumGetBool(FunctionCall6(procedure,
224                                                                           PointerGetDatum(indexRelation),
225                                                                           PointerGetDatum(values),
226                                                                           PointerGetDatum(isnull),
227                                                                           PointerGetDatum(heap_t_ctid),
228                                                                           PointerGetDatum(heapRelation),
229                                                                           Int32GetDatum((int32) checkUnique)));
230 }
231
232 /*
233  * index_beginscan - start a scan of an index with amgettuple
234  *
235  * Caller must be holding suitable locks on the heap and the index.
236  */
237 IndexScanDesc
238 index_beginscan(Relation heapRelation,
239                                 Relation indexRelation,
240                                 Snapshot snapshot,
241                                 int nkeys, int norderbys)
242 {
243         IndexScanDesc scan;
244
245         scan = index_beginscan_internal(indexRelation, nkeys, norderbys, snapshot);
246
247         /*
248          * Save additional parameters into the scandesc.  Everything else was set
249          * up by RelationGetIndexScan.
250          */
251         scan->heapRelation = heapRelation;
252         scan->xs_snapshot = snapshot;
253
254         return scan;
255 }
256
257 /*
258  * index_beginscan_bitmap - start a scan of an index with amgetbitmap
259  *
260  * As above, caller had better be holding some lock on the parent heap
261  * relation, even though it's not explicitly mentioned here.
262  */
263 IndexScanDesc
264 index_beginscan_bitmap(Relation indexRelation,
265                                            Snapshot snapshot,
266                                            int nkeys)
267 {
268         IndexScanDesc scan;
269
270         scan = index_beginscan_internal(indexRelation, nkeys, 0, snapshot);
271
272         /*
273          * Save additional parameters into the scandesc.  Everything else was set
274          * up by RelationGetIndexScan.
275          */
276         scan->xs_snapshot = snapshot;
277
278         return scan;
279 }
280
281 /*
282  * index_beginscan_internal --- common code for index_beginscan variants
283  */
284 static IndexScanDesc
285 index_beginscan_internal(Relation indexRelation,
286                                                  int nkeys, int norderbys, Snapshot snapshot)
287 {
288         IndexScanDesc scan;
289         FmgrInfo   *procedure;
290
291         RELATION_CHECKS;
292         GET_REL_PROCEDURE(ambeginscan);
293
294         if (!(indexRelation->rd_am->ampredlocks))
295                 PredicateLockRelation(indexRelation, snapshot);
296
297         /*
298          * We hold a reference count to the relcache entry throughout the scan.
299          */
300         RelationIncrementReferenceCount(indexRelation);
301
302         /*
303          * Tell the AM to open a scan.
304          */
305         scan = (IndexScanDesc)
306                 DatumGetPointer(FunctionCall3(procedure,
307                                                                           PointerGetDatum(indexRelation),
308                                                                           Int32GetDatum(nkeys),
309                                                                           Int32GetDatum(norderbys)));
310
311         return scan;
312 }
313
314 /* ----------------
315  *              index_rescan  - (re)start a scan of an index
316  *
317  * During a restart, the caller may specify a new set of scankeys and/or
318  * orderbykeys; but the number of keys cannot differ from what index_beginscan
319  * was told.  (Later we might relax that to "must not exceed", but currently
320  * the index AMs tend to assume that scan->numberOfKeys is what to believe.)
321  * To restart the scan without changing keys, pass NULL for the key arrays.
322  * (Of course, keys *must* be passed on the first call, unless
323  * scan->numberOfKeys is zero.)
324  * ----------------
325  */
326 void
327 index_rescan(IndexScanDesc scan,
328                          ScanKey keys, int nkeys,
329                          ScanKey orderbys, int norderbys)
330 {
331         FmgrInfo   *procedure;
332
333         SCAN_CHECKS;
334         GET_SCAN_PROCEDURE(amrescan);
335
336         Assert(nkeys == scan->numberOfKeys);
337         Assert(norderbys == scan->numberOfOrderBys);
338
339         /* Release any held pin on a heap page */
340         if (BufferIsValid(scan->xs_cbuf))
341         {
342                 ReleaseBuffer(scan->xs_cbuf);
343                 scan->xs_cbuf = InvalidBuffer;
344         }
345
346         scan->xs_continue_hot = false;
347
348         scan->kill_prior_tuple = false;         /* for safety */
349
350         FunctionCall5(procedure,
351                                   PointerGetDatum(scan),
352                                   PointerGetDatum(keys),
353                                   Int32GetDatum(nkeys),
354                                   PointerGetDatum(orderbys),
355                                   Int32GetDatum(norderbys));
356 }
357
358 /* ----------------
359  *              index_endscan - end a scan
360  * ----------------
361  */
362 void
363 index_endscan(IndexScanDesc scan)
364 {
365         FmgrInfo   *procedure;
366
367         SCAN_CHECKS;
368         GET_SCAN_PROCEDURE(amendscan);
369
370         /* Release any held pin on a heap page */
371         if (BufferIsValid(scan->xs_cbuf))
372         {
373                 ReleaseBuffer(scan->xs_cbuf);
374                 scan->xs_cbuf = InvalidBuffer;
375         }
376
377         /* End the AM's scan */
378         FunctionCall1(procedure, PointerGetDatum(scan));
379
380         /* Release index refcount acquired by index_beginscan */
381         RelationDecrementReferenceCount(scan->indexRelation);
382
383         /* Release the scan data structure itself */
384         IndexScanEnd(scan);
385 }
386
387 /* ----------------
388  *              index_markpos  - mark a scan position
389  * ----------------
390  */
391 void
392 index_markpos(IndexScanDesc scan)
393 {
394         FmgrInfo   *procedure;
395
396         SCAN_CHECKS;
397         GET_SCAN_PROCEDURE(ammarkpos);
398
399         FunctionCall1(procedure, PointerGetDatum(scan));
400 }
401
402 /* ----------------
403  *              index_restrpos  - restore a scan position
404  *
405  * NOTE: this only restores the internal scan state of the index AM.
406  * The current result tuple (scan->xs_ctup) doesn't change.  See comments
407  * for ExecRestrPos().
408  *
409  * NOTE: in the presence of HOT chains, mark/restore only works correctly
410  * if the scan's snapshot is MVCC-safe; that ensures that there's at most one
411  * returnable tuple in each HOT chain, and so restoring the prior state at the
412  * granularity of the index AM is sufficient.  Since the only current user
413  * of mark/restore functionality is nodeMergejoin.c, this effectively means
414  * that merge-join plans only work for MVCC snapshots.  This could be fixed
415  * if necessary, but for now it seems unimportant.
416  * ----------------
417  */
418 void
419 index_restrpos(IndexScanDesc scan)
420 {
421         FmgrInfo   *procedure;
422
423         Assert(IsMVCCSnapshot(scan->xs_snapshot));
424
425         SCAN_CHECKS;
426         GET_SCAN_PROCEDURE(amrestrpos);
427
428         scan->xs_continue_hot = false;
429
430         scan->kill_prior_tuple = false;         /* for safety */
431
432         FunctionCall1(procedure, PointerGetDatum(scan));
433 }
434
435 /* ----------------
436  * index_getnext_tid - get the next TID from a scan
437  *
438  * The result is the next TID satisfying the scan keys,
439  * or NULL if no more matching tuples exist.
440  * ----------------
441  */
442 ItemPointer
443 index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
444 {
445         FmgrInfo   *procedure;
446         bool            found;
447
448         SCAN_CHECKS;
449         GET_SCAN_PROCEDURE(amgettuple);
450
451         Assert(TransactionIdIsValid(RecentGlobalXmin));
452
453         /*
454          * The AM's amgettuple proc finds the next index entry matching the scan
455          * keys, and puts the TID into scan->xs_ctup.t_self.  It should also set
456          * scan->xs_recheck and possibly scan->xs_itup, though we pay no attention
457          * to those fields here.
458          */
459         found = DatumGetBool(FunctionCall2(procedure,
460                                                                            PointerGetDatum(scan),
461                                                                            Int32GetDatum(direction)));
462
463         /* Reset kill flag immediately for safety */
464         scan->kill_prior_tuple = false;
465
466         /* If we're out of index entries, we're done */
467         if (!found)
468         {
469                 /* ... but first, release any held pin on a heap page */
470                 if (BufferIsValid(scan->xs_cbuf))
471                 {
472                         ReleaseBuffer(scan->xs_cbuf);
473                         scan->xs_cbuf = InvalidBuffer;
474                 }
475                 return NULL;
476         }
477
478         pgstat_count_index_tuples(scan->indexRelation, 1);
479
480         /* Return the TID of the tuple we found. */
481         return &scan->xs_ctup.t_self;
482 }
483
484 /* ----------------
485  *              index_fetch_heap - get the scan's next heap tuple
486  *
487  * The result is a visible heap tuple associated with the index TID most
488  * recently fetched by index_getnext_tid, or NULL if no more matching tuples
489  * exist.  (There can be more than one matching tuple because of HOT chains,
490  * although when using an MVCC snapshot it should be impossible for more than
491  * one such tuple to exist.)
492  *
493  * On success, the buffer containing the heap tup is pinned (the pin will be
494  * dropped in a future index_getnext_tid, index_fetch_heap or index_endscan
495  * call).
496  *
497  * Note: caller must check scan->xs_recheck, and perform rechecking of the
498  * scan keys if required.  We do not do that here because we don't have
499  * enough information to do it efficiently in the general case.
500  * ----------------
501  */
502 HeapTuple
503 index_fetch_heap(IndexScanDesc scan)
504 {
505         ItemPointer tid = &scan->xs_ctup.t_self;
506         bool            all_dead = false;
507         bool            got_heap_tuple;
508
509         /* We can skip the buffer-switching logic if we're in mid-HOT chain. */
510         if (!scan->xs_continue_hot)
511         {
512                 /* Switch to correct buffer if we don't have it already */
513                 Buffer          prev_buf = scan->xs_cbuf;
514
515                 scan->xs_cbuf = ReleaseAndReadBuffer(scan->xs_cbuf,
516                                                                                          scan->heapRelation,
517                                                                                          ItemPointerGetBlockNumber(tid));
518
519                 /*
520                  * Prune page, but only if we weren't already on this page
521                  */
522                 if (prev_buf != scan->xs_cbuf)
523                         heap_page_prune_opt(scan->heapRelation, scan->xs_cbuf,
524                                                                 RecentGlobalXmin);
525         }
526
527         /* Obtain share-lock on the buffer so we can examine visibility */
528         LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
529         got_heap_tuple = heap_hot_search_buffer(tid, scan->heapRelation,
530                                                                                         scan->xs_cbuf,
531                                                                                         scan->xs_snapshot,
532                                                                                         &scan->xs_ctup,
533                                                                                         &all_dead,
534                                                                                         !scan->xs_continue_hot);
535         LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
536
537         if (got_heap_tuple)
538         {
539                 /*
540                  * Only in a non-MVCC snapshot can more than one member of the HOT
541                  * chain be visible.
542                  */
543                 scan->xs_continue_hot = !IsMVCCSnapshot(scan->xs_snapshot);
544                 pgstat_count_heap_fetch(scan->indexRelation);
545                 return &scan->xs_ctup;
546         }
547
548         /* We've reached the end of the HOT chain. */
549         scan->xs_continue_hot = false;
550
551         /*
552          * If we scanned a whole HOT chain and found only dead tuples, tell index
553          * AM to kill its entry for that TID (this will take effect in the next
554          * amgettuple call, in index_getnext_tid).      We do not do this when in
555          * recovery because it may violate MVCC to do so.  See comments in
556          * RelationGetIndexScan().
557          */
558         if (!scan->xactStartedInRecovery)
559                 scan->kill_prior_tuple = all_dead;
560
561         return NULL;
562 }
563
564 /* ----------------
565  *              index_getnext - get the next heap tuple from a scan
566  *
567  * The result is the next heap tuple satisfying the scan keys and the
568  * snapshot, or NULL if no more matching tuples exist.
569  *
570  * On success, the buffer containing the heap tup is pinned (the pin will be
571  * dropped in a future index_getnext_tid, index_fetch_heap or index_endscan
572  * call).
573  *
574  * Note: caller must check scan->xs_recheck, and perform rechecking of the
575  * scan keys if required.  We do not do that here because we don't have
576  * enough information to do it efficiently in the general case.
577  * ----------------
578  */
579 HeapTuple
580 index_getnext(IndexScanDesc scan, ScanDirection direction)
581 {
582         HeapTuple       heapTuple;
583         ItemPointer tid;
584
585         for (;;)
586         {
587                 if (scan->xs_continue_hot)
588                 {
589                         /*
590                          * We are resuming scan of a HOT chain after having returned an
591                          * earlier member.      Must still hold pin on current heap page.
592                          */
593                         Assert(BufferIsValid(scan->xs_cbuf));
594                         Assert(ItemPointerGetBlockNumber(&scan->xs_ctup.t_self) ==
595                                    BufferGetBlockNumber(scan->xs_cbuf));
596                 }
597                 else
598                 {
599                         /* Time to fetch the next TID from the index */
600                         tid = index_getnext_tid(scan, direction);
601
602                         /* If we're out of index entries, we're done */
603                         if (tid == NULL)
604                                 break;
605                 }
606
607                 /*
608                  * Fetch the next (or only) visible heap tuple for this index entry.
609                  * If we don't find anything, loop around and grab the next TID from
610                  * the index.
611                  */
612                 heapTuple = index_fetch_heap(scan);
613                 if (heapTuple != NULL)
614                         return heapTuple;
615         }
616
617         return NULL;                            /* failure exit */
618 }
619
620 /* ----------------
621  *              index_getbitmap - get all tuples at once from an index scan
622  *
623  * Adds the TIDs of all heap tuples satisfying the scan keys to a bitmap.
624  * Since there's no interlock between the index scan and the eventual heap
625  * access, this is only safe to use with MVCC-based snapshots: the heap
626  * item slot could have been replaced by a newer tuple by the time we get
627  * to it.
628  *
629  * Returns the number of matching tuples found.  (Note: this might be only
630  * approximate, so it should only be used for statistical purposes.)
631  * ----------------
632  */
633 int64
634 index_getbitmap(IndexScanDesc scan, TIDBitmap *bitmap)
635 {
636         FmgrInfo   *procedure;
637         int64           ntids;
638         Datum           d;
639
640         SCAN_CHECKS;
641         GET_SCAN_PROCEDURE(amgetbitmap);
642
643         /* just make sure this is false... */
644         scan->kill_prior_tuple = false;
645
646         /*
647          * have the am's getbitmap proc do all the work.
648          */
649         d = FunctionCall2(procedure,
650                                           PointerGetDatum(scan),
651                                           PointerGetDatum(bitmap));
652
653         ntids = DatumGetInt64(d);
654
655         /* If int8 is pass-by-ref, must free the result to avoid memory leak */
656 #ifndef USE_FLOAT8_BYVAL
657         pfree(DatumGetPointer(d));
658 #endif
659
660         pgstat_count_index_tuples(scan->indexRelation, ntids);
661
662         return ntids;
663 }
664
665 /* ----------------
666  *              index_bulk_delete - do mass deletion of index entries
667  *
668  *              callback routine tells whether a given main-heap tuple is
669  *              to be deleted
670  *
671  *              return value is an optional palloc'd struct of statistics
672  * ----------------
673  */
674 IndexBulkDeleteResult *
675 index_bulk_delete(IndexVacuumInfo *info,
676                                   IndexBulkDeleteResult *stats,
677                                   IndexBulkDeleteCallback callback,
678                                   void *callback_state)
679 {
680         Relation        indexRelation = info->index;
681         FmgrInfo        procedure;
682         IndexBulkDeleteResult *result;
683
684         RELATION_CHECKS;
685         GET_UNCACHED_REL_PROCEDURE(ambulkdelete);
686
687         result = (IndexBulkDeleteResult *)
688                 DatumGetPointer(FunctionCall4(&procedure,
689                                                                           PointerGetDatum(info),
690                                                                           PointerGetDatum(stats),
691                                                                           PointerGetDatum((Pointer) callback),
692                                                                           PointerGetDatum(callback_state)));
693
694         return result;
695 }
696
697 /* ----------------
698  *              index_vacuum_cleanup - do post-deletion cleanup of an index
699  *
700  *              return value is an optional palloc'd struct of statistics
701  * ----------------
702  */
703 IndexBulkDeleteResult *
704 index_vacuum_cleanup(IndexVacuumInfo *info,
705                                          IndexBulkDeleteResult *stats)
706 {
707         Relation        indexRelation = info->index;
708         FmgrInfo        procedure;
709         IndexBulkDeleteResult *result;
710
711         RELATION_CHECKS;
712         GET_UNCACHED_REL_PROCEDURE(amvacuumcleanup);
713
714         result = (IndexBulkDeleteResult *)
715                 DatumGetPointer(FunctionCall2(&procedure,
716                                                                           PointerGetDatum(info),
717                                                                           PointerGetDatum(stats)));
718
719         return result;
720 }
721
722 /* ----------------
723  *              index_can_return - does index support index-only scans?
724  * ----------------
725  */
726 bool
727 index_can_return(Relation indexRelation)
728 {
729         FmgrInfo   *procedure;
730
731         RELATION_CHECKS;
732
733         /* amcanreturn is optional; assume FALSE if not provided by AM */
734         if (!RegProcedureIsValid(indexRelation->rd_am->amcanreturn))
735                 return false;
736
737         GET_REL_PROCEDURE(amcanreturn);
738
739         return DatumGetBool(FunctionCall1(procedure,
740                                                                           PointerGetDatum(indexRelation)));
741 }
742
743 /* ----------------
744  *              index_getprocid
745  *
746  *              Index access methods typically require support routines that are
747  *              not directly the implementation of any WHERE-clause query operator
748  *              and so cannot be kept in pg_amop.  Instead, such routines are kept
749  *              in pg_amproc.  These registered procedure OIDs are assigned numbers
750  *              according to a convention established by the access method.
751  *              The general index code doesn't know anything about the routines
752  *              involved; it just builds an ordered list of them for
753  *              each attribute on which an index is defined.
754  *
755  *              As of Postgres 8.3, support routines within an operator family
756  *              are further subdivided by the "left type" and "right type" of the
757  *              query operator(s) that they support.  The "default" functions for a
758  *              particular indexed attribute are those with both types equal to
759  *              the index opclass' opcintype (note that this is subtly different
760  *              from the indexed attribute's own type: it may be a binary-compatible
761  *              type instead).  Only the default functions are stored in relcache
762  *              entries --- access methods can use the syscache to look up non-default
763  *              functions.
764  *
765  *              This routine returns the requested default procedure OID for a
766  *              particular indexed attribute.
767  * ----------------
768  */
769 RegProcedure
770 index_getprocid(Relation irel,
771                                 AttrNumber attnum,
772                                 uint16 procnum)
773 {
774         RegProcedure *loc;
775         int                     nproc;
776         int                     procindex;
777
778         nproc = irel->rd_am->amsupport;
779
780         Assert(procnum > 0 && procnum <= (uint16) nproc);
781
782         procindex = (nproc * (attnum - 1)) + (procnum - 1);
783
784         loc = irel->rd_support;
785
786         Assert(loc != NULL);
787
788         return loc[procindex];
789 }
790
791 /* ----------------
792  *              index_getprocinfo
793  *
794  *              This routine allows index AMs to keep fmgr lookup info for
795  *              support procs in the relcache.  As above, only the "default"
796  *              functions for any particular indexed attribute are cached.
797  *
798  * Note: the return value points into cached data that will be lost during
799  * any relcache rebuild!  Therefore, either use the callinfo right away,
800  * or save it only after having acquired some type of lock on the index rel.
801  * ----------------
802  */
803 FmgrInfo *
804 index_getprocinfo(Relation irel,
805                                   AttrNumber attnum,
806                                   uint16 procnum)
807 {
808         FmgrInfo   *locinfo;
809         int                     nproc;
810         int                     procindex;
811
812         nproc = irel->rd_am->amsupport;
813
814         Assert(procnum > 0 && procnum <= (uint16) nproc);
815
816         procindex = (nproc * (attnum - 1)) + (procnum - 1);
817
818         locinfo = irel->rd_supportinfo;
819
820         Assert(locinfo != NULL);
821
822         locinfo += procindex;
823
824         /* Initialize the lookup info if first time through */
825         if (locinfo->fn_oid == InvalidOid)
826         {
827                 RegProcedure *loc = irel->rd_support;
828                 RegProcedure procId;
829
830                 Assert(loc != NULL);
831
832                 procId = loc[procindex];
833
834                 /*
835                  * Complain if function was not found during IndexSupportInitialize.
836                  * This should not happen unless the system tables contain bogus
837                  * entries for the index opclass.  (If an AM wants to allow a support
838                  * function to be optional, it can use index_getprocid.)
839                  */
840                 if (!RegProcedureIsValid(procId))
841                         elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
842                                  procnum, attnum, RelationGetRelationName(irel));
843
844                 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);
845         }
846
847         return locinfo;
848 }