diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 3e8467995634e607efe91e34692c7c24187be1cb..8ceea820bdc4eb2e6c737c2f98eb1f0bf6fa034b 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -4382,6 +4382,17 @@ examine_simple_variable(PlannerInfo *root, Var *var,
 		/* Subquery should have been planned already */
 		Assert(rel->subroot && IsA(rel->subroot, PlannerInfo));
 
+		/*
+		 * Switch our attention to the subquery as mangled by the planner.
+		 * It was okay to look at the pre-planning version for the tests
+		 * above, but now we need a Var that will refer to the subroot's
+		 * live RelOptInfos.  For instance, if any subquery pullup happened
+		 * during planning, Vars in the targetlist might have gotten replaced,
+		 * and we need to see the replacement expressions.
+		 */
+		subquery = rel->subroot->parse;
+		Assert(IsA(subquery, Query));
+
 		/* Get the subquery output expression referenced by the upper Var */
 		ste = get_tle_by_resno(subquery->targetList, var->varattno);
 		if (ste == NULL || ste->resjunk)