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

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix CPU cost of right-semi and right-anti hash joins
Date: 2026-08-20 06:20:36
Message-ID: CAMbWs48Yhk2R7kHmnJ+CN3_i3i=UhDF-hGgHErs6npnmT11ddQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Aug 18, 2026 at 6:22 PM Ayush Tiwari
<ayushtiwari(dot)slg01(at)gmail(dot)com> wrote:
> I tried a case with 1000 unmatched hash tuples where the filter allowed
> only one through. The executor examined all 1000 and reported "Rows
> Removed by Filter: 999", while path->rows was one. The patch reduced the
> cost by 9.99, exactly 999 * cpu_tuple_cost.
>
> Could using path->rows therefore undercharge the rows that were examined
> but filtered out?

Yeah, you're right. path->rows is the row count after all the quals
have been applied, while the per-tuple charges here are meant to cover
the rows that come out of the hash-clause matching, before any other
quals filter them.

I think a more accurate way is to compute hashjointuples from the
inner side directly:

right-semi: inner_path_rows * outer_match_frac
right-anti: inner_path_rows * (1 - outer_match_frac)

This is mirroring what JOIN_SEMI and JOIN_ANTI already do with the
outer side. Both cpu_tuple_cost and the quals not used for hashing
are charged on that count, so the 1000 rows your executor examined are
all charged, and the filter's selectivity affects only the join's
output row estimate.

Attached v3 patch does that.

- Richard

Attachment Content-Type Size
v3-0001-Fix-CPU-cost-of-right-semi-and-right-anti-hash-jo.patch application/octet-stream 19.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bharath Rupireddy 2026-08-20 06:25:00 Re: [PATCH] Release replication slot on error in SQL-callable slot functions
Previous Message cca5507 2026-08-20 06:17:41 Re: timeout value overflow in wait for lsn