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 1t2ZUt-0054ww-Ne for pgsql-general@arkaria.postgresql.org; Sun, 20 Oct 2024 17:13:43 +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 1t2ZUq-00HIyA-N0 for pgsql-general@arkaria.postgresql.org; Sun, 20 Oct 2024 17:13:41 +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 1t2ZUq-00HIy2-Ag for pgsql-general@lists.postgresql.org; Sun, 20 Oct 2024 17:13:40 +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.94.2) (envelope-from ) id 1t2ZUj-001rzH-O0 for pgsql-general@lists.postgresql.org; Sun, 20 Oct 2024 17:13:39 +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 49KHDVah1342499; Sun, 20 Oct 2024 13:13:31 -0400 From: Tom Lane To: Michel Pelletier cc: pgsql-general Subject: Re: Using Expanded Objects other than Arrays from plpgsql In-reply-to: References: Comments: In-reply-to Michel Pelletier message dated "Sun, 20 Oct 2024 09:32:13 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1342497.1729444411.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 20 Oct 2024 13:13:31 -0400 Message-ID: <1342498.1729444411@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Michel Pelletier writes: > I found this thread from the original path implementation from Tom Lane = in > 2015: > https://www.postgresql.org/message-id/E1Ysvgz-0000s0-DP%40gemulon.postgr= esql.org >> In this initial implementation, a few heuristics have been hard-wired >> into plpgsql to improve performance for arrays that are stored in >> plpgsql variables. We would like to generalize those hacks so that >> other datatypes can obtain similar improvements, but figuring out some >> appropriate APIs is left as a task for future work. Yeah, we thought that it wouldn't be appropriate to try to design general APIs till we had more kinds of expandable objects. Maybe now is a good time to push forward on that. > My first thought was to add a flag to CREATE TYPE like "EXPANDED =3D tru= e" or > some other better name that indicates that the object can be safely take= n > ownership of in its expanded state and not copied. Isn't that inherent in the notion of R/W vs R/O expanded-object pointers? > And then there is just removing the existing restriction on arrays only. > Is any other expanded object out there really interested in being > flattened/expanded over and over again? I'm not sure. It seems certain that if the object is already expanded (either R/W or R/O), the paths for that in plpgsql_exec_function could be taken regardless of its specific type. The thing that is debatable is "if the object is in a flat state, should we forcibly expand it here?". That could be a win if the function later does object accesses that would benefit --- but it might never do so. We chose to always expand arrays, and we've gotten little pushback on that, but the tradeoffs might be different for other sorts of expanded objects. The other problem is that plpgsql only knows how to do such expansion for arrays, and it's not obvious how to extend that part. But it seems like we could get an easy win by adjusting plpgsql_exec_function along the lines of l. 549: - if (!var->isnull && var->datatype->typisarray) + if (!var->isnull) l. 564: - else + else if (var->datatype->typisarray) How far does that improve matters for you? The comment above line 549 cross-references exec_assign_value, but it looks like that's already set up to be similarly type-agnostic about values that are already expanded. regards, tom lane