Does postgresql evalute an exression E2
in (E1 OR E2)
(even if E1
is true)?
Maybe. It is unpredictable per spec:
The order of evaluation of subexpressions is not defined. In particular, the inputs of an operator or function are not necessarily evaluated left-to-right or in any other fixed order.
Furthermore, if the result of an expression can be determined by evaluating only some parts of it, then other subexpressions might not be evaluated at all.
But your expression is safe, because:
SQL uses a three-valued logic system with true, false, and null, which represents "unknown".
In case of NULL
values, evaluation goes as (TRUE OR NULL)
, which is TRUE
.
In fact both of these expressions are satisfying your needs (either not-null or if null then there're no constratints should be applied):
WHERE date_col IS NULL OR date_col > :date
-- vs:
WHERE date_col > :date OR date_col IS NULL
Boolean expressions (AND
/OR
/NOT
combinations) in those clauses can be reorganized in any manner allowed by the laws of Boolean algebra.
This also applies to PostgreSQL 8.4.
(E1 OR E2)
. Is it going to evaluate E2 if E1 is true?OR
? Your comment makes sense if those conditions where combined with anAND
but usingx is null or x > something
does make sense.TRUE or anything
is going to be true