Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x0POE-004cb7-2B for pgsql-hackers@arkaria.postgresql.org; Sat, 29 Aug 2026 20:10:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x0POD-00C2Ay-1V for pgsql-hackers@arkaria.postgresql.org; Sat, 29 Aug 2026 20:10:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x0POD-00C2Aq-08 for pgsql-hackers@lists.postgresql.org; Sat, 29 Aug 2026 20:10:57 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x0POA-000000033Ks-3x0k for pgsql-hackers@lists.postgresql.org; Sat, 29 Aug 2026 20:10:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 67TKAjo0250169; Sat, 29 Aug 2026 16:10:45 -0400 From: Tom Lane To: Tender Wang cc: Richard Guo , Thom Brown , Jacob Brazeal , pgsql-hackers@lists.postgresql.org Subject: Re: remove_useless_joins vs. bug #19560 In-reply-to: <1708836.1787974044@sss.pgh.pa.us> References: <1186816.1784573544@sss.pgh.pa.us> <2727652.1784997994@sss.pgh.pa.us> <2746010.1785014587@sss.pgh.pa.us> <2933135.1785032128@sss.pgh.pa.us> <3304318.1785087206@sss.pgh.pa.us> <3304418.1785087314@sss.pgh.pa.us> <3968565.1785170019@sss.pgh.pa.us> <3975112.1785176472@sss.pgh.pa.us> <670538.1786924698@sss.pgh.pa.us> <839346.1787765112@sss.pgh.pa.us> <954541.1787846010@sss.pgh.pa.us> <1147476.1787924590@sss.pgh.pa.us> <1708836.1787974044@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 28 Aug 2026 23:27:24 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <250153.1788034230.0@sss.pgh.pa.us> Date: Sat, 29 Aug 2026 16:10:45 -0400 Message-ID: <250168.1788034245@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <250153.1788034230.1@sss.pgh.pa.us> I wrote: > What's apparently happening is that we pull up the sub-select, > and eval_const_expressions flattens the CASE to constant-NULL, > so we don't see the reference to sample_1.a and conclude that > sample_1 can be dropped from the query. But when we do that, > we try to mutate the original copy of the sub-select's tlist > which still contains sample_1.a, and so we hit the Assert > saying we should no longer see any such Vars. More specifically, the raw CASE expression gets pushed down into the lateral subquery, wrapped in a PlaceHolderVar. When we pull it back up in extract_lateral_references(), we apply expression preprocessing which flattens the CASE to constant-NULL. So the lateral reference expression seen by the upper query has no reference to sample_1, allowing join removal to remove sample_1. But the PHV inside the subquery still has a reference, which makes ChangeVarNodes fail, and even if we prevented that it'd still be trouble when we repeat extract_lateral_references() after restarting. So after contemplating my navel for awhile, the least grotty solution I can see is to update the subquery's copy of the PHV with the preprocessed expression, as attached. I don't really love doing that, but the alternatives I can think of are worse. Anyone have another idea? One interesting point about this query is that if you remove the topmost right join ("RIGHT JOIN public.ruletest_tbl2 AS ref_3 ON NULL" in the original example, "right join int4_tbl i42 on false" in my proposed regression test), there's no failure. That's not so surprising, because without that join we don't have to wrap the CASE in a PlaceHolderVar, and then there's no discrepancy between what the subquery contains and what the outer level thinks it contains. But what is surprising is that we then fail to make the join removal, which seems like a missed optimization. I didn't poke into that, but I think it'd be worth figuring out why the removal doesn't happen. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="put-back-preprocessed-PHV.patch"; charset="us-ascii" Content-ID: <250153.1788034230.2@sss.pgh.pa.us> Content-Description: put-back-preprocessed-PHV.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimize= r/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, RelOpt= Info *brel, Index rtindex) newvars =3D NIL; foreach(lc, vars) { - Node *node =3D (Node *) lfirst(lc); + Node *orig_node =3D (Node *) lfirst(lc); + Node *node =3D copyObject(orig_node); = - node =3D copyObject(node); if (IsA(node, Var)) { Var *var =3D (Var *) node; @@ -1141,9 +1141,27 @@ extract_lateral_references(PlannerInfo *root, RelOp= tInfo *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 =3D preprocess_phv_expression(root, phv->phexpr); + repl_expr =3D copyObject(phv->phexpr); + IncrementVarSublevelsUp((Node *) repl_expr, levelsup, 0); + ((PlaceHolderVar *) orig_node)->phexpr =3D repl_expr; + } } else Assert(false); diff --git a/src/test/regress/expected/join.out b/src/test/regress/expecte= d/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 =3D 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 =3D 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 =3D 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 =3D 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 ------- =_aaaaaaaaaa0--