PostgreSQL Source Code  git master
tidbitmap.h File Reference
#include "storage/itemptr.h"
#include "utils/dsa.h"
Include dependency graph for tidbitmap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TBMIterateResult
 

Typedefs

typedef struct TIDBitmap TIDBitmap
 
typedef struct TBMIterator TBMIterator
 
typedef struct TBMSharedIterator TBMSharedIterator
 

Functions

TIDBitmaptbm_create (long maxbytes, dsa_area *dsa)
 
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)
 
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)
 
long tbm_calculate_entries (double maxbytes)
 

Typedef Documentation

◆ TBMIterator

Definition at line 36 of file tidbitmap.h.

◆ TBMSharedIterator

Definition at line 37 of file tidbitmap.h.

◆ TIDBitmap

Definition at line 33 of file tidbitmap.h.

Function Documentation

◆ tbm_add_page()

void tbm_add_page ( TIDBitmap tbm,
BlockNumber  pageno 
)

Definition at line 442 of file tidbitmap.c.

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

Referenced by bringetbitmap(), and gingetbitmap().

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

◆ tbm_add_tuples()

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

Definition at line 376 of file tidbitmap.c.

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().

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

◆ tbm_attach_shared_iterate()

TBMSharedIterator* tbm_attach_shared_iterate ( dsa_area dsa,
dsa_pointer  dp 
)

Definition at line 1464 of file tidbitmap.c.

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().

1465 {
1466  TBMSharedIterator *iterator;
1467  TBMSharedIteratorState *istate;
1468 
1469  /*
1470  * Create the TBMSharedIterator struct, with enough trailing space to
1471  * serve the needs of the TBMIterateResult sub-struct.
1472  */
1473  iterator = (TBMSharedIterator *) palloc0(sizeof(TBMSharedIterator) +
1474  MAX_TUPLES_PER_PAGE * sizeof(OffsetNumber));
1475 
1476  istate = (TBMSharedIteratorState *) dsa_get_address(dsa, dp);
1477 
1478  iterator->state = istate;
1479 
1480  iterator->ptbase = dsa_get_address(dsa, istate->pagetable);
1481 
1482  if (istate->npages)
1483  iterator->ptpages = dsa_get_address(dsa, istate->spages);
1484  if (istate->nchunks)
1485  iterator->ptchunks = dsa_get_address(dsa, istate->schunks);
1486 
1487  return iterator;
1488 }
dsa_pointer spages
Definition: tidbitmap.c:197
TBMSharedIteratorState * state
Definition: tidbitmap.c:220
dsa_pointer schunks
Definition: tidbitmap.c:198
PTEntryArray * ptbase
Definition: tidbitmap.c:221
uint16 OffsetNumber
Definition: off.h:24
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
dsa_pointer pagetable
Definition: tidbitmap.c:196
void * palloc0(Size size)
Definition: mcxt.c:864
PTIterationArray * ptpages
Definition: tidbitmap.c:222
#define MAX_TUPLES_PER_PAGE
Definition: tidbitmap.c:56
PTIterationArray * ptchunks
Definition: tidbitmap.c:223

◆ tbm_begin_iterate()

TBMIterator* tbm_begin_iterate ( TIDBitmap tbm)

Definition at line 688 of file tidbitmap.c.

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().

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

◆ tbm_calculate_entries()

long tbm_calculate_entries ( double  maxbytes)

Definition at line 1545 of file tidbitmap.c.

References Max, and Min.

Referenced by compute_bitmap_pages(), and tbm_create().

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

◆ tbm_create()

TIDBitmap* tbm_create ( long  maxbytes,
dsa_area dsa 
)

Definition at line 265 of file tidbitmap.c.

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().

266 {
267  TIDBitmap *tbm;
268 
269  /* Create the TIDBitmap struct and zero all its fields */
270  tbm = makeNode(TIDBitmap);
271 
272  tbm->mcxt = CurrentMemoryContext;
273  tbm->status = TBM_EMPTY;
274 
275  tbm->maxentries = (int) tbm_calculate_entries(maxbytes);
276  tbm->lossify_start = 0;
277  tbm->dsa = dsa;
280  tbm->ptpages = InvalidDsaPointer;
282 
283  return tbm;
284 }
#define InvalidDsaPointer
Definition: dsa.h:78
uint32 lossify_start
Definition: tidbitmap.c:159
dsa_pointer dsapagetable
Definition: tidbitmap.c:164
MemoryContext mcxt
Definition: tidbitmap.c:151
dsa_pointer ptpages
Definition: tidbitmap.c:166
dsa_pointer ptchunks
Definition: tidbitmap.c:167
int maxentries
Definition: tidbitmap.c:155
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define makeNode(_type_)
Definition: nodes.h:560
TBMStatus status
Definition: tidbitmap.c:152
dsa_pointer dsapagetableold
Definition: tidbitmap.c:165
long tbm_calculate_entries(double maxbytes)
Definition: tidbitmap.c:1545
dsa_area * dsa
Definition: tidbitmap.c:168

◆ tbm_end_iterate()

void tbm_end_iterate ( TBMIterator iterator)

Definition at line 1145 of file tidbitmap.c.

References pfree().

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

