-
-
Notifications
You must be signed in to change notification settings - Fork 871
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
Improve performance of site_aggregates and person_aggregates triggers #3704
Closed
RocketDerp
wants to merge
12
commits into
LemmyNet:main
from
RocketDerp:postgresql_trigger_july2023b
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eed17d8
emergency change to improve performance of site_aagregates PostgreSQL…
RocketDerp 815174d
emergency change to improve performance of person_aggregates PostgreS…
RocketDerp 3baf922
simplify SELECT queries in site_aggregates_activity function
RocketDerp 94ebdf2
remove ttesting password from script
RocketDerp aaf9fd6
Merge branch 'main' into postgresql_trigger_july2023b
RocketDerp 69c54fd
remove api_tests reference to not-yet-released aggregates.spec.ts
RocketDerp 2d42786
add site_id index, reset site_iq_seq in tests
phiresky 5068662
Merge branch 'LemmyNet:main' into postgresql_trigger_july2023b
RocketDerp 4bac4ea
enhanced user testing regarding user account delete impacting aggrega…
RocketDerp ba7a25e
Merge branch 'LemmyNet:main' into postgresql_trigger_july2023b
RocketDerp 74b7922
Merge branch 'LemmyNet:main' into postgresql_trigger_july2023b
RocketDerp 102a636
Merge branch 'LemmyNet:main' into postgresql_trigger_july2023b
RocketDerp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
migrations/2023-07-23-182318_site_aggregates_local_only/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
-- This file undoes what is in `up.sql` | ||
-- to ensure no confusion with all the migrations | ||
-- this was mostly created by dumping from PostgreSQL 15.3 schema | ||
|
||
DROP INDEX idx_site_aggregates_site_id; | ||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_comment_delete() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
IF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN | ||
update site_aggregates sa | ||
set comments = comments - 1 | ||
from site s | ||
where sa.site_id = s.id; | ||
END IF; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_comment_insert() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN | ||
update site_aggregates sa | ||
set comments = comments + 1 | ||
from site s | ||
where sa.site_id = s.id; | ||
END IF; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_community_delete() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
IF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN | ||
update site_aggregates sa | ||
set communities = communities - 1 | ||
from site s | ||
where sa.site_id = s.id; | ||
END IF; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_community_insert() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN | ||
update site_aggregates sa | ||
set communities = communities + 1 | ||
from site s | ||
where sa.site_id = s.id; | ||
END IF; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_person_delete() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
-- Join to site since the creator might not be there anymore | ||
update site_aggregates sa | ||
set users = users - 1 | ||
from site s | ||
where sa.site_id = s.id; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_person_insert() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
update site_aggregates | ||
set users = users + 1; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_post_delete() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
IF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN | ||
update site_aggregates sa | ||
set posts = posts - 1 | ||
from site s | ||
where sa.site_id = s.id; | ||
END IF; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_post_insert() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
begin | ||
IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN | ||
update site_aggregates sa | ||
set posts = posts + 1 | ||
from site s | ||
where sa.site_id = s.id; | ||
END IF; | ||
return null; | ||
end $$; | ||
|
||
|
||
|
||
CREATE OR REPLACE FUNCTION site_aggregates_activity(i text) RETURNS integer | ||
LANGUAGE plpgsql | ||
AS $$ | ||
declare | ||
count_ integer; | ||
begin | ||
select count(*) | ||
into count_ | ||
from ( | ||
select c.creator_id from comment c | ||
inner join person u on c.creator_id = u.id | ||
inner join person pe on c.creator_id = pe.id | ||
where c.published > ('now'::timestamp - i::interval) | ||
and u.local = true | ||
and pe.bot_account = false | ||
union | ||
select p.creator_id from post p | ||
inner join person u on p.creator_id = u.id | ||
inner join person pe on p.creator_id = pe.id | ||
where p.published > ('now'::timestamp - i::interval) | ||
and u.local = true | ||
and pe.bot_account = false | ||
) a; | ||
return count_; | ||
end; | ||
$$; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Rather than doing all this with
id = 1
, the migration below should just delete site_aggregates for non-local sites, and some testing should be done to see if non-local site_aggregate rows are wrongly being inserted due to faulty triggers.