Transforming IN (...) to ORs, volatility
Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
From: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
To: PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2011-04-01T11:24:53Z
Lists: pgsql-hackers
We sometimes transform IN-clauses to a list of ORs:
postgres=# explain SELECT * FROM foo WHERE a IN (b, c);
QUERY PLAN
------------------------------------------------------
Seq Scan on foo (cost=0.00..39.10 rows=19 width=12)
Filter: ((a = b) OR (a = c))
(2 rows)
But what if you replace "a" with a volatile function? It doesn't seem
legal to do that transformation in that case, but we do it:
postgres=# explain SELECT * FROM foo WHERE (random()*2)::integer IN (b, c);
QUERY PLAN
-------------------------------------------------------------------------------------------------
-------------------
Seq Scan on foo (cost=0.00..68.20 rows=19 width=12)
Filter: ((((random() * 2::double precision))::integer = b) OR
(((random() * 2::double precision))::integer = c))
(2 rows)
I tried to read the SQL spec to see if it has anything to say about
that, but I couldn't find anything. My common sense says that that
transformation is not legal.
--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com