Re: PG 18 JIT regression: int4range hash breaks multi-key GROUP BY

From: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Tor Erik Linnerud <tel(at)jklm(dot)no>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: PG 18 JIT regression: int4range hash breaks multi-key GROUP BY
Date: 2026-11-17 10:03:47
Message-ID: CAJTYsWVRHfdgVQAXg0cYZypBEO=4F8bJ5TpitpyqS1xofAm7Bg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

On Tue, 17 Nov 2026 at 04:04, David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
>
> On Tue, 17 Nov 2026 at 00:27, Tor Erik Linnerud <tel(at)jklm(dot)no> wrote:
> > create table t as
> > select (random() * 1000)::int as p, random() < 0.5 as b,
> > int4range(d, d + 10) as r
> > from (select (random() * 20)::int * 10 as d from generate_series(1,
1000000)) s;
> > analyze t;
> > set max_parallel_workers_per_gather = 0;
> > set enable_sort = off;
> > set jit = on;
> > set jit_above_cost = 0;
> >
> > -- slow on 18: range key followed by another key
> > select count(*) from (select p, r, b from t group by 1, 2, 3) s;
>
> Many thanks for the clear and concise report and reproducer. That's
> becoming rare.
>
> The attached patch resolves the issue. I had to use a bit of LLM as I
> previously didn't find the proper way to trim the result to 32-bit. I
> had been using LLVMBuildAnd with l_datum_const(0xffffffff). The
> documentation for all this stuff seems nonexistent.

Yeah, I was looking into this yesterday after the report came in to see
if I could find some docs on it, and couldn't find much on the C API
either. The only useful thing I found was the LLVM LangRef for trunc
[1] and zext [2].

> The LLM tells I
> should have used LLVMBuildTrunc(), which the patch does, and now I'm
> truncating before the shift and xor then doing the zero-extend back to
> 64-bit before storing the result.

The fix lgtm. I tried Tor Erik's repro on master (LLVM 18) and it went
from timing out (>2m) down to ~0.5s.

>
> I did some analysis when patched with:
>
> --- a/src/backend/executor/execGrouping.c
> +++ b/src/backend/executor/execGrouping.c
> @@ -413,6 +413,7 @@ LookupTupleHashEntry(TupleHashTable hashtable,
> TupleTableSlot *slot,
> hashtable->cur_eq_func = hashtable->tab_eq_func;
>
> local_hash = TupleHashTableHash_internal(hashtable->hashtab,
NULL);
> + elog(NOTICE, "%u", local_hash);
> entry = LookupTupleHashEntry_internal(hashtable, slot, isnew,
> local_hash);
>
> if (hash != NULL)
>
> and diffed the output with jit=1 vs jit=0. Running your query now
> produces the same hash values with and without jit.

Should we be adding a test case for this? Basically comparing
HashAgg / multi-batch HashJoin output order with jit on/off.

Regards,
Ayush

[1] https://llvm.org/docs/LangRef.html#trunc-to-instruction
[2] https://llvm.org/docs/LangRef.html#zext-to-instruction

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Jesper St John 2026-11-17 11:35:37 Re: Timing issue in pg_basebackup
Previous Message Andrey Rachitskiy 2026-11-17 07:39:23 Re: BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails