pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Sergei Kornilov <sk@zsrv.org>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18172: High memory usage in tSRF function context
Date: Sat, 28 Oct 2023 14:15:08 -0400
Message-ID: <1977915.1698516908@sss.pgh.pa.us> (raw)
In-Reply-To: <223611698479775@omv3tj5zwvd3plqc.iva.yp-c.yandex.net>
References: <18172-9b8c5fc1d676ded3@postgresql.org>
	<1736370.1698463142@sss.pgh.pa.us>
	<223611698479775@omv3tj5zwvd3plqc.iva.yp-c.yandex.net>

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





view thread (5+ messages)  latest in thread

Message-ID: <1977915.1698516908@sss.pgh.pa.us>
Permalink:  ../1977915.1698516908@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1977915.1698516908@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, sk@zsrv.org, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #18172: High memory usage in tSRF function context
  In-Reply-To: <1977915.1698516908@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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