Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q1V35-0004lm-Ph for pgsql-hackers@arkaria.postgresql.org; Tue, 23 May 2023 16:39:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q1V34-0005uT-MO for pgsql-hackers@arkaria.postgresql.org; Tue, 23 May 2023 16:39:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q1V34-0005uJ-DC for pgsql-hackers@lists.postgresql.org; Tue, 23 May 2023 16:39:46 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q1V31-001gde-9E for pgsql-hackers@lists.postgresql.org; Tue, 23 May 2023 16:39:45 +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 34NGdee1960114; Tue, 23 May 2023 12:39:40 -0400 From: Tom Lane To: Tomas Vondra cc: PostgreSQL Hackers Subject: Re: memory leak in trigger handling (since PG12) In-reply-to: <222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com> References: <222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com> Comments: In-reply-to Tomas Vondra message dated "Tue, 23 May 2023 18:23:00 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <960112.1684859980.1@sss.pgh.pa.us> Date: Tue, 23 May 2023 12:39:40 -0400 Message-ID: <960113.1684859980@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tomas Vondra writes: > it seems there's a fairly annoying memory leak in trigger code, > introduced by > ... > Attached is a patch, restoring the pre-12 behavior for me. > While looking for other places allocating stuff in ExecutorState (for > the UPDATE case) and leaving it there, I found two more cases: > 1) copy_plpgsql_datums > 2) make_expanded_record_from_tupdesc > make_expanded_record_from_exprecord > All of this is calls from plpgsql_exec_trigger. Not sure about the expanded-record case, but both of your other two fixes feel like poor substitutes for pushing the memory into a shorter-lived context. In particular I'm quite surprised that plpgsql isn't already allocating that workspace in the "procedure" memory context. > I wonder how much we should care about these cases. On the one hand we > often leave the cleanup up to the memory context, but the assumption is > the context is not unnecessarily long-lived. And ExecutorState is not > that. And leaking memory per-row does not seem great either. I agree per-row leaks in the ExecutorState context are not cool. regards, tom lane