mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
Allow user control of CTE materialization, and change the default behavior.
Historically we've always materialized the full output of a CTE query, treating WITH as an optimization fence (so that, for example, restrictions from the outer query cannot be pushed into it). This is appropriate when the CTE query is INSERT/UPDATE/DELETE, or is recursive; but when the CTE query is non-recursive and side-effect-free, there's no hazard of changing the query results by pushing restrictions down. Another argument for materialization is that it can avoid duplicate computation of an expensive WITH query --- but that only applies if the WITH query is called more than once in the outer query. Even then it could still be a net loss, if each call has restrictions that would allow just a small part of the WITH query to be computed. Hence, let's change the behavior for WITH queries that are non-recursive and side-effect-free. By default, we will inline them into the outer query (removing the optimization fence) if they are called just once. If they are called more than once, we will keep the old behavior by default, but the user can override this and force inlining by specifying NOT MATERIALIZED. Lastly, the user can force the old behavior by specifying MATERIALIZED; this would mainly be useful when the query had deliberately been employing WITH as an optimization fence to prevent a poor choice of plan. Andreas Karlsson, Andrew Gierth, David Fetter Discussion: https://postgr.es/m/87sh48ffhb.fsf@news-spur.riddles.org.uk
This commit is contained in:
@ -479,7 +479,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
|
||||
%type <list> row explicit_row implicit_row type_list array_expr_list
|
||||
%type <node> case_expr case_arg when_clause case_default
|
||||
%type <list> when_clause_list
|
||||
%type <ival> sub_type
|
||||
%type <ival> sub_type opt_materialized
|
||||
%type <value> NumericOnly
|
||||
%type <list> NumericOnly_list
|
||||
%type <alias> alias_clause opt_alias_clause
|
||||
@ -11344,17 +11344,24 @@ cte_list:
|
||||
| cte_list ',' common_table_expr { $$ = lappend($1, $3); }
|
||||
;
|
||||
|
||||
common_table_expr: name opt_name_list AS '(' PreparableStmt ')'
|
||||
common_table_expr: name opt_name_list AS opt_materialized '(' PreparableStmt ')'
|
||||
{
|
||||
CommonTableExpr *n = makeNode(CommonTableExpr);
|
||||
n->ctename = $1;
|
||||
n->aliascolnames = $2;
|
||||
n->ctequery = $5;
|
||||
n->ctematerialized = $4;
|
||||
n->ctequery = $6;
|
||||
n->location = @1;
|
||||
$$ = (Node *) n;
|
||||
}
|
||||
;
|
||||
|
||||
opt_materialized:
|
||||
MATERIALIZED { $$ = CTEMaterializeAlways; }
|
||||
| NOT MATERIALIZED { $$ = CTEMaterializeNever; }
|
||||
| /*EMPTY*/ { $$ = CTEMaterializeDefault; }
|
||||
;
|
||||
|
||||
opt_with_clause:
|
||||
with_clause { $$ = $1; }
|
||||
| /*EMPTY*/ { $$ = NULL; }
|
||||
@ -16237,6 +16244,7 @@ makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
|
||||
/* create common table expression */
|
||||
cte->ctename = relname;
|
||||
cte->aliascolnames = aliases;
|
||||
cte->ctematerialized = CTEMaterializeDefault;
|
||||
cte->ctequery = query;
|
||||
cte->location = -1;
|
||||
|
||||
|
Reference in New Issue
Block a user