PostgreSQL can perform an index scan in both cases, but it prefers a sequential scan and a sort in the first case, because it thinks that will be faster. In the second case, an index-only scan is possible and faster.
You could of course add all table columns to the index, but that is usually unreasonable and may even exceed the size limit for an index entry.
If you have reason to believe that PostgreSQL is doing the wrong thing, you can try if the query is faster with
enable_seqscan set to
off. If yes, perhaps you have to adjust
random_page_cost to match your hardware.