Re: Subquery pull-up increases jointree search space

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Andrei Lepikhov <lepihov(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Subquery pull-up increases jointree search space
Date: 2026-06-08 11:04:47
Message-ID: 872f1a67-e927-4ca8-81e8-3f8b60d3af08@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 6/7/26 20:57, Tom Lane wrote:
> Tomas Vondra <tomas(at)vondra(dot)me> writes:
>> I was wondering about which places allocate most memory during planning.
>> I selected a query that allocates a manageable amount (~2.5GB) to plan,
>> and added some instrumentation to trace alloc/free calls for the
>> PortalContext (which is the context used by the planner).
>
> Thanks for doing this work, very interesting.
>
>> After classifying the allocations a bit, I see this:
>
>> allocation | size
>> --------------------------------------+----------------
>> find_mergeclauses_for_outer_pathkeys | 671 MB
>> generate_join_implied_equalities | 660 MB
>> add_paths_to_joinrel | 242 MB
>> make_inner_pathkeys_for_merge | 195 MB
>> estimate_num_groups | 92 MB
> ...
>
> I experimented with the attached trivial patch, which just avoids
> leaking sublists within find_mergeclauses_for_outer_pathkeys and
> generate_join_implied_equalities. I did not bother with adding
> any measurement infrastructure, just watched the process's virtual
> size with top(1). What I see is that HEAD consumes about 2.6GB
> and this patch gets it down to 1.8GB. So we could move the needle
> noticeably just by not wasting memory we don't have to.
>

Thanks. I've tried the fix on the original query, and I confirm the
memory usage reported by log_planner_stats drops from ~2.7GB to ~1.8GB,
so quite a bit. I assume your numbers were from the same query, so this
aligns with your numbers from 'top'.

I also tried with a larger 13-table join, using ~17GB of memory. The fix
gets it to ~13GB, so it saves ~30% in both cases (the trace log has nice
1.2TB in this case ...).

I haven't measured how this affect plan time, but I can't imagine it'd
make it worse. If anything, it'll reduce the number of page faults -
which can be quite expensive.

So +1 to do this.

Of course, if we want to consider increasing join_collapse_limit, we'd
need to reduce memory usage further. Also, I wonder which other join
features (LATERAL, partition-wise joins, ...) might use significant
amounts of memory. The queries generated by the script are rather basic.
I suppose we might have to impose somewhat stricter rules about freeing
memory during planning, etc.

regards

--
Tomas Vondra

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ilia Evdokimov 2026-06-08 11:12:20 Remove redundant DISTINCT when GROUP BY already guarantees uniqueness
Previous Message Andrey Borodin 2026-06-08 10:48:27 Re: pg_rewind does not rewind diverging timelines