Skip to content

Commit 28555a5

Browse files
committed
Skip system attributes when applying mvdistinct stats
When estimating number of distinct groups, we failed to ignore system attributes when matching the group expressions to mvdistinct stats, causing failures like ERROR: negative bitmapset member not allowed Fix that by simply skipping anything that is not a regular attribute. Backpatch to PostgreSQL 10, where the extended stats were introduced. Bug: #16111 Reported-by: Tuomas Leikola Author: Tomas Vondra Backpatch-through: 10 Discussion: https://postgr.es/m/16111-687799584c3a7e73@postgresql.org
1 parent 24897e1 commit 28555a5

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,14 +3587,19 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
35873587
foreach(lc, *varinfos)
35883588
{
35893589
GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
3590+
AttrNumber attnum;
35903591

35913592
Assert(varinfo->rel == rel);
35923593

3593-
if (IsA(varinfo->var, Var))
3594-
{
3595-
attnums = bms_add_member(attnums,
3596-
((Var *) varinfo->var)->varattno);
3597-
}
3594+
if (!IsA(varinfo->var, Var))
3595+
continue;
3596+
3597+
attnum = ((Var *) varinfo->var)->varattno;
3598+
3599+
if (!AttrNumberIsForUserDefinedAttr(attnum))
3600+
continue;
3601+
3602+
attnums = bms_add_member(attnums, attnum);
35983603
}
35993604

36003605
/* look for the ndistinct statistics matching the most vars */
@@ -3674,6 +3679,10 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
36743679
}
36753680

36763681
attnum = ((Var *) varinfo->var)->varattno;
3682+
3683+
if (!AttrNumberIsForUserDefinedAttr(attnum))
3684+
continue;
3685+
36773686
if (!bms_is_member(attnum, matched))
36783687
newlist = lappend(newlist, varinfo);
36793688
}

src/test/regress/expected/stats_ext.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ SELECT s.stxkind, d.stxdndistinct
216216
{d,f,m} | {"3, 4": 11, "3, 6": 11, "4, 6": 11, "3, 4, 6": 11}
217217
(1 row)
218218

219+
-- minor improvement, make sure the ctid does not break the matching
220+
SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ctid, a, b');
221+
estimated | actual
222+
-----------+--------
223+
11 | 1000
224+
(1 row)
225+
219226
-- Hash Aggregate, thanks to estimates improved by the statistic
220227
SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, b');
221228
estimated | actual

src/test/regress/sql/stats_ext.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ SELECT s.stxkind, d.stxdndistinct
157157
WHERE s.stxrelid = 'ndistinct'::regclass
158158
AND d.stxoid = s.oid;
159159

160+
-- minor improvement, make sure the ctid does not break the matching
161+
SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ctid, a, b');
162+
160163
-- Hash Aggregate, thanks to estimates improved by the statistic
161164
SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, b');
162165

0 commit comments

Comments
 (0)