internal/postgres: improve GetNextVersionsToFetch
internal/postgres: improve GetNextVersionsToFetch

One of the queries in GetNextVersionsToFetch was taking 30s to run.
I couldn't find a way to speed it up. Finally I gave up and rewrote
the method to do something much more straightforward from the DB's
point of view.

First I run a query to get the latest versions of everything in
module_version_states.

Then I scan the entire table for rows eligible for reprocessing,
and group them by priority.

Despite the apparent slowness of these queries, the whole
thing actually runs much faster than the previous version.

Change-Id: Ia88bc26849f0df131d8c00d07bf6a7d160c6b364
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/672575
CI-Result: Cloud Build <ea5729d4cf3392b9d382b6ca272c65aa27554f44@system.gserviceaccount.com>
Reviewed-by: Julie Qiu <e6cb64861edcc38971590ff27c111e0e672de045@google.com>

golang/pkgsite,golang/pkgsite,golang/pkgsite,golang/pkgsite