LCOV - differential code coverage report
Current view: top level - src/backend/access/index - indexam.c (source / functions) Coverage Total Hit LBC UIC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 98.3 % 236 232 1 3 1 143 5 83 3 145 3
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 25 25 21 1 3 21
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * indexam.c
       4                 :  *    general index access method routines
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, 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_parallelscan_estimate - estimate shared memory for parallel scan
      24                 :  *      index_parallelscan_initialize - initialize parallel scan
      25                 :  *      index_parallelrescan  - (re)start a parallel scan of an index
      26                 :  *      index_beginscan_parallel - join parallel index scan
      27                 :  *      index_getnext_tid   - get the next TID from a scan
      28                 :  *      index_fetch_heap        - get the scan's next heap tuple
      29                 :  *      index_getnext_slot  - get the next tuple from a scan
      30                 :  *      index_getbitmap - get all tuples from a scan
      31                 :  *      index_bulk_delete   - bulk deletion of index tuples
      32                 :  *      index_vacuum_cleanup    - post-deletion cleanup of an index
      33                 :  *      index_can_return    - does index support index-only scans?
      34                 :  *      index_getprocid - get a support procedure OID
      35                 :  *      index_getprocinfo - get a support procedure's lookup info
      36                 :  *
      37                 :  * NOTES
      38                 :  *      This file contains the index_ routines which used
      39                 :  *      to be a scattered collection of stuff in access/genam.
      40                 :  *
      41                 :  *-------------------------------------------------------------------------
      42                 :  */
      43                 : 
      44                 : #include "postgres.h"
      45                 : 
      46                 : #include "access/amapi.h"
      47                 : #include "access/heapam.h"
      48                 : #include "access/reloptions.h"
      49                 : #include "access/relscan.h"
      50                 : #include "access/tableam.h"
      51                 : #include "access/transam.h"
      52                 : #include "access/xlog.h"
      53                 : #include "catalog/index.h"
      54                 : #include "catalog/pg_amproc.h"
      55                 : #include "catalog/pg_type.h"
      56                 : #include "commands/defrem.h"
      57                 : #include "nodes/makefuncs.h"
      58                 : #include "pgstat.h"
      59                 : #include "storage/bufmgr.h"
      60                 : #include "storage/lmgr.h"
      61                 : #include "storage/predicate.h"
      62                 : #include "utils/ruleutils.h"
      63                 : #include "utils/snapmgr.h"
      64                 : #include "utils/syscache.h"
      65                 : 
      66                 : 
      67                 : /* ----------------------------------------------------------------
      68                 :  *                  macros used in index_ routines
      69                 :  *
      70                 :  * Note: the ReindexIsProcessingIndex() check in RELATION_CHECKS is there
      71                 :  * to check that we don't try to scan or do retail insertions into an index
      72                 :  * that is currently being rebuilt or pending rebuild.  This helps to catch
      73                 :  * things that don't work when reindexing system catalogs.  The assertion
      74                 :  * doesn't prevent the actual rebuild because we don't use RELATION_CHECKS
      75                 :  * when calling the index AM's ambuild routine, and there is no reason for
      76                 :  * ambuild to call its subsidiary routines through this file.
      77                 :  * ----------------------------------------------------------------
      78                 :  */
      79                 : #define RELATION_CHECKS \
      80                 : ( \
      81                 :     AssertMacro(RelationIsValid(indexRelation)), \
      82                 :     AssertMacro(PointerIsValid(indexRelation->rd_indam)), \
      83                 :     AssertMacro(!ReindexIsProcessingIndex(RelationGetRelid(indexRelation))) \
      84                 : )
      85                 : 
      86                 : #define SCAN_CHECKS \
      87                 : ( \
      88                 :     AssertMacro(IndexScanIsValid(scan)), \
      89                 :     AssertMacro(RelationIsValid(scan->indexRelation)), \
      90                 :     AssertMacro(PointerIsValid(scan->indexRelation->rd_indam)) \
      91                 : )
      92                 : 
      93                 : #define CHECK_REL_PROCEDURE(pname) \
      94                 : do { \
      95                 :     if (indexRelation->rd_indam->pname == NULL) \
      96                 :         elog(ERROR, "function \"%s\" is not defined for index \"%s\"", \
      97                 :              CppAsString(pname), RelationGetRelationName(indexRelation)); \
      98                 : } while(0)
      99                 : 
     100                 : #define CHECK_SCAN_PROCEDURE(pname) \
     101                 : do { \
     102                 :     if (scan->indexRelation->rd_indam->pname == NULL) \
     103                 :         elog(ERROR, "function \"%s\" is not defined for index \"%s\"", \
     104                 :              CppAsString(pname), RelationGetRelationName(scan->indexRelation)); \
     105                 : } while(0)
     106                 : 
     107                 : static IndexScanDesc index_beginscan_internal(Relation indexRelation,
     108                 :                                               int nkeys, int norderbys, Snapshot snapshot,
     109                 :                                               ParallelIndexScanDesc pscan, bool temp_snap);
     110                 : 
     111                 : 
     112                 : /* ----------------------------------------------------------------
     113                 :  *                 index_ interface functions
     114                 :  * ----------------------------------------------------------------
     115                 :  */
     116                 : 
     117                 : /* ----------------
     118                 :  *      index_open - open an index relation by relation OID
     119                 :  *
     120                 :  *      If lockmode is not "NoLock", the specified kind of lock is
     121                 :  *      obtained on the index.  (Generally, NoLock should only be
     122                 :  *      used if the caller knows it has some appropriate lock on the
     123                 :  *      index already.)
     124                 :  *
     125                 :  *      An error is raised if the index does not exist.
     126                 :  *
     127                 :  *      This is a convenience routine adapted for indexscan use.
     128                 :  *      Some callers may prefer to use relation_open directly.
     129                 :  * ----------------
     130                 :  */
     131                 : Relation
     132 CBC    13672125 : index_open(Oid relationId, LOCKMODE lockmode)
     133                 : {
     134                 :     Relation    r;
     135                 : 
     136        13672125 :     r = relation_open(relationId, lockmode);
     137                 : 
     138        13672119 :     if (r->rd_rel->relkind != RELKIND_INDEX &&
     139            5474 :         r->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
     140              15 :         ereport(ERROR,
     141                 :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     142                 :                  errmsg("\"%s\" is not an index",
     143                 :                         RelationGetRelationName(r))));
     144                 : 
     145        13672104 :     return r;
     146                 : }
     147                 : 
     148                 : /* ----------------
     149                 :  *      index_close - close an index relation
     150                 :  *
     151                 :  *      If lockmode is not "NoLock", we then release the specified lock.
     152                 :  *
     153                 :  *      Note that it is often sensible to hold a lock beyond index_close;
     154                 :  *      in that case, the lock is released automatically at xact end.
     155                 :  * ----------------
     156                 :  */
     157                 : void
     158        13700317 : index_close(Relation relation, LOCKMODE lockmode)
     159                 : {
     160        13700317 :     LockRelId   relid = relation->rd_lockInfo.lockRelId;
     161                 : 
     162        13700317 :     Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
     163                 : 
     164                 :     /* The relcache does the real work... */
     165        13700317 :     RelationClose(relation);
     166                 : 
     167        13700317 :     if (lockmode != NoLock)
     168        12924021 :         UnlockRelationId(&relid, lockmode);
     169        13700317 : }
     170                 : 
     171                 : /* ----------------
     172                 :  *      index_insert - insert an index tuple into a relation
     173                 :  * ----------------
     174                 :  */
     175                 : bool
     176         7828817 : index_insert(Relation indexRelation,
     177                 :              Datum *values,
     178                 :              bool *isnull,
     179                 :              ItemPointer heap_t_ctid,
     180                 :              Relation heapRelation,
     181                 :              IndexUniqueCheck checkUnique,
     182                 :              bool indexUnchanged,
     183                 :              IndexInfo *indexInfo)
     184                 : {
     185         7828817 :     RELATION_CHECKS;
     186         7828817 :     CHECK_REL_PROCEDURE(aminsert);
     187                 : 
     188         7828817 :     if (!(indexRelation->rd_indam->ampredlocks))
     189          264185 :         CheckForSerializableConflictIn(indexRelation,
     190                 :                                        (ItemPointer) NULL,
     191                 :                                        InvalidBlockNumber);
     192                 : 
     193         7828817 :     return indexRelation->rd_indam->aminsert(indexRelation, values, isnull,
     194                 :                                              heap_t_ctid, heapRelation,
     195                 :                                              checkUnique, indexUnchanged,
     196                 :                                              indexInfo);
     197                 : }
     198                 : 
     199                 : /*
     200                 :  * index_beginscan - start a scan of an index with amgettuple
     201                 :  *
     202                 :  * Caller must be holding suitable locks on the heap and the index.
     203                 :  */
     204                 : IndexScanDesc
     205         8896116 : index_beginscan(Relation heapRelation,
     206                 :                 Relation indexRelation,
     207                 :                 Snapshot snapshot,
     208                 :                 int nkeys, int norderbys)
     209                 : {
     210                 :     IndexScanDesc scan;
     211                 : 
     212 GNC     8896116 :     Assert(snapshot != InvalidSnapshot);
     213                 : 
     214 CBC     8896116 :     scan = index_beginscan_internal(indexRelation, nkeys, norderbys, snapshot, NULL, false);
     215                 : 
     216 ECB             :     /*
     217                 :      * Save additional parameters into the scandesc.  Everything else was set
     218                 :      * up by RelationGetIndexScan.
     219                 :      */
     220 GIC     8896116 :     scan->heapRelation = heapRelation;
     221         8896116 :     scan->xs_snapshot = snapshot;
     222 ECB             : 
     223                 :     /* prepare to fetch index matches from table */
     224 GIC     8896116 :     scan->xs_heapfetch = table_index_fetch_begin(heapRelation);
     225                 : 
     226 CBC     8896116 :     return scan;
     227                 : }
     228 ECB             : 
     229                 : /*
     230                 :  * index_beginscan_bitmap - start a scan of an index with amgetbitmap
     231                 :  *
     232                 :  * As above, caller had better be holding some lock on the parent heap
     233                 :  * relation, even though it's not explicitly mentioned here.
     234                 :  */
     235                 : IndexScanDesc
     236 GIC        9068 : index_beginscan_bitmap(Relation indexRelation,
     237                 :                        Snapshot snapshot,
     238 ECB             :                        int nkeys)
     239                 : {
     240                 :     IndexScanDesc scan;
     241                 : 
     242 GNC        9068 :     Assert(snapshot != InvalidSnapshot);
     243                 : 
     244 GIC        9068 :     scan = index_beginscan_internal(indexRelation, nkeys, 0, snapshot, NULL, false);
     245                 : 
     246 ECB             :     /*
     247                 :      * Save additional parameters into the scandesc.  Everything else was set
     248                 :      * up by RelationGetIndexScan.
     249                 :      */
     250 GIC        9068 :     scan->xs_snapshot = snapshot;
     251                 : 
     252            9068 :     return scan;
     253                 : }
     254 ECB             : 
     255                 : /*
     256                 :  * index_beginscan_internal --- common code for index_beginscan variants
     257                 :  */
     258                 : static IndexScanDesc
     259 GIC     8905358 : index_beginscan_internal(Relation indexRelation,
     260                 :                          int nkeys, int norderbys, Snapshot snapshot,
     261                 :                          ParallelIndexScanDesc pscan, bool temp_snap)
     262                 : {
     263 ECB             :     IndexScanDesc scan;
     264                 : 
     265 GIC     8905358 :     RELATION_CHECKS;
     266         8905358 :     CHECK_REL_PROCEDURE(ambeginscan);
     267                 : 
     268         8905358 :     if (!(indexRelation->rd_indam->ampredlocks))
     269 CBC        2129 :         PredicateLockRelation(indexRelation, snapshot);
     270 ECB             : 
     271                 :     /*
     272                 :      * We hold a reference count to the relcache entry throughout the scan.
     273                 :      */
     274 GIC     8905358 :     RelationIncrementReferenceCount(indexRelation);
     275                 : 
     276                 :     /*
     277                 :      * Tell the AM to open a scan.
     278 ECB             :      */
     279 GIC     8905358 :     scan = indexRelation->rd_indam->ambeginscan(indexRelation, nkeys,
     280                 :                                                 norderbys);
     281                 :     /* Initialize information for parallel scan. */
     282         8905358 :     scan->parallel_scan = pscan;
     283 CBC     8905358 :     scan->xs_temp_snap = temp_snap;
     284                 : 
     285 GIC     8905358 :     return scan;
     286 ECB             : }
     287                 : 
     288                 : /* ----------------
     289                 :  *      index_rescan  - (re)start a scan of an index
     290                 :  *
     291                 :  * During a restart, the caller may specify a new set of scankeys and/or
     292                 :  * orderbykeys; but the number of keys cannot differ from what index_beginscan
     293                 :  * was told.  (Later we might relax that to "must not exceed", but currently
     294                 :  * the index AMs tend to assume that scan->numberOfKeys is what to believe.)
     295                 :  * To restart the scan without changing keys, pass NULL for the key arrays.
     296                 :  * (Of course, keys *must* be passed on the first call, unless
     297                 :  * scan->numberOfKeys is zero.)
     298                 :  * ----------------
     299                 :  */
     300                 : void
     301 GIC     9091638 : index_rescan(IndexScanDesc scan,
     302                 :              ScanKey keys, int nkeys,
     303                 :              ScanKey orderbys, int norderbys)
     304                 : {
     305 CBC     9091638 :     SCAN_CHECKS;
     306 GIC     9091638 :     CHECK_SCAN_PROCEDURE(amrescan);
     307                 : 
     308         9091638 :     Assert(nkeys == scan->numberOfKeys);
     309 CBC     9091638 :     Assert(norderbys == scan->numberOfOrderBys);
     310 ECB             : 
     311                 :     /* Release resources (like buffer pins) from table accesses */
     312 CBC     9091638 :     if (scan->xs_heapfetch)
     313         9081202 :         table_index_fetch_reset(scan->xs_heapfetch);
     314                 : 
     315 GIC     9091638 :     scan->kill_prior_tuple = false; /* for safety */
     316 CBC     9091638 :     scan->xs_heap_continue = false;
     317 ECB             : 
     318 GIC     9091638 :     scan->indexRelation->rd_indam->amrescan(scan, keys, nkeys,
     319 ECB             :                                             orderbys, norderbys);
     320 CBC     9091638 : }
     321                 : 
     322 ECB             : /* ----------------
     323                 :  *      index_endscan - end a scan
     324                 :  * ----------------
     325                 :  */
     326                 : void
     327 GIC     8904646 : index_endscan(IndexScanDesc scan)
     328                 : {
     329         8904646 :     SCAN_CHECKS;
     330         8904646 :     CHECK_SCAN_PROCEDURE(amendscan);
     331 ECB             : 
     332                 :     /* Release resources (like buffer pins) from table accesses */
     333 CBC     8904646 :     if (scan->xs_heapfetch)
     334 ECB             :     {
     335 GIC     8895609 :         table_index_fetch_end(scan->xs_heapfetch);
     336         8895609 :         scan->xs_heapfetch = NULL;
     337 ECB             :     }
     338                 : 
     339                 :     /* End the AM's scan */
     340 CBC     8904646 :     scan->indexRelation->rd_indam->amendscan(scan);
     341                 : 
     342                 :     /* Release index refcount acquired by index_beginscan */
     343 GIC     8904646 :     RelationDecrementReferenceCount(scan->indexRelation);
     344 ECB             : 
     345 GIC     8904646 :     if (scan->xs_temp_snap)
     346             174 :         UnregisterSnapshot(scan->xs_snapshot);
     347 ECB             : 
     348                 :     /* Release the scan data structure itself */
     349 CBC     8904646 :     IndexScanEnd(scan);
     350         8904646 : }
     351                 : 
     352                 : /* ----------------
     353 ECB             :  *      index_markpos  - mark a scan position
     354                 :  * ----------------
     355                 :  */
     356                 : void
     357 GIC       65029 : index_markpos(IndexScanDesc scan)
     358                 : {
     359           65029 :     SCAN_CHECKS;
     360           65029 :     CHECK_SCAN_PROCEDURE(ammarkpos);
     361 ECB             : 
     362 GIC       65029 :     scan->indexRelation->rd_indam->ammarkpos(scan);
     363 CBC       65029 : }
     364 ECB             : 
     365                 : /* ----------------
     366                 :  *      index_restrpos  - restore a scan position
     367                 :  *
     368                 :  * NOTE: this only restores the internal scan state of the index AM.  See
     369                 :  * comments for ExecRestrPos().
     370                 :  *
     371                 :  * NOTE: For heap, in the presence of HOT chains, mark/restore only works
     372                 :  * correctly if the scan's snapshot is MVCC-safe; that ensures that there's at
     373                 :  * most one returnable tuple in each HOT chain, and so restoring the prior
     374                 :  * state at the granularity of the index AM is sufficient.  Since the only
     375                 :  * current user of mark/restore functionality is nodeMergejoin.c, this
     376                 :  * effectively means that merge-join plans only work for MVCC snapshots.  This
     377                 :  * could be fixed if necessary, but for now it seems unimportant.
     378                 :  * ----------------
     379                 :  */
     380                 : void
     381 GIC       27015 : index_restrpos(IndexScanDesc scan)
     382                 : {
     383           27015 :     Assert(IsMVCCSnapshot(scan->xs_snapshot));
     384                 : 
     385 CBC       27015 :     SCAN_CHECKS;
     386 GIC       27015 :     CHECK_SCAN_PROCEDURE(amrestrpos);
     387 ECB             : 
     388                 :     /* release resources (like buffer pins) from table accesses */
     389 CBC       27015 :     if (scan->xs_heapfetch)
     390           27015 :         table_index_fetch_reset(scan->xs_heapfetch);
     391                 : 
     392 GIC       27015 :     scan->kill_prior_tuple = false; /* for safety */
     393 CBC       27015 :     scan->xs_heap_continue = false;
     394 ECB             : 
     395 GIC       27015 :     scan->indexRelation->rd_indam->amrestrpos(scan);
     396 CBC       27015 : }
     397 ECB             : 
     398                 : /*
     399                 :  * index_parallelscan_estimate - estimate shared memory for parallel scan
     400                 :  *
     401                 :  * Currently, we don't pass any information to the AM-specific estimator,
     402                 :  * so it can probably only return a constant.  In the future, we might need
     403                 :  * to pass more information.
     404                 :  */
     405                 : Size
     406 GIC          26 : index_parallelscan_estimate(Relation indexRelation, Snapshot snapshot)
     407                 : {
     408                 :     Size        nbytes;
     409                 : 
     410 GNC          26 :     Assert(snapshot != InvalidSnapshot);
     411                 : 
     412 CBC          26 :     RELATION_CHECKS;
     413                 : 
     414 GIC          26 :     nbytes = offsetof(ParallelIndexScanDescData, ps_snapshot_data);
     415              26 :     nbytes = add_size(nbytes, EstimateSnapshotSpace(snapshot));
     416 CBC          26 :     nbytes = MAXALIGN(nbytes);
     417                 : 
     418 ECB             :     /*
     419                 :      * If amestimateparallelscan is not provided, assume there is no
     420                 :      * AM-specific data needed.  (It's hard to believe that could work, but
     421                 :      * it's easy enough to cater to it here.)
     422                 :      */
     423 GIC          26 :     if (indexRelation->rd_indam->amestimateparallelscan != NULL)
     424              26 :         nbytes = add_size(nbytes,
     425              26 :                           indexRelation->rd_indam->amestimateparallelscan());
     426                 : 
     427              26 :     return nbytes;
     428                 : }
     429 ECB             : 
     430                 : /*
     431                 :  * index_parallelscan_initialize - initialize parallel scan
     432                 :  *
     433                 :  * We initialize both the ParallelIndexScanDesc proper and the AM-specific
     434                 :  * information which follows it.
     435                 :  *
     436                 :  * This function calls access method specific initialization routine to
     437                 :  * initialize am specific information.  Call this just once in the leader
     438                 :  * process; then, individual workers attach via index_beginscan_parallel.
     439                 :  */
     440                 : void
     441 GIC          26 : index_parallelscan_initialize(Relation heapRelation, Relation indexRelation,
     442                 :                               Snapshot snapshot, ParallelIndexScanDesc target)
     443                 : {
     444                 :     Size        offset;
     445                 : 
     446 GNC          26 :     Assert(snapshot != InvalidSnapshot);
     447                 : 
     448 GIC          26 :     RELATION_CHECKS;
     449 ECB             : 
     450 GIC          26 :     offset = add_size(offsetof(ParallelIndexScanDescData, ps_snapshot_data),
     451                 :                       EstimateSnapshotSpace(snapshot));
     452              26 :     offset = MAXALIGN(offset);
     453                 : 
     454 CBC          26 :     target->ps_relid = RelationGetRelid(heapRelation);
     455 GIC          26 :     target->ps_indexid = RelationGetRelid(indexRelation);
     456 CBC          26 :     target->ps_offset = offset;
     457 GIC          26 :     SerializeSnapshot(snapshot, target->ps_snapshot_data);
     458 ECB             : 
     459                 :     /* aminitparallelscan is optional; assume no-op if not provided by AM */
     460 CBC          26 :     if (indexRelation->rd_indam->aminitparallelscan != NULL)
     461                 :     {
     462 ECB             :         void       *amtarget;
     463                 : 
     464 CBC          26 :         amtarget = OffsetToPointer(target, offset);
     465              26 :         indexRelation->rd_indam->aminitparallelscan(amtarget);
     466                 :     }
     467 GIC          26 : }
     468 ECB             : 
     469                 : /* ----------------
     470                 :  *      index_parallelrescan  - (re)start a parallel scan of an index
     471                 :  * ----------------
     472                 :  */
     473                 : void
     474 GIC          12 : index_parallelrescan(IndexScanDesc scan)
     475 ECB             : {
     476 GIC          12 :     SCAN_CHECKS;
     477                 : 
     478              12 :     if (scan->xs_heapfetch)
     479              12 :         table_index_fetch_reset(scan->xs_heapfetch);
     480                 : 
     481                 :     /* amparallelrescan is optional; assume no-op if not provided by AM */
     482 CBC          12 :     if (scan->indexRelation->rd_indam->amparallelrescan != NULL)
     483 GIC          12 :         scan->indexRelation->rd_indam->amparallelrescan(scan);
     484 CBC          12 : }
     485                 : 
     486 ECB             : /*
     487                 :  * index_beginscan_parallel - join parallel index scan
     488                 :  *
     489                 :  * Caller must be holding suitable locks on the heap and the index.
     490                 :  */
     491                 : IndexScanDesc
     492 CBC         174 : index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys,
     493                 :                          int norderbys, ParallelIndexScanDesc pscan)
     494                 : {
     495                 :     Snapshot    snapshot;
     496                 :     IndexScanDesc scan;
     497                 : 
     498 GIC         174 :     Assert(RelationGetRelid(heaprel) == pscan->ps_relid);
     499             174 :     snapshot = RestoreSnapshot(pscan->ps_snapshot_data);
     500 CBC         174 :     RegisterSnapshot(snapshot);
     501 GIC         174 :     scan = index_beginscan_internal(indexrel, nkeys, norderbys, snapshot,
     502                 :                                     pscan, true);
     503                 : 
     504                 :     /*
     505                 :      * Save additional parameters into the scandesc.  Everything else was set
     506 ECB             :      * up by index_beginscan_internal.
     507                 :      */
     508 CBC         174 :     scan->heapRelation = heaprel;
     509             174 :     scan->xs_snapshot = snapshot;
     510                 : 
     511                 :     /* prepare to fetch index matches from table */
     512 GIC         174 :     scan->xs_heapfetch = table_index_fetch_begin(heaprel);
     513                 : 
     514             174 :     return scan;
     515                 : }
     516 ECB             : 
     517                 : /* ----------------
     518                 :  * index_getnext_tid - get the next TID from a scan
     519                 :  *
     520                 :  * The result is the next TID satisfying the scan keys,
     521                 :  * or NULL if no more matching tuples exist.
     522                 :  * ----------------
     523                 :  */
     524                 : ItemPointer
     525 GIC    21962633 : index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
     526                 : {
     527                 :     bool        found;
     528                 : 
     529        21962633 :     SCAN_CHECKS;
     530        21962633 :     CHECK_SCAN_PROCEDURE(amgettuple);
     531                 : 
     532                 :     /* XXX: we should assert that a snapshot is pushed or registered */
     533 CBC    21962633 :     Assert(TransactionIdIsValid(RecentXmin));
     534                 : 
     535                 :     /*
     536                 :      * The AM's amgettuple proc finds the next index entry matching the scan
     537 ECB             :      * keys, and puts the TID into scan->xs_heaptid.  It should also set
     538                 :      * scan->xs_recheck and possibly scan->xs_itup/scan->xs_hitup, though we
     539                 :      * pay no attention to those fields here.
     540                 :      */
     541 CBC    21962633 :     found = scan->indexRelation->rd_indam->amgettuple(scan, direction);
     542                 : 
     543                 :     /* Reset kill flag immediately for safety */
     544 GIC    21962632 :     scan->kill_prior_tuple = false;
     545        21962632 :     scan->xs_heap_continue = false;
     546                 : 
     547                 :     /* If we're out of index entries, we're done */
     548        21962632 :     if (!found)
     549 ECB             :     {
     550                 :         /* release resources (like buffer pins) from table accesses */
     551 GIC     5221147 :         if (scan->xs_heapfetch)
     552 CBC     5221147 :             table_index_fetch_reset(scan->xs_heapfetch);
     553 ECB             : 
     554 GIC     5221147 :         return NULL;
     555                 :     }
     556 CBC    16741485 :     Assert(ItemPointerIsValid(&scan->xs_heaptid));
     557                 : 
     558 GIC    16741485 :     pgstat_count_index_tuples(scan->indexRelation, 1);
     559 ECB             : 
     560                 :     /* Return the TID of the tuple we found. */
     561 GIC    16741485 :     return &scan->xs_heaptid;
     562 ECB             : }
     563                 : 
     564                 : /* ----------------
     565                 :  *      index_fetch_heap - get the scan's next heap tuple
     566                 :  *
     567                 :  * The result is a visible heap tuple associated with the index TID most
     568                 :  * recently fetched by index_getnext_tid, or NULL if no more matching tuples
     569                 :  * exist.  (There can be more than one matching tuple because of HOT chains,
     570                 :  * although when using an MVCC snapshot it should be impossible for more than
     571                 :  * one such tuple to exist.)
     572                 :  *
     573                 :  * On success, the buffer containing the heap tup is pinned (the pin will be
     574                 :  * dropped in a future index_getnext_tid, index_fetch_heap or index_endscan
     575                 :  * call).
     576                 :  *
     577                 :  * Note: caller must check scan->xs_recheck, and perform rechecking of the
     578                 :  * scan keys if required.  We do not do that here because we don't have
     579                 :  * enough information to do it efficiently in the general case.
     580                 :  * ----------------
     581                 :  */
     582                 : bool
     583 GIC    15331487 : index_fetch_heap(IndexScanDesc scan, TupleTableSlot *slot)
     584                 : {
     585        15331487 :     bool        all_dead = false;
     586                 :     bool        found;
     587                 : 
     588        15331487 :     found = table_index_fetch_tuple(scan->xs_heapfetch, &scan->xs_heaptid,
     589                 :                                     scan->xs_snapshot, slot,
     590                 :                                     &scan->xs_heap_continue, &all_dead);
     591 ECB             : 
     592 GIC    15331482 :     if (found)
     593 CBC    14679918 :         pgstat_count_heap_fetch(scan->indexRelation);
     594                 : 
     595                 :     /*
     596 ECB             :      * If we scanned a whole HOT chain and found only dead tuples, tell index
     597                 :      * AM to kill its entry for that TID (this will take effect in the next
     598                 :      * amgettuple call, in index_getnext_tid).  We do not do this when in
     599                 :      * recovery because it may violate MVCC to do so.  See comments in
     600                 :      * RelationGetIndexScan().
     601                 :      */
     602 GIC    15331482 :     if (!scan->xactStartedInRecovery)
     603        15200175 :         scan->kill_prior_tuple = all_dead;
     604                 : 
     605        15331482 :     return found;
     606                 : }
     607                 : 
     608                 : /* ----------------
     609                 :  *      index_getnext_slot - get the next tuple from a scan
     610 ECB             :  *
     611                 :  * The result is true if a tuple satisfying the scan keys and the snapshot was
     612                 :  * found, false otherwise.  The tuple is stored in the specified slot.
     613                 :  *
     614                 :  * On success, resources (like buffer pins) are likely to be held, and will be
     615                 :  * dropped by a future index_getnext_tid, index_fetch_heap or index_endscan
     616                 :  * call).
     617                 :  *
     618                 :  * Note: caller must check scan->xs_recheck, and perform rechecking of the
     619                 :  * scan keys if required.  We do not do that here because we don't have
     620                 :  * enough information to do it efficiently in the general case.
     621                 :  * ----------------
     622                 :  */
     623                 : bool
     624 GIC    18934292 : index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
     625                 : {
     626                 :     for (;;)
     627                 :     {
     628        19499099 :         if (!scan->xs_heap_continue)
     629                 :         {
     630                 :             ItemPointer tid;
     631                 : 
     632 ECB             :             /* Time to fetch the next TID from the index */
     633 GIC    19325307 :             tid = index_getnext_tid(scan, direction);
     634                 : 
     635                 :             /* If we're out of index entries, we're done */
     636 CBC    19325306 :             if (tid == NULL)
     637 GIC     5181865 :                 break;
     638                 : 
     639        14143441 :             Assert(ItemPointerEquals(tid, &scan->xs_heaptid));
     640                 :         }
     641 ECB             : 
     642                 :         /*
     643                 :          * Fetch the next (or only) visible heap tuple for this index entry.
     644                 :          * If we don't find anything, loop around and grab the next TID from
     645                 :          * the index.
     646                 :          */
     647 CBC    14317233 :         Assert(ItemPointerIsValid(&scan->xs_heaptid));
     648 GIC    14317233 :         if (index_fetch_heap(scan, slot))
     649        13752421 :             return true;
     650                 :     }
     651                 : 
     652         5181865 :     return false;
     653                 : }
     654                 : 
     655 ECB             : /* ----------------
     656                 :  *      index_getbitmap - get all tuples at once from an index scan
     657                 :  *
     658                 :  * Adds the TIDs of all heap tuples satisfying the scan keys to a bitmap.
     659                 :  * Since there's no interlock between the index scan and the eventual heap
     660                 :  * access, this is only safe to use with MVCC-based snapshots: the heap
     661                 :  * item slot could have been replaced by a newer tuple by the time we get
     662                 :  * to it.
     663                 :  *
     664                 :  * Returns the number of matching tuples found.  (Note: this might be only
     665                 :  * approximate, so it should only be used for statistical purposes.)
     666                 :  * ----------------
     667                 :  */
     668                 : int64
     669 GIC        8697 : index_getbitmap(IndexScanDesc scan, TIDBitmap *bitmap)
     670                 : {
     671                 :     int64       ntids;
     672                 : 
     673            8697 :     SCAN_CHECKS;
     674            8697 :     CHECK_SCAN_PROCEDURE(amgetbitmap);
     675                 : 
     676                 :     /* just make sure this is false... */
     677 CBC        8697 :     scan->kill_prior_tuple = false;
     678                 : 
     679                 :     /*
     680                 :      * have the am's getbitmap proc do all the work.
     681 ECB             :      */
     682 CBC        8697 :     ntids = scan->indexRelation->rd_indam->amgetbitmap(scan, bitmap);
     683                 : 
     684 GIC        8697 :     pgstat_count_index_tuples(scan->indexRelation, ntids);
     685 ECB             : 
     686 GIC        8697 :     return ntids;
     687                 : }
     688                 : 
     689                 : /* ----------------
     690 ECB             :  *      index_bulk_delete - do mass deletion of index entries
     691                 :  *
     692                 :  *      callback routine tells whether a given main-heap tuple is
     693                 :  *      to be deleted
     694                 :  *
     695                 :  *      return value is an optional palloc'd struct of statistics
     696                 :  * ----------------
     697                 :  */
     698                 : IndexBulkDeleteResult *
     699 GIC        4075 : index_bulk_delete(IndexVacuumInfo *info,
     700                 :                   IndexBulkDeleteResult *istat,
     701                 :                   IndexBulkDeleteCallback callback,
     702                 :                   void *callback_state)
     703                 : {
     704            4075 :     Relation    indexRelation = info->index;
     705                 : 
     706            4075 :     RELATION_CHECKS;
     707 CBC        4075 :     CHECK_REL_PROCEDURE(ambulkdelete);
     708                 : 
     709 GIC        4075 :     return indexRelation->rd_indam->ambulkdelete(info, istat,
     710                 :                                                  callback, callback_state);
     711                 : }
     712 ECB             : 
     713                 : /* ----------------
     714                 :  *      index_vacuum_cleanup - do post-deletion cleanup of an index
     715                 :  *
     716                 :  *      return value is an optional palloc'd struct of statistics
     717                 :  * ----------------
     718                 :  */
     719                 : IndexBulkDeleteResult *
     720 GIC       94662 : index_vacuum_cleanup(IndexVacuumInfo *info,
     721                 :                      IndexBulkDeleteResult *istat)
     722                 : {
     723           94662 :     Relation    indexRelation = info->index;
     724                 : 
     725           94662 :     RELATION_CHECKS;
     726           94662 :     CHECK_REL_PROCEDURE(amvacuumcleanup);
     727                 : 
     728 CBC       94662 :     return indexRelation->rd_indam->amvacuumcleanup(info, istat);
     729                 : }
     730                 : 
     731 ECB             : /* ----------------
     732                 :  *      index_can_return
     733                 :  *
     734                 :  *      Does the index access method support index-only scans for the given
     735                 :  *      column?
     736                 :  * ----------------
     737                 :  */
     738                 : bool
     739 GIC      537461 : index_can_return(Relation indexRelation, int attno)
     740                 : {
     741          537461 :     RELATION_CHECKS;
     742                 : 
     743                 :     /* amcanreturn is optional; assume false if not provided by AM */
     744          537461 :     if (indexRelation->rd_indam->amcanreturn == NULL)
     745          136077 :         return false;
     746                 : 
     747 CBC      401384 :     return indexRelation->rd_indam->amcanreturn(indexRelation, attno);
     748                 : }
     749 ECB             : 
     750                 : /* ----------------
     751                 :  *      index_getprocid
     752                 :  *
     753                 :  *      Index access methods typically require support routines that are
     754                 :  *      not directly the implementation of any WHERE-clause query operator
     755                 :  *      and so cannot be kept in pg_amop.  Instead, such routines are kept
     756                 :  *      in pg_amproc.  These registered procedure OIDs are assigned numbers
     757                 :  *      according to a convention established by the access method.
     758                 :  *      The general index code doesn't know anything about the routines
     759                 :  *      involved; it just builds an ordered list of them for
     760                 :  *      each attribute on which an index is defined.
     761                 :  *
     762                 :  *      As of Postgres 8.3, support routines within an operator family
     763                 :  *      are further subdivided by the "left type" and "right type" of the
     764                 :  *      query operator(s) that they support.  The "default" functions for a
     765                 :  *      particular indexed attribute are those with both types equal to
     766                 :  *      the index opclass' opcintype (note that this is subtly different
     767                 :  *      from the indexed attribute's own type: it may be a binary-compatible
     768                 :  *      type instead).  Only the default functions are stored in relcache
     769                 :  *      entries --- access methods can use the syscache to look up non-default
     770                 :  *      functions.
     771                 :  *
     772                 :  *      This routine returns the requested default procedure OID for a
     773                 :  *      particular indexed attribute.
     774                 :  * ----------------
     775                 :  */
     776                 : RegProcedure
     777 GIC     9861858 : index_getprocid(Relation irel,
     778                 :                 AttrNumber attnum,
     779                 :                 uint16 procnum)
     780                 : {
     781                 :     RegProcedure *loc;
     782                 :     int         nproc;
     783                 :     int         procindex;
     784                 : 
     785 CBC     9861858 :     nproc = irel->rd_indam->amsupport;
     786                 : 
     787 GIC     9861858 :     Assert(procnum > 0 && procnum <= (uint16) nproc);
     788                 : 
     789         9861858 :     procindex = (nproc * (attnum - 1)) + (procnum - 1);
     790                 : 
     791         9861858 :     loc = irel->rd_support;
     792                 : 
     793 CBC     9861858 :     Assert(loc != NULL);
     794                 : 
     795         9861858 :     return loc[procindex];
     796                 : }
     797 ECB             : 
     798                 : /* ----------------
     799                 :  *      index_getprocinfo
     800                 :  *
     801                 :  *      This routine allows index AMs to keep fmgr lookup info for
     802                 :  *      support procs in the relcache.  As above, only the "default"
     803                 :  *      functions for any particular indexed attribute are cached.
     804                 :  *
     805                 :  * Note: the return value points into cached data that will be lost during
     806                 :  * any relcache rebuild!  Therefore, either use the callinfo right away,
     807                 :  * or save it only after having acquired some type of lock on the index rel.
     808                 :  * ----------------
     809                 :  */
     810                 : FmgrInfo *
     811 GIC    35338164 : index_getprocinfo(Relation irel,
     812                 :                   AttrNumber attnum,
     813                 :                   uint16 procnum)
     814                 : {
     815                 :     FmgrInfo   *locinfo;
     816                 :     int         nproc;
     817                 :     int         optsproc;
     818                 :     int         procindex;
     819 ECB             : 
     820 GIC    35338164 :     nproc = irel->rd_indam->amsupport;
     821        35338164 :     optsproc = irel->rd_indam->amoptsprocnum;
     822                 : 
     823        35338164 :     Assert(procnum > 0 && procnum <= (uint16) nproc);
     824                 : 
     825        35338164 :     procindex = (nproc * (attnum - 1)) + (procnum - 1);
     826                 : 
     827        35338164 :     locinfo = irel->rd_supportinfo;
     828 ECB             : 
     829 CBC    35338164 :     Assert(locinfo != NULL);
     830                 : 
     831        35338164 :     locinfo += procindex;
     832                 : 
     833 ECB             :     /* Initialize the lookup info if first time through */
     834 GIC    35338164 :     if (locinfo->fn_oid == InvalidOid)
     835 ECB             :     {
     836 GIC      512595 :         RegProcedure *loc = irel->rd_support;
     837 ECB             :         RegProcedure procId;
     838                 : 
     839 CBC      512595 :         Assert(loc != NULL);
     840                 : 
     841 GIC      512595 :         procId = loc[procindex];
     842 ECB             : 
     843                 :         /*
     844                 :          * Complain if function was not found during IndexSupportInitialize.
     845                 :          * This should not happen unless the system tables contain bogus
     846                 :          * entries for the index opclass.  (If an AM wants to allow a support
     847                 :          * function to be optional, it can use index_getprocid.)
     848                 :          */
     849 CBC      512595 :         if (!RegProcedureIsValid(procId))
     850 UIC           0 :             elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
     851                 :                  procnum, attnum, RelationGetRelationName(irel));
     852                 : 
     853 GIC      512595 :         fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);
     854                 : 
     855          512595 :         if (procnum != optsproc)
     856                 :         {
     857 ECB             :             /* Initialize locinfo->fn_expr with opclass options Const */
     858 GBC      511840 :             bytea     **attoptions = RelationGetIndexAttOptions(irel, false);
     859 GIC      511840 :             MemoryContext oldcxt = MemoryContextSwitchTo(irel->rd_indexcxt);
     860                 : 
     861 CBC      511840 :             set_fn_opclass_options(locinfo, attoptions[attnum - 1]);
     862                 : 
     863          511840 :             MemoryContextSwitchTo(oldcxt);
     864                 :         }
     865                 :     }
     866 ECB             : 
     867 CBC    35338164 :     return locinfo;
     868                 : }
     869 ECB             : 
     870                 : /* ----------------
     871                 :  *      index_store_float8_orderby_distances
     872                 :  *
     873                 :  *      Convert AM distance function's results (that can be inexact)
     874                 :  *      to ORDER BY types and save them into xs_orderbyvals/xs_orderbynulls
     875                 :  *      for a possible recheck.
     876                 :  * ----------------
     877                 :  */
     878                 : void
     879 GIC      182300 : index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes,
     880                 :                                      IndexOrderByDistance *distances,
     881                 :                                      bool recheckOrderBy)
     882                 : {
     883                 :     int         i;
     884                 : 
     885          182300 :     Assert(distances || !recheckOrderBy);
     886                 : 
     887 CBC      182300 :     scan->xs_recheckorderby = recheckOrderBy;
     888                 : 
     889 GIC      364609 :     for (i = 0; i < scan->numberOfOrderBys; i++)
     890                 :     {
     891          182309 :         if (orderByTypes[i] == FLOAT8OID)
     892                 :         {
     893 ECB             : #ifndef USE_FLOAT8_BYVAL
     894                 :             /* must free any old value to avoid memory leakage */
     895                 :             if (!scan->xs_orderbynulls[i])
     896                 :                 pfree(DatumGetPointer(scan->xs_orderbyvals[i]));
     897                 : #endif
     898 GIC      182244 :             if (distances && !distances[i].isnull)
     899 ECB             :             {
     900 GIC      182214 :                 scan->xs_orderbyvals[i] = Float8GetDatum(distances[i].value);
     901          182214 :                 scan->xs_orderbynulls[i] = false;
     902                 :             }
     903                 :             else
     904                 :             {
     905              30 :                 scan->xs_orderbyvals[i] = (Datum) 0;
     906 CBC          30 :                 scan->xs_orderbynulls[i] = true;
     907                 :             }
     908 ECB             :         }
     909 CBC          65 :         else if (orderByTypes[i] == FLOAT4OID)
     910                 :         {
     911                 :             /* convert distance function's result to ORDER BY type */
     912 GIC          35 :             if (distances && !distances[i].isnull)
     913 ECB             :             {
     914 CBC          35 :                 scan->xs_orderbyvals[i] = Float4GetDatum((float4) distances[i].value);
     915 GIC          35 :                 scan->xs_orderbynulls[i] = false;
     916                 :             }
     917 ECB             :             else
     918                 :             {
     919 UIC           0 :                 scan->xs_orderbyvals[i] = (Datum) 0;
     920 LBC           0 :                 scan->xs_orderbynulls[i] = true;
     921                 :             }
     922 ECB             :         }
     923                 :         else
     924                 :         {
     925                 :             /*
     926                 :              * If the ordering operator's return value is anything else, we
     927 EUB             :              * don't know how to convert the float8 bound calculated by the
     928                 :              * distance function to that.  The executor won't actually need
     929                 :              * the order by values we return here, if there are no lossy
     930                 :              * results, so only insist on converting if the *recheck flag is
     931                 :              * set.
     932                 :              */
     933 GIC          30 :             if (scan->xs_recheckorderby)
     934 UIC           0 :                 elog(ERROR, "ORDER BY operator must return float8 or float4 if the distance function is lossy");
     935 GIC          30 :             scan->xs_orderbynulls[i] = true;
     936                 :         }
     937                 :     }
     938          182300 : }
     939                 : 
     940                 : /* ----------------
     941 ECB             :  *      index_opclass_options
     942 EUB             :  *
     943 ECB             :  *      Parse opclass-specific options for index column.
     944                 :  * ----------------
     945                 :  */
     946                 : bytea *
     947 GIC      397949 : index_opclass_options(Relation indrel, AttrNumber attnum, Datum attoptions,
     948                 :                       bool validate)
     949                 : {
     950          397949 :     int         amoptsprocnum = indrel->rd_indam->amoptsprocnum;
     951          397949 :     Oid         procid = InvalidOid;
     952                 :     FmgrInfo   *procinfo;
     953                 :     local_relopts relopts;
     954                 : 
     955 ECB             :     /* fetch options support procedure if specified */
     956 GIC      397949 :     if (amoptsprocnum != 0)
     957          397921 :         procid = index_getprocid(indrel, attnum, amoptsprocnum);
     958 ECB             : 
     959 CBC      397949 :     if (!OidIsValid(procid))
     960                 :     {
     961                 :         Oid         opclass;
     962                 :         Datum       indclassDatum;
     963                 :         oidvector  *indclass;
     964 ECB             : 
     965 GIC      397116 :         if (!DatumGetPointer(attoptions))
     966 CBC      397113 :             return NULL;        /* ok, no options, no procedure */
     967                 : 
     968                 :         /*
     969                 :          * Report an error if the opclass's options-parsing procedure does not
     970                 :          * exist but the opclass options are specified.
     971                 :          */
     972 GNC           3 :         indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, indrel->rd_indextuple,
     973                 :                                                Anum_pg_index_indclass);
     974 GIC           3 :         indclass = (oidvector *) DatumGetPointer(indclassDatum);
     975               3 :         opclass = indclass->values[attnum - 1];
     976                 : 
     977               3 :         ereport(ERROR,
     978 ECB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     979                 :                  errmsg("operator class %s has no options",
     980                 :                         generate_opclass_name(opclass))));
     981                 :     }
     982                 : 
     983 CBC         833 :     init_local_reloptions(&relopts, 0);
     984                 : 
     985 GIC         833 :     procinfo = index_getprocinfo(indrel, attnum, amoptsprocnum);
     986                 : 
     987             833 :     (void) FunctionCall1(procinfo, PointerGetDatum(&relopts));
     988                 : 
     989 CBC         833 :     return build_local_reloptions(&relopts, attoptions, validate);
     990                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a