1 -- Dropping all the fast tables
3 drop view post_fast_view;
4 drop table post_aggregates_fast;
5 drop view community_fast_view;
6 drop table community_aggregates_fast;
7 drop view reply_fast_view;
8 drop view user_mention_fast_view;
9 drop view comment_fast_view;
10 drop table comment_aggregates_fast;
12 -- Re-adding all the triggers, functions, and mviews
15 create materialized view private_message_mview as select * from private_message_view;
17 create unique index idx_private_message_mview_id on private_message_mview (id);
20 -- Create the triggers
21 create or replace function refresh_private_message()
22 returns trigger language plpgsql
25 refresh materialized view concurrently private_message_mview;
29 create trigger refresh_private_message
30 after insert or update or delete or truncate
33 execute procedure refresh_private_message();
36 create or replace function refresh_user()
37 returns trigger language plpgsql
40 refresh materialized view concurrently user_mview;
41 refresh materialized view concurrently comment_aggregates_mview; -- cause of bans
42 refresh materialized view concurrently post_aggregates_mview;
46 drop trigger refresh_user on user_;
47 create trigger refresh_user
48 after insert or update or delete or truncate
51 execute procedure refresh_user();
52 drop view user_view cascade;
54 create view user_view as
67 u.send_notifications_to_email,
69 (select count(*) from post p where p.creator_id = u.id) as number_of_posts,
70 (select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
71 (select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
72 (select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
75 create materialized view user_mview as select * from user_view;
77 create unique index idx_user_mview_id on user_mview (id);
80 drop trigger refresh_community on community;
81 create trigger refresh_community
82 after insert or update or delete or truncate
85 execute procedure refresh_community();
87 create or replace function refresh_community()
88 returns trigger language plpgsql
91 refresh materialized view concurrently post_aggregates_mview;
92 refresh materialized view concurrently community_aggregates_mview;
93 refresh materialized view concurrently user_mview;
97 drop view community_aggregates_view cascade;
98 create view community_aggregates_view as
99 -- Now that there's public and private keys, you have to be explicit here
114 (select actor_id from user_ u where c.creator_id = u.id) as creator_actor_id,
115 (select local from user_ u where c.creator_id = u.id) as creator_local,
116 (select name from user_ u where c.creator_id = u.id) as creator_name,
117 (select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
118 (select name from category ct where c.category_id = ct.id) as category_name,
119 (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
120 (select count(*) from post p where p.community_id = c.id) as number_of_posts,
121 (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
122 hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
125 create materialized view community_aggregates_mview as select * from community_aggregates_view;
127 create unique index idx_community_aggregates_mview_id on community_aggregates_mview (id);
129 create view community_view as
130 with all_community as
134 from community_aggregates_view ca
140 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
142 cross join all_community ac
150 from all_community ac
153 create view community_mview as
154 with all_community as
158 from community_aggregates_mview ca
164 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
166 cross join all_community ac
174 from all_community ac
178 drop view post_aggregates_view;
181 create view post_aggregates_view as
184 (select u.banned from user_ u where p.creator_id = u.id) as banned,
185 (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
186 (select actor_id from user_ where p.creator_id = user_.id) as creator_actor_id,
187 (select local from user_ where p.creator_id = user_.id) as creator_local,
188 (select name from user_ where p.creator_id = user_.id) as creator_name,
189 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
190 (select actor_id from community where p.community_id = community.id) as community_actor_id,
191 (select local from community where p.community_id = community.id) as community_local,
192 (select name from community where p.community_id = community.id) as community_name,
193 (select removed from community c where p.community_id = c.id) as community_removed,
194 (select deleted from community c where p.community_id = c.id) as community_deleted,
195 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
196 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
197 coalesce(sum(pl.score), 0) as score,
198 count (case when pl.score = 1 then 1 else null end) as upvotes,
199 count (case when pl.score = -1 then 1 else null end) as downvotes,
200 hot_rank(coalesce(sum(pl.score) , 0),
202 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
203 else greatest(c.recent_comment_time, p.published)
208 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
209 else greatest(c.recent_comment_time, p.published)
211 ) as newest_activity_time
213 left join post_like pl on p.id = pl.post_id
216 max(published) as recent_comment_time
219 ) c on p.id = c.post_id
220 group by p.id, c.recent_comment_time;
222 create materialized view post_aggregates_mview as select * from post_aggregates_view;
224 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
226 create view post_view as
230 from post_aggregates_view pa
235 coalesce(pl.score, 0) as my_vote,
236 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
237 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
238 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
240 cross join all_post ap
241 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
255 create view post_mview as
259 from post_aggregates_mview pa
264 coalesce(pl.score, 0) as my_vote,
265 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
266 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
267 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
269 cross join all_post ap
270 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
284 drop trigger refresh_post on post;
285 create trigger refresh_post
286 after insert or update or delete or truncate
289 execute procedure refresh_post();
291 create or replace function refresh_post()
292 returns trigger language plpgsql
295 refresh materialized view concurrently post_aggregates_mview;
296 refresh materialized view concurrently user_mview;
301 -- User mention, comment, reply
302 drop view user_mention_view;
303 drop view comment_view;
304 drop view comment_aggregates_view;
306 -- reply and comment view
307 create view comment_aggregates_view as
310 (select community_id from post p where p.id = c.post_id),
311 (select co.actor_id from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_actor_id,
312 (select co.local from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_local,
313 (select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
314 (select u.banned from user_ u where c.creator_id = u.id) as banned,
315 (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
316 (select actor_id from user_ where c.creator_id = user_.id) as creator_actor_id,
317 (select local from user_ where c.creator_id = user_.id) as creator_local,
318 (select name from user_ where c.creator_id = user_.id) as creator_name,
319 (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
320 coalesce(sum(cl.score), 0) as score,
321 count (case when cl.score = 1 then 1 else null end) as upvotes,
322 count (case when cl.score = -1 then 1 else null end) as downvotes,
323 hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
325 left join comment_like cl on c.id = cl.comment_id
328 create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
330 create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
332 create view comment_view as
337 from comment_aggregates_view ca
343 coalesce(cl.score, 0) as my_vote,
344 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
345 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
347 cross join all_comment ac
348 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
361 create view comment_mview as
366 from comment_aggregates_mview ca
372 coalesce(cl.score, 0) as my_vote,
373 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
374 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
376 cross join all_comment ac
377 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
390 -- Do the reply_view referencing the comment_mview
391 create view reply_view as
395 c2.creator_id as sender_id,
396 c.creator_id as recipient_id
398 inner join comment c2 on c.id = c2.parent_id
399 where c2.creator_id != c.creator_id
400 -- Do union where post is null
404 c.creator_id as sender_id,
405 p.creator_id as recipient_id
406 from comment c, post p
407 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
410 closereply.recipient_id
411 from comment_mview cv, closereply
412 where closereply.id = cv.id
416 create view user_mention_view as
419 um.id as user_mention_id,
432 c.community_actor_id,
436 c.banned_from_community,
447 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
448 (select local from user_ u where u.id = um.recipient_id) as recipient_local
449 from user_mention um, comment_view c
450 where um.comment_id = c.id;
453 create view user_mention_mview as
458 from comment_aggregates_mview ca
463 um.id as user_mention_id,
476 ac.community_actor_id,
480 ac.banned_from_community,
488 coalesce(cl.score, 0) as my_vote,
489 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
491 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
492 (select local from user_ u where u.id = um.recipient_id) as recipient_local
494 cross join all_comment ac
495 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
496 left join user_mention um on um.comment_id = ac.id
502 um.id as user_mention_id,
515 ac.community_actor_id,
519 ac.banned_from_community,
530 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
531 (select local from user_ u where u.id = um.recipient_id) as recipient_local
533 left join user_mention um on um.comment_id = ac.id