- Notifications
You must be signed in to change notification settings - Fork5.2k
Commit0dca5d6
committed
Change SQL-language functions to use the plan cache.
In the historical implementation of SQL functions (if they don't getinlined), we built plans for all the contained queries at first callwithin an outer query, and then re-used those plans for the durationof the outer query, and then forgot everything. This was not ideal,not least because the plans could not be customized to specific valuesof the function's parameters. Our plancache infrastructure seemsmature enough to be used here. That will solve both the problem withnot being able to build custom plans and the problem with not beingable to share work across successive outer queries.Aside from those performance concerns, this change fixes alongstanding bugaboo with SQL functions: you could not write DDL thatwould affect later statements in the same function. That's mostlystill true with new-style SQL functions, since the results of parseanalysis are baked into the stored query trees (and protected bydependency records). But for old-style SQL functions, it will nowwork much as it does with PL/pgSQL functions, because we delay parseanalysis and planning of each query until we're ready to run it.Some edge cases that require replanning are now handled better too;see for example the new rowsecurity test, where we now detect an RLScontext change that was previously missed.One other edge-case change that might be worthy of a release noteis that we now insist that a SQL function's result be generatedby the physically-last query within it. Previously, if the lastoriginal query was deleted by a DO INSTEAD NOTHING rule, we'd bewilling to take the result from the preceding query instead.This behavior was undocumented except in source-code comments,and it seems hard to believe that anyone's relying on it.Along the way to this feature, we needed a few infrastructure changes:* The plancache can now take either a raw parse tree or ananalyzed-but-not-rewritten Query as the starting point for aCachedPlanSource. If given a Query, it is caller's responsibilitythat nothing will happen to invalidate that form of the query.We use this for new-style SQL functions, where what's in pg_proc isserialized Query(s) and we trust the dependency mechanism to disallowDDL that would break those.* The plancache now offers a way to invoke a post-rewrite callbackto examine/modify the rewritten parse tree when it is rebuildingthe parse trees after a cache invalidation. We need this becauseSQL functions sometimes adjust the parse tree to make its outputexactly match the declared result type; if the plan gets rebuilt,that has to be re-done.* There is a new backend module utils/cache/funccache.c thatabstracts the idea of caching data about a specific functionusage (a particular function and set of input data types).The code in it is moved almost verbatim from PL/pgSQL, whichhas done that for a long time. We use that logic now forSQL-language functions too, and maybe other PLs will have usefor it in the future.Author: Alexander Pyhalov <a.pyhalov@postgrespro.ru>Co-authored-by: Tom Lane <tgl@sss.pgh.pa.us>Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com>Discussion:https://postgr.es/m/8216639.NyiUUSuA9g@aivenlaptop1 parente9e7b66 commit0dca5d6
File tree
24 files changed
+2132
-989
lines changed- doc/src/sgml
- src
- backend
- catalog
- executor
- optimizer/util
- parser
- utils/cache
- include
- executor
- parser
- utils
- pl/plpgsql/src
- test
- modules/test_extensions/expected
- regress
- expected
- sql
- tools/pgindent
24 files changed
+2132
-989
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | 237 | | |
253 | 238 | | |
254 | 239 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
960 | 960 | | |
961 | 961 | | |
962 | 962 | | |
963 | | - | |
| 963 | + | |
964 | 964 | | |
965 | 965 | | |
966 | 966 | | |
| |||
0 commit comments
Comments
(0)