diff --git a/swh/storage/postgresql/storage.py b/swh/storage/postgresql/storage.py --- a/swh/storage/postgresql/storage.py +++ b/swh/storage/postgresql/storage.py @@ -107,7 +107,7 @@ class Storage: """SWH storage datastore proxy, encompassing DB and object storage""" - current_version: int = 183 + current_version: int = 184 def __init__( self, diff --git a/swh/storage/sql/40-funcs.sql b/swh/storage/sql/40-funcs.sql --- a/swh/storage/sql/40-funcs.sql +++ b/swh/storage/sql/40-funcs.sql @@ -658,9 +658,16 @@ from origin where url = origin_url ), last_known_visit as ( - select coalesce(max(visit), 0) as visit - from origin_visit - where origin = (select id from origin_id) + select coalesce( + ( + select visit + from origin_visit + where origin = (select id from origin_id) + order by visit desc + limit 1 + for update + ), + 0) as visit ) insert into origin_visit (origin, date, type, visit) values ((select id from origin_id), date, type, diff --git a/swh/storage/sql/upgrades/184.sql b/swh/storage/sql/upgrades/184.sql new file mode 100644 --- /dev/null +++ b/swh/storage/sql/upgrades/184.sql @@ -0,0 +1,33 @@ +-- SWH DB schema upgrade +-- from_version: 183 +-- to_version: 184 +-- description: origin_visit_add: Fix crash when adding multiple visits to the same origin simultaneously + +insert into dbversion(version, release, description) + values(184, now(), 'Work In Progress'); + +create or replace function swh_origin_visit_add(origin_url text, date timestamptz, type text) + returns bigint + language sql +as $$ + with origin_id as ( + select id + from origin + where url = origin_url + ), last_known_visit as ( + select coalesce( + ( + select visit + from origin_visit + where origin = (select id from origin_id) + order by visit desc + limit 1 + for update + ), + 0) as visit + ) + insert into origin_visit (origin, date, type, visit) + values ((select id from origin_id), date, type, + (select visit from last_known_visit) + 1) + returning visit; +$$;