Function in same Session become increasingly slow when run multiple times concurrently
Since the problem are bad generic plans (symptom: execution becomes slower from the sixth execution on) and you are using PostgreSQL v12, the solution is simple:
ALTER FUNCTION xy SET plan_cache_mode = force_custom_plan;