- * First, scan the param list to discover the sets of params that are
- * available from outer query levels and my own query level. We do this
- * once to save time in the per-plan recursion steps. (This calculation
- * is overly generous: it can include a lot of params that actually
- * shouldn't be referenced here. However, valid_params is just used as
- * a debugging crosscheck, so it's not worth trying to be exact.)
+ * Examine any initPlans to determine the set of external params they
+ * reference, the set of output params they supply, and their total cost.
+ * We'll use at least some of this info below. (Note we are assuming that
+ * finalize_plan doesn't touch the initPlans.)
+ *
+ * In the case where attach_initplans is false, we are assuming that the
+ * existing initPlans are siblings that might supply params needed by the
+ * current plan.
+ */
+ initExtParam = initSetParam = NULL;
+ initplan_cost = 0;
+ foreach(l, root->init_plans)
+ {
+ SubPlan *initsubplan = (SubPlan *) lfirst(l);
+ Plan *initplan = planner_subplan_get_plan(root, initsubplan);
+ ListCell *l2;
+
+ initExtParam = bms_add_members(initExtParam, initplan->extParam);
+ foreach(l2, initsubplan->setParam)
+ {
+ initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
+ }
+ initplan_cost += get_initplan_cost(root, initsubplan);
+ }
+
+ /*
+ * Now determine the set of params that are validly referenceable in this
+ * query level; to wit, those available from outer query levels plus the
+ * output parameters of any initPlans. (We do not include output
+ * parameters of regular subplans. Those should only appear within the
+ * testexpr of SubPlan nodes, and are taken care of locally within
+ * finalize_primnode.)
+ *
+ * Note: this is a bit overly generous since some parameters of upper
+ * query levels might belong to query subtrees that don't include this
+ * query. However, valid_params is only a debugging crosscheck, so it
+ * doesn't seem worth expending lots of cycles to try to be exact.