2 DROP PROCEDURE IF EXISTS update_toplist $$
3 CREATE PROCEDURE update_toplist()
5 DECLARE deadlock INT DEFAULT 0;
6 DECLARE attempts INT DEFAULT 0;
8 DROP TABLE IF EXISTS toplist_temp;
9 CREATE TABLE toplist_temp (
10 podcast_id INT PRIMARY KEY REFERENCES podcast (id),
11 subscription_count INT NOT NULL DEFAULT 0,
15 try_loop:WHILE (attempts<3) DO
17 DECLARE deadlock_detected CONDITION FOR 1213;
18 DECLARE EXIT HANDLER FOR deadlock_detected
26 DELETE FROM toplist_temp;
27 INSERT INTO toplist_temp (SELECT a.podcast_id, COUNT(*) AS count_subscription
28 FROM (SELECT DISTINCT podcast_id, user_id
29 FROM public_subscription) a
32 INSERT INTO toplist (podcast_id, subscription_count, id) (SELECT podcast_id, subscription_count, NULL FROM toplist_temp
33 ORDER BY subscription_count DESC LIMIT 100);
40 SET attempts=attempts+1;
45 call FAIL('Toplist is not updated!');
47 DROP TABLE IF EXISTS toplist_temp;