reducing planner memory usage #3 - partition-wise join

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: reducing planner memory usage #3 - partition-wise join
Date: 2026-12-02 23:43:58
Message-ID: ad7279c8-5b8f-4309-be89-73ffcc938dac@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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].

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).

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.)

regards

[1]
https://www.postgresql.org/message-id/f3e94bfa-340c-4b51-9c63-1a5cb57aa27b%40vondra.me

[2]
https://www.postgresql.org/message-id/8035c306-a74b-42ab-bccd-8331b53df300%40vondra.me

--
Tomas Vondra

Attachment Content-Type Size
v1-0001-PWJ-Free-unused-Material-and-Memoize-paths-in-con.patch text/x-patch 18.6 KB
v1-0002-PWJ-Reuse-RestrictInfos-translated-for-partitionw.patch text/x-patch 31.1 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-12-02 23:47:06 Re: [PATCH] pgcrypto: Fix sha-crypt rounds= values that do not fit in int (BUG #19743)
Previous Message Tomas Vondra 2026-12-02 23:38:07 reducing planner memory usage #2 - freeing rejected Material/Memoize paths