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 1odYC1-0006f4-S9 for pgsql-bugs@arkaria.postgresql.org; Wed, 28 Sep 2022 14:37:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1odYC0-0006Yl-FB for pgsql-bugs@arkaria.postgresql.org; Wed, 28 Sep 2022 14:37:44 +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 1odYC0-0006Yc-6q for pgsql-bugs@lists.postgresql.org; Wed, 28 Sep 2022 14:37:44 +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 1odYBx-0002lC-82 for pgsql-bugs@lists.postgresql.org; Wed, 28 Sep 2022 14:37:43 +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 28SEbcOq3945740; Wed, 28 Sep 2022 10:37:39 -0400 From: Tom Lane To: "David G. Johnston" cc: zabetak@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw In-reply-to: References: <17623-2bef2f4ff7fe8051@postgresql.org> Comments: In-reply-to "David G. Johnston" message dated "Wed, 28 Sep 2022 06:56:26 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3945738.1664375858.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 28 Sep 2022 10:37:38 -0400 Message-ID: <3945739.1664375858@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: >> The SQL standard (Section 7.4 general rule 1) mandates that WHERE shoul= d be >> applied to the result of FROM so in the case above pushing filters belo= w >> the join seems to violate the standard. > The failure to document such a deviation from the standard can be > considered a bug but not the deviation itself. That is intentional. In > terms of trade-offs the current behavior seems reasonable. Enforcing such a restriction would be absolutely disastrous for the performance of a large number of real-world queries. I doubt that any other RDBMS takes that spec wording literally either. I'd suggest something like CASE WHEN e.empno =3D 0 THEN NULL ELSE 10 / e.empno END if you need to program around the lack of guarantees about the order of evaluation. A WITH clause can also be used as an optimization fence when you need one. BTW, this *is* documented, see for example https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-EXPRES= S-EVAL regards, tom lane