From 26d7cf3f1aa6832f80ca84cf9e68707bf1de8fe0 Mon Sep 17 00:00:00 2001 From: Ben Hutchinson <38333275+HutchyBen@users.noreply.github.com> Date: Tue, 16 Jun 2026 18:06:27 +0000 Subject: [PATCH] only rebuild chart leaderboard when ranking changes (#1671) * only rebuild chart leaderboard when ranking changes * document partition invariant for leaderboard refresh Adds an explanatory comment noting that the ranking-change check assumes an UPDATE never moves a pb between (chart_id, lens) partitions (true today), plus a trailing newline. Keeps DISTINCT for clarity. --------- Co-authored-by: zk --- ..._only_rebuild_leaderboard_score_change.sql | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 db/migrations/20260615024243_only_rebuild_leaderboard_score_change.sql diff --git a/db/migrations/20260615024243_only_rebuild_leaderboard_score_change.sql b/db/migrations/20260615024243_only_rebuild_leaderboard_score_change.sql new file mode 100644 index 000000000..a19bdce0a --- /dev/null +++ b/db/migrations/20260615024243_only_rebuild_leaderboard_score_change.sql @@ -0,0 +1,35 @@ +CREATE OR REPLACE FUNCTION refresh_leaderboard_stmt_after_pb_update() +RETURNS TRIGGER +LANGUAGE plpgsql +SET search_path = public +AS $$ +BEGIN + -- Only rebuild partitions whose leaderboard ordering actually changed. The + -- ordering columns mirror refresh_chart_leaderboard_partition's ORDER BY, so a + -- non-ranking update (e.g. rivalRank in calculated_data, highlight) leaves the + -- leaderboard intact and is skipped entirely. row_id is pb's PK and is never + -- mutated on UPDATE, so OLD and NEW transition rows pair up 1:1. + -- + -- NOTE: this assumes an UPDATE never changes a row's (chart_id, lens), i.e. a + -- pb never moves between partitions. That holds today (updates only touch score + -- data). If a partition-moving UPDATE is ever introduced, this must also refresh + -- the partition the row left, or the old partition's cached ranks go stale. + PERFORM refresh_chart_leaderboard_partition (d.chart_id, d.lens) + FROM ( + SELECT DISTINCT + n.chart_id, + n.lens + FROM pb_updated_new n + JOIN pb_updated_old o ON o.row_id = n.row_id + WHERE n.ranking_value IS DISTINCT FROM o.ranking_value + OR n.ranking_value_tb1 IS DISTINCT FROM o.ranking_value_tb1 + OR n.ranking_value_tb2 IS DISTINCT FROM o.ranking_value_tb2 + OR n.ranking_value_tb3 IS DISTINCT FROM o.ranking_value_tb3 + OR n.ranking_value_tb4 IS DISTINCT FROM o.ranking_value_tb4 + OR n.ranking_value_tb5 IS DISTINCT FROM o.ranking_value_tb5 + OR n.time_achieved IS DISTINCT FROM o.time_achieved + ) AS d; + + RETURN NULL; +END; +$$;