Skip to content

Commit 33e52ad

Browse files
committed
Fix ndistinct estimates with system attributes
When estimating the number of groups using extended statistics, the code was discarding information about system attributes. This led to strange situation that SELECT 1 FROM t GROUP BY ctid; could have produced higher estimate (equal to pg_class.reltuples) than SELECT 1 FROM t GROUP BY a, b, ctid; with extended statistics on (a,b). Fixed by retaining information about the system attribute. Backpatch all the way to 10, where extended statistics were introduced. Author: Tomas Vondra Backpatch-through: 10
1 parent a14a011 commit 33e52ad

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/backend/utils/adt/selfuncs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -3987,11 +3987,11 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
39873987

39883988
attnum = ((Var *) varinfo->var)->varattno;
39893989

3990-
if (!AttrNumberIsForUserDefinedAttr(attnum))
3990+
if (AttrNumberIsForUserDefinedAttr(attnum) &&
3991+
bms_is_member(attnum, matched))
39913992
continue;
39923993

3993-
if (!bms_is_member(attnum, matched))
3994-
newlist = lappend(newlist, varinfo);
3994+
newlist = lappend(newlist, varinfo);
39953995
}
39963996

39973997
*varinfos = newlist;

src/test/regress/expected/stats_ext.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ SELECT s.stxkind, d.stxdndistinct
260260
SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ctid, a, b');
261261
estimated | actual
262262
-----------+--------
263-
11 | 1000
263+
1000 | 1000
264264
(1 row)
265265

266266
-- Hash Aggregate, thanks to estimates improved by the statistic

0 commit comments

Comments
 (0)