PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tidbitmap.c File Reference
#include "postgres.h"
#include <limits.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  TBMPrivateIterator
 
struct  TBMSharedIteratorState
 
struct  PTIterationArray
 
struct  TBMSharedIterator
 

Macros

#define PAGES_PER_CHUNK   (BLCKSZ / 32)
 
#define WORDNUM(x)   ((x) / BITS_PER_BITMAPWORD)
 
#define BITNUM(x)   ((x) % BITS_PER_BITMAPWORD)
 
#define WORDS_PER_PAGE   ((TBM_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 (Size 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)
 
TBMPrivateIteratortbm_begin_private_iterate (TIDBitmap *tbm)
 
dsa_pointer tbm_prepare_shared_iterate (TIDBitmap *tbm)
 
int tbm_extract_page_tuple (TBMIterateResult *iteritem, OffsetNumber *offsets, uint32 max_offsets)
 
static void tbm_advance_schunkbit (PagetableEntry *chunk, int *schunkbitp)
 
bool tbm_private_iterate (TBMPrivateIterator *iterator, TBMIterateResult *tbmres)
 
bool tbm_shared_iterate (TBMSharedIterator *iterator, TBMIterateResult *tbmres)
 
void tbm_end_private_iterate (TBMPrivateIterator *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)
 
int tbm_calculate_entries (Size maxbytes)
 
TBMIterator tbm_begin_iterate (TIDBitmap *tbm, dsa_area *dsa, dsa_pointer dsp)
 
void tbm_end_iterate (TBMIterator *iterator)
 
bool tbm_iterate (TBMIterator *iterator, TBMIterateResult *tbmres)
 

Macro Definition Documentation

◆ BITNUM

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

Definition at line 70 of file tidbitmap.c.

◆ PAGES_PER_CHUNK

#define PAGES_PER_CHUNK   (BLCKSZ / 32)

Definition at line 65 of file tidbitmap.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 241 of file tidbitmap.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 240 of file tidbitmap.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PagetableEntry

Definition at line 234 of file tidbitmap.c.

◆ SH_EQUAL

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

Definition at line 238 of file tidbitmap.c.

◆ SH_HASH_KEY

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

Definition at line 237 of file tidbitmap.c.

◆ SH_KEY

#define SH_KEY   blockno

Definition at line 236 of file tidbitmap.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   BlockNumber

Definition at line 235 of file tidbitmap.c.

◆ SH_PREFIX

#define SH_PREFIX   pagetable

Definition at line 233 of file tidbitmap.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 239 of file tidbitmap.c.

◆ SH_USE_NONDEFAULT_ALLOCATOR

#define SH_USE_NONDEFAULT_ALLOCATOR

Definition at line 232 of file tidbitmap.c.

◆ WORDNUM

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

Definition at line 69 of file tidbitmap.c.

◆ WORDS_PER_CHUNK

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

Definition at line 75 of file tidbitmap.c.

◆ WORDS_PER_PAGE

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

Definition at line 73 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 130 of file tidbitmap.c.

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 */
TBMIteratingState
Definition: tidbitmap.c:131
@ TBM_ITERATING_SHARED
Definition: tidbitmap.c:134
@ TBM_NOT_ITERATING
Definition: tidbitmap.c:132
@ TBM_ITERATING_PRIVATE
Definition: tidbitmap.c:133

◆ TBMStatus

enum TBMStatus
Enumerator
TBM_EMPTY 
TBM_ONE_PAGE 
TBM_HASH 

Definition at line 120 of file tidbitmap.c.

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;
TBMStatus
Definition: tidbitmap.c:121
@ TBM_EMPTY
Definition: tidbitmap.c:122
@ TBM_ONE_PAGE
Definition: tidbitmap.c:123
@ TBM_HASH
Definition: tidbitmap.c:124

Function Documentation

◆ pagetable_allocate()

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

Definition at line 1497 of file tidbitmap.c.

