@@ -2257,14 +2257,15 @@ cost_agg(Path *path, PlannerInfo *root,
2257
2257
*/
2258
2258
if (aggstrategy == AGG_HASHED || aggstrategy == AGG_MIXED )
2259
2259
{
2260
+ double pages ;
2260
2261
double pages_written = 0.0 ;
2261
2262
double pages_read = 0.0 ;
2262
2263
double hashentrysize ;
2263
2264
double nbatches ;
2264
2265
Size mem_limit ;
2265
2266
uint64 ngroups_limit ;
2266
2267
int num_partitions ;
2267
-
2268
+ int depth ;
2268
2269
2269
2270
/*
2270
2271
* Estimate number of batches based on the computed limits. If less
@@ -2279,25 +2280,22 @@ cost_agg(Path *path, PlannerInfo *root,
2279
2280
nbatches = Max ( (numGroups * hashentrysize ) / mem_limit ,
2280
2281
numGroups / ngroups_limit );
2281
2282
2283
+ nbatches = Max (ceil (nbatches ), 1.0 );
2284
+ num_partitions = Max (num_partitions , 2 );
2285
+
2286
+ /*
2287
+ * The number of partitions can change at different levels of
2288
+ * recursion; but for the purposes of this calculation assume it stays
2289
+ * constant.
2290
+ */
2291
+ depth = ceil ( log (nbatches ) / log (num_partitions ) );
2292
+
2282
2293
/*
2283
2294
* Estimate number of pages read and written. For each level of
2284
2295
* recursion, a tuple must be written and then later read.
2285
2296
*/
2286
- if (nbatches > 1.0 )
2287
- {
2288
- double depth ;
2289
- double pages ;
2290
-
2291
- pages = relation_byte_size (input_tuples , input_width ) / BLCKSZ ;
2292
-
2293
- /*
2294
- * The number of partitions can change at different levels of
2295
- * recursion; but for the purposes of this calculation assume it
2296
- * stays constant.
2297
- */
2298
- depth = ceil ( log (nbatches - 1 ) / log (num_partitions ) );
2299
- pages_written = pages_read = pages * depth ;
2300
- }
2297
+ pages = relation_byte_size (input_tuples , input_width ) / BLCKSZ ;
2298
+ pages_written = pages_read = pages * depth ;
2301
2299
2302
2300
startup_cost += pages_written * random_page_cost ;
2303
2301
total_cost += pages_written * random_page_cost ;
0 commit comments