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.94.2) (envelope-from ) id 1u96Kz-00FNmy-Pt for pgsql-bugs@arkaria.postgresql.org; Sun, 27 Apr 2025 18:02:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u96Kv-00A1ip-7w for pgsql-bugs@arkaria.postgresql.org; Sun, 27 Apr 2025 18:02:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u96Ku-00A1ih-Uw for pgsql-bugs@lists.postgresql.org; Sun, 27 Apr 2025 18:02:41 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u96Ks-002MXY-1K for pgsql-bugs@lists.postgresql.org; Sun, 27 Apr 2025 18:02:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 53RI2a6K435086; Sun, 27 Apr 2025 14:02:36 -0400 From: Tom Lane To: "David G. Johnston" cc: "jh.lai@qq.com" , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #18904: INTERSECT with an impossible where should eliminate both from the query plan In-reply-to: References: <18904-c5fea7892f4d26ed@postgresql.org> Comments: In-reply-to "David G. Johnston" message dated "Sun, 27 Apr 2025 10:32:22 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <435084.1745776956.1@sss.pgh.pa.us> Date: Sun, 27 Apr 2025 14:02:36 -0400 Message-ID: <435085.1745776956@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Sunday, April 27, 2025, PG Bug reporting form > wrote: >> If you have a query of the form: >> Q1 INTERSECT Q2 ... INTERSECT Qn, >> In such cases, you know that query Qn always returns an empty set(e.g., a >> query with WHERE 1=2), then the entire intersection will always be empty. >> I think that such queries should be eliminated during optimization, as they >> will always return an empty set and should never consume execution time. > These failure to optimize requests are not bugs and are better discussed on > the -general list where some sense of demand can be ascertained. Indeed. To get something like this in, you have to demonstrate that the required developer effort and planner runtime will be repaid by successfully optimizing a nontrivial fraction of real-world cases. I'm not really convinced about that in most of these cases. In this specific case, it seems like it would not take very many extra cycles for plan_set_operations and its subroutines to notice that an input relation is "dummy" (proven empty) and then simplify the set operation accordingly. But it might still not be worth doing, if it complicates that already-complicated code a lot. You'd have to be careful about ALL vs. DISTINCT for instance. Anyway, as David said, this is not a bug. If it's something you really want to see happen, try writing a patch yourself. regards, tom lane