From fdb8c9afda45a45b6a5922e14c711f6c1dd201d0 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Mon, 17 Jun 2024 14:11:50 +0200 Subject: [PATCH v20240617 04/56] Remove estimiatedcluases and varRelid arguments comments and Assert around the changes provides more information. --- src/backend/optimizer/path/clausesel.c | 16 ++++++++++------ src/backend/statistics/extended_stats.c | 24 ++++++++++-------------- src/include/statistics/statistics.h | 4 +--- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/backend/optimizer/path/clausesel.c b/src/backend/optimizer/path/clausesel.c index 871d73e3b4f..a0ab95553bc 100644 --- a/src/backend/optimizer/path/clausesel.c +++ b/src/backend/optimizer/path/clausesel.c @@ -209,14 +209,18 @@ clauselist_selectivity_ext(PlannerInfo *root, * to detect when this makes sense, but we can check that there are join * clauses, and that at least some of the rels have stats. * - * XXX Isn't this mutually exclusive with the preceding block which - * calculates estimates for a single relation? + * rel != NULL can't grantee the clause is not a join clause, for example + * t1 left join t2 ON t1.a = 3, but it can grantee we can't use extended + * statistics for estimation since it has only 1 relid. + * + * XXX: so we can grantee estimatedclauses == NULL now, so estimatedclauses + * in statext_try_join_estimates is removed. */ - if (use_extended_stats && - statext_try_join_estimates(root, clauses, varRelid, jointype, sjinfo, - estimatedclauses)) + if (use_extended_stats && rel == NULL && + statext_try_join_estimates(root, clauses, varRelid, jointype, sjinfo)) { - s1 *= statext_clauselist_join_selectivity(root, clauses, varRelid, + Assert(varRelid == 0); + s1 *= statext_clauselist_join_selectivity(root, clauses, jointype, sjinfo, &estimatedclauses); } diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 25b4d486a09..71e47748d23 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -2829,8 +2829,7 @@ statext_determine_join_restrictions(PlannerInfo *root, RelOptInfo *rel, * on the conditions, to make sure it can be estimated using extended stats. */ static bool -statext_is_supported_join_clause(PlannerInfo *root, Node *clause, - int varRelid, SpecialJoinInfo *sjinfo) +statext_is_supported_join_clause(PlannerInfo *root, Node *clause, SpecialJoinInfo *sjinfo) { Oid oprsel; RestrictInfo *rinfo; @@ -2842,7 +2841,9 @@ statext_is_supported_join_clause(PlannerInfo *root, Node *clause, * * XXX See treat_as_join_clause. */ - if ((varRelid != 0) || (sjinfo == NULL)) + + /* duplicated with statext_try_join_estimates */ + if (sjinfo == NULL) return false; /* XXX Can we rely on always getting RestrictInfo here? */ @@ -2928,8 +2929,7 @@ statext_is_supported_join_clause(PlannerInfo *root, Node *clause, */ bool statext_try_join_estimates(PlannerInfo *root, List *clauses, int varRelid, - JoinType jointype, SpecialJoinInfo *sjinfo, - Bitmapset *estimatedclauses) + JoinType jointype, SpecialJoinInfo *sjinfo) { int listidx; int k; @@ -2968,15 +2968,11 @@ statext_try_join_estimates(PlannerInfo *root, List *clauses, int varRelid, /* needs to happen before skipping any clauses */ listidx++; - /* Skip clauses that were already estimated. */ - if (bms_is_member(listidx, estimatedclauses)) - continue; - /* * Skip clauses that are not join clauses or that we don't know how to * handle estimate using extended statistics. */ - if (!statext_is_supported_join_clause(root, clause, varRelid, sjinfo)) + if (!statext_is_supported_join_clause(root, clause, sjinfo)) continue; /* @@ -3048,7 +3044,7 @@ typedef struct JoinPairInfo * with F_EQJOINSEL selectivity function at the moment). */ static JoinPairInfo * -statext_build_join_pairs(PlannerInfo *root, List *clauses, int varRelid, +statext_build_join_pairs(PlannerInfo *root, List *clauses, JoinType jointype, SpecialJoinInfo *sjinfo, Bitmapset *estimatedclauses, int *npairs) { @@ -3084,7 +3080,7 @@ statext_build_join_pairs(PlannerInfo *root, List *clauses, int varRelid, * moment we support just (Expr op Expr) clauses with each side * referencing just a single relation). */ - if (!statext_is_supported_join_clause(root, clause, varRelid, sjinfo)) + if (!statext_is_supported_join_clause(root, clause, sjinfo)) continue; /* statext_is_supported_join_clause guarantees RestrictInfo */ @@ -3282,7 +3278,7 @@ get_expression_for_rel(PlannerInfo *root, RelOptInfo *rel, Node *clause) * XXX Isn't the preceding comment stale? We skip the optimization, no? */ Selectivity -statext_clauselist_join_selectivity(PlannerInfo *root, List *clauses, int varRelid, +statext_clauselist_join_selectivity(PlannerInfo *root, List *clauses, JoinType jointype, SpecialJoinInfo *sjinfo, Bitmapset **estimatedclauses) { @@ -3297,7 +3293,7 @@ statext_clauselist_join_selectivity(PlannerInfo *root, List *clauses, int varRel return 1.0; /* extract pairs of joined relations from the list of clauses */ - info = statext_build_join_pairs(root, clauses, varRelid, jointype, sjinfo, + info = statext_build_join_pairs(root, clauses, jointype, sjinfo, *estimatedclauses, &ninfo); /* no useful join pairs */ diff --git a/src/include/statistics/statistics.h b/src/include/statistics/statistics.h index 48ab718304e..4bd3104a2b7 100644 --- a/src/include/statistics/statistics.h +++ b/src/include/statistics/statistics.h @@ -131,11 +131,9 @@ extern StatisticExtInfo *statext_find_matching_mcv(PlannerInfo *root, RelOptInfo Bitmapset *attnums, List *exprs); extern bool statext_try_join_estimates(PlannerInfo *root, List *clauses, int varRelid, - JoinType jointype, SpecialJoinInfo *sjinfo, - Bitmapset *estimatedclauses); + JoinType jointype, SpecialJoinInfo *sjinfo); extern Selectivity statext_clauselist_join_selectivity(PlannerInfo *root, List *clauses, - int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, Bitmapset **estimatedclauses); -- 2.45.2