Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 67251c8

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 c862299 commit 67251c8

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3989,11 +3989,11 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
39893989

39903990
attnum = ((Var *) varinfo->var)->varattno;
39913991

3992-
if (!AttrNumberIsForUserDefinedAttr(attnum))
3992+
if (AttrNumberIsForUserDefinedAttr(attnum) &&
3993+
bms_is_member(attnum, matched))
39933994
continue;
39943995

3995-
if (!bms_is_member(attnum, matched))
3996-
newlist = lappend(newlist, varinfo);
3996+
newlist = lappend(newlist, varinfo);
39973997
}
39983998

39993999
*varinfos = newlist;

src/test/regress/expected/stats_ext.out

Lines changed: 1 addition & 1 deletion
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)