1515#include "access/sysattr.h"
1616#include "optimizer/restrictinfo.h"
1717#include "optimizer/var.h"
18+ #include "rewrite/rewriteManip.h"
1819#include "utils/memutils.h"
1920
2021
@@ -112,31 +113,19 @@ select_required_plans(HTAB *children_table, Oid *parts, int nparts, int *nres)
112113return result ;
113114}
114115
115- /* ReplaceVars' varnos with thevalue provided by 'parent ' */
116+ /* Replace'varno' of child's Vars with the'append_rel_rti ' */
116117static List *
117- replace_tlist_varnos (List * child_tlist , RelOptInfo * parent )
118+ replace_tlist_varnos (List * tlist , Index old_varno , Index new_varno )
118119{
119- ListCell * lc ;
120- List * result = NIL ;
121- int i = 1 ;/* resnos begin with 1 */
122-
123- foreach (lc ,child_tlist )
124- {
125- Var * var = (Var * ) ((TargetEntry * )lfirst (lc ))-> expr ;
126- Var * newvar = (Var * )palloc (sizeof (Var ));
120+ List * temp_tlist ;
127121
128- Assert (IsA (var ,Var ));
122+ AssertArg (old_varno != 0 );
123+ AssertArg (new_varno != 0 );
129124
130- * newvar = * var ;
131- newvar -> varno = parent -> relid ;
132- newvar -> varnoold = parent -> relid ;
125+ temp_tlist = copyObject (tlist );
126+ ChangeVarNodes ((Node * )temp_tlist ,old_varno ,new_varno ,0 );
133127
134- result = lappend (result ,makeTargetEntry ((Expr * )newvar ,
135- i ++ ,/* item's index */
136- NULL , false));
137- }
138-
139- return result ;
128+ return temp_tlist ;
140129}
141130
142131/* Append partition attribute in case it's not present in target list */
@@ -421,7 +410,9 @@ create_append_plan_common(PlannerInfo *root, RelOptInfo *rel,
421410
422411/* Replace rel's tlist with a matching one */
423412if (!cscan -> scan .plan .targetlist )
424- tlist = replace_tlist_varnos (child_plan -> targetlist ,rel );
413+ tlist = replace_tlist_varnos (child_plan -> targetlist ,
414+ child_rel -> relid ,
415+ rel -> relid );
425416
426417/* Add partition attribute if necessary (for ExecQual()) */
427418child_plan -> targetlist = append_part_attr_to_tlist (child_plan -> targetlist ,
@@ -431,7 +422,8 @@ create_append_plan_common(PlannerInfo *root, RelOptInfo *rel,
431422/* Now make custom_scan_tlist match child plans' targetlists */
432423if (!cscan -> custom_scan_tlist )
433424cscan -> custom_scan_tlist = replace_tlist_varnos (child_plan -> targetlist ,
434- rel );
425+ child_rel -> relid ,
426+ rel -> relid );
435427}
436428}
437429