Duplicate VOLATILE predicates in a WHERE OR clause can change query results

From: wangyang25(at)otcaix(dot)iscas(dot)ac(dot)cn
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Duplicate VOLATILE predicates in a WHERE OR clause can change query results
Date: 2026-11-22 08:55:56
Message-ID: 179533775644.949949.3714382495455549233@otcaix.iscas.ac.cn
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Raja Sai pranav 2026-11-22 12:29:53 Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
Previous Message shihao zhong 2026-11-21 18:51:38 Re: pg_rewind and hint bits leads to visibility corruption