lemmy/migrations/2023-07-27-134652_remove-expensive-broken-trigger/up.sql
phiresky e315092ee3
remove n^2 part of person triggers, improve community aggregate trigger (#3739)
* remove n^2 part of person triggers, improve community aggregate trigger

* comment out comment_score tests since previously they only accidentally succeeded

* empty
2023-07-27 23:36:51 +02:00

47 lines
1.4 KiB
PL/PgSQL

create or replace function person_aggregates_comment_count()
returns trigger language plpgsql
as $$
begin
IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
update person_aggregates
set comment_count = comment_count + 1 where person_id = NEW.creator_id;
ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
update person_aggregates
set comment_count = comment_count - 1 where person_id = OLD.creator_id;
END IF;
return null;
end $$;
create or replace function person_aggregates_post_count()
returns trigger language plpgsql
as $$
begin
IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
update person_aggregates
set post_count = post_count + 1 where person_id = NEW.creator_id;
ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
update person_aggregates
set post_count = post_count - 1 where person_id = OLD.creator_id;
END IF;
return null;
end $$;
create or replace function community_aggregates_comment_count()
returns trigger language plpgsql
as $$
begin
IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
update community_aggregates ca
set comments = comments + 1 from post p
where p.id = NEW.post_id
and ca.community_id = p.community_id;
ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
update community_aggregates ca
set comments = comments - 1 from post p
where p.id = OLD.post_id
and ca.community_id = p.community_id;
END IF;
return null;
end $$;