1498{
1499 TIDBitmap *tbm = (TIDBitmap *) pagetable->private_data;
1500 PTEntryArray *ptbase;
1501
1502 if (tbm->dsa == NULL)
1503 return MemoryContextAllocExtended(pagetable->ctx, size,
1505
1506 /*
1507 * Save the dsapagetable reference in dsapagetableold before allocating
1508 * new memory so that pagetable_free can free the old entry.
1509 */
1510 tbm->dsapagetableold = tbm->dsapagetable;
1512 sizeof(PTEntryArray) + size,
1514 ptbase = dsa_get_address(tbm->dsa, tbm->dsapagetable);
1515
1516 return ptbase->ptentry;
1517}
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
dsa_pointer dsa_allocate_extended(dsa_area *area, size_t size, int flags)
Definition: dsa.c:671
#define DSA_ALLOC_HUGE
Definition: dsa.h:73
#define DSA_ALLOC_ZERO
Definition: dsa.h:75
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:30
#define MCXT_ALLOC_HUGE
Definition: fe_memutils.h:28
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1313
dsa_pointer dsapagetableold
Definition: tidbitmap.c:157
dsa_area * dsa
Definition: tidbitmap.c:160
dsa_pointer dsapagetable
Definition: tidbitmap.c:156

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

◆ pagetable_free()

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

Definition at line 1525 of file tidbitmap.c.

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

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 432 of file tidbitmap.c.

433{
434 /* Enter the page in the bitmap, or mark it lossy if already present */
435 tbm_mark_page_lossy(tbm, pageno);
436 /* If we went over the memory limit, lossify some more pages */
437 if (tbm->nentries > tbm->maxentries)
438 tbm_lossify(tbm);
439}
int nentries
Definition: tidbitmap.c:146
int maxentries
Definition: tidbitmap.c:147
static void tbm_lossify(TIDBitmap *tbm)
Definition: tidbitmap.c:1359
static void tbm_mark_page_lossy(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:1287

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 366 of file tidbitmap.c.

368{
370 PagetableEntry *page = NULL; /* only valid when currblk is valid */
371 int i;
372
374 for (i = 0; i < ntids; i++)
375 {
378 int wordnum,
379 bitnum;
380
381 /* safety check to ensure we don't overrun bit array bounds */
382 if (off < 1 || off > TBM_MAX_TUPLES_PER_PAGE)
383 elog(ERROR, "tuple offset out of range: %u", off);
384
385 /*
386 * Look up target page unless we already did. This saves cycles when
387 * the input includes consecutive tuples on the same page, which is
388 * common enough to justify an extra test here.
389 */
390 if (blk != currblk)
391 {
392 if (tbm_page_is_lossy(tbm, blk))
393 page = NULL; /* remember page is lossy */
394 else
395 page = tbm_get_pageentry(tbm, blk);
396 currblk = blk;
397 }
398
399 if (page == NULL)
400 continue; /* whole page is already marked */
401
402 if (page->ischunk)
403 {
404 /* The page is a lossy chunk header, set bit for itself */
405 wordnum = bitnum = 0;
406 }
407 else
408 {
409 /* Page is exact, so set bit for individual tuple */
410 wordnum = WORDNUM(off - 1);
411 bitnum = BITNUM(off - 1);
412 }
413 page->words[wordnum] |= ((bitmapword) 1 << bitnum);
414 page->recheck |= recheck;
415
416 if (tbm->nentries > tbm->maxentries)
417 {
418 tbm_lossify(tbm);
419 /* Page could have been converted to lossy, so force new lookup */
420 currblk = InvalidBlockNumber;
421 }
422 }
423}
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:226
Assert(PointerIsAligned(start, uint64))
int i
Definition: isn.c:77
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
uint16 OffsetNumber
Definition: off.h:24
bitmapword words[Max(WORDS_PER_PAGE, WORDS_PER_CHUNK)]
Definition: tidbitmap.c:97
TBMIteratingState iterating
Definition: tidbitmap.c:150
#define WORDNUM(x)
Definition: tidbitmap.c:69
static bool tbm_page_is_lossy(const TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:1253
#define BITNUM(x)
Definition: tidbitmap.c:70
static PagetableEntry * tbm_get_pageentry(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:1206
#define TBM_MAX_TUPLES_PER_PAGE
Definition: tidbitmap.h:35

References Assert(), BITNUM, elog, ERROR, i, InvalidBlockNumber, PagetableEntry::ischunk, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), TIDBitmap::iterating, TIDBitmap::maxentries, TIDBitmap::nentries, PagetableEntry::recheck, tbm_get_pageentry(), tbm_lossify(), TBM_MAX_TUPLES_PER_PAGE, 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 938 of file tidbitmap.c.

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

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

Referenced by tbm_private_iterate(), and tbm_shared_iterate().

◆ tbm_attach_shared_iterate()

TBMSharedIterator * tbm_attach_shared_iterate ( dsa_area dsa,
dsa_pointer  dp 
)

Definition at line 1465 of file tidbitmap.c.

1466{
1467 TBMSharedIterator *iterator;
1468 TBMSharedIteratorState *istate;
1469
1470 /*
1471 * Create the TBMSharedIterator struct, with enough trailing space to
1472 * serve the needs of the TBMIterateResult sub-struct.
1473 */
1474 iterator = (TBMSharedIterator *) palloc0(sizeof(TBMSharedIterator));
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}
void * palloc0(Size size)
Definition: mcxt.c:1970
dsa_pointer pagetable
Definition: tidbitmap.c:187
dsa_pointer spages
Definition: tidbitmap.c:188
dsa_pointer schunks
Definition: tidbitmap.c:189
TBMSharedIteratorState * state
Definition: tidbitmap.c:211
PTEntryArray * ptbase
Definition: tidbitmap.c:212
PTIterationArray * ptchunks
Definition: tidbitmap.c:214
PTIterationArray * ptpages
Definition: tidbitmap.c:213

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

Referenced by tbm_begin_iterate().

◆ tbm_begin_iterate()

TBMIterator tbm_begin_iterate ( TIDBitmap tbm,
dsa_area dsa,
dsa_pointer  dsp 
)

Definition at line 1574 of file tidbitmap.c.

1575{
1576 TBMIterator iterator = {0};
1577
1578 /* Allocate a private iterator and attach the shared state to it */
1579 if (DsaPointerIsValid(dsp))
1580 {
1581 iterator.shared = true;
1582 iterator.i.shared_iterator = tbm_attach_shared_iterate(dsa, dsp);
1583 }
1584 else
1585 {
1586 iterator.shared = false;
1588 }
1589
1590 return iterator;
1591}
union TBMIterator::@109 i
TBMSharedIterator * shared_iterator
Definition: tidbitmap.h:57
TBMPrivateIterator * private_iterator
Definition: tidbitmap.h:56
bool shared
Definition: tidbitmap.h:53
TBMSharedIterator * tbm_attach_shared_iterate(dsa_area *dsa, dsa_pointer dp)
Definition: tidbitmap.c:1465
TBMPrivateIterator * tbm_begin_private_iterate(TIDBitmap *tbm)
Definition: tidbitmap.c:678

References DsaPointerIsValid, TBMIterator::i, TBMIterator::private_iterator, TBMIterator::shared, TBMIterator::shared_iterator, tbm_attach_shared_iterate(), and tbm_begin_private_iterate().

Referenced by BitmapTableScanSetup().

◆ tbm_begin_private_iterate()

TBMPrivateIterator * tbm_begin_private_iterate ( TIDBitmap tbm)

Definition at line 678 of file tidbitmap.c.

679{
680 TBMPrivateIterator *iterator;
681
683
684 /*
685 * Create the TBMPrivateIterator struct, with enough trailing space to
686 * serve the needs of the TBMIterateResult sub-struct.
687 */
688 iterator = (TBMPrivateIterator *) palloc(sizeof(TBMPrivateIterator));
689 iterator->tbm = tbm;
690
691 /*
692 * Initialize iteration pointers.
693 */
694 iterator->spageptr = 0;
695 iterator->schunkptr = 0;
696 iterator->schunkbit = 0;
697
698 /*
699 * If we have a hashtable, create and fill the sorted page lists, unless
700 * we already did that for a previous iterator. Note that the lists are
701 * attached to the bitmap not the iterator, so they can be used by more
702 * than one iterator.
703 */
704 if (tbm->status == TBM_HASH && tbm->iterating == TBM_NOT_ITERATING)
705 {
706 pagetable_iterator i;
707 PagetableEntry *page;
708 int npages;
709 int nchunks;
710
711 if (!tbm->spages && tbm->npages > 0)
712 tbm->spages = (PagetableEntry **)
714 tbm->npages * sizeof(PagetableEntry *));
715 if (!tbm->schunks && tbm->nchunks > 0)
716 tbm->schunks = (PagetableEntry **)
718 tbm->nchunks * sizeof(PagetableEntry *));
719
720 npages = nchunks = 0;
721 pagetable_start_iterate(tbm->pagetable, &i);
722 while ((page = pagetable_iterate(tbm->pagetable, &i)) != NULL)
723 {
724 if (page->ischunk)
725 tbm->schunks[nchunks++] = page;
726 else
727 tbm->spages[npages++] = page;
728 }
729 Assert(npages == tbm->npages);
730 Assert(nchunks == tbm->nchunks);
731 if (npages > 1)
732 qsort(tbm->spages, npages, sizeof(PagetableEntry *),
734 if (nchunks > 1)
735 qsort(tbm->schunks, nchunks, sizeof(PagetableEntry *),
737 }
738
740
741 return iterator;
742}
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1256
void * palloc(Size size)
Definition: mcxt.c:1940
#define qsort(a, b, c, d)
Definition: port.h:479
TIDBitmap * tbm
Definition: tidbitmap.c:171
struct pagetable_hash * pagetable
Definition: tidbitmap.c:145
PagetableEntry ** schunks
Definition: tidbitmap.c:155
MemoryContext mcxt
Definition: tidbitmap.c:143
int npages
Definition: tidbitmap.c:148
int nchunks
Definition: tidbitmap.c:149
TBMStatus status
Definition: tidbitmap.c:144
PagetableEntry ** spages
Definition: tidbitmap.c:154
static int tbm_comparator(const void *left, const void *right)
Definition: tidbitmap.c:1428

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

Referenced by startScanEntry(), and tbm_begin_iterate().

◆ tbm_calculate_entries()

int tbm_calculate_entries ( Size  maxbytes)

Definition at line 1545 of file tidbitmap.c.

1546{
1547 Size 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 (int) nbuckets;
1561}
#define Min(x, y)
Definition: c.h:975
#define Max(x, y)
Definition: c.h:969
char * Pointer
Definition: c.h:493
size_t Size
Definition: c.h:576
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 1428 of file tidbitmap.c.

1429{
1430 BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
1431 BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
1432
1433 return pg_cmp_u32(l, r);
1434}
static int pg_cmp_u32(uint32 a, uint32 b)
Definition: int.h:652

References pg_cmp_u32().

Referenced by tbm_begin_private_iterate().

◆ tbm_create()

TIDBitmap * tbm_create ( Size  maxbytes,
dsa_area dsa 
)

Definition at line 255 of file tidbitmap.c.

256{
257 TIDBitmap *tbm;
258
259 /* Create the TIDBitmap struct and zero all its fields */
260 tbm = makeNode(TIDBitmap);
261
263 tbm->status = TBM_EMPTY;
264
265 tbm->maxentries = tbm_calculate_entries(maxbytes);
266 tbm->lossify_start = 0;
267 tbm->dsa = dsa;
272
273 return tbm;
274}
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
#define makeNode(_type_)
Definition: nodes.h:161
dsa_pointer ptpages
Definition: tidbitmap.c:158
uint32 lossify_start
Definition: tidbitmap.c:151
dsa_pointer ptchunks
Definition: tidbitmap.c:159
int tbm_calculate_entries(Size maxbytes)
Definition: tidbitmap.c:1545

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 281 of file tidbitmap.c.

282{
283 Assert(tbm->status != TBM_HASH);
284 Assert(tbm->pagetable == NULL);
285
286 tbm->pagetable = pagetable_create(tbm->mcxt, 128, tbm);
287
288 /* If entry1 is valid, push it into the hashtable */
289 if (tbm->status == TBM_ONE_PAGE)
290 {
291 PagetableEntry *page;
292 bool found;
293 char oldstatus;
294
295 page = pagetable_insert(tbm->pagetable,
296 tbm->entry1.blockno,
297 &found);
298 Assert(!found);
299 oldstatus = page->status;
300 memcpy(page, &tbm->entry1, sizeof(PagetableEntry));
301 page->status = oldstatus;
302 }
303
304 tbm->status = TBM_HASH;
305}
BlockNumber blockno
Definition: tidbitmap.c:93
PagetableEntry entry1
Definition: tidbitmap.c:152

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 1597 of file tidbitmap.c.

1598{
1599 Assert(iterator && !tbm_exhausted(iterator));
1600
1601 if (iterator->shared)
1603 else
1605
1606 *iterator = (TBMIterator)
1607 {
1608 0
1609 };
1610}
void tbm_end_shared_iterate(TBMSharedIterator *iterator)
Definition: tidbitmap.c:1162
void tbm_end_private_iterate(TBMPrivateIterator *iterator)
Definition: tidbitmap.c:1150
struct TBMIterator TBMIterator
static bool tbm_exhausted(TBMIterator *iterator)
Definition: tidbitmap.h:119

References Assert(), TBMIterator::i, TBMIterator::private_iterator, TBMIterator::shared, TBMIterator::shared_iterator, tbm_end_private_iterate(), tbm_end_shared_iterate(), and tbm_exhausted().

Referenced by ExecEndBitmapHeapScan(), and ExecReScanBitmapHeapScan().

◆ tbm_end_private_iterate()

void tbm_end_private_iterate ( TBMPrivateIterator iterator)

Definition at line 1150 of file tidbitmap.c.

1151{
1152 pfree(iterator);
1153}

References pfree().

Referenced by entryGetItem(), ginFreeScanKeys(), startScanEntry(), and tbm_end_iterate().

◆ tbm_end_shared_iterate()

void tbm_end_shared_iterate ( TBMSharedIterator iterator)

Definition at line 1162 of file tidbitmap.c.

1163{
1164 pfree(iterator);
1165}

References pfree().

Referenced by tbm_end_iterate().

◆ tbm_extract_page_tuple()

int tbm_extract_page_tuple ( TBMIterateResult iteritem,
OffsetNumber offsets,
uint32  max_offsets 
)

Definition at line 901 of file tidbitmap.c.

904{
905 PagetableEntry *page = iteritem->internal_page;
906 int wordnum;
907 int ntuples = 0;
908
909 for (wordnum = 0; wordnum < WORDS_PER_PAGE; wordnum++)
910 {
911 bitmapword w = page->words[wordnum];
912
913 if (w != 0)
914 {
915 int off = wordnum * BITS_PER_BITMAPWORD + 1;
916
917 while (w != 0)
918 {
919 if (w & 1)
920 {
921 if (ntuples < max_offsets)
922 offsets[ntuples] = (OffsetNumber) off;
923 ntuples++;
924 }
925 off++;
926 w >>= 1;
927 }
928 }
929 }
930
931 return ntuples;
932}
#define BITS_PER_BITMAPWORD
Definition: bitmapset.h:43
void * internal_page
Definition: tidbitmap.h:79
#define WORDS_PER_PAGE
Definition: tidbitmap.c:73

References BITS_PER_BITMAPWORD, TBMIterateResult::internal_page, PagetableEntry::words, and WORDS_PER_PAGE.

Referenced by BitmapHeapScanNextBlock(), and entryGetItem().

◆ tbm_find_pageentry()

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

Definition at line 1173 of file tidbitmap.c.

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

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 311 of file tidbitmap.c.

312{
313 if (tbm->pagetable)
314 pagetable_destroy(tbm->pagetable);
315 if (tbm->spages)
316 pfree(tbm->spages);
317 if (tbm->schunks)
318 pfree(tbm->schunks);
319 pfree(tbm);
320}

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 330 of file tidbitmap.c.

331{
332 TBMSharedIteratorState *istate = dsa_get_address(dsa, dp);
333 PTEntryArray *ptbase;
334 PTIterationArray *ptpages;
335 PTIterationArray *ptchunks;
336
337 if (DsaPointerIsValid(istate->pagetable))
338 {
339 ptbase = dsa_get_address(dsa, istate->pagetable);
340 if (pg_atomic_sub_fetch_u32(&ptbase->refcount, 1) == 0)
341 dsa_free(dsa, istate->pagetable);
342 }
343 if (DsaPointerIsValid(istate->spages))
344 {
345 ptpages = dsa_get_address(dsa, istate->spages);
346 if (pg_atomic_sub_fetch_u32(&ptpages->refcount, 1) == 0)
347 dsa_free(dsa, istate->spages);
348 }
349 if (DsaPointerIsValid(istate->schunks))
350 {
351 ptchunks = dsa_get_address(dsa, istate->schunks);
352 if (pg_atomic_sub_fetch_u32(&ptchunks->refcount, 1) == 0)
353 dsa_free(dsa, istate->schunks);
354 }
355
356 dsa_free(dsa, dp);
357}
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:439
pg_atomic_uint32 refcount
Definition: tidbitmap.c:105
pg_atomic_uint32 refcount
Definition: tidbitmap.c:201

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 1206 of file tidbitmap.c.

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

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 529 of file tidbitmap.c.

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

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 578 of file tidbitmap.c.

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

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 659 of file tidbitmap.c.

660{
661 return (tbm->nentries == 0);
662}

References TIDBitmap::nentries.

Referenced by MultiExecBitmapAnd(), and startScanEntry().

◆ tbm_iterate()

bool tbm_iterate ( TBMIterator iterator,
TBMIterateResult tbmres 
)

Definition at line 1617 of file tidbitmap.c.

1618{
1619 Assert(iterator);
1620 Assert(tbmres);
1621
1622 if (iterator->shared)
1623 return tbm_shared_iterate(iterator->i.shared_iterator, tbmres);
1624 else
1625 return tbm_private_iterate(iterator->i.private_iterator, tbmres);
1626}
bool tbm_shared_iterate(TBMSharedIterator *iterator, TBMIterateResult *tbmres)
Definition: tidbitmap.c:1057
bool tbm_private_iterate(TBMPrivateIterator *iterator, TBMIterateResult *tbmres)
Definition: tidbitmap.c:977

References Assert(), TBMIterator::i, TBMIterator::private_iterator, TBMIterator::shared, TBMIterator::shared_iterator, tbm_private_iterate(), and tbm_shared_iterate().

Referenced by bitmapheap_stream_read_next().

◆ tbm_lossify()

static void tbm_lossify ( TIDBitmap tbm)
static

Definition at line 1359 of file tidbitmap.c.

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

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 1287 of file tidbitmap.c.

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

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 1253 of file tidbitmap.c.

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

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 754 of file tidbitmap.c.

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

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

◆ tbm_private_iterate()

bool tbm_private_iterate ( TBMPrivateIterator iterator,
TBMIterateResult tbmres 
)

Definition at line 977 of file tidbitmap.c.

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

References Assert(), PagetableEntry::blockno, TBMIterateResult::blockno, TIDBitmap::entry1, TBMIterateResult::internal_page, InvalidBlockNumber, TIDBitmap::iterating, TBMIterateResult::lossy, TIDBitmap::nchunks, TIDBitmap::npages, PAGES_PER_CHUNK, PagetableEntry::recheck, TBMIterateResult::recheck, TBMPrivateIterator::schunkbit, TBMPrivateIterator::schunkptr, TIDBitmap::schunks, TBMPrivateIterator::spageptr, TIDBitmap::spages, TIDBitmap::status, TBMPrivateIterator::tbm, tbm_advance_schunkbit(), TBM_ITERATING_PRIVATE, and TBM_ONE_PAGE.

Referenced by entryGetItem(), and tbm_iterate().

◆ tbm_shared_comparator()

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

Definition at line 1442 of file tidbitmap.c.

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

References arg, and PagetableEntry::blockno.

Referenced by tbm_prepare_shared_iterate().

◆ tbm_shared_iterate()

bool tbm_shared_iterate ( TBMSharedIterator iterator,
TBMIterateResult tbmres 
)

Definition at line 1057 of file tidbitmap.c.

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

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

Referenced by tbm_iterate().

◆ tbm_union()

void tbm_union ( TIDBitmap a,
const TIDBitmap b 
)

Definition at line 447 of file tidbitmap.c.

448{
449 Assert(!a->iterating);
450 /* Nothing to do if b is empty */
451 if (b->nentries == 0)
452 return;
453 /* Scan through chunks and pages in b, merge into a */
454 if (b->status == TBM_ONE_PAGE)
455 tbm_union_page(a, &b->entry1);
456 else
457 {
458 pagetable_iterator i;
459 PagetableEntry *bpage;
460
461 Assert(b->status == TBM_HASH);
462 pagetable_start_iterate(b->pagetable, &i);
463 while ((bpage = pagetable_iterate(b->pagetable, &i)) != NULL)
464 tbm_union_page(a, bpage);
465 }
466}
static void tbm_union_page(TIDBitmap *a, const PagetableEntry *bpage)
Definition: tidbitmap.c:470

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 470 of file tidbitmap.c.

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

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