mirror of
https://github.com/postgres/postgres.git
synced 2025-11-03 09:13:20 +03:00
Ignore nullingrels when looking up statistics
When looking up statistical data about an expression, we do not need to concern ourselves with the outer joins that could null the Vars/PHVs contained in the expression. Accounting for nullingrels in the expression could cause estimate_num_groups to count the same Var multiple times if it's marked with different nullingrels. This is incorrect, and could lead to "ERROR: corrupt MVNDistinct entry" when searching for multivariate n-distinct. Furthermore, the nullingrels could prevent us from matching an expression to expressional index columns or to the expressions in extended statistics, leading to inaccurate estimates. To fix, strip out all the nullingrels from the expression before we look up statistical data about it. There is one ensuing plan change in the regression tests, but it looks reasonable and does not compromise its original purpose. This patch could result in plan changes, but it fixes an actual bug, so back-patch to v16 where the outer-join-aware-Var infrastructure was introduced. Author: Richard Guo Discussion: https://postgr.es/m/CAMbWs4-2Z4k+nFTiZe0Qbu5n8juUWenDAtMzi98bAZQtwHx0-w@mail.gmail.com
This commit is contained in:
@@ -3033,3 +3033,16 @@ SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
|
||||
|
||||
RESET enable_seqscan;
|
||||
RESET enable_indexscan;
|
||||
|
||||
-- Test that we do not account for nullingrels when looking up statistics
|
||||
CREATE TABLE group_tbl (a INT, b INT);
|
||||
INSERT INTO group_tbl SELECT 1, 1;
|
||||
CREATE STATISTICS group_tbl_stat (ndistinct) ON a, b FROM group_tbl;
|
||||
ANALYZE group_tbl;
|
||||
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT 1 FROM group_tbl t1
|
||||
LEFT JOIN (SELECT a c1, COALESCE(a) c2 FROM group_tbl t2) s ON TRUE
|
||||
GROUP BY s.c1, s.c2;
|
||||
|
||||
DROP TABLE group_tbl;
|
||||
|
||||
Reference in New Issue
Block a user