Re: Fix CPU cost of right-semi and right-anti hash joins

Поиск
Список
Период
Сортировка
Искать

Re: Fix CPU cost of right-semi and right-anti hash joins

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
Вложения:
On Wed, Aug 19, 2026 at 4:24 PM Haibo Yan  wrote:
> For JOIN_RIGHT_SEMI and JOIN_RIGHT_ANTI, however, the physical outer and inner
> paths have been swapped.  outer_path_rows therefore belongs to the physically
> swapped probe side, while outer_match_frac was computed by
> compute_semi_anti_join_factors() for the canonical semi/anti orientation.

I agree with this diagnosis.  The problem is hashjointuples itself,
not just the cpu_tuple_cost multiplier.

> I tried fixing that estimate directly instead of changing the population used
> by cpu_tuple_cost:
>
>   if (path->jpath.jointype == JOIN_RIGHT_SEMI ||
>       path->jpath.jointype == JOIN_RIGHT_ANTI)
>       hashjointuples =
>           approx_tuple_count(root, &path->jpath, hashclauses);
>   else if (path->jpath.jointype == JOIN_ANTI)
>       hashjointuples = outer_path_rows - outer_matched_rows;
>   else
>       hashjointuples = outer_matched_rows;
>
> approx_tuple_count() is already used by the other branch of
> final_cost_hashjoin() to estimate the hash-clause candidate-pair population.
> Its pair-count formula is  symmetric under swapping the two input row counts,
> so it does not have the orientation mismatch above.

I don't think this is correct.  approx_tuple_count() estimates the
number of matching pairs under inner-join semantics.  A right-semi
join emits each matched inner row once, so the matching pair count
overestimates whenever inner rows match more than one outer row on
average, which is exactly the situation these join types are for, a
small hashed input probed by a large one.

For right-anti it's not even close to the right answer.  The join
emits the unmatched inner rows, and the matching pair count says
nothing about that.  If anything, the two move in opposite directions.
A mostly-unmatched inner side has pairs ~ 0 while the output is nearly
the whole inner side; a mostly-matched one can have a huge pair count
while the output is nearly empty.

The orientation-independence you point out is the right requirement,
though, and outer_match_frac already has it, as it always describes
the semijoin's LHS.  So we can compute hashjointuples leveraging that,
inner_path_rows * outer_match_frac for right-semi and inner_path_rows
* (1 - outer_match_frac) for right-anti.

Where approx_tuple_count() is the right tool is when we compute
outer_matched_rows in the inner_unique branch.  This is because each
outer row has at most one match there, so the matched outer rows are
exactly the matching pairs.

- Richard


Re: Fix CPU cost of right-semi and right-anti hash joins

От:
wenhui qiu <qiuwenhuifx@gmail.com>
Дата:
Вложения:
Hi Richard



> hashjointuples is the third count for all of the semi/anti join types.
> That's exactly right for otherquals and cpu_tuple_cost, but wrong for
> joinquals.  For plain joins it is the second count, so there the
> charge is right for joinquals and pessimistic for otherquals.  That is
> also what the comment below means:
I found an older explanation from Tom Lane that seems relevant here:
https://postgr.es/m/21225.1371131974@sss.pgh.pa.us

He points out that costing joinrestrictinfo “returns the costs for ALL the quals”, and that after subtracting hash_qual_cost, qp_qual_cost represents all additional clauses beside the hash clauses.

So in this case qp_qual_cost combines two executor qual classes with different evaluation populations: non-hash joinquals and pushed-down otherquals. It therefore seems that changing the multiplier for the combined qp_qual_cost cannot in general make both parts correct.

Would it make sense to split those two costs before applying the right-anti candidate-pair multiplier?



Thanks 

 

Re: Fix CPU cost of right-semi and right-anti hash joins

От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
On Mon, Aug 24, 2026 at 1:35 AM Richard Guo  wrote:
>
> On Sat, Aug 22, 2026 at 2:15 AM Haibo Yan  wrote:
> > One small testing point: the new hj_small/hj_large cases have no non-hash
> > joinqual, so qp_qual_cost.per_tuple is zero and they don’t exercise the new
> > v4 branch.
>
> > Also the test comment saying cpu_tuple_cost and qp_qual_cost are both charged
> > on inner-side rows is no longer quite true for RIGHT_ANTI in v4.
>
> Right.  Fixed.
>
> Attached is v5 of this patch.  I plan to push it soon if there are no
> objections.  It can result in plan changes, so I don't plan to
> back-patch it to stable branches.  I once thought about pushing it to
> v19, but at this late phase of the v19 cycle I'm inclined not to.
> Maybe I would have done that if it were June.
>
> Any thoughts are welcome.
>
> - Richard

Thanks, v5 looks good to me. The RIGHT_ANTI qual-cost split now matches the
executor behavior I was seeing, and the added Join Filter test addresses my
previous testing concern.

I also agree with not back-patching this, and at this point in the cycle I’d
be inclined to leave v19 alone as well.

LGTM.


FAQ