update acl_entry e set acl_object_identity = (
    select min(id)
    from acl_object_identity oi2
    where oi2.object_id_identity = (
        select object_id_identity from acl_object_identity oi1 where oi1.id = e.acl_object_identity
    )
);

update acl_entry e set sid = (
    select min(id)
    from acl_sid sid2
    where sid2.sid = (
        select sid from acl_sid sid1 where sid1.id = e.sid
    )
);

update acl_object_identity e set owner_sid = (
    select min(id)
    from acl_sid sid2
    where sid2.sid = (
        select sid from acl_sid sid1 where sid1.id = e.owner_sid
    )
);

delete from acl_sid s1 where s1.id != ( select min(s2.id) from acl_sid s2 where s2.sid = s1.sid);
delete from acl_object_identity s1 where s1.id != ( select min(s2.id) from acl_object_identity s2 where s2.object_id_identity = s1.object_id_identity);
