Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7cd79ce

Browse files
authored
Reduce DB contention on worker locks (#16160)
1 parent 86ecd34 commit 7cd79ce

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

changelog.d/16160.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduce DB contention on worker locks.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Copyright 2023 The Matrix.org Foundation C.I.C
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- Fix up the triggers that were in `78/04_read_write_locks_triggers.sql`
17+
18+
-- Reduce the number of writes we do on this table.
19+
--
20+
-- Note: that we still want to lock the row here (i.e. still do a `DO UPDATE
21+
-- SET`) so that we serialize updates.
22+
CREATE OR REPLACE FUNCTION upsert_read_write_lock_parent() RETURNS trigger AS $$
23+
BEGIN
24+
INSERT INTO worker_read_write_locks_mode (lock_name, lock_key, write_lock, token)
25+
VALUES (NEW.lock_name, NEW.lock_key, NEW.write_lock, NEW.token)
26+
ON CONFLICT (lock_name, lock_key)
27+
DO UPDATE SET write_lock = NEW.write_lock
28+
WHERE OLD.write_lock != NEW.write_lock;
29+
RETURN NEW;
30+
END
31+
$$
32+
LANGUAGE plpgsql;
33+
34+
DROP TRIGGER IF EXISTS upsert_read_write_lock_parent_trigger ON worker_read_write_locks;
35+
CREATE TRIGGER upsert_read_write_lock_parent_trigger BEFORE INSERT ON worker_read_write_locks
36+
FOR EACH ROW
37+
EXECUTE PROCEDURE upsert_read_write_lock_parent();

0 commit comments

Comments
 (0)