Tom Lane wrote:
>
> Yes. The inner query must happen by sequential scan. But the outer
> query can use an index if it is in the form
> ... where (name = 'a') or (name = 'b') or (name = 'c');
> which is what "where name in ('a','b','c')" gets translated to.
>
> However, the *real* problem here is that the inner query is treated
> as a subplan, which means it is re-evaluated for each tuple scanned
> by the outer query. This is not very bright; the system ought to
> notice that the inner query does not depend on the state of the
> outer query, and do it only once. There is already a notion of
> InitPlan vs. SubPlan. I'm not sure why this case is being classified
> as a SubPlan, but it sure looks like it ought to be done as an
> InitPlan...
Not in all cases. First, to use InitPlan you would have to add
DISTINCT to subquery to avoid duplicates. Second, there is another
way of optimization - cache/hash subquery results to avoid
re-execution of subquery plan. Planner should make decision what
way to follow.
Vadim