Skip to content

Commit

Permalink
fix: make apply_constraints calculate the expected child table
Browse files Browse the repository at this point in the history
  • Loading branch information
keithf4 committed Nov 18, 2024
1 parent c167161 commit 8396317
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 3 additions & 5 deletions sql/functions/apply_constraints.sql
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,24 @@ IF p_child_table IS NULL THEN
END IF;

-- Loop through child tables starting from highest to get a value from the highest non-empty partition in the set
-- Once a child table with a value is found, go back <optimize_constraint> children to make the constraint on that child
-- Once a child table with a value is found, go back <optimize_constraint> + 1 children to make the constraint on that child
FOR v_row_max_value IN
SELECT partition_schemaname, partition_tablename FROM @extschema@.show_partitions(p_parent_table, 'DESC', false)
LOOP
IF v_child_value IS NULL THEN
EXECUTE format('SELECT %L::text FROM %I.%I LIMIT 1'
EXECUTE format('SELECT %I::text FROM %I.%I LIMIT 1'
, v_control
, v_row_max_value.partition_schemaname
, v_row_max_value.partition_tablename
) INTO v_child_value;

ELSE
v_optimize_counter := v_optimize_counter + 1;
IF v_optimize_counter = v_optimize_constraint THEN
IF v_optimize_counter > v_optimize_constraint THEN
v_child_tablename = v_row_max_value.partition_tablename;
EXIT;
END IF;
END IF;
END LOOP;

RAISE DEBUG 'apply_constraint: v_parent_tablename: %, v_last_partition: %, v_child_tablename: %, v_optimize_counter: %'
, v_parent_tablename, v_last_partition, v_child_tablename, v_optimize_counter;

Expand Down
8 changes: 3 additions & 5 deletions updates/pg_partman--5.1.0--5.2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3809,26 +3809,24 @@ IF p_child_table IS NULL THEN
END IF;

-- Loop through child tables starting from highest to get a value from the highest non-empty partition in the set
-- Once a child table with a value is found, go back <optimize_constraint> children to make the constraint on that child
-- Once a child table with a value is found, go back <optimize_constraint> + 1 children to make the constraint on that child
FOR v_row_max_value IN
SELECT partition_schemaname, partition_tablename FROM @extschema@.show_partitions(p_parent_table, 'DESC', false)
LOOP
IF v_child_value IS NULL THEN
EXECUTE format('SELECT %L::text FROM %I.%I LIMIT 1'
EXECUTE format('SELECT %I::text FROM %I.%I LIMIT 1'
, v_control
, v_row_max_value.partition_schemaname
, v_row_max_value.partition_tablename
) INTO v_child_value;

ELSE
v_optimize_counter := v_optimize_counter + 1;
IF v_optimize_counter = v_optimize_constraint THEN
IF v_optimize_counter > v_optimize_constraint THEN
v_child_tablename = v_row_max_value.partition_tablename;
EXIT;
END IF;
END IF;
END LOOP;

RAISE DEBUG 'apply_constraint: v_parent_tablename: %, v_last_partition: %, v_child_tablename: %, v_optimize_counter: %'
, v_parent_tablename, v_last_partition, v_child_tablename, v_optimize_counter;

Expand Down

0 comments on commit 8396317

Please sign in to comment.