Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb140c8d

Browse files
committed
Add SupportRequestInlineInFrom planner support request.
This request allows a support function to replace a function callappearing in FROM (typically a set-returning function) with anequivalent SELECT subquery. The subquery will then be subjectto the planner's usual optimizations, potentially allowing a muchbetter plan to be generated. While the planner has long done thisautomatically for simple SQL-language functions, it's now possiblefor extensions to do it for functions outside that group.Notably, this could be useful for functions that are presentlyimplemented in PL/pgSQL and work by generating and then EXECUTE'inga SQL query.Author: Paul A Jungwirth <pj@illuminatedcomputing.com>Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://postgr.es/m/09de6afa-c33d-4d94-a5cb-afc6cea0d2bb@illuminatedcomputing.com
1 parentc0bc9af commitb140c8d

File tree

9 files changed

+465
-125
lines changed

9 files changed

+465
-125
lines changed

‎doc/src/sgml/xfunc.sgml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4159,6 +4159,31 @@ supportfn(internal) returns internal
41594159
expression and an actual execution of the target function.
41604160
</para>
41614161

4162+
<para>
4163+
<literal>SupportRequestSimplify</literal> is not used
4164+
for <link linkend="queries-tablefunctions">set-returning
4165+
functions</link>. Instead, support functions can implement
4166+
the <literal>SupportRequestInlineInFrom</literal> request to expand
4167+
function calls appearing in the <literal>FROM</literal> clause of a
4168+
query. (It's also allowed to support this request for
4169+
non-set-returning functions, although
4170+
typically <literal>SupportRequestSimplify</literal> would serve as
4171+
well.) For this request type, a successful result must be
4172+
a <literal>SELECT</literal> Query tree, which will replace
4173+
the <literal>FROM</literal> item as though a sub-select had been
4174+
written instead. The Query tree must appear as it would after parse
4175+
analysis and rewrite processing. One way to ensure that that's true
4176+
is to build a SQL string then feed it
4177+
through <function>pg_parse_query</function>
4178+
and <function>pg_analyze_and_rewrite</function>, or related
4179+
functions. <literal>PARAM_EXTERN</literal> <structname>Param</structname>
4180+
nodes can appear within the Query to represent the function's
4181+
arguments; they will be replaced by the actual argument expressions.
4182+
As with <literal>SupportRequestSimplify</literal>, it is the support
4183+
function's responsibility that the replacement Query be equivalent to
4184+
normal execution of the target function.
4185+
</para>
4186+
41624187
<para>
41634188
For target functions that return <type>boolean</type>, it is often useful to estimate
41644189
the fraction of rows that will be selected by a <literal>WHERE</literal> clause using that

‎src/backend/optimizer/prep/prepjointree.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,13 +1066,15 @@ pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
10661066
/*
10671067
* preprocess_function_rtes
10681068
*Constant-simplify any FUNCTION RTEs in the FROM clause, and then
1069-
*attempt to "inline" any thatare set-returning functions.
1069+
*attempt to "inline" any thatcan be converted to simple subqueries.
10701070
*
1071-
* If an RTE_FUNCTION rtable entry invokes a set-returning function that
1071+
* If an RTE_FUNCTION rtable entry invokes a set-returningSQLfunction that
10721072
* contains just a simple SELECT, we can convert the rtable entry to an
1073-
* RTE_SUBQUERY entry exposing the SELECT directly. This is especially
1074-
* useful if the subquery can then be "pulled up" for further optimization,
1075-
* but we do it even if not, to reduce executor overhead.
1073+
* RTE_SUBQUERY entry exposing the SELECT directly. Other sorts of functions
1074+
* are also inline-able if they have a support function that can generate
1075+
* the replacement sub-Query. This is especially useful if the subquery can
1076+
* then be "pulled up" for further optimization, but we do it even if not,
1077+
* to reduce executor overhead.
10761078
*
10771079
* This has to be done before we have started to do any optimization of
10781080
* subqueries, else any such steps wouldn't get applied to subqueries
@@ -1107,7 +1109,7 @@ preprocess_function_rtes(PlannerInfo *root)
11071109
eval_const_expressions(root, (Node*)rte->functions);
11081110

11091111
/* Check safety of expansion, and expand if possible */
1110-
funcquery=inline_set_returning_function(root,rte);
1112+
funcquery=inline_function_in_from(root,rte);
11111113
if (funcquery)
11121114
{
11131115
/* Successful expansion, convert the RTE to a subquery */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp