Files
threshold/internal/db/schema/migrations/oss/postgres/35/02_cleanup.up.sql
T
Jeff Mitchell 396edfca80 Add an extra cleanup task for sessions stuck in canceling state (#2229)
* Add an extra cleanup task for sessions stuck in canceling state

See the comment on the migrations file for details.
2022-06-23 12:25:55 -04:00

74 lines
2.2 KiB
PL/PgSQL

begin;
-- This adds additional cleanup to what went into 0.8.x. There could be a case
-- where sessions can persist in canceled state even if there are no connections
-- for that session (possibly due to cleanup) and will never be transitioned
-- because connection state cannot be determined.
--
-- If they have previously been marked terminated in the warehouse we delete
-- them; if not we mark them terminated so it's recorded in the warehouse and
-- the cleanup job should eventually clear these out.
with
canceling_sessions(session_id) as (
select session_id
from session_state
where state = 'canceling'
and end_time is null
),
no_connection_canceling_sessions as (
select session_id
from canceling_sessions
where session_id not in
(
select session_id
from session_connection
)
),
has_termination_reason (session_id) as (
select public_id
from session
where public_id in
(
select session_id
from no_connection_canceling_sessions
)
and termination_reason is not null
),
terminated_in_warehouse (session_id) as (
select session_id
from has_termination_reason
where session_id in
(
select session_id
from wh_session_accumulating_fact
where session_terminated_date_key <> -1
)
),
not_terminated_in_warehouse (session_id) as (
select session_id
from has_termination_reason
where session_id not in
(
select session_id
from terminated_in_warehouse
)
),
delete_terminated_in_warehouse_from_session as (
delete from session
where public_id in
(
select session_id
from terminated_in_warehouse
)
)
update session
set
version = version + 1,
termination_reason = 'canceled'
where public_id in
(
select session_id
from not_terminated_in_warehouse
);
commit;