fix: correct syntax errors and refactor interval parsing
This commit is contained in:
@@ -120,25 +120,20 @@ BEGIN
|
||||
IF p_period = 'day' THEN
|
||||
v_period_interval := '1 day'::interval;
|
||||
v_start_time := date_trunc('day', now() AT TIME ZONE 'UTC');
|
||||
-- Calculate how many past days cover the retention period
|
||||
v_past_iterations := extract(day from p_keep_history)::integer;
|
||||
-- Safety cap or ensure minimum? default 7 if null?
|
||||
IF v_past_iterations IS NULL THEN v_past_iterations := 7; END IF;
|
||||
-- Calculate how many past days cover the retention period (86400 seconds = 1 day)
|
||||
v_past_iterations := ceil(extract(epoch from p_keep_history) / 86400)::integer;
|
||||
|
||||
ELSIF p_period = 'week' THEN
|
||||
v_period_interval := '1 week'::interval;
|
||||
v_start_time := date_trunc('week', now() AT TIME ZONE 'UTC');
|
||||
v_past_iterations := (extract(day from p_keep_history) / 7)::integer;
|
||||
-- 604800 seconds = 1 week
|
||||
v_past_iterations := ceil(extract(epoch from p_keep_history) / 604800)::integer;
|
||||
|
||||
ELSIF p_period = 'month' THEN
|
||||
v_period_interval := '1 month'::interval;
|
||||
v_start_time := date_trunc('month', now() AT TIME ZONE 'UTC');
|
||||
-- Approximate months
|
||||
v_past_iterations := (extract(year from p_keep_history) * 12 + extract(month from p_keep_history))::integer;
|
||||
-- Fallback if interval is just days (e.g. '365 days')
|
||||
IF v_past_iterations = 0 THEN
|
||||
v_past_iterations := (extract(day from p_keep_history) / 30)::integer;
|
||||
END IF;
|
||||
-- Approximate 30 days per month (2592000 seconds)
|
||||
v_past_iterations := ceil(extract(epoch from p_keep_history) / 2592000)::integer;
|
||||
ELSE
|
||||
RETURN;
|
||||
END IF;
|
||||
@@ -153,7 +148,6 @@ BEGIN
|
||||
);
|
||||
COMMIT; -- Release lock immediately
|
||||
END LOOP;
|
||||
END LOOP;
|
||||
|
||||
-- 2. Create Past Partitions (Covering retention period)
|
||||
IF v_past_iterations > 0 THEN
|
||||
|
||||
Reference in New Issue
Block a user