Re: Using Expanded Objects other than Arrays from plpgsql
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michel Pelletier <pelletier.michel@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Date: 2024-10-20T17:13:31Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Allow extension functions to participate in in-place updates.
- c366d2bdba7c 18.0 landed
-
Implement new optimization rule for updates of expanded variables.
- 6c7251db0ce1 18.0 landed
-
Detect whether plpgsql assignment targets are "local" variables.
- 36fb9ef269a0 18.0 landed
-
Preliminary refactoring of plpgsql expression construction.
- a654af21ae52 18.0 landed
-
Refactor pl_funcs.c to provide a usage-independent tree walker.
- 6a7283dd2f1c 18.0 landed
-
Generalize plpgsql's heuristic for importing expanded objects.
- 534d0ea6c2b9 18.0 landed
Michel Pelletier <pelletier.michel@gmail.com> 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.postgresql.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 = true" or > some other better name that indicates that the object can be safely taken > 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