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 1tc4hD-00FLwM-DY for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Jan 2025 15:37:11 +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 1tc4hC-007Ojv-Em for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Jan 2025 15:37:10 +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 1tc4hC-007Ojn-4c for pgsql-hackers@lists.postgresql.org; Sun, 26 Jan 2025 15:37:10 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tc4h9-001cHi-2p for pgsql-hackers@lists.postgresql.org; Sun, 26 Jan 2025 15:37:09 +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 50QFb59J931399; Sun, 26 Jan 2025 10:37:06 -0500 From: Tom Lane To: Andrey Borodin cc: Michel Pelletier , Pavel Stehule , pgsql-hackers@lists.postgresql.org Subject: Re: Using Expanded Objects other than Arrays from plpgsql In-reply-to: <0AC229FA-A3F1-43FD-B0DC-A46A73FEAFF7@yandex-team.ru> References: <1342498.1729444411@sss.pgh.pa.us> <1445998.1729482404@sss.pgh.pa.us> <2062830.1729625620@sss.pgh.pa.us> <2265411.1729699470@sss.pgh.pa.us> <2354718.1729737539@sss.pgh.pa.us> <2581216.1729794746@sss.pgh.pa.us> <1948345.1730500073@sss.pgh.pa.us> <3797606.1732045516@sss.pgh.pa.us> <647219! ! .1736019347@sss.pgh.pa.us> <1417389.1736964543@sss.pgh.pa.us> <3363452.1737483125@sss.pgh.pa.us> <0AC229FA-A3F1-43FD-B0DC-A46A73FEAFF7@yandex-team.ru> Comments: In-reply-to Andrey Borodin message dated "Sun, 26 Jan 2025 15:07:22 +0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <931397.1737905825.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 26 Jan 2025 10:37:05 -0500 Message-ID: <931398.1737905825@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andrey Borodin writes: >> On 21 Jan 2025, at 23:12, Tom Lane wrote: >> somebody will review this > I'm trying to dig into the patch set. My knowledge of the module is shal= low and I hope to improve it by reading more patches in this area. Thanks for looking! > And the coverage of newly invented mark_stmt() 42.37%. Some of branches = are easy noops, but some are not. Yeah. I'm not too concerned about that because it's pretty much a copy-and-paste of the adjacent code. Maybe we should think about some way of refactoring pl_funcs.c to reduce duplication, but I don't have any great ideas about how. > expr_is_assignment_source() is named like if it should return nool, but = it's void. I've been less than satisfied with that name too. I intended it as a statement of fact, "this expression has been found to be = the source of an assignment". But it does seem confusing. Maybe we should recast it as an action. What do you think of "mark_expr_as_assignment_source"? > I could not grasp from reading the code one generic question about new o= ptimization rule. What cost does checking for possible in-place update inc= urs to code cannot have this optimization? Is it O(numer_of_arguments) of = for every assignment execution? No, the extra effort is incurred at most once per assignment statement per session. (Unless the plpgsql function's cache entry gets invalidated, in which case we'd rebuild all of the function's data structures and have to redo this work too.) We set up the evaluation function "paramfunc" as plpgsql_param_eval_var_check if we think we might be able to apply this optimization, or plpgsql_param_eval_var_ro if we don't think so but the variable is of varlena type. At runtime, if the variable's current value is not actually expanded, then plpgsql_param_eval_var_check falls through doing essentially the same work as plpgsql_param_eval_var_ro, so there should be no added cost. The first time we observe that the value *is* expanded, we incur the cost to detect whether an optimization is really possible, and then we change the "paramfunc" pointer to be the appropriate function so as to apply the optimization or not without rechecking. So generally speaking, if we're considering a variable of a type that doesn't support expansion, there should be zero extra per-execution cost. There is some extra cost at function compilation time to determine which expressions are assignment sources (but we were doing that already) and to discover whether those assignments are to nonlocal variables (which is new work, but only needs to be done in functions with exception blocks). regards, tom lane