I recall using https://pghintplan.osdn.jp/pg_hint_plan.html for this feature, it did what it claimed to be able to do. It's a hack, but it really helps in the worst case scenarios.
There's now a github repo for pg_hint_plan: https://github.com/ossc-db/pg_hint_plan