"Yary Hluchan" <not(dot)com(at)gmail(dot)com> writes:
> My reading of it is that the optimizer isn't applying a distributive rule:
> condition(a union b) == condition(a) union condition(b).
No, the correct reading is that a UNION subquery is planned
independently of the surrounding query. This is not likely to change
soon, as it would involve some rather wholesale changes.
My advice is to lose the open/closed distinction and fold those tables
into single tables with an extra boolean flag column. I suppose the
point of this database layout is to provide fast access to the "open"
subsets, but you could probably achieve that by making partial indexes,
eg
create index vote_open_ids on vote (id) where is_open;
regards, tom lane