I have a postgres database with a Genre table (40 records), a Book table (~1.3M records) and a many-many table to relate the two (~2M records). I want to select books by a particular Genre and then order them by some criteria and take the top n results. Some genres have just a few hundred books while others have several hundred thousand books. The query planner seems to have two ways of doing the query. 1. Sorts the books by the criteria with an index and then takes the top n results with the particular genre or 2. Gets books with the particular genre and then do an external sort on the criteria. 1 works great if the genre has many books but is very slow if the genre only has a few hundred books. 2 works great if the genre has few books but bad if the genre has many books. The query planner seems to just make the decision based on whether there is an index for the sort criteria, so it ends up making the wrong choice often. Assuming we have an index, is there a way to ensure the optimizer makes the right choice?