@@ -2018,11 +2018,16 @@ rowtype_field_matches(Oid rowtypeid, int fieldnum,
2018
2018
* will not be pre-evaluated here, although we will reduce their
2019
2019
* arguments as far as possible.
2020
2020
*
2021
+ * Whenever a function is eliminated from the expression by means of
2022
+ * constant-expression evaluation or inlining, we add the function to
2023
+ * root->glob->invalItems. This ensures the plan is known to depend on
2024
+ * such functions, even though they aren't referenced anymore.
2025
+ *
2021
2026
* We assume that the tree has already been type-checked and contains
2022
2027
* only operators and functions that are reasonable to try to execute.
2023
2028
*
2024
2029
* NOTE: "root" can be passed as NULL if the caller never wants to do any
2025
- * Param substitutions.
2030
+ * Param substitutions nor receive info about inlined functions .
2026
2031
*
2027
2032
* NOTE: the planner assumes that this will always flatten nested AND and
2028
2033
* OR clauses into N-argument form. See comments in prepqual.c.
@@ -4095,6 +4100,7 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
4095
4100
bool modifyTargetList ;
4096
4101
MemoryContext oldcxt ;
4097
4102
MemoryContext mycxt ;
4103
+ List * saveInvalItems ;
4098
4104
inline_error_callback_arg callback_arg ;
4099
4105
ErrorContextCallback sqlerrcontext ;
4100
4106
List * raw_parsetree_list ;
@@ -4181,6 +4187,16 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
4181
4187
ALLOCSET_DEFAULT_MAXSIZE );
4182
4188
oldcxt = MemoryContextSwitchTo (mycxt );
4183
4189
4190
+ /*
4191
+ * When we call eval_const_expressions below, it might try to add items
4192
+ * to root->glob->invalItems. Since it is running in the temp context,
4193
+ * those items will be in that context, and will need to be copied out
4194
+ * if we're successful. Temporarily reset the list so that we can keep
4195
+ * those items separate from the pre-existing list contents.
4196
+ */
4197
+ saveInvalItems = root -> glob -> invalItems ;
4198
+ root -> glob -> invalItems = NIL ;
4199
+
4184
4200
/* Fetch the function body */
4185
4201
tmp = SysCacheGetAttr (PROCOID ,
4186
4202
func_tuple ,
@@ -4307,6 +4323,10 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
4307
4323
4308
4324
querytree = copyObject (querytree );
4309
4325
4326
+ /* copy up any new invalItems, too */
4327
+ root -> glob -> invalItems = list_concat (saveInvalItems ,
4328
+ copyObject (root -> glob -> invalItems ));
4329
+
4310
4330
MemoryContextDelete (mycxt );
4311
4331
error_context_stack = sqlerrcontext .previous ;
4312
4332
ReleaseSysCache (func_tuple );
@@ -4322,6 +4342,7 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
4322
4342
/* Here if func is not inlinable: release temp memory and return NULL */
4323
4343
fail :
4324
4344
MemoryContextSwitchTo (oldcxt );
4345
+ root -> glob -> invalItems = saveInvalItems ;
4325
4346
MemoryContextDelete (mycxt );
4326
4347
error_context_stack = sqlerrcontext .previous ;
4327
4348
ReleaseSysCache (func_tuple );
0 commit comments