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

Commit 9548bee

Browse files
committed
Make contain_volatile_functions/contain_mutable_functions look into SubLinks.
This change prevents us from doing inappropriate subquery flattening in cases such as dangerous functions hidden inside a sub-SELECT in the targetlist of another sub-SELECT. That could result in unexpected behavior due to multiple evaluations of a volatile function, as in a recent complaint from Etienne Dube. It's been questionable from the very beginning whether these functions should look into subqueries (as noted in their comments), and this case seems to provide proof that they should. Because the new code only descends into SubLinks, not SubPlans or InitPlans, the change only affects the planner's behavior during prepjointree processing and not later on --- for example, you can still get it to use a volatile function in an indexqual if you wrap the function in (SELECT ...). That's a historical behavior, for sure, but it's reasonable given that the executor's evaluation rules for subplans don't depend on whether there are volatile functions inside them. In any case, we need to constrain the behavioral change as narrowly as we can to make this reasonable to back-patch.
1 parent 3c24b08 commit 9548bee

File tree

3 files changed

+107
-6
lines changed

3 files changed

+107
-6
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,8 @@ contain_subplans_walker(Node *node, void *context)
822822
* mistakenly think that something like "WHERE random() < 0.5" can be treated
823823
* as a constant qualification.
824824
*
825-
* XXX we do not examine sub-selects to see if they contain uses of
826-
* mutable functions. It's not real clear if that is correct or not...
825+
* We will recursively look into Query nodes (i.e., SubLink sub-selects)
826+
* but not into SubPlans. See comments for contain_volatile_functions().
827827
*/
828828
bool
829829
contain_mutable_functions(Node *clause)
@@ -920,6 +920,13 @@ contain_mutable_functions_walker(Node *node, void *context)
920920
}
921921
/* else fall through to check args */
922922
}
923+
else if (IsA(node, Query))
924+
{
925+
/* Recurse into subselects */
926+
return query_tree_walker((Query *) node,
927+
contain_mutable_functions_walker,
928+
context, 0);
929+
}
923930
return expression_tree_walker(node, contain_mutable_functions_walker,
924931
context);
925932
}
@@ -934,11 +941,18 @@ contain_mutable_functions_walker(Node *node, void *context)
934941
* Recursively search for volatile functions within a clause.
935942
*
936943
* Returns true if any volatile function (or operator implemented by a
937-
* volatile function) is found. This test prevents invalid conversions
938-
* of volatile expressions into indexscan quals.
944+
* volatile function) is found. This test prevents, for example,
945+
* invalid conversions of volatile expressions into indexscan quals.
939946
*
940-
* XXX we do not examine sub-selects to see if they contain uses of
941-
* volatile functions. It's not real clear if that is correct or not...
947+
* We will recursively look into Query nodes (i.e., SubLink sub-selects)
948+
* but not into SubPlans. This is a bit odd, but intentional. If we are
949+
* looking at a SubLink, we are probably deciding whether a query tree
950+
* transformation is safe, and a contained sub-select should affect that;
951+
* for example, duplicating a sub-select containing a volatile function
952+
* would be bad. However, once we've got to the stage of having SubPlans,
953+
* subsequent planning need not consider volatility within those, since
954+
* the executor won't change its evaluation rules for a SubPlan based on
955+
* volatility.
942956
*/
943957
bool
944958
contain_volatile_functions(Node *clause)
@@ -1036,6 +1050,13 @@ contain_volatile_functions_walker(Node *node, void *context)
10361050
}
10371051
/* else fall through to check args */
10381052
}
1053+
else if (IsA(node, Query))
1054+
{
1055+
/* Recurse into subselects */
1056+
return query_tree_walker((Query *) node,
1057+
contain_volatile_functions_walker,
1058+
context, 0);
1059+
}
10391060
return expression_tree_walker(node, contain_volatile_functions_walker,
10401061
context);
10411062
}

src/test/regress/expected/subselect.out

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,3 +636,67 @@ where a.thousand = b.thousand
636636
----------
637637
(0 rows)
638638

639+
--
640+
-- Check that nested sub-selects are not pulled up if they contain volatiles
641+
--
642+
explain (verbose, costs off)
643+
select x, x from
644+
(select (select now()) as x from (values(1),(2)) v(y)) ss;
645+
QUERY PLAN
646+
---------------------------
647+
Values Scan on "*VALUES*"
648+
Output: $0, $1
649+
InitPlan 1 (returns $0)
650+
-> Result
651+
Output: now()
652+
InitPlan 2 (returns $1)
653+
-> Result
654+
Output: now()
655+
(8 rows)
656+
657+
explain (verbose, costs off)
658+
select x, x from
659+
(select (select random()) as x from (values(1),(2)) v(y)) ss;
660+
QUERY PLAN
661+
----------------------------------
662+
Subquery Scan on ss
663+
Output: ss.x, ss.x
664+
-> Values Scan on "*VALUES*"
665+
Output: $0
666+
InitPlan 1 (returns $0)
667+
-> Result
668+
Output: random()
669+
(7 rows)
670+
671+
explain (verbose, costs off)
672+
select x, x from
673+
(select (select now() where y=y) as x from (values(1),(2)) v(y)) ss;
674+
QUERY PLAN
675+
----------------------------------------------------------------------
676+
Values Scan on "*VALUES*"
677+
Output: (SubPlan 1), (SubPlan 2)
678+
SubPlan 1
679+
-> Result
680+
Output: now()
681+
One-Time Filter: ("*VALUES*".column1 = "*VALUES*".column1)
682+
SubPlan 2
683+
-> Result
684+
Output: now()
685+
One-Time Filter: ("*VALUES*".column1 = "*VALUES*".column1)
686+
(10 rows)
687+
688+
explain (verbose, costs off)
689+
select x, x from
690+
(select (select random() where y=y) as x from (values(1),(2)) v(y)) ss;
691+
QUERY PLAN
692+
----------------------------------------------------------------------------
693+
Subquery Scan on ss
694+
Output: ss.x, ss.x
695+
-> Values Scan on "*VALUES*"
696+
Output: (SubPlan 1)
697+
SubPlan 1
698+
-> Result
699+
Output: random()
700+
One-Time Filter: ("*VALUES*".column1 = "*VALUES*".column1)
701+
(8 rows)
702+

src/test/regress/sql/subselect.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,19 @@ where a.thousand = b.thousand
389389
and exists ( select 1 from tenk1 c where b.hundred = c.hundred
390390
and not exists ( select 1 from tenk1 d
391391
where a.thousand = d.thousand ) );
392+
393+
--
394+
-- Check that nested sub-selects are not pulled up if they contain volatiles
395+
--
396+
explain (verbose, costs off)
397+
select x, x from
398+
(select (select now()) as x from (values(1),(2)) v(y)) ss;
399+
explain (verbose, costs off)
400+
select x, x from
401+
(select (select random()) as x from (values(1),(2)) v(y)) ss;
402+
explain (verbose, costs off)
403+
select x, x from
404+
(select (select now() where y=y) as x from (values(1),(2)) v(y)) ss;
405+
explain (verbose, costs off)
406+
select x, x from
407+
(select (select random() where y=y) as x from (values(1),(2)) v(y)) ss;

0 commit comments

Comments
 (0)