@@ -111,6 +111,7 @@ v_tables_list_sql := 'SELECT parent_table
111
111
, ignore_default_data
112
112
, datetime_string
113
113
, maintenance_order
114
+ , date_trunc_interval
114
115
FROM @extschema@.part_config
115
116
WHERE undo_in_progress = false' ;
116
117
@@ -207,8 +208,14 @@ LOOP
207
208
SELECT child_start_time INTO v_last_partition_timestamp
208
209
FROM @extschema@.show_partition_info(v_parent_schema|| ' .' || v_last_partition, v_row .partition_interval , v_row .parent_table );
209
210
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
210
213
IF v_row .retention IS NOT NULL THEN
211
214
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 );
212
219
END IF;
213
220
214
221
-- 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
267
v_drop_count := v_drop_count + @extschema@.drop_partition_time(v_row .parent_table );
261
268
END IF;
262
269
263
- UPDATE @extschema@.part_config SET maintenance_last_run = clock_timestamp() WHERE parent_table = v_row .parent_table ;
264
270
-- Nothing else to do
271
+ UPDATE @extschema@.part_config SET maintenance_last_run = clock_timestamp() WHERE parent_table = v_row .parent_table ;
265
272
CONTINUE;
266
273
END IF;
267
274
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
359
366
END IF;
360
367
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;
361
368
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
363
377
UPDATE @extschema@.part_config SET maintenance_last_run = clock_timestamp() WHERE parent_table = v_row .parent_table ;
364
378
CONTINUE;
365
379
END IF;
402
416
v_premade_count := ((v_next_partition_id - v_current_partition_id) / v_row .partition_interval ::bigint );
403
417
END LOOP;
404
418
405
- -- Run retention if needed
419
+ -- Run retention if needed. Note similar call above when partition set is empty. Keep in sync.
406
420
IF v_row .retention IS NOT NULL THEN
407
421
v_drop_count := v_drop_count + @extschema@.drop_partition_id(v_row .parent_table );
408
422
END IF;
0 commit comments