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 1tzCQs-00GZ1F-UX for pgsql-hackers@arkaria.postgresql.org; Mon, 31 Mar 2025 10:31:55 +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 1tzCQq-003jJN-Dz for pgsql-hackers@arkaria.postgresql.org; Mon, 31 Mar 2025 10:31:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tzCQp-003jJE-UK for pgsql-hackers@lists.postgresql.org; Mon, 31 Mar 2025 10:31:52 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tzCQm-0027Hp-2l for pgsql-hackers@lists.postgresql.org; Mon, 31 Mar 2025 10:31:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1743417105; bh=JreiwTjAIzrHnczxO5V1Ewq5b0XYXWJ04oiOIsfg4m0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:Message-ID:From; b=SsL1W2qG3LqX9vFBmndhh7GsVsjQas2i/KnpsoAGEpoo5Pnxj9czuQMCaHsSAEOrw 1ZoP+DsKg3F+Y4EFc9zO0amgJ0Ym7XhqPGioY0O2gxdnmRYMT2ixjm1Mw8po0SogJq +420XPGCsI2+XrtEj0CW2UWm80N+f2CPat3KCNOWVPrBju28feV/h8Q0YqB5L5/l8X QDykHw2H5TGbPZJwLRcSS+Jl+HuvyV1kxEZZCjiICZ1rro7/HGJ6HYcJDVepbjJKs0 Wr1EljvXeHevwj26Yn8lWtALjueVPvzdVcbiLzIT44TCWPeCE/iRofCRjTiynT8/+o fsPfN0iA8d3Rw== Received: from mail.postgrespro.ru (webmail-slave-mstn.l.postgrespro.ru [192.168.2.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: a.pyhalov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 2901C601E4; Mon, 31 Mar 2025 13:31:45 +0300 (MSK) MIME-Version: 1.0 Date: Mon, 31 Mar 2025 13:31:45 +0300 From: Alexander Pyhalov To: Tom Lane Cc: Pavel Stehule , Alexander Korotkov , pgsql-hackers@lists.postgresql.org, Ronan Dunklau Subject: Re: SQLFunctionCache and generic plans In-Reply-To: <3605427.1743351017@sss.pgh.pa.us> References: <8216639.NyiUUSuA9g@aivenlaptop> <3084885.1675783797@sss.pgh.pa.us> <758251661a3e8760ff52dd5c672a7505@postgrespro.ru> <30f02ca545915627905fbe3f23d93ec1@postgrespro.ru> <2275997.1737138456@sss.pgh.pa.us> <51ebc5f3b846ce279b75ac4f5e5ac33f@postgrespro.ru> <401652.1738598448@sss.pgh.pa.us> <445556.1738612687@sss.pgh.pa.us> <464677.1738620525@sss.pgh.pa.us> <1fc18af56d5fb27bf3e153ffaa2ed70d@postgrespro.ru> <2615770.1740688857@sss.pgh.pa.us> <448c63cf9b4f917c01802309b9c29eb9@postgrespro.ru> <2827970.1741890560@sss.pgh.pa.us> <337e011069b6831e063bfa346f49bb46@postgrespro.ru> <3344719.1741985546@sss.pgh.pa.us> <2691107.1743257329@sss.pgh.pa.us> <3605427.1743351017@sss.pgh.pa.us> Message-ID: <760f8088e1f0c5c08e9dab8a60f9368a@postgrespro.ru> X-Sender: a.pyhalov@postgrespro.ru Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/03/31 06:14:00 #27842604 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi. Tom Lane писал(а) 2025-03-30 19:10: > I spent some time reading and reworking this code, and have > arrived at a patch set that I'm pretty happy with. I'm not > sure it's quite committable but it's close: > > 0005: This extracts the RLS test case you had and commits it > with the old non-failing behavior, just so that we can see that > the new code does it differently. (I didn't adopt your test > from rules.sql, because AFAICS it works the same with or without > this patch set. What was the point of that one again?) The test was introduced after my error to handle case when execution_state is NULL in fcache->func_state list due to statement being completely removed by instead rule. After founding this issue, I've added a test to cover it. Not sure if it should be preserved. > > 0006: The guts of the patch. I couldn't break this down any > further. > > One big difference from what you had is that there is only one path > of control: we always use the plan cache. The hack you had to not > use it for triggers was only needed because you didn't include the > right cache key items to distinguish different trigger usages, but > the code coming from plpgsql has that right. > Yes, now it looks much more consistent. Still going through it. Will do some additional testing here. So far have checked all known corner cases and found no issues. > Also, the memory management is done a bit differently. The > "fcontext" memory context holding the SQLFunctionCache struct is > now discarded at the end of each execution of the SQL function, > which considerably alleviates worries about leaking memory there. > I invented a small "SQLFunctionLink" struct that is what fn_extra > points at, and it survives as long as the FmgrInfo does, so that's > what saves us from redoing hash key computations in most cases. I've looked through it and made some tests, including ones which caused me to create separate context for planing. Was a bit worried that it has gone, but now, as fcache->fcontext is deleted in the end of function execution, I don't see leaks, which were the initial reason for introducing it. -- Best regards, Alexander Pyhalov, Postgres Professional