Permalink
Browse files

Stream write concurrency is serialized with advisory locks

  • Loading branch information...
sbellware committed Jun 4, 2018
1 parent 20bdfce commit 5e81fff4111155798a22f206528c20a4bfc4b6c3
Showing with 7 additions and 0 deletions.
  1. +7 −0 database/functions/write-message.sql
@@ -13,9 +13,13 @@ DECLARE
stream_version bigint;
position bigint;
category varchar;
stream_name_hash bigint;
BEGIN
message_id = uuid(_id);
stream_name_hash = hash_64(_stream_name);
PERFORM pg_advisory_lock(stream_name_hash);
stream_version := stream_version(_stream_name);
if stream_version is null then
@@ -24,6 +28,7 @@ BEGIN
if _expected_version is not null then
if _expected_version != stream_version then
PERFORM pg_advisory_unlock(stream_name_hash);
raise exception 'Wrong expected version: % (Stream: %, Stream Version: %)', _expected_version, _stream_name, stream_version;
end if;
end if;
@@ -50,6 +55,8 @@ BEGIN
)
;
PERFORM pg_advisory_unlock(stream_name_hash);
return position;
END;
$$ LANGUAGE plpgsql;

0 comments on commit 5e81fff

Please sign in to comment.