with ranked as (
  select
    product_id,
    row_number() over (order by updated_at desc, product_id) as position
  from event_product_details
  where recommended = true
    and status = 'published'
)
update event_product_details details
set recommended = false,
    updated_at = now()
from ranked
where details.product_id = ranked.product_id
  and ranked.position > 3;
