Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw
Date
Msg-id 3945739.1664375858@sss.pgh.pa.us
Whole thread Raw
In response to Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw
List pgsql-bugs
"David G. Johnston" <david.g.johnston@gmail.com> writes:
>> The SQL standard (Section 7.4 general rule 1) mandates that WHERE should be
>> applied to the result of FROM so in the case above pushing filters below
>> 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 = 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-EXPRESS-EVAL

            regards, tom lane



pgsql-bugs by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw
Next
From: Stamatis Zampetakis
Date:
Subject: Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw