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 1x0iH7-004m6i-1B for pgsql-hackers@arkaria.postgresql.org; Sun, 30 Aug 2026 16:20:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x0iH5-00Dldp-0M for pgsql-hackers@arkaria.postgresql.org; Sun, 30 Aug 2026 16:20:51 +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 1x0iH4-00Dldh-2F for pgsql-hackers@lists.postgresql.org; Sun, 30 Aug 2026 16:20:50 +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 1x0iH1-00000003Acq-3V1e for pgsql-hackers@lists.postgresql.org; Sun, 30 Aug 2026 16:20:49 +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 67UGKfUg495450; Sun, 30 Aug 2026 12:20:41 -0400 From: Tom Lane To: Richard Guo cc: Tender Wang , Thom Brown , Jacob Brazeal , pgsql-hackers@lists.postgresql.org Subject: Re: remove_useless_joins vs. bug #19560 In-reply-to: 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> <250168.1788034245@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Sun, 30 Aug 2026 15:50:20 +0900" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <495440.1788106828.0@sss.pgh.pa.us> Date: Sun, 30 Aug 2026 12:20:41 -0400 Message-ID: <495449.1788106841@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: <495440.1788106828.1@sss.pgh.pa.us> Richard Guo writes: > ... The exception is > flatten_join_alias_vars, which descends into every PHV and asserts on > SubPlans. I changed it to leave PHVs of upper levels alone; such a > PHV cannot contain join aliases of the level being flattened anyway. BTW, this comment reminds me strongly of an earlier draft I had, which eliminated the assertion failure by just not letting ChangeVarNodes recurse into outer-level PHVs, on the grounds that we can ignore their contents until they get replaced by Params. That didn't seem terribly principled on its own, so I also made eval_const_expressions not do anything to outer-level PHVs, but then I started to wonder just how far the implications should extend. When I realized that a localized fix in extract_lateral_references() seemed possible I gave up on that approach. But maybe it's the way to go. It has the attractive property of removing useless work. Attached is as far as I got with the idea before switching to the other way. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="skip-outer-level-PHVs.patch"; charset="us-ascii" Content-ID: <495440.1788106828.2@sss.pgh.pa.us> Content-Description: skip-outer-level-PHVs.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/= util/clauses.c index 8da4ed617b5..2e7e3ccf45b 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1703,23 +1703,30 @@ find_nonnullable_rels_walker(Node *node, bool top_= level) PlaceHolderVar *phv =3D (PlaceHolderVar *) node; = /* - * If the contained expression forces any rels non-nullable, so does - * the PHV. + * We can ignore outer-level PHVs: they cannot contain any Vars of the + * current level, so they can't force anything non-nullable. */ - result =3D find_nonnullable_rels_walker((Node *) phv->phexpr, top_level= ); + if (phv->phlevelsup =3D=3D 0) + { + /* + * If the contained expression forces any rels non-nullable, so + * does the PHV. + */ + result =3D find_nonnullable_rels_walker((Node *) phv->phexpr, + top_level); = - /* - * If the PHV's syntactic scope is exactly one rel, it will be forced - * to be evaluated at that rel, and so it will behave like a Var of - * that rel: if the rel's entire output goes to null, so will the PHV. - * (If the syntactic scope is a join, we know that the PHV will go to - * null if the whole join does; but that is AND semantics while we - * need OR semantics for find_nonnullable_rels' result, so we can't do - * anything with the knowledge.) - */ - if (phv->phlevelsup =3D=3D 0 && - bms_membership(phv->phrels) =3D=3D BMS_SINGLETON) - result =3D bms_add_members(result, phv->phrels); + /* + * If the PHV's syntactic scope is exactly one rel, it will be + * forced to be evaluated at that rel, and so it will behave like + * a Var of that rel: if the rel's entire output goes to null, so + * will the PHV. (If the syntactic scope is a join, we know that + * the PHV will go to null if the whole join does; but that is AND + * semantics while we need OR semantics for find_nonnullable_rels' + * result, so we can't do anything with the knowledge.) + */ + if (bms_membership(phv->phrels) =3D=3D BMS_SINGLETON) + result =3D bms_add_members(result, phv->phrels); + } } return result; } @@ -1954,7 +1961,13 @@ find_nonnullable_vars_walker(Node *node, bool top_l= evel) { PlaceHolderVar *phv =3D (PlaceHolderVar *) node; = - result =3D find_nonnullable_vars_walker((Node *) phv->phexpr, top_level= ); + /* + * We can ignore outer-level PHVs: they cannot contain any Vars of the + * current level, so they can't force anything non-nullable. + */ + if (phv->phlevelsup =3D=3D 0) + result =3D find_nonnullable_vars_walker((Node *) phv->phexpr, + top_level); } return result; } @@ -4150,20 +4163,31 @@ eval_const_expressions_mutator(Node *node, return (Node *) newcdomain; } case T_PlaceHolderVar: - - /* - * In estimation mode, just strip the PlaceHolderVar node - * altogether; this amounts to estimating that the contained value - * won't be forced to null by an outer join. In regular mode we - * just use the default behavior (ie, simplify the expression but - * leave the PlaceHolderVar node intact). - */ - if (context->estimate) { PlaceHolderVar *phv =3D (PlaceHolderVar *) node; = - return eval_const_expressions_mutator((Node *) phv->phexpr, - context); + /* + * If it's an outer-level PHV, we should return it unmodified. + * Descending into the contents is risky because there may be + * since-deleted Vars there. In any case the current query + * level is not going to do anything with the PHV except + * replace it with a Param (cf. SS_replace_correlation_vars), + * so the effort would be wasted. But we do copy the subtree, + * just to conform to this function's API spec. + */ + if (phv->phlevelsup > 0) + return copyObject(node); + + /* + * In estimation mode, just strip the PlaceHolderVar node + * altogether; this amounts to estimating that the contained + * value won't be forced to null by an outer join. In regular + * mode we just use the default behavior (ie, simplify the + * expression but leave the PlaceHolderVar node intact). + */ + if (context->estimate) + return eval_const_expressions_mutator((Node *) phv->phexpr, + context); } break; case T_ConvertRowtypeExpr: diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewr= iteManip.c index 3653f00d383..90a8db11e92 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -627,7 +627,18 @@ ChangeVarNodes_walker(Node *node, ChangeVarNodes_cont= ext *context) context->rt_index, context->new_index); } - /* fall through to examine children */ + + /* + * If it's an outer-level PHV, do not recurse into its contents. Such + * a PHV must have been pushed into this query level without + * expression preprocessing, so it may contain Vars that we think are + * due to be deleted, triggering our INVALID_VAR assertions. There is + * no need to modify such a PHV anyway, since it will be replaced by a + * Param before anything very interesting happens. + */ + if (phv->phlevelsup > 0) + return node; + /* else fall through to examine children */ } if (IsA(node, PlanRowMark)) { ------- =_aaaaaaaaaa0--