Which of course is much less than the one in the picture, but still very, very bad.
Some so we've refactored the beast. Now it's 2-3 smaller queries (which are much easier to optimize for PostgreSQL and, above all, individually cacheable) which lead to a nearly 100% speedup for common cases. Also, the code is infinitely more readable which means that it's much easier to extend it.
I'm incredibly happy that we've seen the light and fixed it before it grew to proportions like the ones on the original article shudder