Skip to content

Commit

Permalink
Improve performance of update_z_indexes stored procedure by temporari…
Browse files Browse the repository at this point in the history
…ly disabling triggers.
  • Loading branch information
underbluewaters committed Jan 10, 2024
1 parent be2f8a1 commit 1baa8f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/api/migrations/committed/000288.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--! Previous: sha1:38f62a9cfc5d9c555f68f320b486722c2454c64c
--! Hash: sha1:a39327252c79d7c4093213cfa1f7c74b411ae2c3

-- Enter migration here
CREATE OR REPLACE FUNCTION public.update_z_indexes("dataLayerIds" integer[]) RETURNS SETOF public.data_layers
LANGUAGE plpgsql SECURITY DEFINER
AS $$
declare
z int;
pid int;
begin
if (select count(distinct(project_id)) from data_layers where id = any("dataLayerIds")) > 1 then
raise 'Denied. Attempting to modify more than one project.';
end if;
if (session_is_admin((select project_id from data_layers where id = any("dataLayerIds") limit 1))) != true then
raise 'Unauthorized';
end if;
-- Disable triggers to prevent unnecessary checks which could cause
-- deadlocks if rapidly updating z-indexes on a large number of layers.
SET session_replication_role = replica;
z = 0;
for i in array_lower("dataLayerIds", 1)..array_upper("dataLayerIds", 1) loop
z = z + 1;
update data_layers set z_index = z where id = "dataLayerIds"[i];
end loop;
SET session_replication_role = DEFAULT;
return query (select * from data_layers where id = any("dataLayerIds"));
end
$$;
2 changes: 2 additions & 0 deletions packages/api/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13560,11 +13560,13 @@ CREATE FUNCTION public.update_z_indexes("dataLayerIds" integer[]) RETURNS SETOF
if (session_is_admin((select project_id from data_layers where id = any("dataLayerIds") limit 1))) != true then
raise 'Unauthorized';
end if;
SET session_replication_role = replica;
z = 0;
for i in array_lower("dataLayerIds", 1)..array_upper("dataLayerIds", 1) loop
z = z + 1;
update data_layers set z_index = z where id = "dataLayerIds"[i];
end loop;
SET session_replication_role = DEFAULT;
return query (select * from data_layers where id = any("dataLayerIds"));
end
$$;
Expand Down

0 comments on commit 1baa8f2

Please sign in to comment.