Skip to content

Commit 7079042

Browse files
authored
improved exception handling, skip tables that are causing errors (#805)
1 parent 386ef50 commit 7079042

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

sql/functions/run_maintenance.sql

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ v_analyze boolean := FALSE;
1818
v_check_subpart int;
1919
v_child_timestamp timestamptz;
2020
v_control_type text;
21+
v_exception boolean;
2122
v_time_encoder text;
2223
v_time_decoder text;
2324
v_create_count int := 0;
@@ -241,40 +242,44 @@ LOOP
241242
FOR v_row_max_time IN
242243
SELECT partition_schemaname, partition_tablename FROM @[email protected]_partitions(v_row.parent_table, 'DESC', false)
243244
LOOP
245+
BEGIN
246+
IF v_control_type = 'time' OR (v_control_type = 'id' AND v_row.epoch <> 'none') THEN
247+
EXECUTE format('SELECT %s::text FROM %I.%I LIMIT 1'
248+
, v_partition_expression
249+
, v_row_max_time.partition_schemaname
250+
, v_row_max_time.partition_tablename
251+
) INTO v_child_timestamp;
252+
ELSIF v_control_type IN ('text', 'uuid') THEN
253+
EXECUTE format('SELECT %s(%s::text) FROM %I.%I LIMIT 1'
254+
, v_time_decoder
255+
, v_partition_expression
256+
, v_row_max_time.partition_schemaname
257+
, v_row_max_time.partition_tablename
258+
) INTO v_child_timestamp;
259+
END IF;
244260

245-
IF v_control_type = 'time' OR (v_control_type = 'id' AND v_row.epoch <> 'none') THEN
246-
EXECUTE format('SELECT %s::text FROM %I.%I LIMIT 1'
247-
, v_partition_expression
248-
, v_row_max_time.partition_schemaname
249-
, v_row_max_time.partition_tablename
250-
) INTO v_child_timestamp;
251-
ELSIF v_control_type IN ('text', 'uuid') THEN
252-
EXECUTE format('SELECT %s(%s::text) FROM %I.%I LIMIT 1'
253-
, v_time_decoder
254-
, v_partition_expression
255-
, v_row_max_time.partition_schemaname
256-
, v_row_max_time.partition_tablename
257-
) INTO v_child_timestamp;
258-
END IF;
259-
260-
IF v_row.infinite_time_partitions AND v_child_timestamp < CURRENT_TIMESTAMP THEN
261-
-- No new data has been inserted relative to "now", but keep making child tables anyway
262-
v_current_partition_timestamp = CURRENT_TIMESTAMP;
263-
-- Nothing else to do in this case so just end early
264-
EXIT;
265-
END IF;
266-
IF v_child_timestamp IS NOT NULL THEN
267-
SELECT suffix_timestamp INTO v_current_partition_timestamp FROM @[email protected]_partition_name(v_row.parent_table, v_child_timestamp::text);
268-
EXIT;
269-
END IF;
261+
IF v_row.infinite_time_partitions AND v_child_timestamp < CURRENT_TIMESTAMP THEN
262+
-- No new data has been inserted relative to "now", but keep making child tables anyway
263+
v_current_partition_timestamp = CURRENT_TIMESTAMP;
264+
-- Nothing else to do in this case so just end early
265+
EXIT;
266+
END IF;
267+
IF v_child_timestamp IS NOT NULL THEN
268+
SELECT suffix_timestamp INTO v_current_partition_timestamp FROM @[email protected]_partition_name(v_row.parent_table, v_child_timestamp::text);
269+
EXIT;
270+
END IF;
271+
EXCEPTION WHEN others THEN
272+
GET STACKED DIAGNOSTICS ex_message = MESSAGE_TEXT;
273+
RAISE WARNING 'Child partition creation skipped for parent table %: %', v_row.parent_table, ex_message;
274+
CONTINUE;
275+
END;
270276
END LOOP;
271277
IF v_row.infinite_time_partitions AND v_child_timestamp IS NULL THEN
272278
-- If partition set is completely empty, still keep making child tables anyway
273279
-- Has to be separate check outside above loop since "future" tables are likely going to be empty, hence ignored in that loop
274280
v_current_partition_timestamp = CURRENT_TIMESTAMP;
275281
END IF;
276282

277-
278283
-- If not ignoring the default table, check for max values there. If they are there and greater than all child values, use that instead
279284
-- Note the default is NOT to care about data in the default, so maintenance will fail if new child table boundaries overlap with
280285
-- data that exists in the default. This is intentional so user removes data from default to avoid larger problems.
@@ -342,8 +347,15 @@ LOOP
342347
CONTINUE;
343348
END;
344349

345-
v_last_partition_created := @[email protected]_partition_time(v_row.parent_table
350+
BEGIN
351+
v_last_partition_created := @[email protected]_partition_time(v_row.parent_table
346352
, ARRAY[v_next_partition_timestamp]);
353+
EXCEPTION WHEN others THEN
354+
v_exception := true;
355+
GET STACKED DIAGNOSTICS ex_message = MESSAGE_TEXT;
356+
EXIT;
357+
END;
358+
347359
IF v_last_partition_created THEN
348360
v_analyze := true;
349361
v_create_count := v_create_count + 1;
@@ -433,7 +445,13 @@ LOOP
433445
EXIT;
434446
END IF;
435447
v_next_partition_id := v_next_partition_id + v_row.partition_interval::bigint;
436-
v_last_partition_created := @[email protected]_partition_id(v_row.parent_table, ARRAY[v_next_partition_id]);
448+
BEGIN
449+
v_last_partition_created := @[email protected]_partition_id(v_row.parent_table, ARRAY[v_next_partition_id]);
450+
EXCEPTION WHEN others THEN
451+
v_exception := true;
452+
GET STACKED DIAGNOSTICS ex_message = MESSAGE_TEXT;
453+
EXIT;
454+
END;
437455
IF v_last_partition_created THEN
438456
v_analyze := true;
439457
v_create_count := v_create_count + 1;
@@ -448,6 +466,13 @@ LOOP
448466

449467
END IF; -- end main IF check for time or id
450468

469+
-- on Exception with one table continue with the next
470+
IF v_exception THEN
471+
v_exception := false;
472+
RAISE WARNING 'Child partition creation skipped for parent table %: %', v_row.parent_table, ex_message;
473+
CONTINUE;
474+
END IF;
475+
451476
IF v_analyze AND p_analyze THEN
452477
IF v_jobmon_schema IS NOT NULL THEN
453478
v_step_id := add_step(v_job_id, format('Analyzing partition set: %s', v_row.parent_table));

0 commit comments

Comments
 (0)