LCOV - differential code coverage report
Current view: top level - src/backend/access/gist - gistscan.c (source / functions) Coverage Total Hit UBC GBC GNC CBC DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 95.6 % 113 108 5 1 4 103 3
Current Date: 2026-05-05 10:23:31 +0900 Functions: 100.0 % 4 4 2 2
Baseline: lcov-20260505-025707-baseline Branches: 87.1 % 70 61 9 1 60
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 4 4 4
(360..) days: 95.4 % 109 104 5 1 103
Function coverage date bins:
(360..) days: 100.0 % 4 4 2 2
Branch coverage date bins:
(360..) days: 87.1 % 70 61 9 1 60

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * gistscan.c
                                  4                 :                :  *    routines to manage scans on GiST index relations
                                  5                 :                :  *
                                  6                 :                :  *
                                  7                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  8                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/access/gist/gistscan.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "access/gist_private.h"
                                 18                 :                : #include "access/gistscan.h"
                                 19                 :                : #include "access/relscan.h"
                                 20                 :                : #include "utils/float.h"
                                 21                 :                : #include "utils/lsyscache.h"
                                 22                 :                : #include "utils/memutils.h"
                                 23                 :                : #include "utils/rel.h"
                                 24                 :                : 
                                 25                 :                : 
                                 26                 :                : /*
                                 27                 :                :  * Pairing heap comparison function for the GISTSearchItem queue
                                 28                 :                :  */
                                 29                 :                : static int
 4152 heikki.linnakangas@i       30                 :CBC      198558 : pairingheap_GISTSearchItem_cmp(const pairingheap_node *a, const pairingheap_node *b, void *arg)
                                 31                 :                : {
                                 32                 :         198558 :     const GISTSearchItem *sa = (const GISTSearchItem *) a;
                                 33                 :         198558 :     const GISTSearchItem *sb = (const GISTSearchItem *) b;
 5632 tgl@sss.pgh.pa.us          34                 :         198558 :     IndexScanDesc scan = (IndexScanDesc) arg;
                                 35                 :                :     int         i;
                                 36                 :                : 
                                 37                 :                :     /* Order according to distance comparison */
                                 38         [ +  + ]:         200444 :     for (i = 0; i < scan->numberOfOrderBys; i++)
                                 39                 :                :     {
 2420 akorotkov@postgresql       40         [ +  + ]:          37523 :         if (sa->distances[i].isnull)
                                 41                 :                :         {
                                 42         [ +  - ]:             68 :             if (!sb->distances[i].isnull)
 2431                            43                 :             68 :                 return -1;
                                 44                 :                :         }
 2420                            45         [ +  + ]:          37455 :         else if (sb->distances[i].isnull)
                                 46                 :                :         {
 2431                            47                 :             16 :             return 1;
                                 48                 :                :         }
                                 49                 :                :         else
                                 50                 :                :         {
 2420                            51                 :          74878 :             int         cmp = -float8_cmp_internal(sa->distances[i].value,
                                 52                 :          37439 :                                                    sb->distances[i].value);
                                 53                 :                : 
 2431                            54         [ +  + ]:          37439 :             if (cmp != 0)
                                 55                 :          35553 :                 return cmp;
                                 56                 :                :         }
                                 57                 :                :     }
                                 58                 :                : 
                                 59                 :                :     /* Heap items go before inner pages, to ensure a depth-first search */
 4152 heikki.linnakangas@i       60   [ +  +  -  + ]:         162921 :     if (GISTSearchItemIsHeap(*sa) && !GISTSearchItemIsHeap(*sb))
 4152 heikki.linnakangas@i       61                 :UBC           0 :         return 1;
 4095 heikki.linnakangas@i       62   [ +  +  +  + ]:CBC      162921 :     if (!GISTSearchItemIsHeap(*sa) && GISTSearchItemIsHeap(*sb))
 4095 heikki.linnakangas@i       63                 :GBC           2 :         return -1;
                                 64                 :                : 
 4152 heikki.linnakangas@i       65                 :CBC      162919 :     return 0;
                                 66                 :                : }
                                 67                 :                : 
                                 68                 :                : 
                                 69                 :                : /*
                                 70                 :                :  * Index AM API functions for scanning GiST indexes
                                 71                 :                :  */
                                 72                 :                : 
                                 73                 :                : IndexScanDesc
 3761 tgl@sss.pgh.pa.us          74                 :           6273 : gistbeginscan(Relation r, int nkeys, int norderbys)
                                 75                 :                : {
                                 76                 :                :     IndexScanDesc scan;
                                 77                 :                :     GISTSTATE  *giststate;
                                 78                 :                :     GISTScanOpaque so;
                                 79                 :                :     MemoryContext oldCxt;
                                 80                 :                : 
 5633                            81                 :           6273 :     scan = RelationGetIndexScan(r, nkeys, norderbys);
                                 82                 :                : 
                                 83                 :                :     /* First, set up a GISTSTATE with a scan-lifespan memory context */
 5331                            84                 :           6273 :     giststate = initGISTstate(scan->indexRelation);
                                 85                 :                : 
                                 86                 :                :     /*
                                 87                 :                :      * Everything made below is in the scanCxt, or is a child of the scanCxt,
                                 88                 :                :      * so it'll all go away automatically in gistendscan.
                                 89                 :                :      */
                                 90                 :           6273 :     oldCxt = MemoryContextSwitchTo(giststate->scanCxt);
                                 91                 :                : 
                                 92                 :                :     /* initialize opaque data */
  146 michael@paquier.xyz        93                 :GNC        6273 :     so = palloc0_object(GISTScanOpaqueData);
 5331 tgl@sss.pgh.pa.us          94                 :CBC        6273 :     so->giststate = giststate;
                                 95                 :           6273 :     giststate->tempCxt = createTempGistContext();
                                 96                 :           6273 :     so->queue = NULL;
 5077 bruce@momjian.us           97                 :           6273 :     so->queueCxt = giststate->scanCxt;    /* see gistrescan */
                                 98                 :                : 
                                 99                 :                :     /* workspaces with size dependent on numberOfOrderBys: */
 2420 akorotkov@postgresql      100                 :           6273 :     so->distances = palloc(sizeof(so->distances[0]) * scan->numberOfOrderBys);
 5632 tgl@sss.pgh.pa.us         101                 :           6273 :     so->qual_ok = true;          /* in case there are zero keys */
 4008 heikki.linnakangas@i      102         [ +  + ]:           6273 :     if (scan->numberOfOrderBys > 0)
                                103                 :                :     {
  146 michael@paquier.xyz       104                 :GNC          75 :         scan->xs_orderbyvals = palloc0_array(Datum, scan->numberOfOrderBys);
                                105                 :             75 :         scan->xs_orderbynulls = palloc_array(bool, scan->numberOfOrderBys);
 4000 tgl@sss.pgh.pa.us         106                 :CBC          75 :         memset(scan->xs_orderbynulls, true, sizeof(bool) * scan->numberOfOrderBys);
                                107                 :                :     }
                                108                 :                : 
 3891 teodor@sigaev.ru          109                 :           6273 :     so->killedItems = NULL;      /* until needed */
                                110                 :           6273 :     so->numKilled = 0;
                                111                 :           6273 :     so->curBlkno = InvalidBlockNumber;
                                112                 :           6273 :     so->curPageLSN = InvalidXLogRecPtr;
                                113                 :                : 
 5633 tgl@sss.pgh.pa.us         114                 :           6273 :     scan->opaque = so;
                                115                 :                : 
                                116                 :                :     /*
                                117                 :                :      * All fields required for index-only scans are initialized in gistrescan,
                                118                 :                :      * as we don't know yet if we're doing an index-only scan or not.
                                119                 :                :      */
                                120                 :                : 
 5331                           121                 :           6273 :     MemoryContextSwitchTo(oldCxt);
                                122                 :                : 
 3761                           123                 :           6273 :     return scan;
                                124                 :                : }
                                125                 :                : 
                                126                 :                : void
                                127                 :           6353 : gistrescan(IndexScanDesc scan, ScanKey key, int nkeys,
                                128                 :                :            ScanKey orderbys, int norderbys)
                                129                 :                : {
                                130                 :                :     /* nkeys and norderbys arguments are ignored */
 5633                           131                 :           6353 :     GISTScanOpaque so = (GISTScanOpaque) scan->opaque;
                                132                 :                :     bool        first_time;
                                133                 :                :     int         i;
                                134                 :                :     MemoryContext oldCxt;
                                135                 :                : 
                                136                 :                :     /* rescan an existing indexscan --- reset state */
                                137                 :                : 
                                138                 :                :     /*
                                139                 :                :      * The first time through, we create the search queue in the scanCxt.
                                140                 :                :      * Subsequent times through, we create the queue in a separate queueCxt,
                                141                 :                :      * which is created on the second call and reset on later calls.  Thus, in
                                142                 :                :      * the common case where a scan is only rescan'd once, we just put the
                                143                 :                :      * queue in scanCxt and don't pay the overhead of making a second memory
                                144                 :                :      * context.  If we do rescan more than once, the first queue is just left
                                145                 :                :      * for dead until end of scan; this small wastage seems worth the savings
                                146                 :                :      * in the common case.
                                147                 :                :      */
 5331                           148         [ +  + ]:           6353 :     if (so->queue == NULL)
                                149                 :                :     {
                                150                 :                :         /* first time through */
                                151         [ -  + ]:           6273 :         Assert(so->queueCxt == so->giststate->scanCxt);
                                152                 :           6273 :         first_time = true;
                                153                 :                :     }
                                154         [ +  + ]:             80 :     else if (so->queueCxt == so->giststate->scanCxt)
                                155                 :                :     {
                                156                 :                :         /* second time through */
                                157                 :             40 :         so->queueCxt = AllocSetContextCreate(so->giststate->scanCxt,
                                158                 :                :                                              "GiST queue context",
                                159                 :                :                                              ALLOCSET_DEFAULT_SIZES);
                                160                 :             40 :         first_time = false;
                                161                 :                :     }
                                162                 :                :     else
                                163                 :                :     {
                                164                 :                :         /* third or later time through */
                                165                 :             40 :         MemoryContextReset(so->queueCxt);
                                166                 :             40 :         first_time = false;
                                167                 :                :     }
                                168                 :                : 
                                169                 :                :     /*
                                170                 :                :      * If we're doing an index-only scan, on the first call, also initialize a
                                171                 :                :      * tuple descriptor to represent the returned index tuples and create a
                                172                 :                :      * memory context to hold them during the scan.
                                173                 :                :      */
 3354                           174   [ +  +  +  + ]:           6353 :     if (scan->xs_want_itup && !scan->xs_hitupdesc)
                                175                 :                :     {
                                176                 :                :         int         natts;
                                177                 :                :         int         nkeyatts;
                                178                 :                :         int         attno;
                                179                 :                : 
                                180                 :                :         /*
                                181                 :                :          * The storage type of the index can be different from the original
                                182                 :                :          * datatype being indexed, so we cannot just grab the index's tuple
                                183                 :                :          * descriptor. Instead, construct a descriptor with the original data
                                184                 :                :          * types.
                                185                 :                :          */
 4000 bruce@momjian.us          186                 :            458 :         natts = RelationGetNumberOfAttributes(scan->indexRelation);
 2613 akorotkov@postgresql      187                 :            458 :         nkeyatts = IndexRelationGetNumberOfKeyAttributes(scan->indexRelation);
 2723 andres@anarazel.de        188                 :            458 :         so->giststate->fetchTupdesc = CreateTemplateTupleDesc(natts);
 2613 akorotkov@postgresql      189         [ +  + ]:            933 :         for (attno = 1; attno <= nkeyatts; attno++)
                                190                 :                :         {
 4058 heikki.linnakangas@i      191                 :            475 :             TupleDescInitEntry(so->giststate->fetchTupdesc, attno, NULL,
                                192                 :            475 :                                scan->indexRelation->rd_opcintype[attno - 1],
                                193                 :                :                                -1, 0);
                                194                 :                :         }
                                195                 :                : 
 2613 akorotkov@postgresql      196         [ -  + ]:            458 :         for (; attno <= natts; attno++)
                                197                 :                :         {
                                198                 :                :             /* taking opcintype from giststate->tupdesc */
 2613 akorotkov@postgresql      199                 :UBC           0 :             TupleDescInitEntry(so->giststate->fetchTupdesc, attno, NULL,
                                200                 :              0 :                                TupleDescAttr(so->giststate->leafTupdesc,
                                201                 :                :                                              attno - 1)->atttypid,
                                202                 :                :                                -1, 0);
                                203                 :                :         }
   50 drowley@postgresql.o      204                 :GNC         458 :         TupleDescFinalize(so->giststate->fetchTupdesc);
 3354 tgl@sss.pgh.pa.us         205                 :CBC         458 :         scan->xs_hitupdesc = so->giststate->fetchTupdesc;
                                206                 :                : 
                                207                 :                :         /* Also create a memory context that will hold the returned tuples */
 4058 heikki.linnakangas@i      208                 :            458 :         so->pageDataCxt = AllocSetContextCreate(so->giststate->scanCxt,
                                209                 :                :                                                 "GiST page data context",
                                210                 :                :                                                 ALLOCSET_DEFAULT_SIZES);
                                211                 :                :     }
                                212                 :                : 
                                213                 :                :     /* create new, empty pairing heap for search queue */
 5632 tgl@sss.pgh.pa.us         214                 :           6353 :     oldCxt = MemoryContextSwitchTo(so->queueCxt);
 4152 heikki.linnakangas@i      215                 :           6353 :     so->queue = pairingheap_allocate(pairingheap_GISTSearchItem_cmp, scan);
 5632 tgl@sss.pgh.pa.us         216                 :           6353 :     MemoryContextSwitchTo(oldCxt);
                                217                 :                : 
                                218                 :           6353 :     so->firstCall = true;
                                219                 :                : 
                                220                 :                :     /* Update scan key, if a new one is given */
 7658 neilc@samurai.com         221   [ +  -  +  + ]:           6353 :     if (key && scan->numberOfKeys > 0)
                                222                 :                :     {
 4744 tgl@sss.pgh.pa.us         223                 :           6262 :         void      **fn_extras = NULL;
                                224                 :                : 
                                225                 :                :         /*
                                226                 :                :          * If this isn't the first time through, preserve the fn_extra
                                227                 :                :          * pointers, so that if the consistentFns are using them to cache
                                228                 :                :          * data, that data is not leaked across a rescan.
                                229                 :                :          */
 5331                           230         [ +  + ]:           6262 :         if (!first_time)
                                231                 :                :         {
 4744                           232                 :             40 :             fn_extras = (void **) palloc(scan->numberOfKeys * sizeof(void *));
 5331                           233         [ +  + ]:             80 :             for (i = 0; i < scan->numberOfKeys; i++)
 4744                           234                 :             40 :                 fn_extras[i] = scan->keyData[i].sk_func.fn_extra;
                                235                 :                :         }
                                236                 :                : 
  601 peter@eisentraut.org      237                 :           6262 :         memcpy(scan->keyData, key, scan->numberOfKeys * sizeof(ScanKeyData));
                                238                 :                : 
                                239                 :                :         /*
                                240                 :                :          * Modify the scan key so that the Consistent method is called for all
                                241                 :                :          * comparisons. The original operator is passed to the Consistent
                                242                 :                :          * function in the form of its strategy number, which is available
                                243                 :                :          * from the sk_strategy field, and its subtype from the sk_subtype
                                244                 :                :          * field.
                                245                 :                :          *
                                246                 :                :          * Next, if any of keys is a NULL and that key is not marked with
                                247                 :                :          * SK_SEARCHNULL/SK_SEARCHNOTNULL then nothing can be found (ie, we
                                248                 :                :          * assume all indexable operators are strict).
                                249                 :                :          */
 5632 tgl@sss.pgh.pa.us         250                 :           6262 :         so->qual_ok = true;
                                251                 :                : 
 6172 bruce@momjian.us          252         [ +  + ]:          16698 :         for (i = 0; i < scan->numberOfKeys; i++)
                                253                 :                :         {
 5632 tgl@sss.pgh.pa.us         254                 :          10436 :             ScanKey     skey = scan->keyData + i;
                                255                 :                : 
                                256                 :                :             /*
                                257                 :                :              * Copy consistent support function to ScanKey structure instead
                                258                 :                :              * of function implementing filtering operator.
                                259                 :                :              */
 4744                           260                 :          10436 :             fmgr_info_copy(&(skey->sk_func),
                                261                 :          10436 :                            &(so->giststate->consistentFn[skey->sk_attno - 1]),
                                262                 :          10436 :                            so->giststate->scanCxt);
                                263                 :                : 
                                264                 :                :             /* Restore prior fn_extra pointers, if not first time */
                                265         [ +  + ]:          10436 :             if (!first_time)
                                266                 :             40 :                 skey->sk_func.fn_extra = fn_extras[i];
                                267                 :                : 
 5968                           268         [ +  + ]:          10436 :             if (skey->sk_flags & SK_ISNULL)
                                269                 :                :             {
                                270         [ -  + ]:             17 :                 if (!(skey->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL)))
 6409 teodor@sigaev.ru          271                 :UBC           0 :                     so->qual_ok = false;
                                272                 :                :             }
                                273                 :                :         }
                                274                 :                : 
 4744 tgl@sss.pgh.pa.us         275         [ +  + ]:CBC        6262 :         if (!first_time)
                                276                 :             40 :             pfree(fn_extras);
                                277                 :                :     }
                                278                 :                : 
                                279                 :                :     /* Update order-by key, if a new one is given */
 5632                           280   [ +  +  +  + ]:           6353 :     if (orderbys && scan->numberOfOrderBys > 0)
                                281                 :                :     {
 4744                           282                 :            123 :         void      **fn_extras = NULL;
                                283                 :                : 
                                284                 :                :         /* As above, preserve fn_extra if not first time through */
 5331                           285         [ +  + ]:            123 :         if (!first_time)
                                286                 :                :         {
 4744                           287                 :             48 :             fn_extras = (void **) palloc(scan->numberOfOrderBys * sizeof(void *));
 5331                           288         [ +  + ]:             96 :             for (i = 0; i < scan->numberOfOrderBys; i++)
 4744                           289                 :             48 :                 fn_extras[i] = scan->orderByData[i].sk_func.fn_extra;
                                290                 :                :         }
                                291                 :                : 
  601 peter@eisentraut.org      292                 :            123 :         memcpy(scan->orderByData, orderbys, scan->numberOfOrderBys * sizeof(ScanKeyData));
                                293                 :                : 
 4008 heikki.linnakangas@i      294                 :            123 :         so->orderByTypes = (Oid *) palloc(scan->numberOfOrderBys * sizeof(Oid));
                                295                 :                : 
                                296                 :                :         /*
                                297                 :                :          * Modify the order-by key so that the Distance method is called for
                                298                 :                :          * all comparisons. The original operator is passed to the Distance
                                299                 :                :          * function in the form of its strategy number, which is available
                                300                 :                :          * from the sk_strategy field, and its subtype from the sk_subtype
                                301                 :                :          * field.
                                302                 :                :          */
 5632 tgl@sss.pgh.pa.us         303         [ +  + ]:            246 :         for (i = 0; i < scan->numberOfOrderBys; i++)
                                304                 :                :         {
                                305                 :            123 :             ScanKey     skey = scan->orderByData + i;
 4744                           306                 :            123 :             FmgrInfo   *finfo = &(so->giststate->distanceFn[skey->sk_attno - 1]);
                                307                 :                : 
                                308                 :                :             /* Check we actually have a distance function ... */
                                309         [ -  + ]:            123 :             if (!OidIsValid(finfo->fn_oid))
 5632 tgl@sss.pgh.pa.us         310         [ #  # ]:UBC           0 :                 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
                                311                 :                :                      GIST_DISTANCE_PROC, skey->sk_attno,
                                312                 :                :                      RelationGetRelationName(scan->indexRelation));
                                313                 :                : 
                                314                 :                :             /*
                                315                 :                :              * Look up the datatype returned by the original ordering
                                316                 :                :              * operator. GiST always uses a float8 for the distance function,
                                317                 :                :              * but the ordering operator could be anything else.
                                318                 :                :              *
                                319                 :                :              * XXX: The distance function is only allowed to be lossy if the
                                320                 :                :              * ordering operator's result type is float4 or float8.  Otherwise
                                321                 :                :              * we don't know how to return the distance to the executor.  But
                                322                 :                :              * we cannot check that here, as we won't know if the distance
                                323                 :                :              * function is lossy until it returns *recheck = true for the
                                324                 :                :              * first time.
                                325                 :                :              */
 4008 heikki.linnakangas@i      326                 :CBC         123 :             so->orderByTypes[i] = get_func_rettype(skey->sk_func.fn_oid);
                                327                 :                : 
                                328                 :                :             /*
                                329                 :                :              * Copy distance support function to ScanKey structure instead of
                                330                 :                :              * function implementing ordering operator.
                                331                 :                :              */
 3745 teodor@sigaev.ru          332                 :            123 :             fmgr_info_copy(&(skey->sk_func), finfo, so->giststate->scanCxt);
                                333                 :                : 
                                334                 :                :             /* Restore prior fn_extra pointers, if not first time */
 4744 tgl@sss.pgh.pa.us         335         [ +  + ]:            123 :             if (!first_time)
                                336                 :             48 :                 skey->sk_func.fn_extra = fn_extras[i];
                                337                 :                :         }
                                338                 :                : 
                                339         [ +  + ]:            123 :         if (!first_time)
                                340                 :             48 :             pfree(fn_extras);
                                341                 :                :     }
                                342                 :                : 
                                343                 :                :     /* any previous xs_hitup will have been pfree'd in context resets above */
 3288                           344                 :           6353 :     scan->xs_hitup = NULL;
10844 scrappy@hub.org           345                 :           6353 : }
                                346                 :                : 
                                347                 :                : void
 3761 tgl@sss.pgh.pa.us         348                 :           6018 : gistendscan(IndexScanDesc scan)
                                349                 :                : {
 5633                           350                 :           6018 :     GISTScanOpaque so = (GISTScanOpaque) scan->opaque;
                                351                 :                : 
                                352                 :                :     /*
                                353                 :                :      * freeGISTstate is enough to clean up everything made by gistbeginscan,
                                354                 :                :      * as well as the queueCxt if there is a separate context for it.
                                355                 :                :      */
 5632                           356                 :           6018 :     freeGISTstate(so->giststate);
10844 scrappy@hub.org           357                 :           6018 : }
        

Generated by: LCOV version 2.5.0-beta