Fix and optimize rbac_linked_permissions.sql