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 1woOH1-0011eT-29 for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Jul 2026 16:33:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1woOH0-00E3up-29 for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Jul 2026 16:33:50 +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 1woOH0-00E3uh-1E for pgsql-hackers@lists.postgresql.org; Mon, 27 Jul 2026 16:33: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 1woOGy-00000000bih-201u for pgsql-hackers@lists.postgresql.org; Mon, 27 Jul 2026 16:33: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 66RGXdQh3968566; Mon, 27 Jul 2026 12:33:39 -0400 From: Tom Lane To: Richard Guo cc: Thom Brown , 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> Comments: In-reply-to Richard Guo message dated "Mon, 27 Jul 2026 16:07:56 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3968564.1785170019.1@sss.pgh.pa.us> Date: Mon, 27 Jul 2026 12:33:39 -0400 Message-ID: <3968565.1785170019@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > Thanks for the patchset. Nice cleanup: Thanks for looking at it! > I noticed that the call to remove_rels_from_query_tree() at the end of > remove_useless_joins() performs a full traversal over the query tree > to delete the removed relids. I wonder if this is a bit too > brute-force, as AFAICS only PHVs can still contain references to the > removed relids. If there were any other remaining references, > join_is_removable() wouldn't have considered the join removable in the > first place. Yeah, I think this is probably true. The Claude-written comment claims that the removed OJ's relid could appear in surviving Vars' nullingrels, but I don't see how that could be: such Vars would have to refer to the removed baserel, and then they should have prevented the join removal. > If that's correct, perhaps we can simplify > remove_rels_from_query_tree() to something lighter, similar to > remove_result_refs(). Meh. I don't think there's a lot to be gained there: we still have to walk the whole tree, and I'm not excited about duplicating tree-walking code. We could maybe use substitute_phv_relids from prepjointree.c, but I see that has Assert(!IsA(node, AppendRelInfo)); which we'd have to drop. I doubt there'd be any measurable improvement over the ChangeVarNodes coding, anyway. > Also, the issue with duplicate clauses after self-join elimination > reported in bug #19435 is still present in v4. It would be great if > we could address that in passing as well. I'm not excited about that either. AFAICS, with the #19435 query: SELECT 1 AS c1 FROM ( pg_table_a AS tom0 RIGHT JOIN ( (pg_table_a AS tom1 NATURAL JOIN pg_table_a AS tom2) RIGHT JOIN pg_table_a AS tom3 ON tom1.col_bool IS NOT NULL ) ON tom1.col_bool ); The NATURAL JOIN produces quals tom1.id = tom2.id AND tom1.col_bool = tom2.col_bool SJE correctly replaces those with tom2.id IS NOT NULL AND tom2.col_bool IS NOT NULL We do, later on, detect that the id clause is unnecessary because of the id column's NOT NULL constraint. But we don't notice that the generated col_bool clause is redundant with the user-written "tom1.col_bool IS NOT NULL" clause appearing in a higher join level. I don't think it's incumbent on us to fix redundantly-written user queries, and even if we took that on, the SJE code would not be the place for it. You can get similar redundancies without any SJE happening, eg regression=# explain (costs off) select * from pg_table_a as tom1 left join (select * from pg_table_a as tom2 where tom2.col_bool is not null) tom3 on tom3.col_bool is not null; QUERY PLAN --------------------------------------------------------------------------- Nested Loop Left Join -> Seq Scan on pg_table_a tom1 -> Materialize -> Seq Scan on pg_table_a tom2 Filter: ((col_bool IS NOT NULL) AND (col_bool IS NOT NULL)) (5 rows) > Here is a simplified repro: > create table t (a int unique, b int); > explain (costs off) > select * from t t1 left join > (t t2 join t t3 on t2.a = t3.a) on t3.a is not null; On what grounds are these not just stupidly-written queries? We shouldn't fail of course (which was the actual complaint in #19435), but I'm not excited about removing the user's redundancy. If somebody held a gun to my head and said "fix that", I'd probably teach distribute_quals_to_rels to de-duplicate quals as they go into baserestrictinfo and joinrestrictinfo lists. But I'm quite certain that the planner cycles spent on that would be a net loss for most people. regards, tom lane