Re: BUG #18172: High memory usage in tSRF function context
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Sergei Kornilov <sk@zsrv.org>
Cc: pgsql-bugs@lists.postgresql.org
Date: 2023-10-28T18:15:08Z
Lists: pgsql-bugs
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix intra-query memory leak when a SRF returns zero rows.
- 237f8765dfd9 17.0 landed
- d8d7f282fd1d 12.17 landed
- b7684473d74a 13.13 landed
- 7ab6971c657f 11.22 landed
- 5d7515d7d116 14.10 landed
- 592cb11fbee0 15.5 landed
- 07494a0df9a6 16.1 landed
-
Adjust parallel_schedule with event triggers on authenticated login
- 83510534d5f3 17.0 cited
Sergei Kornilov <sk@zsrv.org> writes:
> Thank you! The patch works for me too.
Thanks for testing! I looked at ExecProjectSet a second time
and realized that I probably still hadn't thought hard enough,
because it's also responsible for doing ResetExprContext(econtext)
and that wasn't happening either when looping around after the
SRF returns no rows. So if the SRF itself leaks some memory
in its CurrentMemoryContext and then returns nothing, we can
see bloat in that context. I failed to produce such a behavior
with jsonb_array_elements, but after trying some other things
I found a test case that still leaked even with the initial
patch:
create table zed as
select repeat('xyzzy', 10000) as f1, repeat('g', 10000) as flags
from generate_series(1,100000);
select regexp_matches(f1, 'q', flags) from zed;
The leak here stems from regexp_matches detoasting its flags
argument in the CurrentMemoryContext. A big flags argument
is surely not real-world usage, but perhaps there are other
cases that are more likely to happen.
The fix of course is just to make sure we also do ResetExprContext
when looping around. Applied to all branches.
regards, tom lane