Skip to content

Commit c8f81df

Browse files
committed
Skip eval_const_expressions when the query is such that the expression
would be evaluated only once anyway (ie, it's just a SELECT with no FROM or an INSERT ... VALUES). The planner can't do it any faster than the executor, so no point in an extra copying of the expression tree.
1 parent 03a542b commit c8f81df

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.186 2005/05/22 22:30:19 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.187 2005/05/30 01:04:44 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -385,6 +385,14 @@ subquery_planner(Query *parse, double tuple_fraction)
385385
static Node *
386386
preprocess_expression(Query *parse, Node *expr, int kind)
387387
{
388+
/*
389+
* Fall out quickly if expression is empty. This occurs often enough
390+
* to be worth checking. Note that null->null is the correct conversion
391+
* for implicit-AND result format, too.
392+
*/
393+
if (expr == NULL)
394+
return NULL;
395+
388396
/*
389397
* If the query has any join RTEs, replace join alias variables with
390398
* base-relation variables. We must do this before sublink processing,
@@ -401,8 +409,19 @@ preprocess_expression(Query *parse, Node *expr, int kind)
401409
* form. All processing of a qual expression after this point must be
402410
* careful to maintain AND/OR flatness --- that is, do not generate a tree
403411
* with AND directly under AND, nor OR directly under OR.
412+
*
413+
* Because this is a relatively expensive process, we skip it when the
414+
* query is trivial, such as "SELECT 2+2;" or "INSERT ... VALUES()".
415+
* The expression will only be evaluated once anyway, so no point in
416+
* pre-simplifying; we can't execute it any faster than the executor can,
417+
* and we will waste cycles copying the tree. Notice however that we
418+
* still must do it for quals (to get AND/OR flatness); and if we are
419+
* in a subquery we should not assume it will be done only once.
404420
*/
405-
expr = eval_const_expressions(expr);
421+
if (parse->jointree->fromlist != NIL ||
422+
kind == EXPRKIND_QUAL ||
423+
PlannerQueryLevel > 1)
424+
expr = eval_const_expressions(expr);
406425

407426
/*
408427
* If it's a qual or havingQual, canonicalize it.

0 commit comments

Comments
 (0)