diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index ecdd728..c0ce3c3 100644
*** a/src/backend/optimizer/plan/planner.c
--- b/src/backend/optimizer/plan/planner.c
*************** subquery_planner(PlannerGlobal *glob, Qu
*** 777,782 ****
--- 777,803 ----
  	}
  
  	/*
+ 	 * Now that we are done preprocessing expressions, and in particular done
+ 	 * flattening JOIN alias variables, get rid of the joinaliasvars lists.
+ 	 * They no longer match what expressions in the rest of the tree look
+ 	 * like, because we have not preprocessed expressions in those lists (and
+ 	 * do not want to; for example, expanding a SubLink there would result in
+ 	 * a useless unreferenced subplan).  Leaving them in place simply creates
+ 	 * a hazard for later scans of the tree.  We could try to prevent that by
+ 	 * using QTW_IGNORE_JOINALIASES in every tree scan done after this point,
+ 	 * but that doesn't sound very reliable.
+ 	 */
+ 	if (root->hasJoinRTEs)
+ 	{
+ 		foreach(l, parse->rtable)
+ 		{
+ 			RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
+ 
+ 			rte->joinaliasvars = NIL;
+ 		}
+ 	}
+ 
+ 	/*
  	 * In some cases we may want to transfer a HAVING clause into WHERE. We
  	 * cannot do so if the HAVING clause contains aggregates (obviously) or
  	 * volatile functions (since a HAVING clause is supposed to be executed
