compute_bitmap_pages' loop_count parameter should be double not int.
authorTom Lane <[email protected]>
Mon, 18 Dec 2023 17:46:07 +0000 (12:46 -0500)
committerTom Lane <[email protected]>
Mon, 18 Dec 2023 17:46:10 +0000 (12:46 -0500)
The value was double in the original implementation of this logic.
Commit da08a6598 pulled it out into a subroutine, but carelessly
declared the parameter as int when it should have been double.
On most platforms, the only ill effect would be to clamp the value
to be not more than INT_MAX, which would seldom be exceeded and
probably wouldn't change the estimates too much anyway.  Nonetheless,
it's wrong and can cause complaints from ubsan.

While here, improve the comments and parameter names.

This is an ABI change in a globally exposed subroutine, so
back-patching would create some risk of breaking extensions.
The value of the fix doesn't seem high enough to warrant taking
that risk, so fix in HEAD only.

Per report from Alexander Lakhin.

Discussion: https://postgr.es/m/f5e15fe1-202d-1936-f47c-f0c69a936b72@gmail.com

src/backend/optimizer/path/costsize.c
src/include/optimizer/cost.h

index d6ceafd51c5cdb499704adfab046830c4286338f..5227346aeb1aa07e292a81ecb3a40cfd6ea93df8 100644 (file)
@@ -6393,12 +6393,20 @@ get_parallel_divisor(Path *path)
 
 /*
  * compute_bitmap_pages
+ *   Estimate number of pages fetched from heap in a bitmap heap scan.
  *
- * compute number of pages fetched from heap in bitmap heap scan.
+ * 'baserel' is the relation to be scanned
+ * 'bitmapqual' is a tree of IndexPaths, BitmapAndPaths, and BitmapOrPaths
+ * 'loop_count' is the number of repetitions of the indexscan to factor into
+ *     estimates of caching behavior
+ *
+ * If cost_p isn't NULL, the indexTotalCost estimate is returned in *cost_p.
+ * If tuples_p isn't NULL, the tuples_fetched estimate is returned in *tuples_p.
  */
 double
-compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual,
-                    int loop_count, Cost *cost, double *tuple)
+compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel,
+                    Path *bitmapqual, double loop_count,
+                    Cost *cost_p, double *tuples_p)
 {
    Cost        indexTotalCost;
    Selectivity indexSelectivity;
@@ -6488,10 +6496,10 @@ compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual,
                              (lossy_pages / heap_pages) * baserel->tuples);
    }
 
-   if (cost)
-       *cost = indexTotalCost;
-   if (tuple)
-       *tuple = tuples_fetched;
+   if (cost_p)
+       *cost_p = indexTotalCost;
+   if (tuples_p)
+       *tuples_p = tuples_fetched;
 
    return pages_fetched;
 }
index 6d50afbf74c804800cff38b35b2259edcab5139c..366adbfc39eee895d02209b739cdbe27db9ca27b 100644 (file)
@@ -210,6 +210,7 @@ extern void set_result_size_estimates(PlannerInfo *root, RelOptInfo *rel);
 extern void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel);
 extern PathTarget *set_pathtarget_cost_width(PlannerInfo *root, PathTarget *target);
 extern double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel,
-                                  Path *bitmapqual, int loop_count, Cost *cost, double *tuple);
+                                  Path *bitmapqual, double loop_count,
+                                  Cost *cost_p, double *tuples_p);
 
 #endif                         /* COST_H */