Skip to content

Commit 9cd69e0

Browse files
committed
fix: fix regression when not creating tables due to retention. run retention in proper places.
1 parent 095ae95 commit 9cd69e0

File tree

3 files changed

+348
-7
lines changed

3 files changed

+348
-7
lines changed

sql/functions/run_maintenance.sql

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ v_tables_list_sql := 'SELECT parent_table
111111
, ignore_default_data
112112
, datetime_string
113113
, maintenance_order
114+
, date_trunc_interval
114115
FROM @extschema@.part_config
115116
WHERE undo_in_progress = false';
116117

@@ -207,8 +208,14 @@ LOOP
207208
SELECT child_start_time INTO v_last_partition_timestamp
208209
FROM @extschema@.show_partition_info(v_parent_schema||'.'||v_last_partition, v_row.partition_interval, v_row.parent_table);
209210

211+
-- Do not create child tables if they would be dropped by retention anyway. Edge case where maintenance was missed for
212+
-- an extended period of time
210213
IF v_row.retention IS NOT NULL THEN
211214
v_last_partition_timestamp := greatest(v_last_partition_timestamp, CURRENT_TIMESTAMP - v_row.retention::interval);
215+
-- Need to properly truncate the interval and account for custom date truncation
216+
SELECT base_timestamp
217+
INTO v_last_partition_timestamp
218+
FROM @extschema@.calculate_time_partition_info(v_row.partition_interval::interval, v_last_partition_timestamp, v_row.date_trunc_interval);
212219
END IF;
213220

214221
-- Must be reset to null otherwise if the next partition set in the loop is empty, the previous partition set's value could be used
@@ -260,8 +267,8 @@ LOOP
260267
v_drop_count := v_drop_count + @extschema@.drop_partition_time(v_row.parent_table);
261268
END IF;
262269

263-
UPDATE @extschema@.part_config SET maintenance_last_run = clock_timestamp() WHERE parent_table = v_row.parent_table;
264270
-- Nothing else to do
271+
UPDATE @extschema@.part_config SET maintenance_last_run = clock_timestamp() WHERE parent_table = v_row.parent_table;
265272
CONTINUE;
266273
END IF;
267274
RAISE DEBUG 'run_maint: v_child_timestamp: %, v_current_partition_timestamp: %, v_max_time_default: %', v_child_timestamp, v_current_partition_timestamp, v_max_time_default;
@@ -359,7 +366,14 @@ LOOP
359366
END IF;
360367
RAISE DEBUG 'run_maint: v_max_id: %, v_current_partition_id: %, v_max_id_default: %', v_max_id, v_current_partition_id, v_max_id_default;
361368
IF v_current_partition_id IS NULL AND v_max_id_default IS NULL THEN
362-
-- Partition set is completely empty. Nothing to do
369+
-- Partition set is completely empty.
370+
371+
-- Still need to run retention if needed. Note similar call below for non-empty sets. Keep in sync.
372+
IF v_row.retention IS NOT NULL THEN
373+
v_drop_count := v_drop_count + @extschema@.drop_partition_id(v_row.parent_table);
374+
END IF;
375+
376+
-- Nothing else to do
363377
UPDATE @extschema@.part_config SET maintenance_last_run = clock_timestamp() WHERE parent_table = v_row.parent_table;
364378
CONTINUE;
365379
END IF;
@@ -402,7 +416,7 @@ LOOP
402416
v_premade_count := ((v_next_partition_id - v_current_partition_id) / v_row.partition_interval::bigint);
403417
END LOOP;
404418

405-
-- Run retention if needed
419+
-- Run retention if needed. Note similar call above when partition set is empty. Keep in sync.
406420
IF v_row.retention IS NOT NULL THEN
407421
v_drop_count := v_drop_count + @extschema@.drop_partition_id(v_row.parent_table);
408422
END IF;

0 commit comments

Comments
 (0)