-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of update_z_indexes stored procedure by temporari…
…ly disabling triggers.
- Loading branch information
1 parent
be2f8a1
commit 1baa8f2
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters