Duplicate VOLATILE predicates in a WHERE OR clause can change query results
Thread notes
1 note#bug#not-a-bug
hackorum-bot#bug#not-a-bug
@global #bug #not-a-bug
PostgreSQL server versions tested:
- 18.7; source tag REL_18_7, commit f5c41f6681c910b3f69951bf6fb97c6d74605d25; psql 18.7
- 19.0; source tag REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10; psql 19.0
Platform: Ubuntu 24.04, x86_64; GCC 13.3.0
PostgreSQL 19.0 build options: --without-readline --without-zlib --without-icu --without-llvm
Reproduction (run all statements in one fresh psql -X session):
SELECT setseed(0.314159);
EXPLAIN (VERBOSE, COSTS OFF)
SELECT i
FROM generate_series(1, 200000) AS g(i)
WHERE random() < (0.5::float8 + (i % 2)::float8 / 100::float8)
OR random() < (0.5::float8 + (i % 2)::float8 / 100::float8);
SELECT setseed(0.314159);
SELECT count(*) AS duplicate_or
FROM generate_series(1, 200000) AS g(i)
WHERE random() < (0.5::float8 + (i % 2)::float8 / 100::float8)
OR random() < (0.5::float8 + (i % 2)::float8 / 100::float8);
SELECT setseed(0.314159);
SELECT count(*) AS case_two_calls
FROM generate_series(1, 200000) AS g(i)
WHERE CASE
WHEN random() < (0.5::float8 + (i % 2)::float8 / 100::float8) THEN true
ELSE random() < (0.5::float8 + (i % 2)::float8 / 100::float8)
END;
SELECT setseed(0.314159);
SELECT count(*) AS one_call
FROM generate_series(1, 200000) AS g(i)
WHERE random() < (0.5::float8 + (i % 2)::float8 / 100::float8);
The threshold is 0.50 or 0.51 depending on the row, so the predicate remains a per-row filter. The CASE form preserves the second call when the first comparison is false.
Actual result:
EXPLAIN displays this single random() comparison in the filter, although the query contains two identical OR terms:
Filter: (random() < ('0.5'::double precision + (((g.i % 2))::double precision / '100'::double precision)))
With the seed shown, both tested versions return the same counts:
PostgreSQL 18.7:
duplicate_or 100855
case_two_calls 150925
one_call 100855
PostgreSQL 19.0:
duplicate_or 100855
case_two_calls 150925
one_call 100855
The duplicate-OR result is identical to the single-call control. The CASE control, which retains the second call, accepts substantially more rows.
Expected result:
random() is VOLATILE and can return different values on successive calls. The duplicate-OR predicate should therefore not be reduced to a single evaluation. Its result should differ from the one-call control and be consistent with two comparisons joined by OR.
PostgreSQL documentation states that the optimizer makes no assumptions about VOLATILE functions and names random() as an example:
https://www.postgresql.org/docs/18/xfunc-volatility.html
https://www.postgresql.org/docs/19/xfunc-volatility.html
PostgreSQL permits reordering Boolean predicates; this report concerns a change in the result after one of the volatile calls is removed:
https://www.postgresql.org/docs/18/sql-expressions.html#SYNTAX-EXPRESS-EVAL
https://www.postgresql.org/docs/19/sql-expressions.html#SYNTAX-EXPRESS-EVAL
Version scope:
The behavior was reproduced on PostgreSQL 18.7 and 19.0 on Linux x86_64. PostgreSQL 19.0 was built from REL_19_0 with the options listed above. The test instances used local Unix sockets and nondefault ports for isolation; no query-planning settings were changed. The reproducer creates no persistent objects. Other major versions and build configurations have not been tested.
wangyang25@otcaix.iscas.ac.cn writes:
EXPLAIN (VERBOSE, COSTS OFF)
SELECT i
FROM generate_series(1, 200000) AS g(i)
WHERE random() < (0.5::float8 + (i % 2)::float8 / 100::float8)
OR random() < (0.5::float8 + (i % 2)::float8 / 100::float8);
Expected result:
random() is VOLATILE and can return different values on successive calls. The duplicate-OR predicate should therefore not be reduced to a single evaluation. Its result should differ from the one-call control and be consistent with two comparisons joined by OR.
I can't get excited about this. Cold reality is that if you put
volatile expressions into WHERE, you will not get predictable results
in any but the simplest single-term cases. IMV this is authorized by
the same docs you cited:
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.
...
As a consequence, it is unwise to use functions with side effects
as part of complex expressions. It is particularly dangerous to
rely on side effects or evaluation order in WHERE and HAVING
clauses, since those clauses are extensively reprocessed as part
of developing an execution plan. Boolean expressions (AND/OR/NOT
combinations) in those clauses can be reorganized in any manner
allowed by the laws of Boolean algebra.
Concretely, even if we did not de-duplicate the OR arms, we'd still
short-circuit evaluation of the OR as soon as one clause returns
true, so that the number of evaluations of random() would still be
unpredictable.
If you really need random() to be evaluated twice per row, you could
arrange that by calling it from a plpgsql function. Or maybe use
LATERAL function calls. But don't expect us to lobotomize the
optimizer and executor in service of this; we have more-useful things
to spend our limited engineering effort on.
regards, tom lane