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

Commit 88acb01

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 366aa37 commit 88acb01

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
@@ -3743,11 +3743,11 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
37433743

37443744
attnum = ((Var *) varinfo->var)->varattno;
37453745

3746-
if (!AttrNumberIsForUserDefinedAttr(attnum))
3746+
if (AttrNumberIsForUserDefinedAttr(attnum) &&
3747+
bms_is_member(attnum, matched))
37473748
continue;
37483749

3749-
if (!bms_is_member(attnum, matched))
3750-
newlist = lappend(newlist, varinfo);
3750+
newlist = lappend(newlist, varinfo);
37513751
}
37523752

37533753
*varinfos = newlist;

src/test/regress/expected/stats_ext.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ SELECT s.stxkind, d.stxdndistinct
222222
SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ctid, a, b');
223223
estimated | actual
224224
-----------+--------
225-
11 | 1000
225+
1000 | 1000
226226
(1 row)
227227

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

0 commit comments

Comments
 (0)