1146 {
1147  pfree(iterator);
1148 }
void pfree(void *pointer)
Definition: mcxt.c:936

◆ tbm_end_shared_iterate()

void tbm_end_shared_iterate ( TBMSharedIterator iterator)

Definition at line 1157 of file tidbitmap.c.

References pfree().

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

1158 {
1159  pfree(iterator);
1160 }
void pfree(void *pointer)
Definition: mcxt.c:936

◆ tbm_free()

void tbm_free ( TIDBitmap tbm)

Definition at line 321 of file tidbitmap.c.

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

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

322 {
323  if (tbm->pagetable)
324  pagetable_destroy(tbm->pagetable);
325  if (tbm->spages)
326  pfree(tbm->spages);
327  if (tbm->schunks)
328  pfree(tbm->schunks);
329  pfree(tbm);
330 }
struct pagetable_hash * pagetable
Definition: tidbitmap.c:153
void pfree(void *pointer)
Definition: mcxt.c:936
PagetableEntry ** spages
Definition: tidbitmap.c:162
PagetableEntry ** schunks
Definition: tidbitmap.c:163

◆ tbm_free_shared_area()

void tbm_free_shared_area ( dsa_area dsa,
dsa_pointer  dp 
)

Definition at line 340 of file tidbitmap.c.

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().

341 {
342  TBMSharedIteratorState *istate = dsa_get_address(dsa, dp);
343  PTEntryArray *ptbase;
344  PTIterationArray *ptpages;
345  PTIterationArray *ptchunks;
346 
347  if (DsaPointerIsValid(istate->pagetable))
348  {
349  ptbase = dsa_get_address(dsa, istate->pagetable);
350  if (pg_atomic_sub_fetch_u32(&ptbase->refcount, 1) == 0)
351  dsa_free(dsa, istate->pagetable);
352  }
353  if (DsaPointerIsValid(istate->spages))
354  {
355  ptpages = dsa_get_address(dsa, istate->spages);
356  if (pg_atomic_sub_fetch_u32(&ptpages->refcount, 1) == 0)
357  dsa_free(dsa, istate->spages);
358  }
359  if (DsaPointerIsValid(istate->schunks))
360  {
361  ptchunks = dsa_get_address(dsa, istate->schunks);
362  if (pg_atomic_sub_fetch_u32(&ptchunks->refcount, 1) == 0)
363  dsa_free(dsa, istate->schunks);
364  }
365 
366  dsa_free(dsa, dp);
367 }
dsa_pointer spages
Definition: tidbitmap.c:197
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:412
dsa_pointer schunks
Definition: tidbitmap.c:198
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
dsa_pointer pagetable
Definition: tidbitmap.c:196
pg_atomic_uint32 refcount
Definition: tidbitmap.c:113
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:812
pg_atomic_uint32 refcount
Definition: tidbitmap.c:210

◆ tbm_intersect()

void tbm_intersect ( TIDBitmap a,
const TIDBitmap b 
)

Definition at line 539 of file tidbitmap.c.

References Assert, PagetableEntry::blockno, elog, TIDBitmap::entry1, ERROR, i, PagetableEntry::ischunk, TIDBitmap::iterating, TIDBitmap::nchunks, TIDBitmap::nentries, TIDBitmap::npages, TIDBitmap::pagetable, TIDBitmap::status, TBM_EMPTY, TBM_HASH, tbm_intersect_page(), and TBM_ONE_PAGE.

Referenced by MultiExecBitmapAnd().

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

◆ tbm_is_empty()

bool tbm_is_empty ( const TIDBitmap tbm)

Definition at line 669 of file tidbitmap.c.

References TIDBitmap::nentries.

Referenced by MultiExecBitmapAnd(), and startScanEntry().

670 {
671  return (tbm->nentries == 0);
672 }
int nentries
Definition: tidbitmap.c:154

◆ tbm_iterate()

TBMIterateResult* tbm_iterate ( TBMIterator iterator)

Definition at line 970 of file tidbitmap.c.

References Assert, TBMIterateResult::blockno, PagetableEntry::blockno, TIDBitmap::entry1, TIDBitmap::iterating, TIDBitmap::nchunks, TIDBitmap::npages, TBMIterateResult::ntuples, output(), TBMIterator::output, PAGES_PER_CHUNK, TBMIterateResult::recheck, 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().

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

◆ tbm_prepare_shared_iterate()

dsa_pointer tbm_prepare_shared_iterate ( TIDBitmap tbm)

Definition at line 765 of file tidbitmap.c.

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_TBM, 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().

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

◆ tbm_shared_iterate()

TBMIterateResult* tbm_shared_iterate ( TBMSharedIterator iterator)

Definition at line 1051 of file tidbitmap.c.

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

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

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

◆ tbm_union()

void tbm_union ( TIDBitmap a,
const TIDBitmap b 
)

Definition at line 457 of file tidbitmap.c.

References Assert, TIDBitmap::entry1, i, TIDBitmap::iterating, TIDBitmap::nentries, TIDBitmap::pagetable, TIDBitmap::status, TBM_HASH, TBM_ONE_PAGE, and tbm_union_page().

Referenced by MultiExecBitmapOr().

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