@@ -77,7 +77,7 @@ static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_pa
7777List * tlist ,List * scan_clauses );
7878static ForeignScan * create_foreignscan_plan (PlannerInfo * root ,ForeignPath * best_path ,
7979List * tlist ,List * scan_clauses );
80- static Plan * create_customscan_plan (PlannerInfo * root ,
80+ static CustomScan * create_customscan_plan (PlannerInfo * root ,
8181CustomPath * best_path ,
8282List * tlist ,List * scan_clauses );
8383static NestLoop * create_nestloop_plan (PlannerInfo * root ,NestPath * best_path ,
@@ -86,6 +86,7 @@ static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
8686Plan * outer_plan ,Plan * inner_plan );
8787static HashJoin * create_hashjoin_plan (PlannerInfo * root ,HashPath * best_path ,
8888Plan * outer_plan ,Plan * inner_plan );
89+ static Node * replace_nestloop_params (PlannerInfo * root ,Node * expr );
8990static Node * replace_nestloop_params_mutator (Node * node ,PlannerInfo * root );
9091static void process_subquery_nestloop_params (PlannerInfo * root ,
9192List * subplan_params );
@@ -413,10 +414,10 @@ create_scan_plan(PlannerInfo *root, Path *best_path)
413414break ;
414415
415416case T_CustomScan :
416- plan = create_customscan_plan (root ,
417- (CustomPath * )best_path ,
418- tlist ,
419- scan_clauses );
417+ plan = ( Plan * ) create_customscan_plan (root ,
418+ (CustomPath * )best_path ,
419+ tlist ,
420+ scan_clauses );
420421break ;
421422
422423default :
@@ -2022,11 +2023,11 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
20222023 *
20232024 * Transform a CustomPath into a Plan.
20242025 */
2025- static Plan *
2026+ static CustomScan *
20262027create_customscan_plan (PlannerInfo * root ,CustomPath * best_path ,
20272028List * tlist ,List * scan_clauses )
20282029{
2029- Plan * plan ;
2030+ CustomScan * cplan ;
20302031RelOptInfo * rel = best_path -> path .parent ;
20312032
20322033/*
@@ -2045,23 +2046,35 @@ create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
20452046 * Invoke custom plan provider to create the Plan node represented by the
20462047 * CustomPath.
20472048 */
2048- plan = best_path -> methods -> PlanCustomPath (root ,rel ,best_path ,tlist ,
2049- scan_clauses );
2049+ cplan = (CustomScan * )best_path -> methods -> PlanCustomPath (root ,
2050+ rel ,
2051+ best_path ,
2052+ tlist ,
2053+ scan_clauses );
2054+ Assert (IsA (cplan ,CustomScan ));
20502055
20512056/*
2052- * NOTE: unlike create_foreignscan_plan(), it is the responsibility of the
2053- * custom plan provider to replace outer-relation variables with nestloop
2054- * params, because we cannot know what expression trees may be held in
2055- * private fields.
2057+ * Copy cost data from Path to Plan; no need to make custom-plan providers
2058+ * do this
20562059 */
2060+ copy_path_costsize (& cplan -> scan .plan ,& best_path -> path );
20572061
20582062/*
2059- * Copy cost data from Path to Plan; no need to make custom-plan providers
2060- * do this
2063+ * Replace any outer-relation variables with nestloop params in the qual
2064+ * and custom_exprs expressions. We do this last so that the custom-plan
2065+ * provider doesn't have to be involved. (Note that parts of custom_exprs
2066+ * could have come from join clauses, so doing this beforehand on the
2067+ * scan_clauses wouldn't work.)
20612068 */
2062- copy_path_costsize (plan ,& best_path -> path );
2069+ if (best_path -> path .param_info )
2070+ {
2071+ cplan -> scan .plan .qual = (List * )
2072+ replace_nestloop_params (root , (Node * )cplan -> scan .plan .qual );
2073+ cplan -> custom_exprs = (List * )
2074+ replace_nestloop_params (root , (Node * )cplan -> custom_exprs );
2075+ }
20632076
2064- return plan ;
2077+ return cplan ;
20652078}
20662079
20672080
@@ -2598,7 +2611,7 @@ create_hashjoin_plan(PlannerInfo *root,
25982611 * root->curOuterRels are replaced by Params, and entries are added to
25992612 * root->curOuterParams if not already present.
26002613 */
2601- Node *
2614+ static Node *
26022615replace_nestloop_params (PlannerInfo * root ,Node * expr )
26032616{
26042617/* No setup needed for tree walk, so away we go */