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 1qidKu-007HAT-4g for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2023 16:12:28 +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 1qidKs-00AkvW-R8 for pgsql-bugs@arkaria.postgresql.org; Tue, 19 Sep 2023 16:12:26 +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 1qidKs-00AkvM-IC for pgsql-bugs@lists.postgresql.org; Tue, 19 Sep 2023 16:12:26 +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 1qidKp-0068UU-OK for pgsql-bugs@lists.postgresql.org; Tue, 19 Sep 2023 16:12:25 +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 38JGCBhU376952; Tue, 19 Sep 2023 12:12:11 -0400 From: Tom Lane To: "David G. Johnston" cc: "daidewei@highgo.com" , "pgsql-bugs@lists.postgresql.org" Subject: Re: a plpgsql bug In-reply-to: References: <202309191058550158453@highgo.com> Comments: In-reply-to "David G. Johnston" message dated "Tue, 19 Sep 2023 08:24:25 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <376950.1695139931.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 19 Sep 2023 12:12:11 -0400 Message-ID: <376951.1695139931@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Mon, Sep 18, 2023 at 11:46 PM daidewei@highgo.com > wrote: >> I found a problem in plpgsql. When there is a large loop in plpgsql, >> it is found that the change of search_path will cause memory exhaustion and >> thus disconnect the connection. > It is impossible to prevent queries from exhausting memory so the fact that > this one does isn't an indication of a bug on its own. I'm having trouble > imagining a reasonable use case for this pattern of changing search_path > frequently in a loop within a single execution of a function. That said, > I'm not in a position to judge how easy or difficult an improvement in this > area may be. I poked into this a bit with valgrind. It seems that the problem is that the changes to search_path thrash the "simple expression" mechanism in plpgsql, such that it has to re-plan the various expressions in the called function each time through. It's good about tracking the actual cached plans and not leaking those, but what is getting leaked into transaction-lifespan memory is the data structures made by expr->expr_simple_state = ExecInitExprWithParams(expr->expr_simple_expr, econtext->ecxt_param_list_info); We could conceivably reclaim that data if we were willing to set up yet another per-expression memory context to hold it. That seems like rather a high overhead though. The given test case is obviously a bit artificial, but I think it may be a simplification of fairly plausible use-cases. The triggering condition is that the same textual expression in a plpgsql function gets executed repeatedly with different search_path settings, which doesn't seem that unreasonable. Perhaps another approach could be to assume that only a small number of distinct search_path settings will be used in any one transaction, and cache a separate plan and estate for each one. That would have the nice side-effect of avoiding the replanning overhead, but then we'd have to figure out how to manage the cache and keep it from blowing out. regards, tom lane