| From: | Tomas Vondra <tomas(at)vondra(dot)me> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | reducing planner memory usage #1 - simpler patches |
| Date: | 2026-12-02 23:32:58 |
| Message-ID: | f3e94bfa-340c-4b51-9c63-1a5cb57aa27b@vondra.me |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
Over the past couple months I've been working on various improvements to
join planning (starjoins, join hardness, ...). Back in June I've been
looking into feasibility of increasing join_collapse_limit [1], but it
turned out that's not really feasible for two reasons.
First, the exponential complexity is still there (and won't go away),
and having faster CPUs can't really help with that meaningfuly. Maybe we
could increase it from 8 to 10, but after that the exponential growth wins.
Second, planning can eat mind-boggling amounts of memory, and faster
CPUs can't really help with that. And by mind-boggling I mean a join
with 16 tables can eat 32GB of RAM (yes, it's an adversarial query, but
that's exactly why we have join_collapse_limit limit set so low).
At that point I mostly abandoned the effort to reduce memory usage, as
it was clear it won't get us to significant increases of the
join_collapse_limit anyway. And I started experimenting with modern join
planning algorithms (which are mostly alternatives to GEQO, i.e.
heuristics and not exhaustive algorithms).
But then I realized the memory consumption matters even for those
algorithms, because they work with much larger joins. They save the CPU
time, and limit the number of explored join orders, but they can still
generate a lot of different paths etc. So I revived the patches shared
in the June thread [1] and expanded it a "little" bit.
The total patch series has ~45 patches, reducing memory consumption by
up to ~90% for certain "adversarial" join queries. But most of the
savings (~80%) really comes from only ~10 patches, so I'm going to post
those. The rest is either negligible or a bit too experimental. I may
submit them later, not sure.
I'll split the patches into three separate threads - the whole series
would be too unwieldy. And there really are three separate types of
improvements (by topic / complexity).
In this thread I'm sharing patches that simply free "working" copies of
items (like list of clauses), private in a function. Our custom is to
just leave them around, and MemoryContext will eventually cleanup the
allocations. But with exponential number of long lists that's not really
working well. So this frees such allocations in a couple places.
There's 10 patches, each with a reproducer included. But really, there
are just 3 query shapes - EXISTS, STAR2 and ECCHAIN - targeted by the
patches (there's some overlap of the benefits, ofc). And the reproducers
are effectively the same for "same shape" patches.
- v1-0001-STAR2-Free-mergejoin-clause-and-pathkey-lists-of-.patch
- v1-0002-STAR2-Avoid-leaking-temporary-lists-during-join-p.patch
- v1-0003-EXISTS-Free-temporary-lists-in-estimate_num_group.patch
- v1-0004-STAR2-Free-member-lists-in-generate_join_implied_.patch
- v1-0005-EXISTS-Free-GroupVarInfos-in-estimate_num_groups.patch
- v1-0006-STAR2-Free-hashclause-lists-of-rejected-hashjoin-.patch
- v1-0007-EXISTS-Free-the-attribute-set-built-by-examine_si.patch
- v1-0008-STAR2-Free-temporary-allocations-in-build_joinrel.patch
- v1-0009-ECCHAIN-Free-outerkeys-and-merge_pathkeys-in-sort.patch
- v1-0010-EXISTS-Free-temporary-lists-in-get_joinrel_paramp.patch
The effect of the patches (on a 14-table join) looks like this (the
numbers are memory usage in kB):
ECCHAIN EXIST STAR2
-----------------------------------------------
master 106561 264979 190919
0001 81341 242900 126278
0002 68407 229137 76356
0003 61207 192653 76330
0004 56158 180489 59685
0005 53998 169543 59670
0006 49670 166023 51351
0007 48213 158721 51333
0008 45096 156033 45509
0009 40722 156033 45509
0010 40722 151873 45509
So that's ~50-60% reduction. It helps a bit with planning time too, but
that wasn't the aim here (except for not getting slower).
Earlier I said the patches are merely freeing local "private" copies of
lists, and that's true for 8 out of the 10 patches. But there are two
patches that do something a little bit different:
- v1-0001-STAR2-Free-mergejoin-clause-and-pathkey-lists-of-.patch
- v1-0006-STAR2-Free-hashclause-lists-of-rejected-hashjoin-.patch
These patches are freeing lists from *rejected paths* (mergejoin and
hashjoin). The patches do that in add_path() but that seems a bit dirty.
It relies on the fact that the path can't be reference by anything, and
while I think that's true, maybe it isn't? There might be a better way
to do this - e.g. by making add_path() to return false when the new path
gets rejected. And do the freeing in the caller. But it seems to work
well enought to demonstrate the benefits.
regards
[1]
https://www.postgresql.org/message-id/c81b64db-0e13-4337-9edf-65a5ef422873@vondra.me
--
Tomas Vondra
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-STAR2-Free-mergejoin-clause-and-pathkey-lists-of-.patch | text/x-patch | 19.7 KB |
| v1-0002-STAR2-Avoid-leaking-temporary-lists-during-join-p.patch | text/x-patch | 7.1 KB |
| v1-0003-EXISTS-Free-temporary-lists-in-estimate_num_group.patch | text/x-patch | 6.3 KB |
| v1-0004-STAR2-Free-member-lists-in-generate_join_implied_.patch | text/x-patch | 5.7 KB |
| v1-0005-EXISTS-Free-GroupVarInfos-in-estimate_num_groups.patch | text/x-patch | 6.8 KB |
| v1-0006-STAR2-Free-hashclause-lists-of-rejected-hashjoin-.patch | text/x-patch | 8.6 KB |
| v1-0007-EXISTS-Free-the-attribute-set-built-by-examine_si.patch | text/x-patch | 6.1 KB |
| v1-0008-STAR2-Free-temporary-allocations-in-build_joinrel.patch | text/x-patch | 6.1 KB |
| v1-0009-ECCHAIN-Free-outerkeys-and-merge_pathkeys-in-sort.patch | text/x-patch | 5.1 KB |
| v1-0010-EXISTS-Free-temporary-lists-in-get_joinrel_paramp.patch | text/x-patch | 6.5 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tomas Vondra | 2026-12-02 23:38:07 | reducing planner memory usage #2 - freeing rejected Material/Memoize paths |
| Previous Message | Zsolt Parragi | 2026-12-02 23:01:55 | Re: executor startup doesn't skip sub-partitions of a pruned partition |