pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedAW: BUG #15923: Prepared statements take way too much memory.
5+ messages / 3 participants
[nested] [flat]
* AW: BUG #15923: Prepared statements take way too much memory.
@ 2019-07-24 22:32 Daniel Migowski <dmigowski@ikoffice.de>
2019-07-25 05:56 ` AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
0 siblings, 1 reply; 5+ messages in thread
From: Daniel Migowski @ 2019-07-24 22:32 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
I never keep more that 250 Prepared Statements. Sadly, with 10 connections to the database pooled, this is too much to handle for my poor servers.
But really... 45MB? The query has 132 nodes, and each takes up 300kb memory? Do you store the table contents in that thing? I don't want to be greedy, but might it be possible to store your Prepared Statements in a data structure that is slightly less memory hungry?
Regards and many thanks ahead,
Daniel Migowski
-----Ursprüngliche Nachricht-----
Von: Tom Lane <tgl@sss.pgh.pa.us>
Gesendet: Donnerstag, 25. Juli 2019 00:24
An: Daniel Migowski <dmigowski@ikoffice.de>
Cc: pgsql-bugs@lists.postgresql.org
Betreff: Re: BUG #15923: Prepared statements take way too much memory.
PG Bug reporting form <noreply@postgresql.org> writes:
> Prepared Statements take too much memory.
I'm not really sure what you expect us to do about this. If you PREPARE a bunch of statements and never DEALLOCATE them, then yes they're going to consume memory; maybe a lot of memory if the plans are complex.
The only thing we could do to reduce the memory consumption is to throw away the plans, which kind of defeats the purpose of a prepared statement, wouldn't you say? And even holding onto enough information to re-create the plan later would consume some amount of memory, so you will still have a problem if you keep making prepared statements and never release them.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* AW: BUG #15923: Prepared statements take way too much memory.
2019-07-24 22:32 AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
@ 2019-07-25 05:56 ` Daniel Migowski <dmigowski@ikoffice.de>
2019-07-25 22:13 ` Re: AW: BUG #15923: Prepared statements take way too much memory. Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Daniel Migowski @ 2019-07-25 05:56 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: 'pgsql-bugs@lists.postgresql.org' <pgsql-bugs@lists.postgresql.org>
I am considering trying PostgreSQL hacking to better understand the reasons of the memory consumption of Query Plans in order to be able to maybe reduce the memory used by them.
Given the current implementation and what I learned from reading about memory allocation in the backend I, in my current limited understanding of the backend, hope to find something like:
* MemoryContext's in which stuff is palloc'ed that is not really used anymore. I have read that usually you don't pfree very much assuming that the context will be free'd fully after some time anyway. In the case of PreparedStatements that will be much later than the devs of the Rewriter/Planner had in mind.
* Alternate plans that will never be used anymore but which's allocated memory is still not freed.
* References to data like table and datatype definitions that are copied into the plan but are also copied multiple times. Doesn't matter for small plans, but maybe for large ones where different subselects query the same tables. Detecting such duplicated references would involve sweeping over a plan after it has been created, placing all stuff into some kind of map and unifying the references to those definitions. The time spend for this might even speed up executing the query because less memory has to be touched when the plan is executed potentially leading to better cpu cache hit rates. This seems the most promising to me.
* Or maybe one could, if my previous assumption is true, instead of copying stuff to the plan build a dictionary of this stuff in the connection process and just link to entries in it. I assume that when the structure of datatypes changes on the server the plans have to be discarded anyway and you already know somehow which plans have to be discarded and use what structures, so there already might be such a dictionary in place? If now, it might also increase the speed of dropping stuff when adding a refcount to these structures in the dictionary, so knowing if it has been referenced somewhere.
Because you know the code better, do you think it might be worthwhile to go for these things or would it be waste of time in your eyes?
Regards,
Daniel Migowski
-----Ursprüngliche Nachricht-----
Von: Daniel Migowski
Gesendet: Donnerstag, 25. Juli 2019 00:32
An: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@lists.postgresql.org
Betreff: AW: BUG #15923: Prepared statements take way too much memory.
I never keep more that 250 Prepared Statements. Sadly, with 10 connections to the database pooled, this is too much to handle for my poor servers.
But really... 45MB? The query has 132 nodes, and each takes up 300kb memory? Do you store the table contents in that thing? I don't want to be greedy, but might it be possible to store your Prepared Statements in a data structure that is slightly less memory hungry?
Regards and many thanks ahead,
Daniel Migowski
...
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: AW: BUG #15923: Prepared statements take way too much memory.
2019-07-24 22:32 AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
2019-07-25 05:56 ` AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
@ 2019-07-25 22:13 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-07-25 22:55 ` Re: AW: BUG #15923: Prepared statements take way too much memory. Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2019-07-25 22:13 UTC (permalink / raw)
To: Daniel Migowski <dmigowski@ikoffice.de>; +Cc: 'pgsql-bugs@lists.postgresql.org' <pgsql-bugs@lists.postgresql.org>
Daniel Migowski <dmigowski@ikoffice.de> writes:
> I am considering trying PostgreSQL hacking to better understand the reasons of the memory consumption of Query Plans in order to be able to maybe reduce the memory used by them.
FWIW, I've thought for some time that we should invent a memory context
allocator that's meant for data that doesn't get realloc'd (much) after
first allocation, with lower overhead than aset.c has. Such an allocator
would be ideal for plancache.c, and perhaps other use-cases such as
plpgsql function parsetrees. IMV this would have these properties:
* Doesn't support retail pfree; to recover space you must destroy the
whole context. We could just make pfree a no-op. With the details
sketched below, repalloc would have to throw an error (because it would
not know the size of the old chunk), but I think that's OK for the
intended purpose.
* Minimum chunk header overhead, ie only the context pointer required by
the mmgr.c infrastructure. In particular, don't store the chunk size.
* Request sizes are only rounded up to a MAXALIGN boundary, not to a
power of 2.
* All else could be pretty much the same as aset.c, except we'd not need
to have any freelist management. I would imagine that callers might want
to use a smaller initial and maximum block size than is typical with
aset.c, to reduce end-of-context wastage, but that would be up to the
callers. (Possibly, instead of freelists, we'd trouble to track
end-of-block space in more than one active block, so that we avoid
wasting such space when a large allocation doesn't quite fit.)
This wouldn't take very much new code, and it would potentially offer
up to perhaps 2X space savings compared to aset.c, thanks to cutting the
per-chunk overhead. Whether that's enough to float your boat with respect
to cached plans isn't too clear, though.
A totally different idea is to make a variant version of copyObject
that is intended to produce a compact form of a node tree, and does
not create a separate palloc allocation for each node but just packs
them as tightly as it can in larger palloc chunks. This could outperform
the no-pfree-context idea because it wouldn't need even context-pointer
overhead for each node. (This relies on node trees being read-only to
whatever code is looking at them, which should be OK for finished plan
trees that are copied into the plancache; otherwise somebody might think
they could apply repalloc, GetMemoryChunkContext, etc to the nodes,
which'd crash.) The stumbling block here is that nobody is gonna
tolerate maintaining two versions of copyfuncs.c, so you'd have to
find a way for a single set of copy functions to support this output
format as well as the traditional one. (Alternatively, maybe we
could learn to autogenerate the copy functions from annotated struct
definitions; people have muttered about that for years but not done
anything.)
Or you could imagine flattening the node tree into serialized text
(cf outfuncs.c/readfuncs.c) and compressing that, although I fear that
the overhead of doing so and recovering a node tree later would be
excessive. Also, the serialized-text representation tends to be pretty
bulky, and I don't know if compression would win back enough to make it
all that much smaller than the existing in-memory representation.
> References to data like table and datatype definitions that are copied
> into the plan but are also copied multiple times. Doesn't matter for
> small plans, but maybe for large ones where different subselects query
> the same tables. Detecting such duplicated references would involve
> sweeping over a plan after it has been created, placing all stuff into
> some kind of map and unifying the references to those definitions. The
> time spend for this might even speed up executing the query because less
> memory has to be touched when the plan is executed potentially leading
> to better cpu cache hit rates. This seems the most promising to me.
TBH that sounds like a dead end to me. Detecting duplicate subtrees would
be astonishingly expensive, and I doubt that there will be enough of them
to really repay the effort in typical queries.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: AW: BUG #15923: Prepared statements take way too much memory.
2019-07-24 22:32 AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
2019-07-25 05:56 ` AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
2019-07-25 22:13 ` Re: AW: BUG #15923: Prepared statements take way too much memory. Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-07-25 22:55 ` Thomas Munro <thomas.munro@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Thomas Munro @ 2019-07-25 22:55 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Daniel Migowski <dmigowski@ikoffice.de>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
On Fri, Jul 26, 2019 at 10:13 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> FWIW, I've thought for some time that we should invent a memory context
> allocator that's meant for data that doesn't get realloc'd (much) after
> first allocation, with lower overhead than aset.c has. Such an allocator
> would be ideal for plancache.c, and perhaps other use-cases such as
> plpgsql function parsetrees. IMV this would have these properties:
>
> * Doesn't support retail pfree; to recover space you must destroy the
> whole context. We could just make pfree a no-op. With the details
> sketched below, repalloc would have to throw an error (because it would
> not know the size of the old chunk), but I think that's OK for the
> intended purpose.
Yeah, I wondered the same thing (in a discussion about shared plan
caches, which are pretty hypothetical at this stage but related to the
OP's complaint):
https://www.postgresql.org/message-id/CA%2BhUKGLOivzO5KuBFP27_jYWi%2B_8ki-Y%2BrdXXJZ4kmqF4kae%2BQ%40...
> A totally different idea is to make a variant version of copyObject
> that is intended to produce a compact form of a node tree, and does
> not create a separate palloc allocation for each node but just packs
> them as tightly as it can in larger palloc chunks. This could outperform
> the no-pfree-context idea because it wouldn't need even context-pointer
> overhead for each node. (This relies on node trees being read-only to
> whatever code is looking at them, which should be OK for finished plan
> trees that are copied into the plancache; otherwise somebody might think
> they could apply repalloc, GetMemoryChunkContext, etc to the nodes,
> which'd crash.) The stumbling block here is that nobody is gonna
> tolerate maintaining two versions of copyfuncs.c, so you'd have to
> find a way for a single set of copy functions to support this output
> format as well as the traditional one. (Alternatively, maybe we
> could learn to autogenerate the copy functions from annotated struct
> definitions; people have muttered about that for years but not done
> anything.)
Well, if you had a rip cord type allocator that just advances a
pointer through a chain of chunks and can only free all at once, you'd
get about the same layout anyway with the regular copy functions
anyway, without extra complexity. IIUC the only advantage of a
special compacting copyObject() would be that it could presumably get
precisely the right sized single piece of memory, but is that worth
the complexity of a two-phase design (IIUC you'd want recursive
tell-me-how-much-memory-you'll-need, then allocate all at once)? A
chain of smaller chunks should be nearly as good (just a small amount
of wasted space in the final chunk).
(Auto-generated read/write/copy/eq functions would be a good idea anyway.)
>> References to data like table and datatype definitions that are copied
>> into the plan but are also copied multiple times. Doesn't matter for
>> ...
> TBH that sounds like a dead end to me. Detecting duplicate subtrees would
> be astonishingly expensive, and I doubt that there will be enough of them
> to really repay the effort in typical queries.
Yeah, deduplicating common subplans may be too tricky and expensive.
I don't think it's that crazy to want to be able to share a whole
plans between backends though. Admittedly a lofty and far-off goal,
but I think we can get there eventually. Having the same set of N
explicitly prepared plans in M pooled backend sessions is fairly
common.
--
Thomas Munro
https://enterprisedb.com
^ permalink raw reply [nested|flat] 5+ messages in thread
* AW: BUG #15923: Prepared statements take way too much memory.
@ 2019-07-29 09:55 Daniel Migowski <dmigowski@ikoffice.de>
0 siblings, 0 replies; 5+ messages in thread
From: Daniel Migowski @ 2019-07-29 09:55 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andres Freund <andres@anarazel.de>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Thanks for the insightful and interesting comments.
-----Ursprüngliche Nachricht-----
Von: Thomas Munro <thomas.munro@gmail.com>
Gesendet: Montag, 29. Juli 2019 08:11
An: Daniel Migowski <dmigowski@ikoffice.de>
Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andres Freund <andres@anarazel.de>; pgsql-bugs@lists.postgresql.org
Betreff: Re: BUG #15923: Prepared statements take way too much memory.
On Sun, Jul 28, 2019 at 11:14 PM Daniel Migowski <dmigowski@ikoffice.de> wrote:
> * Why are MemoryContextAllocZeroAligned and MemoryContextAllocZero the same function?
It's not really relevant to the discussion about memory usage, but since you asked about MemoryContextAllocZeroAligned:
I noticed that stuff a while back when another project I follow was tweaking related stuff. The idea behind those functions is that we have our own specialised memset()-like macros.
MemoryContextAllocZeroAligned() uses MemSetLoop(), while
MemoryContextAllocZero() uses MemSetAligned(). Those date back nearly two decades and were apparently based on experiments done at the time.
The comments talk about avoiding function call overhead and specialising for constant lengths, so I suppose we assumed that compilers at the time didn't automatically know how to inline plain old memset() calls.
These days, you'd think you could add "assume aligned" attributes to all memory-allocation-like functions and then change a few key things to be static inline so that the constants can be seen in the right places, and get all of the same benefits (and probably more) for free from the compiler's alignment analysis and inlining powers.
As for what benefits might actually be available, for constants it's clear but for alignment, I'm doubtful. When Clang 8 on amd64 inlines a memset() with a known aligned destination (for various alignment
sizes) and constant size, it generates a bunch of movups instructions, or with -march set to a modern arch, maybe vmovups, and some variations depending on size. Note 'u' (unaligned) instructions, not 'a' (aligned): that is, it doesn't even care that I said it the destination was aligned! I found claims from a couple of Intel sources that this sort of thing stopped making any difference to performance in the Nehalem microarchitecture (2008). It still matters whether the data actually is aligned, but not whether you use the instructions that tolerate misalignment, so there is apparently no point in generating different code, and therefore, as far as memset() goes, apparently no gain from annotating allocation functions as returning aligned pointers. As for other architectures or compilers, I don't know.
For curiosity, here's an experimental patch that gets rid of the
MemSetXXX() stuff, adds some (useless?) annotations about alignment and makes palloc0() and MemoryContextAllocZero() inline so that they can benefit from inlining with a visible constant size in eg newNode(). It didn't seem to do anything very interesting apart from remove a few hundred lines of code, so I didn't get around to digging further or sharing it earlier. Or maybe I was just doing it wrong.
(In this patch it's using sizeof(long) which isn't enough to be considered aligned for the wider move instructions, but I tried various sizes when trying to trigger different codegen, without success).
--
Thomas Munro
https://enterprisedb.com
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2019-07-29 09:55 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-07-24 22:32 AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
2019-07-25 05:56 ` Daniel Migowski <dmigowski@ikoffice.de>
2019-07-25 22:13 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-07-25 22:55 ` Thomas Munro <thomas.munro@gmail.com>
2019-07-29 09:55 AW: BUG #15923: Prepared statements take way too much memory. Daniel Migowski <dmigowski@ikoffice.de>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox