PL/pgSQL function to get the ocurrences of a recurring event