On Tue, 1 Dec 2026 at 07:13, Richard Guo <[email protected]> wrote: > > On Mon, Nov 30, 2026 at 5:51 PM Kirill Reshke <[email protected]> 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
Thanks for the explanations. I did understand that we don't guarantee anything about the number of calls of volatile function, but in this case the query can return different results, which is usually bad. TIL this is not always that bad. Since this was possible before 383eb21, its OK. -- Best regards, Kirill Reshke
