X2VJ3NUGYJVYW3QCHNX6HX2M4QTV6TPKRVHVTYH6FPXMN2DDTLMAC
cart.system_time_start + '30 minutes',
(
select
greatest(
max(system_time_start),
max(case--just to handle deleted line items. worth?
when system_time_end = 'infinity' then '-infinity'
else system_time_end
end)
)
from line_item__between(cart.system_time_start, now())
) + '5 minutes',
create or replace function invalidate_stale_carts() returns void as $$
delete from cart where cart_id in (
select cart_id
from cart inner join line_item__between(system_time_start, now()) as l using (cart_id)
where status = 'active'
group by cart_id
having now() >= least(
cart.system_time_start + '30 minutes',
greatest(
max(l.system_time_start),
max(case
when l.system_time_end = 'infinity' then '-infinity'
else l.system_time_end
end)
) + '5 minutes'
)
)
$$ language sql volatile;