Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdC7S-0005QQ-4X for pgsql-performance@arkaria.postgresql.org; Wed, 20 Oct 2021 13:59:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mdC7R-0002mV-0Y for pgsql-performance@arkaria.postgresql.org; Wed, 20 Oct 2021 13:59:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdC7Q-0002mL-Nx for pgsql-performance@lists.postgresql.org; Wed, 20 Oct 2021 13:59:00 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdC7O-0007uD-6u for pgsql-performance@lists.postgresql.org; Wed, 20 Oct 2021 13:59:00 +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 19KDwtES2133340; Wed, 20 Oct 2021 09:58:55 -0400 From: Tom Lane To: "David G. Johnston" cc: Michael Lewis , Mithran Kulasekaran , Pgsql Performance Subject: Re: Postgres views cannot use both union and join/where In-reply-to: References: Comments: In-reply-to "David G. Johnston" message dated "Tue, 19 Oct 2021 20:38:40 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2133338.1634738335.1@sss.pgh.pa.us> Date: Wed, 20 Oct 2021 09:58:55 -0400 Message-ID: <2133339.1634738335@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 Tuesday, October 19, 2021, Michael Lewis wrote: >> On Tue, Oct 19, 2021 at 3:48 PM Mithran Kulasekaran < >> mithranakulasekaran@gmail.com> wrote: >>> create view template_view (id, name, description, is_staged) as >>> select t.id,t.name, t.description, false as is_staged >>> from template t >>> left join template_staging ts on t.name = ts.name and ts.name is null >> Does that work? I've only seen that type of logic written as- >> left join template_staging ts on t.name = ts.name >> where ts.name is null > The are functionally equivalent, though the timing of the expression > evaluation differs slightly. No, not at all. Michael's version correctly implements an anti-join, where the first version does not. The reason is that the WHERE clause "sees" the column value post-JOIN, whereas the JOIN/ON clause "sees" values pre-JOIN. Assuming that the '=' operator is strict, the first query's ON clause really reduces to constant false, so that you just get a null-extended image of the left table. That's almost surely not what's wanted. regards, tom lane