reducing planner memory usage #2 - freeing rejected Material/Memoize paths

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: reducing planner memory usage #2 - freeing rejected Material/Memoize paths
Date: 2026-12-02 23:38:07
Message-ID: 8035c306-a74b-42ab-bccd-8331b53df300@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Here's the second chunk of patches reducing memory used by the planner.
The first part [1] was pretty straightforward, mostly releasing working
copies of lists in a bunch of functions. This patch is a bit more
invasive - the goal is to release memory used by Material/Memoize paths
that end up not being used.

match_unsorted_outer() creates a Memoize and Material paths for each
combination of outer/inner paths it considers, but most of the paths get
rejected in add_path(). But match_unsorted_outer() does not know that,
and it keeps the Memoize/Material paths allocated - those paths may also
contain long lists etc. Which also uses a lot of memory.

Each patch has a reproducer (but it's the same query, really), with an
EXISTS query. The query is a bit strange as it has a join in the EXISTS
clause. Again, it's intentionally extreme in order to demonstrate the
benefits, and because it partially aims to improve "worst case" queries
with increased join_collapse_limit (is sets it to 16).

The commit messages also include some basic numbers, illustrating the
impact of each patch - how much memory is saved, etc.

1) v1-0001-EXISTS-Free-unused-Material-and-Memoize-paths-in-.patch
----------

The goal is to free Material/Memoize paths in match_unsorted_outer, that
end up being unused used. To do this, we need to know whether
try_nestloop_path succeeded, and in turn whether add_path succeeded. So
the patch adjusts those functions, to provide that as return value.

2) v1-0002-EXISTS-Free-cache-key-lists-of-rejected-Memoize-p.patch
----------

This is a small improvement on top of (1), freeing not just the mpath,
but also the lists built for the mpath.

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

master 0001 0002
-----------------------------------------
repro 264979 196008 159503

That's ~40% reduction, which is not bad for two small patches. It still
uses a lot of memory, of course, but the savings combine with the other
patches shared earlier, to save ~80% of memory. I have a couple more
experimental patches, but the benefits are diminishing and it may not be
worth it.

regards

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

--
Tomas Vondra

Attachment Content-Type Size
v1-0001-EXISTS-Free-unused-Material-and-Memoize-paths-in-.patch text/x-patch 10.3 KB
v1-0002-EXISTS-Free-cache-key-lists-of-rejected-Memoize-p.patch text/x-patch 5.6 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2026-12-02 23:43:58 reducing planner memory usage #3 - partition-wise join
Previous Message Tomas Vondra 2026-12-02 23:32:58 reducing planner memory usage #1 - simpler patches