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