On Fri, 12 Apr 2024 at 23:27, Thierry Henrio <thierry.henrio@xxxxxxxxx> wrote: > JIT: > Functions: 36 > Options: Inlining true, Optimization true, Expressions true, Deforming true > Timing: Generation 1.949 ms, Inlining 28.891 ms, Optimization 207.481 ms, Emission 134.907 ms, Total 373.228 ms > Execution Time: 429.037 ms It looks very much like the majority of the extra time is being spent doing JIT compilation. This triggers for plan A but not plan B. You can see from: > GroupAggregate (cost=401037.82..503755.82 rows=1467400 width=124) (actual time=416.851..426.534 rows=4670 loops=1) that the top-level row estimates are off. This makes the estimated cost higher than it actually is. The planner opts to have tuple deforming and expression evaluation JIT compiled to try to speed up the plan thinking it's worthwhile. It's not in this case. You can switch JIT off to try without with: SET jit=0; You might want to consider editing postgresql.conf and raising the jit_above_cost, jit_inline_above_cost and jit_optimize_above_cost values to some higher value or disable JIT completely. SELECT pg_reload_conf(); -- to reload the config file afterwards. David