Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 455a55f

Browse files
committed
Tighten inline_function's test for overly complex parameters. This
should catch most situations where repeated inlining blows up the expression complexity unreasonably, as in Joe Conway's recent example.
1 parent c1c7b33 commit 455a55f

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/backend/optimizer/util/clauses.c

+28-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.148 2003/07/28 18:33:18 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.149 2003/08/03 23:46:37 tgl Exp $
1212
*
1313
* HISTORY
1414
* AUTHOR DATE MAJOR EVENT
@@ -26,6 +26,7 @@
2626
#include "miscadmin.h"
2727
#include "nodes/makefuncs.h"
2828
#include "optimizer/clauses.h"
29+
#include "optimizer/cost.h"
2930
#include "optimizer/planmain.h"
3031
#include "optimizer/var.h"
3132
#include "parser/analyze.h"
@@ -1710,8 +1711,12 @@ evaluate_function(Oid funcid, Oid result_type, List *args,
17101711
* so we keep track of which functions we are already expanding and
17111712
* do not re-expand them. Also, if a parameter is used more than once
17121713
* in the SQL-function body, we require it not to contain any volatile
1713-
* functions or sublinks --- volatiles might deliver inconsistent answers,
1714-
* and subplans might be unreasonably expensive to evaluate multiple times.
1714+
* functions (volatiles might deliver inconsistent answers) nor to be
1715+
* unreasonably expensive to evaluate. The expensiveness check not only
1716+
* prevents us from doing multiple evaluations of an expensive parameter
1717+
* at runtime, but is a safety value to limit growth of an expression due
1718+
* to repeated inlining.
1719+
*
17151720
* We must also beware of changing the volatility or strictness status of
17161721
* functions by inlining them.
17171722
*
@@ -1912,9 +1917,26 @@ inline_function(Oid funcid, Oid result_type, List *args,
19121917
}
19131918
else if (usecounts[i] != 1)
19141919
{
1915-
/* Param used multiple times: uncool if volatile or expensive */
1916-
if (contain_volatile_functions(param) ||
1917-
contain_subplans(param))
1920+
/* Param used multiple times: uncool if expensive or volatile */
1921+
QualCost eval_cost;
1922+
1923+
/*
1924+
* We define "expensive" as "contains any subplan or more than
1925+
* 10 operators". Note that the subplan search has to be done
1926+
* explicitly, since cost_qual_eval() will barf on unplanned
1927+
* subselects.
1928+
*/
1929+
if (contain_subplans(param))
1930+
goto fail;
1931+
cost_qual_eval(&eval_cost, makeList1(param));
1932+
if (eval_cost.startup + eval_cost.per_tuple >
1933+
10 * cpu_operator_cost)
1934+
goto fail;
1935+
/*
1936+
* Check volatility last since this is more expensive than the
1937+
* above tests
1938+
*/
1939+
if (contain_volatile_functions(param))
19181940
goto fail;
19191941
}
19201942
i++;

0 commit comments

Comments
 (0)