From: | James Hunter <james(dot)hunter(dot)pg(at)gmail(dot)com> |
---|---|
To: | David Rowley <dgrowleyml(at)gmail(dot)com> |
Cc: | Tomas Vondra <tomas(at)vondra(dot)me>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, "Anton A(dot) Melnikov" <a(dot)melnikov(at)postgrespro(dot)ru>, Andres Freund <andres(at)anarazel(dot)de>, Andrei Lepikhov <a(dot)lepikhov(at)postgrespro(dot)ru>, Stephen Frost <sfrost(at)snowman(dot)net>, reid(dot)thompson(at)crunchydata(dot)com, Arne Roland <A(dot)Roland(at)index(dot)de>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, vignesh C <vignesh21(at)gmail(dot)com>, Justin Pryzby <pryzby(at)telsasoft(dot)com>, Ibrar Ahmed <ibrar(dot)ahmad(at)gmail(dot)com>, "stephen(dot)frost" <stephen(dot)frost(at)crunchydata(dot)com> |
Subject: | Re: Add the ability to limit the amount of memory that can be allocated to backends. |
Date: | 2024-12-31 00:34:51 |
Message-ID: | CAJVSvF6i_1Em6VPZ9po5wyTubGwifvfNFLrOYrdgT-e1GmR5Fw@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
On Mon, Dec 30, 2024 at 2:56 PM David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
>
> On Tue, 31 Dec 2024 at 10:11, James Hunter <james(dot)hunter(dot)pg(at)gmail(dot)com> wrote:
> > Does PostgreSQL currently rescan Hash Joins when they are "no longer
> > needed," to free work_mem early? If so, then I would try to reuse this
> > existing logic to decide which nodes need work_mem concurrently.
> >
> > If not, then all nodes that use work_mem actually use it
> > "concurrently," because we don't free that work_mem until we call
> > ExecutorEnd().
>
> The problem with that statement is that Hash Join isn't the only node
> type that uses work_mem. Plenty of other node types do. Have a look
> at MultiExecBitmapOr(). You can see logic there that does tbm_free()
> after the tbm_union() call. Going by that, it seems there is at least
> one place where we might free some work_mem memory before allocating
> another lot.
OK, then as a first approximation, we can assume that all nodes that
use work_mem, use it concurrently. This assumption seems to hold for
Hash Join and [Hash] Agg. PostgreSQL already considers the hash
operations "more important," in some sense, than other operators that
use work_mem -- this is why they get to multiply by
hash_mem_multiplier (defaulted to 2.0).
I think the above approximation answers Tomas's first concern. For
now, we can assume that the amount of work memory used by a query is
the sum of the individual work memories used by all of its operators.
(In the future, we can be more precise, for operators that free their
work memory as soon as they have no more rows to produce.)
Dividing backend_work_mem up and distributing it to the query's
operators would, I think, work something like the following, using a
two-phase algorithm:
1. Planner records the nbytes it estimated, for a given Path, on that
Path. However, it would continue to make its costing (and
partitioning, for parallel Hash Join) decisions, same as before, using
"work_mem [* hash_mem_multiplier]". (So "work_mem" behaves sort of
like a hint to the optimizer.)
2. At runtime, executor checks backend_work_mem. It sums up all the
nbytes fields from all the query's Paths, and compares to
backend_work_mem. And then it sets each node's work_mem field to some
fraction of the total backend_work_mem, based on nbytes and work_mem.
3. Each node then runs exactly as it does now, except instead of
checking the global work_mem [* hash_mem_multiplier] GUCs, it checks
its own work_mem field.
The "proportion" chosen in (2) should probably take into account
nbytes, work_mem, hash_mem_multiplier, etc. I think we'd want it to
overprovision work memory for nodes/paths with small nbytes, to
mitigate error in planner estimates.
James
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Paquier | 2024-12-31 00:49:43 | Re: Remove support for OpenSSL *eay32 libs on Windows |
Previous Message | Tatsuo Ishii | 2024-12-30 23:57:07 | Re: Row pattern recognition |