Use select_for_update to avoid race conditions