fix: correct syntax errors and refactor interval parsing

This commit is contained in:
Maksym Buz
2026-02-19 23:20:09 +00:00
parent d7b8c7c9c3
commit 91eb4e17b8
7 changed files with 148108 additions and 159056 deletions

View File

@@ -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