LCOV - differential code coverage report
Current view: top level - src/backend/executor - nodeBitmapHeapscan.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 1a1248818fb8b8b0353c292c632d6a6af77e8209 vs 8e82d6610e7280d7870c0c36ff1c797e3cfb3e09 Lines: 77.3 % 181 140 41 5 135 11
Current Date: 2026-10-12 11:56:10 +0200 Functions: 88.2 % 17 15 2 4 11
Baseline: lcov-20261012-baseline Branches: 59.3 % 86 51 35 51
Baseline Date: 2026-10-12 10:36:06 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 5 5 5
(30,360] days: 45.0 % 40 18 22 18
(360..) days: 86.0 % 136 117 19 117
Function coverage date bins:
(30,360] days: 100.0 % 3 3 3
(360..) days: 85.7 % 14 12 2 4 8
Branch coverage date bins:
(30,360] days: 30.0 % 20 6 14 6
(360..) days: 68.2 % 66 45 21 45

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * nodeBitmapHeapscan.c
                                  4                 :                :  *    Routines to support bitmapped scans of relations
                                  5                 :                :  *
                                  6                 :                :  * NOTE: it is critical that this plan type only be used with MVCC-compliant
                                  7                 :                :  * snapshots (ie, regular snapshots, not SnapshotAny or one of the other
                                  8                 :                :  * special snapshots).  The reason is that since index and heap scans are
                                  9                 :                :  * decoupled, there can be no assurance that the index tuple prompting a
                                 10                 :                :  * visit to a particular heap TID still exists when the visit is made.
                                 11                 :                :  * Therefore the tuple might not exist anymore either (which is OK because
                                 12                 :                :  * heap_fetch will cope) --- but worse, the tuple slot could have been
                                 13                 :                :  * re-used for a newer tuple.  With an MVCC snapshot the newer tuple is
                                 14                 :                :  * certain to fail the time qual and so it will not be mistakenly returned,
                                 15                 :                :  * but with anything else we might return a tuple that doesn't meet the
                                 16                 :                :  * required index qual conditions.
                                 17                 :                :  *
                                 18                 :                :  *
                                 19                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 20                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 21                 :                :  *
                                 22                 :                :  *
                                 23                 :                :  * IDENTIFICATION
                                 24                 :                :  *    src/backend/executor/nodeBitmapHeapscan.c
                                 25                 :                :  *
                                 26                 :                :  *-------------------------------------------------------------------------
                                 27                 :                :  */
                                 28                 :                : /*
                                 29                 :                :  * INTERFACE ROUTINES
                                 30                 :                :  *      ExecBitmapHeapScan          scans a relation using bitmap info
                                 31                 :                :  *      ExecBitmapHeapNext          workhorse for above
                                 32                 :                :  *      ExecInitBitmapHeapScan      creates and initializes state info.
                                 33                 :                :  *      ExecReScanBitmapHeapScan    prepares to rescan the plan.
                                 34                 :                :  *      ExecEndBitmapHeapScan       releases all storage.
                                 35                 :                :  */
                                 36                 :                : #include "postgres.h"
                                 37                 :                : 
                                 38                 :                : #include "access/relscan.h"
                                 39                 :                : #include "access/tableam.h"
                                 40                 :                : #include "access/visibilitymap.h"
                                 41                 :                : #include "executor/executor.h"
                                 42                 :                : #include "executor/instrument.h"
                                 43                 :                : #include "executor/nodeBitmapHeapscan.h"
                                 44                 :                : #include "miscadmin.h"
                                 45                 :                : #include "pgstat.h"
                                 46                 :                : #include "storage/bufmgr.h"
                                 47                 :                : #include "storage/condition_variable.h"
                                 48                 :                : #include "utils/dsa.h"
                                 49                 :                : #include "utils/rel.h"
                                 50                 :                : #include "utils/spccache.h"
                                 51                 :                : #include "utils/wait_event.h"
                                 52                 :                : 
                                 53                 :                : static void BitmapTableScanSetup(BitmapHeapScanState *node);
                                 54                 :                : static TupleTableSlot *BitmapHeapNext(BitmapHeapScanState *node);
                                 55                 :                : static inline void BitmapDoneInitializingSharedState(ParallelBitmapHeapState *pstate);
                                 56                 :                : static bool BitmapShouldInitializeSharedState(ParallelBitmapHeapState *pstate);
                                 57                 :                : 
                                 58                 :                : 
                                 59                 :                : /* ----------------
                                 60                 :                :  *   SharedBitmapState information
                                 61                 :                :  *
                                 62                 :                :  *      BM_INITIAL      TIDBitmap creation is not yet started, so first worker
                                 63                 :                :  *                      to see this state will set the state to BM_INPROGRESS
                                 64                 :                :  *                      and that process will be responsible for creating
                                 65                 :                :  *                      TIDBitmap.
                                 66                 :                :  *      BM_INPROGRESS   TIDBitmap creation is in progress; workers need to
                                 67                 :                :  *                      sleep until it's finished.
                                 68                 :                :  *      BM_FINISHED     TIDBitmap creation is done, so now all workers can
                                 69                 :                :  *                      proceed to iterate over TIDBitmap.
                                 70                 :                :  * ----------------
                                 71                 :                :  */
                                 72                 :                : typedef enum
                                 73                 :                : {
                                 74                 :                :     BM_INITIAL,
                                 75                 :                :     BM_INPROGRESS,
                                 76                 :                :     BM_FINISHED,
                                 77                 :                : } SharedBitmapState;
                                 78                 :                : 
                                 79                 :                : /* ----------------
                                 80                 :                :  *   ParallelBitmapHeapState information
                                 81                 :                :  *      tbmiterator             iterator for scanning current pages
                                 82                 :                :  *      state                   current state of the TIDBitmap
                                 83                 :                :  *      cv                      conditional wait variable
                                 84                 :                :  * ----------------
                                 85                 :                :  */
                                 86                 :                : typedef struct ParallelBitmapHeapState
                                 87                 :                : {
                                 88                 :                :     dsa_pointer tbmiterator;
                                 89                 :                :     pg_atomic_uint32 state;
                                 90                 :                :     ConditionVariable cv;
                                 91                 :                : } ParallelBitmapHeapState;
                                 92                 :                : 
                                 93                 :                : 
                                 94                 :                : /*
                                 95                 :                :  * Do the underlying index scan, build the bitmap, set up the parallel state
                                 96                 :                :  * needed for parallel workers to iterate through the bitmap, and set up the
                                 97                 :                :  * underlying table scan descriptor.
                                 98                 :                :  */
                                 99                 :                : static void
  620 melanieplageman@gmai      100                 :CBC       14532 : BitmapTableScanSetup(BitmapHeapScanState *node)
                                101                 :                : {
                                102                 :          14532 :     TBMIterator tbmiterator = {0};
                                103                 :          14532 :     ParallelBitmapHeapState *pstate = node->pstate;
                                104                 :          14532 :     dsa_area   *dsa = node->ss.ps.state->es_query_dsa;
                                105                 :                : 
                                106         [ +  + ]:          14532 :     if (!pstate)
                                107                 :                :     {
                                108                 :          14304 :         node->tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
                                109                 :                : 
                                110   [ +  -  -  + ]:          14304 :         if (!node->tbm || !IsA(node->tbm, TIDBitmap))
  620 melanieplageman@gmai      111         [ #  # ]:UBC           0 :             elog(ERROR, "unrecognized result from subplan");
                                112                 :                :     }
  620 melanieplageman@gmai      113         [ +  + ]:CBC         228 :     else if (BitmapShouldInitializeSharedState(pstate))
                                114                 :                :     {
                                115                 :                :         /*
                                116                 :                :          * The leader will immediately come out of the function, but others
                                117                 :                :          * will be blocked until leader populates the TBM and wakes them up.
                                118                 :                :          */
                                119                 :             48 :         node->tbm = (TIDBitmap *) MultiExecProcNode(outerPlanState(node));
                                120   [ +  -  -  + ]:             48 :         if (!node->tbm || !IsA(node->tbm, TIDBitmap))
  620 melanieplageman@gmai      121         [ #  # ]:UBC           0 :             elog(ERROR, "unrecognized result from subplan");
                                122                 :                : 
                                123                 :                :         /*
                                124                 :                :          * Prepare to iterate over the TBM. This will return the dsa_pointer
                                125                 :                :          * of the iterator state which will be used by multiple processes to
                                126                 :                :          * iterate jointly.
                                127                 :                :          */
  620 melanieplageman@gmai      128                 :CBC          48 :         pstate->tbmiterator = tbm_prepare_shared_iterate(node->tbm);
                                129                 :                : 
                                130                 :                :         /* We have initialized the shared state so wake up others. */
                                131                 :             48 :         BitmapDoneInitializingSharedState(pstate);
                                132                 :                :     }
                                133                 :                : 
                                134         [ +  + ]:          14532 :     tbmiterator = tbm_begin_iterate(node->tbm, dsa,
                                135                 :                :                                     pstate ?
                                136                 :                :                                     pstate->tbmiterator :
                                137                 :                :                                     InvalidDsaPointer);
                                138                 :                : 
                                139                 :                :     /*
                                140                 :                :      * If this is the first scan of the underlying table, create the table
                                141                 :                :      * scan descriptor and begin the scan.
                                142                 :                :      */
                                143         [ +  + ]:          14532 :     if (!node->ss.ss_currentScanDesc)
                                144                 :                :     {
  188 tomas.vondra@postgre      145                 :          12370 :         uint32      flags = SO_NONE;
                                146                 :                : 
                                147         [ +  + ]:          12370 :         if (ScanRelIsReadOnly(&node->ss))
                                148                 :           8901 :             flags |= SO_HINT_REL_READ_ONLY;
                                149                 :                : 
                                150         [ -  + ]:          12370 :         if (node->ss.ps.state->es_instrument & INSTRUMENT_IO)
  188 tomas.vondra@postgre      151                 :UBC           0 :             flags |= SO_SCAN_INSTRUMENT;
                                152                 :                : 
  620 melanieplageman@gmai      153                 :CBC       12370 :         node->ss.ss_currentScanDesc =
                                154                 :          12370 :             table_beginscan_bm(node->ss.ss_currentRelation,
                                155                 :          12370 :                                node->ss.ps.state->es_snapshot,
                                156                 :                :                                0,
                                157                 :                :                                NULL,
                                158                 :                :                                flags);
                                159                 :                :     }
                                160                 :                : 
                                161                 :          14532 :     node->ss.ss_currentScanDesc->st.rs_tbmiterator = tbmiterator;
                                162                 :          14532 :     node->initialized = true;
                                163                 :          14532 : }
                                164                 :                : 
                                165                 :                : /* ----------------------------------------------------------------
                                166                 :                :  *      BitmapHeapNext
                                167                 :                :  *
                                168                 :                :  *      Retrieve next tuple from the BitmapHeapScan node's currentRelation
                                169                 :                :  * ----------------------------------------------------------------
                                170                 :                :  */
                                171                 :                : static TupleTableSlot *
 7846 tgl@sss.pgh.pa.us         172                 :        3600113 : BitmapHeapNext(BitmapHeapScanState *node)
                                173                 :                : {
  576 melanieplageman@gmai      174                 :        3600113 :     ExprContext *econtext = node->ss.ps.ps_ExprContext;
                                175                 :        3600113 :     TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
                                176                 :                : 
                                177                 :                :     /*
                                178                 :                :      * If we haven't yet performed the underlying index scan, do it, and begin
                                179                 :                :      * the iteration over the bitmap.
                                180                 :                :      */
 3505 rhaas@postgresql.org      181         [ +  + ]:        3600113 :     if (!node->initialized)
  620 melanieplageman@gmai      182                 :          14532 :         BitmapTableScanSetup(node);
                                183                 :                : 
  576                           184         [ +  + ]:        4100828 :     while (table_scan_bitmap_next_tuple(node->ss.ss_currentScanDesc,
                                185                 :                :                                         slot, &node->recheck,
                                186                 :                :                                         &node->stats.lossy_pages,
                                187                 :                :                                         &node->stats.exact_pages))
                                188                 :                :     {
                                189                 :                :         /*
                                190                 :                :          * Continuing in previously obtained page.
                                191                 :                :          */
                                192         [ +  + ]:        4086551 :         CHECK_FOR_INTERRUPTS();
                                193                 :                : 
                                194                 :                :         /*
                                195                 :                :          * If we are using lossy info, we have to recheck the qual conditions
                                196                 :                :          * at every tuple.
                                197                 :                :          */
                                198         [ +  + ]:        4086551 :         if (node->recheck)
                                199                 :                :         {
                                200                 :        1889299 :             econtext->ecxt_scantuple = slot;
                                201         [ +  + ]:        1889299 :             if (!ExecQualAndReset(node->bitmapqualorig, econtext))
                                202                 :                :             {
                                203                 :                :                 /* Fails recheck, so drop it and loop back for another */
                                204         [ -  + ]:         500715 :                 InstrCountFiltered2(node, 1);
                                205                 :         500715 :                 ExecClearTuple(slot);
                                206                 :         500715 :                 continue;
                                207                 :                :             }
                                208                 :                :         }
                                209                 :                : 
                                210                 :                :         /* OK to return this tuple */
                                211                 :        3585836 :         return slot;
                                212                 :                :     }
                                213                 :                : 
                                214                 :                :     /*
                                215                 :                :      * if we get here it means we are at the end of the scan..
                                216                 :                :      */
 7625 tgl@sss.pgh.pa.us         217                 :          14274 :     return ExecClearTuple(slot);
                                218                 :                : }
                                219                 :                : 
                                220                 :                : /*
                                221                 :                :  *  BitmapDoneInitializingSharedState - Shared state is initialized
                                222                 :                :  *
                                223                 :                :  *  By this time the leader has already populated the TBM and initialized the
                                224                 :                :  *  shared state so wake up other processes.
                                225                 :                :  */
                                226                 :                : static inline void
 3505 rhaas@postgresql.org      227                 :             48 : BitmapDoneInitializingSharedState(ParallelBitmapHeapState *pstate)
                                228                 :                : {
   20 nathan@postgresql.or      229                 :GNC          48 :     pg_atomic_write_membarrier_u32(&pstate->state, BM_FINISHED);
 3505 rhaas@postgresql.org      230                 :CBC          48 :     ConditionVariableBroadcast(&pstate->cv);
                                231                 :             48 : }
                                232                 :                : 
                                233                 :                : /*
                                234                 :                :  * BitmapHeapRecheck -- access method routine to recheck a tuple in EvalPlanQual
                                235                 :                :  */
                                236                 :                : static bool
 6195 tgl@sss.pgh.pa.us         237                 :UBC           0 : BitmapHeapRecheck(BitmapHeapScanState *node, TupleTableSlot *slot)
                                238                 :                : {
                                239                 :                :     ExprContext *econtext;
                                240                 :                : 
                                241                 :                :     /*
                                242                 :                :      * extract necessary information from index scan node
                                243                 :                :      */
                                244                 :              0 :     econtext = node->ss.ps.ps_ExprContext;
                                245                 :                : 
                                246                 :                :     /* Does the tuple meet the original qual conditions? */
                                247                 :              0 :     econtext->ecxt_scantuple = slot;
 3178 andres@anarazel.de        248                 :              0 :     return ExecQualAndReset(node->bitmapqualorig, econtext);
                                249                 :                : }
                                250                 :                : 
                                251                 :                : /* ----------------------------------------------------------------
                                252                 :                :  *      ExecBitmapHeapScan(node)
                                253                 :                :  * ----------------------------------------------------------------
                                254                 :                :  */
                                255                 :                : static TupleTableSlot *
 3374 andres@anarazel.de        256                 :CBC     3441123 : ExecBitmapHeapScan(PlanState *pstate)
                                257                 :                : {
                                258                 :        3441123 :     BitmapHeapScanState *node = castNode(BitmapHeapScanState, pstate);
                                259                 :                : 
 6195 tgl@sss.pgh.pa.us         260                 :        3441123 :     return ExecScan(&node->ss,
                                261                 :                :                     (ExecScanAccessMtd) BitmapHeapNext,
                                262                 :                :                     (ExecScanRecheckMtd) BitmapHeapRecheck);
                                263                 :                : }
                                264                 :                : 
                                265                 :                : /* ----------------------------------------------------------------
                                266                 :                :  *      ExecReScanBitmapHeapScan(node)
                                267                 :                :  * ----------------------------------------------------------------
                                268                 :                :  */
                                269                 :                : void
 5936                           270                 :           2764 : ExecReScanBitmapHeapScan(BitmapHeapScanState *node)
                                271                 :                : {
 4160 bruce@momjian.us          272                 :           2764 :     PlanState  *outerPlan = outerPlanState(node);
                                273                 :                : 
  717 melanieplageman@gmai      274                 :           2764 :     TableScanDesc scan = node->ss.ss_currentScanDesc;
                                275                 :                : 
                                276         [ +  + ]:           2764 :     if (scan)
                                277                 :                :     {
                                278                 :                :         /*
                                279                 :                :          * End iteration on iterators saved in scan descriptor if they have
                                280                 :                :          * not already been cleaned up.
                                281                 :                :          */
  662                           282         [ +  + ]:           2166 :         if (!tbm_exhausted(&scan->st.rs_tbmiterator))
                                283                 :           2162 :             tbm_end_iterate(&scan->st.rs_tbmiterator);
                                284                 :                : 
                                285                 :                :         /* rescan to release any page pin */
  919 tomas.vondra@postgre      286                 :           2166 :         table_rescan(node->ss.ss_currentScanDesc, NULL);
                                287                 :                :     }
                                288                 :                : 
                                289                 :                :     /* release bitmaps and buffers if any */
 7846 tgl@sss.pgh.pa.us         290         [ +  + ]:           2764 :     if (node->tbm)
                                291                 :           2162 :         tbm_free(node->tbm);
                                292                 :           2764 :     node->tbm = NULL;
 3505 rhaas@postgresql.org      293                 :           2764 :     node->initialized = false;
  717 melanieplageman@gmai      294                 :           2764 :     node->recheck = true;
                                295                 :                : 
 6195 tgl@sss.pgh.pa.us         296                 :           2764 :     ExecScanReScan(&node->ss);
                                297                 :                : 
                                298                 :                :     /*
                                299                 :                :      * if chgParam of subnode is not null then plan will be re-scanned by
                                300                 :                :      * first ExecProcNode.
                                301                 :                :      */
 4179 rhaas@postgresql.org      302         [ +  + ]:           2764 :     if (outerPlan->chgParam == NULL)
                                303                 :            184 :         ExecReScan(outerPlan);
 7846 tgl@sss.pgh.pa.us         304                 :           2764 : }
                                305                 :                : 
                                306                 :                : /* ----------------------------------------------------------------
                                307                 :                :  *      ExecEndBitmapHeapScan
                                308                 :                :  * ----------------------------------------------------------------
                                309                 :                :  */
                                310                 :                : void
                                311                 :          15296 : ExecEndBitmapHeapScan(BitmapHeapScanState *node)
                                312                 :                : {
                                313                 :                :     TableScanDesc scanDesc;
                                314                 :                : 
                                315                 :                :     /*
                                316                 :                :      * When ending a parallel worker, copy the statistics gathered by the
                                317                 :                :      * worker back into shared memory so that it can be picked up by the main
                                318                 :                :      * process to report in EXPLAIN ANALYZE.
                                319                 :                :      */
  825 drowley@postgresql.o      320   [ -  +  -  - ]:          15296 :     if (node->sinstrument != NULL && IsParallelWorker())
                                321                 :                :     {
                                322                 :                :         BitmapHeapScanInstrumentation *si;
                                323                 :                : 
  212 tomas.vondra@postgre      324         [ #  # ]:UBC           0 :         Assert(ParallelWorkerNumber < node->sinstrument->num_workers);
  825 drowley@postgresql.o      325                 :              0 :         si = &node->sinstrument->sinstrument[ParallelWorkerNumber];
                                326                 :                : 
                                327                 :                :         /*
                                328                 :                :          * Here we accumulate the stats rather than performing memcpy on
                                329                 :                :          * node->stats into si.  When a Gather/GatherMerge node finishes it
                                330                 :                :          * will perform planner shutdown on the workers.  On rescan it will
                                331                 :                :          * spin up new workers which will have a new BitmapHeapScanState and
                                332                 :                :          * zeroed stats.
                                333                 :                :          */
                                334                 :              0 :         si->exact_pages += node->stats.exact_pages;
                                335                 :              0 :         si->lossy_pages += node->stats.lossy_pages;
                                336                 :                : 
                                337                 :                :         /* collect I/O instrumentation for this process */
  188 tomas.vondra@postgre      338         [ #  # ]:              0 :         if (node->ss.ss_currentScanDesc &&
                                339         [ #  # ]:              0 :             node->ss.ss_currentScanDesc->rs_instrument)
                                340                 :                :         {
                                341                 :              0 :             AccumulateIOStats(&si->stats.io,
                                342                 :              0 :                               &node->ss.ss_currentScanDesc->rs_instrument->io);
                                343                 :                :         }
                                344                 :                :     }
                                345                 :                : 
                                346                 :                :     /*
                                347                 :                :      * extract information from the node
                                348                 :                :      */
 7846 tgl@sss.pgh.pa.us         349                 :CBC       15296 :     scanDesc = node->ss.ss_currentScanDesc;
                                350                 :                : 
                                351                 :                :     /*
                                352                 :                :      * close down subplans
                                353                 :                :      */
                                354                 :          15296 :     ExecEndNode(outerPlanState(node));
                                355                 :                : 
  717 melanieplageman@gmai      356         [ +  + ]:          15296 :     if (scanDesc)
                                357                 :                :     {
                                358                 :                :         /*
                                359                 :                :          * End iteration on iterators saved in scan descriptor if they have
                                360                 :                :          * not already been cleaned up.
                                361                 :                :          */
  662                           362         [ +  - ]:          12289 :         if (!tbm_exhausted(&scanDesc->st.rs_tbmiterator))
                                363                 :          12289 :             tbm_end_iterate(&scanDesc->st.rs_tbmiterator);
                                364                 :                : 
                                365                 :                :         /*
                                366                 :                :          * close table scan
                                367                 :                :          */
  717                           368                 :          12289 :         table_endscan(scanDesc);
                                369                 :                :     }
                                370                 :                : 
                                371                 :                :     /*
                                372                 :                :      * release bitmaps and buffers if any
                                373                 :                :      */
 7846 tgl@sss.pgh.pa.us         374         [ +  + ]:          15296 :     if (node->tbm)
                                375                 :          12109 :         tbm_free(node->tbm);
                                376                 :          15296 : }
                                377                 :                : 
                                378                 :                : /* ----------------------------------------------------------------
                                379                 :                :  *      ExecInitBitmapHeapScan
                                380                 :                :  *
                                381                 :                :  *      Initializes the scan's state information.
                                382                 :                :  * ----------------------------------------------------------------
                                383                 :                :  */
                                384                 :                : BitmapHeapScanState *
 7531                           385                 :          15377 : ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags)
                                386                 :                : {
                                387                 :                :     BitmapHeapScanState *scanstate;
                                388                 :                :     Relation    currentRelation;
                                389                 :                : 
                                390                 :                :     /* check for unsupported flags */
                                391         [ -  + ]:          15377 :     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
                                392                 :                : 
                                393                 :                :     /*
                                394                 :                :      * Assert caller didn't ask for an unsafe snapshot --- see comments at
                                395                 :                :      * head of file.
                                396                 :                :      */
 7625                           397         [ -  + ]:          15377 :     Assert(IsMVCCSnapshot(estate->es_snapshot));
                                398                 :                : 
                                399                 :                :     /*
                                400                 :                :      * create state structure
                                401                 :                :      */
 7846                           402                 :          15377 :     scanstate = makeNode(BitmapHeapScanState);
                                403                 :          15377 :     scanstate->ss.ps.plan = (Plan *) node;
                                404                 :          15377 :     scanstate->ss.ps.state = estate;
 3374 andres@anarazel.de        405                 :          15377 :     scanstate->ss.ps.ExecProcNode = ExecBitmapHeapScan;
                                406                 :                : 
 7846 tgl@sss.pgh.pa.us         407                 :          15377 :     scanstate->tbm = NULL;
                                408                 :                : 
                                409                 :                :     /* Zero the statistics counters */
  825 drowley@postgresql.o      410                 :          15377 :     memset(&scanstate->stats, 0, sizeof(BitmapHeapScanInstrumentation));
                                411                 :                : 
 3505 rhaas@postgresql.org      412                 :          15377 :     scanstate->initialized = false;
                                413                 :          15377 :     scanstate->pstate = NULL;
  717 melanieplageman@gmai      414                 :          15377 :     scanstate->recheck = true;
                                415                 :                : 
                                416                 :                :     /*
                                417                 :                :      * Miscellaneous initialization
                                418                 :                :      *
                                419                 :                :      * create expression context for node
                                420                 :                :      */
 7846 tgl@sss.pgh.pa.us         421                 :          15377 :     ExecAssignExprContext(estate, &scanstate->ss.ps);
                                422                 :                : 
                                423                 :                :     /*
                                424                 :                :      * open the scan relation
                                425                 :                :      */
 3160 andres@anarazel.de        426                 :          15377 :     currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
                                427                 :                : 
                                428                 :                :     /*
                                429                 :                :      * initialize child nodes
                                430                 :                :      */
                                431                 :          15377 :     outerPlanState(scanstate) = ExecInitNode(outerPlan(node), estate, eflags);
                                432                 :                : 
                                433                 :                :     /*
                                434                 :                :      * get the scan type from the relation descriptor.
                                435                 :                :      */
                                436                 :          15377 :     ExecInitScanTupleSlot(estate, &scanstate->ss,
                                437                 :                :                           RelationGetDescr(currentRelation),
                                438                 :                :                           table_slot_callbacks(currentRelation),
                                439                 :                :                           TTS_FLAG_OBEYS_NOT_NULL_CONSTRAINTS);
                                440                 :                : 
                                441                 :                :     /*
                                442                 :                :      * Initialize result type and projection.
                                443                 :                :      */
 2894                           444                 :          15377 :     ExecInitResultTypeTL(&scanstate->ss.ps);
 3160                           445                 :          15377 :     ExecAssignScanProjectionInfo(&scanstate->ss);
                                446                 :                : 
                                447                 :                :     /*
                                448                 :                :      * initialize child expressions
                                449                 :                :      */
                                450                 :          15377 :     scanstate->ss.ps.qual =
                                451                 :          15377 :         ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
                                452                 :          15377 :     scanstate->bitmapqualorig =
                                453                 :          15377 :         ExecInitQual(node->bitmapqualorig, (PlanState *) scanstate);
                                454                 :                : 
 7846 tgl@sss.pgh.pa.us         455                 :          15377 :     scanstate->ss.ss_currentRelation = currentRelation;
                                456                 :                : 
                                457                 :                :     /*
                                458                 :                :      * all done.
                                459                 :                :      */
                                460                 :          15377 :     return scanstate;
                                461                 :                : }
                                462                 :                : 
                                463                 :                : /*----------------
                                464                 :                :  *      BitmapShouldInitializeSharedState
                                465                 :                :  *
                                466                 :                :  *      The first process to come here and see the state to the BM_INITIAL
                                467                 :                :  *      will become the leader for the parallel bitmap scan and will be
                                468                 :                :  *      responsible for populating the TIDBitmap.  The other processes will
                                469                 :                :  *      be blocked by the condition variable until the leader wakes them up.
                                470                 :                :  * ---------------
                                471                 :                :  */
                                472                 :                : static bool
 3505 rhaas@postgresql.org      473                 :            228 : BitmapShouldInitializeSharedState(ParallelBitmapHeapState *pstate)
                                474                 :                : {
                                475                 :                :     uint32      state;
                                476                 :                : 
                                477                 :                :     while (1)
                                478                 :                :     {
   20 nathan@postgresql.or      479                 :GNC         260 :         state = BM_INITIAL;
                                480                 :            260 :         pg_atomic_compare_exchange_u32(&pstate->state, &state, BM_INPROGRESS);
                                481                 :                : 
                                482                 :                :         /* Exit if bitmap is done, or if we're the leader. */
 3505 rhaas@postgresql.org      483         [ +  + ]:CBC         260 :         if (state != BM_INPROGRESS)
                                484                 :            228 :             break;
                                485                 :                : 
                                486                 :                :         /* Wait for the leader to wake us up. */
                                487                 :             32 :         ConditionVariableSleep(&pstate->cv, WAIT_EVENT_PARALLEL_BITMAP_SCAN);
                                488                 :                :     }
                                489                 :                : 
                                490                 :            228 :     ConditionVariableCancelSleep();
                                491                 :                : 
                                492                 :            228 :     return (state == BM_INITIAL);
                                493                 :                : }
                                494                 :                : 
                                495                 :                : /* ----------------------------------------------------------------
                                496                 :                :  *      ExecBitmapHeapEstimate
                                497                 :                :  *
                                498                 :                :  *      Compute the amount of space we'll need in the parallel
                                499                 :                :  *      query DSM, and inform pcxt->estimator about our needs.
                                500                 :                :  * ----------------------------------------------------------------
                                501                 :                :  */
                                502                 :                : void
                                503                 :             12 : ExecBitmapHeapEstimate(BitmapHeapScanState *node,
                                504                 :                :                        ParallelContext *pcxt)
                                505                 :                : {
  188 tomas.vondra@postgre      506                 :             12 :     shm_toc_estimate_chunk(&pcxt->estimator,
                                507                 :                :                            MAXALIGN(sizeof(ParallelBitmapHeapState)));
 3505 rhaas@postgresql.org      508                 :             12 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                509                 :             12 : }
                                510                 :                : 
                                511                 :                : /* ----------------------------------------------------------------
                                512                 :                :  *      ExecBitmapHeapInitializeDSM
                                513                 :                :  *
                                514                 :                :  *      Set up a parallel bitmap heap scan descriptor.
                                515                 :                :  * ----------------------------------------------------------------
                                516                 :                :  */
                                517                 :                : void
                                518                 :             12 : ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node,
                                519                 :                :                             ParallelContext *pcxt)
                                520                 :                : {
                                521                 :                :     ParallelBitmapHeapState *pstate;
 3240                           522                 :             12 :     dsa_area   *dsa = node->ss.ps.state->es_query_dsa;
                                523                 :                : 
                                524                 :                :     /* If there's no DSA, there are no workers; initialize nothing. */
                                525         [ -  + ]:             12 :     if (dsa == NULL)
 3240 rhaas@postgresql.org      526                 :UBC           0 :         return;
                                527                 :                : 
                                528                 :                :     pstate = (ParallelBitmapHeapState *)
  188 tomas.vondra@postgre      529                 :CBC          12 :         shm_toc_allocate(pcxt->toc,
                                530                 :                :                          MAXALIGN(sizeof(ParallelBitmapHeapState)));
                                531                 :                : 
 3505 rhaas@postgresql.org      532                 :             12 :     pstate->tbmiterator = 0;
                                533                 :                : 
   20 nathan@postgresql.or      534                 :GNC          12 :     pg_atomic_init_u32(&pstate->state, BM_INITIAL);
                                535                 :                : 
 3505 rhaas@postgresql.org      536                 :CBC          12 :     ConditionVariableInit(&pstate->cv);
                                537                 :                : 
                                538                 :             12 :     shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, pstate);
                                539                 :             12 :     node->pstate = pstate;
                                540                 :                : }
                                541                 :                : 
                                542                 :                : /* ----------------------------------------------------------------
                                543                 :                :  *      ExecBitmapHeapReInitializeDSM
                                544                 :                :  *
                                545                 :                :  *      Reset shared state before beginning a fresh scan.
                                546                 :                :  * ----------------------------------------------------------------
                                547                 :                :  */
                                548                 :                : void
 3330 tgl@sss.pgh.pa.us         549                 :             36 : ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node,
                                550                 :                :                               ParallelContext *pcxt)
                                551                 :                : {
                                552                 :             36 :     ParallelBitmapHeapState *pstate = node->pstate;
                                553                 :             36 :     dsa_area   *dsa = node->ss.ps.state->es_query_dsa;
                                554                 :                : 
                                555                 :                :     /* If there's no DSA, there are no workers; do nothing. */
 3240 rhaas@postgresql.org      556         [ -  + ]:             36 :     if (dsa == NULL)
 3240 rhaas@postgresql.org      557                 :UBC           0 :         return;
                                558                 :                : 
   20 nathan@postgresql.or      559                 :GNC          36 :     pg_atomic_write_u32(&pstate->state, BM_INITIAL);
                                560                 :                : 
 3330 tgl@sss.pgh.pa.us         561         [ +  - ]:CBC          36 :     if (DsaPointerIsValid(pstate->tbmiterator))
                                562                 :             36 :         tbm_free_shared_area(dsa, pstate->tbmiterator);
                                563                 :                : 
                                564                 :             36 :     pstate->tbmiterator = InvalidDsaPointer;
                                565                 :                : }
                                566                 :                : 
                                567                 :                : /* ----------------------------------------------------------------
                                568                 :                :  *      ExecBitmapHeapInitializeWorker
                                569                 :                :  *
                                570                 :                :  *      Copy relevant information from TOC into planstate.
                                571                 :                :  * ----------------------------------------------------------------
                                572                 :                :  */
                                573                 :                : void
 3252 andres@anarazel.de        574                 :            180 : ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node,
                                575                 :                :                                ParallelWorkerContext *pwcxt)
                                576                 :                : {
 3240 rhaas@postgresql.org      577         [ -  + ]:            180 :     Assert(node->ss.ps.state->es_query_dsa != NULL);
                                578                 :                : 
  188 tomas.vondra@postgre      579                 :            180 :     node->pstate = (ParallelBitmapHeapState *)
                                580                 :            180 :         shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
                                581                 :            180 : }
                                582                 :                : 
                                583                 :                : /*
                                584                 :                :  * Compute the amount of space we'll need for the shared instrumentation and
                                585                 :                :  * inform pcxt->estimator.
                                586                 :                :  */
                                587                 :                : void
                                588                 :             13 : ExecBitmapHeapInstrumentEstimate(BitmapHeapScanState *node,
                                589                 :                :                                  ParallelContext *pcxt)
                                590                 :                : {
                                591                 :                :     Size        size;
                                592                 :                : 
                                593   [ -  +  -  - ]:             13 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
                                594                 :             13 :         return;
                                595                 :                : 
  188 tomas.vondra@postgre      596                 :UBC           0 :     size = add_size(offsetof(SharedBitmapHeapInstrumentation, sinstrument),
                                597                 :              0 :                     mul_size(pcxt->nworkers, sizeof(BitmapHeapScanInstrumentation)));
                                598                 :              0 :     shm_toc_estimate_chunk(&pcxt->estimator, size);
                                599                 :              0 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                600                 :                : }
                                601                 :                : 
                                602                 :                : /*
                                603                 :                :  * Set up parallel bitmap heap scan instrumentation.
                                604                 :                :  */
                                605                 :                : void
  188 tomas.vondra@postgre      606                 :CBC          13 : ExecBitmapHeapInstrumentInitDSM(BitmapHeapScanState *node,
                                607                 :                :                                 ParallelContext *pcxt)
                                608                 :                : {
                                609                 :                :     Size        size;
                                610                 :                : 
                                611   [ -  +  -  - ]:             13 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
                                612                 :             13 :         return;
                                613                 :                : 
  188 tomas.vondra@postgre      614                 :UBC           0 :     size = add_size(offsetof(SharedBitmapHeapInstrumentation, sinstrument),
                                615                 :              0 :                     mul_size(pcxt->nworkers, sizeof(BitmapHeapScanInstrumentation)));
                                616                 :              0 :     node->sinstrument =
                                617                 :              0 :         (SharedBitmapHeapInstrumentation *) shm_toc_allocate(pcxt->toc, size);
                                618                 :                : 
                                619                 :                :     /* Each per-worker area must start out as zeroes */
                                620                 :              0 :     memset(node->sinstrument, 0, size);
                                621                 :              0 :     node->sinstrument->num_workers = pcxt->nworkers;
                                622                 :              0 :     shm_toc_insert(pcxt->toc,
                                623                 :              0 :                    node->ss.ps.plan->plan_node_id +
                                624                 :                :                    PARALLEL_KEY_SCAN_INSTRUMENT_OFFSET,
                                625                 :              0 :                    node->sinstrument);
                                626                 :                : }
                                627                 :                : 
                                628                 :                : /*
                                629                 :                :  * Look up and save the location of the shared instrumentation.
                                630                 :                :  */
                                631                 :                : void
  188 tomas.vondra@postgre      632                 :CBC         181 : ExecBitmapHeapInstrumentInitWorker(BitmapHeapScanState *node,
                                633                 :                :                                    ParallelWorkerContext *pwcxt)
                                634                 :                : {
                                635         [ +  - ]:            181 :     if (!node->ss.ps.instrument)
                                636                 :            181 :         return;
                                637                 :                : 
  188 tomas.vondra@postgre      638                 :UBC           0 :     node->sinstrument = (SharedBitmapHeapInstrumentation *)
                                639                 :              0 :         shm_toc_lookup(pwcxt->toc,
                                640                 :              0 :                        node->ss.ps.plan->plan_node_id +
                                641                 :                :                        PARALLEL_KEY_SCAN_INSTRUMENT_OFFSET,
                                642                 :                :                        false);
                                643                 :                : }
                                644                 :                : 
                                645                 :                : /* ----------------------------------------------------------------
                                646                 :                :  *      ExecBitmapHeapRetrieveInstrumentation
                                647                 :                :  *
                                648                 :                :  *      Transfer bitmap heap scan statistics from DSM to private memory.
                                649                 :                :  * ----------------------------------------------------------------
                                650                 :                :  */
                                651                 :                : void
  825 drowley@postgresql.o      652                 :              0 : ExecBitmapHeapRetrieveInstrumentation(BitmapHeapScanState *node)
                                653                 :                : {
                                654                 :              0 :     SharedBitmapHeapInstrumentation *sinstrument = node->sinstrument;
                                655                 :                :     Size        size;
                                656                 :                : 
                                657         [ #  # ]:              0 :     if (sinstrument == NULL)
                                658                 :              0 :         return;
                                659                 :                : 
                                660                 :              0 :     size = offsetof(SharedBitmapHeapInstrumentation, sinstrument)
                                661                 :              0 :         + sinstrument->num_workers * sizeof(BitmapHeapScanInstrumentation);
                                662                 :                : 
                                663                 :              0 :     node->sinstrument = palloc(size);
                                664                 :              0 :     memcpy(node->sinstrument, sinstrument, size);
                                665                 :                : }
        

Generated by: LCOV version 2.0-1