select * from records where id in ( select max(id) from records group by option_id )
This query works fine even on millions of rows. However as y
select distinct on (option_id) * from records order by option_id, id desc
Indexes will only be used if the cardinality is favorable. That said you can try a composite index
create index index_name on records(option_id, id desc)