plpgsql table rows handling