ydolgikh@jerasoft.net writes:
> This query takes around 35 seconds to run when used in the production
> environment ("clients" has about 1 million records). However, if I take out
> ANY join - the query will take only about 300 ms to execute.
Offhand I'd guess that increasing join_collapse_limit (and possibly also
from_collapse_limit) would help.
regards, tom lane