Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sql/functions/run_maintenance.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE FUNCTION @[email protected]_maintenance(
-- If these defaults change reflect them in `run_maintenance_proc`!
, p_analyze boolean DEFAULT false
, p_jobmon boolean DEFAULT true
, p_lock_parent boolean DEFAULT false
)
RETURNS void
LANGUAGE plpgsql
Expand Down Expand Up @@ -225,6 +226,12 @@ LOOP

-- Loop through child tables starting from highest to get a timestamp from the highest non-empty partition in the set
-- Avoids doing a scan on entire partition set and/or getting any values accidentally in default.
-- Lock the parent_table first to prevent deadlocks
IF p_lock_parent THEN
RAISE DEBUG 'PERFORM LOCK ONLY %.%', v_parent_schema, v_parent_tablename;
PERFORM format('LOCK ONLY ''%I.%I''::regclass IN SHARE UPDATE EXCLUSIVE mode', v_parent_schema, v_parent_tablename);
END IF;

FOR v_row_max_time IN
SELECT partition_schemaname, partition_tablename FROM @[email protected]_partitions(v_row.parent_table, 'DESC', false)
LOOP
Expand Down Expand Up @@ -354,6 +361,12 @@ LOOP
-- 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
v_current_partition_id := NULL;

-- Lock the parent_table first to prevent deadlocks
IF p_lock_parent THEN
RAISE DEBUG 'PERFORM LOCK ONLY %.%', v_parent_schema, v_parent_tablename;
PERFORM format('LOCK ONLY ''%I.%I''::regclass IN SHARE UPDATE EXCLUSIVE mode', v_parent_schema, v_parent_tablename);
END IF;

FOR v_row_max_id IN
SELECT partition_schemaname, partition_tablename FROM @[email protected]_partitions(v_row.parent_table, 'DESC', false)
LOOP
Expand Down
5 changes: 3 additions & 2 deletions sql/procedures/run_maintenance_proc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE PROCEDURE @[email protected]_maintenance_proc(
-- Keep these defaults in sync with `run_maintenance`!
, p_analyze boolean DEFAULT false
, p_jobmon boolean DEFAULT true
, p_lock_parent boolean DEFAULT true
)
LANGUAGE plpgsql
AS $$
Expand Down Expand Up @@ -35,8 +36,8 @@ LOOP
/*
* Run maintenance with a commit between each partition set
*/
v_sql := format('SELECT %s.run_maintenance(%L, p_jobmon := %L',
'@extschema@', v_parent_table, p_jobmon);
v_sql := format('SELECT %s.run_maintenance(%L, p_jobmon := %L, p_lock_parent := %L',
'@extschema@', v_parent_table, p_jobmon, p_lock_parent);

IF p_analyze IS NOT NULL THEN
v_sql := v_sql || format(', p_analyze := %L', p_analyze);
Expand Down