#PostgreSQL has failed me for the first time. Being unable to optimize seemingly simple SELECT ... WHERE TRUE OR field IN (SELECT ...) and eliminate the join, it leaves me no choice but to shove template engine into my database handling code...
@saper plain UNION might work. The idea is that I have a lot of joined tables each of which serves specific part of large AND condition, and when that part is equivalent TRUE I'd like to avoid unneeded table scan.
@AMDmi3 not sure I understand what you are doing. Why is join bad?
@saper the extra table scan is bad, while that table is not really used in the request. Planner is not able to deduce that and avoid the scan, and there's no way to make join or subrequest conditional, so I have to reside to dynamicaly generating SQL queries, which I've hoped to avoid
@saper because it's constant expression which evaluates to TRUE. Well the problem is explained in https://www.postgresql.org/message-id/2374.1366296417%40sss.pgh.pa.us
And between fiddling with the planner and introducing SQL query template engine I think I'll pick the latter, as it's at least straightforward and predictable.