LCOV - code coverage report
Current view: top level - src/backend/nodes - tidbitmap.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 88.4 % 498 440
Test Date: 2026-12-05 20:16:09 Functions: 93.9 % 33 31
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 70.9 % 292 207

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * tidbitmap.c
       4                 :             :  *    PostgreSQL tuple-id (TID) bitmap package
       5                 :             :  *
       6                 :             :  * This module provides bitmap data structures that are spiritually
       7                 :             :  * similar to Bitmapsets, but are specially adapted to store sets of
       8                 :             :  * tuple identifiers (TIDs), or ItemPointers.  In particular, the division
       9                 :             :  * of an ItemPointer into BlockNumber and OffsetNumber is catered for.
      10                 :             :  * Also, since we wish to be able to store very large tuple sets in
      11                 :             :  * memory with this data structure, we support "lossy" storage, in which
      12                 :             :  * we no longer remember individual tuple offsets on a page but only the
      13                 :             :  * fact that a particular page needs to be visited.
      14                 :             :  *
      15                 :             :  * The "lossy" storage uses one bit per disk page, so at the standard 8K
      16                 :             :  * BLCKSZ, we can represent all pages in 64Gb of disk space in about 1Mb
      17                 :             :  * of memory.  People pushing around tables of that size should have a
      18                 :             :  * couple of Mb to spare, so we don't worry about providing a second level
      19                 :             :  * of lossiness.  In theory we could fall back to page ranges at some
      20                 :             :  * point, but for now that seems useless complexity.
      21                 :             :  *
      22                 :             :  * We also support the notion of candidate matches, or rechecking.  This
      23                 :             :  * means we know that a search need visit only some tuples on a page,
      24                 :             :  * but we are not certain that all of those tuples are real matches.
      25                 :             :  * So the eventual heap scan must recheck the quals for these tuples only,
      26                 :             :  * rather than rechecking the quals for all tuples on the page as in the
      27                 :             :  * lossy-bitmap case.  Rechecking can be specified when TIDs are inserted
      28                 :             :  * into a bitmap, and it can also happen internally when we AND a lossy
      29                 :             :  * and a non-lossy page.
      30                 :             :  *
      31                 :             :  *
      32                 :             :  * Copyright (c) 2003-2026, PostgreSQL Global Development Group
      33                 :             :  *
      34                 :             :  * IDENTIFICATION
      35                 :             :  *    src/backend/nodes/tidbitmap.c
      36                 :             :  *
      37                 :             :  *-------------------------------------------------------------------------
      38                 :             :  */
      39                 :             : #include "postgres.h"
      40                 :             : 
      41                 :             : #include <limits.h>
      42                 :             : 
      43                 :             : #include "access/htup_details.h"
      44                 :             : #include "common/hashfn.h"
      45                 :             : #include "nodes/bitmapset.h"
      46                 :             : #include "nodes/tidbitmap.h"
      47                 :             : #include "storage/lwlock.h"
      48                 :             : #include "utils/dsa.h"
      49                 :             : 
      50                 :             : /*
      51                 :             :  * When we have to switch over to lossy storage, we use a data structure
      52                 :             :  * with one bit per page, where all pages having the same number DIV
      53                 :             :  * PAGES_PER_CHUNK are aggregated into one chunk.  When a chunk is present
      54                 :             :  * and has the bit set for a given page, there must not be a per-page entry
      55                 :             :  * for that page in the page table.
      56                 :             :  *
      57                 :             :  * We actually store both exact pages and lossy chunks in the same hash
      58                 :             :  * table, using identical data structures.  (This is because the memory
      59                 :             :  * management for hashtables doesn't easily/efficiently allow space to be
      60                 :             :  * transferred easily from one hashtable to another.)  Therefore it's best
      61                 :             :  * if PAGES_PER_CHUNK is the same as TBM_MAX_TUPLES_PER_PAGE, or at least not
      62                 :             :  * too different.  But we also want PAGES_PER_CHUNK to be a power of 2 to
      63                 :             :  * avoid expensive integer remainder operations.  So, define it like this:
      64                 :             :  */
      65                 :             : #define PAGES_PER_CHUNK  (BLCKSZ / 32)
      66                 :             : 
      67                 :             : /* We use BITS_PER_BITMAPWORD and typedef bitmapword from nodes/bitmapset.h */
      68                 :             : 
      69                 :             : #define WORDNUM(x)  ((x) / BITS_PER_BITMAPWORD)
      70                 :             : #define BITNUM(x)   ((x) % BITS_PER_BITMAPWORD)
      71                 :             : 
      72                 :             : /* number of active words for an exact page: */
      73                 :             : #define WORDS_PER_PAGE  ((TBM_MAX_TUPLES_PER_PAGE - 1) / BITS_PER_BITMAPWORD + 1)
      74                 :             : /* number of active words for a lossy chunk: */
      75                 :             : #define WORDS_PER_CHUNK  ((PAGES_PER_CHUNK - 1) / BITS_PER_BITMAPWORD + 1)
      76                 :             : 
      77                 :             : /*
      78                 :             :  * The hashtable entries are represented by this data structure.  For
      79                 :             :  * an exact page, blockno is the page number and bit k of the bitmap
      80                 :             :  * represents tuple offset k+1.  For a lossy chunk, blockno is the first
      81                 :             :  * page in the chunk (this must be a multiple of PAGES_PER_CHUNK) and
      82                 :             :  * bit k represents page blockno+k.  Note that it is not possible to
      83                 :             :  * have exact storage for the first page of a chunk if we are using
      84                 :             :  * lossy storage for any page in the chunk's range, since the same
      85                 :             :  * hashtable entry has to serve both purposes.
      86                 :             :  *
      87                 :             :  * recheck is used only on exact pages --- it indicates that although
      88                 :             :  * only the stated tuples need be checked, the full index qual condition
      89                 :             :  * must be checked for each (ie, these are candidate matches).
      90                 :             :  */
      91                 :             : typedef struct PagetableEntry
      92                 :             : {
      93                 :             :     BlockNumber blockno;        /* page number (hashtable key) */
      94                 :             :     char        status;         /* hash entry status */
      95                 :             :     bool        ischunk;        /* T = lossy storage, F = exact */
      96                 :             :     bool        recheck;        /* should the tuples be rechecked? */
      97                 :             :     bitmapword  words[Max(WORDS_PER_PAGE, WORDS_PER_CHUNK)];
      98                 :             : } PagetableEntry;
      99                 :             : 
     100                 :             : /*
     101                 :             :  * Holds array of pagetable entries.
     102                 :             :  */
     103                 :             : typedef struct PTEntryArray
     104                 :             : {
     105                 :             :     pg_atomic_uint32 refcount;  /* no. of iterator attached */
     106                 :             :     PagetableEntry ptentry[FLEXIBLE_ARRAY_MEMBER];
     107                 :             : } PTEntryArray;
     108                 :             : 
     109                 :             : /*
     110                 :             :  * We want to avoid the overhead of creating the hashtable, which is
     111                 :             :  * comparatively large, when not necessary. Particularly when we are using a
     112                 :             :  * bitmap scan on the inside of a nestloop join: a bitmap may well live only
     113                 :             :  * long enough to accumulate one entry in such cases.  We therefore avoid
     114                 :             :  * creating an actual hashtable until we need two pagetable entries.  When
     115                 :             :  * just one pagetable entry is needed, we store it in a fixed field of
     116                 :             :  * TIDBitMap.  (NOTE: we don't get rid of the hashtable if the bitmap later
     117                 :             :  * shrinks down to zero or one page again.  So, status can be TBM_HASH even
     118                 :             :  * when nentries is zero or one.)
     119                 :             :  */
     120                 :             : typedef enum
     121                 :             : {
     122                 :             :     TBM_EMPTY,                  /* no hashtable, nentries == 0 */
     123                 :             :     TBM_ONE_PAGE,               /* entry1 contains the single entry */
     124                 :             :     TBM_HASH,                   /* pagetable is valid, entry1 is not */
     125                 :             : } TBMStatus;
     126                 :             : 
     127                 :             : /*
     128                 :             :  * Current iterating state of the TBM.
     129                 :             :  */
     130                 :             : typedef enum
     131                 :             : {
     132                 :             :     TBM_NOT_ITERATING,          /* not yet converted to page and chunk array */
     133                 :             :     TBM_ITERATING_PRIVATE,      /* converted to local page and chunk array */
     134                 :             :     TBM_ITERATING_SHARED,       /* converted to shared page and chunk array */
     135                 :             : } TBMIteratingState;
     136                 :             : 
     137                 :             : /*
     138                 :             :  * Here is the representation for a whole TIDBitMap:
     139                 :             :  */
     140                 :             : struct TIDBitmap
     141                 :             : {
     142                 :             :     NodeTag     type;           /* to make it a valid Node */
     143                 :             :     MemoryContext mcxt;         /* memory context containing me */
     144                 :             :     TBMStatus   status;         /* see codes above */
     145                 :             :     struct pagetable_hash *pagetable;   /* hash table of PagetableEntry's */
     146                 :             :     int         nentries;       /* number of entries in pagetable */
     147                 :             :     int         maxentries;     /* limit on same to meet maxbytes */
     148                 :             :     int         npages;         /* number of exact entries in pagetable */
     149                 :             :     int         nchunks;        /* number of lossy entries in pagetable */
     150                 :             :     TBMIteratingState iterating;    /* tbm_begin_iterate called? */
     151                 :             :     uint32      lossify_start;  /* offset to start lossifying hashtable at */
     152                 :             :     PagetableEntry entry1;      /* used when status == TBM_ONE_PAGE */
     153                 :             :     /* these are valid when iterating is true: */
     154                 :             :     PagetableEntry **spages;    /* sorted exact-page list, or NULL */
     155                 :             :     PagetableEntry **schunks;   /* sorted lossy-chunk list, or NULL */
     156                 :             :     dsa_pointer dsapagetable;   /* dsa_pointer to the element array */
     157                 :             :     dsa_pointer dsapagetableold;    /* dsa_pointer to the old element array */
     158                 :             :     dsa_pointer ptpages;        /* dsa_pointer to the page array */
     159                 :             :     dsa_pointer ptchunks;       /* dsa_pointer to the chunk array */
     160                 :             :     dsa_area   *dsa;            /* reference to per-query dsa area */
     161                 :             : };
     162                 :             : 
     163                 :             : /*
     164                 :             :  * When iterating over a backend-local bitmap in sorted order, a
     165                 :             :  * TBMPrivateIterator is used to track our progress.  There can be several
     166                 :             :  * iterators scanning the same bitmap concurrently.  Note that the bitmap
     167                 :             :  * becomes read-only as soon as any iterator is created.
     168                 :             :  */
     169                 :             : struct TBMPrivateIterator
     170                 :             : {
     171                 :             :     TIDBitmap  *tbm;            /* TIDBitmap we're iterating over */
     172                 :             :     int         spageptr;       /* next spages index */
     173                 :             :     int         schunkptr;      /* next schunks index */
     174                 :             :     int         schunkbit;      /* next bit to check in current schunk */
     175                 :             : };
     176                 :             : 
     177                 :             : /*
     178                 :             :  * Holds the shared members of the iterator so that multiple processes
     179                 :             :  * can jointly iterate.
     180                 :             :  */
     181                 :             : typedef struct TBMSharedIteratorState
     182                 :             : {
     183                 :             :     int         nentries;       /* number of entries in pagetable */
     184                 :             :     int         maxentries;     /* limit on same to meet maxbytes */
     185                 :             :     int         npages;         /* number of exact entries in pagetable */
     186                 :             :     int         nchunks;        /* number of lossy entries in pagetable */
     187                 :             :     dsa_pointer pagetable;      /* dsa pointers to head of pagetable data */
     188                 :             :     dsa_pointer spages;         /* dsa pointer to page array */
     189                 :             :     dsa_pointer schunks;        /* dsa pointer to chunk array */
     190                 :             :     LWLock      lock;           /* lock to protect below members */
     191                 :             :     int         spageptr;       /* next spages index */
     192                 :             :     int         schunkptr;      /* next schunks index */
     193                 :             :     int         schunkbit;      /* next bit to check in current schunk */
     194                 :             : } TBMSharedIteratorState;
     195                 :             : 
     196                 :             : /*
     197                 :             :  * pagetable iteration array.
     198                 :             :  */
     199                 :             : typedef struct PTIterationArray
     200                 :             : {
     201                 :             :     pg_atomic_uint32 refcount;  /* no. of iterator attached */
     202                 :             :     int         index[FLEXIBLE_ARRAY_MEMBER];   /* index array */
     203                 :             : } PTIterationArray;
     204                 :             : 
     205                 :             : /*
     206                 :             :  * same as TBMPrivateIterator, but it is used for joint iteration, therefore
     207                 :             :  * this also holds a reference to the shared state.
     208                 :             :  */
     209                 :             : struct TBMSharedIterator
     210                 :             : {
     211                 :             :     TBMSharedIteratorState *state;  /* shared state */
     212                 :             :     PTEntryArray *ptbase;       /* pagetable element array */
     213                 :             :     PTIterationArray *ptpages;  /* sorted exact page index list */
     214                 :             :     PTIterationArray *ptchunks; /* sorted lossy page index list */
     215                 :             : };
     216                 :             : 
     217                 :             : /* Local function prototypes */
     218                 :             : static void tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage);
     219                 :             : static bool tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage,
     220                 :             :                                const TIDBitmap *b);
     221                 :             : static const PagetableEntry *tbm_find_pageentry(const TIDBitmap *tbm,
     222                 :             :                                                 BlockNumber pageno);
     223                 :             : static PagetableEntry *tbm_get_pageentry(TIDBitmap *tbm, BlockNumber pageno);
     224                 :             : static bool tbm_page_is_lossy(const TIDBitmap *tbm, BlockNumber pageno);
     225                 :             : static void tbm_mark_page_lossy(TIDBitmap *tbm, BlockNumber pageno);
     226                 :             : static void tbm_lossify(TIDBitmap *tbm);
     227                 :             : static inline int tbm_comparator(const PagetableEntry *left,
     228                 :             :                                  const PagetableEntry *right);
     229                 :             : static inline int tbm_shared_comparator(const int *left, const int *right,
     230                 :             :                                         const PagetableEntry *base);
     231                 :             : 
     232                 :             : /* define hashtable mapping block numbers to PagetableEntry's */
     233                 :             : #define SH_USE_NONDEFAULT_ALLOCATOR
     234                 :             : #define SH_PREFIX pagetable
     235                 :             : #define SH_ELEMENT_TYPE PagetableEntry
     236                 :             : #define SH_KEY_TYPE BlockNumber
     237                 :             : #define SH_KEY blockno
     238                 :             : #define SH_HASH_KEY(tb, key) murmurhash32(key)
     239                 :             : #define SH_EQUAL(tb, a, b) a == b
     240                 :             : #define SH_SCOPE static inline
     241                 :             : #define SH_DEFINE
     242                 :             : #define SH_DECLARE
     243                 :             : #include "lib/simplehash.h"
     244                 :             : 
     245                 :             : #define ST_SORT qsort_pagetable
     246                 :             : #define ST_ELEMENT_TYPE PagetableEntry *
     247                 :             : #define ST_COMPARE(a, b) tbm_comparator(*(a), *(b))
     248                 :             : #define ST_SCOPE static
     249                 :             : #define ST_DEFINE
     250                 :             : #include "lib/sort_template.h"
     251                 :             : 
     252                 :             : /*
     253                 :             :  * The shared-iteration arrays contain indexes into the pagetable entry
     254                 :             :  * array rather than pointers, so their comparator needs the base address
     255                 :             :  * of the entry array passed through as an argument.
     256                 :             :  */
     257                 :             : #define ST_SORT qsort_pagetable_shared
     258                 :             : #define ST_ELEMENT_TYPE int
     259                 :             : #define ST_COMPARE(a, b, arg) tbm_shared_comparator(a, b, arg)
     260                 :             : #define ST_COMPARE_ARG_TYPE PagetableEntry
     261                 :             : #define ST_SCOPE static
     262                 :             : #define ST_DEFINE
     263                 :             : #include "lib/sort_template.h"
     264                 :             : 
     265                 :             : /*
     266                 :             :  * tbm_create - create an initially-empty bitmap
     267                 :             :  *
     268                 :             :  * The bitmap will live in the memory context that is CurrentMemoryContext
     269                 :             :  * at the time of this call.  It will be limited to (approximately) maxbytes
     270                 :             :  * total memory consumption.  If the DSA passed to this function is not NULL
     271                 :             :  * then the memory for storing elements of the underlying page table will
     272                 :             :  * be allocated from the DSA.
     273                 :             :  */
     274                 :             : TIDBitmap *
     275                 :       15842 : tbm_create(Size maxbytes, dsa_area *dsa)
     276                 :             : {
     277                 :             :     TIDBitmap  *tbm;
     278                 :             : 
     279                 :             :     /* Create the TIDBitmap struct and zero all its fields */
     280                 :       15842 :     tbm = makeNode(TIDBitmap);
     281                 :             : 
     282                 :       15842 :     tbm->mcxt = CurrentMemoryContext;
     283                 :       15842 :     tbm->status = TBM_EMPTY;
     284                 :             : 
     285                 :       15842 :     tbm->maxentries = tbm_calculate_entries(maxbytes);
     286                 :       15842 :     tbm->lossify_start = 0;
     287                 :       15842 :     tbm->dsa = dsa;
     288                 :       15842 :     tbm->dsapagetable = InvalidDsaPointer;
     289                 :       15842 :     tbm->dsapagetableold = InvalidDsaPointer;
     290                 :       15842 :     tbm->ptpages = InvalidDsaPointer;
     291                 :       15842 :     tbm->ptchunks = InvalidDsaPointer;
     292                 :             : 
     293                 :       15842 :     return tbm;
     294                 :             : }
     295                 :             : 
     296                 :             : /*
     297                 :             :  * Actually create the hashtable.  Since this is a moderately expensive
     298                 :             :  * proposition, we don't do it until we have to.
     299                 :             :  */
     300                 :             : static void
     301                 :        5553 : tbm_create_pagetable(TIDBitmap *tbm)
     302                 :             : {
     303                 :             :     Assert(tbm->status != TBM_HASH);
     304                 :             :     Assert(tbm->pagetable == NULL);
     305                 :             : 
     306                 :        5553 :     tbm->pagetable = pagetable_create(tbm->mcxt, 128, tbm);
     307                 :             : 
     308                 :             :     /* If entry1 is valid, push it into the hashtable */
     309         [ +  + ]:        5553 :     if (tbm->status == TBM_ONE_PAGE)
     310                 :             :     {
     311                 :             :         PagetableEntry *page;
     312                 :             :         bool        found;
     313                 :             :         char        oldstatus;
     314                 :             : 
     315                 :        3641 :         page = pagetable_insert(tbm->pagetable,
     316                 :             :                                 tbm->entry1.blockno,
     317                 :             :                                 &found);
     318                 :             :         Assert(!found);
     319                 :        3641 :         oldstatus = page->status;
     320                 :        3641 :         memcpy(page, &tbm->entry1, sizeof(PagetableEntry));
     321                 :        3641 :         page->status = oldstatus;
     322                 :             :     }
     323                 :             : 
     324                 :        5553 :     tbm->status = TBM_HASH;
     325                 :        5553 : }
     326                 :             : 
     327                 :             : /*
     328                 :             :  * tbm_free - free a TIDBitmap
     329                 :             :  */
     330                 :             : void
     331                 :       15761 : tbm_free(TIDBitmap *tbm)
     332                 :             : {
     333         [ +  + ]:       15761 :     if (tbm->pagetable)
     334                 :        5552 :         pagetable_destroy(tbm->pagetable);
     335         [ +  + ]:       15761 :     if (tbm->spages)
     336                 :        3466 :         pfree(tbm->spages);
     337         [ +  + ]:       15761 :     if (tbm->schunks)
     338                 :        1924 :         pfree(tbm->schunks);
     339                 :       15761 :     pfree(tbm);
     340                 :       15761 : }
     341                 :             : 
     342                 :             : /*
     343                 :             :  * tbm_free_shared_area - free shared state
     344                 :             :  *
     345                 :             :  * Free shared iterator state, Also free shared pagetable and iterator arrays
     346                 :             :  * memory if they are not referred by any of the shared iterator i.e recount
     347                 :             :  * is becomes 0.
     348                 :             :  */
     349                 :             : void
     350                 :          36 : tbm_free_shared_area(dsa_area *dsa, dsa_pointer dp)
     351                 :             : {
     352                 :          36 :     TBMSharedIteratorState *istate = dsa_get_address(dsa, dp);
     353                 :             :     PTEntryArray *ptbase;
     354                 :             :     PTIterationArray *ptpages;
     355                 :             :     PTIterationArray *ptchunks;
     356                 :             : 
     357         [ +  - ]:          36 :     if (DsaPointerIsValid(istate->pagetable))
     358                 :             :     {
     359                 :          36 :         ptbase = dsa_get_address(dsa, istate->pagetable);
     360         [ +  - ]:          36 :         if (pg_atomic_sub_fetch_u32(&ptbase->refcount, 1) == 0)
     361                 :          36 :             dsa_free(dsa, istate->pagetable);
     362                 :             :     }
     363         [ +  - ]:          36 :     if (DsaPointerIsValid(istate->spages))
     364                 :             :     {
     365                 :          36 :         ptpages = dsa_get_address(dsa, istate->spages);
     366         [ +  - ]:          36 :         if (pg_atomic_sub_fetch_u32(&ptpages->refcount, 1) == 0)
     367                 :          36 :             dsa_free(dsa, istate->spages);
     368                 :             :     }
     369         [ -  + ]:          36 :     if (DsaPointerIsValid(istate->schunks))
     370                 :             :     {
     371                 :           0 :         ptchunks = dsa_get_address(dsa, istate->schunks);
     372         [ #  # ]:           0 :         if (pg_atomic_sub_fetch_u32(&ptchunks->refcount, 1) == 0)
     373                 :           0 :             dsa_free(dsa, istate->schunks);
     374                 :             :     }
     375                 :             : 
     376                 :          36 :     dsa_free(dsa, dp);
     377                 :          36 : }
     378                 :             : 
     379                 :             : /*
     380                 :             :  * tbm_add_tuples - add some tuple IDs to a TIDBitmap
     381                 :             :  *
     382                 :             :  * If recheck is true, then the recheck flag will be set in the
     383                 :             :  * TBMIterateResult when any of these tuples are reported out.
     384                 :             :  */
     385                 :             : void
     386                 :     4072712 : tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids,
     387                 :             :                bool recheck)
     388                 :             : {
     389                 :     4072712 :     BlockNumber currblk = InvalidBlockNumber;
     390                 :     4072712 :     PagetableEntry *page = NULL;    /* only valid when currblk is valid */
     391                 :             : 
     392                 :             :     Assert(tbm->iterating == TBM_NOT_ITERATING);
     393         [ +  + ]:     9669292 :     for (int i = 0; i < ntids; i++)
     394                 :             :     {
     395                 :     5596580 :         BlockNumber blk = ItemPointerGetBlockNumber(tids + i);
     396                 :     5596580 :         OffsetNumber off = ItemPointerGetOffsetNumber(tids + i);
     397                 :             :         int         wordnum,
     398                 :             :                     bitnum;
     399                 :             : 
     400                 :             :         /* safety check to ensure we don't overrun bit array bounds */
     401   [ +  -  -  + ]:     5596580 :         if (off < 1 || off > TBM_MAX_TUPLES_PER_PAGE)
     402         [ #  # ]:           0 :             elog(ERROR, "tuple offset out of range: %u", off);
     403                 :             : 
     404                 :             :         /*
     405                 :             :          * Look up target page unless we already did.  This saves cycles when
     406                 :             :          * the input includes consecutive tuples on the same page, which is
     407                 :             :          * common enough to justify an extra test here.
     408                 :             :          */
     409         [ +  + ]:     5596580 :         if (blk != currblk)
     410                 :             :         {
     411         [ +  + ]:     4611411 :             if (tbm_page_is_lossy(tbm, blk))
     412                 :        1904 :                 page = NULL;    /* remember page is lossy */
     413                 :             :             else
     414                 :     4609507 :                 page = tbm_get_pageentry(tbm, blk);
     415                 :     4611411 :             currblk = blk;
     416                 :             :         }
     417                 :             : 
     418         [ +  + ]:     5596580 :         if (page == NULL)
     419                 :        1904 :             continue;           /* whole page is already marked */
     420                 :             : 
     421         [ -  + ]:     5594676 :         if (page->ischunk)
     422                 :             :         {
     423                 :             :             /* The page is a lossy chunk header, set bit for itself */
     424                 :           0 :             wordnum = bitnum = 0;
     425                 :             :         }
     426                 :             :         else
     427                 :             :         {
     428                 :             :             /* Page is exact, so set bit for individual tuple */
     429                 :     5594676 :             wordnum = WORDNUM(off - 1);
     430                 :     5594676 :             bitnum = BITNUM(off - 1);
     431                 :             :         }
     432                 :     5594676 :         page->words[wordnum] |= ((bitmapword) 1 << bitnum);
     433                 :     5594676 :         page->recheck |= recheck;
     434                 :             : 
     435         [ +  + ]:     5594676 :         if (tbm->nentries > tbm->maxentries)
     436                 :             :         {
     437                 :          24 :             tbm_lossify(tbm);
     438                 :             :             /* Page could have been converted to lossy, so force new lookup */
     439                 :          24 :             currblk = InvalidBlockNumber;
     440                 :             :         }
     441                 :             :     }
     442                 :     4072712 : }
     443                 :             : 
     444                 :             : /*
     445                 :             :  * tbm_add_page - add a whole page to a TIDBitmap
     446                 :             :  *
     447                 :             :  * This causes the whole page to be reported (with the recheck flag)
     448                 :             :  * when the TIDBitmap is scanned.
     449                 :             :  */
     450                 :             : void
     451                 :       98645 : tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
     452                 :             : {
     453                 :             :     /* Enter the page in the bitmap, or mark it lossy if already present */
     454                 :       98645 :     tbm_mark_page_lossy(tbm, pageno);
     455                 :             :     /* If we went over the memory limit, lossify some more pages */
     456         [ -  + ]:       98645 :     if (tbm->nentries > tbm->maxentries)
     457                 :           0 :         tbm_lossify(tbm);
     458                 :       98645 : }
     459                 :             : 
     460                 :             : /*
     461                 :             :  * tbm_union - set union
     462                 :             :  *
     463                 :             :  * a is modified in-place, b is not changed
     464                 :             :  */
     465                 :             : void
     466                 :           0 : tbm_union(TIDBitmap *a, const TIDBitmap *b)
     467                 :             : {
     468                 :             :     Assert(!a->iterating);
     469                 :             :     /* Nothing to do if b is empty */
     470         [ #  # ]:           0 :     if (b->nentries == 0)
     471                 :           0 :         return;
     472                 :             :     /* Scan through chunks and pages in b, merge into a */
     473         [ #  # ]:           0 :     if (b->status == TBM_ONE_PAGE)
     474                 :           0 :         tbm_union_page(a, &b->entry1);
     475                 :             :     else
     476                 :             :     {
     477                 :             :         pagetable_iterator i;
     478                 :             :         PagetableEntry *bpage;
     479                 :             : 
     480                 :             :         Assert(b->status == TBM_HASH);
     481                 :           0 :         pagetable_start_iterate(b->pagetable, &i);
     482         [ #  # ]:           0 :         while ((bpage = pagetable_iterate(b->pagetable, &i)) != NULL)
     483                 :           0 :             tbm_union_page(a, bpage);
     484                 :             :     }
     485                 :             : }
     486                 :             : 
     487                 :             : /* Process one page of b during a union op */
     488                 :             : static void
     489                 :           0 : tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
     490                 :             : {
     491                 :             :     PagetableEntry *apage;
     492                 :             : 
     493         [ #  # ]:           0 :     if (bpage->ischunk)
     494                 :             :     {
     495                 :             :         /* Scan b's chunk, mark each indicated page lossy in a */
     496         [ #  # ]:           0 :         for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
     497                 :             :         {
     498                 :           0 :             bitmapword  w = bpage->words[wordnum];
     499                 :             : 
     500         [ #  # ]:           0 :             if (w != 0)
     501                 :             :             {
     502                 :             :                 BlockNumber pg;
     503                 :             : 
     504                 :           0 :                 pg = bpage->blockno + (wordnum * BITS_PER_BITMAPWORD);
     505         [ #  # ]:           0 :                 while (w != 0)
     506                 :             :                 {
     507         [ #  # ]:           0 :                     if (w & 1)
     508                 :           0 :                         tbm_mark_page_lossy(a, pg);
     509                 :           0 :                     pg++;
     510                 :           0 :                     w >>= 1;
     511                 :             :                 }
     512                 :             :             }
     513                 :             :         }
     514                 :             :     }
     515         [ #  # ]:           0 :     else if (tbm_page_is_lossy(a, bpage->blockno))
     516                 :             :     {
     517                 :             :         /* page is already lossy in a, nothing to do */
     518                 :           0 :         return;
     519                 :             :     }
     520                 :             :     else
     521                 :             :     {
     522                 :           0 :         apage = tbm_get_pageentry(a, bpage->blockno);
     523         [ #  # ]:           0 :         if (apage->ischunk)
     524                 :             :         {
     525                 :             :             /* The page is a lossy chunk header, set bit for itself */
     526                 :           0 :             apage->words[0] |= ((bitmapword) 1 << 0);
     527                 :             :         }
     528                 :             :         else
     529                 :             :         {
     530                 :             :             /* Both pages are exact, merge at the bit level */
     531         [ #  # ]:           0 :             for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
     532                 :           0 :                 apage->words[wordnum] |= bpage->words[wordnum];
     533                 :           0 :             apage->recheck |= bpage->recheck;
     534                 :             :         }
     535                 :             :     }
     536                 :             : 
     537         [ #  # ]:           0 :     if (a->nentries > a->maxentries)
     538                 :           0 :         tbm_lossify(a);
     539                 :             : }
     540                 :             : 
     541                 :             : /*
     542                 :             :  * tbm_intersect - set intersection
     543                 :             :  *
     544                 :             :  * a is modified in-place, b is not changed
     545                 :             :  */
     546                 :             : void
     547                 :         122 : tbm_intersect(TIDBitmap *a, const TIDBitmap *b)
     548                 :             : {
     549                 :             :     Assert(!a->iterating);
     550                 :             :     /* Nothing to do if a is empty */
     551         [ -  + ]:         122 :     if (a->nentries == 0)
     552                 :           0 :         return;
     553                 :             :     /* Scan through chunks and pages in a, try to match to b */
     554         [ +  + ]:         122 :     if (a->status == TBM_ONE_PAGE)
     555                 :             :     {
     556         [ -  + ]:          72 :         if (tbm_intersect_page(a, &a->entry1, b))
     557                 :             :         {
     558                 :             :             /* Page is now empty, remove it from a */
     559                 :             :             Assert(!a->entry1.ischunk);
     560                 :           0 :             a->npages--;
     561                 :           0 :             a->nentries--;
     562                 :             :             Assert(a->nentries == 0);
     563                 :           0 :             a->status = TBM_EMPTY;
     564                 :             :         }
     565                 :             :     }
     566                 :             :     else
     567                 :             :     {
     568                 :             :         pagetable_iterator i;
     569                 :             :         PagetableEntry *apage;
     570                 :             : 
     571                 :             :         Assert(a->status == TBM_HASH);
     572                 :          50 :         pagetable_start_iterate(a->pagetable, &i);
     573         [ +  + ]:        7522 :         while ((apage = pagetable_iterate(a->pagetable, &i)) != NULL)
     574                 :             :         {
     575         [ +  + ]:        7422 :             if (tbm_intersect_page(a, apage, b))
     576                 :             :             {
     577                 :             :                 /* Page or chunk is now empty, remove it from a */
     578         [ -  + ]:        7037 :                 if (apage->ischunk)
     579                 :           0 :                     a->nchunks--;
     580                 :             :                 else
     581                 :        7037 :                     a->npages--;
     582                 :        7037 :                 a->nentries--;
     583         [ -  + ]:        7037 :                 if (!pagetable_delete(a->pagetable, apage->blockno))
     584         [ #  # ]:           0 :                     elog(ERROR, "hash table corrupted");
     585                 :             :             }
     586                 :             :         }
     587                 :             :     }
     588                 :             : }
     589                 :             : 
     590                 :             : /*
     591                 :             :  * Process one page of a during an intersection op
     592                 :             :  *
     593                 :             :  * Returns true if apage is now empty and should be deleted from a
     594                 :             :  */
     595                 :             : static bool
     596                 :        7494 : tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
     597                 :             : {
     598                 :             :     const PagetableEntry *bpage;
     599                 :             : 
     600         [ +  + ]:        7494 :     if (apage->ischunk)
     601                 :             :     {
     602                 :             :         /* Scan each bit in chunk, try to clear */
     603                 :          40 :         bool        candelete = true;
     604                 :             : 
     605         [ +  + ]:         200 :         for (int wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
     606                 :             :         {
     607                 :         160 :             bitmapword  w = apage->words[wordnum];
     608                 :             : 
     609         [ +  + ]:         160 :             if (w != 0)
     610                 :             :             {
     611                 :         144 :                 bitmapword  neww = w;
     612                 :             :                 BlockNumber pg;
     613                 :             :                 int         bitnum;
     614                 :             : 
     615                 :         144 :                 pg = apage->blockno + (wordnum * BITS_PER_BITMAPWORD);
     616                 :         144 :                 bitnum = 0;
     617         [ +  + ]:        8808 :                 while (w != 0)
     618                 :             :                 {
     619         [ +  + ]:        8664 :                     if (w & 1)
     620                 :             :                     {
     621   [ +  +  -  + ]:        4480 :                         if (!tbm_page_is_lossy(b, pg) &&
     622                 :         336 :                             tbm_find_pageentry(b, pg) == NULL)
     623                 :             :                         {
     624                 :             :                             /* Page is not in b at all, lose lossy bit */
     625                 :           0 :                             neww &= ~((bitmapword) 1 << bitnum);
     626                 :             :                         }
     627                 :             :                     }
     628                 :        8664 :                     pg++;
     629                 :        8664 :                     bitnum++;
     630                 :        8664 :                     w >>= 1;
     631                 :             :                 }
     632                 :         144 :                 apage->words[wordnum] = neww;
     633         [ +  - ]:         144 :                 if (neww != 0)
     634                 :         144 :                     candelete = false;
     635                 :             :             }
     636                 :             :         }
     637                 :          40 :         return candelete;
     638                 :             :     }
     639         [ -  + ]:        7454 :     else if (tbm_page_is_lossy(b, apage->blockno))
     640                 :             :     {
     641                 :             :         /*
     642                 :             :          * Some of the tuples in 'a' might not satisfy the quals for 'b', but
     643                 :             :          * because the page 'b' is lossy, we don't know which ones. Therefore
     644                 :             :          * we mark 'a' as requiring rechecks, to indicate that at most those
     645                 :             :          * tuples set in 'a' are matches.
     646                 :             :          */
     647                 :           0 :         apage->recheck = true;
     648                 :           0 :         return false;
     649                 :             :     }
     650                 :             :     else
     651                 :             :     {
     652                 :        7454 :         bool        candelete = true;
     653                 :             : 
     654                 :        7454 :         bpage = tbm_find_pageentry(b, apage->blockno);
     655         [ +  + ]:        7454 :         if (bpage != NULL)
     656                 :             :         {
     657                 :             :             /* Both pages are exact, merge at the bit level */
     658                 :             :             Assert(!bpage->ischunk);
     659         [ +  + ]:       36858 :             for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
     660                 :             :             {
     661                 :       30715 :                 apage->words[wordnum] &= bpage->words[wordnum];
     662         [ +  + ]:       30715 :                 if (apage->words[wordnum] != 0)
     663                 :         420 :                     candelete = false;
     664                 :             :             }
     665                 :        6143 :             apage->recheck |= bpage->recheck;
     666                 :             :         }
     667                 :             :         /* If there is no matching b page, we can just delete the a page */
     668                 :        7454 :         return candelete;
     669                 :             :     }
     670                 :             : }
     671                 :             : 
     672                 :             : /*
     673                 :             :  * tbm_is_empty - is a TIDBitmap completely empty?
     674                 :             :  */
     675                 :             : bool
     676                 :         748 : tbm_is_empty(const TIDBitmap *tbm)
     677                 :             : {
     678                 :         748 :     return (tbm->nentries == 0);
     679                 :             : }
     680                 :             : 
     681                 :             : /*
     682                 :             :  * tbm_begin_private_iterate - prepare to iterate through a TIDBitmap
     683                 :             :  *
     684                 :             :  * The TBMPrivateIterator struct is created in the caller's memory context.
     685                 :             :  * For a clean shutdown of the iteration, call tbm_end_private_iterate; but
     686                 :             :  * it's okay to just allow the memory context to be released, too.  It is
     687                 :             :  * caller's responsibility not to touch the TBMPrivateIterator anymore once
     688                 :             :  * the TIDBitmap is freed.
     689                 :             :  *
     690                 :             :  * NB: after this is called, it is no longer allowed to modify the contents
     691                 :             :  * of the bitmap.  However, you can call this multiple times to scan the
     692                 :             :  * contents repeatedly, including parallel scans.
     693                 :             :  */
     694                 :             : TBMPrivateIterator *
     695                 :       15574 : tbm_begin_private_iterate(TIDBitmap *tbm)
     696                 :             : {
     697                 :             :     TBMPrivateIterator *iterator;
     698                 :             : 
     699                 :             :     Assert(tbm->iterating != TBM_ITERATING_SHARED);
     700                 :             : 
     701                 :             :     /*
     702                 :             :      * Create the TBMPrivateIterator struct, with enough trailing space to
     703                 :             :      * serve the needs of the TBMIterateResult sub-struct.
     704                 :             :      */
     705                 :       15574 :     iterator = palloc_object(TBMPrivateIterator);
     706                 :       15574 :     iterator->tbm = tbm;
     707                 :             : 
     708                 :             :     /*
     709                 :             :      * Initialize iteration pointers.
     710                 :             :      */
     711                 :       15574 :     iterator->spageptr = 0;
     712                 :       15574 :     iterator->schunkptr = 0;
     713                 :       15574 :     iterator->schunkbit = 0;
     714                 :             : 
     715                 :             :     /*
     716                 :             :      * If we have a hashtable, create and fill the sorted page lists, unless
     717                 :             :      * we already did that for a previous iterator.  Note that the lists are
     718                 :             :      * attached to the bitmap not the iterator, so they can be used by more
     719                 :             :      * than one iterator.
     720                 :             :      */
     721   [ +  +  +  - ]:       15574 :     if (tbm->status == TBM_HASH && tbm->iterating == TBM_NOT_ITERATING)
     722                 :             :     {
     723                 :             :         pagetable_iterator i;
     724                 :             :         PagetableEntry *page;
     725                 :             :         int         npages;
     726                 :             :         int         nchunks;
     727                 :             : 
     728   [ +  -  +  + ]:        5383 :         if (!tbm->spages && tbm->npages > 0)
     729                 :        3467 :             tbm->spages = (PagetableEntry **)
     730                 :        3467 :                 MemoryContextAlloc(tbm->mcxt,
     731                 :        3467 :                                    tbm->npages * sizeof(PagetableEntry *));
     732   [ +  -  +  + ]:        5383 :         if (!tbm->schunks && tbm->nchunks > 0)
     733                 :        1924 :             tbm->schunks = (PagetableEntry **)
     734                 :        1924 :                 MemoryContextAlloc(tbm->mcxt,
     735                 :        1924 :                                    tbm->nchunks * sizeof(PagetableEntry *));
     736                 :             : 
     737                 :        5383 :         npages = nchunks = 0;
     738                 :        5383 :         pagetable_start_iterate(tbm->pagetable, &i);
     739         [ +  + ]:      124259 :         while ((page = pagetable_iterate(tbm->pagetable, &i)) != NULL)
     740                 :             :         {
     741         [ +  + ]:      118876 :             if (page->ischunk)
     742                 :        1968 :                 tbm->schunks[nchunks++] = page;
     743                 :             :             else
     744                 :      116908 :                 tbm->spages[npages++] = page;
     745                 :             :         }
     746                 :             :         Assert(npages == tbm->npages);
     747                 :             :         Assert(nchunks == tbm->nchunks);
     748         [ +  + ]:        5383 :         if (npages > 1)
     749                 :        3465 :             qsort_pagetable(tbm->spages, npages);
     750         [ +  + ]:        5383 :         if (nchunks > 1)
     751                 :          12 :             qsort_pagetable(tbm->schunks, nchunks);
     752                 :             :     }
     753                 :             : 
     754                 :       15574 :     tbm->iterating = TBM_ITERATING_PRIVATE;
     755                 :             : 
     756                 :       15574 :     return iterator;
     757                 :             : }
     758                 :             : 
     759                 :             : /*
     760                 :             :  * tbm_prepare_shared_iterate - prepare shared iteration state for a TIDBitmap.
     761                 :             :  *
     762                 :             :  * The necessary shared state will be allocated from the DSA passed to
     763                 :             :  * tbm_create, so that multiple processes can attach to it and iterate jointly.
     764                 :             :  *
     765                 :             :  * This will convert the pagetable hash into page and chunk array of the index
     766                 :             :  * into pagetable array.
     767                 :             :  */
     768                 :             : dsa_pointer
     769                 :          48 : tbm_prepare_shared_iterate(TIDBitmap *tbm)
     770                 :             : {
     771                 :             :     dsa_pointer dp;
     772                 :             :     TBMSharedIteratorState *istate;
     773                 :          48 :     PTEntryArray *ptbase = NULL;
     774                 :          48 :     PTIterationArray *ptpages = NULL;
     775                 :          48 :     PTIterationArray *ptchunks = NULL;
     776                 :             : 
     777                 :             :     Assert(tbm->dsa != NULL);
     778                 :             :     Assert(tbm->iterating != TBM_ITERATING_PRIVATE);
     779                 :             : 
     780                 :             :     /*
     781                 :             :      * Allocate TBMSharedIteratorState from DSA to hold the shared members and
     782                 :             :      * lock, this will also be used by multiple worker for shared iterate.
     783                 :             :      */
     784                 :          48 :     dp = dsa_allocate0(tbm->dsa, sizeof(TBMSharedIteratorState));
     785                 :          48 :     istate = dsa_get_address(tbm->dsa, dp);
     786                 :             : 
     787                 :             :     /*
     788                 :             :      * If we're not already iterating, create and fill the sorted page lists.
     789                 :             :      * (If we are, the sorted page lists are already stored in the TIDBitmap,
     790                 :             :      * and we can just reuse them.)
     791                 :             :      */
     792         [ +  - ]:          48 :     if (tbm->iterating == TBM_NOT_ITERATING)
     793                 :             :     {
     794                 :             :         pagetable_iterator i;
     795                 :             :         PagetableEntry *page;
     796                 :             :         int         idx;
     797                 :             :         int         npages;
     798                 :             :         int         nchunks;
     799                 :             : 
     800                 :             :         /*
     801                 :             :          * Allocate the page and chunk array memory from the DSA to share
     802                 :             :          * across multiple processes.
     803                 :             :          */
     804         [ +  - ]:          48 :         if (tbm->npages)
     805                 :             :         {
     806                 :          48 :             tbm->ptpages = dsa_allocate(tbm->dsa, sizeof(PTIterationArray) +
     807                 :             :                                         tbm->npages * sizeof(int));
     808                 :          48 :             ptpages = dsa_get_address(tbm->dsa, tbm->ptpages);
     809                 :          48 :             pg_atomic_init_u32(&ptpages->refcount, 0);
     810                 :             :         }
     811         [ +  + ]:          48 :         if (tbm->nchunks)
     812                 :             :         {
     813                 :           4 :             tbm->ptchunks = dsa_allocate(tbm->dsa, sizeof(PTIterationArray) +
     814                 :             :                                          tbm->nchunks * sizeof(int));
     815                 :           4 :             ptchunks = dsa_get_address(tbm->dsa, tbm->ptchunks);
     816                 :           4 :             pg_atomic_init_u32(&ptchunks->refcount, 0);
     817                 :             :         }
     818                 :             : 
     819                 :             :         /*
     820                 :             :          * If TBM status is TBM_HASH then iterate over the pagetable and
     821                 :             :          * convert it to page and chunk arrays.  But if it's in the
     822                 :             :          * TBM_ONE_PAGE mode then directly allocate the space for one entry
     823                 :             :          * from the DSA.
     824                 :             :          */
     825                 :          48 :         npages = nchunks = 0;
     826         [ +  - ]:          48 :         if (tbm->status == TBM_HASH)
     827                 :             :         {
     828                 :          48 :             ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
     829                 :             : 
     830                 :          48 :             pagetable_start_iterate(tbm->pagetable, &i);
     831         [ +  + ]:       18068 :             while ((page = pagetable_iterate(tbm->pagetable, &i)) != NULL)
     832                 :             :             {
     833                 :       18020 :                 idx = page - ptbase->ptentry;
     834         [ +  + ]:       18020 :                 if (page->ischunk)
     835                 :          16 :                     ptchunks->index[nchunks++] = idx;
     836                 :             :                 else
     837                 :       18004 :                     ptpages->index[npages++] = idx;
     838                 :             :             }
     839                 :             : 
     840                 :             :             Assert(npages == tbm->npages);
     841                 :             :             Assert(nchunks == tbm->nchunks);
     842                 :             :         }
     843         [ #  # ]:           0 :         else if (tbm->status == TBM_ONE_PAGE)
     844                 :             :         {
     845                 :             :             /*
     846                 :             :              * In one page mode allocate the space for one pagetable entry,
     847                 :             :              * initialize it, and directly store its index (i.e. 0) in the
     848                 :             :              * page array.
     849                 :             :              */
     850                 :           0 :             tbm->dsapagetable = dsa_allocate(tbm->dsa, sizeof(PTEntryArray) +
     851                 :             :                                              sizeof(PagetableEntry));
     852                 :           0 :             ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
     853                 :           0 :             memcpy(ptbase->ptentry, &tbm->entry1, sizeof(PagetableEntry));
     854                 :           0 :             ptpages->index[0] = 0;
     855                 :             :         }
     856                 :             : 
     857         [ +  - ]:          48 :         if (ptbase != NULL)
     858                 :          48 :             pg_atomic_init_u32(&ptbase->refcount, 0);
     859         [ +  - ]:          48 :         if (npages > 1)
     860                 :          48 :             qsort_pagetable_shared(ptpages->index, npages, ptbase->ptentry);
     861         [ +  + ]:          48 :         if (nchunks > 1)
     862                 :           4 :             qsort_pagetable_shared(ptchunks->index, nchunks, ptbase->ptentry);
     863                 :             :     }
     864                 :             : 
     865                 :             :     /*
     866                 :             :      * Store the TBM members in the shared state so that we can share them
     867                 :             :      * across multiple processes.
     868                 :             :      */
     869                 :          48 :     istate->nentries = tbm->nentries;
     870                 :          48 :     istate->maxentries = tbm->maxentries;
     871                 :          48 :     istate->npages = tbm->npages;
     872                 :          48 :     istate->nchunks = tbm->nchunks;
     873                 :          48 :     istate->pagetable = tbm->dsapagetable;
     874                 :          48 :     istate->spages = tbm->ptpages;
     875                 :          48 :     istate->schunks = tbm->ptchunks;
     876                 :             : 
     877                 :          48 :     ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
     878                 :          48 :     ptpages = dsa_get_address(tbm->dsa, tbm->ptpages);
     879                 :          48 :     ptchunks = dsa_get_address(tbm->dsa, tbm->ptchunks);
     880                 :             : 
     881                 :             :     /*
     882                 :             :      * For every shared iterator referring to pagetable and iterator array,
     883                 :             :      * increase the refcount by 1 so that while freeing the shared iterator we
     884                 :             :      * don't free pagetable and iterator array until its refcount becomes 0.
     885                 :             :      */
     886         [ +  - ]:          48 :     if (ptbase != NULL)
     887                 :          48 :         pg_atomic_add_fetch_u32(&ptbase->refcount, 1);
     888         [ +  - ]:          48 :     if (ptpages != NULL)
     889                 :          48 :         pg_atomic_add_fetch_u32(&ptpages->refcount, 1);
     890         [ +  + ]:          48 :     if (ptchunks != NULL)
     891                 :           4 :         pg_atomic_add_fetch_u32(&ptchunks->refcount, 1);
     892                 :             : 
     893                 :             :     /* Initialize the iterator lock */
     894                 :          48 :     LWLockInitialize(&istate->lock, LWTRANCHE_SHARED_TIDBITMAP);
     895                 :             : 
     896                 :             :     /* Initialize the shared iterator state */
     897                 :          48 :     istate->schunkbit = 0;
     898                 :          48 :     istate->schunkptr = 0;
     899                 :          48 :     istate->spageptr = 0;
     900                 :             : 
     901                 :          48 :     tbm->iterating = TBM_ITERATING_SHARED;
     902                 :             : 
     903                 :          48 :     return dp;
     904                 :             : }
     905                 :             : 
     906                 :             : /*
     907                 :             :  * tbm_extract_page_tuple - extract the tuple offsets from a page
     908                 :             :  *
     909                 :             :  * Returns the number of offsets it filled in if <= max_offsets. Otherwise,
     910                 :             :  * fills in as many offsets as fit and returns the total number of offsets in
     911                 :             :  * the page.
     912                 :             :  */
     913                 :             : int
     914                 :      141943 : tbm_extract_page_tuple(TBMIterateResult *iteritem,
     915                 :             :                        OffsetNumber *offsets,
     916                 :             :                        uint32 max_offsets)
     917                 :             : {
     918                 :      141943 :     PagetableEntry *page = iteritem->internal_page;
     919                 :      141943 :     int         ntuples = 0;
     920                 :             : 
     921         [ +  + ]:      851658 :     for (int wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
     922                 :             :     {
     923                 :      709715 :         bitmapword  w = page->words[wordnum];
     924                 :             : 
     925         [ +  + ]:      709715 :         if (w != 0)
     926                 :             :         {
     927                 :      310246 :             int         off = wordnum * BITS_PER_BITMAPWORD + 1;
     928                 :             : 
     929         [ +  + ]:    13240739 :             while (w != 0)
     930                 :             :             {
     931         [ +  + ]:    12930493 :                 if (w & 1)
     932                 :             :                 {
     933         [ +  - ]:     3679218 :                     if (ntuples < max_offsets)
     934                 :     3679218 :                         offsets[ntuples] = (OffsetNumber) off;
     935                 :     3679218 :                     ntuples++;
     936                 :             :                 }
     937                 :    12930493 :                 off++;
     938                 :    12930493 :                 w >>= 1;
     939                 :             :             }
     940                 :             :         }
     941                 :             :     }
     942                 :             : 
     943                 :      141943 :     return ntuples;
     944                 :             : }
     945                 :             : 
     946                 :             : /*
     947                 :             :  *  tbm_advance_schunkbit - Advance the schunkbit
     948                 :             :  */
     949                 :             : static inline void
     950                 :      113013 : tbm_advance_schunkbit(PagetableEntry *chunk, int *schunkbitp)
     951                 :             : {
     952                 :      113013 :     int         schunkbit = *schunkbitp;
     953                 :             : 
     954         [ +  + ]:      513992 :     while (schunkbit < PAGES_PER_CHUNK)
     955                 :             :     {
     956                 :      512008 :         int         wordnum = WORDNUM(schunkbit);
     957                 :      512008 :         int         bitnum = BITNUM(schunkbit);
     958                 :             : 
     959         [ +  + ]:      512008 :         if ((chunk->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
     960                 :      111029 :             break;
     961                 :      400979 :         schunkbit++;
     962                 :             :     }
     963                 :             : 
     964                 :      113013 :     *schunkbitp = schunkbit;
     965                 :      113013 : }
     966                 :             : 
     967                 :             : /*
     968                 :             :  * tbm_private_iterate - scan through next page of a TIDBitmap
     969                 :             :  *
     970                 :             :  * Caller must pass in a TBMIterateResult to be filled.
     971                 :             :  *
     972                 :             :  * Pages are guaranteed to be delivered in numerical order.
     973                 :             :  *
     974                 :             :  * Returns false when there are no more pages to scan and true otherwise. When
     975                 :             :  * there are no more pages to scan, tbmres->blockno is set to
     976                 :             :  * InvalidBlockNumber.
     977                 :             :  *
     978                 :             :  * If lossy is true, then the bitmap is "lossy" and failed to remember
     979                 :             :  * the exact tuples to look at on this page --- the caller must examine all
     980                 :             :  * tuples on the page and check if they meet the intended condition. If lossy
     981                 :             :  * is false, the caller must later extract the tuple offsets from the page
     982                 :             :  * pointed to by internal_page with tbm_extract_page_tuple.
     983                 :             :  *
     984                 :             :  * If tbmres->recheck is true, only the indicated tuples need be examined, but
     985                 :             :  * the condition must be rechecked anyway.  (For ease of testing, recheck is
     986                 :             :  * always set true when lossy is true.)
     987                 :             :  */
     988                 :             : bool
     989                 :      244370 : tbm_private_iterate(TBMPrivateIterator *iterator, TBMIterateResult *tbmres)
     990                 :             : {
     991                 :      244370 :     TIDBitmap  *tbm = iterator->tbm;
     992                 :             : 
     993                 :             :     Assert(tbm->iterating == TBM_ITERATING_PRIVATE);
     994                 :             : 
     995                 :             :     /*
     996                 :             :      * If lossy chunk pages remain, make sure we've advanced schunkptr/
     997                 :             :      * schunkbit to the next set bit.
     998                 :             :      */
     999         [ +  + ]:      246338 :     while (iterator->schunkptr < tbm->nchunks)
    1000                 :             :     {
    1001                 :      108913 :         PagetableEntry *chunk = tbm->schunks[iterator->schunkptr];
    1002                 :      108913 :         int         schunkbit = iterator->schunkbit;
    1003                 :             : 
    1004                 :      108913 :         tbm_advance_schunkbit(chunk, &schunkbit);
    1005         [ +  + ]:      108913 :         if (schunkbit < PAGES_PER_CHUNK)
    1006                 :             :         {
    1007                 :      106945 :             iterator->schunkbit = schunkbit;
    1008                 :      106945 :             break;
    1009                 :             :         }
    1010                 :             :         /* advance to next chunk */
    1011                 :        1968 :         iterator->schunkptr++;
    1012                 :        1968 :         iterator->schunkbit = 0;
    1013                 :             :     }
    1014                 :             : 
    1015                 :             :     /*
    1016                 :             :      * If both chunk and per-page data remain, must output the numerically
    1017                 :             :      * earlier page.
    1018                 :             :      */
    1019         [ +  + ]:      244370 :     if (iterator->schunkptr < tbm->nchunks)
    1020                 :             :     {
    1021                 :      106945 :         PagetableEntry *chunk = tbm->schunks[iterator->schunkptr];
    1022                 :             :         BlockNumber chunk_blockno;
    1023                 :             : 
    1024                 :      106945 :         chunk_blockno = chunk->blockno + iterator->schunkbit;
    1025         [ +  + ]:      106945 :         if (iterator->spageptr >= tbm->npages ||
    1026         [ +  + ]:        8300 :             chunk_blockno < tbm->spages[iterator->spageptr]->blockno)
    1027                 :             :         {
    1028                 :             :             /* Return a lossy page indicator from the chunk */
    1029                 :      104857 :             tbmres->blockno = chunk_blockno;
    1030                 :      104857 :             tbmres->lossy = true;
    1031                 :      104857 :             tbmres->recheck = true;
    1032                 :      104857 :             tbmres->internal_page = NULL;
    1033                 :      104857 :             iterator->schunkbit++;
    1034                 :      104857 :             return true;
    1035                 :             :         }
    1036                 :             :     }
    1037                 :             : 
    1038         [ +  + ]:      139513 :     if (iterator->spageptr < tbm->npages)
    1039                 :             :     {
    1040                 :             :         PagetableEntry *page;
    1041                 :             : 
    1042                 :             :         /* In TBM_ONE_PAGE state, we don't allocate an spages[] array */
    1043         [ +  + ]:      123954 :         if (tbm->status == TBM_ONE_PAGE)
    1044                 :        7695 :             page = &tbm->entry1;
    1045                 :             :         else
    1046                 :      116259 :             page = tbm->spages[iterator->spageptr];
    1047                 :             : 
    1048                 :      123954 :         tbmres->internal_page = page;
    1049                 :      123954 :         tbmres->blockno = page->blockno;
    1050                 :      123954 :         tbmres->lossy = false;
    1051                 :      123954 :         tbmres->recheck = page->recheck;
    1052                 :      123954 :         iterator->spageptr++;
    1053                 :      123954 :         return true;
    1054                 :             :     }
    1055                 :             : 
    1056                 :             :     /* Nothing more in the bitmap */
    1057                 :       15559 :     tbmres->blockno = InvalidBlockNumber;
    1058                 :       15559 :     return false;
    1059                 :             : }
    1060                 :             : 
    1061                 :             : /*
    1062                 :             :  *  tbm_shared_iterate - scan through next page of a TIDBitmap
    1063                 :             :  *
    1064                 :             :  *  As above, but this will iterate using an iterator which is shared
    1065                 :             :  *  across multiple processes.  We need to acquire the iterator LWLock,
    1066                 :             :  *  before accessing the shared members.
    1067                 :             :  */
    1068                 :             : bool
    1069                 :       20300 : tbm_shared_iterate(TBMSharedIterator *iterator, TBMIterateResult *tbmres)
    1070                 :             : {
    1071                 :       20300 :     TBMSharedIteratorState *istate = iterator->state;
    1072                 :       20300 :     PagetableEntry *ptbase = NULL;
    1073                 :       20300 :     int        *idxpages = NULL;
    1074                 :       20300 :     int        *idxchunks = NULL;
    1075                 :             : 
    1076         [ +  - ]:       20300 :     if (iterator->ptbase != NULL)
    1077                 :       20300 :         ptbase = iterator->ptbase->ptentry;
    1078         [ +  - ]:       20300 :     if (iterator->ptpages != NULL)
    1079                 :       20300 :         idxpages = iterator->ptpages->index;
    1080         [ +  + ]:       20300 :     if (iterator->ptchunks != NULL)
    1081                 :        4960 :         idxchunks = iterator->ptchunks->index;
    1082                 :             : 
    1083                 :             :     /* Acquire the LWLock before accessing the shared members */
    1084                 :       20300 :     LWLockAcquire(&istate->lock, LW_EXCLUSIVE);
    1085                 :             : 
    1086                 :             :     /*
    1087                 :             :      * If lossy chunk pages remain, make sure we've advanced schunkptr/
    1088                 :             :      * schunkbit to the next set bit.
    1089                 :             :      */
    1090         [ +  + ]:       20316 :     while (istate->schunkptr < istate->nchunks)
    1091                 :             :     {
    1092                 :        4100 :         PagetableEntry *chunk = &ptbase[idxchunks[istate->schunkptr]];
    1093                 :        4100 :         int         schunkbit = istate->schunkbit;
    1094                 :             : 
    1095                 :        4100 :         tbm_advance_schunkbit(chunk, &schunkbit);
    1096         [ +  + ]:        4100 :         if (schunkbit < PAGES_PER_CHUNK)
    1097                 :             :         {
    1098                 :        4084 :             istate->schunkbit = schunkbit;
    1099                 :        4084 :             break;
    1100                 :             :         }
    1101                 :             :         /* advance to next chunk */
    1102                 :          16 :         istate->schunkptr++;
    1103                 :          16 :         istate->schunkbit = 0;
    1104                 :             :     }
    1105                 :             : 
    1106                 :             :     /*
    1107                 :             :      * If both chunk and per-page data remain, must output the numerically
    1108                 :             :      * earlier page.
    1109                 :             :      */
    1110         [ +  + ]:       20300 :     if (istate->schunkptr < istate->nchunks)
    1111                 :             :     {
    1112                 :        4084 :         PagetableEntry *chunk = &ptbase[idxchunks[istate->schunkptr]];
    1113                 :             :         BlockNumber chunk_blockno;
    1114                 :             : 
    1115                 :        4084 :         chunk_blockno = chunk->blockno + istate->schunkbit;
    1116                 :             : 
    1117         [ +  - ]:        4084 :         if (istate->spageptr >= istate->npages ||
    1118         [ +  + ]:        4084 :             chunk_blockno < ptbase[idxpages[istate->spageptr]].blockno)
    1119                 :             :         {
    1120                 :             :             /* Return a lossy page indicator from the chunk */
    1121                 :        2068 :             tbmres->blockno = chunk_blockno;
    1122                 :        2068 :             tbmres->lossy = true;
    1123                 :        2068 :             tbmres->recheck = true;
    1124                 :        2068 :             tbmres->internal_page = NULL;
    1125                 :        2068 :             istate->schunkbit++;
    1126                 :             : 
    1127                 :        2068 :             LWLockRelease(&istate->lock);
    1128                 :        2068 :             return true;
    1129                 :             :         }
    1130                 :             :     }
    1131                 :             : 
    1132         [ +  + ]:       18232 :     if (istate->spageptr < istate->npages)
    1133                 :             :     {
    1134                 :       18004 :         PagetableEntry *page = &ptbase[idxpages[istate->spageptr]];
    1135                 :             : 
    1136                 :       18004 :         tbmres->internal_page = page;
    1137                 :       18004 :         tbmres->blockno = page->blockno;
    1138                 :       18004 :         tbmres->lossy = false;
    1139                 :       18004 :         tbmres->recheck = page->recheck;
    1140                 :       18004 :         istate->spageptr++;
    1141                 :             : 
    1142                 :       18004 :         LWLockRelease(&istate->lock);
    1143                 :             : 
    1144                 :       18004 :         return true;
    1145                 :             :     }
    1146                 :             : 
    1147                 :         228 :     LWLockRelease(&istate->lock);
    1148                 :             : 
    1149                 :             :     /* Nothing more in the bitmap */
    1150                 :         228 :     tbmres->blockno = InvalidBlockNumber;
    1151                 :         228 :     return false;
    1152                 :             : }
    1153                 :             : 
    1154                 :             : /*
    1155                 :             :  * tbm_end_private_iterate - finish an iteration over a TIDBitmap
    1156                 :             :  *
    1157                 :             :  * Currently this is just a pfree, but it might do more someday.  (For
    1158                 :             :  * instance, it could be useful to count open iterators and allow the
    1159                 :             :  * bitmap to return to read/write status when there are no more iterators.)
    1160                 :             :  */
    1161                 :             : void
    1162                 :       15493 : tbm_end_private_iterate(TBMPrivateIterator *iterator)
    1163                 :             : {
    1164                 :       15493 :     pfree(iterator);
    1165                 :       15493 : }
    1166                 :             : 
    1167                 :             : /*
    1168                 :             :  * tbm_end_shared_iterate - finish a shared iteration over a TIDBitmap
    1169                 :             :  *
    1170                 :             :  * This doesn't free any of the shared state associated with the iterator,
    1171                 :             :  * just our backend-private state.
    1172                 :             :  */
    1173                 :             : void
    1174                 :         228 : tbm_end_shared_iterate(TBMSharedIterator *iterator)
    1175                 :             : {
    1176                 :         228 :     pfree(iterator);
    1177                 :         228 : }
    1178                 :             : 
    1179                 :             : /*
    1180                 :             :  * tbm_find_pageentry - find a PagetableEntry for the pageno
    1181                 :             :  *
    1182                 :             :  * Returns NULL if there is no non-lossy entry for the pageno.
    1183                 :             :  */
    1184                 :             : static const PagetableEntry *
    1185                 :        7790 : tbm_find_pageentry(const TIDBitmap *tbm, BlockNumber pageno)
    1186                 :             : {
    1187                 :             :     const PagetableEntry *page;
    1188                 :             : 
    1189         [ -  + ]:        7790 :     if (tbm->nentries == 0)      /* in case pagetable doesn't exist */
    1190                 :           0 :         return NULL;
    1191                 :             : 
    1192         [ -  + ]:        7790 :     if (tbm->status == TBM_ONE_PAGE)
    1193                 :             :     {
    1194                 :           0 :         page = &tbm->entry1;
    1195         [ #  # ]:           0 :         if (page->blockno != pageno)
    1196                 :           0 :             return NULL;
    1197                 :             :         Assert(!page->ischunk);
    1198                 :           0 :         return page;
    1199                 :             :     }
    1200                 :             : 
    1201                 :        7790 :     page = pagetable_lookup(tbm->pagetable, pageno);
    1202         [ +  + ]:        7790 :     if (page == NULL)
    1203                 :        1311 :         return NULL;
    1204         [ -  + ]:        6479 :     if (page->ischunk)
    1205                 :           0 :         return NULL;            /* don't want a lossy chunk header */
    1206                 :        6479 :     return page;
    1207                 :             : }
    1208                 :             : 
    1209                 :             : /*
    1210                 :             :  * tbm_get_pageentry - find or create a PagetableEntry for the pageno
    1211                 :             :  *
    1212                 :             :  * If new, the entry is marked as an exact (non-chunk) entry.
    1213                 :             :  *
    1214                 :             :  * This may cause the table to exceed the desired memory size.  It is
    1215                 :             :  * up to the caller to call tbm_lossify() at the next safe point if so.
    1216                 :             :  */
    1217                 :             : static PagetableEntry *
    1218                 :     4609507 : tbm_get_pageentry(TIDBitmap *tbm, BlockNumber pageno)
    1219                 :             : {
    1220                 :             :     PagetableEntry *page;
    1221                 :             :     bool        found;
    1222                 :             : 
    1223         [ +  + ]:     4609507 :     if (tbm->status == TBM_EMPTY)
    1224                 :             :     {
    1225                 :             :         /* Use the fixed slot */
    1226                 :       11336 :         page = &tbm->entry1;
    1227                 :       11336 :         found = false;
    1228                 :       11336 :         tbm->status = TBM_ONE_PAGE;
    1229                 :             :     }
    1230                 :             :     else
    1231                 :             :     {
    1232         [ +  + ]:     4598171 :         if (tbm->status == TBM_ONE_PAGE)
    1233                 :             :         {
    1234                 :      131981 :             page = &tbm->entry1;
    1235         [ +  + ]:      131981 :             if (page->blockno == pageno)
    1236                 :      128340 :                 return page;
    1237                 :             :             /* Time to switch from one page to a hashtable */
    1238                 :        3641 :             tbm_create_pagetable(tbm);
    1239                 :             :         }
    1240                 :             : 
    1241                 :             :         /* Look up or create an entry */
    1242                 :     4469831 :         page = pagetable_insert(tbm->pagetable, pageno, &found);
    1243                 :             :     }
    1244                 :             : 
    1245                 :             :     /* Initialize it if not present before */
    1246         [ +  + ]:     4481167 :     if (!found)
    1247                 :             :     {
    1248                 :      170069 :         char        oldstatus = page->status;
    1249                 :             : 
    1250   [ +  -  +  -  :     1190483 :         MemSet(page, 0, sizeof(PagetableEntry));
          +  -  +  -  +  
                      + ]
    1251                 :      170069 :         page->status = oldstatus;
    1252                 :      170069 :         page->blockno = pageno;
    1253                 :             :         /* must count it too */
    1254                 :      170069 :         tbm->nentries++;
    1255                 :      170069 :         tbm->npages++;
    1256                 :             :     }
    1257                 :             : 
    1258                 :     4481167 :     return page;
    1259                 :             : }
    1260                 :             : 
    1261                 :             : /*
    1262                 :             :  * tbm_page_is_lossy - is the page marked as lossily stored?
    1263                 :             :  */
    1264                 :             : static bool
    1265                 :     4623009 : tbm_page_is_lossy(const TIDBitmap *tbm, BlockNumber pageno)
    1266                 :             : {
    1267                 :             :     PagetableEntry *page;
    1268                 :             :     BlockNumber chunk_pageno;
    1269                 :             :     int         bitno;
    1270                 :             : 
    1271                 :             :     /* we can skip the lookup if there are no lossy chunks */
    1272         [ +  + ]:     4623009 :     if (tbm->nchunks == 0)
    1273                 :     4536165 :         return false;
    1274                 :             :     Assert(tbm->status == TBM_HASH);
    1275                 :             : 
    1276                 :       86844 :     bitno = pageno % PAGES_PER_CHUNK;
    1277                 :       86844 :     chunk_pageno = pageno - bitno;
    1278                 :             : 
    1279                 :       86844 :     page = pagetable_lookup(tbm->pagetable, chunk_pageno);
    1280                 :             : 
    1281   [ +  -  +  + ]:       86844 :     if (page != NULL && page->ischunk)
    1282                 :             :     {
    1283                 :       12756 :         int         wordnum = WORDNUM(bitno);
    1284                 :       12756 :         int         bitnum = BITNUM(bitno);
    1285                 :             : 
    1286         [ +  + ]:       12756 :         if ((page->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
    1287                 :        5712 :             return true;
    1288                 :             :     }
    1289                 :       81132 :     return false;
    1290                 :             : }
    1291                 :             : 
    1292                 :             : /*
    1293                 :             :  * tbm_mark_page_lossy - mark the page number as lossily stored
    1294                 :             :  *
    1295                 :             :  * This may cause the table to exceed the desired memory size.  It is
    1296                 :             :  * up to the caller to call tbm_lossify() at the next safe point if so.
    1297                 :             :  */
    1298                 :             : static void
    1299                 :      110957 : tbm_mark_page_lossy(TIDBitmap *tbm, BlockNumber pageno)
    1300                 :             : {
    1301                 :             :     PagetableEntry *page;
    1302                 :             :     bool        found;
    1303                 :             :     BlockNumber chunk_pageno;
    1304                 :             :     int         bitno;
    1305                 :             :     int         wordnum;
    1306                 :             :     int         bitnum;
    1307                 :             : 
    1308                 :             :     /* We force the bitmap into hashtable mode whenever it's lossy */
    1309         [ +  + ]:      110957 :     if (tbm->status != TBM_HASH)
    1310                 :        1912 :         tbm_create_pagetable(tbm);
    1311                 :             : 
    1312                 :      110957 :     bitno = pageno % PAGES_PER_CHUNK;
    1313                 :      110957 :     chunk_pageno = pageno - bitno;
    1314                 :             : 
    1315                 :             :     /*
    1316                 :             :      * Remove any extant non-lossy entry for the page.  If the page is its own
    1317                 :             :      * chunk header, however, we skip this and handle the case below.
    1318                 :             :      */
    1319         [ +  + ]:      110957 :     if (bitno != 0)
    1320                 :             :     {
    1321         [ +  + ]:      109405 :         if (pagetable_delete(tbm->pagetable, pageno))
    1322                 :             :         {
    1323                 :             :             /* It was present, so adjust counts */
    1324                 :       12312 :             tbm->nentries--;
    1325                 :       12312 :             tbm->npages--;       /* assume it must have been non-lossy */
    1326                 :             :         }
    1327                 :             :     }
    1328                 :             : 
    1329                 :             :     /* Look up or create entry for chunk-header page */
    1330                 :      110957 :     page = pagetable_insert(tbm->pagetable, chunk_pageno, &found);
    1331                 :             : 
    1332                 :             :     /* Initialize it if not present before */
    1333         [ +  + ]:      110957 :     if (!found)
    1334                 :             :     {
    1335                 :        1912 :         char        oldstatus = page->status;
    1336                 :             : 
    1337   [ +  -  +  -  :       13384 :         MemSet(page, 0, sizeof(PagetableEntry));
          +  -  +  -  +  
                      + ]
    1338                 :        1912 :         page->status = oldstatus;
    1339                 :        1912 :         page->blockno = chunk_pageno;
    1340                 :        1912 :         page->ischunk = true;
    1341                 :             :         /* must count it too */
    1342                 :        1912 :         tbm->nentries++;
    1343                 :        1912 :         tbm->nchunks++;
    1344                 :             :     }
    1345         [ +  + ]:      109045 :     else if (!page->ischunk)
    1346                 :             :     {
    1347                 :         104 :         char        oldstatus = page->status;
    1348                 :             : 
    1349                 :             :         /* chunk header page was formerly non-lossy, make it lossy */
    1350   [ +  -  +  -  :         728 :         MemSet(page, 0, sizeof(PagetableEntry));
          +  -  +  -  +  
                      + ]
    1351                 :         104 :         page->status = oldstatus;
    1352                 :         104 :         page->blockno = chunk_pageno;
    1353                 :         104 :         page->ischunk = true;
    1354                 :             :         /* we assume it had some tuple bit(s) set, so mark it lossy */
    1355                 :         104 :         page->words[0] = ((bitmapword) 1 << 0);
    1356                 :             :         /* adjust counts */
    1357                 :         104 :         tbm->nchunks++;
    1358                 :         104 :         tbm->npages--;
    1359                 :             :     }
    1360                 :             : 
    1361                 :             :     /* Now set the original target page's bit */
    1362                 :      110957 :     wordnum = WORDNUM(bitno);
    1363                 :      110957 :     bitnum = BITNUM(bitno);
    1364                 :      110957 :     page->words[wordnum] |= ((bitmapword) 1 << bitnum);
    1365                 :      110957 : }
    1366                 :             : 
    1367                 :             : /*
    1368                 :             :  * tbm_lossify - lose some information to get back under the memory limit
    1369                 :             :  */
    1370                 :             : static void
    1371                 :          24 : tbm_lossify(TIDBitmap *tbm)
    1372                 :             : {
    1373                 :             :     pagetable_iterator i;
    1374                 :             :     PagetableEntry *page;
    1375                 :             : 
    1376                 :             :     /*
    1377                 :             :      * XXX Really stupid implementation: this just lossifies pages in
    1378                 :             :      * essentially random order.  We should be paying some attention to the
    1379                 :             :      * number of bits set in each page, instead.
    1380                 :             :      *
    1381                 :             :      * Since we are called as soon as nentries exceeds maxentries, we should
    1382                 :             :      * push nentries down to significantly less than maxentries, or else we'll
    1383                 :             :      * just end up doing this again very soon.  We shoot for maxentries/2.
    1384                 :             :      */
    1385                 :             :     Assert(tbm->iterating == TBM_NOT_ITERATING);
    1386                 :             :     Assert(tbm->status == TBM_HASH);
    1387                 :             : 
    1388                 :          24 :     pagetable_start_iterate_at(tbm->pagetable, &i, tbm->lossify_start);
    1389         [ +  - ]:       12328 :     while ((page = pagetable_iterate(tbm->pagetable, &i)) != NULL)
    1390                 :             :     {
    1391         [ -  + ]:       12328 :         if (page->ischunk)
    1392                 :           0 :             continue;           /* already a chunk header */
    1393                 :             : 
    1394                 :             :         /*
    1395                 :             :          * If the page would become a chunk header, we won't save anything by
    1396                 :             :          * converting it to lossy, so skip it.
    1397                 :             :          */
    1398         [ +  + ]:       12328 :         if ((page->blockno % PAGES_PER_CHUNK) == 0)
    1399                 :          16 :             continue;
    1400                 :             : 
    1401                 :             :         /* This does the dirty work ... */
    1402                 :       12312 :         tbm_mark_page_lossy(tbm, page->blockno);
    1403                 :             : 
    1404         [ +  + ]:       12312 :         if (tbm->nentries <= tbm->maxentries / 2)
    1405                 :             :         {
    1406                 :             :             /*
    1407                 :             :              * We have made enough room. Remember where to start lossifying
    1408                 :             :              * next round, so we evenly iterate over the hashtable.
    1409                 :             :              */
    1410                 :          24 :             tbm->lossify_start = i.cur;
    1411                 :          24 :             break;
    1412                 :             :         }
    1413                 :             : 
    1414                 :             :         /*
    1415                 :             :          * Note: tbm_mark_page_lossy may have inserted a lossy chunk into the
    1416                 :             :          * hashtable and may have deleted the non-lossy chunk.  We can
    1417                 :             :          * continue the same hash table scan, since failure to visit one
    1418                 :             :          * element or visiting the newly inserted element, isn't fatal.
    1419                 :             :          */
    1420                 :             :     }
    1421                 :             : 
    1422                 :             :     /*
    1423                 :             :      * With a big bitmap and small work_mem, it's possible that we cannot get
    1424                 :             :      * under maxentries.  Again, if that happens, we'd end up uselessly
    1425                 :             :      * calling tbm_lossify over and over.  To prevent this from becoming a
    1426                 :             :      * performance sink, force maxentries up to at least double the current
    1427                 :             :      * number of entries.  (In essence, we're admitting inability to fit
    1428                 :             :      * within work_mem when we do this.)  Note that this test will not fire if
    1429                 :             :      * we broke out of the loop early; and if we didn't, the current number of
    1430                 :             :      * entries is simply not reducible any further.
    1431                 :             :      */
    1432         [ -  + ]:          24 :     if (tbm->nentries > tbm->maxentries / 2)
    1433                 :           0 :         tbm->maxentries = Min(tbm->nentries, (INT_MAX - 1) / 2) * 2;
    1434                 :          24 : }
    1435                 :             : 
    1436                 :             : /*
    1437                 :             :  * qsort comparator to handle PagetableEntry pointers.
    1438                 :             :  */
    1439                 :             : static inline int
    1440                 :      921777 : tbm_comparator(const PagetableEntry *left, const PagetableEntry *right)
    1441                 :             : {
    1442         [ +  + ]:      921777 :     if (left->blockno < right->blockno)
    1443                 :      425917 :         return -1;
    1444         [ +  - ]:      495860 :     else if (left->blockno > right->blockno)
    1445                 :      495860 :         return 1;
    1446                 :           0 :     return 0;
    1447                 :             : }
    1448                 :             : 
    1449                 :             : /*
    1450                 :             :  * As above, but this will get index into PagetableEntry array.  Therefore,
    1451                 :             :  * it needs to get actual PagetableEntry using the index before comparing the
    1452                 :             :  * blockno.
    1453                 :             :  */
    1454                 :             : static inline int
    1455                 :      158860 : tbm_shared_comparator(const int *left, const int *right,
    1456                 :             :                       const PagetableEntry *base)
    1457                 :             : {
    1458                 :      158860 :     return tbm_comparator(&base[*left], &base[*right]);
    1459                 :             : }
    1460                 :             : 
    1461                 :             : /*
    1462                 :             :  *  tbm_attach_shared_iterate
    1463                 :             :  *
    1464                 :             :  *  Allocate a backend-private iterator and attach the shared iterator state
    1465                 :             :  *  to it so that multiple processed can iterate jointly.
    1466                 :             :  *
    1467                 :             :  *  We also converts the DSA pointers to local pointers and store them into
    1468                 :             :  *  our private iterator.
    1469                 :             :  */
    1470                 :             : TBMSharedIterator *
    1471                 :         228 : tbm_attach_shared_iterate(dsa_area *dsa, dsa_pointer dp)
    1472                 :             : {
    1473                 :             :     TBMSharedIterator *iterator;
    1474                 :             :     TBMSharedIteratorState *istate;
    1475                 :             : 
    1476                 :             :     /*
    1477                 :             :      * Create the TBMSharedIterator struct, with enough trailing space to
    1478                 :             :      * serve the needs of the TBMIterateResult sub-struct.
    1479                 :             :      */
    1480                 :         228 :     iterator = palloc0_object(TBMSharedIterator);
    1481                 :             : 
    1482                 :         228 :     istate = (TBMSharedIteratorState *) dsa_get_address(dsa, dp);
    1483                 :             : 
    1484                 :         228 :     iterator->state = istate;
    1485                 :             : 
    1486                 :         228 :     iterator->ptbase = dsa_get_address(dsa, istate->pagetable);
    1487                 :             : 
    1488         [ +  - ]:         228 :     if (istate->npages)
    1489                 :         228 :         iterator->ptpages = dsa_get_address(dsa, istate->spages);
    1490         [ +  + ]:         228 :     if (istate->nchunks)
    1491                 :          20 :         iterator->ptchunks = dsa_get_address(dsa, istate->schunks);
    1492                 :             : 
    1493                 :         228 :     return iterator;
    1494                 :             : }
    1495                 :             : 
    1496                 :             : /*
    1497                 :             :  * pagetable_allocate
    1498                 :             :  *
    1499                 :             :  * Callback function for allocating the memory for hashtable elements.
    1500                 :             :  * Allocate memory for hashtable elements, using DSA if available.
    1501                 :             :  */
    1502                 :             : static inline void *
    1503                 :        5791 : pagetable_allocate(pagetable_hash *pagetable, Size size)
    1504                 :             : {
    1505                 :        5791 :     TIDBitmap  *tbm = (TIDBitmap *) pagetable->private_data;
    1506                 :             :     PTEntryArray *ptbase;
    1507                 :             : 
    1508         [ +  + ]:        5791 :     if (tbm->dsa == NULL)
    1509                 :        5687 :         return MemoryContextAllocExtended(pagetable->ctx, size,
    1510                 :             :                                           MCXT_ALLOC_HUGE | MCXT_ALLOC_ZERO);
    1511                 :             : 
    1512                 :             :     /*
    1513                 :             :      * Save the dsapagetable reference in dsapagetableold before allocating
    1514                 :             :      * new memory so that pagetable_free can free the old entry.
    1515                 :             :      */
    1516                 :         104 :     tbm->dsapagetableold = tbm->dsapagetable;
    1517                 :         104 :     tbm->dsapagetable = dsa_allocate_extended(tbm->dsa,
    1518                 :             :                                               sizeof(PTEntryArray) + size,
    1519                 :             :                                               DSA_ALLOC_HUGE | DSA_ALLOC_ZERO);
    1520                 :         104 :     ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
    1521                 :             : 
    1522                 :         104 :     return ptbase->ptentry;
    1523                 :             : }
    1524                 :             : 
    1525                 :             : /*
    1526                 :             :  * pagetable_free
    1527                 :             :  *
    1528                 :             :  * Callback function for freeing hash table elements.
    1529                 :             :  */
    1530                 :             : static inline void
    1531                 :        5790 : pagetable_free(pagetable_hash *pagetable, void *pointer)
    1532                 :             : {
    1533                 :        5790 :     TIDBitmap  *tbm = (TIDBitmap *) pagetable->private_data;
    1534                 :             : 
    1535                 :             :     /* pfree the input pointer if DSA is not available */
    1536         [ +  + ]:        5790 :     if (tbm->dsa == NULL)
    1537                 :        5686 :         pfree(pointer);
    1538         [ +  + ]:         104 :     else if (DsaPointerIsValid(tbm->dsapagetableold))
    1539                 :             :     {
    1540                 :          56 :         dsa_free(tbm->dsa, tbm->dsapagetableold);
    1541                 :          56 :         tbm->dsapagetableold = InvalidDsaPointer;
    1542                 :             :     }
    1543                 :        5790 : }
    1544                 :             : 
    1545                 :             : /*
    1546                 :             :  * tbm_calculate_entries
    1547                 :             :  *
    1548                 :             :  * Estimate number of hashtable entries we can have within maxbytes.
    1549                 :             :  */
    1550                 :             : int
    1551                 :      598212 : tbm_calculate_entries(Size maxbytes)
    1552                 :             : {
    1553                 :             :     Size        nbuckets;
    1554                 :             : 
    1555                 :             :     /*
    1556                 :             :      * Estimate number of hashtable entries we can have within maxbytes. This
    1557                 :             :      * estimates the hash cost as sizeof(PagetableEntry), which is good enough
    1558                 :             :      * for our purpose.  Also count an extra Pointer per entry for the arrays
    1559                 :             :      * created during iteration readout.
    1560                 :             :      */
    1561                 :      598212 :     nbuckets = maxbytes /
    1562                 :             :         (sizeof(PagetableEntry) + sizeof(Pointer) + sizeof(Pointer));
    1563                 :      598212 :     nbuckets = Min(nbuckets, INT_MAX - 1);  /* safety limit */
    1564                 :      598212 :     nbuckets = Max(nbuckets, 16);   /* sanity limit */
    1565                 :             : 
    1566                 :      598212 :     return (int) nbuckets;
    1567                 :             : }
    1568                 :             : 
    1569                 :             : /*
    1570                 :             :  * Create a shared or private bitmap iterator and start iteration.
    1571                 :             :  *
    1572                 :             :  * `tbm` is only used to create the private iterator and dsa and dsp are only
    1573                 :             :  * used to create the shared iterator.
    1574                 :             :  *
    1575                 :             :  * Before invoking tbm_begin_iterate() to create a shared iterator, one
    1576                 :             :  * process must already have invoked tbm_prepare_shared_iterate() to create
    1577                 :             :  * and set up the TBMSharedIteratorState.
    1578                 :             :  */
    1579                 :             : TBMIterator
    1580                 :       15396 : tbm_begin_iterate(TIDBitmap *tbm, dsa_area *dsa, dsa_pointer dsp)
    1581                 :             : {
    1582                 :       15396 :     TBMIterator iterator = {0};
    1583                 :             : 
    1584                 :             :     /* Allocate a private iterator and attach the shared state to it */
    1585         [ +  + ]:       15396 :     if (DsaPointerIsValid(dsp))
    1586                 :             :     {
    1587                 :         228 :         iterator.shared = true;
    1588                 :         228 :         iterator.i.shared_iterator = tbm_attach_shared_iterate(dsa, dsp);
    1589                 :             :     }
    1590                 :             :     else
    1591                 :             :     {
    1592                 :       15168 :         iterator.shared = false;
    1593                 :       15168 :         iterator.i.private_iterator = tbm_begin_private_iterate(tbm);
    1594                 :             :     }
    1595                 :             : 
    1596                 :       15396 :     return iterator;
    1597                 :             : }
    1598                 :             : 
    1599                 :             : /*
    1600                 :             :  * Clean up shared or private bitmap iterator.
    1601                 :             :  */
    1602                 :             : void
    1603                 :       15315 : tbm_end_iterate(TBMIterator *iterator)
    1604                 :             : {
    1605                 :             :     Assert(iterator && !tbm_exhausted(iterator));
    1606                 :             : 
    1607         [ +  + ]:       15315 :     if (iterator->shared)
    1608                 :         228 :         tbm_end_shared_iterate(iterator->i.shared_iterator);
    1609                 :             :     else
    1610                 :       15087 :         tbm_end_private_iterate(iterator->i.private_iterator);
    1611                 :             : 
    1612                 :       15315 :     *iterator = (TBMIterator)
    1613                 :             :     {
    1614                 :             :         0
    1615                 :             :     };
    1616                 :       15315 : }
    1617                 :             : 
    1618                 :             : /*
    1619                 :             :  * Populate the next TBMIterateResult using the shared or private bitmap
    1620                 :             :  * iterator. Returns false when there is nothing more to scan.
    1621                 :             :  */
    1622                 :             : bool
    1623                 :      260735 : tbm_iterate(TBMIterator *iterator, TBMIterateResult *tbmres)
    1624                 :             : {
    1625                 :             :     Assert(iterator);
    1626                 :             :     Assert(tbmres);
    1627                 :             : 
    1628         [ +  + ]:      260735 :     if (iterator->shared)
    1629                 :       20300 :         return tbm_shared_iterate(iterator->i.shared_iterator, tbmres);
    1630                 :             :     else
    1631                 :      240435 :         return tbm_private_iterate(iterator->i.private_iterator, tbmres);
    1632                 :             : }
        

Generated by: LCOV version 2.0-1