Обсуждение: Retrieve memory size allocated by libpq
Hello, I would like to be able to retrieve the size of memory internally allocated by libpq for a result. The reason is that we have a Ruby wrapper that exposes libpq in Ruby. The problem is that Ruby's GC doesn't know how much memory has been allocated by libpq, so no pressure is applied to the GC when it should be. With this function we could instruct the GC about the memory usage associated to each result object. This issue has already been discussed in the following thread, with the request to use custom malloc/realloc/free functions: https://www.postgresql.org/message-id/flat/20170828172834.GA71455%40TC.local#20170828172834.GA71455@TC.local Retrieving the allocated memory size is another approach to solve the same base issue. However since the relation between memory consumption and the particular result object is maintained, it can additionally be used to provide diagnostic information to each object. What do you think about adding such a function? -- Kind Regards, Lars
Вложения
Attached is an updated patch of PQresultMemsize(). The implementation is unchanged, but I added SGML documentation about this new function. I'd be pleased about comments to adding this to libpq. -- Kind Regards, Lars
Вложения
The following review has been posted through the commitfest application: make installcheck-world: tested, passed Implements feature: not tested Spec compliant: not tested Documentation: tested, passed The implementation of PQresultMemsize is simple and correct. There are no warnings, and all tests passed. I'll mark this patch as ready for committer The new status of this patch is: Ready for Committer
Pavel Stehule <pavel.stehule@gmail.com> writes: > The implementation of PQresultMemsize is simple and correct. Hm, well, only for small values of "correct". A quick look at PQclear to see just what it clears shows that PGEvent objects associated with the PGresult were overlooked. While that's not hard to fix (and I did so), there's a problem hiding behind that: we have no idea what sort of "instance data" the external event procedures may have associated with the PGresult. In principle, any space occupied by such data needs to be accounted for in this number. This isn't an issue if the event procedure takes the lazy man's approach of using PQresultAlloc to allocate such data. That's recommendable anyway since it saves having to write cleanup logic; and even if you don't do it like that, the space involved may be negligible. Still, I can foresee an eventual need to let event procedures adjust the result-size number, along the lines of PQincrementMemorySize(PGresult *res, size_t delta). But that can probably wait till somebody actually requests it. I didn't like the name "PQresultMemsize" much either, and changed it to "PQresultMemorySize", which seems more in keeping with naming and capitalization conventions elsewhere in libpq. Pushed with those and some cosmetic changes. regards, tom lane
2018-09-12 0:59 GMT+02:00 Tom Lane <tgl@sss.pgh.pa.us>:
Pavel Stehule <pavel.stehule@gmail.com> writes:
> The implementation of PQresultMemsize is simple and correct.
Hm, well, only for small values of "correct". A quick look at PQclear
to see just what it clears shows that PGEvent objects associated with the
PGresult were overlooked.
My mistake. I though so event data are independent.
While that's not hard to fix (and I did so),
there's a problem hiding behind that: we have no idea what sort of
"instance data" the external event procedures may have associated with
the PGresult. In principle, any space occupied by such data needs to
be accounted for in this number.
This isn't an issue if the event procedure takes the lazy man's approach
of using PQresultAlloc to allocate such data. That's recommendable anyway
since it saves having to write cleanup logic; and even if you don't do it
like that, the space involved may be negligible. Still, I can foresee an
eventual need to let event procedures adjust the result-size number,
along the lines of PQincrementMemorySize(PGresult *res, size_t delta).
But that can probably wait till somebody actually requests it.
I didn't like the name "PQresultMemsize" much either, and changed it to
"PQresultMemorySize", which seems more in keeping with naming and
capitalization conventions elsewhere in libpq.
Pushed with those and some cosmetic changes.
Thank you
Pavel
regards, tom lane