Skip to content

Commit 1f171a1

Browse files
committed
Fix thinko in estimate_num_groups
The code for the reworked n-distinct estimation on commit 7b504eb was written differently in a previous version of the patch, prior to commit; on rewriting it, we missed updating an initializer. This caused the code to (mistakenly) apply a fudge factor even in the case where a single value is applied, leading to incorrect results. This means that the 'relvarcount' variable name is now wrong. Add a comment to try and make the situation clearer, and remove an incorrect comment I added. Problem noticed, and code patch, by Tomas Vondra. Additional commentary by Álvaro.
1 parent 827d6f9 commit 1f171a1

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/backend/utils/adt/selfuncs.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -3404,7 +3404,7 @@ estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
34043404
RelOptInfo *rel = varinfo1->rel;
34053405
double reldistinct = 1;
34063406
double relmaxndistinct = reldistinct;
3407-
int relvarcount = 1;
3407+
int relvarcount = 0;
34083408
List *newvarinfos = NIL;
34093409
List *relvarinfos = NIL;
34103410

@@ -3436,6 +3436,10 @@ estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
34363436
* we multiply them together. Any remaining relvarinfos after
34373437
* no more multivariate matches are found are assumed independent too,
34383438
* so their individual ndistinct estimates are multiplied also.
3439+
*
3440+
* While iterating, count how many separate numdistinct values we
3441+
* apply. We apply a fudge factor below, but only if we multiplied
3442+
* more than one such values.
34393443
*/
34403444
while (relvarinfos)
34413445
{
@@ -3447,7 +3451,7 @@ estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
34473451
reldistinct *= mvndistinct;
34483452
if (relmaxndistinct < mvndistinct)
34493453
relmaxndistinct = mvndistinct;
3450-
relvarcount++; /* inaccurate, but doesn't matter */
3454+
relvarcount++;
34513455
}
34523456
else
34533457
{

0 commit comments

Comments
 (0)