Unique pair checking query improving in pivot table
The query can be made easier:

select least(profile_id,followed_id), greatest(profile_id, followed_id), count(*)
from tbl_profile_follow 
group by least(profile_id,followed_id), greatest(profile_id, followed_id)
having count(*) &gt; 1;


Using this approach you can also create a unique index on the table

create unique index idx_unique_pair 
   on tbl_profile_follow (least(profile_id,followed_id), greatest(profile_id, followed_id));


This above will however not prevent or detect multi-level loops:

profile_id | followed_id
         3 |           5
         5 |           6
         6 |           3