From eb12c61a4872f5ac735ba85f89fea5b9e543edb4 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Tue, 20 Oct 2026 16:14:50 -0300 Subject: [PATCH v2] Ignore grouping-step nullingrels when looking up statistics Since commit f5050f795, a grouping expression that does not appear in every grouping set is marked nullable by the grouping step: the RT index of the GROUP RTE is added to the nullingrels of the Vars it contains. When looking up statistics, selfuncs.c strips nullingrels belonging to outer joins (commit e28033fe1), but not the grouping step's RT index. As a result, estimate_num_groups could create two GroupVarInfos for the same Var: one for a plain grouping column that is present in all grouping sets, and one for the same column pulled out of an expression that is not. When an ndistinct extended statistics object covers that column, estimate_multivariate_ndistinct counts the duplicate as two matching attributes, then fails to find an item for what is really a single attribute, and reports "corrupt MVNDistinct entry". The extra nullingrels bit also kept examine_variable from matching such expressions to expression indexes or to expressions in extended statistics, since pull_varnos reports the GROUP RTE as a second relation and equal() sees the differing nullingrels. To fix, also strip the grouping step's RT index in add_unique_group_var and examine_variable. The other place that strips outer-join nullingrels, estimate_multivariate_bucketsize, only sees hash join keys, which are computed below the grouping step and so never carry its RT index. Back-patch to v18, where the GROUP RTE was introduced. Reported-by: alpkeles99@gmail.com Reviewed-by: Aleksander Alekseev Discussion: https://www.postgresql.org/message-id/19772-d8c886b4db230132@postgresql.org Backpatch-through: 18 --- src/backend/utils/adt/selfuncs.c | 25 ++++++++++--- src/test/regress/expected/stats_ext.out | 47 +++++++++++++++++++++++++ src/test/regress/sql/stats_ext.sql | 39 ++++++++++++++++++++ 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index 5ee377595bd..05f0a2a69c6 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -3656,6 +3656,21 @@ matchingjoinsel(PG_FUNCTION_ARGS) } +/* + * Get the set of nulling relids that should be ignored when looking up + * statistical data about an expression (the outer joins, plus the grouping + * step if there is one). + */ +static Relids +get_stats_nulling_relids(PlannerInfo *root) +{ + if (root->group_rtindex == 0) + return root->outer_join_rels; + + return bms_add_member(bms_copy(root->outer_join_rels), + root->group_rtindex); +} + /* * Helper routine for estimate_num_groups: add an item to a list of * GroupVarInfos, but only if it's not known equal to any of the existing @@ -3687,7 +3702,7 @@ add_unique_group_var(PlannerInfo *root, List *varinfos, * extended statistics (see estimate_multivariate_ndistinct). So strip * them out first. */ - var = remove_nulling_relids(var, root->outer_join_rels, NULL); + var = remove_nulling_relids(var, get_stats_nulling_relids(root), NULL); foreach(lc, varinfos) { @@ -5653,6 +5668,7 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid, Node *basenode; Relids varnos; Relids basevarnos; + Relids nullingrels; RelOptInfo *onerel; /* Make sure we don't return dangling pointers in vardata */ @@ -5703,7 +5719,8 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid, * relation are considered "real" vars. */ varnos = pull_varnos(root, basenode); - basevarnos = bms_difference(varnos, root->outer_join_rels); + nullingrels = get_stats_nulling_relids(root); + basevarnos = bms_difference(varnos, nullingrels); onerel = NULL; @@ -5775,8 +5792,8 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid, * matching it to expressional index columns or to the expressions in * extended statistics. So strip them out first. */ - if (bms_overlap(varnos, root->outer_join_rels)) - node = remove_nulling_relids(node, root->outer_join_rels, NULL); + if (bms_overlap(varnos, nullingrels)) + node = remove_nulling_relids(node, nullingrels, NULL); foreach(ilist, onerel->indexlist) { diff --git a/src/test/regress/expected/stats_ext.out b/src/test/regress/expected/stats_ext.out index 42d6c2777ea..c1d2948291b 100644 --- a/src/test/regress/expected/stats_ext.out +++ b/src/test/regress/expected/stats_ext.out @@ -1104,6 +1104,53 @@ SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, ( DROP STATISTICS s11; DROP STATISTICS s12; +-- grouping sets where an expression on a column is nullable by grouping but +-- the plain column is not, so the column's Var gets different nullingrels +CREATE STATISTICS s11 (ndistinct) ON a, b FROM ndistinct; +ANALYZE ndistinct; +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, ROLLUP ((a+1))'); + estimated | actual +-----------+-------- + 6 | 6 +(1 row) + +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY GROUPING SETS ((a, (a+1)), (a))'); + estimated | actual +-----------+-------- + 6 | 6 +(1 row) + +DROP STATISTICS s11; +-- same, with statistics on the expression +CREATE STATISTICS s11 (ndistinct) ON a, (a+1) FROM ndistinct; +ANALYZE ndistinct; +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, ROLLUP ((a+1))'); + estimated | actual +-----------+-------- + 6 | 6 +(1 row) + +DROP STATISTICS s11; +-- a grouping expression that is nullable by grouping should still be +-- matched to expression statistics and expression indexes +CREATE STATISTICS s11 ON (d % 2) FROM ndistinct; +ANALYZE ndistinct; +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ROLLUP ((d % 2))'); + estimated | actual +-----------+-------- + 3 | 3 +(1 row) + +DROP STATISTICS s11; +CREATE INDEX ndistinct_d_mod_idx ON ndistinct ((d % 2)); +ANALYZE ndistinct; +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ROLLUP ((d % 2))'); + estimated | actual +-----------+-------- + 3 | 3 +(1 row) + +DROP INDEX ndistinct_d_mod_idx; -- functional dependencies tests CREATE TABLE functional_dependencies ( filler1 TEXT, diff --git a/src/test/regress/sql/stats_ext.sql b/src/test/regress/sql/stats_ext.sql index 6045cfa51e8..146e4ed558c 100644 --- a/src/test/regress/sql/stats_ext.sql +++ b/src/test/regress/sql/stats_ext.sql @@ -596,6 +596,45 @@ SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, ( DROP STATISTICS s11; DROP STATISTICS s12; +-- grouping sets where an expression on a column is nullable by grouping but +-- the plain column is not, so the column's Var gets different nullingrels +CREATE STATISTICS s11 (ndistinct) ON a, b FROM ndistinct; + +ANALYZE ndistinct; + +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, ROLLUP ((a+1))'); + +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY GROUPING SETS ((a, (a+1)), (a))'); + +DROP STATISTICS s11; + +-- same, with statistics on the expression +CREATE STATISTICS s11 (ndistinct) ON a, (a+1) FROM ndistinct; + +ANALYZE ndistinct; + +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, ROLLUP ((a+1))'); + +DROP STATISTICS s11; + +-- a grouping expression that is nullable by grouping should still be +-- matched to expression statistics and expression indexes +CREATE STATISTICS s11 ON (d % 2) FROM ndistinct; + +ANALYZE ndistinct; + +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ROLLUP ((d % 2))'); + +DROP STATISTICS s11; + +CREATE INDEX ndistinct_d_mod_idx ON ndistinct ((d % 2)); + +ANALYZE ndistinct; + +SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY ROLLUP ((d % 2))'); + +DROP INDEX ndistinct_d_mod_idx; + -- functional dependencies tests CREATE TABLE functional_dependencies ( filler1 TEXT, -- 2.50.1 (Apple Git-155)