Re: NOT-IN to ANTI-JOIN conversation changes query result

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: NOT-IN to ANTI-JOIN conversation changes query result
Date: 2026-12-01 02:13:47
Message-ID: CAMbWs49DnMPHwHiYknybNa--7=HQB01Ry-hHUfPvBunaJburDg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Nov 30, 2026 at 5:51 PM Kirill Reshke <reshkekirill(at)gmail(dot)com> wrote:
> Since 383eb21 conversion planner can change query results when the
> uncorrelated RHS subquery has a volatile filter and the join plan
> rescans that side.

I don't think this is a bug, and I don't think this is specific to NOT
IN. Positive IN has been pulled up to a semi-join for a long time,
and it behaves the same way. Using your tables:

SET enable_hashjoin = off;
SET enable_mergejoin = off;
SET enable_material = off;
SET enable_hashagg = off;
SET enable_sort = off;

SELECT a FROM notin_volatile_outer
WHERE a IN
(SELECT v FROM notin_volatile_inner
WHERE nextval('notin_volatile_seq') % 3 <> 1);

This returns {2} via a nestloop semi-join that rescans the inner side,
whereas the same query kept as a hashed SubPlan returns no rows.

How many times a volatile function in a sublink gets evaluated has
always depended on the plan. Even before 383eb21, a NOT IN SubPlan
that isn't hashed and has no Material node above it reruns for every
outer row.

Regarding your patch, rejecting volatile sub-selects only for NOT IN
would make it inconsistent with IN, and doing so for IN as well would
change plans that have been around for years. So I'm not inclined to
change this, unless there's a real-world case where it matters.

- Richard

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ideriha, Takeshi 2026-12-01 02:36:09 Doc: Fix BrinOpcInfo members to match the code
Previous Message David Rowley 2026-12-01 01:56:57 Should we add timing output to pg_upgrade?