diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c index c66019e..20604d6 100644 --- a/src/backend/nodes/tidbitmap.c +++ b/src/backend/nodes/tidbitmap.c @@ -1562,3 +1562,38 @@ pagetable_free(pagetable_hash *pagetable, void *pointer) tbm->dsapagetableold = InvalidDsaPointer; } } + +/* + * tbm_calculate_exact_pages + * + * Calculate the number of exact pages which will fit into bitmap for given + * memory (maxbytes) and given pages. + */ +double +tbm_calculate_exact_pages(double maxbytes, double pages) +{ + long nbuckets; + double exact = -1; + + /* + * Estimate number of hashtable entries we can have within maxbytes. This + * estimates the hash cost as sizeof(PagetableEntry). + */ + nbuckets = maxbytes / + (sizeof(PagetableEntry) + sizeof(Pointer) + sizeof(Pointer)); + nbuckets = Min(nbuckets, INT_MAX - 1); /* safety limit */ + nbuckets = Max(nbuckets, 16); /* sanity limit */ + + /* + * Compute the number of exact_bucktes. Number of exact pages will be same + * as exact_bucket as each exct_bucket will contain only one page + * information. We derive below formulae by these 2 equations. + * + * Eq1: nbuckets = exact_bucket + lossy_buckets + * Eq2: total_pages = exact_bucket + (lossy_buckets * WORDS_PER_CHUNK) + */ + exact = (WORDS_PER_CHUNK * nbuckets) / (WORDS_PER_CHUNK-1) - + pages / (WORDS_PER_CHUNK-1); + + return exact; +} diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 52643d0..1d18177 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -5125,6 +5125,9 @@ compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, double T; double pages_fetched; double tuples_fetched; + double heap_pages = 0; + double exact_pages = 0; + double lossy_pages = 0; /* * Fetch total cost of obtaining the bitmap, as well as its total @@ -5169,8 +5172,45 @@ compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, else pages_fetched = ceil(pages_fetched); + /* + * Calculate the number of pages fetched from the heap. Then based on + * current work_mem estimate how many pages are going to exactly fit + * into bitmap and how many pages will be lossyfied. + * + * Fixme: Mackert and Lohman formula will give the number of heap pages + * fetched but for our calculation we need number of unique heap_pages + * which are actualy going to the bitmap. + */ + heap_pages = Min(pages_fetched, baserel->pages); + exact_pages = tbm_calculate_exact_pages(work_mem * 1024L, heap_pages); + + /* + * We have already got the exact pages so remaining pages will be lossy. + */ + if (heap_pages > exact_pages) + lossy_pages = heap_pages - exact_pages; + + /* + * If there are lossy pages then recompute the number of tuples processed + * by the bitmap heap node. For the exact_pages it's baserel->tuples * + * indexSelectivity and for lossy_pages we have to process all the tuples. + */ + if (exact_pages > 0 && lossy_pages) + tuples_fetched = clamp_row_est (indexSelectivity * + (exact_pages / heap_pages) * baserel->tuples + + (lossy_pages / heap_pages) * baserel->tuples); + if (cost) + { *cost = indexTotalCost; + + /* + * If exact_pages are -ve then even after lossifying we will not be + * able to fit them into work_mem. Hence, disable this path. + */ + if (exact_pages < 0) + *cost += disable_cost; + } if (tuple) *tuple = tuples_fetched; diff --git a/src/include/nodes/tidbitmap.h b/src/include/nodes/tidbitmap.h index 87f4bb7..39c3da3 100644 --- a/src/include/nodes/tidbitmap.h +++ b/src/include/nodes/tidbitmap.h @@ -71,4 +71,5 @@ extern void tbm_end_shared_iterate(TBMSharedIterator *iterator); extern TBMSharedIterator *tbm_attach_shared_iterate(dsa_area *dsa, dsa_pointer dp); +extern double tbm_calculate_exact_pages(double maxbytes, double pages); #endif /* TIDBITMAP_H */