LCOV - differential code coverage report
Current view: top level - src/backend/access/gist - gistscan.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 95.5 % 112 107 5 107
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 4 4 4
Baseline: lcov-20250906-005545-baseline Branches: 87.1 % 70 61 9 61
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 2 2 2
(360..) days: 95.5 % 110 105 5 105
Function coverage date bins:
(360..) days: 100.0 % 4 4 4
Branch coverage date bins:
(360..) days: 87.1 % 70 61 9 61

 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-2025, 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
 3911 heikki.linnakangas@i       30                 :CBC      184214 : pairingheap_GISTSearchItem_cmp(const pairingheap_node *a, const pairingheap_node *b, void *arg)
                                 31                 :                : {
                                 32                 :         184214 :     const GISTSearchItem *sa = (const GISTSearchItem *) a;
                                 33                 :         184214 :     const GISTSearchItem *sb = (const GISTSearchItem *) b;
 5391 tgl@sss.pgh.pa.us          34                 :         184214 :     IndexScanDesc scan = (IndexScanDesc) arg;
                                 35                 :                :     int         i;
                                 36                 :                : 
                                 37                 :                :     /* Order according to distance comparison */
                                 38         [ +  + ]:         186176 :     for (i = 0; i < scan->numberOfOrderBys; i++)
                                 39                 :                :     {
 2179 akorotkov@postgresql       40         [ +  + ]:          34833 :         if (sa->distances[i].isnull)
                                 41                 :                :         {
                                 42         [ +  - ]:             54 :             if (!sb->distances[i].isnull)
 2190                            43                 :             54 :                 return -1;
                                 44                 :                :         }
 2179                            45         [ +  + ]:          34779 :         else if (sb->distances[i].isnull)
                                 46                 :                :         {
 2190                            47                 :             15 :             return 1;
                                 48                 :                :         }
                                 49                 :                :         else
                                 50                 :                :         {
 2179                            51                 :          69528 :             int         cmp = -float8_cmp_internal(sa->distances[i].value,
                                 52                 :          34764 :                                                    sb->distances[i].value);
                                 53                 :                : 
 2190                            54         [ +  + ]:          34764 :             if (cmp != 0)
                                 55                 :          32802 :                 return cmp;
                                 56                 :                :         }
                                 57                 :                :     }
                                 58                 :                : 
                                 59                 :                :     /* Heap items go before inner pages, to ensure a depth-first search */
 3911 heikki.linnakangas@i       60   [ +  +  -  + ]:         151343 :     if (GISTSearchItemIsHeap(*sa) && !GISTSearchItemIsHeap(*sb))
 3911 heikki.linnakangas@i       61                 :UBC           0 :         return 1;
 3854 heikki.linnakangas@i       62   [ +  +  +  + ]:CBC      151343 :     if (!GISTSearchItemIsHeap(*sa) && GISTSearchItemIsHeap(*sb))
                                 63                 :              2 :         return -1;
                                 64                 :                : 
 3911                            65                 :         151341 :     return 0;
                                 66                 :                : }
                                 67                 :                : 
                                 68                 :                : 
                                 69                 :                : /*
                                 70                 :                :  * Index AM API functions for scanning GiST indexes
                                 71                 :                :  */
                                 72                 :                : 
                                 73                 :                : IndexScanDesc
 3520 tgl@sss.pgh.pa.us          74                 :           2892 : gistbeginscan(Relation r, int nkeys, int norderbys)
                                 75                 :                : {
                                 76                 :                :     IndexScanDesc scan;
                                 77                 :                :     GISTSTATE  *giststate;
                                 78                 :                :     GISTScanOpaque so;
                                 79                 :                :     MemoryContext oldCxt;
                                 80                 :                : 
 5392                            81                 :           2892 :     scan = RelationGetIndexScan(r, nkeys, norderbys);
                                 82                 :                : 
                                 83                 :                :     /* First, set up a GISTSTATE with a scan-lifespan memory context */
 5090                            84                 :           2892 :     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                 :           2892 :     oldCxt = MemoryContextSwitchTo(giststate->scanCxt);
                                 91                 :                : 
                                 92                 :                :     /* initialize opaque data */
 5391                            93                 :           2892 :     so = (GISTScanOpaque) palloc0(sizeof(GISTScanOpaqueData));
 5090                            94                 :           2892 :     so->giststate = giststate;
                                 95                 :           2892 :     giststate->tempCxt = createTempGistContext();
                                 96                 :           2892 :     so->queue = NULL;
 4836 bruce@momjian.us           97                 :           2892 :     so->queueCxt = giststate->scanCxt;    /* see gistrescan */
                                 98                 :                : 
                                 99                 :                :     /* workspaces with size dependent on numberOfOrderBys: */
 2179 akorotkov@postgresql      100                 :           2892 :     so->distances = palloc(sizeof(so->distances[0]) * scan->numberOfOrderBys);
 5391 tgl@sss.pgh.pa.us         101                 :           2892 :     so->qual_ok = true;          /* in case there are zero keys */
 3767 heikki.linnakangas@i      102         [ +  + ]:           2892 :     if (scan->numberOfOrderBys > 0)
                                103                 :                :     {
 3759 tgl@sss.pgh.pa.us         104                 :             63 :         scan->xs_orderbyvals = palloc0(sizeof(Datum) * scan->numberOfOrderBys);
 3767 heikki.linnakangas@i      105                 :             63 :         scan->xs_orderbynulls = palloc(sizeof(bool) * scan->numberOfOrderBys);
 3759 tgl@sss.pgh.pa.us         106                 :             63 :         memset(scan->xs_orderbynulls, true, sizeof(bool) * scan->numberOfOrderBys);
                                107                 :                :     }
                                108                 :                : 
 3650 teodor@sigaev.ru          109                 :           2892 :     so->killedItems = NULL;      /* until needed */
                                110                 :           2892 :     so->numKilled = 0;
                                111                 :           2892 :     so->curBlkno = InvalidBlockNumber;
                                112                 :           2892 :     so->curPageLSN = InvalidXLogRecPtr;
                                113                 :                : 
 5392 tgl@sss.pgh.pa.us         114                 :           2892 :     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                 :                : 
 5090                           121                 :           2892 :     MemoryContextSwitchTo(oldCxt);
                                122                 :                : 
 3520                           123                 :           2892 :     return scan;
                                124                 :                : }
                                125                 :                : 
                                126                 :                : void
                                127                 :           2937 : gistrescan(IndexScanDesc scan, ScanKey key, int nkeys,
                                128                 :                :            ScanKey orderbys, int norderbys)
                                129                 :                : {
                                130                 :                :     /* nkeys and norderbys arguments are ignored */
 5392                           131                 :           2937 :     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                 :                :      */
 5090                           148         [ +  + ]:           2937 :     if (so->queue == NULL)
                                149                 :                :     {
                                150                 :                :         /* first time through */
                                151         [ -  + ]:           2892 :         Assert(so->queueCxt == so->giststate->scanCxt);
                                152                 :           2892 :         first_time = true;
                                153                 :                :     }
                                154         [ +  + ]:             45 :     else if (so->queueCxt == so->giststate->scanCxt)
                                155                 :                :     {
                                156                 :                :         /* second time through */
                                157                 :             15 :         so->queueCxt = AllocSetContextCreate(so->giststate->scanCxt,
                                158                 :                :                                              "GiST queue context",
                                159                 :                :                                              ALLOCSET_DEFAULT_SIZES);
                                160                 :             15 :         first_time = false;
                                161                 :                :     }
                                162                 :                :     else
                                163                 :                :     {
                                164                 :                :         /* third or later time through */
                                165                 :             30 :         MemoryContextReset(so->queueCxt);
                                166                 :             30 :         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                 :                :      */
 3113                           174   [ +  +  +  + ]:           2937 :     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                 :                :          */
 3759 bruce@momjian.us          186                 :            334 :         natts = RelationGetNumberOfAttributes(scan->indexRelation);
 2372 akorotkov@postgresql      187                 :            334 :         nkeyatts = IndexRelationGetNumberOfKeyAttributes(scan->indexRelation);
 2482 andres@anarazel.de        188                 :            334 :         so->giststate->fetchTupdesc = CreateTemplateTupleDesc(natts);
 2372 akorotkov@postgresql      189         [ +  + ]:            681 :         for (attno = 1; attno <= nkeyatts; attno++)
                                190                 :                :         {
 3817 heikki.linnakangas@i      191                 :            347 :             TupleDescInitEntry(so->giststate->fetchTupdesc, attno, NULL,
                                192                 :            347 :                                scan->indexRelation->rd_opcintype[attno - 1],
                                193                 :                :                                -1, 0);
                                194                 :                :         }
                                195                 :                : 
 2372 akorotkov@postgresql      196         [ -  + ]:            334 :         for (; attno <= natts; attno++)
                                197                 :                :         {
                                198                 :                :             /* taking opcintype from giststate->tupdesc */
 2372 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                 :                :         }
 3113 tgl@sss.pgh.pa.us         204                 :CBC         334 :         scan->xs_hitupdesc = so->giststate->fetchTupdesc;
                                205                 :                : 
                                206                 :                :         /* Also create a memory context that will hold the returned tuples */
 3817 heikki.linnakangas@i      207                 :            334 :         so->pageDataCxt = AllocSetContextCreate(so->giststate->scanCxt,
                                208                 :                :                                                 "GiST page data context",
                                209                 :                :                                                 ALLOCSET_DEFAULT_SIZES);
                                210                 :                :     }
                                211                 :                : 
                                212                 :                :     /* create new, empty pairing heap for search queue */
 5391 tgl@sss.pgh.pa.us         213                 :           2937 :     oldCxt = MemoryContextSwitchTo(so->queueCxt);
 3911 heikki.linnakangas@i      214                 :           2937 :     so->queue = pairingheap_allocate(pairingheap_GISTSearchItem_cmp, scan);
 5391 tgl@sss.pgh.pa.us         215                 :           2937 :     MemoryContextSwitchTo(oldCxt);
                                216                 :                : 
                                217                 :           2937 :     so->firstCall = true;
                                218                 :                : 
                                219                 :                :     /* Update scan key, if a new one is given */
 7417 neilc@samurai.com         220   [ +  -  +  + ]:           2937 :     if (key && scan->numberOfKeys > 0)
                                221                 :                :     {
 4503 tgl@sss.pgh.pa.us         222                 :           2862 :         void      **fn_extras = NULL;
                                223                 :                : 
                                224                 :                :         /*
                                225                 :                :          * If this isn't the first time through, preserve the fn_extra
                                226                 :                :          * pointers, so that if the consistentFns are using them to cache
                                227                 :                :          * data, that data is not leaked across a rescan.
                                228                 :                :          */
 5090                           229         [ +  + ]:           2862 :         if (!first_time)
                                230                 :                :         {
 4503                           231                 :             15 :             fn_extras = (void **) palloc(scan->numberOfKeys * sizeof(void *));
 5090                           232         [ +  + ]:             30 :             for (i = 0; i < scan->numberOfKeys; i++)
 4503                           233                 :             15 :                 fn_extras[i] = scan->keyData[i].sk_func.fn_extra;
                                234                 :                :         }
                                235                 :                : 
  360 peter@eisentraut.org      236                 :           2862 :         memcpy(scan->keyData, key, scan->numberOfKeys * sizeof(ScanKeyData));
                                237                 :                : 
                                238                 :                :         /*
                                239                 :                :          * Modify the scan key so that the Consistent method is called for all
                                240                 :                :          * comparisons. The original operator is passed to the Consistent
                                241                 :                :          * function in the form of its strategy number, which is available
                                242                 :                :          * from the sk_strategy field, and its subtype from the sk_subtype
                                243                 :                :          * field.
                                244                 :                :          *
                                245                 :                :          * Next, if any of keys is a NULL and that key is not marked with
                                246                 :                :          * SK_SEARCHNULL/SK_SEARCHNOTNULL then nothing can be found (ie, we
                                247                 :                :          * assume all indexable operators are strict).
                                248                 :                :          */
 5391 tgl@sss.pgh.pa.us         249                 :           2862 :         so->qual_ok = true;
                                250                 :                : 
 5931 bruce@momjian.us          251         [ +  + ]:           7137 :         for (i = 0; i < scan->numberOfKeys; i++)
                                252                 :                :         {
 5391 tgl@sss.pgh.pa.us         253                 :           4275 :             ScanKey     skey = scan->keyData + i;
                                254                 :                : 
                                255                 :                :             /*
                                256                 :                :              * Copy consistent support function to ScanKey structure instead
                                257                 :                :              * of function implementing filtering operator.
                                258                 :                :              */
 4503                           259                 :           4275 :             fmgr_info_copy(&(skey->sk_func),
                                260                 :           4275 :                            &(so->giststate->consistentFn[skey->sk_attno - 1]),
                                261                 :           4275 :                            so->giststate->scanCxt);
                                262                 :                : 
                                263                 :                :             /* Restore prior fn_extra pointers, if not first time */
                                264         [ +  + ]:           4275 :             if (!first_time)
                                265                 :             15 :                 skey->sk_func.fn_extra = fn_extras[i];
                                266                 :                : 
 5727                           267         [ +  + ]:           4275 :             if (skey->sk_flags & SK_ISNULL)
                                268                 :                :             {
                                269         [ -  + ]:             13 :                 if (!(skey->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL)))
 6168 teodor@sigaev.ru          270                 :UBC           0 :                     so->qual_ok = false;
                                271                 :                :             }
                                272                 :                :         }
                                273                 :                : 
 4503 tgl@sss.pgh.pa.us         274         [ +  + ]:CBC        2862 :         if (!first_time)
                                275                 :             15 :             pfree(fn_extras);
                                276                 :                :     }
                                277                 :                : 
                                278                 :                :     /* Update order-by key, if a new one is given */
 5391                           279   [ +  +  +  + ]:           2937 :     if (orderbys && scan->numberOfOrderBys > 0)
                                280                 :                :     {
 4503                           281                 :             99 :         void      **fn_extras = NULL;
                                282                 :                : 
                                283                 :                :         /* As above, preserve fn_extra if not first time through */
 5090                           284         [ +  + ]:             99 :         if (!first_time)
                                285                 :                :         {
 4503                           286                 :             36 :             fn_extras = (void **) palloc(scan->numberOfOrderBys * sizeof(void *));
 5090                           287         [ +  + ]:             72 :             for (i = 0; i < scan->numberOfOrderBys; i++)
 4503                           288                 :             36 :                 fn_extras[i] = scan->orderByData[i].sk_func.fn_extra;
                                289                 :                :         }
                                290                 :                : 
  360 peter@eisentraut.org      291                 :             99 :         memcpy(scan->orderByData, orderbys, scan->numberOfOrderBys * sizeof(ScanKeyData));
                                292                 :                : 
 3767 heikki.linnakangas@i      293                 :             99 :         so->orderByTypes = (Oid *) palloc(scan->numberOfOrderBys * sizeof(Oid));
                                294                 :                : 
                                295                 :                :         /*
                                296                 :                :          * Modify the order-by key so that the Distance method is called for
                                297                 :                :          * all comparisons. The original operator is passed to the Distance
                                298                 :                :          * function in the form of its strategy number, which is available
                                299                 :                :          * from the sk_strategy field, and its subtype from the sk_subtype
                                300                 :                :          * field.
                                301                 :                :          */
 5391 tgl@sss.pgh.pa.us         302         [ +  + ]:            198 :         for (i = 0; i < scan->numberOfOrderBys; i++)
                                303                 :                :         {
                                304                 :             99 :             ScanKey     skey = scan->orderByData + i;
 4503                           305                 :             99 :             FmgrInfo   *finfo = &(so->giststate->distanceFn[skey->sk_attno - 1]);
                                306                 :                : 
                                307                 :                :             /* Check we actually have a distance function ... */
                                308         [ -  + ]:             99 :             if (!OidIsValid(finfo->fn_oid))
 5391 tgl@sss.pgh.pa.us         309         [ #  # ]:UBC           0 :                 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
                                310                 :                :                      GIST_DISTANCE_PROC, skey->sk_attno,
                                311                 :                :                      RelationGetRelationName(scan->indexRelation));
                                312                 :                : 
                                313                 :                :             /*
                                314                 :                :              * Look up the datatype returned by the original ordering
                                315                 :                :              * operator. GiST always uses a float8 for the distance function,
                                316                 :                :              * but the ordering operator could be anything else.
                                317                 :                :              *
                                318                 :                :              * XXX: The distance function is only allowed to be lossy if the
                                319                 :                :              * ordering operator's result type is float4 or float8.  Otherwise
                                320                 :                :              * we don't know how to return the distance to the executor.  But
                                321                 :                :              * we cannot check that here, as we won't know if the distance
                                322                 :                :              * function is lossy until it returns *recheck = true for the
                                323                 :                :              * first time.
                                324                 :                :              */
 3767 heikki.linnakangas@i      325                 :CBC          99 :             so->orderByTypes[i] = get_func_rettype(skey->sk_func.fn_oid);
                                326                 :                : 
                                327                 :                :             /*
                                328                 :                :              * Copy distance support function to ScanKey structure instead of
                                329                 :                :              * function implementing ordering operator.
                                330                 :                :              */
 3504 teodor@sigaev.ru          331                 :             99 :             fmgr_info_copy(&(skey->sk_func), finfo, so->giststate->scanCxt);
                                332                 :                : 
                                333                 :                :             /* Restore prior fn_extra pointers, if not first time */
 4503 tgl@sss.pgh.pa.us         334         [ +  + ]:             99 :             if (!first_time)
                                335                 :             36 :                 skey->sk_func.fn_extra = fn_extras[i];
                                336                 :                :         }
                                337                 :                : 
                                338         [ +  + ]:             99 :         if (!first_time)
                                339                 :             36 :             pfree(fn_extras);
                                340                 :                :     }
                                341                 :                : 
                                342                 :                :     /* any previous xs_hitup will have been pfree'd in context resets above */
 3047                           343                 :           2937 :     scan->xs_hitup = NULL;
10603 scrappy@hub.org           344                 :           2937 : }
                                345                 :                : 
                                346                 :                : void
 3520 tgl@sss.pgh.pa.us         347                 :           2760 : gistendscan(IndexScanDesc scan)
                                348                 :                : {
 5392                           349                 :           2760 :     GISTScanOpaque so = (GISTScanOpaque) scan->opaque;
                                350                 :                : 
                                351                 :                :     /*
                                352                 :                :      * freeGISTstate is enough to clean up everything made by gistbeginscan,
                                353                 :                :      * as well as the queueCxt if there is a separate context for it.
                                354                 :                :      */
 5391                           355                 :           2760 :     freeGISTstate(so->giststate);
10603 scrappy@hub.org           356                 :           2760 : }
        

Generated by: LCOV version 2.4-beta