Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: zabetak(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw
Date: 2022-09-28 14:37:38
Message-ID: 3945739.1664375858@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

"David G. Johnston" <david(dot)g(dot)johnston(at)gmail(dot)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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Stamatis Zampetakis 2022-09-28 15:44:25 Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw
Previous Message David G. Johnston 2022-09-28 13:56:26 Re: BUG #17623: WHERE should be evaluated after FROM clause when operators may throw