Re: reducing planner memory usage #3 - partition-wise join

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: Tomas Vondra <tomas(at)vondra(dot)me>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: reducing planner memory usage #3 - partition-wise join
Date: 2026-12-04 11:07:13
Message-ID: CAExHW5sjrVEEL6B9eTdY7uxUz-jpeRcbBqGZQB1s5sb5D0+tVQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Dec 3, 2026 at 5:14 AM Tomas Vondra <tomas(at)vondra(dot)me> wrote:

> Hi,
>
> Here's the final part of my work on reducing memory used by the planner,
> focused on the partition-wise join. The patches are a bit more complex,
> so I've kept it intentionally short with just 2 patches. I have a couple
> more patches targeting PWJ, but the improvements are comparatively
> smaller so let's not get distracted by those for now.
>
> Each patch has a set of reproducers (in reproducers/ directory), joining
> tables with 10, 100 and 1000 partitions. The reproducers are really the
> same, I've kept copies to that the patches may be applied independently.
>
> So the patches are meant to improve the same query shape - joins of
> partitioned tables, with partition-wise join enabled. But they aim at
> different parts of the planning, wasting memory in different ways.
>
> Admittedly, the reproducers are a bit extreme (particularly the one with
> 1000 partitions). Joining 6 tables with the same partitioning scheme may
> not be particularly common. It's meant to demonstrate the memory usage,
> and I think the issue is valid and affecting even smaller cases.
>
> The commit messages also include some basic numbers, illustrating the
> impact of each patch - how much memory is saved, etc.
>
>
> 1) v1-0001-PWJ-Free-unused-Material-and-Memoize-paths-in-con.patch
> ----------
>
> The planner tends to not relese memory, relying on memory contexts to do
> the cleanup later. This applies to paths in most places - the path is
> constructed, passed to add_(partial_)path, and then left behind as it's
> not known if the path was added to the pathlist or not.
>
> That's usually OK, but with a partition-wise join the number of such
> paths explodes, as it forms joins of individual partitions. This is
> particularly visible in try_partial_nestloop_path(), which constructs
> Material/Memoize paths for the per-partition joins, and leaves them
> allocated. In the reproducer with 1000 partitions, this is be hundreds
> of megabytes worth of memory.
>
> The patch adjusts add_partial_path() to indicate whether the path was
> added or not, and uses that to free the Material/Memoize paths when
> possible.
>
> In a way, this is a partition-wise variant of the patch from [2].
>

I had proposed a much invasive approach [1] to reclaim the memory consumed
by unused paths. But this looks a much simpler way to reclaim memory from
some unused paths. +1. I haven't looked into the patch in detail.

>
>
> 2) v1-0002-PWJ-Reuse-RestrictInfos-translated-for-partitionw.patch
> ----------
>
> An rather expensive part of the partition-wise join is translating the
> RestrictInfo lists from the parent to child joins. Which happens in
> try_partitionwise_join(), and it needs to happen at some point - but the
> current code translates the same RestrictInfo multiple times when
> considering joins in different orders.
>
> Consider two joins
>
> * p1 joined to p2
> * p1 joined to (p2 JOIN p3)
>
> and a join clause "p1.k = p2.k". It'll be translated twice (for each
> partition), once for each considered join order. But the results are
> necessarily exactly the same, of course. This wastes both memory and CPU
> time.
>
> The reproducer joins the relations on the same column, which means there
> will be one large EC for all the join clauses. That probably is not a
> supper common join pattern, but it's also is not all that rare.
>
> I've seen this pattern in OLTP all the time - "base" table, and then
> tables with additional details for the entries (example: table with
> basic info about payments, and then tables with additional info about
> different payment types). That's mostly OLTP without partitioning, but
> similar schemas can be used in OLAP world too.
>
> However, this effort is partially motivated by better handling of worst
> cases, so that even rare query shapes don't risk triggering OOM. So even
> if it doesn't improve the "normal" case very much.
>
> Anyway, the patch improves this in a simple way - the translated rinfos
> are kept in a hash table, and try_partitionwise_join tries to reuse the
> already translated rinfos (with key being parent_rinfo + child relids).
>
>
This looks very similar to my [2]. That thread was later repurposed for
optimizing EC lookup. But the latest patches using similar approach as
yours can be found at [3].

Both approaches add hash table as a repository of child RestrictInfos in
PlannerInfo. Both approaches build a new child RelOptInfo if only it
doesn't exist.

The key differences are
o. My approach also handles RestrictInfos in reparameterized paths, which
may require translation through multiple levels of the partition hierarchy.
Using rinfo_serial together with the translated required_relids as the
cache key allows translations of the same condition to reuse one child
RestrictInfo, regardless of the sequence of translations or join order
through which it is reached.
o. Your key uses parent_info which takes care of producing commuted child
RestrictInfo faithfully. My patches require special handling for the same
in build_child_iclauses_multilevel(). In my patches there's possibility of
producing duplicate commuted child RestrictInfos. rinfo_serial +
required_relids will be same for the pair of commuted clauses. I remember
making commuted flag part of the key, but don't find that version easily.
o. Your patches may produce duplicate child RestrictInfos in different
iterations of GEQO. In my case all GEQO iterations use the same hash table;
we need to make sure that doesn't leave a dangling RestrictInfos across
iterations of GEQO.
o. The walker find_used_appinfos_walker() may add overhead, have you tried
to measure it?

There may be more differences. Overall, it looks like my approach covers
more cases, doesn't add overhead of a walker. Yours looks simpler. There's
a chance that it saves more memory than your 0002 alone. However, I may be
biased - it's my child. Please take a look at my patchset and we can
discuss the merits of each after that.

>
> Evaluation
> ----------
>
> Here's a brief evaluation, showing the impact of the two patches. The
> numbers are memory usage in kB, per EXPLAIN (MEMORY), for the reproducer
> (same for both patches), with 10, 100 and 1000 partitions:
>
> master 0001 0002
> ------------------------------------
> 10 27388 22993 19826
> 100 322697 273254 228827
> 1000 4200413 3710277 2823449
>
> That's ~40% reduction, which is not bad. It still uses a lot of memory,
> of course. This is not sufficient to make PWJ free, of course. But the
> patches do combine with the patches shared in the two other batches,
> with the total saving ~50-60%. (And I have a couple more experimental
> patches, that bring the total to ~80-90% reduction.)
>
>
A large part of that comes from relids bitmapsets, something which I
mentioned in [4]. Maybe a hash table of relids can be used to avoid
duplicate relids :).

[1]
https://www.postgresql.org/message-id/CAExHW5tUcVsBkq9qT%3DL5vYz4e-cwQNw%3DKAGJrtSyzOp3F%3DXacA%40mail.gmail.com
[2]
https://www.postgresql.org/message-id/CAExHW5s%3DbCLMMq8n_bN6iU%2BPjau0DS3z_6Dn6iLE69ESmsPMJQ%40mail.gmail.com
[3]
https://www.postgresql.org/message-id/CAExHW5t6NpGaif6ZO_P%2BL1cPZk27%2BYe2LxfqRVXf0OiSsW9WSg%40mail.gmail.com
[4]
https://www.postgresql.org/message-id/CAExHW5s4EqY43oB%3Dne6B2%3D-xLgrs9ZGeTr1NXwkGFt2j-OmaQQ%40mail.gmail.com

--
Best Wishes,
Ashutosh Bapat

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ashutosh Bapat 2026-12-04 11:07:40 Re: Changing shared_buffers without restart
Previous Message Nazir Bilal Yavuz 2026-12-04 10:38:45 ci: macOS jobs are cancelled due to capacity constraints