Author could lose a layer of subselect with the filter on rank by using first_value over the window frame instead. https://gist.github.com/dbenhur/951b7bae4e07d7998268
I'll play with this later today and see if it performs any better. In any case, it makes the query easier to read, so we'll probably use this unless it somehow makes the function slower.
Thanks again!
Interesting, and very surprising! I'm going to dig around and see if I can't figure out why this is the case.
I would have guessed there would be a minor performance improvement, because the first_value approach effectively allows PostgreSQL to discard the data sooner, instead of materializing it in an additional subquery.