Why is PostgreSQL very slow when I execute a SELECT query with OR in WHERE clause? The query is like:

select count(*)
from test1 as r
WHERE r.testid in (select p.testid from test2 p where p.testname IS NULL)
OR r.testid &gt; (SELECT MAX(p2.testid)FROM test2 p2);


but when I rewrite it as UNION it works. What is the problem of this query?
This is a common issue - PostgreSQL planner cannot use indexes (it's not able to optimize OR, when OR is used for subselects) and rewriting with UNION helps. Last year Tom Lane wrote a patch, but as far as I know, this patch was not merged yet.