-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use advisory lock when assigning sequence IDs #401
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DROP FUNCTION insert_group_message; | ||
|
||
DROP FUNCTION insert_welcome_message; | ||
|
||
DROP FUNCTION insert_inbox_log; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
CREATE FUNCTION insert_group_message(group_id BYTEA, data BYTEA, group_id_data_hash BYTEA) | ||
RETURNS SETOF group_messages | ||
AS $$ | ||
BEGIN | ||
-- Ensures that the generated sequence ID matches the insertion order | ||
-- Only released at the end of the enclosing transaction - beware if called within a long transaction | ||
PERFORM | ||
pg_advisory_xact_lock(hashtext('group_messages_sequence'), hashtext(encode(group_id, 'hex'))); | ||
RETURN QUERY INSERT INTO group_messages(group_id, data, group_id_data_hash) | ||
VALUES(group_id, data, group_id_data_hash) | ||
RETURNING | ||
*; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
|
||
CREATE FUNCTION insert_welcome_message(installation_key BYTEA, data BYTEA, installation_key_data_hash BYTEA, hpke_public_key BYTEA) | ||
RETURNS SETOF welcome_messages | ||
AS $$ | ||
BEGIN | ||
PERFORM | ||
pg_advisory_xact_lock(hashtext('welcome_messages_sequence'), hashtext(encode(installation_key, 'hex'))); | ||
RETURN QUERY INSERT INTO welcome_messages(installation_key, data, installation_key_data_hash, hpke_public_key) | ||
VALUES(installation_key, data, installation_key_data_hash, hpke_public_key) | ||
RETURNING | ||
*; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
|
||
CREATE FUNCTION insert_inbox_log(inbox_id BYTEA, server_timestamp_ns BIGINT, identity_update_proto BYTEA) | ||
RETURNS SETOF inbox_log | ||
AS $$ | ||
BEGIN | ||
PERFORM | ||
pg_advisory_xact_lock(hashtext('inbox_log_sequence'), hashtext(encode(inbox_id, 'hex'))); | ||
RETURN QUERY INSERT INTO inbox_log(inbox_id, server_timestamp_ns, identity_update_proto) | ||
VALUES(inbox_id, server_timestamp_ns, identity_update_proto) | ||
RETURNING | ||
*; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could cut down on the number of hashes by concatenating the two strings before hashing if we're concerned about perf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another good option if perf becomes an issue. Separated it for this implementation to reduce collisions (especially against other tables)