Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qwnq9-00ELuM-Mh for pgsql-bugs@arkaria.postgresql.org; Sat, 28 Oct 2023 18:15:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qwnq8-005Fey-7N for pgsql-bugs@arkaria.postgresql.org; Sat, 28 Oct 2023 18:15:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qwnq7-005Feq-V2 for pgsql-bugs@lists.postgresql.org; Sat, 28 Oct 2023 18:15:15 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qwnq2-003PiU-GR for pgsql-bugs@lists.postgresql.org; Sat, 28 Oct 2023 18:15:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 39SIF82e1977916; Sat, 28 Oct 2023 14:15:09 -0400 From: Tom Lane To: Sergei Kornilov cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18172: High memory usage in tSRF function context 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> Comments: In-reply-to Sergei Kornilov message dated "Sat, 28 Oct 2023 10:56:15 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1977914.1698516908.1@sss.pgh.pa.us> Date: Sat, 28 Oct 2023 14:15:08 -0400 Message-ID: <1977915.1698516908@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Sergei Kornilov 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