PostgreSQL Source Code  git master
tidbitmap.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/htup_details.h"
#include "common/hashfn.h"
#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
#include "utils/dsa.h"
#include "lib/simplehash.h"
Include dependency graph for tidbitmap.c:

Go to the source code of this file.

Data Structures

struct  PagetableEntry
 
struct  PTEntryArray
 
struct  TIDBitmap
 
struct  TBMIterator
 
struct  TBMSharedIteratorState
 
struct  PTIterationArray
 
struct  TBMSharedIterator
 

Macros

#define MAX_TUPLES_PER_PAGE   MaxHeapTuplesPerPage
 
#define PAGES_PER_CHUNK   (BLCKSZ / 32)
 
#define WORDNUM(x)   ((x) / BITS_PER_BITMAPWORD)
 
#define BITNUM(x)   ((x) % BITS_PER_BITMAPWORD)
 
#define WORDS_PER_PAGE   ((MAX_TUPLES_PER_PAGE - 1) / BITS_PER_BITMAPWORD + 1)
 
#define WORDS_PER_CHUNK   ((PAGES_PER_CHUNK - 1) / BITS_PER_BITMAPWORD + 1)
 
#define SH_USE_NONDEFAULT_ALLOCATOR
 
#define SH_PREFIX   pagetable
 
#define SH_ELEMENT_TYPE   PagetableEntry
 
#define SH_KEY_TYPE   BlockNumber
 
#define SH_KEY   blockno
 
#define SH_HASH_KEY(tb, key)   murmurhash32(key)
 
#define SH_EQUAL(tb, a, b)   a == b
 
#define SH_SCOPE   static inline
 
#define SH_DEFINE
 
#define SH_DECLARE
 

Typedefs

typedef struct PagetableEntry PagetableEntry
 
typedef struct PTEntryArray PTEntryArray
 
typedef struct TBMSharedIteratorState TBMSharedIteratorState
 
typedef struct PTIterationArray PTIterationArray
 

Enumerations

enum  TBMStatus { TBM_EMPTY , TBM_ONE_PAGE , TBM_HASH }
 
enum  TBMIteratingState { TBM_NOT_ITERATING , TBM_ITERATING_PRIVATE , TBM_ITERATING_SHARED }
 

Functions

static void tbm_union_page (TIDBitmap *a, const PagetableEntry *bpage)
 
