diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index a2fb0b55a79..1c60b7b5bcf 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -1118,9 +1118,9 @@ extract_lateral_references(PlannerInfo *root, RelOptInfo *brel, Index rtindex)
 	newvars = NIL;
 	foreach(lc, vars)
 	{
-		Node	   *node = (Node *) lfirst(lc);
+		Node	   *orig_node = (Node *) lfirst(lc);
+		Node	   *node = copyObject(orig_node);
 
-		node = copyObject(node);
 		if (IsA(node, Var))
 		{
 			Var		   *var = (Var *) node;
@@ -1141,9 +1141,27 @@ extract_lateral_references(PlannerInfo *root, RelOptInfo *brel, Index rtindex)
 			 * If we pulled the PHV out of a subquery RTE, its expression
 			 * needs to be preprocessed.  subquery_planner() already did this
 			 * for level-zero PHVs in function and values RTEs, though.
+			 *
+			 * Furthermore, we modify the subquery by putting the preprocessed
+			 * expression back into the subquery's PHV, after reversing the
+			 * levelsup adjustment again.  This is rather grotty: it'd be
+			 * better if this processing didn't modify the subquery.  However,
+			 * it's essential for some optimization scenarios.  For example,
+			 * if the PHV contains a Var of our level that is deleted during
+			 * preprocessing (say, by simplifying a CASE with constant test
+			 * expression), join simplification may decide that it can remove
+			 * the rel that is the source of the Var.  If the subquery still
+			 * contains a reference to that Var, trouble will ensue.
 			 */
 			if (levelsup > 0)
+			{
+				Expr	   *repl_expr;
+
 				phv->phexpr = preprocess_phv_expression(root, phv->phexpr);
+				repl_expr = copyObject(phv->phexpr);
+				IncrementVarSublevelsUp((Node *) repl_expr, levelsup, 0);
+				((PlaceHolderVar *) orig_node)->phexpr = repl_expr;
+			}
 		}
 		else
 			Assert(false);
diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out
index db4fcc5a5a0..bebad6e4406 100644
--- a/src/test/regress/expected/join.out
+++ b/src/test/regress/expected/join.out
@@ -7043,6 +7043,35 @@ on lhs.id = rhs.id;
          ->  Result
 (5 rows)
 
+-- check handling of a removed Var that's pushed down into a subquery
+-- (fallout from the fix for bug #19560)
+explain (verbose, costs off)
+select c1, c2
+from (select case when false then remov.id end as c1
+      from int4_tbl i41 left join a remov on i41.f1 = remov.id) ss1
+     right join int4_tbl i42 on false,
+     lateral (select ss1.c1 as c2 from int4_tbl i43 offset 0) ss2;
+                  QUERY PLAN                  
+----------------------------------------------
+ Nested Loop
+   Output: (NULL::integer), ((NULL::integer))
+   ->  Nested Loop Left Join
+         Output: (NULL::integer)
+         Join Filter: false
+         ->  Seq Scan on public.int4_tbl i42
+               Output: i42.f1
+         ->  Result
+               Output: NULL::integer
+               Replaces: Scan on i41
+               One-Time Filter: false
+   ->  Memoize
+         Output: ((NULL::integer))
+         Cache Key: (NULL::integer)
+         Cache Mode: binary
+         ->  Seq Scan on public.int4_tbl i43
+               Output: (NULL::integer)
+(17 rows)
+
 -- More tests of correct placement of pseudoconstant quals
 -- simple constant-false condition
 explain (costs off)
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index 9533af8656e..088aa26c69a 100644
--- a/src/test/regress/sql/join.sql
+++ b/src/test/regress/sql/join.sql
@@ -2581,6 +2581,15 @@ full join
   ) as rhs
 on lhs.id = rhs.id;
 
+-- check handling of a removed Var that's pushed down into a subquery
+-- (fallout from the fix for bug #19560)
+explain (verbose, costs off)
+select c1, c2
+from (select case when false then remov.id end as c1
+      from int4_tbl i41 left join a remov on i41.f1 = remov.id) ss1
+     right join int4_tbl i42 on false,
+     lateral (select ss1.c1 as c2 from int4_tbl i43 offset 0) ss2;
+
 -- More tests of correct placement of pseudoconstant quals
 
 -- simple constant-false condition
