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 1q2C9Z-0003Ku-EU for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2023 14:41:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q2C9Y-0007Vd-Aj for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2023 14:41:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q2C9X-0007VR-TL for pgsql-hackers@lists.postgresql.org; Thu, 25 May 2023 14:41:20 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q2C9U-001pqn-Cs for pgsql-hackers@lists.postgresql.org; Thu, 25 May 2023 14:41:18 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-96fd3a658eeso112677066b.1 for ; Thu, 25 May 2023 07:41:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1685025675; x=1687617675; h=in-reply-to:references:cc:to:from:content-language:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=CnG4vq2vFSKl3wQ7CfZwihtXhPljfWFDhY2oF5r0I1A=; b=Eskk1oO/tIc4w4HL9lz1wctEAkfwDWZYfwgRbYdEjIMdLoLFDZwkbtEW6w+L1SSetr 1Tt7OevmqwWgMGtXu0K/l6y8sN/hgfzPyI+B+ocLadvWrs/h2P1txU9WlrTLgMK4mLAL XT73sUC7hyoKhBZn8hmIgndrjm8Qpgm6sCCCoJA9Mbk54jOMoQGuDgbG7wBwHefn9iy5 GRWVumKjeoghRak6hdTJI5hsIjjNAtdk3XOBR2ZgDn08tIA0Iv1h1l/In3HNk185yplH fyFGf1cHdhQVfLxcOq7OCwuC6+Hn1JwKXhT+vNG0QwZ7zYv5Nntj9H7tUaZTnHs86kGJ hZcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685025675; x=1687617675; h=in-reply-to:references:cc:to:from:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=CnG4vq2vFSKl3wQ7CfZwihtXhPljfWFDhY2oF5r0I1A=; b=QGnqljDG2CVjMKoj+5MSgbNc7m4cLUobw4UU8mR9WMYSD48z+zy4tSQzl8mg0u2xV9 Dt+cPUTDN+SJRNyWKF0QRFj5tm9sbNqTn3G9pgcbpfwSGkJI1+HaKNPus76gTRj+4ugf 9VGz9fUXkAfSghYdHTnBXjj/fUSCRkcSICWossLQfloaz42UCjLrvxc8bV6XqjbMCOSI QkokekqBo/gJrzz5B/Q6w/2tHROKYXMm+WiiVjrBhqjX21Q3yAX8IWM3UuOXP9iooKAZ 7Bidbwv2ZxFKVF6TY9GsZ6/LiNNUybxAQKywqnk2AbXeQRhJjed38ib+9LgGx+UY4LLM i9lw== X-Gm-Message-State: AC+VfDwPb10XsGlM6Ib9rx/6rAH34OTvBdAO891JYCcQV4t4/aoDoBQN lEt2YB9J48MV5lZVrkTj07KM+F3A18vnEsMJkCUIYZu3LFFoAdsAvsnQ8Ve+3AUE6XSeW0p1hMX LYyWEM4yUyGQle4QP0lL99njxb935NW8n04lHwYPKll9xBJ4o9mfKT4qKTi8+VIGZtbhIe6a5fO 6seiwNgEHpNbP4mZF2gpM/UyMKxXoxaiK5i/kP2AlRK1kol05qtRW67b6S9FIdAxEkqA== X-Google-Smtp-Source: ACHHUZ6hGwE9MYSMF85alS6i/z5oBHG3haV/u+FVocM0Lnq9CyK2QAgAo4u8EAt6wH0aJ8Voi+Cwsw== X-Received: by 2002:a17:907:628a:b0:96f:44b0:b3c0 with SMTP id nd10-20020a170907628a00b0096f44b0b3c0mr1933143ejc.56.1685025674729; Thu, 25 May 2023 07:41:14 -0700 (PDT) Received: from [10.137.0.17] (static-84-42-175-93.bb.vodafone.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id gs8-20020a170906f18800b0096f4389714csm932791ejb.55.2023.05.25.07.41.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 25 May 2023 07:41:14 -0700 (PDT) Content-Type: multipart/mixed; boundary="------------4PHEJAioxGAjMqNODFO03M1K" Message-ID: Date: Thu, 25 May 2023 16:41:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: memory leak in trigger handling (since PG12) Content-Language: en-US From: Tomas Vondra To: Andres Freund Cc: Tom Lane , PostgreSQL Hackers References: <222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com> <960113.1684859980@sss.pgh.pa.us> <20230524185559.un4dqfem3u3ktons@awork3.anarazel.de> In-Reply-To: X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------4PHEJAioxGAjMqNODFO03M1K Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 5/24/23 21:49, Tomas Vondra wrote: > > > On 5/24/23 20:55, Andres Freund wrote: >> Hi, >> >> On 2023-05-24 15:38:41 +0200, Tomas Vondra wrote: >>> I looked at this again, and I think GetPerTupleMemoryContext(estate) >>> might do the trick, see the 0002 part. >> >> Yea, that seems like the right thing here. >> >> >>> Unfortunately it's not much >>> smaller/simpler than just freeing the chunks, because we end up doing >>> >>> oldcxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); >>> updatedCols = ExecGetAllUpdatedCols(relinfo, estate); >>> MemoryContextSwitchTo(oldcxt); >> >> We could add a variant of ExecGetAllUpdatedCols that switches the context. >> > > Yeah, we could do that. I was thinking about backpatching, and modifying > ExecGetAllUpdatedCols signature would be ABI break, but adding a > variant should be fine. > >> >>> and then have to pass updatedCols elsewhere. It's tricky to just switch >>> to the context (e.g. in ExecASUpdateTriggers/ExecARUpdateTriggers), as >>> AfterTriggerSaveEvent allocates other bits of memory too (in a longer >>> lived context). >> >> Hm - on a quick look the allocations in trigger.c itself are done with >> MemoryContextAlloc(). >> >> I did find a problematic path, namely that ExecGetChildToRootMap() ends up >> building resultRelInfo->ri_ChildToRootMap in CurrentMemoryContext. >> >> That seems like a flat out bug to me - we can't just store data in a >> ResultRelInfo without ensuring the memory is lives long enough. Nearby places >> like ExecGetRootToChildMap() do make sure to switch to es_query_cxt. >> >> >> Did you see other bits of memory getting allocated in CurrentMemoryContext? >> > > No, I simply tried to do the context switch and then gave up when it > crashed on the ExecGetRootToChildMap info. I haven't looked much > further, but you may be right it's the only bit. > > It didn't occur to me it might be a bug - I think the code simply > assumes it gets called with suitable memory context, just like we do in > various other places. Maybe it should document the assumption. > >> >>> So we'd have to do another switch again. Not sure how >>> backpatch-friendly would that be. >> >> Yea, that's a valid concern. I think it might be reasonable to use something >> like ExecGetAllUpdatedColsCtx() in the backbranches, and switch to a >> short-lived context for the trigger invocations in >= 16. >> > The attached patch does this - I realized we actually have estate in ExecGetAllUpdatedCols(), so we don't even need a variant with a different signature. That makes the patch much simpler. The question is whether we need the signature anyway. There might be a caller expecting the result to be in CurrentMemoryContext (be it ExecutorState or something else), and this change might break it. But I'm not aware of any callers, so maybe that's fine. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------4PHEJAioxGAjMqNODFO03M1K Content-Type: text/x-patch; charset=UTF-8; name="0001-Use-per-tuple-context-in-ExecGetAllUpdatedCols.patch" Content-Disposition: attachment; filename*0="0001-Use-per-tuple-context-in-ExecGetAllUpdatedCols.patch" Content-Transfer-Encoding: base64 RnJvbSA5YTZlY2NlYmFlYTk2OWViNWQyZTE1ZGU5OWFhYWJiNDBiMDI0OTljIE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBUb21hcyBWb25kcmEgPHRvbWFzLnZvbmRyYUBwb3N0 Z3Jlc3FsLm9yZz4KRGF0ZTogVHVlLCAyMyBNYXkgMjAyMyAxNzo0NTo0NyArMDIwMApTdWJq ZWN0OiBbUEFUQ0hdIFVzZSBwZXItdHVwbGUgY29udGV4dCBpbiBFeGVjR2V0QWxsVXBkYXRl ZENvbHMKCi0tLQogc3JjL2JhY2tlbmQvZXhlY3V0b3IvZXhlY1V0aWxzLmMgfCAxMiArKysr KysrKysrLS0KIDEgZmlsZSBjaGFuZ2VkLCAxMCBpbnNlcnRpb25zKCspLCAyIGRlbGV0aW9u cygtKQoKZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2V4ZWN1dG9yL2V4ZWNVdGlscy5jIGIv c3JjL2JhY2tlbmQvZXhlY3V0b3IvZXhlY1V0aWxzLmMKaW5kZXggMjVhYmE5YTI0M2EuLjM3 ZjJmYTE3M2Y2IDEwMDY0NAotLS0gYS9zcmMvYmFja2VuZC9leGVjdXRvci9leGVjVXRpbHMu YworKysgYi9zcmMvYmFja2VuZC9leGVjdXRvci9leGVjVXRpbHMuYwpAQCAtMTM0OSw4ICsx MzQ5LDE2IEBAIEV4ZWNHZXRFeHRyYVVwZGF0ZWRDb2xzKFJlc3VsdFJlbEluZm8gKnJlbGlu Zm8sIEVTdGF0ZSAqZXN0YXRlKQogQml0bWFwc2V0ICoKIEV4ZWNHZXRBbGxVcGRhdGVkQ29s cyhSZXN1bHRSZWxJbmZvICpyZWxpbmZvLCBFU3RhdGUgKmVzdGF0ZSkKIHsKLQlyZXR1cm4g Ym1zX3VuaW9uKEV4ZWNHZXRVcGRhdGVkQ29scyhyZWxpbmZvLCBlc3RhdGUpLAotCQkJCQkg RXhlY0dldEV4dHJhVXBkYXRlZENvbHMocmVsaW5mbywgZXN0YXRlKSk7CisJQml0bWFwc2V0 CSAgICpyZXQ7CisJTWVtb3J5Q29udGV4dAlvbGRjeHQKKwkJPSBNZW1vcnlDb250ZXh0U3dp dGNoVG8oR2V0UGVyVHVwbGVNZW1vcnlDb250ZXh0KGVzdGF0ZSkpOworCisJcmV0ID0gYm1z X3VuaW9uKEV4ZWNHZXRVcGRhdGVkQ29scyhyZWxpbmZvLCBlc3RhdGUpLAorCQkJCQlFeGVj R2V0RXh0cmFVcGRhdGVkQ29scyhyZWxpbmZvLCBlc3RhdGUpKTsKKworCU1lbW9yeUNvbnRl eHRTd2l0Y2hUbyhvbGRjeHQpOworCisJcmV0dXJuIHJldDsKIH0KIAogLyoKLS0gCjIuNDAu MQoK --------------4PHEJAioxGAjMqNODFO03M1K--