static bool tbm_intersect_page (TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
 
static const PagetableEntrytbm_find_pageentry (const TIDBitmap *tbm, BlockNumber pageno)
 
static PagetableEntrytbm_get_pageentry (TIDBitmap *tbm, BlockNumber pageno)
 
static bool tbm_page_is_lossy (const TIDBitmap *tbm, BlockNumber pageno)
 
static void tbm_mark_page_lossy (TIDBitmap *tbm, BlockNumber pageno)
 
static void tbm_lossify (TIDBitmap *tbm)
 
static int tbm_comparator (const void *left, const void *right)
 
static int tbm_shared_comparator (const void *left, const void *right, void *arg)
 
TIDBitmaptbm_create (long maxbytes, dsa_area *dsa)
 
static void tbm_create_pagetable (TIDBitmap *tbm)
 
void tbm_free (TIDBitmap *tbm)
 
void tbm_free_shared_area (dsa_area *dsa, dsa_pointer dp)
 
void tbm_add_tuples (TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
 
void tbm_add_page (TIDBitmap *tbm, BlockNumber pageno)
 
void tbm_union (TIDBitmap *a, const TIDBitmap *b)
 
void tbm_intersect (TIDBitmap *a, const TIDBitmap *b)
 
bool tbm_is_empty (const TIDBitmap *tbm)
 
TBMIteratortbm_begin_iterate (TIDBitmap *tbm)
 
dsa_pointer tbm_prepare_shared_iterate (TIDBitmap *tbm)
 
static int tbm_extract_page_tuple (PagetableEntry *page, TBMIterateResult *output)
 
static void tbm_advance_schunkbit (PagetableEntry *chunk, int *schunkbitp)
 
TBMIterateResulttbm_iterate (TBMIterator *iterator)
 
TBMIterateResulttbm_shared_iterate (TBMSharedIterator *iterator)
 
void tbm_end_iterate (TBMIterator *iterator)
 
void tbm_end_shared_iterate (TBMSharedIterator *iterator)
 
TBMSharedIteratortbm_attach_shared_iterate (dsa_area *dsa, dsa_pointer dp)
 
static void * pagetable_allocate (pagetable_hash *pagetable, Size size)
 
static void pagetable_free (pagetable_hash *pagetable, void *pointer)
 
long tbm_calculate_entries (double maxbytes)
 

Macro Definition Documentation

◆ BITNUM

#define BITNUM (   x)    ((x) % BITS_PER_BITMAPWORD)

Definition at line 79 of file tidbitmap.c.

◆ MAX_TUPLES_PER_PAGE

#define MAX_TUPLES_PER_PAGE   MaxHeapTuplesPerPage

Definition at line 57 of file tidbitmap.c.

◆ PAGES_PER_CHUNK

#define PAGES_PER_CHUNK   (BLCKSZ / 32)

Definition at line 74 of file tidbitmap.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 252 of file tidbitmap.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 251 of file tidbitmap.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PagetableEntry

Definition at line 245 of file tidbitmap.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)    a == b

Definition at line 249 of file tidbitmap.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    murmurhash32(key)

Definition at line 248 of file tidbitmap.c.

◆ SH_KEY

#define SH_KEY   blockno

Definition at line 247 of file tidbitmap.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   BlockNumber

Definition at line 246 of file tidbitmap.c.

◆ SH_PREFIX

#define SH_PREFIX   pagetable

Definition at line 244 of file tidbitmap.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 250 of file tidbitmap.c.

◆ SH_USE_NONDEFAULT_ALLOCATOR

#define SH_USE_NONDEFAULT_ALLOCATOR

Definition at line 243 of file tidbitmap.c.

◆ WORDNUM

#define WORDNUM (   x)    ((x) / BITS_PER_BITMAPWORD)

Definition at line 78 of file tidbitmap.c.

◆ WORDS_PER_CHUNK

#define WORDS_PER_CHUNK   ((PAGES_PER_CHUNK - 1) / BITS_PER_BITMAPWORD + 1)

Definition at line 84 of file tidbitmap.c.

◆ WORDS_PER_PAGE

#define WORDS_PER_PAGE   ((MAX_TUPLES_PER_PAGE - 1) / BITS_PER_BITMAPWORD + 1)

Definition at line 82 of file tidbitmap.c.

Typedef Documentation

◆ PagetableEntry

◆ PTEntryArray

typedef struct PTEntryArray PTEntryArray

◆ PTIterationArray

◆ TBMSharedIteratorState

Enumeration Type Documentation

◆ TBMIteratingState

Enumerator
TBM_NOT_ITERATING 
TBM_ITERATING_PRIVATE 
TBM_ITERATING_SHARED 

Definition at line 139 of file tidbitmap.c.

140 {
141  TBM_NOT_ITERATING, /* not yet converted to page and chunk array */
142  TBM_ITERATING_PRIVATE, /* converted to local page and chunk array */
143  TBM_ITERATING_SHARED, /* converted to shared page and chunk array */
TBMIteratingState
Definition: tidbitmap.c:140
@ TBM_ITERATING_SHARED
Definition: tidbitmap.c:143
@ TBM_NOT_ITERATING
Definition: tidbitmap.c:141
@ TBM_ITERATING_PRIVATE
Definition: tidbitmap.c:142

◆ TBMStatus

enum TBMStatus
Enumerator
TBM_EMPTY 
TBM_ONE_PAGE 
TBM_HASH 

Definition at line 129 of file tidbitmap.c.

130 {
131  TBM_EMPTY, /* no hashtable, nentries == 0 */
132  TBM_ONE_PAGE, /* entry1 contains the single entry */
133  TBM_HASH, /* pagetable is valid, entry1 is not */
134 } TBMStatus;
TBMStatus
Definition: tidbitmap.c:130
@ TBM_EMPTY
Definition: tidbitmap.c:131
@ TBM_ONE_PAGE
Definition: tidbitmap.c:132
@ TBM_HASH
Definition: tidbitmap.c:133

Function Documentation

◆ pagetable_allocate()

static void* pagetable_allocate ( pagetable_hash *  pagetable,
Size  size 
)
inlinestatic

Definition at line 1494 of file tidbitmap.c.

1495 {
1496  TIDBitmap *tbm = (TIDBitmap *) pagetable->private_data;
1497  PTEntryArray *ptbase;
1498 
1499  if (tbm->dsa == NULL)
1500  return MemoryContextAllocExtended(pagetable->ctx, size,
1502 
1503  /*
1504  * Save the dsapagetable reference in dsapagetableold before allocating
1505  * new memory so that pagetable_free can free the old entry.
1506  */
1507  tbm->dsapagetableold = tbm->dsapagetable;
1509  sizeof(PTEntryArray) + size,
1511  ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
1512 
1513  return ptbase->ptentry;
1514 }
dsa_pointer dsa_allocate_extended(dsa_area *area, size_t size, int flags)
Definition: dsa.c:684
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:955
#define DSA_ALLOC_HUGE
Definition: dsa.h:73
#define DSA_ALLOC_ZERO
Definition: dsa.h:75
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:18
#define MCXT_ALLOC_HUGE
Definition: fe_memutils.h:16
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1225
static pg_noinline void Size size
Definition: slab.c:607
dsa_pointer dsapagetableold
Definition: tidbitmap.c:166
dsa_area * dsa
Definition: tidbitmap.c:169
dsa_pointer dsapagetable
Definition: tidbitmap.c:165

References TIDBitmap::dsa, DSA_ALLOC_HUGE, DSA_ALLOC_ZERO, dsa_allocate_extended(), dsa_get_address(), TIDBitmap::dsapagetable, TIDBitmap::dsapagetableold, if(), MCXT_ALLOC_HUGE, MCXT_ALLOC_ZERO, MemoryContextAllocExtended(), and size.

◆ pagetable_free()

static void pagetable_free ( pagetable_hash *  pagetable,
void *  pointer 
)
inlinestatic

Definition at line 1522 of file tidbitmap.c.

1523 {
1524  TIDBitmap *tbm = (TIDBitmap *) pagetable->private_data;
1525 
1526  /* pfree the input pointer if DSA is not available */
1527  if (tbm->dsa == NULL)
1528  pfree(pointer);
1529  else if (DsaPointerIsValid(tbm->dsapagetableold))
1530  {
1531  dsa_free(tbm->dsa, tbm->dsapagetableold);
1533  }
1534 }
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:839
#define InvalidDsaPointer
Definition: dsa.h:78
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void pfree(void *pointer)
Definition: mcxt.c:1508

References TIDBitmap::dsa, dsa_free(), TIDBitmap::dsapagetableold, DsaPointerIsValid, if(), InvalidDsaPointer, and pfree().

◆ tbm_add_page()

void tbm_add_page ( TIDBitmap tbm,
BlockNumber  pageno 
)

Definition at line 443 of file tidbitmap.c.

444 {
445  /* Enter the page in the bitmap, or mark it lossy if already present */
446  tbm_mark_page_lossy(tbm, pageno);
447  /* If we went over the memory limit, lossify some more pages */
448  if (tbm->nentries > tbm->maxentries)
449  tbm_lossify(tbm);
450 }
int nentries
Definition: tidbitmap.c:155
int maxentries
Definition: tidbitmap.c:156
static void tbm_lossify(TIDBitmap *tbm)
Definition: tidbitmap.c:1355
static void tbm_mark_page_lossy(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:1283

References TIDBitmap::maxentries, TIDBitmap::nentries, tbm_lossify(), and tbm_mark_page_lossy().

Referenced by bringetbitmap(), and gingetbitmap().

◆ tbm_add_tuples()

void tbm_add_tuples ( TIDBitmap tbm,
const ItemPointer  tids,
int  ntids,
bool  recheck 
)

Definition at line 377 of file tidbitmap.c.

379 {
381  PagetableEntry *page = NULL; /* only valid when currblk is valid */
382  int i;
383 
385  for (i = 0; i < ntids; i++)
386  {
389  int wordnum,
390  bitnum;
391 
392  /* safety check to ensure we don't overrun bit array bounds */
393  if (off < 1 || off > MAX_TUPLES_PER_PAGE)
394  elog(ERROR, "tuple offset out of range: %u", off);
395 
396  /*
397  * Look up target page unless we already did. This saves cycles when
398  * the input includes consecutive tuples on the same page, which is
399  * common enough to justify an extra test here.
400  */
401  if (blk != currblk)
402  {
403  if (tbm_page_is_lossy(tbm, blk))
404  page = NULL; /* remember page is lossy */
405  else
406  page = tbm_get_pageentry(tbm, blk);
407  currblk = blk;
408  }
409 
410  if (page == NULL)
411  continue; /* whole page is already marked */
412 
413  if (page->ischunk)
414  {
415  /* The page is a lossy chunk header, set bit for itself */
416  wordnum = bitnum = 0;
417  }
418  else
419  {
420  /* Page is exact, so set bit for individual tuple */
421  wordnum = WORDNUM(off - 1);
422  bitnum = BITNUM(off - 1);
423  }
424  page->words[wordnum] |= ((bitmapword) 1 << bitnum);
425  page->recheck |= recheck;
426 
427  if (tbm->nentries > tbm->maxentries)
428  {
429  tbm_lossify(tbm);
430  /* Page could have been converted to lossy, so force new lookup */
431  currblk = InvalidBlockNumber;
432  }
433  }
434 }
uint32 bitmapword
Definition: bitmapset.h:44
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
int i
Definition: isn.c:73
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
Assert(fmt[strlen(fmt) - 1] !='\n')
uint16 OffsetNumber
Definition: off.h:24
bitmapword words[Max(WORDS_PER_PAGE, WORDS_PER_CHUNK)]
Definition: tidbitmap.c:106
TBMIteratingState iterating
Definition: tidbitmap.c:159
#define MAX_TUPLES_PER_PAGE
Definition: tidbitmap.c:57
#define WORDNUM(x)
Definition: tidbitmap.c:78
static bool tbm_page_is_lossy(const TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:1249
#define BITNUM(x)
Definition: tidbitmap.c:79
static PagetableEntry * tbm_get_pageentry(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:1202

References Assert(), BITNUM, elog, ERROR, i, InvalidBlockNumber, PagetableEntry::ischunk, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), TIDBitmap::iterating, MAX_TUPLES_PER_PAGE, TIDBitmap::maxentries, TIDBitmap::nentries, PagetableEntry::recheck, tbm_get_pageentry(), tbm_lossify(), TBM_NOT_ITERATING, tbm_page_is_lossy(), WORDNUM, and PagetableEntry::words.

Referenced by blgetbitmap(), btgetbitmap(), collectMatchBitmap(), GinDataLeafPageGetItemsToTbm(), gingetbitmap(), ginPostingListDecodeAllSegmentsToTbm(), gistScanPage(), hashgetbitmap(), scanPendingInsert(), and storeBitmap().

◆ tbm_advance_schunkbit()

static void tbm_advance_schunkbit ( PagetableEntry chunk,
int *  schunkbitp 
)
inlinestatic

Definition at line 941 of file tidbitmap.c.

942 {
943  int schunkbit = *schunkbitp;
944 
945  while (schunkbit < PAGES_PER_CHUNK)
946  {
947  int wordnum = WORDNUM(schunkbit);
948  int bitnum = BITNUM(schunkbit);
949 
950  if ((chunk->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
951  break;
952  schunkbit++;
953  }
954 
955  *schunkbitp = schunkbit;
956 }
#define PAGES_PER_CHUNK
Definition: tidbitmap.c:74

References BITNUM, PAGES_PER_CHUNK, WORDNUM, and PagetableEntry::words.

Referenced by tbm_iterate(), and tbm_shared_iterate().

◆ tbm_attach_shared_iterate()

TBMSharedIterator* tbm_attach_shared_iterate ( dsa_area dsa,
dsa_pointer  dp 
)

Definition at line 1461 of file tidbitmap.c.

1462 {
1463  TBMSharedIterator *iterator;
1464  TBMSharedIteratorState *istate;
1465 
1466  /*
1467  * Create the TBMSharedIterator struct, with enough trailing space to
1468  * serve the needs of the TBMIterateResult sub-struct.
1469  */
1470  iterator = (TBMSharedIterator *) palloc0(sizeof(TBMSharedIterator) +
1471  MAX_TUPLES_PER_PAGE * sizeof(OffsetNumber));
1472 
1473  istate = (TBMSharedIteratorState *) dsa_get_address(dsa, dp);
1474 
1475  iterator->state = istate;
1476 
1477  iterator->ptbase = dsa_get_address(dsa, istate->pagetable);
1478 
1479  if (istate->npages)
1480  iterator->ptpages = dsa_get_address(dsa, istate->spages);
1481  if (istate->nchunks)
1482  iterator->ptchunks = dsa_get_address(dsa, istate->schunks);
1483 
1484  return iterator;
1485 }
void * palloc0(Size size)
Definition: mcxt.c:1334
dsa_pointer pagetable
Definition: tidbitmap.c:197
dsa_pointer spages
Definition: tidbitmap.c:198
dsa_pointer schunks
Definition: tidbitmap.c:199
TBMSharedIteratorState * state
Definition: tidbitmap.c:221
PTEntryArray * ptbase
Definition: tidbitmap.c:222
PTIterationArray * ptchunks
Definition: tidbitmap.c:224
PTIterationArray * ptpages
Definition: tidbitmap.c:223

References dsa_get_address(), MAX_TUPLES_PER_PAGE, TBMSharedIteratorState::nchunks, TBMSharedIteratorState::npages, TBMSharedIteratorState::pagetable, palloc0(), TBMSharedIterator::ptbase, TBMSharedIterator::ptchunks, TBMSharedIterator::ptpages, TBMSharedIteratorState::schunks, TBMSharedIteratorState::spages, and TBMSharedIterator::state.

Referenced by BitmapHeapNext().

◆ tbm_begin_iterate()

TBMIterator* tbm_begin_iterate ( TIDBitmap tbm)

Definition at line 689 of file tidbitmap.c.

690 {
691  TBMIterator *iterator;
692 
694 
695  /*
696  * Create the TBMIterator struct, with enough trailing space to serve the
697  * needs of the TBMIterateResult sub-struct.
698  */
699  iterator = (TBMIterator *) palloc(sizeof(TBMIterator) +
701  iterator->tbm = tbm;
702 
703  /*
704  * Initialize iteration pointers.
705  */
706  iterator->spageptr = 0;
707  iterator->schunkptr = 0;
708  iterator->schunkbit = 0;
709 
710  /*
711  * If we have a hashtable, create and fill the sorted page lists, unless
712  * we already did that for a previous iterator. Note that the lists are
713  * attached to the bitmap not the iterator, so they can be used by more
714  * than one iterator.
715  */
716  if (tbm->status == TBM_HASH && tbm->iterating == TBM_NOT_ITERATING)
717  {
718  pagetable_iterator i;
719  PagetableEntry *page;
720  int npages;
721  int nchunks;
722 
723  if (!tbm->spages && tbm->npages > 0)
724  tbm->spages = (PagetableEntry **)
726  tbm->npages * sizeof(PagetableEntry *));
727  if (!tbm->schunks && tbm->nchunks > 0)
728  tbm->schunks = (PagetableEntry **)
730  tbm->nchunks * sizeof(PagetableEntry *));
731 
732  npages = nchunks = 0;
733  pagetable_start_iterate(tbm->pagetable, &i);
734  while ((page = pagetable_iterate(tbm->pagetable, &i)) != NULL)
735  {
736  if (page->ischunk)
737  tbm->schunks[nchunks++] = page;
738  else
739  tbm->spages[npages++] = page;
740  }
741  Assert(npages == tbm->npages);
742  Assert(nchunks == tbm->nchunks);
743  if (npages > 1)
744  qsort(tbm->spages, npages, sizeof(PagetableEntry *),
746  if (nchunks > 1)
747  qsort(tbm->schunks, nchunks, sizeof(PagetableEntry *),
749  }
750 
752 
753  return iterator;
754 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1168
void * palloc(Size size)
Definition: mcxt.c:1304
#define qsort(a, b, c, d)
Definition: port.h:449
int spageptr
Definition: tidbitmap.c:181
TIDBitmap * tbm
Definition: tidbitmap.c:180
int schunkbit
Definition: tidbitmap.c:183
int schunkptr
Definition: tidbitmap.c:182
struct pagetable_hash * pagetable
Definition: tidbitmap.c:154
PagetableEntry ** schunks
Definition: tidbitmap.c:164
MemoryContext mcxt
Definition: tidbitmap.c:152
int npages
Definition: tidbitmap.c:157
int nchunks
Definition: tidbitmap.c:158
TBMStatus status
Definition: tidbitmap.c:153
PagetableEntry ** spages
Definition: tidbitmap.c:163
static int tbm_comparator(const void *left, const void *right)
Definition: tidbitmap.c:1424

References Assert(), i, PagetableEntry::ischunk, TIDBitmap::iterating, MAX_TUPLES_PER_PAGE, TIDBitmap::mcxt, MemoryContextAlloc(), TIDBitmap::nchunks, TIDBitmap::npages, TIDBitmap::pagetable, palloc(), qsort, TBMIterator::schunkbit, TBMIterator::schunkptr, TIDBitmap::schunks, TBMIterator::spageptr, TIDBitmap::spages, TIDBitmap::status, TBMIterator::tbm, tbm_comparator(), TBM_HASH, TBM_ITERATING_PRIVATE, TBM_ITERATING_SHARED, and TBM_NOT_ITERATING.

Referenced by BitmapHeapNext(), and startScanEntry().

◆ tbm_calculate_entries()

long tbm_calculate_entries ( double  maxbytes)

Definition at line 1542 of file tidbitmap.c.

1543 {
1544  long nbuckets;
1545 
1546  /*
1547  * Estimate number of hashtable entries we can have within maxbytes. This
1548  * estimates the hash cost as sizeof(PagetableEntry), which is good enough
1549  * for our purpose. Also count an extra Pointer per entry for the arrays
1550  * created during iteration readout.
1551  */
1552  nbuckets = maxbytes /
1553  (sizeof(PagetableEntry) + sizeof(Pointer) + sizeof(Pointer));
1554  nbuckets = Min(nbuckets, INT_MAX - 1); /* safety limit */
1555  nbuckets = Max(nbuckets, 16); /* sanity limit */
1556 
1557  return nbuckets;
1558 }
#define Min(x, y)
Definition: c.h:991
#define Max(x, y)
Definition: c.h:985
char * Pointer
Definition: c.h:470
struct PagetableEntry PagetableEntry

References Max, and Min.

Referenced by compute_bitmap_pages(), and tbm_create().

◆ tbm_comparator()

static int tbm_comparator ( const void *  left,
const void *  right 
)
static

Definition at line 1424 of file tidbitmap.c.

1425 {
1426  BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
1427  BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
1428 
1429  return pg_cmp_u32(l, r);
1430 }
static int pg_cmp_u32(uint32 a, uint32 b)
Definition: int.h:489

References pg_cmp_u32().

Referenced by tbm_begin_iterate().

◆ tbm_create()

TIDBitmap* tbm_create ( long  maxbytes,
dsa_area dsa 
)

Definition at line 266 of file tidbitmap.c.

267 {
268  TIDBitmap *tbm;
269 
270  /* Create the TIDBitmap struct and zero all its fields */
271  tbm = makeNode(TIDBitmap);
272 
273  tbm->mcxt = CurrentMemoryContext;
274  tbm->status = TBM_EMPTY;
275 
276  tbm->maxentries = (int) tbm_calculate_entries(maxbytes);
277  tbm->lossify_start = 0;
278  tbm->dsa = dsa;
281  tbm->ptpages = InvalidDsaPointer;
283 
284  return tbm;
285 }
MemoryContext CurrentMemoryContext
Definition: mcxt.c:131
#define makeNode(_type_)
Definition: nodes.h:155
dsa_pointer ptpages
Definition: tidbitmap.c:167
uint32 lossify_start
Definition: tidbitmap.c:160
dsa_pointer ptchunks
Definition: tidbitmap.c:168
long tbm_calculate_entries(double maxbytes)
Definition: tidbitmap.c:1542

References CurrentMemoryContext, TIDBitmap::dsa, TIDBitmap::dsapagetable, TIDBitmap::dsapagetableold, InvalidDsaPointer, TIDBitmap::lossify_start, makeNode, TIDBitmap::maxentries, TIDBitmap::mcxt, TIDBitmap::ptchunks, TIDBitmap::ptpages, TIDBitmap::status, tbm_calculate_entries(), and TBM_EMPTY.

Referenced by collectMatchBitmap(), MultiExecBitmapIndexScan(), and MultiExecBitmapOr().

◆ tbm_create_pagetable()

static void tbm_create_pagetable ( TIDBitmap tbm)
static

Definition at line 292 of file tidbitmap.c.

293 {
294  Assert(tbm->status != TBM_HASH);
295  Assert(tbm->pagetable == NULL);
296 
297  tbm->pagetable = pagetable_create(tbm->mcxt, 128, tbm);
298 
299  /* If entry1 is valid, push it into the hashtable */
300  if (tbm->status == TBM_ONE_PAGE)
301  {
302  PagetableEntry *page;
303  bool found;
304  char oldstatus;
305 
306  page = pagetable_insert(tbm->pagetable,
307  tbm->entry1.blockno,
308  &found);
309  Assert(!found);
310  oldstatus = page->status;
311  memcpy(page, &tbm->entry1, sizeof(PagetableEntry));
312  page->status = oldstatus;
313  }
314 
315  tbm->status = TBM_HASH;
316 }
BlockNumber blockno
Definition: tidbitmap.c:102
PagetableEntry entry1
Definition: tidbitmap.c:161

References Assert(), PagetableEntry::blockno, TIDBitmap::entry1, TIDBitmap::mcxt, TIDBitmap::pagetable, PagetableEntry::status, TIDBitmap::status, TBM_HASH, and TBM_ONE_PAGE.

Referenced by tbm_get_pageentry(), and tbm_mark_page_lossy().

◆ tbm_end_iterate()

void tbm_end_iterate ( TBMIterator iterator)

Definition at line 1146 of file tidbitmap.c.

1147 {
1148  pfree(iterator);
1149 }

References pfree().

Referenced by BitmapPrefetch(), entryGetItem(), ExecEndBitmapHeapScan(), ExecReScanBitmapHeapScan(), ginFreeScanKeys(), and startScanEntry().

◆ tbm_end_shared_iterate()

void tbm_end_shared_iterate ( TBMSharedIterator iterator)

Definition at line 1158 of file tidbitmap.c.

1159 {
1160  pfree(iterator);
1161 }

References pfree().

Referenced by BitmapPrefetch(), ExecEndBitmapHeapScan(), and ExecReScanBitmapHeapScan().

◆ tbm_extract_page_tuple()

static int tbm_extract_page_tuple ( PagetableEntry page,
TBMIterateResult output 
)
inlinestatic

Definition at line 911 of file tidbitmap.c.

912 {
913  int wordnum;
914  int ntuples = 0;
915 
916  for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
917  {
918  bitmapword w = page->words[wordnum];
919 
920  if (w != 0)
921  {
922  int off = wordnum * BITS_PER_BITMAPWORD + 1;
923 
924  while (w != 0)
925  {
926  if (w & 1)
927  output->offsets[ntuples++] = (OffsetNumber) off;
928  off++;
929  w >>= 1;
930  }
931  }
932  }
933 
934  return ntuples;
935 }
#define BITS_PER_BITMAPWORD
Definition: bitmapset.h:43
FILE * output
#define WORDS_PER_PAGE
Definition: tidbitmap.c:82

References BITS_PER_BITMAPWORD, output, PagetableEntry::words, and WORDS_PER_PAGE.

Referenced by tbm_iterate(), and tbm_shared_iterate().

◆ tbm_find_pageentry()

static const PagetableEntry * tbm_find_pageentry ( const TIDBitmap tbm,
BlockNumber  pageno 
)
static

Definition at line 1169 of file tidbitmap.c.

1170 {
1171  const PagetableEntry *page;
1172 
1173  if (tbm->nentries == 0) /* in case pagetable doesn't exist */
1174  return NULL;
1175 
1176  if (tbm->status == TBM_ONE_PAGE)
1177  {
1178  page = &tbm->entry1;
1179  if (page->blockno != pageno)
1180  return NULL;
1181  Assert(!page->ischunk);
1182  return page;
1183  }
1184 
1185  page = pagetable_lookup(tbm->pagetable, pageno);
1186  if (page == NULL)
1187  return NULL;
1188  if (page->ischunk)
1189  return NULL; /* don't want a lossy chunk header */
1190  return page;
1191 }

References Assert(), PagetableEntry::blockno, TIDBitmap::entry1, PagetableEntry::ischunk, TIDBitmap::nentries, TIDBitmap::pagetable, TIDBitmap::status, and TBM_ONE_PAGE.

Referenced by tbm_intersect_page().

◆ tbm_free()

void tbm_free ( TIDBitmap tbm)

Definition at line 322 of file tidbitmap.c.

323 {
324  if (tbm->pagetable)
325  pagetable_destroy(tbm->pagetable);
326  if (tbm->spages)
327  pfree(tbm->spages);
328  if (tbm->schunks)
329  pfree(tbm->schunks);
330  pfree(tbm);
331 }

References TIDBitmap::pagetable, pfree(), TIDBitmap::schunks, and TIDBitmap::spages.

Referenced by ExecEndBitmapHeapScan(), ExecReScanBitmapHeapScan(), ginFreeScanKeys(), MultiExecBitmapAnd(), MultiExecBitmapOr(), and startScanEntry().

◆ tbm_free_shared_area()

void tbm_free_shared_area ( dsa_area dsa,
dsa_pointer  dp 
)

Definition at line 341 of file tidbitmap.c.

342 {
343  TBMSharedIteratorState *istate = dsa_get_address(dsa, dp);
344  PTEntryArray *ptbase;
345  PTIterationArray *ptpages;
346  PTIterationArray *ptchunks;
347 
348  if (DsaPointerIsValid(istate->pagetable))
349  {
350  ptbase = dsa_get_address(dsa, istate->pagetable);
351  if (pg_atomic_sub_fetch_u32(&ptbase->refcount, 1) == 0)
352  dsa_free(dsa, istate->pagetable);
353  }
354  if (DsaPointerIsValid(istate->spages))
355  {
356  ptpages = dsa_get_address(dsa, istate->spages);
357  if (pg_atomic_sub_fetch_u32(&ptpages->refcount, 1) == 0)
358  dsa_free(dsa, istate->spages);
359  }
360  if (DsaPointerIsValid(istate->schunks))
361  {
362  ptchunks = dsa_get_address(dsa, istate->schunks);
363  if (pg_atomic_sub_fetch_u32(&ptchunks->refcount, 1) == 0)
364  dsa_free(dsa, istate->schunks);
365  }
366 
367  dsa_free(dsa, dp);
368 }
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:434
pg_atomic_uint32 refcount
Definition: tidbitmap.c:114
pg_atomic_uint32 refcount
Definition: tidbitmap.c:211

References dsa_free(), dsa_get_address(), DsaPointerIsValid, TBMSharedIteratorState::pagetable, pg_atomic_sub_fetch_u32(), PTEntryArray::refcount, PTIterationArray::refcount, TBMSharedIteratorState::schunks, and TBMSharedIteratorState::spages.

Referenced by ExecBitmapHeapReInitializeDSM().

◆ tbm_get_pageentry()

static PagetableEntry * tbm_get_pageentry ( TIDBitmap tbm,
BlockNumber  pageno 
)
static

Definition at line 1202 of file tidbitmap.c.

1203 {
1204  PagetableEntry *page;
1205  bool found;
1206 
1207  if (tbm->status == TBM_EMPTY)
1208  {
1209  /* Use the fixed slot */
1210  page = &tbm->entry1;
1211  found = false;
1212  tbm->status = TBM_ONE_PAGE;
1213  }
1214  else
1215  {
1216  if (tbm->status == TBM_ONE_PAGE)
1217  {
1218  page = &tbm->entry1;
1219  if (page->blockno == pageno)
1220  return page;
1221  /* Time to switch from one page to a hashtable */
1222  tbm_create_pagetable(tbm);
1223  }
1224 
1225  /* Look up or create an entry */
1226  page = pagetable_insert(tbm->pagetable, pageno, &found);
1227  }
1228 
1229  /* Initialize it if not present before */
1230  if (!found)
1231  {
1232  char oldstatus = page->status;
1233 
1234  MemSet(page, 0, sizeof(PagetableEntry));
1235  page->status = oldstatus;
1236  page->blockno = pageno;
1237  /* must count it too */
1238  tbm->nentries++;
1239  tbm->npages++;
1240  }
1241 
1242  return page;
1243 }
#define MemSet(start, val, len)
Definition: c.h:1007
static void tbm_create_pagetable(TIDBitmap *tbm)
Definition: tidbitmap.c:292

References PagetableEntry::blockno, TIDBitmap::entry1, MemSet, TIDBitmap::nentries, TIDBitmap::npages, TIDBitmap::pagetable, PagetableEntry::status, TIDBitmap::status, tbm_create_pagetable(), TBM_EMPTY, and TBM_ONE_PAGE.

Referenced by tbm_add_tuples(), and tbm_union_page().

◆ tbm_intersect()

void tbm_intersect ( TIDBitmap a,
const TIDBitmap b 
)

Definition at line 540 of file tidbitmap.c.

541 {
542  Assert(!a->iterating);
543  /* Nothing to do if a is empty */
544  if (a->nentries == 0)
545  return;
546  /* Scan through chunks and pages in a, try to match to b */
547  if (a->status == TBM_ONE_PAGE)
548  {
549  if (tbm_intersect_page(a, &a->entry1, b))
550  {
551  /* Page is now empty, remove it from a */
552  Assert(!a->entry1.ischunk);
553  a->npages--;
554  a->nentries--;
555  Assert(a->nentries == 0);
556  a->status = TBM_EMPTY;
557  }
558  }
559  else
560  {
561  pagetable_iterator i;
562  PagetableEntry *apage;
563 
564  Assert(a->status == TBM_HASH);
565  pagetable_start_iterate(a->pagetable, &i);
566  while ((apage = pagetable_iterate(a->pagetable, &i)) != NULL)
567  {
568  if (tbm_intersect_page(a, apage, b))
569  {
570  /* Page or chunk is now empty, remove it from a */
571  if (apage->ischunk)
572  a->nchunks--;
573  else
574  a->npages--;
575  a->nentries--;
576  if (!pagetable_delete(a->pagetable, apage->blockno))
577  elog(ERROR, "hash table corrupted");
578  }
579  }
580  }
581 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
static bool tbm_intersect_page(TIDBitmap *a, PagetableEntry *apage, const TIDBitmap *b)
Definition: tidbitmap.c:589

References a, Assert(), b, PagetableEntry::blockno, elog, ERROR, i, PagetableEntry::ischunk, TBM_EMPTY, TBM_HASH, tbm_intersect_page(), and TBM_ONE_PAGE.

Referenced by MultiExecBitmapAnd().

◆ tbm_intersect_page()

static bool tbm_intersect_page ( TIDBitmap a,
PagetableEntry apage,
const TIDBitmap b 
)
static

Definition at line 589 of file tidbitmap.c.

590 {
591  const PagetableEntry *bpage;
592  int wordnum;
593 
594  if (apage->ischunk)
595  {
596  /* Scan each bit in chunk, try to clear */
597  bool candelete = true;
598 
599  for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
600  {
601  bitmapword w = apage->words[wordnum];
602 
603  if (w != 0)
604  {
605  bitmapword neww = w;
606  BlockNumber pg;
607  int bitnum;
608 
609  pg = apage->blockno + (wordnum * BITS_PER_BITMAPWORD);
610  bitnum = 0;
611  while (w != 0)
612  {
613  if (w & 1)
614  {
615  if (!tbm_page_is_lossy(b, pg) &&
616  tbm_find_pageentry(b, pg) == NULL)
617  {
618  /* Page is not in b at all, lose lossy bit */
619  neww &= ~((bitmapword) 1 << bitnum);
620  }
621  }
622  pg++;
623  bitnum++;
624  w >>= 1;
625  }
626  apage->words[wordnum] = neww;
627  if (neww != 0)
628  candelete = false;
629  }
630  }
631  return candelete;
632  }
633  else if (tbm_page_is_lossy(b, apage->blockno))
634  {
635  /*
636  * Some of the tuples in 'a' might not satisfy the quals for 'b', but
637  * because the page 'b' is lossy, we don't know which ones. Therefore
638  * we mark 'a' as requiring rechecks, to indicate that at most those
639  * tuples set in 'a' are matches.
640  */
641  apage->recheck = true;
642  return false;
643  }
644  else
645  {
646  bool candelete = true;
647 
648  bpage = tbm_find_pageentry(b, apage->blockno);
649  if (bpage != NULL)
650  {
651  /* Both pages are exact, merge at the bit level */
652  Assert(!bpage->ischunk);
653  for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
654  {
655  apage->words[wordnum] &= bpage->words[wordnum];
656  if (apage->words[wordnum] != 0)
657  candelete = false;
658  }
659  apage->recheck |= bpage->recheck;
660  }
661  /* If there is no matching b page, we can just delete the a page */
662  return candelete;
663  }
664 }
static const PagetableEntry * tbm_find_pageentry(const TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:1169
#define WORDS_PER_CHUNK
Definition: tidbitmap.c:84

References Assert(), b, BITS_PER_BITMAPWORD, PagetableEntry::blockno, PagetableEntry::ischunk, PagetableEntry::recheck, tbm_find_pageentry(), tbm_page_is_lossy(), PagetableEntry::words, WORDS_PER_CHUNK, and WORDS_PER_PAGE.

Referenced by tbm_intersect().

◆ tbm_is_empty()

bool tbm_is_empty ( const TIDBitmap tbm)

Definition at line 670 of file tidbitmap.c.

671 {
672  return (tbm->nentries == 0);
673 }

References TIDBitmap::nentries.

Referenced by MultiExecBitmapAnd(), and startScanEntry().

◆ tbm_iterate()

TBMIterateResult* tbm_iterate ( TBMIterator iterator)

Definition at line 971 of file tidbitmap.c.

972 {
973  TIDBitmap *tbm = iterator->tbm;
974  TBMIterateResult *output = &(iterator->output);
975 
977 
978  /*
979  * If lossy chunk pages remain, make sure we've advanced schunkptr/
980  * schunkbit to the next set bit.
981  */
982  while (iterator->schunkptr < tbm->nchunks)
983  {
984  PagetableEntry *chunk = tbm->schunks[iterator->schunkptr];
985  int schunkbit = iterator->schunkbit;
986 
987  tbm_advance_schunkbit(chunk, &schunkbit);
988  if (schunkbit < PAGES_PER_CHUNK)
989  {
990  iterator->schunkbit = schunkbit;
991  break;
992  }
993  /* advance to next chunk */
994  iterator->schunkptr++;
995  iterator->schunkbit = 0;
996  }
997 
998  /*
999  * If both chunk and per-page data remain, must output the numerically
1000  * earlier page.
1001  */
1002  if (iterator->schunkptr < tbm->nchunks)
1003  {
1004  PagetableEntry *chunk = tbm->schunks[iterator->schunkptr];
1005  BlockNumber chunk_blockno;
1006 
1007  chunk_blockno = chunk->blockno + iterator->schunkbit;
1008  if (iterator->spageptr >= tbm->npages ||
1009  chunk_blockno < tbm->spages[iterator->spageptr]->blockno)
1010  {
1011  /* Return a lossy page indicator from the chunk */
1012  output->blockno = chunk_blockno;
1013  output->ntuples = -1;
1014  output->recheck = true;
1015  iterator->schunkbit++;
1016  return output;
1017  }
1018  }
1019 
1020  if (iterator->spageptr < tbm->npages)
1021  {
1022  PagetableEntry *page;
1023  int ntuples;
1024 
1025  /* In TBM_ONE_PAGE state, we don't allocate an spages[] array */
1026  if (tbm->status == TBM_ONE_PAGE)
1027  page = &tbm->entry1;
1028  else
1029  page = tbm->spages[iterator->spageptr];
1030 
1031  /* scan bitmap to extract individual offset numbers */
1032  ntuples = tbm_extract_page_tuple(page, output);
1033  output->blockno = page->blockno;
1034  output->ntuples = ntuples;
1035  output->recheck = page->recheck;
1036  iterator->spageptr++;
1037  return output;
1038  }
1039 
1040  /* Nothing more in the bitmap */
1041  return NULL;
1042 }
TBMIterateResult output
Definition: tidbitmap.c:184
static void tbm_advance_schunkbit(PagetableEntry *chunk, int *schunkbitp)
Definition: tidbitmap.c:941
static int tbm_extract_page_tuple(PagetableEntry *page, TBMIterateResult *output)
Definition: tidbitmap.c:911

References Assert(), PagetableEntry::blockno, TIDBitmap::entry1, TIDBitmap::iterating, TIDBitmap::nchunks, TIDBitmap::npages, TBMIterator::output, output, PAGES_PER_CHUNK, PagetableEntry::recheck, TBMIterator::schunkbit, TBMIterator::schunkptr, TIDBitmap::schunks, TBMIterator::spageptr, TIDBitmap::spages, TIDBitmap::status, TBMIterator::tbm, tbm_advance_schunkbit(), tbm_extract_page_tuple(), TBM_ITERATING_PRIVATE, and TBM_ONE_PAGE.

Referenced by BitmapAdjustPrefetchIterator(), BitmapHeapNext(), BitmapPrefetch(), and entryGetItem().

◆ tbm_lossify()

static void tbm_lossify ( TIDBitmap tbm)
static

Definition at line 1355 of file tidbitmap.c.

1356 {
1357  pagetable_iterator i;
1358  PagetableEntry *page;
1359 
1360  /*
1361  * XXX Really stupid implementation: this just lossifies pages in
1362  * essentially random order. We should be paying some attention to the
1363  * number of bits set in each page, instead.
1364  *
1365  * Since we are called as soon as nentries exceeds maxentries, we should
1366  * push nentries down to significantly less than maxentries, or else we'll
1367  * just end up doing this again very soon. We shoot for maxentries/2.
1368  */
1370  Assert(tbm->status == TBM_HASH);
1371 
1372  pagetable_start_iterate_at(tbm->pagetable, &i, tbm->lossify_start);
1373  while ((page = pagetable_iterate(tbm->pagetable, &i)) != NULL)
1374  {
1375  if (page->ischunk)
1376  continue; /* already a chunk header */
1377 
1378  /*
1379  * If the page would become a chunk header, we won't save anything by
1380  * converting it to lossy, so skip it.
1381  */
1382  if ((page->blockno % PAGES_PER_CHUNK) == 0)
1383  continue;
1384 
1385  /* This does the dirty work ... */
1386  tbm_mark_page_lossy(tbm, page->blockno);
1387 
1388  if (tbm->nentries <= tbm->maxentries / 2)
1389  {
1390  /*
1391  * We have made enough room. Remember where to start lossifying
1392  * next round, so we evenly iterate over the hashtable.
1393  */
1394  tbm->lossify_start = i.cur;
1395  break;
1396  }
1397 
1398  /*
1399  * Note: tbm_mark_page_lossy may have inserted a lossy chunk into the
1400  * hashtable and may have deleted the non-lossy chunk. We can
1401  * continue the same hash table scan, since failure to visit one
1402  * element or visiting the newly inserted element, isn't fatal.
1403  */
1404  }
1405 
1406  /*
1407  * With a big bitmap and small work_mem, it's possible that we cannot get
1408  * under maxentries. Again, if that happens, we'd end up uselessly
1409  * calling tbm_lossify over and over. To prevent this from becoming a
1410  * performance sink, force maxentries up to at least double the current
1411  * number of entries. (In essence, we're admitting inability to fit
1412  * within work_mem when we do this.) Note that this test will not fire if
1413  * we broke out of the loop early; and if we didn't, the current number of
1414  * entries is simply not reducible any further.
1415  */
1416  if (tbm->nentries > tbm->maxentries / 2)
1417  tbm->maxentries = Min(tbm->nentries, (INT_MAX - 1) / 2) * 2;
1418 }

References Assert(), PagetableEntry::blockno, i, PagetableEntry::ischunk, TIDBitmap::iterating, TIDBitmap::lossify_start, TIDBitmap::maxentries, Min, TIDBitmap::nentries, PAGES_PER_CHUNK, TIDBitmap::pagetable, TIDBitmap::status, TBM_HASH, tbm_mark_page_lossy(), and TBM_NOT_ITERATING.

Referenced by tbm_add_page(), tbm_add_tuples(), and tbm_union_page().

◆ tbm_mark_page_lossy()

static void tbm_mark_page_lossy ( TIDBitmap tbm,
BlockNumber  pageno 
)
static

Definition at line 1283 of file tidbitmap.c.

1284 {
1285  PagetableEntry *page;
1286  bool found;
1287  BlockNumber chunk_pageno;
1288  int bitno;
1289  int wordnum;
1290  int bitnum;
1291 
1292  /* We force the bitmap into hashtable mode whenever it's lossy */
1293  if (tbm->status != TBM_HASH)
1294  tbm_create_pagetable(tbm);
1295 
1296  bitno = pageno % PAGES_PER_CHUNK;
1297  chunk_pageno = pageno - bitno;
1298 
1299  /*
1300  * Remove any extant non-lossy entry for the page. If the page is its own
1301  * chunk header, however, we skip this and handle the case below.
1302  */
1303  if (bitno != 0)
1304  {
1305  if (pagetable_delete(tbm->pagetable, pageno))
1306  {
1307  /* It was present, so adjust counts */
1308  tbm->nentries--;
1309  tbm->npages--; /* assume it must have been non-lossy */
1310  }
1311  }
1312 
1313  /* Look up or create entry for chunk-header page */
1314  page = pagetable_insert(tbm->pagetable, chunk_pageno, &found);
1315 
1316  /* Initialize it if not present before */
1317  if (!found)
1318  {
1319  char oldstatus = page->status;
1320 
1321  MemSet(page, 0, sizeof(PagetableEntry));
1322  page->status = oldstatus;
1323  page->blockno = chunk_pageno;
1324  page->ischunk = true;
1325  /* must count it too */
1326  tbm->nentries++;
1327  tbm->nchunks++;
1328  }
1329  else if (!page->ischunk)
1330  {
1331  char oldstatus = page->status;
1332 
1333  /* chunk header page was formerly non-lossy, make it lossy */
1334  MemSet(page, 0, sizeof(PagetableEntry));
1335  page->status = oldstatus;
1336  page->blockno = chunk_pageno;
1337  page->ischunk = true;
1338  /* we assume it had some tuple bit(s) set, so mark it lossy */
1339  page->words[0] = ((bitmapword) 1 << 0);
1340  /* adjust counts */
1341  tbm->nchunks++;
1342  tbm->npages--;
1343  }
1344 
1345  /* Now set the original target page's bit */
1346  wordnum = WORDNUM(bitno);
1347  bitnum = BITNUM(bitno);
1348  page->words[wordnum] |= ((bitmapword) 1 << bitnum);
1349 }

References BITNUM, PagetableEntry::blockno, PagetableEntry::ischunk, MemSet, TIDBitmap::nchunks, TIDBitmap::nentries, TIDBitmap::npages, PAGES_PER_CHUNK, TIDBitmap::pagetable, PagetableEntry::status, TIDBitmap::status, tbm_create_pagetable(), TBM_HASH, WORDNUM, and PagetableEntry::words.

Referenced by tbm_add_page(), tbm_lossify(), and tbm_union_page().

◆ tbm_page_is_lossy()

static bool tbm_page_is_lossy ( const TIDBitmap tbm,
BlockNumber  pageno 
)
static

Definition at line 1249 of file tidbitmap.c.

1250 {
1251  PagetableEntry *page;
1252  BlockNumber chunk_pageno;
1253  int bitno;
1254 
1255  /* we can skip the lookup if there are no lossy chunks */
1256  if (tbm->nchunks == 0)
1257  return false;
1258  Assert(tbm->status == TBM_HASH);
1259 
1260  bitno = pageno % PAGES_PER_CHUNK;
1261  chunk_pageno = pageno - bitno;
1262 
1263  page = pagetable_lookup(tbm->pagetable, chunk_pageno);
1264 
1265  if (page != NULL && page->ischunk)
1266  {
1267  int wordnum = WORDNUM(bitno);
1268  int bitnum = BITNUM(bitno);
1269 
1270  if ((page->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
1271  return true;
1272  }
1273  return false;
1274 }

References Assert(), BITNUM, PagetableEntry::ischunk, TIDBitmap::nchunks, PAGES_PER_CHUNK, TIDBitmap::pagetable, TIDBitmap::status, TBM_HASH, WORDNUM, and PagetableEntry::words.

Referenced by tbm_add_tuples(), tbm_intersect_page(), and tbm_union_page().

◆ tbm_prepare_shared_iterate()

dsa_pointer tbm_prepare_shared_iterate ( TIDBitmap tbm)

Definition at line 766 of file tidbitmap.c.

767 {
768  dsa_pointer dp;
769  TBMSharedIteratorState *istate;
770  PTEntryArray *ptbase = NULL;
771  PTIterationArray *ptpages = NULL;
772  PTIterationArray *ptchunks = NULL;
773 
774  Assert(tbm->dsa != NULL);
776 
777  /*
778  * Allocate TBMSharedIteratorState from DSA to hold the shared members and
779  * lock, this will also be used by multiple worker for shared iterate.
780  */
781  dp = dsa_allocate0(tbm->dsa, sizeof(TBMSharedIteratorState));
782  istate = dsa_get_address(tbm->dsa, dp);
783 
784  /*
785  * If we're not already iterating, create and fill the sorted page lists.
786  * (If we are, the sorted page lists are already stored in the TIDBitmap,
787  * and we can just reuse them.)
788  */
789  if (tbm->iterating == TBM_NOT_ITERATING)
790  {
791  pagetable_iterator i;
792  PagetableEntry *page;
793  int idx;
794  int npages;
795  int nchunks;
796 
797  /*
798  * Allocate the page and chunk array memory from the DSA to share
799  * across multiple processes.
800  */
801  if (tbm->npages)
802  {
803  tbm->ptpages = dsa_allocate(tbm->dsa, sizeof(PTIterationArray) +
804  tbm->npages * sizeof(int));
805  ptpages = dsa_get_address(tbm->dsa, tbm->ptpages);
806  pg_atomic_init_u32(&ptpages->refcount, 0);
807  }
808  if (tbm->nchunks)
809  {
810  tbm->ptchunks = dsa_allocate(tbm->dsa, sizeof(PTIterationArray) +
811  tbm->nchunks * sizeof(int));
812  ptchunks = dsa_get_address(tbm->dsa, tbm->ptchunks);
813  pg_atomic_init_u32(&ptchunks->refcount, 0);
814  }
815 
816  /*
817  * If TBM status is TBM_HASH then iterate over the pagetable and
818  * convert it to page and chunk arrays. But if it's in the
819  * TBM_ONE_PAGE mode then directly allocate the space for one entry
820  * from the DSA.
821  */
822  npages = nchunks = 0;
823  if (tbm->status == TBM_HASH)
824  {
825  ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
826 
827  pagetable_start_iterate(tbm->pagetable, &i);
828  while ((page = pagetable_iterate(tbm->pagetable, &i)) != NULL)
829  {
830  idx = page - ptbase->ptentry;
831  if (page->ischunk)
832  ptchunks->index[nchunks++] = idx;
833  else
834  ptpages->index[npages++] = idx;
835  }
836 
837  Assert(npages == tbm->npages);
838  Assert(nchunks == tbm->nchunks);
839  }
840  else if (tbm->status == TBM_ONE_PAGE)
841  {
842  /*
843  * In one page mode allocate the space for one pagetable entry,
844  * initialize it, and directly store its index (i.e. 0) in the
845  * page array.
846  */
847  tbm->dsapagetable = dsa_allocate(tbm->dsa, sizeof(PTEntryArray) +
848  sizeof(PagetableEntry));
849  ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
850  memcpy(ptbase->ptentry, &tbm->entry1, sizeof(PagetableEntry));
851  ptpages->index[0] = 0;
852  }
853 
854  if (ptbase != NULL)
855  pg_atomic_init_u32(&ptbase->refcount, 0);
856  if (npages > 1)
857  qsort_arg(ptpages->index, npages, sizeof(int),
858  tbm_shared_comparator, ptbase->ptentry);
859  if (nchunks > 1)
860  qsort_arg(ptchunks->index, nchunks, sizeof(int),
861  tbm_shared_comparator, ptbase->ptentry);
862  }
863 
864  /*
865  * Store the TBM members in the shared state so that we can share them
866  * across multiple processes.
867  */
868  istate->nentries = tbm->nentries;
869  istate->maxentries = tbm->maxentries;
870  istate->npages = tbm->npages;
871  istate->nchunks = tbm->nchunks;
872  istate->pagetable = tbm->dsapagetable;
873  istate->spages = tbm->ptpages;
874  istate->schunks = tbm->ptchunks;
875 
876  ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
877  ptpages = dsa_get_address(tbm->dsa, tbm->ptpages);
878  ptchunks = dsa_get_address(tbm->dsa, tbm->ptchunks);
879 
880  /*
881  * For every shared iterator, referring to pagetable and iterator array,
882  * increase the refcount by 1 so that while freeing the shared iterator we
883  * don't free pagetable and iterator array until its refcount becomes 0.
884  */
885  if (ptbase != NULL)
886  pg_atomic_add_fetch_u32(&ptbase->refcount, 1);
887  if (ptpages != NULL)
888  pg_atomic_add_fetch_u32(&ptpages->refcount, 1);
889  if (ptchunks != NULL)
890  pg_atomic_add_fetch_u32(&ptchunks->refcount, 1);
891 
892  /* Initialize the iterator lock */
894 
895  /* Initialize the shared iterator state */
896  istate->schunkbit = 0;
897  istate->schunkptr = 0;
898  istate->spageptr = 0;
899 
901 
902  return dp;
903 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:216
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:419
#define dsa_allocate0(area, size)
Definition: dsa.h:88
uint64 dsa_pointer
Definition: dsa.h:62
#define dsa_allocate(area, size)
Definition: dsa.h:84
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
@ LWTRANCHE_SHARED_TIDBITMAP
Definition: lwlock.h:202
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
PagetableEntry ptentry[FLEXIBLE_ARRAY_MEMBER]
Definition: tidbitmap.c:115
int index[FLEXIBLE_ARRAY_MEMBER]
Definition: tidbitmap.c:212
static int tbm_shared_comparator(const void *left, const void *right, void *arg)
Definition: tidbitmap.c:1438

References Assert(), TIDBitmap::dsa, dsa_allocate, dsa_allocate0, dsa_get_address(), TIDBitmap::dsapagetable, TIDBitmap::entry1, i, idx(), PTIterationArray::index, PagetableEntry::ischunk, TIDBitmap::iterating, TBMSharedIteratorState::lock, LWLockInitialize(), LWTRANCHE_SHARED_TIDBITMAP, TIDBitmap::maxentries, TBMSharedIteratorState::maxentries, TIDBitmap::nchunks, TBMSharedIteratorState::nchunks, TIDBitmap::nentries, TBMSharedIteratorState::nentries, TIDBitmap::npages, TBMSharedIteratorState::npages, TIDBitmap::pagetable, TBMSharedIteratorState::pagetable, pg_atomic_add_fetch_u32(), pg_atomic_init_u32(), TIDBitmap::ptchunks, PTEntryArray::ptentry, TIDBitmap::ptpages, qsort_arg(), PTEntryArray::refcount, PTIterationArray::refcount, TBMSharedIteratorState::schunkbit, TBMSharedIteratorState::schunkptr, TBMSharedIteratorState::schunks, TBMSharedIteratorState::spageptr, TBMSharedIteratorState::spages, TIDBitmap::status, TBM_HASH, TBM_ITERATING_PRIVATE, TBM_ITERATING_SHARED, TBM_NOT_ITERATING, TBM_ONE_PAGE, and tbm_shared_comparator().

Referenced by BitmapHeapNext().

◆ tbm_shared_comparator()

static int tbm_shared_comparator ( const void *  left,
const void *  right,
void *  arg 
)
static

Definition at line 1438 of file tidbitmap.c.

1439 {
1440  PagetableEntry *base = (PagetableEntry *) arg;
1441  PagetableEntry *lpage = &base[*(int *) left];
1442  PagetableEntry *rpage = &base[*(int *) right];
1443 
1444  if (lpage->blockno < rpage->blockno)
1445  return -1;
1446  else if (lpage->blockno > rpage->blockno)
1447  return 1;
1448  return 0;
1449 }
void * arg

References arg, and PagetableEntry::blockno.

Referenced by tbm_prepare_shared_iterate().

◆ tbm_shared_iterate()

TBMIterateResult* tbm_shared_iterate ( TBMSharedIterator iterator)

Definition at line 1052 of file tidbitmap.c.

1053 {
1054  TBMIterateResult *output = &iterator->output;
1055  TBMSharedIteratorState *istate = iterator->state;
1056  PagetableEntry *ptbase = NULL;
1057  int *idxpages = NULL;
1058  int *idxchunks = NULL;
1059 
1060  if (iterator->ptbase != NULL)
1061  ptbase = iterator->ptbase->ptentry;
1062  if (iterator->ptpages != NULL)
1063  idxpages = iterator->ptpages->index;
1064  if (iterator->ptchunks != NULL)
1065  idxchunks = iterator->ptchunks->index;
1066 
1067  /* Acquire the LWLock before accessing the shared members */
1068  LWLockAcquire(&istate->lock, LW_EXCLUSIVE);
1069 
1070  /*
1071  * If lossy chunk pages remain, make sure we've advanced schunkptr/
1072  * schunkbit to the next set bit.
1073  */
1074  while (istate->schunkptr < istate->nchunks)
1075  {
1076  PagetableEntry *chunk = &ptbase[idxchunks[istate->schunkptr]];
1077  int schunkbit = istate->schunkbit;
1078 
1079  tbm_advance_schunkbit(chunk, &schunkbit);
1080  if (schunkbit < PAGES_PER_CHUNK)
1081  {
1082  istate->schunkbit = schunkbit;
1083  break;
1084  }
1085  /* advance to next chunk */
1086  istate->schunkptr++;
1087  istate->schunkbit = 0;
1088  }
1089 
1090  /*
1091  * If both chunk and per-page data remain, must output the numerically
1092  * earlier page.
1093  */
1094  if (istate->schunkptr < istate->nchunks)
1095  {
1096  PagetableEntry *chunk = &ptbase[idxchunks[istate->schunkptr]];
1097  BlockNumber chunk_blockno;
1098 
1099  chunk_blockno = chunk->blockno + istate->schunkbit;
1100 
1101  if (istate->spageptr >= istate->npages ||
1102  chunk_blockno < ptbase[idxpages[istate->spageptr]].blockno)
1103  {
1104  /* Return a lossy page indicator from the chunk */
1105  output->blockno = chunk_blockno;
1106  output->ntuples = -1;
1107  output->recheck = true;
1108  istate->schunkbit++;
1109 
1110  LWLockRelease(&istate->lock);
1111  return output;
1112  }
1113  }
1114 
1115  if (istate->spageptr < istate->npages)
1116  {
1117  PagetableEntry *page = &ptbase[idxpages[istate->spageptr]];
1118  int ntuples;
1119 
1120  /* scan bitmap to extract individual offset numbers */
1121  ntuples = tbm_extract_page_tuple(page, output);
1122  output->blockno = page->blockno;
1123  output->ntuples = ntuples;
1124  output->recheck = page->recheck;
1125  istate->spageptr++;
1126 
1127  LWLockRelease(&istate->lock);
1128 
1129  return output;
1130  }
1131 
1132  LWLockRelease(&istate->lock);
1133 
1134  /* Nothing more in the bitmap */
1135  return NULL;
1136 }
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1172
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1785
@ LW_EXCLUSIVE
Definition: lwlock.h:116
TBMIterateResult output
Definition: tidbitmap.c:225

References PagetableEntry::blockno, PTIterationArray::index, TBMSharedIteratorState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), TBMSharedIteratorState::nchunks, TBMSharedIteratorState::npages, TBMSharedIterator::output, output, PAGES_PER_CHUNK, TBMSharedIterator::ptbase, TBMSharedIterator::ptchunks, PTEntryArray::ptentry, TBMSharedIterator::ptpages, PagetableEntry::recheck, TBMSharedIteratorState::schunkbit, TBMSharedIteratorState::schunkptr, TBMSharedIteratorState::spageptr, TBMSharedIterator::state, tbm_advance_schunkbit(), and tbm_extract_page_tuple().

Referenced by BitmapAdjustPrefetchIterator(), BitmapHeapNext(), and BitmapPrefetch().

◆ tbm_union()

void tbm_union ( TIDBitmap a,
const TIDBitmap b 
)

Definition at line 458 of file tidbitmap.c.

459 {
460  Assert(!a->iterating);
461  /* Nothing to do if b is empty */
462  if (b->nentries == 0)
463  return;
464  /* Scan through chunks and pages in b, merge into a */
465  if (b->status == TBM_ONE_PAGE)
466  tbm_union_page(a, &b->entry1);
467  else
468  {
469  pagetable_iterator i;
470  PagetableEntry *bpage;
471 
472  Assert(b->status == TBM_HASH);
473  pagetable_start_iterate(b->pagetable, &i);
474  while ((bpage = pagetable_iterate(b->pagetable, &i)) != NULL)
475  tbm_union_page(a, bpage);
476  }
477 }
static void tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
Definition: tidbitmap.c:481

References a, Assert(), b, i, TBM_HASH, TBM_ONE_PAGE, and tbm_union_page().

Referenced by MultiExecBitmapOr().

◆ tbm_union_page()

static void tbm_union_page ( TIDBitmap a,
const PagetableEntry bpage 
)
static

Definition at line 481 of file tidbitmap.c.

482 {
483  PagetableEntry *apage;
484  int wordnum;
485 
486  if (bpage->ischunk)
487  {
488  /* Scan b's chunk, mark each indicated page lossy in a */
489  for (wordnum = 0; wordnum < WORDS_PER_CHUNK; wordnum++)
490  {
491  bitmapword w = bpage->words[wordnum];
492 
493  if (w != 0)
494  {
495  BlockNumber pg;
496 
497  pg = bpage->blockno + (wordnum * BITS_PER_BITMAPWORD);
498  while (w != 0)
499  {
500  if (w & 1)
501  tbm_mark_page_lossy(a, pg);
502  pg++;
503  w >>= 1;
504  }
505  }
506  }
507  }
508  else if (tbm_page_is_lossy(a, bpage->blockno))
509  {
510  /* page is already lossy in a, nothing to do */
511  return;
512  }
513  else
514  {
515  apage = tbm_get_pageentry(a, bpage->blockno);
516  if (apage->ischunk)
517  {
518  /* The page is a lossy chunk header, set bit for itself */
519  apage->words[0] |= ((bitmapword) 1 << 0);
520  }
521  else
522  {
523  /* Both pages are exact, merge at the bit level */
524  for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
525  apage->words[wordnum] |= bpage->words[wordnum];
526  apage->recheck |= bpage->recheck;
527  }
528  }
529 
530  if (a->nentries > a->maxentries)
531  tbm_lossify(a);
532 }

References a, BITS_PER_BITMAPWORD, PagetableEntry::blockno, PagetableEntry::ischunk, PagetableEntry::recheck, tbm_get_pageentry(), tbm_lossify(), tbm_mark_page_lossy(), tbm_page_is_lossy(), PagetableEntry::words, WORDS_PER_CHUNK, and WORDS_PER_PAGE.

Referenced by tbm_union().